diff --git a/src/server/models/cash.js b/src/server/models/cash.js index ee99fa3..1b937cd 100755 --- a/src/server/models/cash.js +++ b/src/server/models/cash.js @@ -5,6 +5,8 @@ const tools = require('../tools/general'); const { ObjectID } = require('mongodb'); +const shared_consts = require('../tools/shared_nodejs'); + mongoose.Promise = global.Promise; mongoose.level = "F"; @@ -22,47 +24,29 @@ const CashSchema = new Schema({ type: String, }, idCashCategory: { - type: String - }, - idSubCashCategory: { - type: String + type: String, }, type: { // CashType: TYPE_IN, TYPE_OUT - type: Number + type: Number, + required: true, }, + fromUserId: { type: Schema.Types.ObjectId, ref: 'User' }, + toUserId: { type: Schema.Types.ObjectId, ref: 'User' }, + idOrdersCart: { type: Schema.Types.ObjectId, ref: 'OrdersCart' }, date_created: { - type: Date + type: Date, + default: Date.now, }, date_payment: { - type: Date + type: Date, + default: Date.now, }, - descr: { + causale: { type: String, }, price: { type: Number }, - quantity: { - type: Number - }, - total: { - type: Number - }, - fromWalletUserId: { // walletCommonCash or any Member - type: String, - }, - toWalletUserId: { // walletCommonCash or any Member - type: String, - }, - walletStatus: { // WalletFinalStatusType: None: 0, InCommonCash: 1, InMyWallet : 2 - type: Number, - }, - walletTemporaryToUserId: { // WalletCommonCash - type: String, - }, - walletCashId: { - type: String, - }, internal: { type: Boolean, default: false, @@ -71,9 +55,6 @@ const CashSchema = new Schema({ type: Boolean, default: false, }, - notes: { - type: String - }, confirmed: { type: Boolean, default: false, @@ -114,8 +95,22 @@ module.exports.getCashByID = function (id, callback) { Cash.findById(id, callback); } -module.exports.createCash = async function (Cash) { - const CashModel = new Cash(Cash); +module.exports.createMovementCashByOrdersCart = async function (ordersCart, userIdStore, req, internal) { + + let casjobj = { + idapp: req.user.iapp, + idOrdersCart: ordersCart._id, + creatorUserId: req.user ? req.user._id : null, + type: shared_consts.TYPECASH.IN, + fromUserId: ordersCart.userId, + toUserId: userIdStore, + causale: 'Pagato Ordine n.' + ordersCart.numorder, + date_payment: new Date(), + price: ordersCart.totalPrice, + internal, + } + + const CashModel = new Cash(casjobj); return await CashModel.save(Cash) .then((ris) => { diff --git a/src/server/models/orderscart.js b/src/server/models/orderscart.js index 3b9fbcf..167d3e5 100755 --- a/src/server/models/orderscart.js +++ b/src/server/models/orderscart.js @@ -10,6 +10,7 @@ const Storehouse = require('../models/storehouse'); const Provider = require('../models/provider'); const Gasordine = require('../models/gasordine'); const Product = require('../models/product'); +const Cash = require('../models/cash'); const ProductInfo = require('../models/productInfo'); const tools = require('../tools/general'); @@ -469,12 +470,25 @@ module.exports.createOrdersCart = async function (newOrdersCart) { return await newOrdersCart.save() } -module.exports.updateStockQtaDalMagazzinoOrdineConfermato = async function (idorderscart) { + +module.exports.addOrderToCash = async function (idorderscart, userIdStore, req) { try { const myorderscart = await OrdersCart.findOne({ _id: idorderscart }).populate('items.order').lean(); + const mycash = await Cash.createMovementCashByOrdersCart(myorderscart, userIdStore, req); + + } catch (e) { + console.error('Err', e); + } +} + +module.exports.updateStockQtaDalMagazzinoOrdineConfermato = 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; @@ -633,8 +647,9 @@ module.exports.updateStockQtaPerCancellazioneOrdine = async function (idordersca } -module.exports.updateCmd = async function (ordersCart, status, value) { +module.exports.updateCmd = async function (ordersCart, status, value, req, options) { + const userIdStore = options.userIdStore ?? null; let myOrderCart = await OrdersCart.findOne({ _id: ordersCart._id }) .populate('items.order').lean(); @@ -659,6 +674,10 @@ module.exports.updateCmd = async function (ordersCart, status, value) { } else if (status === shared_consts.OrderStatus.PAYED) { + if (value) { + await OrdersCart.addOrderToCash(id, userIdStore, req); + } + ris = await OrdersCart.setPagatoById(value, myOrderCart); } else if (status === shared_consts.OrderStatus.DELIVERED) { // Consegnato diff --git a/src/server/models/storehouse.js b/src/server/models/storehouse.js index ac838ed..68835a1 100755 --- a/src/server/models/storehouse.js +++ b/src/server/models/storehouse.js @@ -18,6 +18,11 @@ const storehouseSchema = new Schema({ }, name: { type: String, + required: true, + }, + username: { + type: String, + required: true, }, description: { type: String, diff --git a/src/server/router/cart_router.js b/src/server/router/cart_router.js index e4f7558..27dafa8 100755 --- a/src/server/router/cart_router.js +++ b/src/server/router/cart_router.js @@ -97,7 +97,7 @@ router.post('/:userId', authenticate, async function (req, res, next) { myord = arrord ? arrord[0] : null; } } else { - return res.send({ code: server_constants.RIS_CODE_ERR, cart: null, myord: null, msgerr: 'Non è possibile acquistare nello stesso ordine, su negozi differenti!' }); + return res.send({ code: server_constants.RIS_CODE_ERR, cart: null, myord: null, msgerr: 'Non è possibile acquistare nello stesso ordine, su negozi differenti!' }); } } else { await newCart.updatetotals(); @@ -239,6 +239,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res, const user = req.user; let status = req.body.status; let note = req.body.note; + let options = req.body.options; try { let mycart = await Cart.findOne({ _id: cart_id }); @@ -290,7 +291,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res, return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 }); else { - await Order.updateStatusOrders(mycart.items, status); + await Order.updateStatusOrders(mycart.items, status); const myris = ris; // Cancella il Cart appena salvato in OrdersCart @@ -299,10 +300,10 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res, .then((ris) => { return OrdersCart.getOrdersCartByUserId(userId, idapp, numorder) - .then((orders) => { + .then(async (orders) => { if (!!orders) { - OrdersCart.updateCmd(orders[0], status, true); + await OrdersCart.updateCmd(orders[0], status, true, req, options); // Invia la email dell'Ordine sendemail.sendEmail_OrderProduct(user.lang, idapp, orders[0], user) @@ -351,6 +352,7 @@ router.post('/:userId/ordercartstatus', authenticate, async function (req, res, let order_id = req.body.order_id; const user = req.user; let status = req.body.status; + let options = req.body.options; const { User } = require('../models/user'); @@ -373,7 +375,7 @@ router.post('/:userId/ordercartstatus', authenticate, async function (req, res, if (ris) { // Aggiorna gli Stati Interni ! - orderCart = await OrdersCart.updateCmd(orderCart, status, true); + orderCart = await OrdersCart.updateCmd(orderCart, status, true, options); let ordertype = ''; diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 9c2d5e2..be6a651 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -68,6 +68,7 @@ const Storehouse = require('../models/storehouse'); const Provider = require('../models/provider'); const CatProd = require('../models/catprod'); const Gasordine = require('../models/gasordine'); +const Product = require('../models/product'); const ProductInfo = require('../models/productInfo'); const Scontistica = require('../models/scontistica'); const Department = require('../models/department'); @@ -1440,6 +1441,7 @@ function load(req, res, version) { let providers = Provider.findAllIdApp(idapp); let catprods = CatProd.findAllIdApp(idapp); let gasordines = Gasordine.findAllIdApp(idapp); + let products = Product.findAllIdApp(idapp); let productInfos = ProductInfo.findAllIdApp(idapp); let scontisticas = Scontistica.findAllIdApp(idapp); let departments = Department.findAllIdApp(idapp); @@ -1531,6 +1533,7 @@ function load(req, res, version) { providers, scontisticas, gasordines, + products, productInfos, catprods, ]).then((arrdata) => { @@ -1618,8 +1621,9 @@ function load(req, res, version) { providers: arrdata[40], scontisticas: arrdata[41], gasordines: arrdata[42], - productInfos: arrdata[43], - catprods: arrdata[44], + products: arrdata[43], + productInfos: arrdata[44], + catprods: arrdata[45], }); const prova = 1; diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index f00613f..db4b2f1 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -337,6 +337,27 @@ module.exports = { OPZ1_2: 2, }, + TYPECASH: { + NESSUNO: 0, + IN: 1, + OUT: 2, + }, + + TypeCashStr: [ + { + label: '[Nessuno]', + value: 0, + }, + { + label: 'Ingresso', + value: 1, + }, + { + label: 'Uscita', + value: 2, + }, + ], + UNITS_OF_MEASURE: { NESSUNO: 0, GRAMMI: 1,