2018-11-16 03:35:10 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
2018-11-26 14:01:20 +00:00
|
|
|
import { map } from 'lodash';
|
2019-01-14 09:54:44 +00:00
|
|
|
import { Date, Link } from '@woocommerce/components';
|
2019-01-25 17:59:53 +00:00
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { formatValue } from '@woocommerce/number';
|
2020-02-17 01:12:35 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2018-11-16 03:35:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import { defaultTableDateFormat } from '../../../lib/date';
|
|
|
|
import ReportTable from '../../components/report-table';
|
|
|
|
import { CurrencyContext } from '../../../lib/currency-context';
|
2018-11-16 03:35:10 +00:00
|
|
|
|
2020-04-02 21:54:38 +00:00
|
|
|
class CouponsReportTable extends Component {
|
2018-11-26 14:01:20 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
|
|
|
this.getSummary = this.getSummary.bind( this );
|
|
|
|
}
|
|
|
|
|
2018-11-16 03:35:10 +00:00
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Coupon Code', 'woocommerce-admin' ),
|
2019-01-10 17:10:31 +00:00
|
|
|
key: 'code',
|
2018-11-16 03:35:10 +00:00
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
2019-01-10 17:10:31 +00:00
|
|
|
isSortable: true,
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Orders', 'woocommerce-admin' ),
|
2018-11-16 03:35:10 +00:00
|
|
|
key: 'orders_count',
|
|
|
|
required: true,
|
|
|
|
defaultSort: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Amount Discounted', 'woocommerce-admin' ),
|
2018-12-21 02:13:57 +00:00
|
|
|
key: 'amount',
|
2018-11-16 03:35:10 +00:00
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Created', 'woocommerce-admin' ),
|
2018-11-16 03:35:10 +00:00
|
|
|
key: 'created',
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Expires', 'woocommerce-admin' ),
|
2018-11-16 03:35:10 +00:00
|
|
|
key: 'expires',
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Type', 'woocommerce-admin' ),
|
2018-11-16 03:35:10 +00:00
|
|
|
key: 'type',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( coupons ) {
|
2019-01-25 17:59:53 +00:00
|
|
|
const { query } = this.props;
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
2020-02-17 01:12:35 +00:00
|
|
|
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
2020-06-17 23:33:40 +00:00
|
|
|
formatAmount,
|
2020-04-02 21:54:38 +00:00
|
|
|
formatDecimal: getCurrencyFormatDecimal,
|
2020-06-17 23:33:40 +00:00
|
|
|
getCurrencyConfig,
|
2020-04-02 21:54:38 +00:00
|
|
|
} = this.context;
|
2019-01-25 17:59:53 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return map( coupons, ( coupon ) => {
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
coupon_id: couponId,
|
|
|
|
orders_count: ordersCount,
|
|
|
|
} = coupon;
|
2020-02-14 02:23:21 +00:00
|
|
|
const extendedInfo = coupon.extended_info || {};
|
|
|
|
const {
|
|
|
|
code,
|
|
|
|
date_created: dateCreated,
|
|
|
|
date_expires: dateExpires,
|
|
|
|
discount_type: discountType,
|
|
|
|
} = extendedInfo;
|
2018-11-16 03:35:10 +00:00
|
|
|
|
2020-06-25 12:51:17 +00:00
|
|
|
const couponUrl =
|
|
|
|
couponId > 0
|
|
|
|
? getNewPath( persistedQuery, '/analytics/coupons', {
|
|
|
|
filter: 'single_coupon',
|
|
|
|
coupons: couponId,
|
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const couponLink =
|
|
|
|
couponUrl === null ? (
|
|
|
|
code
|
|
|
|
) : (
|
|
|
|
<Link href={ couponUrl } type="wc-admin">
|
|
|
|
{ code }
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
|
|
|
const ordersUrl =
|
|
|
|
couponId > 0
|
|
|
|
? getNewPath( persistedQuery, '/analytics/orders', {
|
|
|
|
filter: 'advanced',
|
|
|
|
coupon_includes: couponId,
|
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
const ordersLink =
|
|
|
|
ordersUrl === null ? (
|
|
|
|
ordersCount
|
|
|
|
) : (
|
|
|
|
<Link href={ ordersUrl } type="wc-admin">
|
|
|
|
{ formatValue(
|
|
|
|
getCurrencyConfig(),
|
|
|
|
'number',
|
|
|
|
ordersCount
|
|
|
|
) }
|
|
|
|
</Link>
|
|
|
|
);
|
2018-11-16 03:35:10 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: couponLink,
|
2018-12-21 02:13:57 +00:00
|
|
|
value: code,
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: ordersLink,
|
2020-02-14 02:23:21 +00:00
|
|
|
value: ordersCount,
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( amount ),
|
2018-12-21 02:13:57 +00:00
|
|
|
value: getCurrencyFormatDecimal( amount ),
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-25 12:51:17 +00:00
|
|
|
display: dateCreated ? (
|
2020-02-14 02:23:21 +00:00
|
|
|
<Date
|
|
|
|
date={ dateCreated }
|
2020-02-17 01:12:35 +00:00
|
|
|
visibleFormat={ dateFormat }
|
2020-02-14 02:23:21 +00:00
|
|
|
/>
|
2020-06-25 12:51:17 +00:00
|
|
|
) : (
|
|
|
|
__( 'N/A', 'woocommerce-admin' )
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
|
|
|
value: dateCreated,
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: dateExpires ? (
|
|
|
|
<Date
|
|
|
|
date={ dateExpires }
|
2020-02-17 01:12:35 +00:00
|
|
|
visibleFormat={ dateFormat }
|
2020-02-14 02:23:21 +00:00
|
|
|
/>
|
2019-01-14 09:54:44 +00:00
|
|
|
) : (
|
2019-03-13 17:14:02 +00:00
|
|
|
__( 'N/A', 'woocommerce-admin' )
|
2019-01-14 09:54:44 +00:00
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: dateExpires,
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: this.getCouponType( discountType ),
|
|
|
|
value: discountType,
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getSummary( totals ) {
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
|
|
|
coupons_count: couponsCount = 0,
|
|
|
|
orders_count: ordersCount = 0,
|
|
|
|
amount = 0,
|
|
|
|
} = totals;
|
2020-06-17 23:33:40 +00:00
|
|
|
const { formatAmount, getCurrencyConfig } = this.context;
|
|
|
|
const currency = getCurrencyConfig();
|
2018-11-16 03:35:10 +00:00
|
|
|
return [
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
|
|
|
'coupon',
|
|
|
|
'coupons',
|
|
|
|
couponsCount,
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', couponsCount ),
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
|
|
|
'order',
|
|
|
|
'orders',
|
|
|
|
ordersCount,
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', ordersCount ),
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'amount discounted', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( amount ),
|
2018-11-16 03:35:10 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
getCouponType( discountType ) {
|
2018-12-21 02:13:57 +00:00
|
|
|
const couponTypes = {
|
2019-03-13 17:14:02 +00:00
|
|
|
percent: __( 'Percentage', 'woocommerce-admin' ),
|
|
|
|
fixed_cart: __( 'Fixed cart', 'woocommerce-admin' ),
|
|
|
|
fixed_product: __( 'Fixed product', 'woocommerce-admin' ),
|
2018-12-21 02:13:57 +00:00
|
|
|
};
|
2020-06-25 12:51:17 +00:00
|
|
|
return couponTypes[ discountType ] || __( 'N/A', 'woocommerce-admin' );
|
2018-12-21 02:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 03:35:10 +00:00
|
|
|
render() {
|
2019-08-27 19:33:31 +00:00
|
|
|
const { advancedFilters, filters, isRequesting, query } = this.props;
|
2018-11-16 03:35:10 +00:00
|
|
|
|
|
|
|
return (
|
2018-11-26 14:01:20 +00:00
|
|
|
<ReportTable
|
|
|
|
compareBy="coupons"
|
|
|
|
endpoint="coupons"
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
getRowsContent={ this.getRowsContent }
|
|
|
|
getSummary={ this.getSummary }
|
2020-06-17 23:33:40 +00:00
|
|
|
summaryFields={ [ 'coupons_count', 'orders_count', 'amount' ] }
|
2019-02-26 09:28:50 +00:00
|
|
|
isRequesting={ isRequesting }
|
2018-11-26 14:01:20 +00:00
|
|
|
itemIdField="coupon_id"
|
2018-11-22 23:12:12 +00:00
|
|
|
query={ query }
|
2019-02-01 09:55:19 +00:00
|
|
|
searchBy="coupons"
|
2018-12-21 02:13:57 +00:00
|
|
|
tableQuery={ {
|
2019-01-25 17:59:53 +00:00
|
|
|
orderby: query.orderby || 'orders_count',
|
|
|
|
order: query.order || 'desc',
|
2018-12-21 02:13:57 +00:00
|
|
|
extended_info: true,
|
|
|
|
} }
|
2019-03-13 17:14:02 +00:00
|
|
|
title={ __( 'Coupons', 'woocommerce-admin' ) }
|
2018-12-13 20:34:23 +00:00
|
|
|
columnPrefsKey="coupons_report_columns"
|
2019-03-21 03:25:05 +00:00
|
|
|
filters={ filters }
|
2019-08-27 19:33:31 +00:00
|
|
|
advancedFilters={ advancedFilters }
|
2018-11-16 03:35:10 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 21:54:38 +00:00
|
|
|
|
|
|
|
CouponsReportTable.contextType = CurrencyContext;
|
|
|
|
|
|
|
|
export default CouponsReportTable;
|