public ActionResult <StockTE> GetStockPrice([FromForm] ProductWithCategoryIdsTE prod)
        {
            int     ProdId = _context.products.Single(x => x.ProductName == prod.ProductName && x.CategoryId == prod.CategoryId).Id;
            StockTE stock  = _context.stocks.SingleOrDefault(x => x.ProductId == ProdId && x.Size == prod.ProductSize);

            return(Ok(stock));
        }
        public ActionResult <List <ProductsInStock> > GetProdctsInStock()
        {
            List <ProductsInStock> productsInStocks = new List <ProductsInStock>();
            var products = _context.products.ToList();

            foreach (ProductTE prod in products)
            {
                ProductsInStock productsInStock = new ProductsInStock();
                productsInStock.Id           = prod.Id;
                productsInStock.ProductName  = prod.ProductName;
                productsInStock.ProductImage = prod.ImagePath;
                var stockBySize = _context.stocks.Where(x => x.ProductId == prod.Id).ToList();
                if (stockBySize.Count > 0)
                {
                    List <StockTE> stockTEs = new List <StockTE>();

                    foreach (StockTE stock in stockBySize)
                    {
                        StockTE stockTE = new StockTE();
                        stockTE.Id        = stock.Id;
                        stockTE.Quantity  = stock.Quantity;
                        stockTE.Size      = stock.Size;
                        stockTE.Cost      = stock.Cost;
                        stockTE.ProductId = stock.ProductId;

                        stockTEs.Add(stockTE);
                    }

                    productsInStock.listOfstocksBySize = stockTEs;
                    productsInStocks.Add(productsInStock);
                }
            }

            return(Ok(productsInStocks));
        }
        public ActionResult <String> SetStockPrice([FromForm] ProductWithCategoryIdsTE prod)
        {
            int     ProdId = _context.products.Single(x => x.ProductName == prod.ProductName && x.CategoryId == prod.CategoryId).Id;
            StockTE stock  = _context.stocks.SingleOrDefault(x => x.ProductId == ProdId && x.Size == prod.ProductSize);

            if (stock != null)
            {
                stock.Cost = prod.Cost;
                _context.Entry(stock).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }
            else
            {
                return(Ok("Product Not Found.. Please check!"));
            }
            return(Ok("Success Fully Done!"));
        }
        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));
        }
        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"));
        }