Stock management changes

master
Mark Moffat 2019-02-09 21:16:41 +10:30
parent b352058e76
commit 3ab76684bb
6 changed files with 34 additions and 7 deletions

View File

@ -142,9 +142,7 @@ $(document).ready(function (){
cartUpdate(qtyElement);
});
$(document).on('change', '.cart-product-quantity', function (e){
cartUpdate(e.target);
});
@ -735,7 +733,7 @@ function updateCart(){
$('#cart-count').text(msg.totalCartItems);
})
.fail(function(msg){
showNotification(msg.responseJSON.message, 'danger');
showNotification(msg.responseJSON.message, 'danger', true);
});
}

File diff suppressed because one or more lines are too long

View File

@ -168,8 +168,10 @@ router.get('/product/:id', (req, res) => {
// Updates a single product quantity
router.post('/product/updatecart', (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
let cartItems = JSON.parse(req.body.items);
let hasError = false;
let stockError = false;
async.eachSeries(cartItems, (cartItem, callback) => {
let productQuantity = cartItem.itemQuantity ? cartItem.itemQuantity : 1;
@ -183,6 +185,16 @@ router.post('/product/updatecart', (req, res, next) => {
console.error(colors.red('Error updating cart', err));
}
if(product){
// If stock management on check there is sufficient stock for this product
if(config.trackStock){
if(productQuantity > product.productStock){
hasError = true;
stockError = true;
callback(null);
return;
}
}
let productPrice = parseFloat(product.productPrice).toFixed(2);
if(req.session.cart[cartItem.cartIndex]){
req.session.cart[cartItem.cartIndex].quantity = productQuantity;
@ -202,9 +214,13 @@ router.post('/product/updatecart', (req, res, next) => {
// show response
if(hasError === false){
res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length});
}else{
if(stockError){
res.status(400).json({message: 'There is insufficient stock of this product.', totalCartItems: Object.keys(req.session.cart).length});
}else{
res.status(400).json({message: 'There was an error updating the cart', totalCartItems: Object.keys(req.session.cart).length});
}
}
});
});

View File

@ -86,7 +86,8 @@ router.post('/admin/product/insert', common.restrict, common.checkAccess, (req,
productTags: req.body.frmProductTags,
productOptions: common.cleanHtml(req.body.productOptJson),
productComment: common.checkboxBool(req.body.frmProductComment),
productAddedDate: new Date()
productAddedDate: new Date(),
productStock: req.body.frmProductStock ? parseInt(req.body.frmProductStock) : null
};
db.products.count({'productPermalink': req.body.frmProductPermalink}, (err, product) => {
@ -106,6 +107,7 @@ router.post('/admin/product/insert', common.restrict, common.checkAccess, (req,
req.session.productPermalink = req.body.productOptJson;
req.session.productComment = common.checkboxBool(req.body.frmProductComment);
req.session.productTags = req.body.frmProductTags;
req.session.productStock = req.body.frmProductStock ? parseInt(req.body.frmProductStock) : null;
// redirect to insert
res.redirect('/admin/insert');
@ -122,6 +124,7 @@ router.post('/admin/product/insert', common.restrict, common.checkAccess, (req,
req.session.productPermalink = req.body.productOptJson;
req.session.productComment = common.checkboxBool(req.body.frmProductComment);
req.session.productTags = req.body.frmProductTags;
req.session.productStock = req.body.frmProductStock ? parseInt(req.body.frmProductStock) : null;
req.session.message = 'Error: Inserting product';
req.session.messageType = 'danger';

View File

@ -33,12 +33,14 @@
</select>
</div>
</div>
{{#if config.trackStock}}
<div class="form-group">
<label for="frmProductStock" class="col-sm-2 control-label">Stock level</label>
<div class="col-sm-6">
<input type="number" name="frmProductStock" class="form-control" value="{{result.productStock}}" step="any" />
</div>
</div>
{{/if}}
<div class="form-group">
<label for="editor" class="col-sm-2 control-label">Product description *</label>
<div class="col-sm-10">

View File

@ -32,6 +32,14 @@
</select>
</div>
</div>
{{#if config.trackStock}}
<div class="form-group">
<label for="frmProductStock" class="col-sm-2 control-label">Stock level</label>
<div class="col-sm-6">
<input type="number" name="frmProductStock" class="form-control" value="{{productStock}}" step="any" />
</div>
</div>
{{/if}}
<div class="form-group" id="editor-wrapper">
<label for="editor" class="col-sm-2 control-label">Product description *</label>
<div class="col-sm-10">