From 7639cd8390dc23b506e13a6365311315c3211031 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Sun, 22 Jan 2012 02:35:02 +0000 Subject: [PATCH] Calculations + ajax order item rows --- woocommerce-ajax.php | 129 +++++++++++++------------------------------ 1 file changed, 38 insertions(+), 91 deletions(-) diff --git a/woocommerce-ajax.php b/woocommerce-ajax.php index a2d09b3f128..863938174f5 100644 --- a/woocommerce-ajax.php +++ b/woocommerce-ajax.php @@ -548,7 +548,7 @@ function woocommerce_add_order_item() { sku) echo $_product->sku; else echo '-'; ?> - + @@ -570,23 +570,11 @@ function woocommerce_add_order_item() { - - - - - - - - - - - - - - - - + - - + + + + - - + + + + @@ -627,7 +611,7 @@ function woocommerce_add_order_item() { } /** - * Calc line taxes + * Calc line tax */ add_action('wp_ajax_woocommerce_calc_line_taxes', 'woocommerce_calc_line_taxes'); @@ -638,81 +622,44 @@ function woocommerce_calc_line_taxes() { $tax = new woocommerce_tax(); - $unit_tax = 0; - $line_tax = 0; + $base_tax_amount = 0; + $line_tax_amount = 0; $country = strtoupper(esc_attr($_POST['country'])); $state = strtoupper(esc_attr($_POST['state'])); $postcode = strtoupper(esc_attr($_POST['postcode'])); - $unit = esc_attr($_POST['unit_cost']); - $line = esc_attr($_POST['line_cost']); - $tax_class = esc_attr($_POST['tax_class']); - $tax_status = esc_attr($_POST['tax_status']); - if ($tax_status=='taxable') : - $tax_rates = $tax->find_rates( $country, $state, $postcode, $tax_class ); + $line_subtotal = esc_attr($_POST['line_subtotal']); + $line_total = esc_attr($_POST['line_total']); + + $item_id = esc_attr($_POST['item_id']); + $tax_class = esc_attr($_POST['tax_class']); + + // Get product details + $_product = new woocommerce_product($item_id); + $item_tax_status = $_product->get_tax_status(); + + if ($item_tax_status=='taxable') : + + $tax_rates = $tax->find_rates( $country, $state, $postcode, $tax_class ); + + $line_subtotal_tax_amount = rtrim(rtrim(number_format( array_sum($tax->calc_tax( $line_subtotal, $tax_rates, false )), 4, '.', ''), '0'), '.'); + $line_tax_amount = rtrim(rtrim(number_format( array_sum($tax->calc_tax( $line_total, $tax_rates, false )), 4, '.', ''), '0'), '.'); - $unit_tax = number_format( array_sum($tax->calc_tax( $unit, $tax_rates, false )), 2, '.', ''); - $line_tax = number_format( array_sum($tax->calc_tax( $line, $tax_rates, false )), 2, '.', ''); endif; + if ($line_subtotal_tax_amount<0) $line_subtotal_tax_amount = 0; + if ($line_tax_amount<0) $line_tax_amount = 0; + echo json_encode(array( - 'unit' => $unit_tax, - 'line' => $line_tax + 'line_subtotal_tax' => $line_subtotal_tax_amount, + 'line_tax' => $line_tax_amount )); // Quit out die(); } -/** - * Calc line cost - */ -add_action('wp_ajax_woocommerce_calc_line_cost', 'woocommerce_calc_line_cost'); - -function woocommerce_calc_line_cost() { - global $woocommerce; - - check_ajax_referer( 'calc-totals', 'security' ); - - $tax = new woocommerce_tax(); - - $line_cost = 0; - $line_tax = 0; - - $country = strtoupper(esc_attr($_POST['country'])); - $state = strtoupper(esc_attr($_POST['state'])); - $postcode = strtoupper(esc_attr($_POST['postcode'])); - $unit = esc_attr($_POST['unit_cost']); - $unit_tax = esc_attr($_POST['unit_tax']); - $quantity = esc_attr($_POST['quantity']); - $tax_class = esc_attr($_POST['tax_class']); - $tax_status = esc_attr($_POST['tax_status']); - - if ($tax_status=='taxable') : - $tax_rates = $tax->find_rates( $country, $state, $postcode, $tax_class ); - - if (get_option('woocommerce_prices_include_tax')=='yes') : - - $inc_tax = ( $unit + $unit_tax ) * $quantity; - - $line_tax = number_format( array_sum($tax->calc_tax( $inc_tax, $tax_rates, true )), 2, '.', ''); - $line_cost = $inc_tax - $line_tax; - - else : - $line_cost = $unit * $quantity; - $line_tax = number_format( array_sum($tax->calc_tax( $cost, $tax_rates, false )), 2, '.', ''); - endif; - endif; - - echo json_encode(array( - 'cost' => $line_cost, - 'tax' => $line_tax - )); - - // Quit out - die(); -} /** * Add order note via ajax