2018-06-07 16:05:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { find, get } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total for the discount value
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Total amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderDiscountTotal( order ) {
|
|
|
|
const coupons = get( order, 'coupon_lines', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
const total = coupons.reduce(
|
|
|
|
( sum, value ) => sum + parseFloat( value.discount ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
return parseFloat( total ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value for a single fee on a given order
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {number} id The ID of the fee_line
|
|
|
|
* @return {number} The total fee amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderFeeCost( order, id ) {
|
|
|
|
const item = find( get( order, 'fee_lines', [] ), { id } );
|
|
|
|
if ( item ) {
|
|
|
|
return parseFloat( item.total ) || 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the fee total on a given order
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} The total fee amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderFeeTotal( order ) {
|
|
|
|
const fees = get( order, 'fee_lines', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
const total = fees.reduce(
|
|
|
|
( sum, value ) => sum + parseFloat( value.total ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
return parseFloat( total ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the individual price for a given item, pre-discounts.
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {number} id The ID of the line_item
|
|
|
|
* @return {number} Total amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderItemCost( order, id ) {
|
|
|
|
const item = find( get( order, 'line_items', [] ), { id } );
|
|
|
|
const subtotal = parseFloat( get( item, 'subtotal', 0 ) ) || 0;
|
|
|
|
const qty = parseFloat( get( item, 'quantity', 1 ) ) || 1;
|
|
|
|
return subtotal / qty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the refund value on a given order
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} The refund amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderRefundTotal( order ) {
|
|
|
|
const refunds = get( order, 'refunds', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
const total = refunds.reduce(
|
|
|
|
( sum, value ) => sum + parseFloat( value.total ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
return parseFloat( total ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total for the shipping value
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Total amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderShippingTotal( order ) {
|
|
|
|
const shipping = get( order, 'shipping_lines', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
const total = shipping.reduce(
|
|
|
|
( sum, value ) => sum + parseFloat( value.total ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
return parseFloat( total ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total for the subtotal value (total of all line items)
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Total amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderSubtotal( order ) {
|
|
|
|
const items = get( order, 'line_items', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
const total = items.reduce(
|
|
|
|
( sum, value ) => sum + parseFloat( value.subtotal ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
return parseFloat( total ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total value on a given order
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} The total amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderTotal( order ) {
|
|
|
|
const discount = getOrderDiscountTotal( order );
|
|
|
|
const fees = getOrderFeeTotal( order );
|
|
|
|
const refunds = getOrderRefundTotal( order );
|
|
|
|
const shipping = getOrderShippingTotal( order );
|
|
|
|
const subtotal = getOrderSubtotal( order );
|
|
|
|
// Refunds are negative, but discounts are not
|
|
|
|
return subtotal - discount + fees + shipping + refunds;
|
|
|
|
}
|