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';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2023-02-28 16:55:49 +00:00
|
|
|
import { CurrencyContext } from '@woocommerce/currency';
|
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';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2018-12-14 23:58:08 +00:00
|
|
|
|
2022-01-06 12:53:30 +00:00
|
|
|
const stockStatuses = getAdminSetting( 'stockStatuses', {} );
|
2019-09-23 21:47:08 +00:00
|
|
|
|
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 [
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Product / Variation', 'woocommerce' ),
|
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
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'SKU', 'woocommerce' ),
|
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
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Status', 'woocommerce' ),
|
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
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Stock', 'woocommerce' ),
|
2018-12-14 23:58:08 +00:00
|
|
|
key: 'stock_quantity',
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:47:29 +00:00
|
|
|
getRowsContent( products = [] ) {
|
2018-12-14 23:58:08 +00:00
|
|
|
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',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
) }
|
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,
|
2021-07-27 20:11:16 +00:00
|
|
|
value: stockStatuses[ 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
|
|
|
|
)
|
2022-03-30 09:00:04 +00:00
|
|
|
: __( 'N/A', 'woocommerce' ),
|
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 [
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: _n( 'Product', 'Products', products, 'woocommerce' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', products ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Out of stock', 'woocommerce' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', outofstock ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Low stock', 'woocommerce' ),
|
2020-08-13 20:25:08 +00:00
|
|
|
value: formatValue( currency, 'number', lowstock ),
|
2018-12-14 23:58:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'On backorder', 'woocommerce' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', onbackorder ),
|
2019-02-14 03:31:27 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'In stock', 'woocommerce' ),
|
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',
|
|
|
|
} }
|
2022-03-30 09:00:04 +00:00
|
|
|
title={ __( 'Stock', 'woocommerce' ) }
|
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;
|