Changeset 3259100
- Timestamp:
- 03/20/2025 11:50:54 AM (13 months ago)
- Location:
- ceretax
- Files:
-
- 25 added
- 5 edited
-
tags/1.4.0 (added)
-
tags/1.4.0/assets (added)
-
tags/1.4.0/assets/css (added)
-
tags/1.4.0/assets/css/admin.css (added)
-
tags/1.4.0/assets/js (added)
-
tags/1.4.0/assets/js/admin.js (added)
-
tags/1.4.0/assets/js/front.js (added)
-
tags/1.4.0/ceretax.php (added)
-
tags/1.4.0/changelog.txt (added)
-
tags/1.4.0/inc (added)
-
tags/1.4.0/inc/admin (added)
-
tags/1.4.0/inc/admin/class-cwafc-admin-action.php (added)
-
tags/1.4.0/inc/admin/class-cwafc-admin-filter.php (added)
-
tags/1.4.0/inc/admin/class-cwafc-admin.php (added)
-
tags/1.4.0/inc/class-cwafc.php (added)
-
tags/1.4.0/inc/front (added)
-
tags/1.4.0/inc/front/class-cwafc-front-action.php (added)
-
tags/1.4.0/inc/front/class-cwafc-front-filter.php (added)
-
tags/1.4.0/inc/front/class-cwafc-front.php (added)
-
tags/1.4.0/languages (added)
-
tags/1.4.0/languages/ceretax.pot (added)
-
tags/1.4.0/readme.txt (added)
-
tags/1.4.0/woocommerce (added)
-
tags/1.4.0/woocommerce/cart (added)
-
tags/1.4.0/woocommerce/cart/shipping-calculator.php (added)
-
trunk/ceretax.php (modified) (1 diff)
-
trunk/changelog.txt (modified) (1 diff)
-
trunk/inc/admin/class-cwafc-admin-action.php (modified) (5 diffs)
-
trunk/inc/class-cwafc.php (modified) (40 diffs)
-
trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ceretax/trunk/ceretax.php
r3248052 r3259100 4 4 * Plugin URI: https://wordpress.org/plugins/ceretax/ 5 5 * Description: Simplify sales tax complexity with CereTax for WooCommerce. 6 * Version: 1. 3.86 * Version: 1.4.0 7 7 * Author: CereTax, Inc. 8 8 * Author URI: https://www.ceretax.com/ -
ceretax/trunk/changelog.txt
r3248052 r3259100 32 32 33 33 2025-02-19 - version 1.3.6 34 * fix - * fix -If fee name changed from admin option, destroy order calculation issue34 * fix - If fee name changed from admin option, destroy order calculation issue 35 35 36 36 2025-02-24 - version 1.3.7 37 37 * fix - If the tax showing zero in order calculation issue 38 38 39 2025-0 2-27 - version 1.3.840 * fix - Restore to 1.3.6 functionality39 2025-03-08 - version 1.3.9 40 * fix - Postcode in lower case causing tax calculation issue 41 41 42 2025-03-19 - version 1.4.0 43 * add - Disable CereTax for refund orders -
ceretax/trunk/inc/admin/class-cwafc-admin-action.php
r3248052 r3259100 42 42 add_action( 'created_product_cat', array( $this, 'action__cwafc_save_custom_field_in_product_category' ), 10, 2 ); 43 43 add_action( 'edited_product_cat', array( $this, 'action__cwafc_save_custom_field_in_product_category' ), 10, 2 ); 44 // Add admin order line item column. 45 add_action( 'woocommerce_admin_order_item_headers', array( $this, 'action__cwafc_woocommerce_admin_order_item_headers' ), 20 ); 46 // Populate the new column with custom data. 47 add_action( 'woocommerce_admin_order_item_values', array( $this, 'action__cwafc_woocommerce_admin_order_item_values' ), 10, 3 ); 48 // Add admin order style and script. 44 49 add_action( 'admin_footer', array( $this, 'action__cwafc_admin_footer_function' ), 20 ); 45 50 } … … 152 157 153 158 array( 159 'name' => __( 'Disable CereTax For Refund Orders', 'ceretax' ), 160 'type' => 'checkbox', 161 'id' => CWAFC_PREFIX . '_cere_tax_refund_disable', 162 'class' => 'cere_tax_checkbox', 163 'default' => 'no', 164 ), 165 166 array( 154 167 'name' => __( 'Post finalized transactions to CereTax', 'ceretax' ), 155 168 'type' => 'checkbox', … … 238 251 'autoload' => false, 239 252 'default' => 'Tax', 253 ), 254 255 array( 256 'name' => __( 'Line Item Tax Column Name', 'ceretax' ), 257 'type' => 'text', 258 'desc' => __( 'Enter the admin order line item tax column name. If you are using a multilingual plugin do not make any changes here.', 'ceretax' ), 259 'id' => CWAFC_PREFIX . '_cere_tax_line_item_column_name', 260 'desc_tip' => true, 261 'autoload' => false, 262 'default' => 'Tax Total', 240 263 ), 241 264 … … 384 407 385 408 /** 386 * Admin footer s tyle409 * Admin footer script & style 387 410 * 388 411 * @return void 389 412 */ 390 413 public function action__cwafc_admin_footer_function() { 391 414 $order_id = null; 392 415 if ( ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) || ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) ) { 393 416 if ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) { … … 412 435 } 413 436 if ( ! empty( $order_id ) && ! empty( $ceretax_data ) && $fee_amount > 0 ) { ?> 414 <style> 415 /* Hide the 3rd tr of .wc-order-totals if .wc-used-coupons is found */ 416 .wc-order-data-row:has(.wc-used-coupons) .wc-order-totals:first-of-type tr:nth-of-type(3) { 417 display: none !important; 437 <style> 438 /* Hide the 3rd tr of .wc-order-totals if .wc-used-coupons is found */ 439 .wc-order-data-row:has(.wc-used-coupons) .wc-order-totals:first-of-type tr:nth-of-type(3) { 440 display: none !important; 441 } 442 443 /* Hide the 2nd tr of .wc-order-totals if .wc-used-coupons is not found */ 444 .wc-order-data-row:not(:has(.wc-used-coupons)) .wc-order-totals:first-of-type tr:nth-of-type(2) { 445 display: none !important; 446 } 447 448 </style> 449 <?php } if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { ?> 450 <style> 451 /* Hide Tax column for ceretax order item list */ 452 .woocommerce_order_items tr th.line_tax, 453 .woocommerce_order_items tr td.line_tax { 454 display: none !important; 455 } 456 457 </style> 458 <?php 459 } 460 461 // Refund order script. 462 if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { ?> 463 <script> 464 jQuery(function($) { 465 jQuery( '#woocommerce-order-items' ).on( 'change', 'input.refund_order_item_qty', function() { 466 var $row = $( this ).closest( 'tr.item' ); 467 var qty = $row.find( 'input.quantity' ).val(); 468 var refund_qty = $( this ).val(); 469 var ceretax_line_total = $( 'input.ceretax_line_total', $row ); 470 var line_total = $( 'input.line_total', $row ); 471 var refund_line_total = $( 'input.refund_line_total', $row ); 472 473 var unit_line_ceretax_total = accounting.unformat( ceretax_line_total.attr( 'data-line-tax' ), woocommerce_admin.mon_decimal_point ) / qty; 474 ceretax_line_total.val( 475 parseFloat( accounting.formatNumber( unit_line_ceretax_total * refund_qty, 2, '' ) ) 476 .toString() 477 .replace( '.', woocommerce_admin.mon_decimal_point ) 478 ).trigger( 'change' ); 479 480 // Totals 481 var unit_total = accounting.unformat( line_total.attr( 'data-total' ), woocommerce_admin.mon_decimal_point ) / qty; 482 483 refund_line_total.val( 484 parseFloat( accounting.formatNumber( unit_total * refund_qty, woocommerce_admin_meta_boxes.rounding_precision, '' ) ) 485 .toString() 486 .replace( '.', woocommerce_admin.mon_decimal_point ) 487 ).trigger( 'change' ); 488 489 $( this ).trigger( 'refund_quantity_changed' ); 490 491 }); 492 $( '#woocommerce-order-items' ).on( 'change', '.refund input.ceretax_line_total', function() { 493 494 var refund_amount = 0; 495 var $items = $( '.woocommerce_order_items' ).find( 'tr.item, tr.fee, tr.shipping' ); 496 var round_at_subtotal = 'yes' === woocommerce_admin_meta_boxes.round_at_subtotal; 497 498 // Restrict value based on data-line-tax 499 var maxValue = parseFloat( $( this ).data('line-tax') ) || 0; 500 var inputValue = parseFloat( $( this ).val() ) || 0; 501 502 if ( inputValue > maxValue ) { 503 alert( 'You cannot refund the line item tax exceeding the allowed limit of ' + maxValue + '.' ); 504 $( this ).val( maxValue ); 505 } 506 507 $items.each(function() { 508 var $row = $( this ); 509 var refund_cost_fields = $row.find( '.refund input:not(.refund_order_item_qty)' ); 510 511 refund_cost_fields.each(function( index, el ) { 512 var field_amount = accounting.unformat( $( el ).val() || 0, woocommerce_admin.mon_decimal_point ); 513 refund_amount += parseFloat( round_at_subtotal ? 514 field_amount : 515 accounting.formatNumber( field_amount, woocommerce_admin_meta_boxes.currency_format_num_decimals, '' ) ); 516 }); 517 }); 518 519 $( '#refund_amount' ).val( 520 accounting.formatNumber( 521 refund_amount, 522 woocommerce_admin_meta_boxes.currency_format_num_decimals, 523 '', 524 woocommerce_admin.mon_decimal_point 525 ) ).trigger( 'change' ); 526 527 }); 528 }); 529 </script> 530 <?php } 531 532 } 533 534 /** 535 * Add admin order line item column header. 536 * 537 * @param object $order Order object. 538 * @return void 539 */ 540 public function action__cwafc_woocommerce_admin_order_item_headers( $order ) { 541 $ceretax_data_lineitem = get_post_meta( $order->get_id(), 'ceretax_data_lineitem', true ); 542 if ( ! empty( $ceretax_data_lineitem ) ) { 543 $cere_tax_line_item_column_name = get_option( CWAFC_PREFIX . '_cere_tax_line_item_column_name', 'Tax Total' ); 544 echo '<th class="ceretax_line_heading" width="7%">' . esc_html( $cere_tax_line_item_column_name ) . '</th>'; 545 } 546 } 547 548 /** 549 * Add admin order line item column header. 550 * 551 * @param object $product Product object. 552 * @param object $item Product item object. 553 * @param string $item_id Order object. 554 * @return void 555 */ 556 public function action__cwafc_woocommerce_admin_order_item_values( $product, $item, $item_id ) { 557 558 $order_id = null; 559 if ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) { 560 $order_id = $_GET['post']; 561 } else if ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) { 562 $order_id = $_GET['id']; 563 } else { 564 $order_id = null; 565 } 566 567 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 568 $order = wc_get_order( $order_id ); 569 $ceretax_data_lineitem = json_decode( get_post_meta( $order_id, 'ceretax_data_lineitem', true ) ); 570 $ceretax_data = json_decode( get_post_meta( $order_id, 'ceretax_data', true ) ); 571 572 if ( ! empty( $ceretax_data_lineitem ) && ! empty( $ceretax_data ) && ! empty( $order ) ) { 573 574 if ( ! empty( $product ) ) { 575 576 // $refunded_qty = -1 * $order->get_total_refunded_for_item( $item_id ); 577 578 echo '<td class="ceretax_line_value" width="7%">'; 579 if ( is_object( $ceretax_data_lineitem ) && is_object( $ceretax_data ) ) { 580 $ceretax_items_data = $ceretax_data_lineitem->invoice->lineItems; 581 $ceretax_data = $ceretax_data->invoice->lineItems; 582 if ( ! empty( $ceretax_items_data ) && ! empty( $ceretax_data ) ) { 583 $product_id = $product->get_id(); 584 $filtered = array_filter( $ceretax_items_data, function ( $item ) use ( $product_id ) { 585 return isset( $item->itemNumber ) && $item->itemNumber == $product_id; 586 }); 587 588 $refunded_filtered = array_filter( $ceretax_data, function ( $refunded_item ) use ( $product_id ) { 589 return isset( $refunded_item->itemNumber ) && $refunded_item->itemNumber == $product_id; 590 }); 591 $total_line_tax = ! empty( $filtered ) ? reset( $filtered )->totalTaxLine : null; 592 $total_line_refunded_tax = ! empty( $refunded_filtered ) ? reset( $refunded_filtered )->totalTaxLine : null; 593 $ceretax_refunded_line_item_tax = $item->get_meta( '_ceretax_refunded_line_item_tax' ); 594 echo '<div class="view">'; 595 echo wc_price( $total_line_tax, array( 'currency' => $order->get_currency() ) ); 596 if ( is_array( $order->get_refunds() ) && count( $order->get_refunds() ) > 0 && ! empty( $ceretax_refunded_line_item_tax ) ) { 597 echo '<small class="refunded">' . wc_price( $ceretax_refunded_line_item_tax, array( 'currency' => $order->get_currency() ) ) . '</small>'; 598 } 599 echo '</div>'; 600 601 echo '<div class="refund" style="display: none;"> 602 <input type="text" name="ceretax_line_total[' . absint( $item_id ) . ']" placeholder="' . esc_attr( wc_format_localized_price( 0 ) ) . '" class="refund_line_tax ceretax_line_total wc_input_price" size="4" data-line-tax="' . esc_attr( wc_format_localized_price( $total_line_tax ) ) . '" /> 603 </div>'; 604 } 605 } 606 echo '</td>'; 607 608 } elseif ( $item instanceof WC_Order_Item_Fee && $item->get_name() === $cere_tax_name ) { 609 // } elseif ( isset( $item['name'] ) && $item['name'] === $cere_tax_name ) { 610 echo '<td class="ceretax_line_value" width="7%"></td>'; 611 } else { 612 echo '<td class="ceretax_line_value" width="7%">'; 613 // echo '<div class="refund" style="display: none;"> 614 // <input type="text" name="ceretax_line_total[' . absint( $item_id ) . ']" placeholder="' . esc_attr( wc_format_localized_price( 0 ) ) . '" class="refund_line_tax ceretax_line_total wc_input_price" size="4" /> 615 // </div>'; 616 echo '</td>'; 418 617 } 419 420 /* Hide the 2nd tr of .wc-order-totals if .wc-used-coupons is not found */421 .wc-order-data-row:not(:has(.wc-used-coupons)) .wc-order-totals:first-of-type tr:nth-of-type(2) {422 display: none !important;423 }424 425 </style>426 <?php } if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { ?>427 <style>428 /* Hide Tax column for ceretax order item list */429 .woocommerce_order_items tr th.line_tax,430 .woocommerce_order_items tr td.line_tax {431 display: none !important;432 }433 434 </style>435 <?php436 618 } 437 619 } -
ceretax/trunk/inc/class-cwafc.php
r3248052 r3259100 66 66 // Plugin activation hook. 67 67 register_activation_hook( CWAFC_FILE, array( $this, 'action__cwafc_plugin_activation' ) ); 68 // Init hook.69 // add_action( 'init', array( $this, 'action__cwafc_init' ) );70 68 // Make plugin HPOS compatible. 71 69 add_action( 'before_woocommerce_init', array( $this, 'action__cwafc_before_woocommerce_init' ) ); … … 104 102 // Modify the fee name dynamically when orders are loaded. 105 103 add_filter( 'woocommerce_order_get_items', array( $this, 'filter__cwafc_modify_fee_name_dynamically' ), 10, 2 ); 104 // Update tax calculation on subscription. 105 add_action( 'woocommerce_checkout_create_subscription', array( $this, 'action__cwafc_update_tax_calculation_for_subscription' ), 10 ); 106 // Clear Avatax from Ceretax renewal orders. 107 add_filter( 'wcs_new_order_created', array( $this, 'action__cwafc_clear_avatx_new_order_created' ), 10, 3 ); 106 108 } 107 109 … … 115 117 116 118 $ceretax_data = get_post_meta( $order_id, 'ceretax_data', true ); 119 120 // If it's empty, return early. 117 121 if ( empty( $ceretax_data ) ) { 118 122 return; … … 120 124 $ceretax_data = json_decode( $ceretax_data ); 121 125 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 122 $total_tax_invoice = abs( $ceretax_data->invoice->totalTaxInvoice ); 123 126 $total_tax_invoice = $ceretax_data->invoice->totalTaxInvoice; 124 127 $order = wc_get_order( $order_id ); 125 128 $fee_amount = null; 126 129 $order_total_fees = $order->get_total_fees(); 127 130 $order_remain_fees = 0; 131 132 // If refunded order. 133 if ( is_array( $order->get_refunds() ) && count( $order->get_refunds() ) > 0 ) { 134 $ceretax_data_lineitem = get_post_meta( $order_id, 'ceretax_data_lineitem', true ); 135 $ceretax_data_lineitem = json_decode( $ceretax_data_lineitem ); 136 $total_tax_invoice = isset( $ceretax_data_lineitem->invoice->totalTaxInvoice ) ? $ceretax_data_lineitem->invoice->totalTaxInvoice : null; 137 } 138 128 139 foreach ( $order->get_fees() as $fee_id => $fee ) { 129 140 if ( $fee->get_name() === $cere_tax_name ) { … … 136 147 } 137 148 } 138 139 149 if ( 0 < $order_remain_fees ) { 140 150 echo '<tr>'; 141 echo '<td class="label">' . __( 'Fees', 'ceretax') . ':</td>';151 echo '<td class="label">' . esc_html( __( 'Fees', 'ceretax' ) ) . ':</td>'; 142 152 echo '<td width="1%"></td>'; 143 echo '<td class="total">' . wc_price( $order_remain_fees ) . '</td>';153 echo '<td class="total">' . wc_price( $order_remain_fees, array( 'currency' => $order->get_currency() ) ) . '</td>'; 144 154 echo '</tr>'; 145 155 } … … 147 157 echo '<td class="label">' . esc_html( $cere_tax_name ) . ':</td>'; 148 158 echo '<td width="1%"></td>'; 149 echo '<td class="total">' . wc_price( $total_tax_invoice ) . '</td>';159 echo '<td class="total">' . wc_price( $total_tax_invoice, array( 'currency' => $order->get_currency() ) ) . '</td>'; 150 160 echo '</tr>'; 151 161 } … … 254 264 $country = isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( $_POST['country'] ) ) : ''; 255 265 $state = isset( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : ''; 256 $postcode = isset( $_POST['postcode'] ) ? s anitize_text_field( wp_unslash( $_POST['postcode']) ) : '';266 $postcode = isset( $_POST['postcode'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['postcode'] ) ) ) : ''; 257 267 258 268 // Call your third-party API here. … … 299 309 $country = isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( $_POST['country'] ) ) : ''; 300 310 $state = isset( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : ''; 301 $postcode = isset( $_POST['postcode'] ) ? s anitize_text_field( wp_unslash( $_POST['postcode']) ) : '';311 $postcode = isset( $_POST['postcode'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['postcode'] ) ) ) : ''; 302 312 $address_validation = isset( $_POST['address_validation'] ) ? sanitize_text_field( wp_unslash( $_POST['address_validation'] ) ) : ''; 303 313 … … 316 326 'city' => $city, 317 327 'state' => $state, 328 'postalCode' => $postcode, 318 329 ), 319 330 ); … … 366 377 $validated_state = $data['results'][0]['validatedAddressDetails']['state']; 367 378 $validated_country = $data['results'][0]['validatedAddressDetails']['country']; 368 $validated_postal_code = $data['results'][0]['validatedAddressDetails']['postalCode']; 379 $validated_postal_code = strtoupper( $data['results'][0]['submittedAddressDetails']['postalCode'] ); 380 381 // Update submitted postal code. 382 $validated_address_details['postalCode'] = $validated_postal_code; 369 383 370 384 if ( 'shipping' === $address_validation ) { … … 487 501 $calc_shipping_city = isset( $_POST['calc_shipping_city'] ) ? sanitize_text_field( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; 488 502 $calc_shipping_state = isset( $_POST['calc_shipping_state'] ) ? sanitize_text_field( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; 489 $calc_shipping_postcode = isset( $_POST['calc_shipping_postcode'] ) ? s anitize_text_field( wp_unslash( $_POST['calc_shipping_postcode']) ) : '';503 $calc_shipping_postcode = isset( $_POST['calc_shipping_postcode'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['calc_shipping_postcode'] ) ) ) : ''; 490 504 491 505 $tax_rate = $this->cwafc_get_tax_rate_from_api( … … 520 534 $city = isset( $_POST['s_city'] ) ? sanitize_text_field( wp_unslash( $_POST['s_city'] ) ) : ''; 521 535 $state = isset( $_POST['s_state'] ) ? sanitize_text_field( wp_unslash( $_POST['s_state'] ) ) : ''; 522 $postcode = isset( $_POST['s_postcode'] ) ? s anitize_text_field( wp_unslash( $_POST['s_postcode']) ) : '';536 $postcode = isset( $_POST['s_postcode'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['s_postcode'] ) ) ) : ''; 523 537 } else { 524 538 $address_1 = isset( $_POST['address'] ) ? sanitize_text_field( wp_unslash( $_POST['address'] ) ) : ''; … … 527 541 $city = isset( $_POST['city'] ) ? sanitize_text_field( wp_unslash( $_POST['city'] ) ) : ''; 528 542 $state = isset( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : ''; 529 $postcode = isset( $_POST['postcode'] ) ? s anitize_text_field( wp_unslash( $_POST['postcode']) ) : '';543 $postcode = isset( $_POST['postcode'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['postcode'] ) ) ) : ''; 530 544 } 531 545 … … 747 761 public function action__cwafc_checkout_field_update_order_meta( $order_id ) { 748 762 $ceretax_data = WC()->session->get( 'ceretax_data' ); 749 update_post_meta( $order_id, 'ceretax_data', wp_json_encode( $ceretax_data ) ); 763 if ( ! empty( $ceretax_data ) ) { 764 update_post_meta( $order_id, 'ceretax_data', wp_json_encode( $ceretax_data ) ); 765 } 750 766 } 751 767 … … 763 779 $ceretax_items_data = $ceretax_data->invoice->lineItems; 764 780 if ( ! empty( $ceretax_items_data ) ) { 765 $tax_table .= '<t able class="woocommerce_order_items ceratax-tax-table">';781 $tax_table .= '<tr><td colspan="7"><table class="woocommerce_order_items ceratax-tax-table">'; 766 782 foreach ( $ceretax_items_data as $ceretax_item_data_key => $ceretax_item_data ) { 767 783 if ( ! empty( $ceretax_item_data->taxes ) ) { … … 804 820 } 805 821 } 806 $tax_table .= '</table> ';822 $tax_table .= '</table></td></tr>'; 807 823 } 808 824 } … … 823 839 $order_ceretax_data = get_post_meta( $order_id, 'ceretax_data', true ); 824 840 $order_avatax_data = get_post_meta( $order_id, '_wc_avatax_tax_calculated', true ); 825 $order_contains_renewal = ( function_exists( 'wcs_order_contains_renewal' ) ) ? wcs_order_contains_renewal( $order_id ) : true;826 841 // Check if order have other tax system. 827 842 if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) { … … 860 875 861 876 if ( is_admin() && ! empty( $res ) ) { 862 877 $fee_exists = false; 863 878 foreach ( $order->get_fees() as $fee ) { 864 879 if ( $fee->get_name() === $cere_tax_name ) { … … 932 947 } 933 948 934 if ( ! empty( $res )) {935 949 if ( isset( $res ) && null !== $res ) { 950 $fee_exists = false; 936 951 foreach ( $r_order->get_fees() as $fee ) { 937 952 if ( $fee->get_name() === $cere_tax_name ) { … … 975 990 $order_ceretax_data = get_post_meta( $order->get_id(), 'ceretax_data', true ); 976 991 $order_avatax_data = get_post_meta( $order->get_id(), '_wc_avatax_tax_calculated', true ); 977 $order_contains_renewal = ( function_exists( 'wcs_order_contains_renewal' ) ) ? wcs_order_contains_renewal( $order->get_id() ) : true;978 992 // Check if order have other tax system. 979 993 if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) { … … 1062 1076 $refund = wc_get_order( $refund_id ); 1063 1077 1078 // Save ceretax line item tax in order line items. 1079 $line_item_result = array(); 1080 if ( isset( $_POST['line_item_tax_totals'] ) && ! empty( $_POST['line_item_tax_totals'] ) ) { 1081 $line_item_json = stripslashes( $_POST['line_item_tax_totals'] ); 1082 $line_item_data = json_decode( $line_item_json, true ); 1083 $line_item_result = array_map( function( $item ) { 1084 return $item['undefined']; 1085 }, $line_item_data ); 1086 $line_item_result = array_filter( $line_item_result, function( $value ) { 1087 return ! empty( $value ); 1088 }); 1089 if ( ! empty( $line_item_result ) && ! empty( $order->get_items() ) ) { 1090 foreach ( $order->get_items() as $item_id => $item ) { 1091 if ( array_key_exists( $item_id, $line_item_result ) ) { 1092 $item->add_meta_data( '_ceretax_refunded_line_item_tax', $line_item_result[$item_id], true ); 1093 $item->save(); 1094 } 1095 } 1096 } 1097 } 1098 1064 1099 $cere_tax_post_transactions = get_option( CWAFC_PREFIX . '_cere_tax_post_transactions' ); 1065 1100 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 1066 1067 if ( 'yes' !== $cere_tax_post_transactions ) { 1101 $cere_tax_refund_disable = get_option( CWAFC_PREFIX . '_cere_tax_refund_disable' ); 1102 1103 if ( 'yes' !== $cere_tax_post_transactions || 'yes' === $cere_tax_refund_disable ) { 1068 1104 return false; 1069 1105 } … … 1088 1124 } 1089 1125 $res = ''; 1090 if ( ! empty( $address_data ) && ! empty( $order->get_items() ) ) { 1091 $res = $this->cwafc_calculate_cere_tax_refunded( $address_data, $order ); 1092 } 1093 1094 if ( ! empty( $res ) ) { 1126 if ( ! empty( $address_data ) && ! empty( $refund->get_items() ) ) { 1127 $res = $this->cwafc_calculate_cere_tax_refunded( $address_data, $refund, $order ); 1128 } 1129 1130 $is_run = false; 1131 if ( $is_run && ! empty( $res ) ) { 1095 1132 $fee_exists = false; 1096 1133 foreach ( $order->get_fees() as $fee ) { … … 1111 1148 $item_fee->set_amount( $new_fee_amount ); 1112 1149 $item_fee->set_total( $new_fee_amount ); 1113 $ order->add_item( $item_fee );1150 $refund->add_item( $item_fee ); 1114 1151 } 1115 1152 // Recalculate order totals. 1116 $ order->calculate_totals();1153 $refund->calculate_totals(); 1117 1154 1118 1155 // Save the order. 1119 $ order->save();1156 $refund->save(); 1120 1157 } 1121 1158 } … … 1152 1189 $calculation_type = 'S'; 1153 1190 $invoice_number = $calculation_type . '-' . wp_rand( 100000, 99999999 ); 1154 $customer_account = ( is_user_logged_in() ) ? get_current_user_id() : wp_rand( 1000, 99999 );1155 1191 $shipping_charges = $order->get_shipping_total(); 1156 1192 $shipping_zones = WC_Shipping_Zones::get_zones(); 1193 1194 // Get order customer id. 1195 if ( $order->get_customer_id() ) { 1196 $customer_account = $order->get_customer_id(); 1197 } elseif ( is_user_logged_in() ) { 1198 $customer_account = get_current_user_id(); 1199 } else { 1200 $customer_account = wp_rand( 1000, 99999 ); 1201 } 1157 1202 1158 1203 // Caclulate tax if only CereTax calculation option is not enabled. … … 1167 1212 } 1168 1213 1169 $item_count = 0; 1170 $order_status = $order->get_status(); 1171 $created_via = $order->get_meta( '_created_via' ); 1172 $order_parent_id = $order->get_parent_id(); 1173 $order_type = $order->get_type(); 1214 $item_count = 0; 1215 $order_status = $order->get_status(); 1216 $created_via = $order->get_meta( '_created_via' ); 1217 $order_parent_id = $order->get_parent_id(); 1218 $order_type = $order->get_type(); 1219 $first_renewal_payment = get_post_meta( $order->get_id(), 'is_first_renewal_payment', true ); 1174 1220 1175 1221 $total_renewal_orders = 0; … … 1180 1226 $total_renewal_orders = count( $renewal_orders ); 1181 1227 } 1228 } 1229 if ( 1 === $total_renewal_orders ) { 1230 // Update renewal payment meta. 1231 update_post_meta( $order->get_id(), 'is_first_renewal_payment', 'yes' ); 1182 1232 } 1183 1233 … … 1242 1292 1243 1293 // Check if subscription trial main order. 1244 if ( 'subscription' === $created_via && ! empty( $order_item_trial_length ) && 1 == $total_renewal_orders) {1294 if ( 'subscription' === $created_via && ! empty( $order_item_trial_length ) && 'yes' === $first_renewal_payment ) { 1245 1295 $line_items[ $item_count ]['attributes'] = array( 1246 1296 array( … … 1333 1383 } 1334 1384 update_post_meta( $order->get_id(), 'ceretax_data', wp_json_encode( $data ) ); 1385 update_post_meta( $order->get_id(), 'ceretax_data_lineitem', wp_json_encode( $data ) ); 1335 1386 return $data['invoice']['totalTaxInvoice']; 1336 1387 } … … 1372 1423 } 1373 1424 update_post_meta( $order->get_id(), 'ceretax_data', wp_json_encode( $data ) ); 1425 update_post_meta( $order->get_id(), 'ceretax_data_lineitem', wp_json_encode( $data ) ); 1374 1426 return $data['invoice']['totalTaxInvoice']; 1375 1427 } … … 1382 1434 * 1383 1435 * @param array $address_data Address data array. 1436 * @param object $refund Order refund object. 1384 1437 * @param object $order Order object. 1385 1438 * @param string $tax_status Cetatax tax status. 1386 1439 * @return mix 1387 1440 */ 1388 public function cwafc_calculate_cere_tax_refunded( $address_data, $ order, $tax_status = 'Posted' ) {1441 public function cwafc_calculate_cere_tax_refunded( $address_data, $refund, $order, $tax_status = 'Posted' ) { 1389 1442 1390 1443 // address is required for tax calulation. … … 1392 1445 return false; 1393 1446 } 1394 $order_subtotal = (float) $order->get_subtotal(); 1395 $order_subtotal = ( $order_subtotal > 0 ) ? ( $order_subtotal * -1 ) : -0; 1447 $order_subtotal = 0; 1396 1448 $line_items = array(); 1397 1449 … … 1409 1461 $cere_tax_validate_address_on_transaction = 'yes' === $cere_tax_validate_address_on_transaction ? true : false; 1410 1462 $calculation_type = 'S'; 1411 $invoice_number = $calculation_type . '-' . wp_rand( 100000, 99999999 ); 1412 $customer_account = ( is_user_logged_in() ) ? get_current_user_id() : wp_rand( 1000, 99999 ); 1413 $shipping_charges = (float) $order->get_shipping_total(); 1414 $shipping_charges = ( $shipping_charges > 0 ) ? ( $shipping_charges * -1 ) : -0; 1463 $shipping_charges = (float) $refund->get_shipping_total(); 1415 1464 $shipping_zones = WC_Shipping_Zones::get_zones(); 1465 1466 // Get order customer id. 1467 if ( $order->get_customer_id() ) { 1468 $customer_account = $order->get_customer_id(); 1469 } elseif ( is_user_logged_in() ) { 1470 $customer_account = get_current_user_id(); 1471 } else { 1472 $customer_account = wp_rand( 1000, 99999 ); 1473 } 1416 1474 1417 1475 // Caclulate tax if only CereTax calculation option is not enabled. … … 1426 1484 } 1427 1485 1428 $item_count = 0; 1429 $order_status = $order->get_status(); 1430 $created_via = $order->get_meta( '_created_via' ); 1431 $order_parent_id = $order->get_parent_id(); 1432 $order_type = $order->get_type(); 1433 1434 $total_renewal_orders = 0; 1435 $order_subscriptions = ( function_exists( 'wcs_get_subscriptions_for_order' ) ) ? wcs_get_subscriptions_for_order( $order->get_id(), array( 'order_type' => 'any' ) ) : array(); 1436 if ( ! empty( $order_subscriptions ) ) { 1437 foreach ( $order_subscriptions as $subscription_id => $subscription ) { 1438 $renewal_orders = ( class_exists( 'WC_Subscriptions' ) && is_object( $subscription ) && method_exists( $subscription, 'get_related_orders' ) ) ? $subscription->get_related_orders( 'all', 'renewal' ) : array(); 1439 $total_renewal_orders = count( $renewal_orders ); 1440 } 1441 } 1442 1443 foreach ( $order->get_items() as $item_id => $item ) { 1486 $item_count = 0; 1487 $order_status = $order->get_status(); 1488 $created_via = $order->get_meta( '_created_via' ); 1489 $order_parent_id = $order->get_parent_id(); 1490 $order_type = $order->get_type(); 1491 $first_renewal_payment = get_post_meta( $order->get_id(), 'is_first_renewal_payment', true ); 1492 1493 foreach ( $refund->get_items() as $item_id => $item ) { 1444 1494 1445 1495 $product_id = $item->get_product_id(); … … 1466 1516 1467 1517 $item_total = (float) $item->get_total(); 1468 $ item_total = ( $item_total > 0 ) ? ( $item_total * -1 ) : -0;1518 $order_subtotal = $order_subtotal + $item_total; 1469 1519 $line_items[ $item_count ] = array( 1470 1520 'lineID' => (string) $item_id, … … 1503 1553 1504 1554 // Check if subscription trial main order. 1505 if ( 'subscription' === $created_via && ! empty( $order_item_trial_length ) && 1 == $total_renewal_orders) {1555 if ( 'subscription' === $created_via && ! empty( $order_item_trial_length ) && 'yes' === $first_renewal_payment ) { 1506 1556 $line_items[ $item_count ]['attributes'] = array( 1507 1557 array( … … 1519 1569 'contentYear' => gmdate( 'Y' ), 1520 1570 'contentMonth' => gmdate( 'm' ), 1521 'decimals' => 6,1571 'decimals' => 2, 1522 1572 'calculationType' => $calculation_type, 1523 1573 'profileId' => $cere_tax_profile, … … 1580 1630 $data = json_decode( $data, true ); 1581 1631 } 1582 1632 1583 1633 if ( is_wp_error( $response ) ) { 1584 1634 return false; … … 1590 1640 } 1591 1641 update_post_meta( $order->get_id(), 'ceretax_data', wp_json_encode( $data ) ); 1592 return abs( $data['invoice']['totalTaxInvoice'] );1642 return $data['invoice']['totalTaxInvoice']; 1593 1643 } 1594 1644 return false; … … 1878 1928 // Caclulate tax if only CereTax calculation option is not enabled. 1879 1929 $cere_tax_enable = get_option( CWAFC_PREFIX . '_cere_tax_enable' ); 1880 return 'yes' === $cere_tax_enable ? true : false; 1930 return 'yes' === $cere_tax_enable ? true : false; 1881 1931 } 1882 1932 … … 1950 2000 } 1951 2001 return $items; 2002 } 2003 2004 /** 2005 * Update tax calculation for subscription. 2006 * 2007 * @param object $subscription Subscription Object. 2008 * @return void 2009 */ 2010 public function action__cwafc_update_tax_calculation_for_subscription( $subscription ) { 2011 if ( ! empty( $subscription ) && $subscription instanceof WC_Subscription ) { 2012 $subscription->calculate_totals(); 2013 } 2014 } 2015 2016 /** 2017 * Clear avatax for new create order. 2018 * 2019 * @param object $order Order object. 2020 * @param object $subscription Subscription object. 2021 * @param string $type Type string. 2022 * @return mix 2023 */ 2024 public function action__cwafc_clear_avatx_new_order_created( $order, $subscription, $type ) { 2025 2026 if ( ! class_exists( 'WC_AvaTax_Order_Handler' ) && ! empty( $subscription->get_meta( '_wc_avatax_tax_calculated' ) ) && $subscription->get_meta( '_wc_avatax_tax_calculated' ) == 'yes' && empty( $order->get_meta( '_wc_avatax_tax_calculated' ) ) ) { 2027 2028 // Clear avatax data from order item meta. 2029 foreach ( $order->get_items( 'line_item' ) as $item ) { 2030 2031 $meta = $item->get_meta_data(); 2032 2033 foreach ( $item->get_meta_data() as $meta_data ) { 2034 2035 $data = $meta_data->get_data(); 2036 2037 if ( ! empty( $data['key'] ) && str_contains( $data['key'], 'avatax' ) ) { 2038 $item->delete_meta_data( $data['key'] ); 2039 } 2040 } 2041 $item->save(); 2042 } 2043 2044 // Clear avatax data from order shipping meta. 2045 foreach ( $order->get_items( 'shipping' ) as $item ) { 2046 $meta = $item->get_meta_data(); 2047 foreach ( $item->get_meta_data() as $meta_data ) { 2048 $data = $meta_data->get_data(); 2049 if ( ! empty( $data['key'] ) && str_contains( $data['key'], 'avatax' ) ) { 2050 $item->delete_meta_data( $data['key'] ); 2051 } 2052 } 2053 $item->save(); 2054 } 2055 2056 // Clear old Avatax taxes from Ceretax renewal order. 2057 $require_recalculation = 0; 2058 foreach ( $order->get_items( 'tax' ) as $item_id => $item ) { 2059 if ( ! empty( $item->get_name() ) && str_contains( $item->get_name(), 'AVATAX' ) ) { 2060 $item->delete(); 2061 $require_recalculation = 1; 2062 } 2063 } 2064 if ( $require_recalculation == 1 ) { 2065 $order->save(); 2066 $order->calculate_totals(); 2067 } 2068 } 2069 return $order; 1952 2070 } 1953 2071 } -
ceretax/trunk/readme.txt
r3248052 r3259100 5 5 Tested up to: 6.6.1 6 6 Requires PHP: 7.4 7 Stable tag: 1. 3.87 Stable tag: 1.4.0 8 8 License: GNU General Public License v3.0 9 9 URI: http://www.gnu.org/licenses/gpl-3.0.html … … 36 36 == Changelog == 37 37 38 = 1.3.8 = 39 * fix - Restore to 1.3.6 functionality 38 = 1.4.0 = 39 * add - Disable CereTax for refund orders 40 41 = 1.3.9 = 42 * fix - Postcode in lower case causing tax calculation issue 40 43 41 44 = 1.3.7 = … … 77 80 == Upgrade Notice == 78 81 79 = 1.3.8 = 80 * fix - Restore to 1.3.6 functionality 82 = 1.4.0 = 83 * add - Disable CereTax for refund orders 84 85 = 1.3.9 = 86 * fix - Postcode in lower case causing tax calculation issue 81 87 82 88 = 1.3.7 =
Note: See TracChangeset
for help on using the changeset viewer.