public ActionResult <int> GetGlobalCash()
        {
            int  Cashposition    = 0;
            bool isDataAvailable = _context.cashposition.Any();

            if (isDataAvailable)
            {
                int Id = _context.cashposition.Max(x => x.Id);
                var CashPositionData = _context.cashposition.Single(x => x.Id == Id);
                Cashposition = CashPositionData.Globalcash;
            }
            else
            {
                //It will Run Just One Time in Entire Application
                CashPositionTE cashPosition = new CashPositionTE()
                {
                    Globalcash = 0
                };
                _context.Add(cashPosition);
                _context.SaveChanges();
                Cashposition = 0;
            }
            return(Ok(Cashposition));
        }
        public ActionResult <string> StorePayment([FromForm] CustomerTxHistoryTE bills)
        {
            string result = "";

            BillsPendingTE BillData = _context.billspending
                                      .Single(x => x.Billnumber == bills.Billnumber && x.Customerid == bills.Customerid);

            if (BillData != null)
            {
                if (bills.Paymentmode == "ACCOUNT")
                {
                    var CustAccountData = _context.customeraccounts.Single(x => Convert.ToInt32(x.Customerid) == bills.Customerid);
                    if (CustAccountData != null)
                    {
                        CustAccountData.Availableamount      -= bills.Paidamount;
                        _context.Entry(CustAccountData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        _context.SaveChanges();
                    }
                    else
                    {
                        result = "Customer Account Data Not Found - Server Side";
                    }
                }
                else if (bills.Paymentmode == "CASH")
                {
                    bool isDataAvailable = _context.cashposition.Any();
                    if (isDataAvailable)
                    {
                        int Id = _context.cashposition.Max(x => x.Id);
                        var CashPositionData = _context.cashposition.Single(x => x.Id == Id);
                        CashPositionData.Globalcash           += bills.Paidamount;
                        _context.Entry(CashPositionData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        _context.SaveChanges();
                    }
                    else
                    {
                        CashPositionTE cashPosition = new CashPositionTE()
                        {
                            Globalcash = bills.Paidamount
                        };
                        _context.Add(cashPosition);
                        _context.SaveChanges();
                    }
                }
                BillData.Pendingamount = BillData.Pendingamount - bills.Paidamount;

                if (BillData.Pendingamount == 0)
                {
                    BillData.Iscompleted = true;
                }

                _context.Entry(BillData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();

                CustomerTxHistoryTE customer = new CustomerTxHistoryTE()
                {
                    Billnumber  = bills.Billnumber,
                    Paidamount  = bills.Paidamount,
                    Paiddate    = DateTime.Now.Date,
                    Paymentmode = bills.Paymentmode,
                    Customerid  = bills.Customerid
                };
                _context.Add(customer);
                _context.SaveChanges();
                result = "Done!";
            }
            else
            {
                result = "Please check your Bill Number!";
            }
            return(Ok(result));
        }
        public ActionResult <string> ProductRegistration([FromForm] ProductEntryData formData)
        {
            int PurchaseCost = 0;

            var ProductObj = _context.products.Single(x => x.ProductName == formData.Name);

            ProdAddHistoryTE prodAddHistory = new ProdAddHistoryTE();

            prodAddHistory.Quantity  = formData.Quantity;
            prodAddHistory.Cost      = formData.Cost;
            prodAddHistory.Size      = formData.Size;
            prodAddHistory.Totalcost = formData.Quantity * formData.Cost;
            prodAddHistory.Date      = DateTime.Now;
            prodAddHistory.ProductId = ProductObj.Id;

            _context.Add(prodAddHistory);
            _context.SaveChanges();

            //Make Purchase impacting the Global Cash
            PurchaseCost = formData.Quantity * formData.Cost;
            bool isDataAvailable = _context.cashposition.Any();

            if (isDataAvailable)
            {
                int Id = _context.cashposition.Max(x => x.Id);
                var CashPositionData = _context.cashposition.Single(x => x.Id == Id);
                CashPositionData.Globalcash           -= PurchaseCost;
                _context.Entry(CashPositionData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }
            else
            {
                CashPositionTE cashPosition = new CashPositionTE()
                {
                    Globalcash = -PurchaseCost
                };
                _context.Add(cashPosition);
                _context.SaveChanges();
            }

            //Make Effect in Stock Table once Product Registered

            var IsAnyStock = _context.stocks.Any();

            if (IsAnyStock)
            {
                var StockObj = _context.stocks.SingleOrDefault(x => x.ProductId == prodAddHistory.ProductId && x.Size == prodAddHistory.Size);
                if (StockObj != null)
                {
                    StockObj.Cost      = formData.Cost + Convert.ToInt32(0.15 * formData.Cost);
                    StockObj.Quantity += formData.Quantity;
                    _context.Entry(StockObj).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    _context.SaveChanges();
                }
                else
                {
                    StockTE stock = new StockTE()
                    {
                        Quantity  = formData.Quantity,
                        Size      = formData.Size,
                        Cost      = formData.Cost,
                        ProductId = prodAddHistory.ProductId
                    };
                    _context.Add(stock);
                    _context.SaveChanges();
                }
            }
            else
            {
                //One Time Execution to Create Stock Table with data
                StockTE stock = new StockTE()
                {
                    Quantity  = formData.Quantity,
                    Size      = formData.Size,
                    Cost      = formData.Cost,
                    ProductId = prodAddHistory.ProductId
                };
                _context.Add(stock);
                _context.SaveChanges();
            }
            return(Ok("Data Posted Successfully"));
        }