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-04 00:00:13 +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
|
|
|
*/
|
2018-10-29 07:57:05 +00:00
|
|
|
import {
|
|
|
|
appendTimestamp,
|
|
|
|
getCurrentDates,
|
|
|
|
getDateFormatsForInterval,
|
|
|
|
getIntervalForQuery,
|
2018-10-30 18:57:48 +00:00
|
|
|
} from '@woocommerce/date';
|
2018-12-04 00:00:13 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
2018-11-05 21:02:04 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
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-11-26 03:31:38 +00:00
|
|
|
import { numberFormat } from 'lib/number';
|
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 [
|
|
|
|
{
|
|
|
|
label: __( 'Date', 'wc-admin' ),
|
|
|
|
key: 'date',
|
|
|
|
required: true,
|
|
|
|
defaultSort: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Orders', 'wc-admin' ),
|
|
|
|
key: 'orders_count',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Gross Revenue', 'wc-admin' ),
|
|
|
|
key: 'gross_revenue',
|
|
|
|
required: true,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Refunds', 'wc-admin' ),
|
|
|
|
key: 'refunds',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Coupons', 'wc-admin' ),
|
|
|
|
key: 'coupons',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Taxes', 'wc-admin' ),
|
|
|
|
key: 'taxes',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Shipping', 'wc-admin' ),
|
|
|
|
key: 'shipping',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Net Revenue', 'wc-admin' ),
|
|
|
|
key: 'net_revenue',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( data = [] ) {
|
|
|
|
const { query } = this.props;
|
|
|
|
const currentInterval = getIntervalForQuery( query );
|
|
|
|
const formats = getDateFormatsForInterval( currentInterval );
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// @TODO How to create this per-report? Can use `w`, `year`, `m` to build time-specific order links
|
|
|
|
// 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 [
|
|
|
|
{
|
|
|
|
display: formatDate( formats.tableFormat, row.date_start ),
|
|
|
|
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 ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:28:18 +00:00
|
|
|
getSummary( totals ) {
|
|
|
|
if ( ! totals ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const { tableData } = this.props;
|
|
|
|
const daysCount = tableData.items.totalCount;
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: _n( 'day', 'days', daysCount, 'wc-admin' ),
|
|
|
|
value: numberFormat( daysCount ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: _n( 'order', 'orders', totals.orders_count, 'wc-admin' ),
|
|
|
|
value: numberFormat( totals.orders_count ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'gross revenue', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.gross_revenue ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'refunds', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.refunds ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'coupons', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.coupons ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'taxes', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.taxes ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'shipping', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.shipping ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'net revenue', 'wc-admin' ),
|
|
|
|
value: formatCurrency( totals.net_revenue ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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 }
|
2018-10-23 08:07:23 +00:00
|
|
|
title={ __( 'Revenue', 'wc-admin' ) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
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 );
|
|
|
|
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' );
|
2018-10-23 08:07:23 +00:00
|
|
|
|
2018-10-29 07:57:05 +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',
|
|
|
|
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' ),
|
2018-10-23 08:07:23 +00:00
|
|
|
};
|
2018-12-04 00:00:13 +00:00
|
|
|
const revenueData = getReportStats( 'revenue', tableQuery );
|
|
|
|
const isError = isReportStatsError( 'revenue', tableQuery );
|
|
|
|
const isRequesting = isReportStatsRequesting( 'revenue', tableQuery );
|
2018-10-29 07:57:05 +00:00
|
|
|
|
|
|
|
return {
|
2018-12-04 00:00:13 +00:00
|
|
|
tableData: {
|
|
|
|
items: {
|
|
|
|
data: get( revenueData, [ 'data', 'intervals' ] ),
|
|
|
|
totalCount: get( revenueData, [ 'totalResults' ] ),
|
|
|
|
},
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
query: tableQuery,
|
|
|
|
},
|
2018-10-29 07:57:05 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( RevenueReportTable );
|