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-11-26 19:39:40 +00:00
|
|
|
import { appendTimestamp, defaultTableDateFormat, getCurrentDates } from 'lib/date';
|
2019-01-14 09:54:44 +00:00
|
|
|
import { Date, Link } from '@woocommerce/components';
|
2019-11-21 21:51:52 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal, renderCurrency } from 'lib/currency-format';
|
|
|
|
import { formatValue } from 'lib/number-format';
|
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';
|
2019-12-05 22:38:26 +00:00
|
|
|
import { getReportTableQuery } from 'wc-api/reports/utils';
|
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-11-22 15:06:14 +00:00
|
|
|
label: __( 'Gross Sales', 'woocommerce-admin' ),
|
|
|
|
key: 'gross_sales',
|
|
|
|
required: false,
|
2018-10-23 08:07:23 +00:00
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
label: __( 'Returns', '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-11-22 15:06:14 +00:00
|
|
|
{
|
|
|
|
label: __( 'Net Sales', 'woocommerce-admin' ),
|
|
|
|
key: 'net_revenue',
|
|
|
|
required: false,
|
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
2018-10-23 08:07:23 +00:00
|
|
|
{
|
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-11-22 15:06:14 +00:00
|
|
|
label: __( 'Total Sales', 'woocommerce-admin' ),
|
|
|
|
key: 'total_sales',
|
|
|
|
required: true,
|
2018-10-23 08:07:23 +00:00
|
|
|
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,
|
2019-11-22 15:06:14 +00:00
|
|
|
gross_sales,
|
|
|
|
total_sales,
|
2018-10-23 08:07:23 +00:00
|
|
|
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"
|
|
|
|
>
|
2019-11-21 21:51:52 +00:00
|
|
|
{ formatValue( 'number', 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 ),
|
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
display: renderCurrency( gross_sales ),
|
|
|
|
value: getCurrencyFormatDecimal( gross_sales ),
|
2018-10-23 08:07:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( refunds ),
|
|
|
|
value: getCurrencyFormatDecimal( refunds ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( coupons ),
|
|
|
|
value: getCurrencyFormatDecimal( coupons ),
|
|
|
|
},
|
2019-11-22 15:06:14 +00:00
|
|
|
{
|
|
|
|
display: renderCurrency( net_revenue ),
|
|
|
|
value: getCurrencyFormatDecimal( net_revenue ),
|
|
|
|
},
|
2018-10-23 08:07:23 +00:00
|
|
|
{
|
2019-05-16 05:04:37 +00:00
|
|
|
display: renderCurrency( taxes ),
|
2018-10-23 08:07:23 +00:00
|
|
|
value: getCurrencyFormatDecimal( taxes ),
|
|
|
|
},
|
|
|
|
{
|
2019-05-16 05:04:37 +00:00
|
|
|
display: renderCurrency( shipping ),
|
2018-10-23 08:07:23 +00:00
|
|
|
value: getCurrencyFormatDecimal( shipping ),
|
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
display: renderCurrency( total_sales ),
|
|
|
|
value: getCurrencyFormatDecimal( total_sales ),
|
2018-10-23 08:07:23 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-02-05 12:00:37 +00:00
|
|
|
getSummary( totals, totalResults = 0 ) {
|
|
|
|
const {
|
|
|
|
orders_count = 0,
|
2019-11-22 15:06:14 +00:00
|
|
|
gross_sales = 0,
|
|
|
|
total_sales = 0,
|
2019-02-05 12:00:37 +00:00
|
|
|
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' ),
|
2019-11-21 21:51:52 +00:00
|
|
|
value: formatValue( 'number', 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-11-21 21:51:52 +00:00
|
|
|
value: formatValue( 'number', orders_count ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
label: __( 'gross sales', 'woocommerce-admin' ),
|
|
|
|
value: formatCurrency( gross_sales ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
label: __( 'returns', '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-11-22 15:06:14 +00:00
|
|
|
{
|
|
|
|
label: __( 'net sales', 'woocommerce-admin' ),
|
|
|
|
value: formatCurrency( net_revenue ),
|
|
|
|
},
|
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-11-22 15:06:14 +00:00
|
|
|
label: __( 'total sales', 'woocommerce-admin' ),
|
|
|
|
value: formatCurrency( total_sales ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-29 07:57:05 +00:00
|
|
|
render() {
|
2019-08-27 19:33:31 +00:00
|
|
|
const { advancedFilters, filters, tableData, query } = 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"
|
2019-08-27 19:33:31 +00:00
|
|
|
filters={ filters }
|
|
|
|
advancedFilters={ advancedFilters }
|
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 ) => {
|
2019-12-05 22:38:26 +00:00
|
|
|
const { query, filters, advancedFilters } = props;
|
2018-10-29 07:57:05 +00:00
|
|
|
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',
|
2019-07-11 17:22:26 +00:00
|
|
|
page: query.paged || 1,
|
2018-10-29 07:57:05 +00:00
|
|
|
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
|
|
|
};
|
2019-12-05 22:38:26 +00:00
|
|
|
const filteredTableQuery = getReportTableQuery( {
|
|
|
|
endpoint: 'revenue',
|
|
|
|
query,
|
|
|
|
select,
|
|
|
|
tableQuery,
|
|
|
|
filters,
|
|
|
|
advancedFilters,
|
|
|
|
} );
|
|
|
|
const revenueData = getReportStats( 'revenue', filteredTableQuery );
|
|
|
|
const isError = Boolean( getReportStatsError( 'revenue', filteredTableQuery ) );
|
|
|
|
const isRequesting = isReportStatsRequesting( 'revenue', filteredTableQuery );
|
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 );
|