2018-10-11 08:30:51 +00:00
|
|
|
|
/** @format */
|
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2018-10-31 19:09:38 +00:00
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
|
import { format as formatDate } from '@wordpress/date';
|
2018-10-29 07:57:05 +00:00
|
|
|
|
import { compose } from '@wordpress/compose';
|
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
|
|
|
|
*/
|
2018-10-29 07:57:05 +00:00
|
|
|
|
import {
|
|
|
|
|
appendTimestamp,
|
|
|
|
|
getCurrentDates,
|
|
|
|
|
getIntervalForQuery,
|
|
|
|
|
getDateFormatsForInterval,
|
2018-10-30 18:57:48 +00:00
|
|
|
|
} from '@woocommerce/date';
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import { Link, OrderStatus, ViewMoreList } from '@woocommerce/components';
|
|
|
|
|
import { formatCurrency } from '@woocommerce/currency';
|
|
|
|
|
import { getAdminLink } from '@woocommerce/navigation';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2018-10-26 08:19:39 +00:00
|
|
|
|
import { QUERY_DEFAULTS } from 'store/constants';
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import { getFilterQuery } from 'store/reports/utils';
|
2018-11-26 03:31:38 +00:00
|
|
|
|
import { numberFormat } from 'lib/number';
|
2018-11-29 15:57:49 +00:00
|
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-12-03 03:40:57 +00:00
|
|
|
|
import ReportTable from 'analytics/components/report-table';
|
|
|
|
|
import { formatTableOrders } from './utils';
|
2018-10-18 10:43:45 +00:00
|
|
|
|
import './style.scss';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
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 ) {
|
|
|
|
|
const { query } = this.props;
|
|
|
|
|
const currentInterval = getIntervalForQuery( query );
|
|
|
|
|
const { tableFormat } = getDateFormatsForInterval( currentInterval );
|
|
|
|
|
return map( tableData, row => {
|
|
|
|
|
const {
|
2018-10-16 08:50:07 +00:00
|
|
|
|
date,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
id,
|
|
|
|
|
status,
|
|
|
|
|
customer_id,
|
|
|
|
|
line_items,
|
|
|
|
|
items_sold,
|
|
|
|
|
coupon_lines,
|
|
|
|
|
currency,
|
|
|
|
|
net_revenue,
|
|
|
|
|
} = row;
|
|
|
|
|
|
2018-10-12 21:29:25 +00:00
|
|
|
|
const products = line_items
|
|
|
|
|
.sort( ( itemA, itemB ) => itemB.quantity - itemA.quantity )
|
|
|
|
|
.map( item => ( {
|
|
|
|
|
label: item.name,
|
|
|
|
|
href: 'post.php?post=' + item.product_id + '&action=edit',
|
|
|
|
|
quantity: item.quantity,
|
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
|
|
const coupons = coupon_lines.map( coupon => ( {
|
|
|
|
|
label: coupon.code,
|
|
|
|
|
// @TODO It should link to the coupons report
|
|
|
|
|
href: 'edit.php?s=' + coupon.code + '&post_type=shop_coupon',
|
|
|
|
|
} ) );
|
|
|
|
|
|
2018-10-11 08:30:51 +00:00
|
|
|
|
return [
|
|
|
|
|
{
|
2018-10-16 08:50:07 +00:00
|
|
|
|
display: formatDate( tableFormat, date ),
|
|
|
|
|
value: date,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
display: <a href={ getAdminLink( 'post.php?post=' + id + '&action=edit' ) }>{ id }</a>,
|
|
|
|
|
value: id,
|
|
|
|
|
},
|
|
|
|
|
{
|
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,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// @TODO This should display customer type (new/returning) once it's
|
|
|
|
|
// implemented in the API.
|
|
|
|
|
display: customer_id,
|
|
|
|
|
value: customer_id,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
display: this.renderList(
|
2018-10-12 21:29:25 +00:00
|
|
|
|
products.length ? [ products[ 0 ] ] : [],
|
|
|
|
|
products.map( product => ( {
|
|
|
|
|
label: sprintf( __( '%s× %s', 'wc-admin' ), product.quantity, product.label ),
|
|
|
|
|
href: product.href,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
} ) )
|
|
|
|
|
),
|
2018-10-12 21:29:25 +00:00
|
|
|
|
value: products.map( product => product.label ).join( ' ' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2018-11-26 03:31:38 +00:00
|
|
|
|
display: numberFormat( items_sold ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
value: items_sold,
|
|
|
|
|
},
|
|
|
|
|
{
|
2018-10-12 21:29:25 +00:00
|
|
|
|
display: this.renderList( coupons.length ? [ coupons[ 0 ] ] : [], coupons ),
|
|
|
|
|
value: coupons.map( item => item.code ).join( ' ' ),
|
2018-10-11 08:30:51 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
display: formatCurrency( net_revenue, currency ),
|
|
|
|
|
value: net_revenue,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
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 ) => (
|
2018-10-24 07:50:05 +00:00
|
|
|
|
<Link href={ item.href } key={ i } type="wp-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() {
|
2018-12-03 03:40:57 +00:00
|
|
|
|
const { query, tableData } = 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 }
|
|
|
|
|
tableData={ tableData }
|
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
|
|
|
|
}
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
export default compose(
|
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
|
const { query } = props;
|
|
|
|
|
const datesFromQuery = getCurrentDates( query );
|
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
2018-10-23 07:49:04 +00:00
|
|
|
|
|
2018-12-03 03:40:57 +00:00
|
|
|
|
const { getOrders, getOrdersTotalCount, isGetOrdersError, isGetOrdersRequesting } = select(
|
|
|
|
|
'wc-api'
|
|
|
|
|
);
|
2018-11-29 15:57:49 +00:00
|
|
|
|
|
2018-10-23 07:49:04 +00:00
|
|
|
|
const tableQuery = {
|
|
|
|
|
orderby: query.orderby || 'date',
|
|
|
|
|
order: query.order || 'asc',
|
2018-10-29 07:57:05 +00:00
|
|
|
|
page: query.page || 1,
|
|
|
|
|
per_page: query.per_page || QUERY_DEFAULTS.pageSize,
|
|
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
|
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
|
|
|
|
|
status: [ 'processing', 'on-hold', 'completed' ],
|
|
|
|
|
...filterQuery,
|
2018-10-23 07:49:04 +00:00
|
|
|
|
};
|
2018-10-29 07:57:05 +00:00
|
|
|
|
const orders = getOrders( tableQuery );
|
2018-12-03 03:40:57 +00:00
|
|
|
|
const ordersTotalCount = getOrdersTotalCount( tableQuery );
|
|
|
|
|
const isError = isGetOrdersError( tableQuery );
|
|
|
|
|
const isRequesting = isGetOrdersRequesting( tableQuery );
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
return {
|
2018-12-03 03:40:57 +00:00
|
|
|
|
tableData: {
|
|
|
|
|
items: {
|
|
|
|
|
data: formatTableOrders( orders ),
|
|
|
|
|
totalCount: ordersTotalCount,
|
|
|
|
|
},
|
|
|
|
|
isError,
|
|
|
|
|
isRequesting,
|
|
|
|
|
query: tableQuery,
|
|
|
|
|
},
|
2018-10-29 07:57:05 +00:00
|
|
|
|
};
|
|
|
|
|
} )
|
|
|
|
|
)( OrdersReportTable );
|