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 14:59:35 +00:00
|
|
|
import { getAdminLink, updateQueryString } from 'lib/nav-utils';
|
2018-06-20 15:09:37 +00:00
|
|
|
import { getCurrencyFormatString } from 'lib/currency';
|
2018-06-26 21:02:08 +00:00
|
|
|
import { getReportData } from 'lib/swagger';
|
2018-06-14 20:15:11 +00:00
|
|
|
import Header from 'components/header';
|
|
|
|
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
|
|
|
|
|
|
|
// Mock data until we fetch from an API
|
|
|
|
import rawData from './mock-data';
|
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 ) {
|
|
|
|
return map( data, ( row, label ) => {
|
|
|
|
// @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 = (
|
|
|
|
<a href={ getAdminLink( '/edit.php?post_type=shop_order&w=4&year=2018' ) }>
|
|
|
|
{ row.order_count }
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
label,
|
|
|
|
orderLink,
|
|
|
|
getCurrencyFormatString( row.gross_revenue ),
|
|
|
|
getCurrencyFormatString( row.refunds ),
|
|
|
|
getCurrencyFormatString( row.coupons ),
|
|
|
|
getCurrencyFormatString( row.taxes ),
|
|
|
|
getCurrencyFormatString( row.shipping ),
|
|
|
|
getCurrencyFormatString( row.net_revenue ),
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:15:11 +00:00
|
|
|
render() {
|
|
|
|
const { path, query } = this.props;
|
2018-06-20 15:09:37 +00:00
|
|
|
const rows = this.getRowsContent( rawData.intervals[ 0 ].week[ 0 ] );
|
|
|
|
const headers = [
|
|
|
|
__( 'Date', 'woo-dash' ),
|
|
|
|
__( 'Orders', 'woo-dash' ),
|
|
|
|
__( 'Gross Revenue', 'woo-dash' ),
|
|
|
|
__( 'Refunds', 'woo-dash' ),
|
|
|
|
__( 'Coupons', 'woo-dash' ),
|
|
|
|
__( 'Taxes', 'woo-dash' ),
|
|
|
|
__( 'Shipping', 'woo-dash' ),
|
|
|
|
__( 'Net Revenue', 'woo-dash' ),
|
|
|
|
];
|
2018-06-26 21:02:08 +00:00
|
|
|
const summaryStats = get( this.state.stats, 'totals', {} );
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-06-14 20:15:11 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Header
|
|
|
|
sections={ [
|
|
|
|
[ '/analytics', __( 'Analytics', 'woo-dash' ) ],
|
|
|
|
__( 'Revenue', 'woo-dash' ),
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
<DatePicker query={ query } path={ path } />
|
|
|
|
|
|
|
|
<SummaryList>
|
|
|
|
<SummaryNumber
|
2018-06-26 21:02:08 +00:00
|
|
|
value={ summaryStats.gross_revenue }
|
2018-06-14 20:15:11 +00:00
|
|
|
label={ __( 'Gross Revenue', 'woo-dash' ) }
|
|
|
|
delta={ 29 }
|
|
|
|
/>
|
|
|
|
<SummaryNumber
|
2018-06-26 21:02:08 +00:00
|
|
|
value={ summaryStats.refunds }
|
2018-06-14 20:15:11 +00:00
|
|
|
label={ __( 'Refunds', 'woo-dash' ) }
|
|
|
|
delta={ -10 }
|
|
|
|
selected
|
|
|
|
/>
|
2018-06-26 21:02:08 +00:00
|
|
|
<SummaryNumber
|
|
|
|
value={ summaryStats.coupons }
|
|
|
|
label={ __( 'Coupons', 'woo-dash' ) }
|
|
|
|
delta={ 15 }
|
|
|
|
/>
|
|
|
|
<SummaryNumber value={ summaryStats.taxes } label={ __( 'Taxes', 'woo-dash' ) } />
|
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 />
|
|
|
|
<Table rows={ rows } headers={ headers } caption={ __( 'Revenue Last Week' ) } />
|
|
|
|
</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;
|