Starting stock management
parent
300057aa88
commit
b352058e76
|
@ -97,6 +97,10 @@
|
|||
},
|
||||
"env": {
|
||||
"type": "string"
|
||||
},
|
||||
"trackStock": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
|
@ -22,5 +22,6 @@
|
|||
"currencySymbol": "£",
|
||||
"paymentGateway": "stripe",
|
||||
"databaseConnectionString": "mongodb://127.0.0.1:27017/expresscart",
|
||||
"theme": "Cloth"
|
||||
"theme": "Cloth",
|
||||
"trackStock": true
|
||||
}
|
||||
|
|
|
@ -11,16 +11,37 @@ router.get('/payment/:orderId', async (req, res, next) => {
|
|||
let config = req.app.config;
|
||||
|
||||
// render the payment complete message
|
||||
db.orders.findOne({_id: common.getId(req.params.orderId)}, async (err, result) => {
|
||||
db.orders.findOne({_id: common.getId(req.params.orderId)}, async (err, order) => {
|
||||
if(err){
|
||||
console.info(err.stack);
|
||||
}
|
||||
|
||||
// If stock management is turned on payment approved update stock level
|
||||
if(config.trackStock && req.session.paymentApproved){
|
||||
order.orderProducts.forEach(async (product) => {
|
||||
const dbProduct = await db.products.findOne({_id: common.getId(product.productId)});
|
||||
let newStockLevel = dbProduct.productStock - product.quantity;
|
||||
if(newStockLevel < 1){
|
||||
newStockLevel = 0;
|
||||
}
|
||||
|
||||
// Update product stock
|
||||
await db.products.update({
|
||||
_id: common.getId(product.productId)
|
||||
}, {
|
||||
$set: {
|
||||
productStock: newStockLevel
|
||||
}
|
||||
}, {multi: false});
|
||||
});
|
||||
}
|
||||
|
||||
res.render(`${config.themeViews}payment_complete`, {
|
||||
title: 'Payment complete',
|
||||
config: req.app.config,
|
||||
session: req.session,
|
||||
pageCloseBtn: common.showCartCloseBtn('payment'),
|
||||
result: result,
|
||||
result: order,
|
||||
message: common.clearSessionValue(req.session, 'message'),
|
||||
messageType: common.clearSessionValue(req.session, 'messageType'),
|
||||
helpers: req.handlebars.helpers,
|
||||
|
@ -217,6 +238,7 @@ router.post('/product/emptycart', (req, res, next) => {
|
|||
// Add item to cart
|
||||
router.post('/product/addtocart', (req, res, next) => {
|
||||
const db = req.app.db;
|
||||
const config = req.app.config;
|
||||
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;
|
||||
const productComment = req.body.productComment ? req.body.productComment : null;
|
||||
|
||||
|
@ -242,6 +264,13 @@ router.post('/product/addtocart', (req, res, next) => {
|
|||
return res.status(400).json({message: 'Error updating cart. Please try again.'});
|
||||
}
|
||||
|
||||
// If stock management on check there is sufficient stock for this product
|
||||
if(config.trackStock){
|
||||
if(productQuantity > product.productStock){
|
||||
return res.status(400).json({message: 'There is insufficient stock of this product.'});
|
||||
}
|
||||
}
|
||||
|
||||
let productPrice = parseFloat(product.productPrice).toFixed(2);
|
||||
|
||||
// Doc used to test if existing in the cart with the options. If not found, we add new.
|
||||
|
|
|
@ -61,7 +61,6 @@ router.get('/admin/order/view/:id', common.restrict, (req, res) => {
|
|||
if(err){
|
||||
console.info(err.stack);
|
||||
}
|
||||
|
||||
res.render('order', {
|
||||
title: 'View order',
|
||||
result: result,
|
||||
|
|
|
@ -212,6 +212,7 @@ router.post('/admin/product/update', common.restrict, common.checkAccess, (req,
|
|||
req.session.productTags = req.body.frmProductTags;
|
||||
req.session.productOptions = req.body.productOptJson;
|
||||
req.session.productComment = common.checkboxBool(req.body.frmProductComment);
|
||||
req.session.productStock = req.body.frmProductStock ? req.body.frmProductStock : null;
|
||||
|
||||
// redirect to insert
|
||||
res.redirect('/admin/product/edit/' + req.body.frmProductId);
|
||||
|
@ -225,11 +226,10 @@ router.post('/admin/product/update', common.restrict, common.checkAccess, (req,
|
|||
productPermalink: req.body.frmProductPermalink,
|
||||
productTags: common.cleanHtml(req.body.frmProductTags),
|
||||
productOptions: common.cleanHtml(req.body.productOptJson),
|
||||
productComment: common.checkboxBool(req.body.frmProductComment)
|
||||
productComment: common.checkboxBool(req.body.frmProductComment),
|
||||
productStock: req.body.frmProductStock ? parseInt(req.body.frmProductStock) : null
|
||||
};
|
||||
|
||||
console.log('test', productDoc);
|
||||
|
||||
// if no featured image
|
||||
if(!product.productImage){
|
||||
if(images.length > 0){
|
||||
|
|
|
@ -33,6 +33,12 @@
|
|||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<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>
|
||||
<div class="form-group">
|
||||
<label for="editor" class="col-sm-2 control-label">Product description *</label>
|
||||
<div class="col-sm-10">
|
||||
|
|
Loading…
Reference in New Issue