woocommerce/plugins/woocommerce-admin/client/analytics/report/revenue/table.js

386 lines
8.9 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { __, _n } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { format as formatDate } from '@wordpress/date';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { get, memoize } from 'lodash';
import { Date, Link } from '@woocommerce/components';
import { formatValue } from '@woocommerce/number';
import {
getReportTableQuery,
REPORTS_STORE_NAME,
SETTINGS_STORE_NAME,
QUERY_DEFAULTS,
} from '@woocommerce/data';
import {
appendTimestamp,
defaultTableDateFormat,
getCurrentDates,
} from '@woocommerce/date';
import { stringify } from 'qs';
import { CurrencyContext } from '@woocommerce/currency';
/**
* Internal dependencies
*/
import ReportTable from '../../components/report-table';
import { getAdminSetting } from '~/utils/admin-settings';
const EMPTY_ARRAY = [];
const summaryFields = [
'orders_count',
'gross_sales',
'total_sales',
'refunds',
'coupons',
'taxes',
'shipping',
'net_revenue',
];
class RevenueReportTable extends Component {
constructor() {
super();
this.getHeadersContent = this.getHeadersContent.bind( this );
this.getRowsContent = this.getRowsContent.bind( this );
this.getSummary = this.getSummary.bind( this );
}
getHeadersContent() {
return [
{
label: __( 'Date', 'woocommerce' ),
key: 'date',
required: true,
defaultSort: true,
isLeftAligned: true,
isSortable: true,
},
{
label: __( 'Orders', 'woocommerce' ),
key: 'orders_count',
required: false,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Gross sales', 'woocommerce' ),
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
key: 'gross_sales',
required: false,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Returns', 'woocommerce' ),
key: 'refunds',
required: false,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Coupons', 'woocommerce' ),
key: 'coupons',
required: false,
isSortable: true,
isNumeric: true,
},
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
{
label: __( 'Net sales', 'woocommerce' ),
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
key: 'net_revenue',
required: false,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Taxes', 'woocommerce' ),
key: 'taxes',
required: false,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Shipping', 'woocommerce' ),
key: 'shipping',
required: false,
isSortable: true,
isNumeric: true,
},
{
label: __( 'Total sales', 'woocommerce' ),
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
key: 'total_sales',
required: false,
isSortable: true,
isNumeric: true,
},
];
}
getRowsContent( data = [] ) {
const dateFormat = getAdminSetting(
'dateFormat',
defaultTableDateFormat
);
const {
formatAmount,
render: renderCurrency,
formatDecimal: getCurrencyFormatDecimal,
getCurrencyConfig,
} = this.context;
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
2020-03-25 03:20:17 +00:00
return data.map( ( row ) => {
const {
coupons,
gross_sales: grossSales,
total_sales: totalSales,
net_revenue: netRevenue,
orders_count: ordersCount,
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"
>
{ formatValue(
getCurrencyConfig(),
'number',
ordersCount
) }
</Link>
);
return [
{
display: (
<Date
date={ row.date_start }
visibleFormat={ dateFormat }
/>
),
value: row.date_start,
},
{
display: orderLink,
value: Number( ordersCount ),
},
{
display: renderCurrency( grossSales ),
value: getCurrencyFormatDecimal( grossSales ),
},
{
display: formatAmount( refunds ),
value: getCurrencyFormatDecimal( refunds ),
},
{
display: formatAmount( coupons ),
value: getCurrencyFormatDecimal( coupons ),
},
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
{
display: renderCurrency( netRevenue ),
value: getCurrencyFormatDecimal( netRevenue ),
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
},
{
display: renderCurrency( taxes ),
value: getCurrencyFormatDecimal( taxes ),
},
{
display: renderCurrency( shipping ),
value: getCurrencyFormatDecimal( shipping ),
},
{
display: renderCurrency( totalSales ),
value: getCurrencyFormatDecimal( totalSales ),
},
];
} );
}
getSummary( totals, totalResults = 0 ) {
const {
orders_count: ordersCount = 0,
gross_sales: grossSales = 0,
total_sales: totalSales = 0,
refunds = 0,
coupons = 0,
taxes = 0,
shipping = 0,
net_revenue: netRevenue = 0,
} = totals;
const { formatAmount, getCurrencyConfig } = this.context;
const currency = getCurrencyConfig();
return [
{
label: _n( 'day', 'days', totalResults, 'woocommerce' ),
value: formatValue( currency, 'number', totalResults ),
},
{
label: _n( 'order', 'orders', ordersCount, 'woocommerce' ),
value: formatValue( currency, 'number', ordersCount ),
},
{
label: __( 'Gross sales', 'woocommerce' ),
value: formatAmount( grossSales ),
},
{
label: __( 'Returns', 'woocommerce' ),
value: formatAmount( refunds ),
},
{
label: __( 'Coupons', 'woocommerce' ),
value: formatAmount( coupons ),
},
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
{
label: __( 'Net sales', 'woocommerce' ),
value: formatAmount( netRevenue ),
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
},
{
label: __( 'Taxes', 'woocommerce' ),
value: formatAmount( taxes ),
},
{
label: __( 'Shipping', 'woocommerce' ),
value: formatAmount( shipping ),
},
{
label: __( 'Total sales', 'woocommerce' ),
value: formatAmount( totalSales ),
},
];
}
render() {
const { advancedFilters, filters, tableData, query } = this.props;
return (
<ReportTable
endpoint="revenue"
getHeadersContent={ this.getHeadersContent }
getRowsContent={ this.getRowsContent }
getSummary={ this.getSummary }
summaryFields={ summaryFields }
query={ query }
tableData={ tableData }
title={ __( 'Revenue', 'woocommerce' ) }
columnPrefsKey="revenue_report_columns"
filters={ filters }
advancedFilters={ advancedFilters }
/>
);
}
}
RevenueReportTable.contextType = CurrencyContext;
/**
* 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( ':' )
);
export default compose(
withSelect( ( select, props ) => {
const { query, filters, advancedFilters } = props;
wp.data Settings refactor add data store for settings using wp.data add use-select-with-refresh example replace fresh-data usage with new settings data store for settings page Add data package move to packages Fix isDirty after save Add isBusy to primary button when saving update Readme remove comment readme to use useSelect Revert "update Readme" This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. Data Layer: Settings page to use Settings store (https://github.com/woocommerce/woocommerce-admin/pull/3430) * Data Layer: Settings store as source of truth for settings page This reverts commit 7402fd49b8f384fde5878e0bee0616f0a87bb4f6. * fixup * save on reset * non mutable constants * add set/getSettings * save using setSettings * separate HOC * cleanup * remove settingsToData * withHydration * remove withSettings HOC * renmove useSettins for now * withSettingsHydration updates * Revert "withSettingsHydration updates" This reverts commit f2adf108fbe19b574978fea5925a1a18e7ed3007. * rename withSettingsHydration * redo withSettingsHydration simplification * restore * useSettings * render using useSettings * handleInputChange working * get setIsDirty working * saving works * reset and cleanup * cleanup * use snake case on hook files * use clearIsDirty * Avoid mutation on setting update * remove @todo * persiting -> isPersisting * better reducer ternaries * add wcSettings as arg to withSettingsHydration reset package-lock Settings: split out mutable wcAdminSettings (https://github.com/woocommerce/woocommerce-admin/pull/3675) Settings: handle async settings groups (https://github.com/woocommerce/woocommerce-admin/pull/3707)
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 );
const { getReportStats, getReportStatsError, isResolving } =
select( REPORTS_STORE_NAME );
const tableQuery = formatTableQuery(
query.order || 'desc',
query.orderby || 'date',
query.paged || 1,
query.per_page || QUERY_DEFAULTS.pageSize,
datesFromQuery
);
const filteredTableQuery = getReportTableQuery( {
endpoint: 'revenue',
query,
select,
tableQuery,
filters,
advancedFilters,
} );
const revenueData = getReportStats( 'revenue', filteredTableQuery );
const isError = Boolean(
getReportStatsError( 'revenue', filteredTableQuery )
);
const isRequesting = isResolving( 'getReportStats', [
'revenue',
filteredTableQuery,
] );
return formatProps( isError, isRequesting, tableQuery, revenueData );
} )
)( RevenueReportTable );