Changeset 3235501
- Timestamp:
- 02/05/2025 03:04:44 PM (12 months ago)
- Location:
- ceretax/trunk
- Files:
-
- 4 edited
-
ceretax.php (modified) (1 diff)
-
changelog.txt (modified) (1 diff)
-
inc/class-cwafc.php (modified) (8 diffs)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ceretax/trunk/ceretax.php
r3232301 r3235501 4 4 * Plugin URI: https://wordpress.org/plugins/ceretax/ 5 5 * Description: Simplify sales tax complexity with CereTax for WooCommerce. 6 * Version: 1.3. 36 * Version: 1.3.4 7 7 * Author: CereTax, Inc. 8 8 * Author URI: https://www.ceretax.com/ -
ceretax/trunk/changelog.txt
r3232301 r3235501 26 26 2025-01-28 - version 1.3.3 27 27 * fix - Hide defualt tax column if ceretax found from order admin edit screen 28 29 2025-02-03 - version 1.3.4 30 * fix - If shipping address not found bypass ceretax call 31 * add - Refunded order tax calculation in ceretax -
ceretax/trunk/inc/class-cwafc.php
r3229669 r3235501 87 87 // Add/Update order meta. 88 88 add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'action__cwafc_checkout_field_update_order_meta' ) ); 89 // Add tax ca culation table on admin order edit page.89 // Add tax calculation table on admin order edit page. 90 90 add_action( 'woocommerce_admin_order_items_after_shipping', array( $this, 'action__cwafc_add_tax_calculation_table_after_shipping' ), 99 ); 91 // Add tax cal ulation on order place.92 add_action( 'woocommerce_new_order', array( $this, 'action__cwafc_add_tax_cal ulation_when_order_placed' ), 10, 2 );93 // Add tax cal ulation on subscription renewal order place.94 add_filter( 'wcs_renewal_order_created', array( $this, 'filter__cwafc_add_tax_cal ulation_when_renewal_order_placed' ), 10 );95 // Update tax cal ulation on order update.91 // Add tax calculation on order place. 92 add_action( 'woocommerce_new_order', array( $this, 'action__cwafc_add_tax_calculation_when_order_placed' ), 10, 2 ); 93 // Add tax calculation on subscription renewal order place. 94 add_filter( 'wcs_renewal_order_created', array( $this, 'filter__cwafc_add_tax_calculation_when_renewal_order_placed' ), 10 ); 95 // Update tax calculation on order update. 96 96 add_action( 'woocommerce_order_after_calculate_totals', array( $this, 'action__cwafc_order_after_calculate_totals' ), 10, 2 ); 97 // Update tax calculation when refunded. 98 add_action( 'woocommerce_order_refunded', array( $this, 'action__cwafc_add_tax_calculation_when_order_refunded' ), 10, 2 ); 97 99 // Allow woocommerce subscriptions to add recurring fee. 98 100 add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' ); … … 116 118 $ceretax_data = json_decode( $ceretax_data ); 117 119 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 118 $total_tax_invoice = $ceretax_data->invoice->totalTaxInvoice;119 120 $order = wc_get_order( $order_id );121 $fee_amount = null;122 $order_total_fees = $order->get_total_fees();120 $total_tax_invoice = abs( $ceretax_data->invoice->totalTaxInvoice ); 121 122 $order = wc_get_order( $order_id ); 123 $fee_amount = null; 124 $order_total_fees = $order->get_total_fees(); 123 125 $order_remain_fees = 0; 124 126 foreach ( $order->get_fees() as $fee_id => $fee ) { … … 813 815 * @return mix 814 816 */ 815 public function action__cwafc_add_tax_cal ulation_when_order_placed( $order_id, $order ) {817 public function action__cwafc_add_tax_calculation_when_order_placed( $order_id, $order ) { 816 818 817 819 $cere_tax_post_transactions = get_option( CWAFC_PREFIX . '_cere_tax_post_transactions' ); 818 820 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 821 $order_ceretax_data = get_post_meta( $order_id, 'ceretax_data', true ); 822 $order_avatax_data = get_post_meta( $order_id, '_wc_avatax_tax_calculated', true ); 823 // Check if order have other tax system. 824 if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) { 825 return false; 826 } 819 827 if ( 'yes' !== $cere_tax_post_transactions ) { 820 828 return false; … … 882 890 * @return mix 883 891 */ 884 public function filter__cwafc_add_tax_cal ulation_when_renewal_order_placed( $renewal_order ) {892 public function filter__cwafc_add_tax_calculation_when_renewal_order_placed( $renewal_order ) { 885 893 $cere_tax_post_transactions = get_option( CWAFC_PREFIX . '_cere_tax_post_transactions' ); 886 894 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 895 $order_ceretax_data = get_post_meta( $renewal_order->get_id(), 'ceretax_data', true ); 896 $order_avatax_data = get_post_meta( $renewal_order->get_id(), '_wc_avatax_tax_calculated', true ); 897 // Check if order have other tax system. 898 if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) { 899 return false; 900 } 887 901 if ( 'yes' !== $cere_tax_post_transactions ) { 888 902 return false; … … 943 957 return $r_order; 944 958 } 945 959 946 960 /** 947 961 * Add tax calulation on order calculate totals. … … 952 966 */ 953 967 public function action__cwafc_order_after_calculate_totals( $and_taxes, $order ) { 968 954 969 $cere_tax_post_transactions = get_option( CWAFC_PREFIX . '_cere_tax_post_transactions' ); 955 970 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 971 $order_ceretax_data = get_post_meta( $order->get_id(), 'ceretax_data', true ); 972 $order_avatax_data = get_post_meta( $order->get_id(), '_wc_avatax_tax_calculated', true ); 973 // Check if order have other tax system. 974 if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) { 975 return false; 976 } 977 // Check order if refunded. 978 if ( $order instanceof WC_Order_Refund || 'refunded' === $order->get_status() ) { 979 return false; 980 } 956 981 if ( 'yes' !== $cere_tax_post_transactions ) { 957 982 return false; … … 1014 1039 // Remove the transient after recalculation. 1015 1040 delete_transient( 'prevent_order_calculate_totals' ); 1041 1042 // Save the order. 1043 $order->save(); 1044 } 1045 } 1046 1047 /** 1048 * Update tax calculation when order refunded. 1049 * 1050 * @param int $order_id Order ID. 1051 * @param int $refund_id Order Refunded ID. 1052 * 1053 * @return mix 1054 */ 1055 public function action__cwafc_add_tax_calculation_when_order_refunded( $order_id, $refund_id ) { 1056 1057 $order = wc_get_order( $order_id ); 1058 $refund = wc_get_order( $refund_id ); 1059 1060 $cere_tax_post_transactions = get_option( CWAFC_PREFIX . '_cere_tax_post_transactions' ); 1061 $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); 1062 1063 if ( 'yes' !== $cere_tax_post_transactions ) { 1064 return false; 1065 } 1066 1067 $shipping_address_1 = $order->get_shipping_address_1(); 1068 if ( ! empty( $shipping_address_1 ) ) { 1069 $address_data = array( 1070 'address_1' => $order->get_shipping_address_1(), 1071 'address_2' => $order->get_shipping_address_2(), 1072 'city' => $order->get_shipping_city(), 1073 'state' => $order->get_shipping_state(), 1074 'postcode' => $order->get_shipping_postcode(), 1075 ); 1076 } else { 1077 $address_data = array( 1078 'address_1' => $order->get_billing_address_1(), 1079 'address_2' => $order->get_billing_address_2(), 1080 'city' => $order->get_billing_city(), 1081 'state' => $order->get_billing_state(), 1082 'postcode' => $order->get_billing_postcode(), 1083 ); 1084 } 1085 $res = ''; 1086 if ( ! empty( $address_data ) && ! empty( $order->get_items() ) ) { 1087 $res = $this->cwafc_calculate_cere_tax_refunded( $address_data, $order ); 1088 } 1089 1090 if ( ! empty( $res ) ) { 1091 $fee_exists = false; 1092 foreach ( $order->get_fees() as $fee ) { 1093 if ( $fee->get_name() === $cere_tax_name ) { 1094 $new_fee_amount = $res; 1095 $fee->set_amount( $new_fee_amount ); 1096 $fee->set_total( $new_fee_amount ); 1097 $fee->save(); 1098 $fee_exists = true; 1099 break; 1100 } 1101 } 1102 1103 if ( ! $fee_exists ) { 1104 $item_fee = new WC_Order_Item_Fee(); 1105 $item_fee->set_name( $cere_tax_name ); 1106 $new_fee_amount = $res; // Replace this with the actual fee amount. 1107 $item_fee->set_amount( $new_fee_amount ); 1108 $item_fee->set_total( $new_fee_amount ); 1109 $order->add_item( $item_fee ); 1110 } 1111 // Recalculate order totals. 1112 $order->calculate_totals(); 1016 1113 1017 1114 // Save the order. … … 1276 1373 1277 1374 /** 1375 * On order refunded calculate cera tax from API 1376 * 1377 * @param array $address_data Address data array. 1378 * @param object $order Order object. 1379 * @param string $tax_status Cetatax tax status. 1380 * @return mix 1381 */ 1382 public function cwafc_calculate_cere_tax_refunded( $address_data, $order, $tax_status = 'Posted' ) { 1383 1384 // address is required for tax calulation. 1385 if ( ( isset( $address_data['address_1'] ) && empty( $address_data['address_1'] ) ) || ( isset( $address_data['city'] ) && empty( $address_data['city'] ) ) || ( isset( $address_data['state'] ) && empty( $address_data['state'] ) ) || ( isset( $address_data['postcode'] ) && empty( $address_data['postcode'] ) ) ) { 1386 return false; 1387 } 1388 $order_subtotal = (float) $order->get_subtotal(); 1389 $order_subtotal = ( $order_subtotal > 0 ) ? ( $order_subtotal * -1 ) : -0; 1390 $line_items = array(); 1391 1392 $cere_tax_api_key = get_option( CWAFC_PREFIX . '_cere_tax_api_key' ); 1393 $cere_tax_tax_env = get_option( CWAFC_PREFIX . '_cere_tax_environment' ); 1394 $cere_tax_profile = get_option( CWAFC_PREFIX . '_cere_tax_profile', 'sales' ); 1395 $cere_tax_business_type = get_option( CWAFC_PREFIX . '_cere_tax_business_type', '01' ); 1396 $cere_tax_customer_type = get_option( CWAFC_PREFIX . '_cere_tax_customer_type', '02' ); 1397 $cere_tax_ps_code = get_option( CWAFC_PREFIX . '_cere_tax_ps_code', '13010100' ); 1398 $cere_tax_tax_included = get_option( CWAFC_PREFIX . '_cere_tax_tax_included' ); 1399 $cere_tax_post_transactions = get_option( CWAFC_PREFIX . '_cere_tax_post_transactions' ); 1400 $cere_tax_validate_address_on_transaction = get_option( CWAFC_PREFIX . '_cere_tax_validate_on_transaction' ); 1401 $cere_tax_tax_status = $tax_status; 1402 $cere_tax_tax_included = 'yes' === $cere_tax_tax_included ? true : false; 1403 $cere_tax_validate_address_on_transaction = 'yes' === $cere_tax_validate_address_on_transaction ? true : false; 1404 $calculation_type = 'S'; 1405 $invoice_number = $calculation_type . '-' . wp_rand( 100000, 99999999 ); 1406 $customer_account = ( is_user_logged_in() ) ? get_current_user_id() : wp_rand( 1000, 99999 ); 1407 $shipping_charges = (float) $order->get_shipping_total(); 1408 $shipping_charges = ( $shipping_charges > 0 ) ? ( $shipping_charges * -1 ) : -0; 1409 $shipping_zones = WC_Shipping_Zones::get_zones(); 1410 1411 // Caclulate tax if only CereTax calculation option is not enabled. 1412 if ( ! $this->cwafc_is_ceretax_enable() ) { 1413 return false; 1414 } 1415 1416 if ( 'production' === $cere_tax_tax_env ) { 1417 $api_url = 'https://calc.prod.ceretax.net/sale'; 1418 } else { 1419 $api_url = 'https://calc.cert.ceretax.net/sale'; 1420 } 1421 1422 $item_count = 0; 1423 $order_status = $order->get_status(); 1424 $created_via = $order->get_meta( '_created_via' ); 1425 $order_parent_id = $order->get_parent_id(); 1426 $order_type = $order->get_type(); 1427 1428 $total_renewal_orders = 0; 1429 $order_subscriptions = ( function_exists( 'wcs_get_subscriptions_for_order' ) ) ? wcs_get_subscriptions_for_order( $order->get_id(), array( 'order_type' => 'any' ) ) : array(); 1430 if ( ! empty( $order_subscriptions ) ) { 1431 foreach ( $order_subscriptions as $subscription_id => $subscription ) { 1432 $renewal_orders = ( class_exists( 'WC_Subscriptions' ) && is_object( $subscription ) && method_exists( $subscription, 'get_related_orders' ) ) ? $subscription->get_related_orders( 'all', 'renewal' ) : array(); 1433 $total_renewal_orders = count( $renewal_orders ); 1434 } 1435 } 1436 1437 foreach ( $order->get_items() as $item_id => $item ) { 1438 1439 $product_id = $item->get_product_id(); 1440 $product = wc_get_product( $product_id ); 1441 $order_item_trial_length = $product->get_meta( '_subscription_trial_length' ); 1442 1443 // Get PS code. 1444 $product_ps_code = $product->get_meta( CWAFC_PREFIX . '_cere_tax_product_ps_code' ); 1445 $product_terms = get_the_terms( $product_id, 'product_cat' ); 1446 if ( ! empty( $product_ps_code ) ) { 1447 $cere_tax_ps_code = $product_ps_code; 1448 } elseif ( ! empty( $product_terms ) ) { 1449 $product_term = reset( $product_terms ); 1450 $product_term_id = ( isset( $product_term->term_id ) && ! empty( $product_term->term_id ) ) ? $product_term->term_id : 0; 1451 $product_cat_ps_code = get_term_meta( $product_term_id, CWAFC_PREFIX . '_cere_tax_product_cat_ps_code', true ); 1452 if ( ! empty( $product_cat_ps_code ) ) { 1453 $cere_tax_ps_code = $product_cat_ps_code; 1454 } else { 1455 $cere_tax_ps_code = get_option( CWAFC_PREFIX . '_cere_tax_ps_code', '13010100' ); 1456 } 1457 } else { 1458 $cere_tax_ps_code = get_option( CWAFC_PREFIX . '_cere_tax_ps_code', '13010100' ); 1459 } 1460 1461 $item_total = (float) $item->get_total(); 1462 $item_total = ( $item_total > 0 ) ? ( $item_total * -1 ) : -0; 1463 $line_items[ $item_count ] = array( 1464 'lineID' => (string) $item_id, 1465 'dateOfTransaction' => gmdate( 'Y-m-d' ), 1466 'itemNumber' => (string) $product_id, 1467 'itemDescription' => esc_html( $product->get_title() ), 1468 'revenue' => $item_total, 1469 'psCode' => $cere_tax_ps_code, 1470 'revenueIncludesTax' => $cere_tax_tax_included, 1471 'units' => array( 1472 'quantity' => (int) $item->get_quantity(), 1473 'type' => '01', 1474 ), 1475 'situs' => array( 1476 'taxSitusRule' => 'T', 1477 'shipToAddress' => array( 1478 'addressLine1' => $address_data['address_1'], 1479 'addressLine2' => $address_data['address_2'], 1480 'city' => $address_data['city'], 1481 'state' => $address_data['state'], 1482 'postalCode' => $address_data['postcode'], 1483 'validateAddress' => $cere_tax_validate_address_on_transaction, 1484 ), 1485 ), 1486 ); 1487 1488 // Check if deposit and scheduled orders. 1489 if ( ! empty( $order_parent_id ) && 'wc_deposits' === $created_via && 'scheduled-payment' === $order_status ) { 1490 $line_items[ $item_count ]['attributes'] = array( 1491 array( 1492 'key' => 'noDeliveryFee', 1493 'value' => 'Y', 1494 ), 1495 ); 1496 } 1497 1498 // Check if subscription trial main order. 1499 if ( 'subscription' === $created_via && ! empty( $order_item_trial_length ) && 1 == $total_renewal_orders ) { 1500 $line_items[ $item_count ]['attributes'] = array( 1501 array( 1502 'key' => 'noDeliveryFee', 1503 'value' => 'Y', 1504 ), 1505 ); 1506 } 1507 $item_count++; 1508 } 1509 1510 $request_body = array( 1511 'configuration' => array( 1512 'status' => $cere_tax_tax_status, 1513 'contentYear' => gmdate( 'Y' ), 1514 'contentMonth' => gmdate( 'm' ), 1515 'decimals' => 6, 1516 'calculationType' => $calculation_type, 1517 'profileId' => $cere_tax_profile, 1518 ), 1519 'invoice' => array( 1520 'invoiceDate' => gmdate( 'Y-m-d' ), 1521 'invoiceNumber' => (string) $order->get_id(), 1522 'customerAccount' => (string) $customer_account, 1523 'businessType' => $cere_tax_business_type, 1524 'customerType' => $cere_tax_customer_type, 1525 'sellerType' => '01', 1526 'invoiceTotalAmount' => $order_subtotal, 1527 'transactionCharges' => array( 1528 array( 1529 'shipping' => $shipping_charges, 1530 'freightOnBoard' => 'D', 1531 'deliveryType' => 'C', 1532 'isMandatory' => true, 1533 ), 1534 ), 1535 'lineItems' => $line_items, 1536 ), 1537 ); 1538 1539 $body = wp_json_encode( $request_body ); 1540 1541 // add api request data in wc logs if log option enabled. 1542 if ( 'Posted' === $cere_tax_tax_status ) { 1543 $this->cwafc_wc_add_custom_logs( $request_body, 'ceretax-api-request-with-posted-refunded' ); 1544 } else { 1545 $this->cwafc_wc_add_custom_logs( $request_body, 'ceretax-api-request-refunded' ); 1546 } 1547 1548 $ceretax_stau = get_post_meta( $order->get_id(), 'ceretax_stau_' . $order->get_id(), true ); 1549 1550 $response = wp_remote_post( 1551 $api_url, 1552 array( 1553 'method' => 'POST', 1554 'body' => $body, 1555 'headers' => array( 1556 'x-api-key' => $cere_tax_api_key, 1557 'Content-Type' => 'application/json', 1558 ), 1559 'timeout' => 60, 1560 ) 1561 ); 1562 1563 // add api response data in wc logs if log option enabled. 1564 if ( 'Posted' === $cere_tax_tax_status ) { 1565 $this->cwafc_wc_add_custom_logs( $response, 'ceretax-api-response-with-posted-refunded' ); 1566 } else { 1567 $this->cwafc_wc_add_custom_logs( $response, 'ceretax-api-response-refunded' ); 1568 } 1569 1570 if ( is_wp_error( $response ) ) { 1571 return 'Request failed: ' . $response->get_error_message(); 1572 } else { 1573 $data = wp_remote_retrieve_body( $response ); 1574 $data = json_decode( $data, true ); 1575 } 1576 1577 if ( is_wp_error( $response ) ) { 1578 return false; 1579 } 1580 1581 if ( isset( $data['invoice']['totalTaxInvoice'] ) ) { 1582 if ( ! empty( $data['systemTraceAuditNumber'] ) ) { 1583 update_post_meta( $order->get_id(), 'ceretax_stau_' . $order->get_id(), $data['systemTraceAuditNumber'] ); 1584 } 1585 update_post_meta( $order->get_id(), 'ceretax_data', wp_json_encode( $data ) ); 1586 return abs( $data['invoice']['totalTaxInvoice'] ); 1587 } 1588 return false; 1589 } 1590 1591 /** 1278 1592 * Hide tax fields from checkout 1279 1593 * -
ceretax/trunk/readme.txt
r3232301 r3235501 4 4 Requires at least: 4.0.1 5 5 Tested up to: 6.6.1 6 Requires PHP: 5.67 Stable tag: 1.3. 36 Requires PHP: 7.4 7 Stable tag: 1.3.4 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.4 = 39 * fix - If shipping address not found bypass ceretax call 40 * add - Refunded order tax calculation in ceretax 41 38 42 = 1.3.3 = 39 43 * fix - Hide defualt tax column if ceretax found from order admin edit screen … … 64 68 == Upgrade Notice == 65 69 70 = 1.3.4 = 71 * fix - If shipping address not found bypass ceretax call 72 * add - Refunded order tax calculation in ceretax 73 66 74 = 1.3.3 = 67 75 * fix - Hide defualt tax column if ceretax found from order admin edit screen
Note: See TracChangeset
for help on using the changeset viewer.