public ProfitAndLossVM(ProfitAndLossDTO model)
 {
     Date         = model.Date;
     ProductName  = model.ProductName;
     ProductValue = model.ProductValue;
     SoldValue    = model.SoldValue;
     Profit       = model.Profit;
     NetProfit    = model.NetProfit;
 }
예제 #2
0
        public ActionResult CreateSaleInvoice(StockVm model)
        {
            //Check if requested quantity is available in the stock
            using (Db db = new Db())
            {
                var avl = db.Products.Find(model.Id);
                if (avl.Quantity < model.Quantity)
                {
                    TempData["error"] = "Expected quantity s not available in stock";
                    return(RedirectToAction("CreateSaleInvoice"));
                }
            }

            using (Db db = new Db())
            {
                //Get the product from the product_Id received
                var product = db.Products.Find(model.Id);
                //Decrease the quantity of that product
                product.Quantity -= model.Quantity;

                //Create the SaleDto model to add to database
                SaleDTO saleModel = new SaleDTO()
                {
                    //ProductName = model.Name,
                    Quantity    = model.Quantity,
                    Price       = model.Price,
                    Total       = model.Price * model.Quantity,
                    date        = DateTime.Now,
                    ProductName = product.Name
                };
                //saleModel.ProductName = product.Name;
                db.Sales.Add(saleModel);

                //create the ProfitAndLossDTO mode to add data into profit and loss table
                //Net profit of the
                ProfitAndLossDTO profitAndLossDTO = new ProfitAndLossDTO()
                {
                    Date         = saleModel.date,
                    ProductName  = saleModel.ProductName,
                    ProductValue = product.Price * saleModel.Quantity,
                    SoldValue    = saleModel.Total,
                };
                profitAndLossDTO.Profit = profitAndLossDTO.SoldValue - profitAndLossDTO.ProductValue;
                //var p = db.ProfitAndLoss.Last();
                var p = db.ProfitAndLoss.OrderByDescending(x => x.Id).FirstOrDefault();
                profitAndLossDTO.NetProfit = profitAndLossDTO.Profit + p.NetProfit;
                db.ProfitAndLoss.Add(profitAndLossDTO);

                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }