2018-06-14 20:15:11 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-06-26 21:02:08 +00:00
|
|
|
import { get, map } from 'lodash';
|
2018-06-14 20:15:11 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-06-20 15:09:37 +00:00
|
|
|
import Card from 'components/card';
|
2018-06-14 20:15:11 +00:00
|
|
|
import DatePicker from 'components/date-picker';
|
2018-06-26 22:17:05 +00:00
|
|
|
import { formatCurrency } from 'lib/currency';
|
2018-06-26 14:59:35 +00:00
|
|
|
import { getAdminLink, updateQueryString } from 'lib/nav-utils';
|
2018-06-26 21:02:08 +00:00
|
|
|
import { getReportData } from 'lib/swagger';
|
2018-06-28 13:52:45 +00:00
|
|
|
import Header from 'layout/header';
|
2018-06-14 20:15:11 +00:00
|
|
|
import { SummaryList, SummaryNumber } from 'components/summary';
|
2018-06-20 15:09:37 +00:00
|
|
|
import Table from 'components/table';
|
2018-06-26 14:59:35 +00:00
|
|
|
import Pagination from 'components/pagination';
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-06-14 20:15:11 +00:00
|
|
|
class RevenueReport extends Component {
|
2018-06-26 14:59:35 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.onPageChange = this.onPageChange.bind( this );
|
|
|
|
this.onPerPageChange = this.onPerPageChange.bind( this );
|
2018-06-26 21:02:08 +00:00
|
|
|
|
|
|
|
// TODO remove this when we implement real endpoints
|
|
|
|
this.state = { stats: {} };
|
2018-06-26 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onPageChange( page ) {
|
|
|
|
updateQueryString( { page } );
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:02:08 +00:00
|
|
|
componentDidMount() {
|
|
|
|
// Swagger doesn't support returning different data based on query args
|
|
|
|
// this is more or less to show how we will manipulate data calls based on props
|
|
|
|
const statsQueryArgs = {
|
|
|
|
interval: 'week',
|
|
|
|
after: '2018-04-22',
|
|
|
|
before: '2018-05-06',
|
|
|
|
};
|
|
|
|
|
|
|
|
getReportData( 'revenue/stats', statsQueryArgs ).then( response => {
|
|
|
|
if ( ! response.ok ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.json().then( data => {
|
|
|
|
this.setState( { stats: data } );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-06-26 14:59:35 +00:00
|
|
|
onPerPageChange( perPage ) {
|
|
|
|
updateQueryString( { per_page: perPage } );
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:09:37 +00:00
|
|
|
getRowsContent( data ) {
|
2018-06-26 22:17:05 +00:00
|
|
|
return map( data, row => {
|
|
|
|
const {
|
|
|
|
coupons,
|
|
|
|
gross_revenue,
|
|
|
|
net_revenue,
|
|
|
|
orders_count,
|
|
|
|
refunds,
|
|
|
|
shipping,
|
|
|
|
taxes,
|
|
|
|
} = row.subtotals;
|
|
|
|
|
2018-07-13 19:36:41 +00:00
|
|
|
// @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
|
2018-06-20 15:09:37 +00:00
|
|
|
const orderLink = (
|
|
|
|
<a href={ getAdminLink( '/edit.php?post_type=shop_order&w=4&year=2018' ) }>
|
2018-06-26 22:17:05 +00:00
|
|
|
{ orders_count }
|
2018-06-20 15:09:37 +00:00
|
|
|
</a>
|
|
|
|
);
|
|
|
|
return [
|
2018-06-26 22:17:05 +00:00
|
|
|
row.start_date,
|
2018-06-20 15:09:37 +00:00
|
|
|
orderLink,
|
2018-06-26 22:17:05 +00:00
|
|
|
formatCurrency( gross_revenue ),
|
|
|
|
formatCurrency( refunds ),
|
|
|
|
formatCurrency( coupons ),
|
|
|
|
formatCurrency( taxes ),
|
|
|
|
formatCurrency( shipping ),
|
|
|
|
formatCurrency( net_revenue ),
|
2018-06-20 15:09:37 +00:00
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:15:11 +00:00
|
|
|
render() {
|
|
|
|
const { path, query } = this.props;
|
2018-06-26 22:17:05 +00:00
|
|
|
const summaryStats = get( this.state.stats, 'totals', {} );
|
|
|
|
const intervalStats = get( this.state.stats, 'intervals', [] );
|
2018-07-13 19:36:41 +00:00
|
|
|
const rows = this.getRowsContent( intervalStats ) || [];
|
2018-06-26 22:17:05 +00:00
|
|
|
|
2018-06-20 15:09:37 +00:00
|
|
|
const headers = [
|
2018-07-10 12:48:06 +00:00
|
|
|
__( 'Date', 'wc-admin' ),
|
|
|
|
__( 'Orders', 'wc-admin' ),
|
|
|
|
__( 'Gross Revenue', 'wc-admin' ),
|
|
|
|
__( 'Refunds', 'wc-admin' ),
|
|
|
|
__( 'Coupons', 'wc-admin' ),
|
|
|
|
__( 'Taxes', 'wc-admin' ),
|
|
|
|
__( 'Shipping', 'wc-admin' ),
|
|
|
|
__( 'Net Revenue', 'wc-admin' ),
|
2018-06-20 15:09:37 +00:00
|
|
|
];
|
|
|
|
|
2018-06-14 20:15:11 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Header
|
|
|
|
sections={ [
|
2018-07-10 12:48:06 +00:00
|
|
|
[ '/analytics', __( 'Analytics', 'wc-admin' ) ],
|
|
|
|
__( 'Revenue', 'wc-admin' ),
|
2018-06-14 20:15:11 +00:00
|
|
|
] }
|
|
|
|
/>
|
|
|
|
<DatePicker query={ query } path={ path } />
|
|
|
|
|
|
|
|
<SummaryList>
|
|
|
|
<SummaryNumber
|
2018-06-26 21:39:29 +00:00
|
|
|
value={ formatCurrency( summaryStats.gross_revenue ) }
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Gross Revenue', 'wc-admin' ) }
|
2018-06-14 20:15:11 +00:00
|
|
|
delta={ 29 }
|
|
|
|
/>
|
|
|
|
<SummaryNumber
|
2018-06-26 21:39:29 +00:00
|
|
|
value={ formatCurrency( summaryStats.refunds ) }
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Refunds', 'wc-admin' ) }
|
2018-06-14 20:15:11 +00:00
|
|
|
delta={ -10 }
|
|
|
|
selected
|
|
|
|
/>
|
2018-06-26 21:02:08 +00:00
|
|
|
<SummaryNumber
|
2018-06-26 21:39:29 +00:00
|
|
|
value={ formatCurrency( summaryStats.coupons ) }
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Coupons', 'wc-admin' ) }
|
2018-06-26 21:02:08 +00:00
|
|
|
delta={ 15 }
|
|
|
|
/>
|
2018-06-26 21:39:29 +00:00
|
|
|
<SummaryNumber
|
|
|
|
value={ formatCurrency( summaryStats.taxes ) }
|
2018-07-10 12:48:06 +00:00
|
|
|
label={ __( 'Taxes', 'wc-admin' ) }
|
2018-06-26 21:39:29 +00:00
|
|
|
/>
|
2018-06-14 20:15:11 +00:00
|
|
|
</SummaryList>
|
2018-06-26 14:59:35 +00:00
|
|
|
|
2018-06-20 15:09:37 +00:00
|
|
|
<Card title={ __( 'Gross Revenue' ) }>
|
|
|
|
<p>Graph here</p>
|
|
|
|
<hr />
|
2018-07-13 19:36:41 +00:00
|
|
|
{ /* @todo Switch a placeholder view if we don't have rows */ }
|
|
|
|
<Table rows={ rows } headers={ headers } caption={ __( 'Revenue Last Week' ) } />
|
2018-06-20 15:09:37 +00:00
|
|
|
</Card>
|
2018-06-26 14:59:35 +00:00
|
|
|
|
|
|
|
<Pagination
|
|
|
|
page={ parseInt( query.page ) || 1 }
|
|
|
|
perPage={ parseInt( query.per_page ) || 25 }
|
|
|
|
total={ 5000 }
|
|
|
|
onPageChange={ this.onPageChange }
|
|
|
|
onPerPageChange={ this.onPerPageChange }
|
|
|
|
/>
|
2018-06-14 20:15:11 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RevenueReport.propTypes = {
|
|
|
|
params: PropTypes.object.isRequired,
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RevenueReport;
|