2018-10-23 08:07:23 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-12-04 19:28:18 +00:00
|
|
|
import { __, _n } from '@wordpress/i18n';
|
2018-10-23 08:07:23 +00:00
|
|
|
import { Component } 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-05 17:05:31 +00:00
|
|
|
import { get } from 'lodash';
|
2018-10-23 08:07:23 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-30 18:57:48 +00:00
|
|
|
* WooCommerce dependencies
|
2018-10-23 08:07:23 +00:00
|
|
|
*/
|
2019-01-09 00:24:49 +00:00
|
|
|
import { appendTimestamp, defaultTableDateFormat, getCurrentDates } from '@woocommerce/date';
|
2019-01-14 09:54:44 +00:00
|
|
|
import { Date, Link } from '@woocommerce/components';
|
2018-11-05 21:02:04 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } 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
|
|
|
|
*/
|
2019-01-30 07:22:10 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
2018-12-04 00:00:13 +00:00
|
|
|
import ReportTable from 'analytics/components/report-table';
|
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-10-23 08:07:23 +00:00
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
class RevenueReportTable extends Component {
|
2018-12-04 00:00:13 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
2018-12-04 19:28:18 +00:00
|
|
|
this.getSummary = this.getSummary.bind( this );
|
2018-12-04 00:00:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 08:07:23 +00:00
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Date', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'date',
|
|
|
|
required: true,
|
|
|
|
defaultSort: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Orders', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'orders_count',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Gross Revenue', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'gross_revenue',
|
|
|
|
required: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Refunds', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'refunds',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Coupons', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'coupons',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Taxes', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'taxes',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Shipping', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'shipping',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Net Revenue', 'woocommerce-admin' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
key: 'net_revenue',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( data = [] ) {
|
2018-12-04 00:00:13 +00:00
|
|
|
return data.map( row => {
|
2018-10-23 08:07:23 +00:00
|
|
|
const {
|
|
|
|
coupons,
|
|
|
|
gross_revenue,
|
|
|
|
net_revenue,
|
|
|
|
orders_count,
|
|
|
|
refunds,
|
|
|
|
shipping,
|
|
|
|
taxes,
|
|
|
|
} = row.subtotals;
|
|
|
|
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo How to create this per-report? Can use `w`, `year`, `m` to build time-specific order links
|
2018-10-23 08:07:23 +00:00
|
|
|
// we need to know which kind of report this is, and parse the `label` to get this row's date
|
|
|
|
const orderLink = (
|
|
|
|
<Link
|
|
|
|
href={ 'edit.php?post_type=shop_order&m=' + formatDate( 'Ymd', row.date_start ) }
|
|
|
|
type="wp-admin"
|
|
|
|
>
|
2018-11-26 03:31:38 +00:00
|
|
|
{ numberFormat( orders_count ) }
|
2018-10-23 08:07:23 +00:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
{
|
2019-01-14 09:54:44 +00:00
|
|
|
display: <Date date={ row.date_start } visibleFormat={ defaultTableDateFormat } />,
|
2018-10-23 08:07:23 +00:00
|
|
|
value: row.date_start,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: orderLink,
|
|
|
|
value: Number( orders_count ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( gross_revenue ),
|
|
|
|
value: getCurrencyFormatDecimal( gross_revenue ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( refunds ),
|
|
|
|
value: getCurrencyFormatDecimal( refunds ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( coupons ),
|
|
|
|
value: getCurrencyFormatDecimal( coupons ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( taxes ),
|
|
|
|
value: getCurrencyFormatDecimal( taxes ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( shipping ),
|
|
|
|
value: getCurrencyFormatDecimal( shipping ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( net_revenue ),
|
|
|
|
value: getCurrencyFormatDecimal( net_revenue ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-02-05 12:00:37 +00:00
|
|
|
getSummary( totals, totalResults = 0 ) {
|
|
|
|
const {
|
|
|
|
orders_count = 0,
|
|
|
|
gross_revenue = 0,
|
|
|
|
refunds = 0,
|
|
|
|
coupons = 0,
|
|
|
|
taxes = 0,
|
|
|
|
shipping = 0,
|
|
|
|
net_revenue = 0,
|
|
|
|
} = totals;
|
2018-12-04 19:28:18 +00:00
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: _n( 'day', 'days', totalResults, 'woocommerce-admin' ),
|
2018-12-14 23:56:52 +00:00
|
|
|
value: numberFormat( totalResults ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: _n( 'order', 'orders', orders_count, 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: numberFormat( orders_count ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'gross revenue', 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: formatCurrency( gross_revenue ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'refunds', 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: formatCurrency( refunds ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'coupons', 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: formatCurrency( coupons ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'taxes', 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: formatCurrency( taxes ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'shipping', 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: formatCurrency( shipping ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'net revenue', 'woocommerce-admin' ),
|
2019-02-05 12:00:37 +00:00
|
|
|
value: formatCurrency( net_revenue ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
render() {
|
2018-12-04 00:00:13 +00:00
|
|
|
const { query, tableData } = this.props;
|
2018-10-23 08:07:23 +00:00
|
|
|
|
|
|
|
return (
|
2018-12-04 00:00:13 +00:00
|
|
|
<ReportTable
|
|
|
|
endpoint="revenue"
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
getRowsContent={ this.getRowsContent }
|
2018-12-04 19:28:18 +00:00
|
|
|
getSummary={ this.getSummary }
|
2018-12-04 00:00:13 +00:00
|
|
|
query={ query }
|
|
|
|
tableData={ tableData }
|
2019-03-13 17:14:02 +00:00
|
|
|
title={ __( 'Revenue', 'woocommerce-admin' ) }
|
2018-12-13 19:24:54 +00:00
|
|
|
columnPrefsKey="revenue_report_columns"
|
2018-10-23 08:07:23 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-10-29 07:57:05 +00:00
|
|
|
}
|
2018-10-23 08:07:23 +00:00
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
|
|
|
const { query } = props;
|
|
|
|
const datesFromQuery = getCurrentDates( query );
|
2018-12-15 12:38:54 +00:00
|
|
|
const { getReportStats, getReportStatsError, isReportStatsRequesting } = select( 'wc-api' );
|
2018-10-23 08:07:23 +00:00
|
|
|
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo Support hour here when viewing a single day
|
2018-10-23 08:07:23 +00:00
|
|
|
const tableQuery = {
|
2018-10-29 07:57:05 +00:00
|
|
|
interval: 'day',
|
2018-10-23 08:07:23 +00:00
|
|
|
orderby: query.orderby || 'date',
|
2019-01-17 21:14:12 +00:00
|
|
|
order: query.order || 'desc',
|
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' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
};
|
2018-12-04 00:00:13 +00:00
|
|
|
const revenueData = getReportStats( 'revenue', tableQuery );
|
2018-12-15 12:38:54 +00:00
|
|
|
const isError = Boolean( getReportStatsError( 'revenue', tableQuery ) );
|
2018-12-04 00:00:13 +00:00
|
|
|
const isRequesting = isReportStatsRequesting( 'revenue', tableQuery );
|
2018-10-29 07:57:05 +00:00
|
|
|
|
|
|
|
return {
|
2018-12-04 00:00:13 +00:00
|
|
|
tableData: {
|
|
|
|
items: {
|
2019-02-13 19:37:25 +00:00
|
|
|
data: get( revenueData, [ 'data', 'intervals' ], [] ),
|
|
|
|
totalResults: get( revenueData, [ 'totalResults' ], 0 ),
|
2018-12-04 00:00:13 +00:00
|
|
|
},
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
query: tableQuery,
|
|
|
|
},
|
2018-10-29 07:57:05 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( RevenueReportTable );
|