2018-11-16 03:35:10 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { format as formatDate } from '@wordpress/date';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
import { get, map, orderBy } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2018-11-22 23:12:12 +00:00
|
|
|
import { getIntervalForQuery, getDateFormatsForInterval } from '@woocommerce/date';
|
2018-11-16 03:35:10 +00:00
|
|
|
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-16 03:35:10 +00:00
|
|
|
|
|
|
|
class CouponsReportTable extends Component {
|
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: __( 'Coupon Code', 'wc-admin' ),
|
|
|
|
// @TODO it should be the coupon code, not the coupon ID
|
|
|
|
key: 'coupon_id',
|
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Orders', 'wc-admin' ),
|
|
|
|
key: 'orders_count',
|
|
|
|
required: true,
|
|
|
|
defaultSort: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'G. Discounted', 'wc-admin' ),
|
|
|
|
key: 'gross_discount',
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Created', 'wc-admin' ),
|
|
|
|
key: 'created',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Expires', 'wc-admin' ),
|
|
|
|
key: 'expires',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Type', 'wc-admin' ),
|
|
|
|
key: 'type',
|
|
|
|
isSortable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( coupons ) {
|
|
|
|
const { query } = this.props;
|
|
|
|
const currentInterval = getIntervalForQuery( query );
|
|
|
|
const { tableFormat } = getDateFormatsForInterval( currentInterval );
|
|
|
|
|
|
|
|
return map( coupons, coupon => {
|
|
|
|
const { coupon_id, gross_discount, orders_count } = coupon;
|
|
|
|
|
|
|
|
// @TODO must link to the coupon detail report
|
|
|
|
const couponLink = (
|
|
|
|
<Link href="" type="wc-admin">
|
|
|
|
{ coupon_id }
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
|
|
|
const ordersLink = (
|
|
|
|
<Link
|
|
|
|
href={ '/analytics/orders?filter=advanced&code_includes=' + coupon_id }
|
|
|
|
type="wc-admin"
|
|
|
|
>
|
2018-11-26 03:31:38 +00:00
|
|
|
{ numberFormat( orders_count ) }
|
2018-11-16 03:35:10 +00:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
|
|
|
// @TODO it should be the coupon code, not the coupon ID
|
|
|
|
{
|
|
|
|
display: couponLink,
|
|
|
|
value: coupon_id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: ordersLink,
|
|
|
|
value: orders_count,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( gross_discount ),
|
|
|
|
value: getCurrencyFormatDecimal( gross_discount ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// @TODO
|
|
|
|
display: formatDate( tableFormat, '' ),
|
|
|
|
value: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// @TODO
|
|
|
|
display: formatDate( tableFormat, '' ),
|
|
|
|
value: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// @TODO
|
|
|
|
display: '',
|
|
|
|
value: '',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getSummary( totals ) {
|
|
|
|
if ( ! totals ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: _n( 'coupon', 'coupons', totals.coupons_count, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
value: numberFormat( totals.coupons_count ),
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: _n( 'order', 'orders', totals.orders_count, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
value: numberFormat( totals.orders_count ),
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'gross discounted', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.gross_discount ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-11-22 23:12:12 +00:00
|
|
|
const { tableData, primaryData } = this.props;
|
|
|
|
const { items, query } = tableData;
|
2018-11-16 03:35:10 +00:00
|
|
|
|
2018-11-22 23:12:12 +00:00
|
|
|
const isError = tableData.isError || primaryData.isError;
|
2018-11-16 03:35:10 +00:00
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
return <ReportError isError />;
|
|
|
|
}
|
|
|
|
|
2018-11-22 23:12:12 +00:00
|
|
|
const isRequesting = tableData.isRequesting || primaryData.isRequesting;
|
2018-11-16 03:35:10 +00:00
|
|
|
|
|
|
|
const headers = this.getHeadersContent();
|
2018-11-22 23:12:12 +00:00
|
|
|
const orderedCoupons = orderBy( items, query.orderby, query.order );
|
2018-11-16 03:35:10 +00:00
|
|
|
const rows = this.getRowsContent( orderedCoupons );
|
2018-11-22 23:12:12 +00:00
|
|
|
const totalRows = get( primaryData, [ 'data', 'totals', 'coupons_count' ], items.length );
|
2018-11-16 03:35:10 +00:00
|
|
|
const summary = primaryData.data.totals ? this.getSummary( primaryData.data.totals ) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TableCard
|
|
|
|
title={ __( 'Coupons', 'wc-admin' ) }
|
|
|
|
compareBy={ 'coupons' }
|
|
|
|
ids={ orderedCoupons.map( coupon => coupon.coupon_id ) }
|
|
|
|
rows={ rows }
|
|
|
|
totalRows={ totalRows }
|
2018-11-22 23:12:12 +00:00
|
|
|
rowsPerPage={ query.per_page }
|
2018-11-16 03:35:10 +00:00
|
|
|
headers={ headers }
|
|
|
|
isLoading={ isRequesting }
|
|
|
|
onQueryChange={ onQueryChange }
|
2018-11-22 23:12:12 +00:00
|
|
|
query={ query }
|
2018-11-16 03:35:10 +00:00
|
|
|
summary={ summary }
|
|
|
|
downloadable
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
const { query } = props;
|
|
|
|
const primaryData = getReportChartData( 'coupons', 'primary', query, select );
|
2018-11-22 23:12:12 +00:00
|
|
|
const tableData = getReportTableData( 'coupons', query, select );
|
2018-11-16 03:35:10 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
primaryData,
|
2018-11-22 23:12:12 +00:00
|
|
|
tableData,
|
2018-11-16 03:35:10 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( CouponsReportTable );
|