Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 710
  • 1.3k
  • 62.4k

The Array is not able to send data from the client side to the server

Jan 27 2025 5:06 PM

Hello Team,
I have some few issues with my project,
The Array is not able to send the table data rom the client side to the server side.
I want the controller to calculate taxes and profit on each individual sales and save them into the sales table.
I want to print sales receipt after sales by Transaction.
the stock table contains  cost price and the selling price of the items.

 function SaveSales() {
        debugger;
        var ObjSalesModel = {
            tblSales: {
                // Prepare the data
                Amount: parseFloat($('#txtTotal').val()),
                Discount: parseFloat($('#discountPercent').val()),
                GrandTotal: parseFloat($('#grandTotal').val()),
            },
            SalesItems: []
        };
        $("#tblAppendHere").find("tr:gt(0)").each(function () {
            debugger;
            var SalesItem = {
                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(SalesItem);
        });
               
        //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.message==true){
                 toastr.success(data.message==true"Sales process successfully!..."); 
                // After successful save, open the print preview in a popup 
                var orderNumber = data.TransactionNo;
                // Assuming the order number is returned from the server 
                var printUrl = '/SalesEntry/SalesReceipt?TransactionNo=' + orderNumber;
                //window.open(printUrl, 'PrintPreview', 'width=800,height=600');
                window.location = printUrl;
                     }                
            }, error: function () {
                alert("There is some problem adding the data");
            }
        //});

            //}
        });
    }

 

  public ActionResult SaveSales(SalesModel ObjSalesModel)
        {
            if (ObjSalesModel == null || ObjSalesModel.tblSales == null)
            {
                return Json(new { result = false, message = "Invalid data received." }, JsonRequestBehavior.AllowGet);
            }

            try
            {
                decimal vat = Vat;
                decimal costPrice = CostPrice;
                decimal sellingPrice = SellingPrice;
                decimal Profit = SellingPrice - CostPrice;
                decimal Vatpercent = (vat / 100) * sellingPrice;

                tblSale ObjSales = new tblSale();                
                ObjSales.OrderDate = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
                ObjSales.TransactionNo = string.Format("{0:ddmmmyyyyhhmmss }", DateTime.Now);
                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;
                foreach (var sales in ObjSalesModel.SalesItems)
                {

                    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();
           
                    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 ex)
            {
                //throw;
                return Json(new { result = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
                //return Json(new { result = true, message = "Sales successfully saved!" }, JsonRequestBehavior.AllowGet);
            }
        }


Answers (2)