Aggiornamento modifiche preOrdini

This commit is contained in:
Surya Paolo
2023-12-20 21:52:17 +01:00
parent 56b5bac5f0
commit 0e1fc359a0
11 changed files with 290 additions and 61 deletions

View File

@@ -34,24 +34,49 @@ class Cart {
}
}
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) {
myitem.order.quantity++;
if (this.isAvailableByOrder(myitem.order)) {
myitem.order.quantity++;
} else {
myitem.order.quantitypreordered++;
}
this.updatetotals();
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order.quantity;
return myitem.order;
}
}
async subqty(itemorder) {
try {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem && myitem.order.quantity > 0) {
myitem.order.quantity--;
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.quantity;
return myitem.order;
}
} catch (e) {
console.error('Err: ', e);
@@ -76,17 +101,22 @@ class Cart {
}
generateModel() {
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
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() {
@@ -95,19 +125,19 @@ class Cart {
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;
this.totalQty += order.quantity + order.quantitypreordered;
// Calcolo Sconto
let sconti_da_applicare = [];
if (order.scontisticas) {
let qtadascontare = order.quantity
let qtadascontare = order.quantity + order.quantitypreordered
let qtanonscontata = 0
while (qtadascontare > 0) {
@@ -145,9 +175,9 @@ class Cart {
if (qtanonscontata > 0) {
mypricecalc += order.price * qtanonscontata;
}
} else {
mypricecalc = order.price * order.quantity;
mypricecalc = (order.price * order.quantity) + (order.price * order.quantitypreordered);
}
order.TotalPriceProduct += mypricecalc;
this.totalPrice += order.TotalPriceProduct;