我正在开展一个商店管理项目,现在我已经迷上了几天了。 我正在尝试将销售商品添加到saleItem表,然后在product和supplyItem表中更新剩余数量和销售数量。
如果itemList中没有重复的产品,则以下代码可以正常工作,即如果产品未添加多次,但仅在添加重复产品时更新最后一个产品。
关于向IONIC SQLite发出异步请求,我需要知道什么吗?
/**
* Save saleItems, update quantity left in product table and update quantity sold and quantity left in supplyItem table
**/
addSaleItems(saleID, store){
try{
/*this.itemList is an array containing all informations required
loop through Items */
this.itemList.forEach((item) => {
/*insert save items*/
let query = "INSERT INTO SaleItem (";
query += "Name, IsActive, TheSale, TheProduct, BatchNo, UnitPrice, Total, TotalCostPrice, Profit_Loss, Quantity";
query += ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
let data = [item.TheProductName,'true',saleID,item.TheProduct,item.BatchNo,item.UnitPrice,item.Total,item.TotalCostPrice,item.Profit_Loss,item.Quantity];
this.database.noRetValQuery(query, data).then((retval) => {
let prod_q = "SELECT Quantity FROM Product WHERE ID = ?";
let prod_d = [item.TheProductID]
/* Get quantity of that particular item left in product table */
this.database.retValQuery(prod_q, prod_d).then((prodRetVal) => {
let leftPro = Number(prodRetVal[0].Quantity) - Number(item.Quantity);
let prod_data = [leftPro, item.TheProductID];
return prod_data;
})
.then((prod_data) => {
/*Update quantity left in product table*/
let prod_query = "UPDATE Product SET Quantity = ? WHERE ID = ?";
this.database.noRetValQuery(prod_query, prod_data).then((retval) => { });
})
/* Get QuantitySold and QuantityLeft of that particular item left in SupplyItem table */
let queryPro = "SELECT QuantitySold, QuantityLeft FROM SupplyItem WHERE BatchNo = ? AND TheProduct = ? AND TheStore = ? LIMIT 1";
let dataPro = [item.BatchNo, item.TheProductID, store];
this.database.retValQuery(queryPro, dataPro).then((saleRetVal) => {
let qtyS = Number(saleRetVal[0].QuantitySold) + Number(item.Quantity);
let qtyL = Number(saleRetVal[0].QuantityLeft) - Number(item.Quantity);
let sup_data = [qtyL, qtyS, item.BatchNo, item.TheProductID];
return sup_data;
}).then((sup_data) => {
/*Update Supply Items*/
let sup_query = "UPDATE SupplyItem SET QuantityLeft = ?, QuantitySold = ? WHERE BatchNo = ? AND TheProduct = ?";
this.database.noRetValQuery(sup_query, sup_data).then((retval) => { })
});
});
});
} finally {
this.dismissView();
}
}