addtocash using sendcoins Circuit

This commit is contained in:
Surya Paolo
2024-01-03 15:46:42 +01:00
parent 75ef581635
commit 66e4c577c7
6 changed files with 97 additions and 44 deletions

View File

@@ -105,7 +105,6 @@ module.exports.createMovementCashByOrdersCart = async function (ordersCart, user
fromUsername: ordersCart.user.username,
toUsername: usernameStore,
causale: 'Pagato Ordine n.' + ordersCart.numorder,
date_payment: new Date(),
price: ordersCart.totalPrice,
internal,
}

View File

@@ -708,7 +708,7 @@ CircuitSchema.statics.sendCoins = async function (onlycheck, idapp, usernameOrig
if (!onlycheck) {
// Add a Transaction !
if (ris.cansend) {
ris.rec = await Movement.addMov(idapp, accountorigTable, accountdestTable, myqty, extrarec.causal, extrarec.notifId);
ris.rec = await Movement.addMov(idapp, accountorigTable, accountdestTable, myqty, extrarec.causal, extrarec.notifId, null);
}
if (ris.cansend && ris.rec) {
@@ -1225,6 +1225,26 @@ CircuitSchema.statics.setFido = async function (idapp, username, circuitName, gr
return null;
};
CircuitSchema.statics.addMovementByOrdersCart = async function (idapp, ordersCart, usernameDest, groupDest) {
const { User } = require('../models/user');
let extrarec = {
causale: 'Pagato Ordine n.' + ordersCart.numorder,
circuitname: 'Euro',
idOrdersCart: ordersCart._id,
qty: ordersCart.totalPrice,
dest: usernameDest,
groupdest: groupDest,
contoComDest: '',
};
const usernameOrig = await User.getUsernameById(idapp, userId);
return this.sendCoins(false, idapp, usernameOrig, extrarec);
};
const Circuit = mongoose.model('Circuit', CircuitSchema);
Circuit.createIndexes((err) => {

View File

@@ -36,6 +36,7 @@ const MovementSchema = new Schema({
accountToId: {
type: String,
},
idOrdersCart: { type: Schema.Types.ObjectId, ref: 'OrdersCart' },
causal_table: {
type: String,
},
@@ -56,6 +57,10 @@ const MovementSchema = new Schema({
expiringDate: {
type: Date,
},
confirmed: {
type: Boolean,
default: false,
},
});
MovementSchema.statics.findAllIdApp = async function (idapp) {
@@ -90,7 +95,7 @@ MovementSchema.statics.executeQueryTable = function (idapp, params) {
return tools.executeQueryTable(this, 0, params);
};
MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accountToIdTable, amount, causal, notifId) {
MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accountToIdTable, amount, causal, notifId, idOrdersCart) {
try {
// Only positive values
@@ -102,6 +107,7 @@ MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accou
transactionDate: new Date(),
accountFromId: accountFromIdTable._id,
accountToId: accountToIdTable._id,
idOrdersCart: idOrdersCart._id ?? null,
amount,
causal,
residual: 0,

View File

@@ -13,6 +13,8 @@ const Product = require('../models/product');
const Cash = require('../models/cash');
const ProductInfo = require('../models/productInfo');
const { Circuit } = require('../models/circuit');
const tools = require('../tools/general');
const { ObjectID } = require('mongodb');
@@ -477,13 +479,48 @@ module.exports.addOrderToCash = async function (idorderscart, usernameStore, req
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
const mycash = await Cash.createMovementCashByOrdersCart(myorderscart, usernameStore, req);
const mymov = await Circuit.addMovementByOrdersCart(id, myOrderCart, usernameStore, '');
// const mycash = await Cash.createMovementCashByOrdersCart(myorderscart, usernameStore, req);
} catch (e) {
console.error('Err', e);
}
}
module.exports.updateMagazzinoOrdineInLavorazione = async function (idorderscart) {
try {
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
if (myorderscart) {
for (const idkey in myorderscart.items) {
let order = myorderscart.items[idkey].order;
if (!order.confermato) { // Se ancora non è stato confermato:
let update = {
$inc: {
bookedQtyOrdered: order.quantity,
}
};
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
update = {
$inc: {
bookedGASQtyOrdered: order.quantitypreordered,
}
};
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
}
}
}
} catch (e) {
console.error('Err', e);
}
}
module.exports.updateStockQtaDalMagazzinoOrdineConfermato = async function (idorderscart) {
try {
@@ -507,40 +544,7 @@ module.exports.updateStockQtaDalMagazzinoOrdineConfermato = async function (idor
$inc: {
maxbookableGASQty: -order.quantitypreordered,
bookableGASBloccatiQty: order.quantitypreordered,
bookedGASQtyOrdered: order.quantitypreordered,
}
};
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
}
}
}
} catch (e) {
console.error('Err', e);
}
}
module.exports.updateMagazzinoOrdineInLavorazione = async function (idorderscart) {
try {
const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean();
if (myorderscart) {
for (const idkey in myorderscart.items) {
let order = myorderscart.items[idkey].order;
if (!order.confermato) { // Se ancora non è stato confermato:
let update = {
$inc: {
bookedQtyConfirmed: order.quantity,
}
};
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
update = {
$inc: {
bookedGASQtyOrdered: order.quantitypreordered,
bookedGASQtyConfirmed: order.quantitypreordered,
}
};
await Product.findOneAndUpdate({ _id: order.idProduct }, update, { new: false });
@@ -613,6 +617,11 @@ module.exports.updateStockQtaPerCancellazioneOrdine = async function (idordersca
}
}
fieldstoUpdate = {
...fieldstoUpdate,
bookedQtyOrdered,
}
update = {
$inc: fieldstoUpdate,
};
@@ -627,10 +636,15 @@ module.exports.updateStockQtaPerCancellazioneOrdine = async function (idordersca
...fieldstoUpdate,
maxbookableGASQty: order.quantitypreordered,
bookableGASBloccatiQty: -order.quantitypreordered,
bookedGASQtyOrdered: -order.quantitypreordered,
bookedGASQtyConfirmed: -order.quantitypreordered,
}
}
fieldstoUpdate = {
...fieldstoUpdate,
bookedGASQtyOrdered,
}
update = {
$inc: fieldstoUpdate,
};
@@ -649,7 +663,7 @@ module.exports.updateStockQtaPerCancellazioneOrdine = async function (idordersca
module.exports.updateCmd = async function (ordersCart, status, value, req, options) {
const usernameStore = options.usernameStore ?? null;
const usernameStore = (options && options.usernameStore) ?? null;
let myOrderCart = await OrdersCart.findOne({ _id: ordersCart._id })
.populate('items.order').lean();

View File

@@ -84,6 +84,10 @@ const productSchema = new Schema({
type: Number,
default: 0,
},
bookedQtyOrdered: { // Quantità Prenotate ordinate (in Lavorazione)
type: Number,
default: 0,
},
bookedQtyConfirmed: { // Quantità Prenotate Confermate Totali
type: Number,
default: 0,
@@ -98,7 +102,11 @@ const productSchema = new Schema({
type: Number,
default: 0,
},
bookedGASQtyOrdered: { // Quantità Ordine GAS Prenotate Confermate Totali
bookedGASQtyOrdered: { // Quantità Ordine GAS Prenotate Totali
type: Number,
default: 0,
},
bookedGASQtyConfirmed: { // Quantità Ordine GAS Confermate Totali
type: Number,
default: 0,
},
@@ -548,6 +556,7 @@ module.exports.singlerecconvert_AfterImport_AndSave = async function (idapp, pro
maxBookableSinglePersQty: 0,
bookedGASQtyOrdered: 0,
bookableGASBloccatiQty: 0,
bookedGASQtyConfirmed: 0,
// qtyToReachForGas: 0,
// maxbookableGASQty: 0,
}

View File

@@ -17,6 +17,7 @@ const _ = require('lodash');
const Product = require('../models/product');
const Order = require('../models/order');
const Variant = require('../models/variant');
const { User } = require('../models/user');
/*const Department = require('../models/Department')
const Category = require('../models/Category')
@@ -255,6 +256,8 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
let numorder = await OrdersCart.getLastNumOrder(idapp);
let numord_pers = await OrdersCart.getLastNumOrdPers(userId, idapp);
const userDest = await User.getUserById(idapp, userId);
// Esiste l'ordine ?
let myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
if (!myorderCart) {
@@ -306,7 +309,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
await OrdersCart.updateCmd(orders[0], status, true, req, options);
// Invia la email dell'Ordine
sendemail.sendEmail_OrderProduct(user.lang, idapp, orders[0], user)
sendemail.sendEmail_OrderProduct(userDest.lang, idapp, orders[0], userDest)
.then(async (ris) => {
myorderCart = await OrdersCart.findById(idordercart).lean();
return res.send({
@@ -356,6 +359,8 @@ router.post('/:userId/ordercartstatus', authenticate, async function (req, res,
const { User } = require('../models/user');
const userDest = await User.getUserById(userId);
let orderCart = await OrdersCart.findOne({ idapp, _id: order_id }).lean();
if ((userId !== String(req.user._id)) && !User.isManager(req.user.perm)) {
@@ -388,7 +393,7 @@ router.post('/:userId/ordercartstatus', authenticate, async function (req, res,
}
if (ordertype !== '') {
sendemail.sendEmail_Order(user.lang, idapp, orderCart, user, ordertype, status)
sendemail.sendEmail_Order(userDest.lang, idapp, orderCart, userDest, ordertype, status)
.then((ris) => {
})