public void WarehouseInputRegister(WareHouseIO wareHouseIO, List <WareHouseIODetail> warehouseIODetails) { using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { wareHouseIO.SetInfo(false); this.Context.WareHouseIO.Add(wareHouseIO); this.Context.SaveChanges(); var medicineDictionary = new Dictionary <int, int>(); foreach (var wareHouseIoDetail in warehouseIODetails) { wareHouseIoDetail.WareHouseIOId = wareHouseIO.Id; wareHouseIoDetail.SetInfo(false); this.Context.WareHouseIODetail.Add(wareHouseIoDetail); if (medicineDictionary.ContainsKey(wareHouseIoDetail.MedicineId)) { medicineDictionary[wareHouseIoDetail.MedicineId] += wareHouseIoDetail.Qty; continue; } medicineDictionary.Add(wareHouseIoDetail.MedicineId, wareHouseIoDetail.Qty); } this.Context.SaveChanges(); foreach (var item in medicineDictionary.Keys) { var warehouse = this.Context.WareHouses.FirstOrDefault(x => x.MedicineId == item && x.ClinicId == AppContext.CurrentClinic.Id); if (warehouse == null) { warehouse = new WareHouse() { ClinicId = AppContext.CurrentClinic.Id, MedicineId = item, Volumn = medicineDictionary[item], MinAllowed = 0 }; this.Context.WareHouses.Add(warehouse); this.Context.SaveChanges(); } else { warehouse.Volumn += medicineDictionary[item]; } foreach (var warehouseIoDetail in warehouseIODetails) { if (warehouseIoDetail.MedicineId != item) { continue; } var warehouseDetail = new WareHouseDetail { WareHouseId = warehouse.Id, MedicineId = warehouseIoDetail.MedicineId, Unit = warehouseIoDetail.Unit, UnitPrice = warehouseIoDetail.UnitPrice ?? 0, LotNo = warehouseIoDetail.LotNo, ExpiredDate = warehouseIoDetail.ExpireDate, WareHouseIODetailId = warehouseIoDetail.Id, OriginalVolumn = warehouseIoDetail.Qty, CurrentVolumn = warehouseIoDetail.Qty }; warehouseDetail.SetInfo(false); this.Context.WareHouseDetails.Add(warehouseDetail); } } this.Context.SaveChanges(); scope.Complete(); } }
public void WarehouseOutputRegister(WareHouseIO wareHouseIO, List <WareHouseIODetail> warehouseIODetails) { using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { wareHouseIO.SetInfo(false); this.Context.WareHouseIO.Add(wareHouseIO); this.Context.SaveChanges(); var medicineDictionary = new Dictionary <int, int>(); foreach (var wareHouseIoDetail in warehouseIODetails) { wareHouseIoDetail.WareHouseIOId = wareHouseIO.Id; wareHouseIoDetail.SetInfo(false); this.Context.WareHouseIODetail.Add(wareHouseIoDetail); if (medicineDictionary.ContainsKey(wareHouseIoDetail.MedicineId)) { medicineDictionary[wareHouseIoDetail.MedicineId] += wareHouseIoDetail.Qty; continue; } medicineDictionary.Add(wareHouseIoDetail.MedicineId, wareHouseIoDetail.Qty); } this.Context.SaveChanges(); // var allocatedList = new List<WareHouseExportAllocate>(); foreach (var item in medicineDictionary.Keys) { var warehouse = this.Context.WareHouses.FirstOrDefault(x => x.MedicineId == item && x.ClinicId == AppContext.CurrentClinic.Id); var validWarehouseIODetail = this.Context.VWarehouseDetailFull.Where(x => x.MedicineId == item && x.ClinicId == AppContext.CurrentClinic.Id && (x.Date == null || x.Date <= wareHouseIO.Date) && x.CurrentVolumn > 0).Select(x => x.Id).ToList <int>(); var warehouseDetailList = this.Context.WareHouseDetails.Where(x => validWarehouseIODetail.Contains(x.Id)).ToList(); var warehouseOutputDetail = warehouseIODetails.Where(x => x.MedicineId == item).ToList(); foreach (var outputItem in warehouseOutputDetail) { var qty = outputItem.Qty; foreach (var detail in warehouseDetailList) { if (!detail.LotNo.Equals(outputItem.LotNo)) { continue; } var allotcateItem = new WareHouseExportAllocate { WareHouseDetailId = detail.Id, WareHouseIODetailId = outputItem.Id, Unit = outputItem.Unit, Volumn = detail.CurrentVolumn > qty ? qty : detail.CurrentVolumn, Version = 0 }; // allocatedList.Add(allotcateItem); this.Context.WareHouseExportAllocates.Add(allotcateItem); qty -= allotcateItem.Volumn; detail.CurrentVolumn -= allotcateItem.Volumn; detail.SetInfo(true); warehouse.Volumn -= allotcateItem.Volumn; warehouse.SetInfo(true); if (qty == 0) { break; } } if (qty != 0) { throw new Exception("Số lượng thuốc đã bị lệch trong lúc thay đổi, hãy chọn lại số lượng cho khớp."); } } } this.Context.SaveChanges(); scope.Complete(); } }