2018-10-11 08:30:51 +00:00
|
|
|
|
/** @format */
|
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2019-01-18 02:52:58 +00:00
|
|
|
|
import { __, _n, _x, sprintf } from '@wordpress/i18n';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import { map } from 'lodash';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2018-10-30 18:57:48 +00:00
|
|
|
|
* WooCommerce dependencies
|
2018-10-11 08:30:51 +00:00
|
|
|
|
*/
|
2019-01-14 09:54:44 +00:00
|
|
|
|
import { Date, Link, OrderStatus, ViewMoreList } from '@woocommerce/components';
|
2019-01-18 02:52:58 +00:00
|
|
|
|
import { defaultTableDateFormat } from '@woocommerce/date';
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import { formatCurrency } from '@woocommerce/currency';
|
2019-01-29 16:48:46 +00:00
|
|
|
|
import { numberFormat } from '@woocommerce/number';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import ReportTable from 'analytics/components/report-table';
|
2019-01-20 22:31:03 +00:00
|
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
2018-10-18 10:43:45 +00:00
|
|
|
|
import './style.scss';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
|
export default class OrdersReportTable extends Component {
|
2018-12-03 03:40:57 +00:00
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
|
|
|
|
this.getSummary = this.getSummary.bind( this );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 08:30:51 +00:00
|
|
|
|
getHeadersContent() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Date', 'wc-admin' ),
|
2018-10-16 08:50:07 +00:00
|
|
|
|
key: 'date',
|
2018-10-11 08:30:51 +00:00
|
|
|
|
required: true,
|
|
|
|
|
defaultSort: true,
|
|
|
|
|
isLeftAligned: true,
|
|
|
|
|
isSortable: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Order #', 'wc-admin' ),
|
2018-11-30 20:44:02 +00:00
|
|
|
|
screenReaderLabel: __( 'Order ID', 'wc-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'id',
|
|
|
|
|
required: true,
|
|
|
|
|
isSortable: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Status', 'wc-admin' ),
|
|
|
|
|
key: 'status',
|
|
|
|
|
required: false,
|
2018-10-16 08:50:07 +00:00
|
|
|
|
isSortable: false,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Customer', 'wc-admin' ),
|
|
|
|
|
key: 'customer_id',
|
|
|
|
|
required: false,
|
2018-10-16 08:50:07 +00:00
|
|
|
|
isSortable: false,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Product(s)', 'wc-admin' ),
|
2018-11-30 20:44:02 +00:00
|
|
|
|
screenReaderLabel: __( 'Products', 'wc-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'products',
|
|
|
|
|
required: false,
|
|
|
|
|
isSortable: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Items Sold', 'wc-admin' ),
|
|
|
|
|
key: 'items_sold',
|
|
|
|
|
required: false,
|
2018-10-16 08:50:07 +00:00
|
|
|
|
isSortable: false,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
isNumeric: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'Coupon(s)', 'wc-admin' ),
|
2018-11-30 20:44:02 +00:00
|
|
|
|
screenReaderLabel: __( 'Coupons', 'wc-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'coupons',
|
|
|
|
|
required: false,
|
|
|
|
|
isSortable: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'N. Revenue', 'wc-admin' ),
|
2018-11-30 20:44:02 +00:00
|
|
|
|
screenReaderLabel: __( 'Net Revenue', 'wc-admin' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
key: 'net_revenue',
|
|
|
|
|
required: true,
|
2018-10-16 08:50:07 +00:00
|
|
|
|
isSortable: false,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
isNumeric: true,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRowsContent( tableData ) {
|
2019-01-20 22:31:03 +00:00
|
|
|
|
const { query } = this.props;
|
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
2018-10-11 08:30:51 +00:00
|
|
|
|
return map( tableData, row => {
|
|
|
|
|
const {
|
2019-01-18 02:52:58 +00:00
|
|
|
|
currency,
|
|
|
|
|
customer_type,
|
|
|
|
|
date_created,
|
|
|
|
|
extended_info,
|
|
|
|
|
net_total,
|
|
|
|
|
num_items_sold,
|
|
|
|
|
order_id,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
status,
|
|
|
|
|
} = row;
|
2019-01-18 02:52:58 +00:00
|
|
|
|
const { coupons, products } = extended_info;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
|
const formattedProducts = products
|
2018-10-12 21:29:25 +00:00
|
|
|
|
.sort( ( itemA, itemB ) => itemB.quantity - itemA.quantity )
|
|
|
|
|
.map( item => ( {
|
|
|
|
|
label: item.name,
|
|
|
|
|
quantity: item.quantity,
|
2019-01-20 22:31:03 +00:00
|
|
|
|
href: getNewPath( persistedQuery, 'products', {
|
|
|
|
|
filter: 'single_product',
|
|
|
|
|
products: item.id,
|
|
|
|
|
} ),
|
2018-10-12 21:29:25 +00:00
|
|
|
|
} ) );
|
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
|
const formattedCoupons = coupons.map( coupon => ( {
|
2018-10-12 21:29:25 +00:00
|
|
|
|
label: coupon.code,
|
2019-01-20 22:31:03 +00:00
|
|
|
|
href: getNewPath( persistedQuery, 'coupons', {
|
|
|
|
|
filter: 'single_coupon',
|
|
|
|
|
coupons: coupon.id,
|
|
|
|
|
} ),
|
2018-10-12 21:29:25 +00:00
|
|
|
|
} ) );
|
|
|
|
|
|
2018-10-11 08:30:51 +00:00
|
|
|
|
return [
|
|
|
|
|
{
|
2019-01-18 02:52:58 +00:00
|
|
|
|
display: <Date date={ date_created } visibleFormat={ defaultTableDateFormat } />,
|
|
|
|
|
value: date_created,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-12-18 09:44:43 +00:00
|
|
|
|
display: (
|
2019-01-18 02:52:58 +00:00
|
|
|
|
<Link href={ 'post.php?post=' + order_id + '&action=edit' } type="wp-admin">
|
|
|
|
|
{ order_id }
|
2018-12-18 09:44:43 +00:00
|
|
|
|
</Link>
|
|
|
|
|
),
|
2019-01-18 02:52:58 +00:00
|
|
|
|
value: order_id,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-10-18 10:43:45 +00:00
|
|
|
|
display: (
|
|
|
|
|
<OrderStatus className="woocommerce-orders-table__status" order={ { status } } />
|
|
|
|
|
),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
value: status,
|
|
|
|
|
},
|
|
|
|
|
{
|
2019-01-18 02:52:58 +00:00
|
|
|
|
display:
|
|
|
|
|
customer_type === 'new'
|
|
|
|
|
? _x( 'New', 'customer type', 'wc-admin' )
|
|
|
|
|
: _x( 'Returning', 'customer type', 'wc-admin' ),
|
|
|
|
|
value: customer_type,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
display: this.renderList(
|
2019-01-18 02:52:58 +00:00
|
|
|
|
formattedProducts.length ? [ formattedProducts[ 0 ] ] : [],
|
|
|
|
|
formattedProducts.map( product => ( {
|
2018-10-12 21:29:25 +00:00
|
|
|
|
label: sprintf( __( '%s× %s', 'wc-admin' ), product.quantity, product.label ),
|
|
|
|
|
href: product.href,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
} ) )
|
|
|
|
|
),
|
2019-01-18 02:52:58 +00:00
|
|
|
|
value: formattedProducts.map( product => product.label ).join( ' ' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-01-18 02:52:58 +00:00
|
|
|
|
display: numberFormat( num_items_sold ),
|
|
|
|
|
value: num_items_sold,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-01-18 02:52:58 +00:00
|
|
|
|
display: this.renderList(
|
|
|
|
|
formattedCoupons.length ? [ formattedCoupons[ 0 ] ] : [],
|
|
|
|
|
formattedCoupons
|
|
|
|
|
),
|
|
|
|
|
value: formattedCoupons.map( item => item.code ).join( ' ' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-01-18 02:52:58 +00:00
|
|
|
|
display: formatCurrency( net_total, currency ),
|
|
|
|
|
value: net_total,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 19:09:38 +00:00
|
|
|
|
getSummary( totals ) {
|
|
|
|
|
if ( ! totals ) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
{
|
2018-12-05 21:54:52 +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-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'new customer', 'new customers', totals.num_new_customers, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
|
value: numberFormat( totals.num_new_customers ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n(
|
|
|
|
|
'returning customer',
|
|
|
|
|
'returning customers',
|
|
|
|
|
totals.num_returning_customers,
|
|
|
|
|
'wc-admin'
|
|
|
|
|
),
|
2018-11-26 03:31:38 +00:00
|
|
|
|
value: numberFormat( totals.num_returning_customers ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'product', 'products', totals.products, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
|
value: numberFormat( totals.products ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'item sold', 'items sold', totals.num_items_sold, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
|
value: numberFormat( totals.num_items_sold ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'coupon', 'coupons', totals.coupons, 'wc-admin' ),
|
2018-11-26 03:31:38 +00:00
|
|
|
|
value: numberFormat( totals.coupons ),
|
2018-10-31 19:09:38 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'net revenue', 'wc-admin' ),
|
|
|
|
|
value: formatCurrency( totals.net_revenue ),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 21:29:25 +00:00
|
|
|
|
renderLinks( items = [] ) {
|
2018-10-18 20:45:59 +00:00
|
|
|
|
return items.map( ( item, i ) => (
|
2019-01-20 22:31:03 +00:00
|
|
|
|
<Link href={ item.href } key={ i } type="wc-admin">
|
2018-10-12 21:29:25 +00:00
|
|
|
|
{ item.label }
|
|
|
|
|
</Link>
|
2018-10-11 08:30:51 +00:00
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 21:29:25 +00:00
|
|
|
|
renderList( visibleItems, popoverItems ) {
|
|
|
|
|
return (
|
|
|
|
|
<Fragment>
|
|
|
|
|
{ this.renderLinks( visibleItems ) }
|
|
|
|
|
{ popoverItems.length > 1 && <ViewMoreList items={ this.renderLinks( popoverItems ) } /> }
|
|
|
|
|
</Fragment>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
render() {
|
2019-01-18 02:52:58 +00:00
|
|
|
|
const { query } = this.props;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
|
|
|
|
return (
|
2018-12-03 03:40:57 +00:00
|
|
|
|
<ReportTable
|
|
|
|
|
endpoint="orders"
|
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
|
getRowsContent={ this.getRowsContent }
|
|
|
|
|
getSummary={ this.getSummary }
|
|
|
|
|
query={ query }
|
2019-01-18 02:52:58 +00:00
|
|
|
|
tableQuery={ {
|
|
|
|
|
extended_info: true,
|
|
|
|
|
} }
|
2018-10-16 08:50:07 +00:00
|
|
|
|
title={ __( 'Orders', 'wc-admin' ) }
|
2018-12-13 20:34:23 +00:00
|
|
|
|
columnPrefsKey="orders_report_columns"
|
2018-10-11 08:30:51 +00:00
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-10-29 07:57:05 +00:00
|
|
|
|
}
|