Skip to content

Commit

Permalink
Simplify project invoices.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Mar 18, 2024
1 parent 0d5f7d0 commit 95ac944
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 450 deletions.
10 changes: 10 additions & 0 deletions admin/meta-box-project-details.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
$billed_to = ( false === $value ) ? null : $value->setTime( 0, 0 );
}

$final_invoice_number = \get_post_meta( $post->ID, '_orbis_project_invoice_number', true );

?>
<table class="form-table">
<tbody>
Expand Down Expand Up @@ -266,6 +268,14 @@
<input id="orbis_project_billed_to" name="_orbis_project_billed_to" value="<?php echo esc_attr( null === $billed_to ? '' : $billed_to->format( 'Y-m-d' ) ); ?>" type="date" />
</td>
</tr>
<tr>
<th scope="row">
<label for="orbis_project_invoice_number"><?php esc_html_e( 'Final invoice number', 'orbis-projects' ); ?></label>
</th>
<td>
<input id="orbis_project_invoice_number" name="_orbis_project_invoice_number" value="<?php echo esc_attr( $final_invoice_number ); ?>" type="text" />
</td>
</tr>
</tbody>
</table>

Expand Down
139 changes: 0 additions & 139 deletions admin/meta-box-project-invoices.php

This file was deleted.

137 changes: 0 additions & 137 deletions classes/AdminProjectPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __construct( $plugin ) {
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );

add_action( 'save_post_' . self::POST_TYPE, [ $this, 'save_project' ], 10, 2 );
add_action( 'save_post_' . self::POST_TYPE, [ $this, 'save_project_invoices' ], 20 );
add_action( 'save_post_' . self::POST_TYPE, [ $this, 'save_project_sync' ], 500, 2 );
}

Expand Down Expand Up @@ -105,15 +104,6 @@ public function add_meta_boxes() {
'normal',
'high'
);

add_meta_box(
'orbis_project_invoices',
__( 'Project Invoices', 'orbis-projects' ),
[ $this, 'meta_box_invoices' ],
'orbis_project',
'normal',
'high'
);
}

/**
Expand All @@ -125,17 +115,6 @@ public function meta_box_details( $post ) {
include __DIR__ . '/../admin/meta-box-project-details.php';
}

/**
* Invoices meta box.
*
* @param mixed $post
*/
public function meta_box_invoices( $post ) {
wp_nonce_field( 'orbis_save_project_invoices', 'orbis_project_invoices_nonce' );

include __DIR__ . '/../admin/meta-box-project-invoices.php';
}

/**
* Save project.
*
Expand Down Expand Up @@ -197,10 +176,6 @@ public function save_project( $post_id, $post ) {
$is_finished_old = filter_var( get_post_meta( $post_id, '_orbis_project_is_finished', true ), FILTER_VALIDATE_BOOLEAN );
$is_finished_new = filter_var( $data['_orbis_project_is_finished'], FILTER_VALIDATE_BOOLEAN );

// Invoice number
$invoice_number_old = get_post_meta( $post_id, '_orbis_project_invoice_number', true );
$invoice_number_new = $data['_orbis_project_invoice_number'];

foreach ( $data as $key => $value ) {
if ( empty( $value ) ) {
delete_post_meta( $post_id, $key );
Expand Down Expand Up @@ -308,116 +283,4 @@ public function save_project_sync( $post_id, $post ) {

update_post_meta( $post_id, '_orbis_project_id', $orbis_id );
}

/**
* Save invoices.
*
* @param int $post_id
* @param mixed $post
*/
public function save_project_invoices( $post_id ) {
global $wpdb;
global $wp_locale;

// Check nonce.
if ( ! filter_has_var( INPUT_POST, 'orbis_project_invoices_nonce' ) ) {
return;
}

$nonce = filter_input( INPUT_POST, 'orbis_project_invoices_nonce', FILTER_SANITIZE_STRING );

if ( ! wp_verify_nonce( $nonce, 'orbis_save_project_invoices' ) ) {
return;
}

// Ok.
$project_id = get_post_meta( $post_id, '_orbis_project_id', true );

if ( empty( $project_id ) ) {
return;
}

// Final Invoice.
$final_invoice_id = filter_input( INPUT_POST, '_is_final_invoice', FILTER_SANITIZE_STRING );
$final_invoice_number = get_post_meta( $post_id, '_orbis_project_invoice_number', true );

// Invoices Data.
$invoices_data = filter_input( INPUT_POST, '_orbis_project_invoices', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );

foreach ( $invoices_data as $id => $invoice_data ) {
$invoice_data = wp_parse_args(
$invoice_data,
[
'invoice_number' => null,
'amount' => null,
'seconds' => null,
'create_date' => null,
'delete' => false,
]
);

$date = $invoice_data['date'];
$amount = filter_var(
$invoice_data['amount'],
FILTER_VALIDATE_FLOAT,
[
'flags' => FILTER_FLAG_ALLOW_THOUSAND,
'options' => [
'decimal' => $wp_locale->number_format['decimal_point'],
],
]
);
$seconds = orbis_parse_time( $invoice_data['seconds'] );
$number = $invoice_data['number'];
$delete = $invoice_data['delete'];

if ( $id == $final_invoice_id ) { // WPCS: loose comparison ok.
$final_invoice_number = $number;
}

$data = [
'invoice_number' => $number,
'amount' => $amount,
'seconds' => $seconds,
'create_date' => $date,
];

$format = [
'invoice_number' => '%s',
'amount' => '%f',
'seconds' => '%d',
'create_date' => '%s',
];

if ( 'new' === $id && filter_has_var( INPUT_POST, 'orbis_projects_invoice_add' ) ) {
$data['project_id'] = $project_id;
$format['project_id'] = '%d';

$data['user_id'] = get_current_user_id();
$format['user_id'] = '%d';

$wpdb->insert( $wpdb->orbis_invoices, $data, $format );
} elseif ( $delete ) {
if ( $number == $final_invoice_number ) { // WPCS: loose comparison ok.
$final_invoice_number = null;
}

$result = $wpdb->delete(
$wpdb->orbis_projects_invoices,
[ 'id' => $id ],
[ 'id' => '%d' ]
);
} else {
$result = $wpdb->update(
$wpdb->orbis_invoices,
$data,
[ 'id' => $id ],
$format,
[ 'id' => '%d' ]
);
}
}

update_post_meta( $post_id, '_orbis_project_invoice_number', $final_invoice_number );
}
}
Binary file modified languages/orbis-projects-nl_NL.mo
Binary file not shown.
Loading

0 comments on commit 95ac944

Please sign in to comment.