const shared_consts = require('../tools/shared_nodejs'); const cartModel = require('../models/cart') const { ObjectID } = require('mongodb'); const Order = require('../models/order'); class Cart { constructor(order, arrorders) { this.modify_at = new Date(); this.items = {}; if (!!order) { this.initializeFromOrder(order); } else if (!!arrorders) { for (const ord of arrorders) { this.items.push(ord) } } this.updatetotals(); } initializeFromOrder(order) { this.idapp = order.idapp || 0; this.userId = order.userId || ""; this.items[order._id] = order; } static constructByCart(cart) { try { const mynewcart = new Cart(null); mynewcart.idapp = cart.idapp || 0; mynewcart.items = cart.items; mynewcart.department = cart.department; mynewcart.userId = cart.userId || ""; mynewcart.modify_at = new Date(); return mynewcart; } catch (e) { console.log('Error', e); return null; } } isAvailableByOrder(order) { if (order && order.product) { return (order.product.quantityAvailable > 0) } return false; } isInPreorderByOrder(order) { if (order && order.product) { return (order.product.bookableAvailableQty > 0) } return false; } async addqty(itemorder) { const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id) if (!!myitem) { let step = 1; if (this.isAvailableByOrder(myitem.order)) { if (myitem.order.quantity === 0) step = myitem.order.product.minBuyQty | 1 else if (myitem.order.quantity >= 10) step = 2 else if (myitem.order.quantity >= 20) step = 5 myitem.order.quantity += step; } else { if (myitem.order.quantitypreordered === 0) step = myitem.order.product.minBuyQty | 1 else if (myitem.order.quantitypreordered >= 10) step = 2 else if (myitem.order.quantitypreordered >= 20) step = 5 myitem.order.quantitypreordered += step; } this.updatetotals(); await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false }); return myitem.order; } } async subqty(itemorder) { try { const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id) if (!!myitem) { if (myitem.order.quantitypreordered > 0) { myitem.order.quantitypreordered--; } else { if (myitem.order.quantity > 0) { myitem.order.quantity--; } } this.updatetotals(); await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false }); return myitem.order; } } catch (e) { console.error('Err: ', e); } } addItem(itemorder) { // this.items.push(itemorder); let ind = this.items.length; this.items[ind] = {}; this.items[ind].order = itemorder; this.items[ind].order = Order.updateTotals(this.items[ind].order); this.updatetotals(); return ind; } removeItem(orderId) { // this.items.push(itemorder); this.items = this.items.filter(item => item.order._id.toString() !== orderId.toString()); this.updatetotals(); } generateModel() { try { let newCart = new cartModel({ idapp: this.idapp, items: this.generateArray(), totalQty: this.totalQty, totalPrice: this.totalPrice, userId: this.userId, department: this.department, note: this.note, modify_at: this.modify_at }) return newCart } catch (e) { console.error('Err', e); } return null; } updatetotals() { try { this.totalQty = 0; this.totalPrice = 0; for (const rec in this.items) { let mypricecalc = 0; let order = this.items[rec].order; if (!order) { order = this.items[rec]; } order.TotalPriceProduct = 0; this.totalQty += order.quantity + order.quantitypreordered; // Calcolo Sconto let sconti_da_applicare = []; if (order.scontisticas) { let qtadascontare = order.quantity + order.quantitypreordered let qtanonscontata = 0 while (qtadascontare > 0) { let scontoapplicato = null for (const sconto of order.scontisticas.filter((rec) => !rec.cumulativo)) { if (qtadascontare >= sconto.qta) { scontoapplicato = sconto scontoapplicato.qtadascontare = sconto.qta } } if (scontoapplicato && scontoapplicato.qtadascontare > 0) { sconti_da_applicare.push(scontoapplicato) qtadascontare -= scontoapplicato.qtadascontare } else { qtanonscontata = qtadascontare qtadascontare = 0 } } /*for (const sconto of order.scontisticas.filter((rec) => rec.cumulativo)) { if ((sconto.qta % order.quantity) === 0) { sconti_da_applicare.push(sconto) } }*/ if (sconti_da_applicare.length > 0) { for (const sconto of sconti_da_applicare) { if (sconto.perc_sconto > 0) { mypricecalc += (sconto.qtadascontare * order.price) * (1 - (sconto.perc_sconto / 100)) } else { mypricecalc += sconto.price } } } if (qtanonscontata > 0) { mypricecalc += order.price * qtanonscontata; } } else { mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered); } order.TotalPriceProduct += mypricecalc; this.totalPrice += order.TotalPriceProduct; } this.totalPrice = parseFloat(this.totalPrice.toFixed(2)) } catch (e) { console.error('Err: ', e); } } generateArray() { let arr = []; for (let id in this.items) { arr.push(this.items[id]) } return arr; } } module.exports = Cart