public ActionResult <CustwithOrder> GetBillData([FromForm] BillsPendingTE billsPending)
        {
            CustwithOrder CustWithBillAmount = new CustwithOrder();
            var           BillData           = _context.billspending.SingleOrDefault(x => x.Billnumber == billsPending.Billnumber);

            if (BillData != null)
            {
                var CustData = _context.customers.SingleOrDefault(x => x.CustomerId == BillData.Customerid.ToString());

                CustWithBillAmount.customer  = CustData;
                CustWithBillAmount.Totalcost = BillData.Pendingamount;
            }
            else
            {
                CustWithBillAmount.customer  = null;
                CustWithBillAmount.Totalcost = 0;
            }
            return(Ok(CustWithBillAmount));
        }
        public ActionResult <string> PurchaseStock(CustwithOrder custwithOrder)
        {
            foreach (CartItems item in custwithOrder.cartItems)
            {
                SalewithCustIdTE salewithCustId = new SalewithCustIdTE
                {
                    Productname  = item.productName,
                    Prodsize     = item.size,
                    Quantity     = item.Quantity,
                    Unitprice    = item.cost,
                    TotalCost    = item.Quantity * item.cost,
                    Purchasedate = DateTime.Now,
                    Productid    = item.id,
                    Custid       = custwithOrder.customer.CustomerId
                };
                _context.Add(salewithCustId);
                _context.SaveChanges();
            }

            foreach (CartItems item in custwithOrder.cartItems)
            {
                StockTE stockTE = _context.stocks.Single(x => x.ProductId == item.id && x.Size == item.size);
                stockTE.Quantity = stockTE.Quantity - item.Quantity;
                _context.Entry(stockTE).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }

            //To Calculate Bill profit
            List <Profitmodel> Datas = new List <Profitmodel>();
            int Totalprofit          = 0;

            foreach (CartItems item in custwithOrder.cartItems)
            {
                ProductProfitService profitService = new ProductProfitService(_context);
                Profitmodel          model         = new Profitmodel();
                model.Profit = profitService
                               .CalculatingProductProfitFoCurrentPruchase(item.id, item.size, item.Quantity, item.cost);
                Datas.Add(model);
            }
            foreach (var data in Datas)
            {
                Totalprofit += data.Profit;
            }



            //Generate Bill No and Passing Complete Purchased Object back to FrontEnd for PDF generation
            int  Billno;
            bool Notempty = _context.billscollections.Any();

            if (Notempty)
            {
                int MaxIdValue = _context.billscollections.Max(x => x.Id);
                int LastBillNo = _context.billscollections.Single(x => x.Id == MaxIdValue).Billnumber;
                Billno = LastBillNo + 1;
            }
            else
            {
                Billno = 801000;
            }
            CustObjwithBillNo custObjwithBillNo = new CustObjwithBillNo
            {
                Custwithorder = custwithOrder,
                Billnumber    = Billno,
                Billprofit    = Totalprofit
            };



            return(Ok(custObjwithBillNo));
        }