2018-06-07 16:05:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { find, get } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax for the discount value
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderDiscountTax( order ) {
|
|
|
|
const coupons = get( order, 'coupon_lines', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
const tax = coupons.reduce(
|
|
|
|
( sum, value ) => sum + parseFloat( value.discount_tax ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
return parseFloat( tax ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax for a given line item's value
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {number} id The ID of the line_item
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderLineItemTax( order, id ) {
|
|
|
|
const items = get( order, 'line_items', [] );
|
|
|
|
const tax = get( find( items, { id } ), 'taxes[0].total', 0 );
|
|
|
|
return parseFloat( tax ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax for a given fee
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {number} id The ID of a fee line in this order
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderFeeTax( order, id ) {
|
|
|
|
const items = get( order, 'fee_lines', [] );
|
|
|
|
const tax = get( find( items, { id } ), 'taxes[0].total', 0 );
|
|
|
|
return parseFloat( tax ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax for all fees in an order (total of all fee lines)
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderFeeTotalTax( order ) {
|
|
|
|
const lines = get( order, 'fee_lines', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
return lines.reduce(
|
|
|
|
( sum, value ) => sum + getOrderFeeTax( order, value.id ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax for the shipping value
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderShippingTax( order ) {
|
|
|
|
const tax = get( order, 'shipping_lines[0].taxes[0].total', 0 );
|
|
|
|
return parseFloat( tax ) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax 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} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderSubtotalTax( order ) {
|
|
|
|
const items = get( order, 'line_items', [] );
|
2020-02-14 02:23:21 +00:00
|
|
|
return items.reduce(
|
|
|
|
( sum, value ) => sum + getOrderLineItemTax( order, value.id ),
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total tax for the total value
|
|
|
|
*
|
|
|
|
* @param {Object} order An order as returned from API
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {number} Tax amount as a decimal number
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
|
|
|
export function getOrderTotalTax( order ) {
|
|
|
|
const subtotal = getOrderSubtotalTax( order );
|
|
|
|
const shipping = getOrderShippingTax( order );
|
|
|
|
const fees = getOrderFeeTotalTax( order );
|
|
|
|
return subtotal + shipping + fees;
|
|
|
|
}
|