Hello Team,
I'm writing this loop where I want it to saves data into sales table and sales item table[that is Sales Detail table], and I want it to perform the following operation such as deducting the quantity sold from the stock table and also calculating profit made on each sales into the profit column and finally print the sales receipt.
function SaveSales() { debugger; var ObjSalesModel = { tblSales: { // Prepare the data //ObjSalesModel.Date = $("#").val(); Amount: parseFloat($('#txtTotal').val()), Discount: parseFloat($('#discountPercent').val()), //ObjSalesModel.Tax = $("#").val(); GrandTotal: parseFloat($('#grandTotal').val()), }, SalesItems: [] }; $("#tblAppendHere").find("tr:gt(0)").each(function () { debugger; var ObjectSalesItems = { StockID : parseInt($(this).find("td:eq(1)").text()), SalesID : parseInt($(this).find("td:eq(2)").text()), Qty : parseInt($(this).find("td:eq(3)").text()), Rate : parseInt($(this).find("td:eq(4)").text()), Amount : parseInt($(this).find("td:eq(5)").text()) }; ObjSalesModel.SalesItems.push(ObjectSalesItems); }); //post data to server $.ajax({ async: true, contentType: "application/json; charset=utf-8", type: "POST", dataType: "JSON", url: "/SalesEntry/SaveSales", data: JSON.stringify(ObjSalesModel), success: function (data) {
if (data.result == true) { //closeModal(); alert(data.message = "Sales process successfully!..."); } else { //closeModal(); }
return false;
} }) }
public ActionResult SaveSales(SalesModel ObjSalesModel) { try { //decimal costPrice = ObjSalesModel.CP; //decimal sellingPrice = ObjSalesModel.SP; //decimal Profit = SP - CP;
tblSalesItem ObjSalesItem = new tblSalesItem(); ObjSalesItem.StockID = ObjSalesModel.StockID; ObjSalesItem.SalesID = ObjSalesModel.SalesID; ObjSalesItem.Qty = ObjSalesModel.Qty; ObjSalesItem.Rate = ObjSalesModel.Rate; ObjSalesItem.Amount = ObjSalesModel.Amount; objASPNETPHARMACYDBEntities.tblSalesItems.Add(ObjSalesItem); objASPNETPHARMACYDBEntities.SaveChanges(); foreach (var sales in ObjSalesModel.SalesItems) { tblSale ObjSales = new tblSale(); //ObjSales.Date = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"); ObjSales.Amount = ObjSalesModel.Amount; ObjSales.Discount = ObjSalesModel.Discount; ObjSales.Tax = ObjSalesModel.Tax; ObjSales.GrandTotal = ObjSalesModel.GrandTotal; ObjSales.Profit = ObjSalesModel.Profit; ObjSales.UserID = ObjSalesModel.UserID; objASPNETPHARMACYDBEntities.tblSales.Add(ObjSales); objASPNETPHARMACYDBEntities.SaveChanges(); ObjSales.SalesID = ObjSalesModel.SalesID; var ObjtblStock = objASPNETPHARMACYDBEntities.tblStocks.Where(x => x.StockID == sales.StockID).FirstOrDefault(); ObjtblStock.Qty = (ObjtblStock.Qty) - (sales.Qty); objASPNETPHARMACYDBEntities.Entry(ObjtblStock).State = EntityState.Modified; objASPNETPHARMACYDBEntities.SaveChanges(); } return Json(new { result = true, message = "Sales fail to saved!" }, JsonRequestBehavior.AllowGet); } catch (Exception) { //throw; return Json(new { result = true, message = "Sales successfully saved!" }, JsonRequestBehavior.AllowGet); } }