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';
|
|
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
|
import { get, map, orderBy } 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 { Link, OrderStatus, TableCard, ViewMoreList } from '@woocommerce/components';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
2018-10-29 07:57:05 +00:00
|
|
|
|
import {
|
|
|
|
|
appendTimestamp,
|
|
|
|
|
getCurrentDates,
|
|
|
|
|
getIntervalForQuery,
|
|
|
|
|
getDateFormatsForInterval,
|
2018-10-30 18:57:48 +00:00
|
|
|
|
} from '@woocommerce/date';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2018-10-11 08:30:51 +00:00
|
|
|
|
import { getAdminLink, onQueryChange } from 'lib/nav-utils';
|
2018-10-29 07:57:05 +00:00
|
|
|
|
import ReportError from 'analytics/components/report-error';
|
2018-10-26 08:19:39 +00:00
|
|
|
|
import { QUERY_DEFAULTS } from 'store/constants';
|
2018-10-29 07:57:05 +00:00
|
|
|
|
import { getReportChartData, getFilterQuery } from 'store/reports/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-10-11 08:30:51 +00:00
|
|
|
|
constructor( props ) {
|
|
|
|
|
super( props );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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' ),
|
|
|
|
|
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' ),
|
|
|
|
|
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' ),
|
|
|
|
|
key: 'coupons',
|
|
|
|
|
required: false,
|
|
|
|
|
isSortable: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: __( 'N. Revenue', 'wc-admin' ),
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formatTableData( data ) {
|
|
|
|
|
return map( data, row => {
|
|
|
|
|
const {
|
|
|
|
|
date_created,
|
|
|
|
|
id,
|
|
|
|
|
status,
|
|
|
|
|
customer_id,
|
|
|
|
|
line_items,
|
|
|
|
|
coupon_lines,
|
|
|
|
|
currency,
|
|
|
|
|
total,
|
|
|
|
|
total_tax,
|
|
|
|
|
shipping_total,
|
|
|
|
|
discount_total,
|
|
|
|
|
} = row;
|
|
|
|
|
|
|
|
|
|
return {
|
2018-10-16 08:50:07 +00:00
|
|
|
|
date: date_created,
|
2018-10-11 08:30:51 +00:00
|
|
|
|
id,
|
|
|
|
|
status,
|
|
|
|
|
customer_id,
|
|
|
|
|
line_items,
|
|
|
|
|
items_sold: line_items.reduce( ( acc, item ) => item.quantity + acc, 0 ),
|
|
|
|
|
coupon_lines,
|
|
|
|
|
currency,
|
|
|
|
|
net_revenue: getCurrencyFormatDecimal(
|
|
|
|
|
total - total_tax - shipping_total - discount_total
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
display: items_sold,
|
|
|
|
|
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 [
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'order', 'orders', totals.num_items_sold, 'wc-admin' ),
|
|
|
|
|
value: totals.orders_count,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'new customer', 'new customers', totals.num_new_customers, 'wc-admin' ),
|
|
|
|
|
value: totals.num_new_customers,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n(
|
|
|
|
|
'returning customer',
|
|
|
|
|
'returning customers',
|
|
|
|
|
totals.num_returning_customers,
|
|
|
|
|
'wc-admin'
|
|
|
|
|
),
|
|
|
|
|
value: totals.num_returning_customers,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'product', 'products', totals.products, 'wc-admin' ),
|
|
|
|
|
value: totals.products,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'item sold', 'items sold', totals.num_items_sold, 'wc-admin' ),
|
|
|
|
|
value: totals.num_items_sold,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: _n( 'coupon', 'coupons', totals.coupons, 'wc-admin' ),
|
|
|
|
|
value: totals.coupons,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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() {
|
|
|
|
|
const { isTableDataError, isTableDataRequesting, primaryData, query, orders } = this.props;
|
|
|
|
|
const isError = isTableDataError || primaryData.isError;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
if ( isError ) {
|
|
|
|
|
return <ReportError isError />;
|
|
|
|
|
}
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
const isRequesting = isTableDataRequesting || primaryData.isRequesting;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
const tableQuery = {
|
|
|
|
|
...query,
|
|
|
|
|
orderby: query.orderby || 'date',
|
|
|
|
|
order: query.order || 'asc',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const headers = this.getHeadersContent();
|
2018-10-11 08:30:51 +00:00
|
|
|
|
const rows = this.getRowsContent(
|
2018-10-23 07:49:04 +00:00
|
|
|
|
orderBy( this.formatTableData( orders ), tableQuery.orderby, tableQuery.order )
|
2018-10-11 08:30:51 +00:00
|
|
|
|
);
|
2018-10-29 07:57:05 +00:00
|
|
|
|
const rowsPerPage = parseInt( tableQuery.per_page ) || QUERY_DEFAULTS.pageSize;
|
|
|
|
|
const totalRows = get( primaryData, [ 'data', 'totals', 'orders_count' ], orders.length );
|
2018-10-31 19:09:38 +00:00
|
|
|
|
const summary = primaryData.data.totals ? this.getSummary( primaryData.data.totals ) : null;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TableCard
|
2018-10-16 08:50:07 +00:00
|
|
|
|
title={ __( 'Orders', 'wc-admin' ) }
|
2018-10-11 08:30:51 +00:00
|
|
|
|
rows={ rows }
|
2018-10-16 08:50:07 +00:00
|
|
|
|
totalRows={ totalRows }
|
2018-10-11 08:30:51 +00:00
|
|
|
|
rowsPerPage={ rowsPerPage }
|
|
|
|
|
headers={ headers }
|
2018-10-29 07:57:05 +00:00
|
|
|
|
isLoading={ isRequesting }
|
2018-10-11 08:30:51 +00:00
|
|
|
|
onQueryChange={ onQueryChange }
|
|
|
|
|
query={ tableQuery }
|
2018-10-31 19:09:38 +00:00
|
|
|
|
summary={ summary }
|
2018-10-24 07:48:20 +00:00
|
|
|
|
downloadable
|
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 primaryData = getReportChartData( 'orders', 'primary', query, select );
|
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
2018-10-23 07:49:04 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
const { getOrders, isGetOrdersError, isGetOrdersRequesting } = select( 'wc-admin' );
|
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 );
|
|
|
|
|
const isTableDataError = isGetOrdersError( tableQuery );
|
|
|
|
|
const isTableDataRequesting = isGetOrdersRequesting( tableQuery );
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
|
return {
|
|
|
|
|
isTableDataError,
|
|
|
|
|
isTableDataRequesting,
|
|
|
|
|
orders,
|
|
|
|
|
primaryData,
|
|
|
|
|
};
|
|
|
|
|
} )
|
|
|
|
|
)( OrdersReportTable );
|