From d26c826d33828d10b18a16bf538040b4217da71b Mon Sep 17 00:00:00 2001 From: Alexandre Faustino Date: Tue, 31 Dec 2024 09:04:35 +0000 Subject: [PATCH] New: Add `cac:Price` support to UBL --- ubl/Handlers/Invoice/InvoiceLineHandler.php | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ubl/Handlers/Invoice/InvoiceLineHandler.php b/ubl/Handlers/Invoice/InvoiceLineHandler.php index 03d3fedf..4e03a7f4 100644 --- a/ubl/Handlers/Invoice/InvoiceLineHandler.php +++ b/ubl/Handlers/Invoice/InvoiceLineHandler.php @@ -140,6 +140,18 @@ public function handle( $data, $options = array() ) { ), ), ), + array( + 'name' => 'cac:Price', + 'value' => array( + array( + 'name' => 'cbc:PriceAmount', + 'value' => round( $this->get_item_unit_price( $item ), 2 ), + 'attributes' => array( + 'currencyID' => $this->document->order->get_currency(), + ), + ), + ), + ), ), ); @@ -152,4 +164,20 @@ public function handle( $data, $options = array() ) { return $data; } + /** + * Get the unit price of an item + * + * @param WC_Order_Item $item + * @return int|float + */ + private function get_item_unit_price( $item ) { + if ( is_a( $item, 'WC_Order_Item_Product' ) ) { + return $item->get_subtotal() / $item->get_quantity(); + } elseif ( is_a( $item, 'WC_Order_Item_Shipping' ) || is_a( $item, 'WC_Order_Item_Fee' ) ) { + return $item->get_total(); + } else { + return 0; + } + } + }