Work on calc_tax

This commit is contained in:
Mike Jolley 2012-11-12 18:53:40 +00:00
parent 98ddb3bbc5
commit c2e1258d04
4 changed files with 141 additions and 65 deletions

View File

@ -579,7 +579,7 @@ function woocommerce_order_totals_meta_box($post) {
<ul class="totals">
<li class="left">
<label><?php _e( 'Sales Tax:', 'woocommerce' ); ?></label>
<label><?php _e( 'Sales Tax:', 'woocommerce' ); ?>&nbsp;<a class="tips" data-tip="<?php _e( 'Total tax for line items + fees.', 'woocommerce' ); ?>" href="#">[?]</a></label>
<input type="text" id="_order_tax" name="_order_tax" placeholder="0.00" value="<?php
if ( isset( $data['_order_tax'][0] ) )
echo esc_attr( $data['_order_tax'][0] );

View File

@ -263,6 +263,7 @@ jQuery( function($){
var total = 0;
fields.each(function(){
if ( $(this).val() )
total = total + parseFloat( $(this).val() );
});
@ -302,16 +303,28 @@ jQuery( function($){
var city = $('#_billing_city').val();
}
$items.each(function( idx ){
// Get items and values
var calculate_items = {};
$items.each( function() {
var $row = $(this);
var item_id = $row.find('input.order_item_id').val();
var line_subtotal = $row.find('input.line_subtotal').val();
var line_total = $row.find('input.line_total').val();
var tax_class = $row.find('select.tax_class').val();
calculate_items[ item_id ] = {};
calculate_items[ item_id ].line_subtotal = line_subtotal;
calculate_items[ item_id ].line_total = line_total;
calculate_items[ item_id ].tax_class = tax_class;
} );
var data = {
action: 'woocommerce_calc_line_taxes',
order_item_id: $row.find('input.order_item_id').val(),
line_subtotal: $row.find('input.line_subtotal').val(),
line_total: $row.find('input.line_total').val(),
tax_class: $row.find('select.tax_class').val(),
items: calculate_items,
shipping: accounting.unformat( $('#_order_shipping').val() ),
country: country,
state: state,
postcode: postcode,
@ -319,18 +332,21 @@ jQuery( function($){
security: woocommerce_writepanel_params.calc_totals_nonce
};
$.post( woocommerce_writepanel_params.ajax_url, data, function(response) {
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
result = jQuery.parseJSON( response );
$row.find('input.line_subtotal_tax').val( result.line_subtotal_tax ).change();
$row.find('input.line_tax').val( result.line_tax ).change();
if (idx == ($items.size() - 1)) {
$items.each( function() {
var $row = $(this);
var item_id = $row.find('input.order_item_id').val();
$row.find('input.line_tax').val( result['item_taxes'][ item_id ]['line_tax'] ).change();
$row.find('input.line_subtotal_tax').val( result['item_taxes'][ item_id ]['line_subtotal_tax'] ).change();
} );
$('#_order_shipping_tax').val( result['shipping_tax'] ).change();
$('.woocommerce_order_items_wrapper').unblock();
}
});
});
} else {
@ -338,9 +354,9 @@ jQuery( function($){
}
return false;
}).hover(function() {
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax').css('background-color', '#d8c8d2');
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax').css('background-color', '#d8c8d2');
}, function() {
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax').css('background-color', '');
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax').css('background-color', '');
});

File diff suppressed because one or more lines are too long

View File

@ -1101,22 +1101,27 @@ function woocommerce_calc_line_taxes() {
$tax = new WC_Tax();
$base_tax_amount = 0;
$line_tax_amount = 0;
$taxes = $tax_rows = $item_taxes = $shipping_taxes = array();
$country = strtoupper( esc_attr( $_POST['country'] ) );
$state = strtoupper( esc_attr( $_POST['state'] ) );
$postcode = strtoupper( esc_attr( $_POST['postcode'] ) );
$city = sanitize_title( esc_attr( $_POST['city'] ) );
$line_subtotal = isset( $_POST['line_subtotal'] ) ? esc_attr( $_POST['line_subtotal'] ) : 0;
$line_total = esc_attr( $_POST['line_total'] );
$items = $_POST['items'];
$shipping = $_POST['shipping'];
$item_id = esc_attr( $_POST['order_item_id'] );
$tax_class = esc_attr( $_POST['tax_class'] );
// Calculate sales tax first
if ( sizeof( $items ) > 0 ) {
foreach( $items as $item_id => $item ) {
$item_id = absint( $item_id );
$line_subtotal = isset( $item['line_subtotal'] ) ? esc_attr( $item['line_subtotal'] ) : '';
$line_total = esc_attr( $item['line_total'] );
$tax_class = esc_attr( $item['tax_class'] );
if ( ! $item_id || $tax_class == '0' )
return;
continue;
// Get product details
if ( get_post_type( $item_id ) == 'product' ) {
@ -1126,6 +1131,7 @@ function woocommerce_calc_line_taxes() {
$item_tax_status = 'taxable';
}
// Only calc if taxable
if ( $item_tax_status == 'taxable' ) {
$tax_rates = $tax->find_rates( array(
@ -1136,17 +1142,71 @@ function woocommerce_calc_line_taxes() {
'tax_class' => $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' ), '.' );
$line_subtotal_taxes = $tax->calc_tax( $line_subtotal, $tax_rates, false );
$line_taxes = $tax->calc_tax( $line_total, $tax_rates, false );
$line_subtotal_tax = rtrim( rtrim( number_format( array_sum( $line_subtotal_taxes ), 4, '.', '' ), '0' ), '.' );
$line_tax = rtrim( rtrim( number_format( array_sum( $line_taxes ), 4, '.', '' ), '0' ), '.' );
if ( $line_subtotal_tax < 0 )
$line_subtotal_tax = 0;
if ( $line_tax < 0 )
$line_tax = 0;
$item_taxes[ $item_id ] = array(
'line_subtotal_tax' => $line_subtotal_tax,
'line_tax' => $line_tax
);
// Sum the item taxes
foreach ( array_keys( $taxes + $line_taxes ) as $key )
$taxes[ $key ] = ( isset( $line_taxes[ $key ] ) ? $line_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
}
if ( $line_subtotal_tax_amount < 0 ) $line_subtotal_tax_amount = 0;
if ( $line_tax_amount < 0 ) $line_tax_amount = 0;
}
}
// Now calculate shipping tax
$matched_tax_rates = array();
$tax_rates = $tax->find_rates( array(
'country' => $country,
'state' => $state,
'postcode' => $postcode,
'city' => $city,
'tax_class' => ''
) );
if ( $tax_rates )
foreach ( $tax_rates as $key => $rate )
if ( isset( $rate['shipping'] ) && $rate['shipping'] == 'yes' )
$matched_tax_rates[ $key ] = $rate;
$shipping_taxes = $tax->calc_shipping_tax( $shipping, $matched_tax_rates );
$shipping_tax = rtrim( rtrim( number_format( array_sum( $shipping_taxes ), 2, '.', '' ), '0' ), '.' );
// Now merge to keep tax rows
foreach ( array_keys( $taxes + $shipping_taxes ) as $key ) {
$is_compound = $tax->is_compound( $key ) ? 1 : 0;
$cart_tax = isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0;
$shipping_tax = isset( $shipping_taxes[ $key ] ) ? $shipping_taxes[ $key ] : 0;
$tax_rows[] = array(
'label' => $tax->get_rate_label( $key ),
'compound' => $is_compound,
'cart_tax' => woocommerce_format_total( $cart_tax ),
'shipping_tax' => woocommerce_format_total( $shipping_tax )
);
}
// Return
echo json_encode( array(
'line_subtotal_tax' => $line_subtotal_tax_amount,
'line_tax' => $line_tax_amount
'item_taxes' => $item_taxes,
'shipping_tax' => $shipping_tax,
'tax_rows' => $tax_rows
) );
// Quit out