internal List <StockVm> GetStockReportList(ReportVm reportVm) { List <Sale> Sales = reportDal.GetSalesByReportVm(reportVm); List <Purchase> Purchases = reportDal.GetPurchasesByReportVm(reportVm); if (Purchases != null) { foreach (var item in Purchases) { foreach (var itemDes in item.PurchaseDetail) { StockVm stockVm = new StockVm(); if (StockReportList.Any(m => m.ItemName == itemDes.Item.Name) == false) { stockVm.ItemName = itemDes.Item.Name; stockVm.StockQuantity = common.GetItemStockById(itemDes.Item.Id); stockVm.CategoryFullPath = GetCategoryFullPathById(itemDes.Item.Id); stockVm.Price = itemDes.Item.CostPrice; StockReportList.Add(stockVm); } } } } return(StockReportList.ToList()); }
public ActionResult EditProduct(StockVm model) { if (!ModelState.IsValid) { return(Content("Invalid data received for update")); } using (Db db = new Db()) { var product = db.Products.Include("Category").Single(p => p.Id == model.Id); product.Name = model.Name; product.Description = model.Description; product.Price = model.Price; product.Quantity = model.Quantity; //get foreign key by the of the model parameter add this as a refference of product's category table. CategoryDTO cat = db.categories.Single(c => c.Id == model.CategoryId); product.Category = cat; db.SaveChanges(); } //TempData["cnf"] = "Product data successfully edited"; return(RedirectToAction("Index", "Stock")); }
public ActionResult Create(StockVm model) { //check if Model state is valid then return if (!ModelState.IsValid) { return(View(model)); //this model will af=gain dislay in the input form } //copy from view model to DTO StockDTO dto = new StockDTO() { Name = model.Name, Description = model.Description, Price = model.Price, Quantity = model.Quantity }; using (Db db = new Db()) { var cat = db.categories.Find(model.CategoryId); dto.Category = cat; db.Products.Add(dto); db.SaveChanges(); } TempData["cnf"] = "You have added a new item in stock"; return(RedirectToAction("index")); }
public ActionResult EditProduct(int id) { StockVm model; List <SelectListItem> slist = new List <SelectListItem>(); using (Db db = new Db()) { //Find the product with the id received var product = db.Products.Include("Category").Single(x => x.Id == id); if (product == null) { return(Content("Requested page dows not exist")); } model = new StockVm(product); var listFromDb = db.categories.ToList(); foreach (var item in listFromDb) { slist.Add(new SelectListItem() { Text = item.Id.ToString(), Value = item.Categoryname }); } } ViewBag.categoryDropDown = new SelectList(slist, "Text", "Value"); return(View(model)); }
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")); }