diff --git a/includes/Documents/OrderDocument.php b/includes/Documents/OrderDocument.php index 743c8cc5..2da2ddaf 100644 --- a/includes/Documents/OrderDocument.php +++ b/includes/Documents/OrderDocument.php @@ -460,12 +460,13 @@ public function delete( $order = null ) { public function regenerate( $order = null, $data = null ) { $order = empty( $order ) ? $this->order : $order; + if ( empty( $order ) ) { - return; //Nothing to update + return; } // pass data to setter functions - if( ! empty( $data ) ) { + if ( ! empty( $data ) ) { $this->set_data( $data, $order ); $this->save(); } @@ -473,14 +474,28 @@ public function regenerate( $order = null, $data = null ) { // save settings $this->save_settings( true ); - //Add order note $parent_order = $refund_id = false; + // If credit note - if ( $this->get_type() == 'credit-note' ) { - $refund_id = $order->get_id(); + if ( 'credit-note' === $this->get_type() ) { + $refund_id = $order->get_id(); $parent_order = wc_get_order( $order->get_parent_id() ); - } /*translators: 1. credit note title, 2. refund id */ - $note = $refund_id ? sprintf( __( '%1$s (refund #%2$s) was regenerated.', 'woocommerce-pdf-invoices-packing-slips' ), ucfirst( $this->get_title() ), $refund_id ) : sprintf( __( '%s was regenerated', 'woocommerce-pdf-invoices-packing-slips' ), ucfirst( $this->get_title() ) ); + + // UBL + } else { + wpo_ips_ubl_save_order_taxes( $order ); + } + + $note = $refund_id ? sprintf( + /* translators: 1. credit note title, 2. refund id */ + __( '%1$s (refund #%2$s) was regenerated.', 'woocommerce-pdf-invoices-packing-slips' ), ucfirst( $this->get_title() ), + $refund_id + ) : sprintf( + /* translators: 1. document title */ + __( '%s was regenerated', 'woocommerce-pdf-invoices-packing-slips' ), + ucfirst( $this->get_title() ) + ); + $note = wp_kses( $note, 'strip' ); $parent_order ? $parent_order->add_order_note( $note ) : $order->add_order_note( $note ); diff --git a/includes/Settings/SettingsUbl.php b/includes/Settings/SettingsUbl.php index 156783e6..8f2507bf 100644 --- a/includes/Settings/SettingsUbl.php +++ b/includes/Settings/SettingsUbl.php @@ -86,7 +86,7 @@ public function save_taxes_on_order_totals( $and_taxes, $order ) { // it seems $and taxes is mostly false, meaning taxes are calculated separately, // but we still update just in case anything changed if ( ! empty( $order ) ) { - $this->save_order_taxes( $order ); + wpo_ips_ubl_save_order_taxes( $order ); } } @@ -96,61 +96,7 @@ public function save_taxes_on_checkout( $order_id, $posted_data, $order ) { } if ( $order ) { - $this->save_order_taxes( $order ); - } - } - - public function save_order_taxes( $order ) { - foreach ( $order->get_taxes() as $item_id => $tax_item ) { - if ( is_a( $tax_item, '\WC_Order_Item_Tax' ) && is_callable( array( $tax_item, 'get_rate_id' ) ) ) { - // get tax rate id from item - $tax_rate_id = $tax_item->get_rate_id(); - - // read tax rate data from db - if ( class_exists( '\WC_TAX' ) && is_callable( array( '\WC_TAX', '_get_tax_rate' ) ) ) { - $tax_rate = \WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT ); - - if ( ! empty( $tax_rate ) && is_numeric( $tax_rate->tax_rate ) ) { - // store percentage in tax item meta - wc_update_order_item_meta( $item_id, '_wcpdf_rate_percentage', $tax_rate->tax_rate ); - - $ubl_tax_settings = get_option( 'wpo_wcpdf_settings_ubl_taxes' ); - - $category = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ]['category'] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ]['category'] : ''; - $scheme = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ]['scheme'] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ]['scheme'] : ''; - $reason = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ]['reason'] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ]['reason'] : ''; - $tax_rate_class = $tax_rate->tax_rate_class; - - if ( empty( $tax_rate_class ) ) { - $tax_rate_class = 'standard'; - } - - if ( empty( $category ) ) { - $category = isset( $ubl_tax_settings['class'][ $tax_rate_class ]['category'] ) ? $ubl_tax_settings['class'][ $tax_rate_class ]['category'] : ''; - } - - if ( empty( $scheme ) ) { - $scheme = isset( $ubl_tax_settings['class'][ $tax_rate_class ]['scheme'] ) ? $ubl_tax_settings['class'][ $tax_rate_class ]['scheme'] : ''; - } - - if ( empty( $reason ) ) { - $reason = isset( $ubl_tax_settings['class'][ $tax_rate_class ]['reason'] ) ? $ubl_tax_settings['class'][ $tax_rate_class ]['reason'] : ''; - } - - if ( ! empty( $category ) ) { - wc_update_order_item_meta( $item_id, '_wcpdf_ubl_tax_category', $category ); - } - - if ( ! empty( $scheme ) ) { - wc_update_order_item_meta( $item_id, '_wcpdf_ubl_tax_scheme', $scheme ); - } - - if ( ! empty( $reason ) ) { - wc_update_order_item_meta( $item_id, '_wcpdf_ubl_tax_reason', $reason ); - } - } - } - } + wpo_ips_ubl_save_order_taxes( $order ); } } diff --git a/ubl/Documents/Document.php b/ubl/Documents/Document.php index 6a265ae6..68626f8c 100644 --- a/ubl/Documents/Document.php +++ b/ubl/Documents/Document.php @@ -96,31 +96,28 @@ public function get_tax_rates() { $percentage = wc_get_order_item_meta( $tax_item_key, '_wcpdf_rate_percentage', true ); } - $rate_id = absint( $tax_item['rate_id'] ); + $tax_rate_id = absint( $tax_item['rate_id'] ); if ( ! is_numeric( $percentage ) ) { - $percentage = $this->get_percentage_from_fallback( $tax_data, $rate_id ); + $percentage = $this->get_percentage_from_fallback( $tax_data, $tax_rate_id ); wc_update_order_item_meta( $tax_item_key, '_wcpdf_rate_percentage', $percentage ); } - $fields = array( - 'category' => '_wcpdf_ubl_tax_category', - 'scheme' => '_wcpdf_ubl_tax_scheme', - 'reason' => '_wcpdf_ubl_tax_reason', - ); + $fields = array( 'category', 'scheme', 'reason' ); - foreach ( $fields as $key => $meta_key ) { - $value = wc_get_order_item_meta( $tax_item_key, $meta_key, true ); + foreach ( $fields as $field ) { + $meta_key = '_wcpdf_ubl_tax_' . $field; + $value = wc_get_order_item_meta( $tax_item_key, $meta_key, true ); - if ( empty( $value ) || ! $use_historical_settings ) { - $value = $this->get_tax_data_from_fallback( $key, $rate_id ); + if ( empty( $value ) || 'default' === $value || ! $use_historical_settings ) { + $value = wpo_ips_ubl_get_tax_data_from_fallback( $field, $tax_rate_id ); } if ( $use_historical_settings ) { wc_update_order_item_meta( $tax_item_key, $meta_key, $value ); } - ${$key} = $value; + ${$field} = $value; } } @@ -163,41 +160,6 @@ public function get_percentage_from_fallback( array $tax_data, int $rate_id ) { } return $percentage; - } - - /** - * Get tax data from fallback - * - * @param string $key Can be category, scheme, or reason - * @param int $rate_id The tax rate ID - * @return string - */ - public function get_tax_data_from_fallback( string $key, int $rate_id ): string { - $result = ''; - - if ( ! in_array( $key, array( 'category', 'scheme', 'reason' ) ) ) { - return $result; - } - - if ( class_exists( '\WC_TAX' ) && is_callable( array( '\WC_TAX', '_get_tax_rate' ) ) ) { - $tax_rate = \WC_Tax::_get_tax_rate( $rate_id, OBJECT ); - - if ( ! empty( $tax_rate ) && is_numeric( $tax_rate->tax_rate ) ) { - $ubl_tax_settings = get_option( 'wpo_wcpdf_settings_ubl_taxes', array() ); - $result = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $key ] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $key ] : ''; - $tax_rate_class = $tax_rate->tax_rate_class; - - if ( empty( $tax_rate_class ) ) { - $tax_rate_class = 'standard'; - } - - if ( empty( $result ) ) { - $result = isset( $ubl_tax_settings['class'][ $tax_rate_class ][ $key ] ) ? $ubl_tax_settings['class'][ $tax_rate_class ][ $key ] : ''; - } - } - } - - return $result; } } diff --git a/ubl/Handlers/Common/TaxTotalHandler.php b/ubl/Handlers/Common/TaxTotalHandler.php index 8bfa4304..5ea225ff 100644 --- a/ubl/Handlers/Common/TaxTotalHandler.php +++ b/ubl/Handlers/Common/TaxTotalHandler.php @@ -31,7 +31,7 @@ public function handle( $data, $options = array() ) { ); // Add TaxExemptionReason only if it's not empty - if ( ! empty( $item['reason'] ) ) { + if ( ! empty( $item['reason'] ) && 'none' !== $item['reason'] ) { $reasonKey = $item['reason']; $reason = ! empty( $taxReasons[ $reasonKey ] ) ? $taxReasons[ $reasonKey ] : $reasonKey; $taxCategory[] = array( diff --git a/ubl/Handlers/Invoice/InvoiceLineHandler.php b/ubl/Handlers/Invoice/InvoiceLineHandler.php index 40ff8232..a254c6ad 100644 --- a/ubl/Handlers/Invoice/InvoiceLineHandler.php +++ b/ubl/Handlers/Invoice/InvoiceLineHandler.php @@ -51,7 +51,7 @@ public function handle( $data, $options = array() ) { ); // Add TaxExemptionReason only if it's not empty - if ( ! empty( $taxOrderData['reason'] ) ) { + if ( ! empty( $taxOrderData['reason'] ) && 'none' !== $taxOrderData['reason'] ) { $reasonKey = $taxOrderData['reason']; $reason = ! empty( $taxReasons[ $reasonKey ] ) ? $taxReasons[ $reasonKey ] : $reasonKey; $taxCategory[] = array( diff --git a/ubl/Settings/TaxesSettings.php b/ubl/Settings/TaxesSettings.php index b87268b8..08675f7f 100644 --- a/ubl/Settings/TaxesSettings.php +++ b/ubl/Settings/TaxesSettings.php @@ -83,9 +83,9 @@ public function output_table_for_tax_class( $slug, $name ) { $state = empty( $result->tax_rate_state ) ? '*' : $result->tax_rate_state; $postcode = empty( $postcode ) ? '*' : implode( '; ', $postcode ); $city = empty( $city ) ? '*' : implode( '; ', $city ); - $scheme = isset( $this->settings['rate'][ $result->tax_rate_id ]['scheme'] ) ? $this->settings['rate'][ $result->tax_rate_id ]['scheme'] : ''; - $category = isset( $this->settings['rate'][ $result->tax_rate_id ]['category'] ) ? $this->settings['rate'][ $result->tax_rate_id ]['category'] : ''; - $reason = isset( $this->settings['rate'][ $result->tax_rate_id ]['reason'] ) ? $this->settings['rate'][ $result->tax_rate_id ]['reason'] : ''; + $scheme = isset( $this->settings['rate'][ $result->tax_rate_id ]['scheme'] ) ? $this->settings['rate'][ $result->tax_rate_id ]['scheme'] : 'default'; + $category = isset( $this->settings['rate'][ $result->tax_rate_id ]['category'] ) ? $this->settings['rate'][ $result->tax_rate_id ]['category'] : 'default'; + $reason = isset( $this->settings['rate'][ $result->tax_rate_id ]['reason'] ) ? $this->settings['rate'][ $result->tax_rate_id ]['reason'] : 'default'; echo ''; echo '' . $country . ''; @@ -107,12 +107,13 @@ public function output_table_for_tax_class( $slug, $name ) { : settings['class'][ $slug ]['scheme'] ) ? $this->settings['class'][ $slug ]['scheme'] : ''; - $category = isset( $this->settings['class'][ $slug ]['category'] ) ? $this->settings['class'][ $slug ]['category'] : ''; + $scheme = isset( $this->settings['class'][ $slug ]['scheme'] ) ? $this->settings['class'][ $slug ]['scheme'] : 'default'; + $category = isset( $this->settings['class'][ $slug ]['category'] ) ? $this->settings['class'][ $slug ]['category'] : 'default'; + $reason = isset( $this->settings['class'][ $slug ]['reason'] ) ? $this->settings['class'][ $slug ]['reason'] : 'default'; ?> get_select_for( 'scheme', 'class', $slug, $scheme ); ?> get_select_for( 'category', 'class', $slug, $category ); ?> - + get_select_for( 'reason', 'class', $slug, $reason ); ?> @@ -130,7 +131,9 @@ public function output_table_for_tax_class( $slug, $name ) { * @return string */ public function get_select_for( string $for, string $type, string $id, string $selected ): string { - $default_name = ( 'reason' !== $for ) ? __( 'Default', 'woocommerce-pdf-invoices-packing-slips' ) : __( 'None', 'woocommerce-pdf-invoices-packing-slips' ); + $defaults = array( + 'default' => __( 'Default', 'woocommerce-pdf-invoices-packing-slips' ), + ); switch ( $for ) { case 'scheme': @@ -140,16 +143,27 @@ public function get_select_for( string $for, string $type, string $id, string $s $options = $this->get_available_categories(); break; case 'reason': - $options = self::get_available_reasons(); + $defaults['none'] = __( 'None', 'woocommerce-pdf-invoices-packing-slips' ); + $options = self::get_available_reasons(); break; default: $options = array(); } - $select = ''; + + foreach ( $defaults as $key => $value ) { + if ( 'class' === $type && 'default' === $key ) { + continue; + } + + $select .= ''; + } + foreach ( $options as $key => $value ) { $select .= ''; } + $select .= ''; return $select; diff --git a/wpo-ips-functions-ubl.php b/wpo-ips-functions-ubl.php index def68fe8..a93de787 100644 --- a/wpo-ips-functions-ubl.php +++ b/wpo-ips-functions-ubl.php @@ -21,3 +21,80 @@ function wpo_ips_ubl_sanitize_string( string $string ): string { $string = wp_strip_all_tags( $string ); return htmlspecialchars_decode( $string, ENT_QUOTES ); } + +/** + * Get UBL tax data from fallback + * + * @param string $key Can be category, scheme, or reason + * @param int $rate_id The tax rate ID + * @return string + */ +function wpo_ips_ubl_get_tax_data_from_fallback( string $key, int $rate_id ): string { + $result = ''; + + if ( ! in_array( $key, array( 'category', 'scheme', 'reason' ) ) ) { + return $result; + } + + if ( class_exists( '\WC_TAX' ) && is_callable( array( '\WC_TAX', '_get_tax_rate' ) ) ) { + $tax_rate = \WC_Tax::_get_tax_rate( $rate_id, OBJECT ); + + if ( ! empty( $tax_rate ) && is_numeric( $tax_rate->tax_rate ) ) { + $ubl_tax_settings = get_option( 'wpo_wcpdf_settings_ubl_taxes', array() ); + $result = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $key ] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $key ] : ''; + $tax_rate_class = $tax_rate->tax_rate_class; + + if ( empty( $tax_rate_class ) ) { + $tax_rate_class = 'standard'; + } + + if ( empty( $result ) || 'default' === $result ) { + $result = isset( $ubl_tax_settings['class'][ $tax_rate_class ][ $key ] ) ? $ubl_tax_settings['class'][ $tax_rate_class ][ $key ] : ''; + } + } + } + + return $result; +} + +/** + * Save UBL order taxes + * + * @param \WC_Abstract_Order $order + * @return void + */ +function wpo_ips_ubl_save_order_taxes( \WC_Abstract_Order $order ): void { + if ( empty( $order ) ) { + return; + } + + foreach ( $order->get_taxes() as $item_id => $tax_item ) { + if ( is_a( $tax_item, '\WC_Order_Item_Tax' ) && is_callable( array( $tax_item, 'get_rate_id' ) ) ) { + // get tax rate id from item + $tax_rate_id = $tax_item->get_rate_id(); + + // read tax rate data from db + if ( class_exists( '\WC_TAX' ) && is_callable( array( '\WC_TAX', '_get_tax_rate' ) ) ) { + $tax_rate = \WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT ); + + if ( ! empty( $tax_rate ) && is_numeric( $tax_rate->tax_rate ) ) { + // store percentage in tax item meta + wc_update_order_item_meta( $item_id, '_wcpdf_rate_percentage', $tax_rate->tax_rate ); + + $ubl_tax_settings = get_option( 'wpo_wcpdf_settings_ubl_taxes', array() ); + $tax_fields = array( 'category', 'scheme', 'reason' ); + + foreach ( $tax_fields as $field ) { + $value = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $field ] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $field ] : ''; + + if ( empty( $value ) || 'default' === $value ) { + $value = wpo_ips_ubl_get_tax_data_from_fallback( $field, $tax_rate_id ); + } + + wc_update_order_item_meta( $item_id, '_wcpdf_ubl_tax_' . $field, $value ); + } + } + } + } + } +} \ No newline at end of file