2018-10-11 08:30:51 +00:00
|
|
|
|
/** @format */
|
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2018-10-12 21:29:25 +00:00
|
|
|
|
import { __, 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';
|
|
|
|
|
import { map, orderBy } from 'lodash';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2018-10-12 21:29:25 +00:00
|
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
Link,
|
|
|
|
|
OrderStatus,
|
|
|
|
|
TableCard,
|
|
|
|
|
TablePlaceholder,
|
|
|
|
|
ViewMoreList,
|
|
|
|
|
} from '@woocommerce/components';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
import { downloadCSVFile, generateCSVDataFromTable, generateCSVFileName } from 'lib/csv';
|
|
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from 'lib/currency';
|
|
|
|
|
import { getIntervalForQuery, getDateFormatsForInterval } from 'lib/date';
|
|
|
|
|
import { getAdminLink, onQueryChange } from 'lib/nav-utils';
|
2018-10-18 10:43:45 +00:00
|
|
|
|
import './style.scss';
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
|
|
|
|
export default class OrdersReportTable extends Component {
|
|
|
|
|
constructor( props ) {
|
|
|
|
|
super( props );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onDownload( headers, rows, query ) {
|
|
|
|
|
// @TODO The current implementation only downloads the contents displayed in the table.
|
|
|
|
|
// Another solution is required when the data set is larger (see #311).
|
|
|
|
|
return () => {
|
|
|
|
|
downloadCSVFile(
|
|
|
|
|
generateCSVFileName( 'orders', query ),
|
|
|
|
|
generateCSVDataFromTable( headers, rows )
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-12 21:29:25 +00:00
|
|
|
|
renderLinks( items = [] ) {
|
2018-10-18 20:45:59 +00:00
|
|
|
|
return items.map( ( item, i ) => (
|
|
|
|
|
<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-23 07:49:04 +00:00
|
|
|
|
renderPlaceholderTable( tableQuery ) {
|
2018-10-11 08:30:51 +00:00
|
|
|
|
const headers = this.getHeadersContent();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card
|
|
|
|
|
title={ __( 'Orders', 'wc-admin' ) }
|
|
|
|
|
className="woocommerce-analytics__table-placeholder"
|
|
|
|
|
>
|
2018-10-23 07:49:04 +00:00
|
|
|
|
<TablePlaceholder
|
|
|
|
|
caption={ __( 'Orders', 'wc-admin' ) }
|
|
|
|
|
headers={ headers }
|
|
|
|
|
query={ tableQuery }
|
|
|
|
|
/>
|
2018-10-11 08:30:51 +00:00
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 07:49:04 +00:00
|
|
|
|
renderTable( tableQuery ) {
|
2018-10-16 08:50:07 +00:00
|
|
|
|
const { orders, query, totalRows } = this.props;
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-23 07:49:04 +00:00
|
|
|
|
const rowsPerPage = parseInt( tableQuery.per_page ) || 25;
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const headers = this.getHeadersContent();
|
|
|
|
|
|
|
|
|
|
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-23 07:49:04 +00:00
|
|
|
|
onClickDownload={ this.onDownload( headers, rows, query ) }
|
2018-10-11 08:30:51 +00:00
|
|
|
|
onQueryChange={ onQueryChange }
|
|
|
|
|
query={ tableQuery }
|
|
|
|
|
summary={ null }
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-10-23 07:49:04 +00:00
|
|
|
|
const { isRequesting, query } = this.props;
|
|
|
|
|
|
|
|
|
|
const tableQuery = {
|
|
|
|
|
...query,
|
|
|
|
|
orderby: query.orderby || 'date',
|
|
|
|
|
order: query.order || 'asc',
|
|
|
|
|
};
|
2018-10-11 08:30:51 +00:00
|
|
|
|
|
2018-10-23 07:49:04 +00:00
|
|
|
|
return isRequesting
|
|
|
|
|
? this.renderPlaceholderTable( tableQuery )
|
|
|
|
|
: this.renderTable( tableQuery );
|
2018-10-11 08:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|