2018-10-23 08:07:23 +00:00
|
|
|
/**
|
|
|
|
* 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';
|
2020-09-03 21:45:40 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
2018-10-29 07:57:05 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2021-01-07 01:01:45 +00:00
|
|
|
import { get, memoize } from 'lodash';
|
2019-01-14 09:54:44 +00:00
|
|
|
import { Date, Link } from '@woocommerce/components';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { formatValue } from '@woocommerce/number';
|
2020-08-14 11:19:04 +00:00
|
|
|
import {
|
|
|
|
getReportTableQuery,
|
|
|
|
REPORTS_STORE_NAME,
|
|
|
|
SETTINGS_STORE_NAME,
|
2020-09-03 21:45:40 +00:00
|
|
|
QUERY_DEFAULTS,
|
2020-08-14 11:19:04 +00:00
|
|
|
} from '@woocommerce/data';
|
2020-08-13 02:05:22 +00:00
|
|
|
import {
|
|
|
|
appendTimestamp,
|
|
|
|
defaultTableDateFormat,
|
|
|
|
getCurrentDates,
|
2020-08-17 21:53:13 +00:00
|
|
|
} from '@woocommerce/date';
|
2021-01-07 01:01:45 +00:00
|
|
|
import { stringify } from 'qs';
|
2020-08-17 21:53:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import ReportTable from '../../components/report-table';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { CurrencyContext } from '../../../lib/currency-context';
|
2018-10-23 08:07:23 +00:00
|
|
|
|
2021-01-07 01:01:45 +00:00
|
|
|
const EMPTY_ARRAY = [];
|
|
|
|
|
|
|
|
const summaryFields = [
|
|
|
|
'orders_count',
|
|
|
|
'gross_sales',
|
|
|
|
'total_sales',
|
|
|
|
'refunds',
|
|
|
|
'coupons',
|
|
|
|
'taxes',
|
|
|
|
'shipping',
|
|
|
|
'net_revenue',
|
|
|
|
];
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Gross sales', 'woocommerce-admin' ),
|
2019-11-22 15:06:14 +00:00
|
|
|
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
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Net sales', 'woocommerce-admin' ),
|
2019-11-22 15:06:14 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Total sales', 'woocommerce-admin' ),
|
2019-11-22 15:06:14 +00:00
|
|
|
key: 'total_sales',
|
2020-05-20 16:03:30 +00:00
|
|
|
required: false,
|
2018-10-23 08:07:23 +00:00
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( data = [] ) {
|
2022-01-06 12:53:30 +00:00
|
|
|
const dateFormat = getAdminSetting(
|
|
|
|
'dateFormat',
|
|
|
|
defaultTableDateFormat
|
|
|
|
);
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
2020-06-17 23:33:40 +00:00
|
|
|
formatAmount,
|
2020-04-02 21:54:38 +00:00
|
|
|
render: renderCurrency,
|
|
|
|
formatDecimal: getCurrencyFormatDecimal,
|
2020-06-17 23:33:40 +00:00
|
|
|
getCurrencyConfig,
|
2020-04-02 21:54:38 +00:00
|
|
|
} = this.context;
|
2020-02-17 01:12:35 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
return data.map( ( row ) => {
|
2018-10-23 08:07:23 +00:00
|
|
|
const {
|
|
|
|
coupons,
|
2020-02-14 02:23:21 +00:00
|
|
|
gross_sales: grossSales,
|
|
|
|
total_sales: totalSales,
|
|
|
|
net_revenue: netRevenue,
|
|
|
|
orders_count: ordersCount,
|
2018-10-23 08:07:23 +00:00
|
|
|
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
|
2020-02-14 02:23:21 +00:00
|
|
|
href={
|
|
|
|
'edit.php?post_type=shop_order&m=' +
|
|
|
|
formatDate( 'Ymd', row.date_start )
|
|
|
|
}
|
2018-10-23 08:07:23 +00:00
|
|
|
type="wp-admin"
|
|
|
|
>
|
2020-06-17 23:33:40 +00:00
|
|
|
{ formatValue(
|
|
|
|
getCurrencyConfig(),
|
|
|
|
'number',
|
|
|
|
ordersCount
|
|
|
|
) }
|
2018-10-23 08:07:23 +00:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: (
|
|
|
|
<Date
|
|
|
|
date={ row.date_start }
|
2020-02-17 01:12:35 +00:00
|
|
|
visibleFormat={ dateFormat }
|
2020-02-14 02:23:21 +00:00
|
|
|
/>
|
|
|
|
),
|
2018-10-23 08:07:23 +00:00
|
|
|
value: row.date_start,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: orderLink,
|
2020-02-14 02:23:21 +00:00
|
|
|
value: Number( ordersCount ),
|
2018-10-23 08:07:23 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: renderCurrency( grossSales ),
|
|
|
|
value: getCurrencyFormatDecimal( grossSales ),
|
2018-10-23 08:07:23 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( refunds ),
|
2018-10-23 08:07:23 +00:00
|
|
|
value: getCurrencyFormatDecimal( refunds ),
|
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( coupons ),
|
2018-10-23 08:07:23 +00:00
|
|
|
value: getCurrencyFormatDecimal( coupons ),
|
|
|
|
},
|
2019-11-22 15:06:14 +00:00
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: renderCurrency( netRevenue ),
|
|
|
|
value: getCurrencyFormatDecimal( netRevenue ),
|
2019-11-22 15:06:14 +00:00
|
|
|
},
|
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 ),
|
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: renderCurrency( totalSales ),
|
|
|
|
value: getCurrencyFormatDecimal( totalSales ),
|
2018-10-23 08:07:23 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-02-05 12:00:37 +00:00
|
|
|
getSummary( totals, totalResults = 0 ) {
|
|
|
|
const {
|
2020-02-14 02:23:21 +00:00
|
|
|
orders_count: ordersCount = 0,
|
|
|
|
gross_sales: grossSales = 0,
|
|
|
|
total_sales: totalSales = 0,
|
2019-02-05 12:00:37 +00:00
|
|
|
refunds = 0,
|
|
|
|
coupons = 0,
|
|
|
|
taxes = 0,
|
|
|
|
shipping = 0,
|
2020-02-14 02:23:21 +00:00
|
|
|
net_revenue: netRevenue = 0,
|
2019-02-05 12:00:37 +00:00
|
|
|
} = totals;
|
2020-06-17 23:33:40 +00:00
|
|
|
const { formatAmount, getCurrencyConfig } = this.context;
|
|
|
|
const currency = getCurrencyConfig();
|
2018-12-04 19:28:18 +00:00
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: _n( 'day', 'days', totalResults, 'woocommerce-admin' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', totalResults ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
|
|
|
'order',
|
|
|
|
'orders',
|
|
|
|
ordersCount,
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', ordersCount ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Gross sales', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( grossSales ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Returns', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( refunds ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Coupons', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( coupons ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
2019-11-22 15:06:14 +00:00
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Net sales', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( netRevenue ),
|
2019-11-22 15:06:14 +00:00
|
|
|
},
|
2018-12-04 19:28:18 +00:00
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Taxes', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( taxes ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Shipping', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( shipping ),
|
2018-12-04 19:28:18 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Total sales', 'woocommerce-admin' ),
|
2020-06-17 23:33:40 +00:00
|
|
|
value: formatAmount( totalSales ),
|
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 }
|
2021-01-07 01:01:45 +00:00
|
|
|
summaryFields={ summaryFields }
|
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
|
|
|
|
2020-04-02 21:54:38 +00:00
|
|
|
RevenueReportTable.contextType = CurrencyContext;
|
|
|
|
|
2021-01-07 01:01:45 +00:00
|
|
|
/**
|
|
|
|
* Memoized props object formatting function.
|
|
|
|
*
|
|
|
|
* @param {boolean} isError
|
|
|
|
* @param {boolean} isRequesting
|
|
|
|
* @param {Object} tableQuery
|
|
|
|
* @param {Object} revenueData
|
|
|
|
* @return {Object} formatted tableData prop
|
|
|
|
*/
|
|
|
|
const formatProps = memoize(
|
|
|
|
( isError, isRequesting, tableQuery, revenueData ) => ( {
|
|
|
|
tableData: {
|
|
|
|
items: {
|
|
|
|
data: get( revenueData, [ 'data', 'intervals' ], EMPTY_ARRAY ),
|
|
|
|
totalResults: get( revenueData, [ 'totalResults' ], 0 ),
|
|
|
|
},
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
query: tableQuery,
|
|
|
|
},
|
|
|
|
} ),
|
|
|
|
( isError, isRequesting, tableQuery, revenueData ) =>
|
|
|
|
[
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
stringify( tableQuery ),
|
|
|
|
get( revenueData, [ 'totalResults' ], 0 ),
|
|
|
|
get( revenueData, [ 'data', 'intervals' ], EMPTY_ARRAY ).length,
|
|
|
|
].join( ':' )
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Memoized table query formatting function.
|
|
|
|
*
|
|
|
|
* @param {string} order
|
|
|
|
* @param {string} orderBy
|
|
|
|
* @param {number} page
|
|
|
|
* @param {number} pageSize
|
|
|
|
* @param {Object} datesFromQuery
|
|
|
|
* @return {Object} formatted tableQuery object
|
|
|
|
*/
|
|
|
|
const formatTableQuery = memoize(
|
|
|
|
// @todo Support hour here when viewing a single day
|
|
|
|
( order, orderBy, page, pageSize, datesFromQuery ) => ( {
|
|
|
|
interval: 'day',
|
|
|
|
orderby: orderBy,
|
|
|
|
order,
|
|
|
|
page,
|
|
|
|
per_page: pageSize,
|
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
|
|
|
|
} ),
|
|
|
|
( order, orderBy, page, pageSize, datesFromQuery ) =>
|
|
|
|
[
|
|
|
|
order,
|
|
|
|
orderBy,
|
|
|
|
page,
|
|
|
|
pageSize,
|
|
|
|
datesFromQuery.primary.after,
|
|
|
|
datesFromQuery.primary.before,
|
|
|
|
].join( ':' )
|
|
|
|
);
|
|
|
|
|
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;
|
2020-03-25 03:20:17 +00:00
|
|
|
const { woocommerce_default_date_range: defaultDateRange } = select(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
).getSetting( 'wc_admin', 'wcAdminSettings' );
|
|
|
|
const datesFromQuery = getCurrentDates( query, defaultDateRange );
|
2020-08-14 11:19:04 +00:00
|
|
|
const { getReportStats, getReportStatsError, isResolving } = select(
|
|
|
|
REPORTS_STORE_NAME
|
|
|
|
);
|
2018-10-23 08:07:23 +00:00
|
|
|
|
2021-01-07 01:01:45 +00:00
|
|
|
const tableQuery = formatTableQuery(
|
|
|
|
query.order || 'desc',
|
|
|
|
query.orderby || 'date',
|
|
|
|
query.paged || 1,
|
|
|
|
query.per_page || QUERY_DEFAULTS.pageSize,
|
|
|
|
datesFromQuery
|
|
|
|
);
|
2019-12-05 22:38:26 +00:00
|
|
|
const filteredTableQuery = getReportTableQuery( {
|
|
|
|
endpoint: 'revenue',
|
|
|
|
query,
|
|
|
|
select,
|
|
|
|
tableQuery,
|
|
|
|
filters,
|
|
|
|
advancedFilters,
|
|
|
|
} );
|
|
|
|
const revenueData = getReportStats( 'revenue', filteredTableQuery );
|
2020-02-14 02:23:21 +00:00
|
|
|
const isError = Boolean(
|
|
|
|
getReportStatsError( 'revenue', filteredTableQuery )
|
|
|
|
);
|
2020-08-14 11:19:04 +00:00
|
|
|
const isRequesting = isResolving( 'getReportStats', [
|
2020-02-14 02:23:21 +00:00
|
|
|
'revenue',
|
2020-08-14 11:19:04 +00:00
|
|
|
filteredTableQuery,
|
|
|
|
] );
|
2018-10-29 07:57:05 +00:00
|
|
|
|
2021-01-07 01:01:45 +00:00
|
|
|
return formatProps( isError, isRequesting, tableQuery, revenueData );
|
2018-10-29 07:57:05 +00:00
|
|
|
} )
|
|
|
|
)( RevenueReportTable );
|