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';
|
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';
|
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-02-17 01:12:35 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-08-14 11:19:04 +00:00
|
|
|
import {
|
|
|
|
getReportTableQuery,
|
|
|
|
REPORTS_STORE_NAME,
|
|
|
|
SETTINGS_STORE_NAME,
|
|
|
|
} 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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import { QUERY_DEFAULTS } from '../../../wc-api/constants';
|
|
|
|
import ReportTable from '../../components/report-table';
|
|
|
|
import withSelect from '../../../wc-api/with-select';
|
|
|
|
import { CurrencyContext } from '../../../lib/currency-context';
|
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',
|
2020-05-20 16:03:30 +00:00
|
|
|
required: false,
|
2018-10-23 08:07:23 +00:00
|
|
|
isSortable: true,
|
|
|
|
isNumeric: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( data = [] ) {
|
2020-02-17 01:12:35 +00:00
|
|
|
const dateFormat = getSetting( '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
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +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
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +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
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2019-03-13 17:14:02 +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
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +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
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +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 }
|
2020-03-31 15:08:40 +00:00
|
|
|
summaryFields={ [
|
|
|
|
'orders_count',
|
|
|
|
'gross_sales',
|
|
|
|
'total_sales',
|
|
|
|
'refunds',
|
|
|
|
'coupons',
|
|
|
|
'taxes',
|
|
|
|
'shipping',
|
|
|
|
'net_revenue',
|
|
|
|
] }
|
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;
|
|
|
|
|
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
|
|
|
|
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 );
|
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
|
|
|
|
|
|
|
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 );
|