A start on negative line items

@claudiosmweb
This commit is contained in:
Mike Jolley 2014-07-23 17:41:35 +01:00
parent dcf4a4e5a8
commit 11835826de
6 changed files with 64 additions and 18 deletions

File diff suppressed because one or more lines are too long

View File

@ -695,6 +695,9 @@ ul.wc_coupon_list_block {
h3 small {
color: #999;
}
.amount {
white-space: nowrap;
}
}
.woocommerce_order_items_wrapper {
margin: 0;

View File

@ -582,10 +582,11 @@ jQuery( function ( $ ) {
if ( window.confirm( woocommerce_admin_meta_boxes.i18n_do_refund ) ) {
var refund_amount = $( 'input#refund_amount' ).val();
var refund_reason = $( 'input#refund_reason' ).val();
var refund_qty = $.map( $( 'input[type=number][name^=order_item_refund_qty]' ), function( item ) {
var result = [];
result.push( $( item ).closest( 'tr.item,tr.fee' ).data( 'order_item_id' ), item.value );
return result;
var refund_qty = {};
$( 'input[type=number][name^=order_item_refund_qty]' ).each( function( index, item ) {
if ( $( item ).closest( 'tr' ).data( 'order_item_id' ) ) {
refund_qty[ $( item ).closest( 'tr' ).data( 'order_item_id' ) ] = item.value;
}
});
var data = {
action: 'woocommerce_refund_line_items',
@ -602,6 +603,8 @@ jQuery( function ( $ ) {
} else if ( response.error ) {
window.alert( response.error );
removeOrderItemsLoading();
} else {
console.log(response);
}
});
} else {

File diff suppressed because one or more lines are too long

View File

@ -1792,11 +1792,10 @@ class WC_AJAX {
$order_id = absint( $_POST['order_id'] );
$refund_amount = sanitize_text_field( $_POST['refund_amount'] );
$refund_reason = sanitize_text_field( $_POST['refund_reason'] );
$refund_qty = json_decode( sanitize_text_field( stripslashes( $_POST['refund_qty'] ) ) );
$refund_qty = json_decode( sanitize_text_field( stripslashes( $_POST['refund_qty'] ) ), true );
$api_refund = $_POST['api_refund'] === 'true' ? true : false;
try {
// Validate that the refund can occur
$order = get_order( $order_id );
$max_refund = $order->get_total() - $order->get_total_refunded();
@ -1809,7 +1808,8 @@ class WC_AJAX {
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id
'order_id' => $order_id,
'line_item_qty' => $refund_qty
) );
if ( is_wp_error( $refund ) ) {

View File

@ -529,7 +529,8 @@ function wc_create_refund( $args = array() ) {
'amount' => '',
'reason' => null,
'order_id' => 0,
'refund_id' => 0
'refund_id' => 0,
'line_item_qty' => array()
);
$args = wp_parse_args( $args, $default_args );
@ -563,11 +564,50 @@ function wc_create_refund( $args = array() ) {
return $refund_id;
}
// Default refund meta data
if ( ! $updating ) {
// Default refund meta data
update_post_meta( $refund_id, '_refund_amount', wc_format_decimal( $args['amount'] ) );
// Negative line items
if ( sizeof( $args['line_item_qty'] ) > 0 ) {
$order = get_order( $args['order_id'] );
$order_items = $order->get_items();
$refund = get_order( $refund_id );
$order_taxes = array();
foreach ( $args['line_item_qty'] as $refund_item_id => $refund_qty ) {
if ( isset( $order_items[ $refund_item_id ] ) && $refund_qty ) {
$refund_item = $order_items[ $refund_item_id ];
$product = $order->get_product_from_item( $refund_item );
$qty = $refund_item['qty'];
$args = array();
$args['totals']['subtotal'] = ( ( $refund_item['line_subtotal'] / $qty ) * $refund_qty ) * -1;
$args['totals']['subtotal_tax'] = ( ( $refund_item['line_subtotal_tax'] / $qty ) * $refund_qty ) * -1;
$args['totals']['total'] = ( ( $refund_item['line_total'] / $qty ) * $refund_qty ) * -1;
$args['totals']['tax'] = ( ( $refund_item['line_tax'] / $qty ) * $refund_qty ) * -1;
$args['totals']['tax_data'] = array( 'total' => array(), 'subtotal' => array() );
if ( isset( $refund_item['line_tax_data'] ) ) {
$line_tax_data = maybe_unserialize( $refund_item['line_tax_data'] );
foreach ( $line_tax_data['total'] as $key => $tax ) {
$args['totals']['tax_data']['total'][ $key ] = ( ( $tax / $qty ) * $refund_qty ) * -1;
$order_taxes[ $key ] += ( ( $tax / $qty ) * $refund_qty ) * -1;
}
foreach ( $line_tax_data['subtotal'] as $key => $tax ) {
$args['totals']['tax_data']['subtotal'][ $key ] = ( ( $tax / $qty ) * $refund_qty ) * -1;
}
}
$refund->add_product( $product, $refund_qty, $args );
}
}
// Save order tax rows
foreach ( $order_taxes as $tax_rate_id => $tax_amount ) {
$refund->add_tax( $tax_rate_id, $tax_amount );
}
}
}
return new WC_Order_Refund( $refund_id );
}