`updateQuantity(event) {
console.log(“onchange – entered updateQuantity event handler——->”)
this.shippingAddressId = event.target.dataset.id;
this.posItemId = event.target.dataset.pos;
this.quantityUpdateValue = event.target.value;
console.log("onchange - this.quantityUpdateValue------->",this.quantityUpdateValue);
this.orderId = event.target.dataset.order;
var posItemsListWithCart = JSON.parse(JSON.stringify(this.posItemsListInCart))
// console.log('posItemsListWithCart--->',JSON.stringify(posItemsListWithCart))
for (var i = 0; i < posItemsListWithCart.length; i++) {
var individualQty = 0;
// Author: Shilpa and Kavya
// Date: 19-12-2023
// To get the Logical_Inventory__c from POS_Item_Facilities__r
for(var z=0;z<posItemsListWithCart[i].posItem.POS_Item_Facilities__r.length;z++){
var currentAvailableStock = posItemsListWithCart[i].posItem.POS_Item_Facilities__r[z].Logical_Inventory__c;
}
// Changes done upto here
for (var j = 0; j < posItemsListWithCart[i].selectedAddressWithQtyDetails.length; j++) {
if (this.shippingAddressId == posItemsListWithCart[i].selectedAddressWithQtyDetails[j].selectedAddress.Id) {
let quantityTobeUpdated = event.target.value;
this.quantityUpdateValue = quantityTobeUpdated == '' ? 0 : quantityTobeUpdated;
console.log("onchange - checking if it is empty or not ----------> ",this.quantityUpdateValue)
posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity = this.quantityUpdateValue;
console.log("onchange - to update the quantity in the data structure ----------> ",posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity)
}
// after getting the qunity to calculate the totla quntity
var quantity = posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity == '' ? 0 : posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity;
console.log("onchange - to ensure that the quantity variable always contains a number------>",quantity);
individualQty = parseInt(individualQty) + parseInt(quantity);
console.log("onchange - to sum up the individual quantities for each selected address within a POS item------>",individualQty);
posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity = parseInt(quantity);
console.log("onchange - assigning the parser integer value --------->",posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity)
}
posItemsListWithCart[i].totalQuantityFromAllAddresses = individualQty;
console.log('onchange - asssigning parsed integer value to the totalQuantityFromAllAddresses container--------->',posItemsListWithCart[i].totalQuantityFromAllAddresses)
// to check if the total quntity is less than current available stock
if (posItemsListWithCart[i].totalQuantityFromAllAddresses <= currentAvailableStock) {
posItemsListWithCart[i].isAvailableQuantityExceeded = false;
} else {
posItemsListWithCart[i].isAvailableQuantityExceeded = true;
this.disablePreview = true;
}
}
this.posItemsListInCart = posItemsListWithCart;
console.log("onchange - this.posItemsListInCart ---------->",JSON.stringify(this.posItemsListInCart))
// to send the updated quntity ot the apex method "updateQuantityForSelectedPosItem" with the parameter positemid,shipping address id and order id and quntity added.
updateQuantityForSelectedPosItem({
posItemId: this.posItemId,
shippingAddressId: this.shippingAddressId,
orderId: this.orderId,
quantity: this.quantityUpdateValue
})
.then(result => {
// once the quntity is sent to the apex method the result should be sent as 'done'
let resultObtained = result;
// console.log("resultObtained ---------> ",resultObtained)
return refreshApex(this.cartItemsToBeDisplayed);
})
.catch(error => {
this.error = error;
//console.log('ERROR', this.error);
});
}`
`get cartItemDetails() {
// To get the pos items in cart to display
if (this.cartItemsToBeDisplayed.data) {
console.log(‘onload START – inside if’)
console.log(“onload – after updating this.cartItemsToBeDisplayed.data”,JSON.stringify(this.cartItemsToBeDisplayed.data))
// console.log(‘cartItemsToBeDisplayed–>’, JSON.stringify(this.cartItemsToBeDisplayed.data))
this.posItemsListInCart = this.cartItemsToBeDisplayed.data;
// console.log("onload - hold the updated data in the this.posItemsListInCart continer ",JSON.stringify(this.posItemsListInCart))
//To check if the given pos item has the data or not
if (this.posItemsListInCart.length > 0) {
// console.log('onload - inside if 1 ')
// console.log("onload - check the length -------->",this.posItemsListInCart.length)
this.isAddressAvailableForDisplay = true;
// console.log("onload - this.isAddressAvailableForDisplay is true -------->",this.isAddressAvailableForDisplay)
} else {
this.isAddressAvailableForDisplay = false;
// console.log("onload - this.isAddressAvailableForDisplay is false -------->",this.isAddressAvailableForDisplay)
}
// to assign the total quanity to zero by default
this.totalQuantity = 0;
// if total quntity is 0 then disable the preview button
this.disablePreview = true;
// local variable to convert the pos items in cart to json parse so that anyone can edit the incoming items
var posItemsListWithCart = JSON.parse(JSON.stringify(this.posItemsListInCart));
// console.log("onload - local variable to convert the pos items in cart to json parse posItemsListWithCart -------->",JSON.stringify(this.isAddressAvailableForDisplay))
var isAvailableQuantityExceeded = false;
//to check if the entred quantity has exceed initally
for (var i = 0; i < posItemsListWithCart.length; i++) {
// console.log('inside for loop--------->')
this.totalQuantity += this.posItemsListInCart[i].totalQuantityFromAllAddresses;
// console.log('onload - increments this.totalQuantity--------->',this.totalQuantity)
var individualQty = 0;
// Author: Shilpa and Kavya
// Date: 19-12-2023
// To get the Logical_Inventory__c from POS_Item_Facilities__r
for(var z=0;z<posItemsListWithCart[i].posItem.POS_Item_Facilities__r.length;z++){
// console.log('inside for loop 1')
//local variable to store thhe logical inventory stock
var currentAvailableStock = posItemsListWithCart[i].posItem.POS_Item_Facilities__r[z].Logical_Inventory__c;
// console.log("onload - currentAvailableStock",currentAvailableStock)
}
// Changes done upto here
//console.log('currentAvailableStock',currentAvailableStock)
// to check if any item in cart has a quanity entred greater than available stock
for (var j = 0; j < posItemsListWithCart[i].selectedAddressWithQtyDetails.length; j++) {
// console.log('onload - inside for loop 2')
var quantity = posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity == '' ? 0 : posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity;
// console.log('onload - quantity -------->',quantity)
individualQty = parseInt(individualQty) + parseInt(quantity);
// console.log('onload - individualQty -------->',individualQty)
posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity = parseInt(quantity);
// console.log('onload - posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity -------->',posItemsListWithCart[i].selectedAddressWithQtyDetails[j].quantity)
}
posItemsListWithCart[i].totalQuantityFromAllAddresses = individualQty;
// console.log('onload - posItemsListWithCart[i].totalQuantityFromAllAddresses -------->',posItemsListWithCart[i].totalQuantityFromAllAddresses)
//if exceded then disable the preview button
//to display error message
if (posItemsListWithCart[i].totalQuantityFromAllAddresses <= currentAvailableStock) {
posItemsListWithCart[i].isAvailableQuantityExceeded = false;
} else {
posItemsListWithCart[i].isAvailableQuantityExceeded = true;
isAvailableQuantityExceeded = true;
}
}
this.posItemsListInCart = posItemsListWithCart;
// console.log('onload - this.posItemsListInCart -------->',JSON.stringify(this.posItemsListInCart))
this.cartItemsToBeDisplayed.data = posItemsListWithCart;
// console.log('onload END - this.cartItemsToBeDisplayed.data -------->',JSON.stringify(this.cartItemsToBeDisplayed.data))
// if quntity is > 0 then enable the repview button
if (this.totalQuantity > 0) {
this.disablePreview = false;
}
// if quntity is = 0 then the repview button is disabled with the help of the variable "disablePreview"
if (this.totalQuantity == 0 || isAvailableQuantityExceeded) {
this.disablePreview = true;
}
if (this.posItemsListInCart.length > 0) {
if (this.posItemsListInCart[0].orderId) {
this.orderId = this.posItemsListInCart[0].orderId;
}
}
/**
refreshApex(this.cartItemsToBeDisplayed);
}`
Ananya SR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.