2018-12-14 23:58:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-05-08 14:25:46 +00:00
|
|
|
import { __, _n, _x } from '@wordpress/i18n';
|
2018-12-14 23:58:08 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2020-03-02 22:22:32 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2018-12-14 23:58:08 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { formatValue } from '@woocommerce/number';
|
2019-11-22 17:07:26 +00:00
|
|
|
import { getAdminLink, getSetting } from '@woocommerce/wc-admin-settings';
|
2018-12-14 23:58:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import ReportTable from '../../components/report-table';
|
2019-05-08 14:25:46 +00:00
|
|
|
import { isLowStock } from './utils';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { CurrencyContext } from '../../../lib/currency-context';
|
2018-12-14 23:58:08 +00:00
|
|
|
|
2019-09-23 21:47:08 +00:00
|
|
|
const stockStatuses = getSetting( 'stockStatuses', {} );
|
|
|
|
|
2020-04-02 21:54:38 +00:00
|
|
|
class StockReportTable extends Component {
|
2018-12-14 23:58:08 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
|
|
|
this.getSummary = this.getSummary.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Product / Variation', 'woocommerce-admin' ),
|
2019-01-10 17:10:31 +00:00
|
|
|
key: 'title',
|
2018-12-14 23:58:08 +00:00
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
2019-01-10 17:10:31 +00:00
|
|
|
isSortable: true,
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'SKU', 'woocommerce-admin' ),
|
2018-12-14 23:58:08 +00:00
|
|
|
key: 'sku',
|
2019-01-10 17:10:31 +00:00
|
|
|
isSortable: true,
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Status', 'woocommerce-admin' ),
|
2018-12-14 23:58:08 +00:00
|
|
|
key: 'stock_status',
|
2019-01-21 17:11:20 +00:00
|
|
|
isSortable: true,
|
|
|
|
defaultSort: true,
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Stock', 'woocommerce-admin' ),
|
2018-12-14 23:58:08 +00:00
|
|
|
key: 'stock_quantity',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( products ) {
|
|
|
|
const { query } = this.props;
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return products.map( ( product ) => {
|
2019-05-08 14:25:46 +00:00
|
|
|
const {
|
|
|
|
id,
|
2020-02-14 02:23:21 +00:00
|
|
|
manage_stock: manageStock,
|
|
|
|
parent_id: parentId,
|
2019-05-08 14:25:46 +00:00
|
|
|
sku,
|
2020-02-14 02:23:21 +00:00
|
|
|
stock_quantity: stockQuantity,
|
|
|
|
stock_status: stockStatus,
|
|
|
|
low_stock_amount: lowStockAmount,
|
2019-05-08 14:25:46 +00:00
|
|
|
} = product;
|
2018-12-14 23:58:08 +00:00
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
const name = decodeEntities( product.name );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const productDetailLink = getNewPath(
|
|
|
|
persistedQuery,
|
|
|
|
'/analytics/products',
|
|
|
|
{
|
|
|
|
filter: 'single_product',
|
|
|
|
products: parentId || id,
|
|
|
|
}
|
|
|
|
);
|
2018-12-14 23:58:08 +00:00
|
|
|
|
|
|
|
const nameLink = (
|
|
|
|
<Link href={ productDetailLink } type="wc-admin">
|
2019-02-18 18:24:48 +00:00
|
|
|
{ name }
|
2018-12-14 23:58:08 +00:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const editProductLink = getAdminLink(
|
|
|
|
'post.php?action=edit&post=' + ( parentId || id )
|
|
|
|
);
|
|
|
|
const stockStatusLink = isLowStock(
|
|
|
|
stockStatus,
|
|
|
|
stockQuantity,
|
|
|
|
lowStockAmount
|
|
|
|
) ? (
|
2019-11-22 17:07:26 +00:00
|
|
|
<Link href={ editProductLink } type="wp-admin">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ _x(
|
|
|
|
'Low',
|
|
|
|
'Indication of a low quantity',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-05-08 14:25:46 +00:00
|
|
|
</Link>
|
|
|
|
) : (
|
2019-11-22 17:07:26 +00:00
|
|
|
<Link href={ editProductLink } type="wp-admin">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ stockStatuses[ stockStatus ] }
|
2018-12-14 23:58:08 +00:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: nameLink,
|
2019-02-18 18:24:48 +00:00
|
|
|
value: name,
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: sku,
|
|
|
|
value: sku,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: stockStatusLink,
|
2020-02-14 02:23:21 +00:00
|
|
|
value: stockStatus,
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: manageStock
|
2020-04-02 21:54:38 +00:00
|
|
|
? formatValue(
|
2020-06-17 23:33:40 +00:00
|
|
|
this.context.getCurrencyConfig(),
|
2020-04-02 21:54:38 +00:00
|
|
|
'number',
|
|
|
|
stockQuantity
|
|
|
|
)
|
2019-11-21 21:51:52 +00:00
|
|
|
: __( 'N/A', 'woocommerce-admin' ),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: stockQuantity,
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getSummary( totals ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
products = 0,
|
|
|
|
outofstock = 0,
|
|
|
|
lowstock = 0,
|
|
|
|
instock = 0,
|
|
|
|
onbackorder = 0,
|
|
|
|
} = totals;
|
2020-06-17 23:33:40 +00:00
|
|
|
const currency = this.context.getCurrencyConfig();
|
2018-12-14 23:58:08 +00:00
|
|
|
return [
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
|
|
|
'product',
|
|
|
|
'products',
|
|
|
|
products,
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', products ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'out of stock', outofstock, 'woocommerce-admin' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', outofstock ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'low stock', lowstock, 'woocommerce-admin' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( 'currency, number', lowstock ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'on backorder', onbackorder, 'woocommerce-admin' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', onbackorder ),
|
2019-02-14 03:31:27 +00:00
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'in stock', instock, 'woocommerce-admin' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', instock ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-08-27 19:33:31 +00:00
|
|
|
const { advancedFilters, filters, query } = this.props;
|
2018-12-14 23:58:08 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ReportTable
|
|
|
|
endpoint="stock"
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
getRowsContent={ this.getRowsContent }
|
2019-02-14 03:31:27 +00:00
|
|
|
getSummary={ this.getSummary }
|
2020-03-31 15:08:40 +00:00
|
|
|
summaryFields={ [
|
|
|
|
'products',
|
|
|
|
'outofstock',
|
|
|
|
'lowstock',
|
|
|
|
'instock',
|
|
|
|
'onbackorder',
|
|
|
|
] }
|
2018-12-14 23:58:08 +00:00
|
|
|
query={ query }
|
|
|
|
tableQuery={ {
|
2019-01-21 17:11:20 +00:00
|
|
|
orderby: query.orderby || 'stock_status',
|
|
|
|
order: query.order || 'asc',
|
2018-12-14 23:58:08 +00:00
|
|
|
type: query.type || 'all',
|
|
|
|
} }
|
2019-03-13 17:14:02 +00:00
|
|
|
title={ __( 'Stock', 'woocommerce-admin' ) }
|
2019-03-21 03:25:05 +00:00
|
|
|
filters={ filters }
|
2019-08-27 19:33:31 +00:00
|
|
|
advancedFilters={ advancedFilters }
|
2018-12-14 23:58:08 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 21:54:38 +00:00
|
|
|
|
|
|
|
StockReportTable.contextType = CurrencyContext;
|
|
|
|
|
|
|
|
export default StockReportTable;
|