Skip to content

Commit

Permalink
Deleting errors when invoice numbers are set manually
Browse files Browse the repository at this point in the history
Also fixing related i18n strings
  • Loading branch information
aldavigdis committed Aug 19, 2024
1 parent 75baccd commit 9fd1ef9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Hooks/WooOrderStatusChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function maybe_send_invoice_on_payment(
) {
$wc_order->add_order_note(
__(
'An invoice was not created as the customer entered a kennitala. The invoice needs to be created manually in DK.',
'An invoice was not created as the customer entered a kennitala. The invoice needs to be created manually.',
'1984-dk-woo'
)
);
Expand All @@ -97,7 +97,7 @@ public static function maybe_send_invoice_on_payment(
) {
$wc_order->add_order_note(
__(
'An invoice was not created as the customer did not enter a kennitala. The invoice needs to be created manually in DK.',
'An invoice was not created as the customer did not enter a kennitala. The invoice needs to be created manually.',
'1984-dk-woo'
)
);
Expand Down
24 changes: 23 additions & 1 deletion src/Rest/OrderDKInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use NineteenEightyFour\NineteenEightyWoo\Rest\EmptyBodyEndpointTemplate;
use NineteenEightyFour\NineteenEightyWoo\Export\Invoice as ExportInvoice;
use NineteenEightyFour\NineteenEightyWoo\Config;
use NineteenEightyFour\NineteenEightyWoo\Helpers\Order as OrderHelper;

/**
* The Order DK Invoice endpoint class
Expand Down Expand Up @@ -59,13 +60,24 @@ public static function rest_api_callback(
): WP_REST_Response|WP_Error {
$wc_order = wc_get_order( $request['order_id'] );

if ( ! OrderHelper::can_be_invoiced( $wc_order ) ) {
$wc_order->add_order_note(
__(
'An invoice could not be created in DK for this order as a line item in this order does not have a SKU.',
'1984-dk-woo'
)
);

return new WP_REST_Response( status: 400 );
}

$invoice_number = ExportInvoice::create_in_dk(
$wc_order,
true
);

if ( ! is_string( $invoice_number ) ) {
return new WP_Error();
return new WP_REST_Response( status: 400 );
}

$wc_order->add_order_note(
Expand Down Expand Up @@ -97,6 +109,16 @@ public static function rest_api_callback(
}
}

$wc_order->delete_meta_data(
'1984_dk_woo_invoice_creation_error'
);

$wc_order->delete_meta_data(
'1984_dk_woo_invoice_creation_error_message'
);

$wc_order->save_meta_data();

return new WP_REST_Response(
$invoice_number,
201
Expand Down
18 changes: 16 additions & 2 deletions src/Rest/OrderInvoiceNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public static function rest_api_callback(
$wc_order = wc_get_order( $rest_json->order_id );

if ( $rest_json->type === 'debit' ) {
$original_invoice_number = ExportInvoice::get_dk_invoice_number( $wc_order );
$original_invoice_number = ExportInvoice::get_dk_invoice_number(
$wc_order
);

if ( $rest_json->invoice_number !== $original_invoice_number ) {
ExportInvoice::assign_dk_invoice_number(
Expand All @@ -93,7 +95,9 @@ public static function rest_api_callback(
}

if ( $rest_json->type === 'credit' ) {
$original_credit_invoice_number = ExportInvoice::get_dk_credit_invoice_number( $wc_order );
$original_credit_invoice_number = ExportInvoice::get_dk_credit_invoice_number(
$wc_order
);

if ( $rest_json->invoice_number !== $original_credit_invoice_number ) {
ExportInvoice::assign_dk_credit_invoice_number(
Expand All @@ -114,6 +118,16 @@ public static function rest_api_callback(
);
}

$wc_order->delete_meta_data(
'1984_dk_woo_invoice_creation_error'
);

$wc_order->delete_meta_data(
'1984_dk_woo_invoice_creation_error_message'
);

$wc_order->save_meta_data();

return new WP_REST_Response( status: 200 );
}

Expand Down

0 comments on commit 9fd1ef9

Please sign in to comment.