2018-11-19 15:55:15 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
import { get, map, orderBy } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { Link, TableCard } from '@woocommerce/components';
|
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
|
|
|
import { onQueryChange } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ReportError from 'analytics/components/report-error';
|
2018-11-22 23:12:12 +00:00
|
|
|
import { getReportChartData, getReportTableData } from 'store/reports/utils';
|
2018-11-26 03:31:38 +00:00
|
|
|
import { numberFormat } from 'lib/number';
|
2018-11-19 15:55:15 +00:00
|
|
|
|
|
|
|
class TaxesReportTable extends Component {
|
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: __( 'Tax Code', 'wc-admin' ),
|
|
|
|
// @TODO it should be the tax code, not the ID
|
|
|
|
key: 'tax_rate_id',
|
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Rate', 'wc-admin' ),
|
|
|
|
key: 'rate',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Total Tax', 'wc-admin' ),
|
|
|
|
key: 'total_tax',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Order Tax', 'wc-admin' ),
|
|
|
|
key: 'order_tax',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Shipping Tax', 'wc-admin' ),
|
|
|
|
key: 'shipping_tax',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Orders', 'wc-admin' ),
|
|
|
|
key: 'orders_count',
|
|
|
|
required: true,
|
|
|
|
defaultSort: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( taxes ) {
|
|
|
|
return map( taxes, tax => {
|
|
|
|
const { order_tax, orders_count, tax_rate_id, total_tax, shipping_tax } = tax;
|
|
|
|
|
|
|
|
// @TODO must link to the tax detail report
|
|
|
|
const taxLink = (
|
|
|
|
<Link href="" type="wc-admin">
|
|
|
|
{ tax_rate_id }
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
|
|
|
// @TODO it should be the tax code, not the tax ID
|
|
|
|
{
|
|
|
|
display: taxLink,
|
|
|
|
value: tax_rate_id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// @TODO add `rate` once it's returned by the API
|
|
|
|
display: '',
|
|
|
|
value: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( total_tax ),
|
|
|
|
value: getCurrencyFormatDecimal( total_tax ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( order_tax ),
|
|
|
|
value: getCurrencyFormatDecimal( order_tax ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( shipping_tax ),
|
|
|
|
value: getCurrencyFormatDecimal( shipping_tax ),
|
|
|
|
},
|
|
|
|
{
|
2018-11-26 03:31:38 +00:00
|
|
|
display: numberFormat( orders_count ),
|
2018-11-19 15:55:15 +00:00
|
|
|
value: orders_count,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getSummary( totals ) {
|
|
|
|
if ( ! totals ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// @TODO the number of total rows should come from the API
|
|
|
|
const totalRows = 0;
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: _n( 'tax code', 'tax codes', totalRows, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
value: numberFormat( totalRows ),
|
2018-11-19 15:55:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'total tax', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.total_tax ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'order tax', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.order_tax ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'shipping tax', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.shipping_tax ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: _n( 'order', 'orders', totals.orders_count, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
value: numberFormat( totals.orders_count ),
|
2018-11-19 15:55:15 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-11-22 23:12:12 +00:00
|
|
|
const { primaryData, tableData } = this.props;
|
|
|
|
const { items, query } = tableData;
|
2018-11-19 15:55:15 +00:00
|
|
|
|
2018-11-22 23:12:12 +00:00
|
|
|
const isError = tableData.isError || primaryData.isError;
|
2018-11-19 15:55:15 +00:00
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
2018-11-22 23:12:12 +00:00
|
|
|
const isRequesting = tableData.isLoading || primaryData.isRequesting;
|
2018-11-19 15:55:15 +00:00
|
|
|
|
|
|
|
const headers = this.getHeadersContent();
|
2018-11-22 23:12:12 +00:00
|
|
|
const orderedTaxes = orderBy( items, query.orderby, query.order );
|
2018-11-19 15:55:15 +00:00
|
|
|
const rows = this.getRowsContent( orderedTaxes );
|
2018-11-22 23:12:12 +00:00
|
|
|
const totalRows = get( primaryData, [ 'data', 'totals', 'taxes_count' ], items.length );
|
2018-11-19 15:55:15 +00:00
|
|
|
const summary = primaryData.data.totals ? this.getSummary( primaryData.data.totals ) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TableCard
|
|
|
|
title={ __( 'Taxes', 'wc-admin' ) }
|
|
|
|
compareBy={ 'taxes' }
|
|
|
|
ids={ orderedTaxes.map( tax => tax.tax_rate_id ) }
|
|
|
|
rows={ rows }
|
|
|
|
totalRows={ totalRows }
|
2018-11-22 23:12:12 +00:00
|
|
|
rowsPerPage={ query.per_page }
|
2018-11-19 15:55:15 +00:00
|
|
|
headers={ headers }
|
|
|
|
isLoading={ isRequesting }
|
|
|
|
onQueryChange={ onQueryChange }
|
2018-11-22 23:12:12 +00:00
|
|
|
query={ query }
|
2018-11-19 15:55:15 +00:00
|
|
|
summary={ summary }
|
|
|
|
downloadable
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
const { query } = props;
|
|
|
|
const primaryData = getReportChartData( 'taxes', 'primary', query, select );
|
2018-11-22 23:12:12 +00:00
|
|
|
const tableData = getReportTableData( 'taxes', query, select );
|
2018-11-19 15:55:15 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
primaryData,
|
2018-11-22 23:12:12 +00:00
|
|
|
tableData,
|
2018-11-19 15:55:15 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( TaxesReportTable );
|