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); cartUpdate(qtyElement);
}); });
$(document).on('change', '.cart-product-quantity', function (e){ $(document).on('change', '.cart-product-quantity', function (e){
cartUpdate(e.target); cartUpdate(e.target);
}); });
@ -735,7 +733,7 @@ function updateCart(){
$('#cart-count').text(msg.totalCartItems); $('#cart-count').text(msg.totalCartItems);
}) })
.fail(function(msg){ .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 // Updates a single product quantity
router.post('/product/updatecart', (req, res, next) => { router.post('/product/updatecart', (req, res, next) => {
const db = req.app.db; const db = req.app.db;
const config = req.app.config;
let cartItems = JSON.parse(req.body.items); let cartItems = JSON.parse(req.body.items);
let hasError = false; let hasError = false;
let stockError = false;
async.eachSeries(cartItems, (cartItem, callback) => { async.eachSeries(cartItems, (cartItem, callback) => {
let productQuantity = cartItem.itemQuantity ? cartItem.itemQuantity : 1; 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)); console.error(colors.red('Error updating cart', err));
} }
if(product){ 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); let productPrice = parseFloat(product.productPrice).toFixed(2);
if(req.session.cart[cartItem.cartIndex]){ if(req.session.cart[cartItem.cartIndex]){
req.session.cart[cartItem.cartIndex].quantity = productQuantity; req.session.cart[cartItem.cartIndex].quantity = productQuantity;
@ -202,9 +214,13 @@ router.post('/product/updatecart', (req, res, next) => {
// show response // show response
if(hasError === false){ if(hasError === false){
res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length}); 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{ }else{
res.status(400).json({message: 'There was an error updating the cart', totalCartItems: Object.keys(req.session.cart).length}); 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, productTags: req.body.frmProductTags,
productOptions: common.cleanHtml(req.body.productOptJson), productOptions: common.cleanHtml(req.body.productOptJson),
productComment: common.checkboxBool(req.body.frmProductComment), 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) => { 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.productPermalink = req.body.productOptJson;
req.session.productComment = common.checkboxBool(req.body.frmProductComment); req.session.productComment = common.checkboxBool(req.body.frmProductComment);
req.session.productTags = req.body.frmProductTags; req.session.productTags = req.body.frmProductTags;
req.session.productStock = req.body.frmProductStock ? parseInt(req.body.frmProductStock) : null;
// redirect to insert // redirect to insert
res.redirect('/admin/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.productPermalink = req.body.productOptJson;
req.session.productComment = common.checkboxBool(req.body.frmProductComment); req.session.productComment = common.checkboxBool(req.body.frmProductComment);
req.session.productTags = req.body.frmProductTags; 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.message = 'Error: Inserting product';
req.session.messageType = 'danger'; req.session.messageType = 'danger';

View File

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

View File

@ -32,6 +32,14 @@
</select> </select>
</div> </div>
</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"> <div class="form-group" id="editor-wrapper">
<label for="editor" class="col-sm-2 control-label">Product description *</label> <label for="editor" class="col-sm-2 control-label">Product description *</label>
<div class="col-sm-10"> <div class="col-sm-10">