From 9fd1ef9b92d800dd7b896b8731f5b63920276ce9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alda=20Vigd=C3=ADs=20Skarph=C3=A9=C3=B0insd=C3=B3ttir?=
 <aldavigdis@aldavigdis.is>
Date: Mon, 19 Aug 2024 03:11:39 +0200
Subject: [PATCH] Deleting errors when invoice numbers are set manually

Also fixing related i18n strings
---
 src/Hooks/WooOrderStatusChanges.php |  4 ++--
 src/Rest/OrderDKInvoice.php         | 24 +++++++++++++++++++++++-
 src/Rest/OrderInvoiceNumber.php     | 18 ++++++++++++++++--
 3 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/src/Hooks/WooOrderStatusChanges.php b/src/Hooks/WooOrderStatusChanges.php
index aa12f0f..ec4db4b 100644
--- a/src/Hooks/WooOrderStatusChanges.php
+++ b/src/Hooks/WooOrderStatusChanges.php
@@ -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'
 				)
 			);
@@ -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'
 				)
 			);
diff --git a/src/Rest/OrderDKInvoice.php b/src/Rest/OrderDKInvoice.php
index 8eee761..a25655f 100644
--- a/src/Rest/OrderDKInvoice.php
+++ b/src/Rest/OrderDKInvoice.php
@@ -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
@@ -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(
@@ -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
diff --git a/src/Rest/OrderInvoiceNumber.php b/src/Rest/OrderInvoiceNumber.php
index 4feaa0b..a5eb2d8 100644
--- a/src/Rest/OrderInvoiceNumber.php
+++ b/src/Rest/OrderInvoiceNumber.php
@@ -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(
@@ -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(
@@ -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 );
 	}