* Add customer functions to order and refund classes

* Use parent order for refund order number

* Add renderCurrency method to format negative currencies

* Allow negative number of items for refund fields in database
This commit is contained in:
Joshua T Flowers 2019-05-16 13:04:37 +08:00 committed by GitHub
parent b3bc7dce97
commit c87ab4eed7
14 changed files with 81 additions and 30 deletions

View File

@ -10,7 +10,7 @@ import { map } from 'lodash';
/**
* WooCommerce dependencies
*/
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
import { formatCurrency, getCurrencyFormatDecimal, renderCurrency } from '@woocommerce/currency';
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
import { Link } from '@woocommerce/components';
import { numberFormat } from '@woocommerce/number';
@ -86,7 +86,7 @@ class CategoriesReportTable extends Component {
value: items_sold,
},
{
display: formatCurrency( net_revenue ),
display: renderCurrency( net_revenue ),
value: getCurrencyFormatDecimal( net_revenue ),
},
{

View File

@ -11,7 +11,7 @@ import { map } from 'lodash';
*/
import { Date, Link, OrderStatus, ViewMoreList } from '@woocommerce/components';
import { defaultTableDateFormat } from '@woocommerce/date';
import { formatCurrency } from '@woocommerce/currency';
import { formatCurrency, renderCurrency } from '@woocommerce/currency';
import { numberFormat } from '@woocommerce/number';
/**
@ -90,6 +90,17 @@ export default class OrdersReportTable extends Component {
];
}
getCustomerType( customerType ) {
switch ( customerType ) {
case 'new':
return _x( 'New', 'customer type', 'woocommerce-admin' );
case 'returning':
return _x( 'Returning', 'customer type', 'woocommerce-admin' );
default:
return _x( 'N/A', 'customer type', 'woocommerce-admin' );
}
}
getRowsContent( tableData ) {
const { query } = this.props;
const persistedQuery = getPersistedQuery( query );
@ -102,6 +113,7 @@ export default class OrdersReportTable extends Component {
num_items_sold,
order_id,
order_number,
parent_id,
status,
} = row;
const extended_info = row.extended_info || {};
@ -133,7 +145,15 @@ export default class OrdersReportTable extends Component {
},
{
display: (
<Link href={ 'post.php?post=' + order_id + '&action=edit' } type="wp-admin">
<Link
href={
'post.php?post=' +
( parent_id ? parent_id : order_id ) +
'&action=edit' +
( parent_id ? '#order_refunds' : '' )
}
type="wp-admin"
>
{ order_number }
</Link>
),
@ -146,10 +166,7 @@ export default class OrdersReportTable extends Component {
value: status,
},
{
display:
customer_type === 'new'
? _x( 'New', 'customer type', 'woocommerce-admin' )
: _x( 'Returning', 'customer type', 'woocommerce-admin' ),
display: this.getCustomerType( customer_type ),
value: customer_type,
},
{
@ -178,7 +195,7 @@ export default class OrdersReportTable extends Component {
value: formattedCoupons.map( item => item.code ).join( ' ' ),
},
{
display: formatCurrency( net_total, currency ),
display: renderCurrency( net_total, currency ),
value: net_total,
},
];

View File

@ -10,7 +10,7 @@ import { map } from 'lodash';
/**
* WooCommerce dependencies
*/
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
import { formatCurrency, getCurrencyFormatDecimal, renderCurrency } from '@woocommerce/currency';
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
import { Link, Tag } from '@woocommerce/components';
import { numberFormat } from '@woocommerce/number';
@ -153,7 +153,7 @@ class ProductsReportTable extends Component {
value: items_sold,
},
{
display: formatCurrency( net_revenue ),
display: renderCurrency( net_revenue ),
value: getCurrencyFormatDecimal( net_revenue ),
},
{

View File

@ -13,7 +13,7 @@ import { get } from 'lodash';
*/
import { appendTimestamp, defaultTableDateFormat, getCurrentDates } from '@woocommerce/date';
import { Date, Link } from '@woocommerce/components';
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
import { formatCurrency, getCurrencyFormatDecimal, renderCurrency } from '@woocommerce/currency';
import { numberFormat } from '@woocommerce/number';
/**
@ -126,7 +126,7 @@ class RevenueReportTable extends Component {
value: Number( orders_count ),
},
{
display: formatCurrency( gross_revenue ),
display: renderCurrency( gross_revenue ),
value: getCurrencyFormatDecimal( gross_revenue ),
},
{
@ -138,15 +138,15 @@ class RevenueReportTable extends Component {
value: getCurrencyFormatDecimal( coupons ),
},
{
display: formatCurrency( taxes ),
display: renderCurrency( taxes ),
value: getCurrencyFormatDecimal( taxes ),
},
{
display: formatCurrency( shipping ),
display: renderCurrency( shipping ),
value: getCurrencyFormatDecimal( shipping ),
},
{
display: formatCurrency( net_revenue ),
display: renderCurrency( net_revenue ),
value: getCurrencyFormatDecimal( net_revenue ),
},
];

View File

@ -10,7 +10,7 @@ import { map } from 'lodash';
* WooCommerce dependencies
*/
import { Link } from '@woocommerce/components';
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
import { formatCurrency, getCurrencyFormatDecimal, renderCurrency } from '@woocommerce/currency';
import { getTaxCode } from './utils';
import { numberFormat } from '@woocommerce/number';
@ -91,15 +91,15 @@ export default class TaxesReportTable extends Component {
value: tax_rate,
},
{
display: formatCurrency( total_tax ),
display: renderCurrency( total_tax ),
value: getCurrencyFormatDecimal( total_tax ),
},
{
display: formatCurrency( order_tax ),
display: renderCurrency( order_tax ),
value: getCurrencyFormatDecimal( order_tax ),
},
{
display: formatCurrency( shipping_tax ),
display: renderCurrency( shipping_tax ),
value: getCurrencyFormatDecimal( shipping_tax ),
},
{

View File

@ -175,19 +175,20 @@ class WC_Admin_REST_Reports_Controller extends WC_REST_Reports_Controller {
/**
* Get the order number for an order. If no filter is present for `woocommerce_order_number`, we can just return the ID.
* Returns the parent order number if the order is actually a refund.
*
* @param int $order_id Order ID.
* @return string
*/
public function get_order_number( $order_id ) {
if ( ! has_filter( 'woocommerce_order_number' ) ) {
return $order_id;
$order = wc_get_order( $order_id );
if ( 'shop_order_refund' === $order->get_type() ) {
$order = wc_get_order( $order->get_parent_id() );
}
$order = new WC_Order( $order_id );
if ( 'shop_order' !== $order->get_type() ) {
return $order_id;
if ( ! has_filter( 'woocommerce_order_number' ) ) {
return $order->get_id();
}
return $order->get_order_number();

View File

@ -94,7 +94,7 @@ class WC_Admin_Install {
order_id bigint(20) unsigned NOT NULL,
parent_id bigint(20) unsigned DEFAULT 0 NOT NULL,
date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
num_items_sold int(11) UNSIGNED DEFAULT 0 NOT NULL,
num_items_sold int(11) DEFAULT 0 NOT NULL,
gross_total double DEFAULT 0 NOT NULL,
tax_total double DEFAULT 0 NOT NULL,
shipping_total double DEFAULT 0 NOT NULL,

View File

@ -48,7 +48,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
'postcode' => 'postcode',
'date_registered' => 'date_registered',
'date_last_active' => 'date_last_active',
'orders_count' => 'COUNT( order_id ) as orders_count',
'orders_count' => 'SUM( CASE WHEN parent_id = 0 THEN 1 ELSE 0 END ) as orders_count',
'total_spend' => 'SUM( gross_total ) as total_spend',
'avg_order_value' => '( SUM( gross_total ) / COUNT( order_id ) ) as avg_order_value',
);

View File

@ -26,6 +26,7 @@ class WC_Admin_Reports_Orders_Data_Store extends WC_Admin_Reports_Data_Store imp
*/
protected $column_types = array(
'order_id' => 'intval',
'parent_id' => 'intval',
'date_created' => 'strval',
'status' => 'strval',
'customer_id' => 'intval',
@ -51,13 +52,14 @@ class WC_Admin_Reports_Orders_Data_Store extends WC_Admin_Reports_Data_Store imp
// Avoid ambigious columns in SQL query.
$this->report_columns = array(
'order_id' => "{$table_name}.order_id",
'parent_id' => "{$table_name}.parent_id",
'date_created' => "{$table_name}.date_created",
'status' => "REPLACE({$table_name}.status, 'wc-', '') as status",
'customer_id' => "{$table_name}.customer_id",
'net_total' => "{$table_name}.net_total",
'gross_total' => "{$table_name}.gross_total",
'num_items_sold' => "{$table_name}.num_items_sold",
'customer_type' => "(CASE WHEN {$table_name}.returning_customer <> 0 THEN 'returning' ELSE 'new' END) as customer_type",
'customer_type' => "(CASE WHEN {$table_name}.returning_customer = 1 THEN 'returning' WHEN {$table_name}.returning_customer = 0 THEN 'new' ELSE '' END) as customer_type",
);
}

View File

@ -433,6 +433,16 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
'%d',
);
if ( 'shop_order_refund' === $order->get_type() ) {
$parent_order = wc_get_order( $order->get_parent_id() );
$data['customer_id'] = WC_Admin_Reports_Customers_Data_Store::get_or_create_customer_from_order( $parent_order );
$data['parent_id'] = $parent_order->get_id();
$format[] = '%d';
} else {
$data['returning_customer'] = self::is_returning_customer( $order );
$data['customer_id'] = WC_Admin_Reports_Customers_Data_Store::get_or_create_customer_from_order( $order );
}
// Update or add the information to the DB.
$result = $wpdb->replace( $table_name, $data, $format );

View File

@ -402,6 +402,12 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
$shipping_tax_amount = $order->get_item_shipping_tax_amount( $order_item );
$coupon_amount = $order->get_item_coupon_amount( $order_item );
// Skip line items without changes to product quantity.
if ( ! $product_qty ) {
$num_updated++;
continue;
}
// Tax amount.
$tax_amount = 0;
$order_taxes = $order->get_taxes();

View File

@ -37,7 +37,7 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
protected $report_columns = array(
'items_sold' => 'SUM(product_qty) as items_sold',
'net_revenue' => 'SUM(product_net_revenue) AS net_revenue',
'orders_count' => 'COUNT(DISTINCT order_id) as orders_count',
'orders_count' => 'COUNT( DISTINCT ( CASE WHEN product_gross_revenue >= 0 THEN order_id END ) ) as orders_count',
'products_count' => 'COUNT(DISTINCT product_id) as products_count',
'variations_count' => 'COUNT(DISTINCT variation_id) as variations_count',
);

View File

@ -223,6 +223,11 @@
max-width: 40px;
}
.is-negative {
color: $alert-red;
font-weight: bold;
}
&.is-sorted {
background-color: $core-grey-light-100;
}

View File

@ -69,3 +69,13 @@ export function getCurrencyFormatString( number ) {
}
return number.toFixed( precision );
}
export function renderCurrency( number, currencySymbol ) {
if ( 'number' !== typeof number ) {
number = parseFloat( number );
}
if ( number < 0 ) {
return <span className="is-negative">{ formatCurrency( number, currencySymbol ) }</span>;
}
return formatCurrency( number, currencySymbol );
}