Skip to content

Commit

Permalink
Tweaks: enhance UBL Taxes default selector, 'None' Option, and Regene…
Browse files Browse the repository at this point in the history
…ration

#988
  • Loading branch information
alexmigf committed Jan 7, 2025
1 parent a1feb76 commit 4f9ec0f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 121 deletions.
29 changes: 22 additions & 7 deletions includes/Documents/OrderDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,27 +460,42 @@ 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();
}

// 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 );

Expand Down
58 changes: 2 additions & 56 deletions includes/Settings/SettingsUbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand All @@ -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 );
}
}

Expand Down
56 changes: 9 additions & 47 deletions ubl/Documents/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion ubl/Handlers/Common/TaxTotalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion ubl/Handlers/Invoice/InvoiceLineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 23 additions & 9 deletions ubl/Settings/TaxesSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<tr>';
echo '<td>' . $country . '</td>';
Expand All @@ -107,12 +107,13 @@ public function output_table_for_tax_class( $slug, $name ) {
<tr>
<th colspan="5" style="text-align: right;"><?php _e( 'Tax class default', 'woocommerce-pdf-invoices-packing-slips' ); ?>:</th>
<?php
$scheme = isset( $this->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';
?>
<th><?php echo $this->get_select_for( 'scheme', 'class', $slug, $scheme ); ?></th>
<th><?php echo $this->get_select_for( 'category', 'class', $slug, $category ); ?></th>
<th></th>
<th><?php echo $this->get_select_for( 'reason', 'class', $slug, $reason ); ?></th>
</tr>
</tfoot>
</table>
Expand All @@ -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':
Expand All @@ -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 = '<select name="wpo_wcpdf_settings_ubl_taxes[' . $type . '][' . $id . '][' . $for . ']"><option value="">' . $default_name . '</option>';
$select = '<select name="wpo_wcpdf_settings_ubl_taxes[' . $type . '][' . $id . '][' . $for . ']">';

foreach ( $defaults as $key => $value ) {
if ( 'class' === $type && 'default' === $key ) {
continue;
}

$select .= '<option ' . selected( $key, $selected, false ) . ' value="' . $key . '">' . $value . '</option>';
}

foreach ( $options as $key => $value ) {
$select .= '<option ' . selected( $key, $selected, false ) . ' value="' . $key . '">' . $value . '</option>';
}

$select .= '</select>';

return $select;
Expand Down
77 changes: 77 additions & 0 deletions wpo-ips-functions-ubl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
}
}
}

0 comments on commit 4f9ec0f

Please sign in to comment.