public JsonResult Save(ProductionMasterViewModel ProductionMasterViewModel, ProductionDetailsViewModel[] productionDetailsViewModels) { if (Session["strUserId"] == null) { return(Json(new { redirectUrl = Url.Action("Login", "Account"), isRedirect = true })); } else { LoadSession(); productionServices.Create(ProductionMasterViewModel, productionDetailsViewModels, intStoreId); return(Json(new { success = true })); } }
public void Create(ProductionMasterViewModel ProductionMasterViewModel, ProductionDetailsViewModel[] productionDetailsViewModels, int?intStoreId) { int?StorId = null; int?SubStorId = null; int?SubSubStorId = null; int?SubSubSubStorId = null; int?SubSubSubSubStorId = null; if (intStoreId >= 1000 && intStoreId < 2000) { StorId = intStoreId; } else if (intStoreId >= 2000 && intStoreId < 3000) { SubStorId = intStoreId; } else if (intStoreId >= 2000 && intStoreId < 3000) { SubSubStorId = intStoreId; } else if (intStoreId >= 2000 && intStoreId < 3000) { SubSubSubStorId = intStoreId; } else if (intStoreId >= 2000 && intStoreId < 3000) { SubSubSubSubStorId = intStoreId; } ProductionMaster Producionheader = new ProductionMaster(); Producionheader.ProductId = ProductionMasterViewModel.ProductId; Producionheader.ProductionQuantity = ProductionMasterViewModel.ProductionQuantity; Producionheader.ProductPrice = ProductionMasterViewModel.ProductPrice; Producionheader.Productiondate = DateTime.Now; Producionheader.StoreId = StorId; Producionheader.SubStoreId = SubStorId; Producionheader.SubSubStoreId = SubSubStorId; Producionheader.SubSubSubStoreId = SubSubSubStorId; Producionheader.SubSubSubSubStoreId = SubSubSubSubStorId; unitOfWork.ProductionMasterRepository.Insert(Producionheader); unitOfWork.Save(); var Productionhdid = Producionheader.ProductionMasterId; foreach (var items in productionDetailsViewModels) { var productionDetails = new ProductionDetails { ProductionMasterId = Productionhdid, ItemId = items.ItemId, ItemQuantity = items.ItemQuantity, ItemCost = items.ItemCost, SubTotal = items.SubTotal }; unitOfWork.ProductionDetailsRepository.Insert(productionDetails); unitOfWork.Save(); var avalableqty = unitOfWork.InventoryMasterRepository.Get().Join(unitOfWork.InventoryDetailRepository.Get(), (invd => invd.Inv_HD_ID), (invh => invh.Inv_HD_ID), ((invd, invh) => new { Inventorymaster = invd, inventorydetails = invh })).Where(k => k.Inventorymaster.StoreId == StorId || k.Inventorymaster.SubStoreId == SubStorId || k.Inventorymaster.SubSubStoreId == SubSubStorId || k.Inventorymaster.SubSubSubStoreId == SubSubSubStorId || k.Inventorymaster.SubSubSubSubStoreId == SubSubSubSubStorId).Where(i => i.inventorydetails.ItemId == items.ItemId).Select(q => q.inventorydetails.AvailableQty).Sum(); var itemqty = Convert.ToDecimal(items.ItemQuantity); var invdetails = (from p in unitOfWork.InventoryDetailRepository.Get() join m in unitOfWork.InventoryMasterRepository.Get() on p.Inv_HD_ID equals m.Inv_HD_ID where p.AvailableQty > 0 && p.ItemId == items.ItemId && (m.StoreId == StorId || m.SubStoreId == SubStorId || m.SubSubStoreId == SubSubStorId || m.SubSubSubStoreId == SubSubSubStorId || m.SubSubSubSubStoreId == SubSubSubSubStorId) select new { InvDetailsId = p.Inv_Details_ID, }).ToList(); foreach (var invid in invdetails) { if (itemqty > 0) { var invdetailidqty = unitOfWork.InventoryDetailRepository.Get().Where(m => m.Inv_Details_ID == invid.InvDetailsId).Select(l => l.AvailableQty).SingleOrDefault(); if (itemqty <= invdetailidqty) { var result = unitOfWork.InventoryDetailRepository.Get().SingleOrDefault(b => b.Inv_Details_ID == invid.InvDetailsId); if (result != null) { result.AvailableQty = invdetailidqty - itemqty; unitOfWork.Save(); itemqty = Convert.ToDecimal(items.ItemQuantity) - itemqty; } } else { var result = unitOfWork.InventoryDetailRepository.Get().SingleOrDefault(b => b.Inv_Details_ID == invid.InvDetailsId); if (result != null) { result.AvailableQty = result.AvailableQty - invdetailidqty; unitOfWork.Save(); itemqty = itemqty - invdetailidqty; } } } } } var InventoryMaster = new InventoryMaster { ProductionMasterId = Productionhdid, StoreId = ProductionMasterViewModel.StoreId, CreateDate = DateTime.Now }; unitOfWork.InventoryMasterRepository.Insert(InventoryMaster); unitOfWork.Save(); var invId = InventoryMaster.Inv_HD_ID; InventoryDetail inventoryDetail = new InventoryDetail(); inventoryDetail.Inv_HD_ID = invId; inventoryDetail.ProductId = ProductionMasterViewModel.ProductId; inventoryDetail.TransactionQty = ProductionMasterViewModel.ProductionQuantity; inventoryDetail.AvailableQty = ProductionMasterViewModel.ProductionQuantity; inventoryDetail.PO_Price = ProductionMasterViewModel.ProductPrice / ProductionMasterViewModel.ProductionQuantity; unitOfWork.InventoryDetailRepository.Insert(inventoryDetail); unitOfWork.Save(); //foreach (var items in productionDetailsViewModels) //{ // //InventoryDetail lst = new InventoryDetail(); // var ProductionQty = items.ItemQuantity; // var inventoryQty = unitOfWork.InventoryDetailRepository.Get().Where(m => m.ItemId == items.ItemId).Select(k => k.TransactionQty).Sum(); // var updateqty = inventoryQty - ProductionQty; // if (inventoryQty > ProductionQty) // { // //var jj = (from p in unitOfWork.InventoryDetailRepository.Get() join m in unitOfWork.InventoryMasterRepository.Get() on p.Inv_HD_ID equals m.Inv_HD_ID where p.ItemId == items.ItemId && m.StoreId == ProductionMasterViewModel.StoreId select p).SingleOrDefault(); // var kk = unitOfWork.InventoryDetailRepository.Get().SingleOrDefault(b => b.ItemId == items.ItemId); // //foreach (var f in jj) // //{ // // lst.Inv_Details_ID = f.Inv_Details_ID; // // lst.Inv_HD_ID = f.Inv_HD_ID; // // lst.ItemId = f.ItemId; // // lst.UnitId = f.UnitId; // // lst.MethodId = f.MethodId; // // lst.DateOfExpired = f.DateOfExpired; // // lst.DateOfNextMaintainance = f.DateOfNextMaintainance; // // lst.TransactionQty = inventoryQty - ProductionQty; // //} // kk.AvailableQty = updateqty; // unitOfWork.Save(); // } //} }