Starting stock management

master
Mark Moffat 2019-02-09 14:37:02 +10:30
parent 300057aa88
commit b352058e76
6 changed files with 46 additions and 7 deletions

View File

@ -97,6 +97,10 @@
}, },
"env": { "env": {
"type": "string" "type": "string"
},
"trackStock": {
"type": "boolean",
"default": false
} }
}, },
"required": [ "required": [

View File

@ -22,5 +22,6 @@
"currencySymbol": "£", "currencySymbol": "£",
"paymentGateway": "stripe", "paymentGateway": "stripe",
"databaseConnectionString": "mongodb://127.0.0.1:27017/expresscart", "databaseConnectionString": "mongodb://127.0.0.1:27017/expresscart",
"theme": "Cloth" "theme": "Cloth",
"trackStock": true
} }

View File

@ -11,16 +11,37 @@ router.get('/payment/:orderId', async (req, res, next) => {
let config = req.app.config; let config = req.app.config;
// render the payment complete message // 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){ if(err){
console.info(err.stack); 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`, { res.render(`${config.themeViews}payment_complete`, {
title: 'Payment complete', title: 'Payment complete',
config: req.app.config, config: req.app.config,
session: req.session, session: req.session,
pageCloseBtn: common.showCartCloseBtn('payment'), pageCloseBtn: common.showCartCloseBtn('payment'),
result: result, result: order,
message: common.clearSessionValue(req.session, 'message'), message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'), messageType: common.clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers, helpers: req.handlebars.helpers,
@ -217,6 +238,7 @@ router.post('/product/emptycart', (req, res, next) => {
// Add item to cart // Add item to cart
router.post('/product/addtocart', (req, res, next) => { router.post('/product/addtocart', (req, res, next) => {
const db = req.app.db; const db = req.app.db;
const config = req.app.config;
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1; let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;
const productComment = req.body.productComment ? req.body.productComment : null; 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.'}); 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); 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. // Doc used to test if existing in the cart with the options. If not found, we add new.

View File

@ -61,7 +61,6 @@ router.get('/admin/order/view/:id', common.restrict, (req, res) => {
if(err){ if(err){
console.info(err.stack); console.info(err.stack);
} }
res.render('order', { res.render('order', {
title: 'View order', title: 'View order',
result: result, result: result,

View File

@ -212,6 +212,7 @@ router.post('/admin/product/update', common.restrict, common.checkAccess, (req,
req.session.productTags = req.body.frmProductTags; req.session.productTags = req.body.frmProductTags;
req.session.productOptions = req.body.productOptJson; req.session.productOptions = req.body.productOptJson;
req.session.productComment = common.checkboxBool(req.body.frmProductComment); req.session.productComment = common.checkboxBool(req.body.frmProductComment);
req.session.productStock = req.body.frmProductStock ? req.body.frmProductStock : null;
// redirect to insert // redirect to insert
res.redirect('/admin/product/edit/' + req.body.frmProductId); 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, productPermalink: req.body.frmProductPermalink,
productTags: common.cleanHtml(req.body.frmProductTags), productTags: common.cleanHtml(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),
productStock: req.body.frmProductStock ? parseInt(req.body.frmProductStock) : null
}; };
console.log('test', productDoc);
// if no featured image // if no featured image
if(!product.productImage){ if(!product.productImage){
if(images.length > 0){ if(images.length > 0){

View File

@ -33,6 +33,12 @@
</select> </select>
</div> </div>
</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"> <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">