Allow disabling stock for certain products

master
Mark Moffat 2020-03-22 19:53:47 +10:30
parent 1c4303018a
commit 2a78fe5290
9 changed files with 66 additions and 42 deletions

View File

@ -162,8 +162,9 @@
"productPublished": true,
"productTags": "subscription",
"productOptions": "",
"productStock": null,
"productSubscription": "plan_XXXXXXXXXXXXXX"
"productStock": 0,
"productSubscription": "plan_XXXXXXXXXXXXXX",
"productStockDisable": true
}
],
"customers": [

View File

@ -38,6 +38,9 @@
},
"productStock": {
"type": ["number", "null"]
},
"productStockDisable": {
"type": "boolean"
}
},
"errorMessage": {

View File

@ -35,6 +35,9 @@
},
"productStock": {
"type": ["number", "null"]
},
"productStockDisable": {
"type": "boolean"
}
},
"errorMessage": {

View File

@ -205,5 +205,6 @@
"Logout": "Logout",
"Company": "Company",
"View": "View",
"Related products": "Related products"
"Related products": "Related products",
"Disable stock tracking": "Disable stock tracking"
}

View File

@ -244,6 +244,7 @@ $(document).ready(function (){
productPrice: $('#productPrice').val(),
productPublished: $('#productPublished').val(),
productStock: $('#productStock').val(),
productStockDisable: $('#productStockDisable').is(':checked'),
productDescription: $('#productDescription').val(),
productPermalink: $('#productPermalink').val(),
productOptions: $('#productOptions').val(),

File diff suppressed because one or more lines are too long

View File

@ -548,40 +548,43 @@ router.post('/product/addtocart', async (req, res, next) => {
// If stock management on check there is sufficient stock for this product
if(config.trackStock){
// If there is more stock than total (ignoring held)
if(productQuantity > product.productStock){
return res.status(400).json({ message: 'There is insufficient stock of this product.' });
}
const stockHeld = await db.cart.aggregate(
{
$match: {
cart: { $elemMatch: { productId: product._id.toString() } }
}
},
{ $unwind: '$cart' },
{
$group: {
_id: '$cart.productId',
sumHeld: { $sum: '$cart.quantity' }
}
},
{
$project: {
sumHeld: 1
}
}
).toArray();
// If there is stock
if(stockHeld.length > 0){
const totalHeld = _.find(stockHeld, { _id: product._id.toString() }).sumHeld;
const netStock = product.productStock - totalHeld;
// Check there is sufficient stock
if(productQuantity > netStock){
// Only if not disabled
if(product.productStockDisable !== true){
// If there is more stock than total (ignoring held)
if(productQuantity > product.productStock){
return res.status(400).json({ message: 'There is insufficient stock of this product.' });
}
const stockHeld = await db.cart.aggregate(
{
$match: {
cart: { $elemMatch: { productId: product._id.toString() } }
}
},
{ $unwind: '$cart' },
{
$group: {
_id: '$cart.productId',
sumHeld: { $sum: '$cart.quantity' }
}
},
{
$project: {
sumHeld: 1
}
}
).toArray();
// If there is stock
if(stockHeld.length > 0){
const totalHeld = _.find(stockHeld, { _id: product._id.toString() }).sumHeld;
const netStock = product.productStock - totalHeld;
// Check there is sufficient stock
if(productQuantity > netStock){
return res.status(400).json({ message: 'There is insufficient stock of this product.' });
}
}
}
}

View File

@ -108,7 +108,8 @@ router.post('/admin/product/insert', restrict, checkAccess, async (req, res) =>
productOptions: productOptions || null,
productComment: common.checkboxBool(req.body.productComment),
productAddedDate: new Date(),
productStock: common.safeParseInt(req.body.productStock) || null
productStock: common.safeParseInt(req.body.productStock) || null,
productStockDisable: common.convertBool(req.body.productStockDisable)
};
// Validate the body again schema
@ -251,7 +252,8 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
productTags: req.body.productTags,
productOptions: productOptions || null,
productComment: common.checkboxBool(req.body.productComment),
productStock: common.safeParseInt(req.body.productStock) || null
productStock: common.safeParseInt(req.body.productStock) || null,
productStockDisable: common.convertBool(req.body.productStockDisable)
};
// Validate the body again schema

View File

@ -37,10 +37,20 @@
</div>
</div>
{{#if config.trackStock}}
<div class="col-sm-6">
<div class="form-group">
<label for="productStock" class="control-label">{{ @root.__ "Stock level" }}</label>
<input type="number" id="productStock" class="form-control" value="{{result.productStock}}" step="any" />
<div class="row">
<div class="col-sm-5 ml-3">
<div class="form-group">
<label for="productStock" class="control-label">{{ @root.__ "Stock level" }}</label>
<input type="number" id="productStock" class="form-control" value="{{result.productStock}}" step="any" />
</div>
</div>
<div class="col-sm-5">
<label for="productStockDisable" class="control-label">{{ @root.__ "Disable stock tracking" }}</label>
<div class="checkbox">
<label>
<input class="productStockDisable" type="checkbox" {{checkedState result.productStockDisable}} id="productStockDisable">
</label>
</div>
</div>
</div>
{{/if}}