public async Task <int> Update(int id, InputInspectionMaterialViewModel viewModel) { int result = 0; var dbModel = await _repository.ReadByIdAsync(id); var model = new DyeingPrintingAreaInputModel(viewModel.Date, viewModel.Area, viewModel.Shift, viewModel.BonNo, viewModel.Group, viewModel.InspectionMaterialProductionOrders.Select(s => new DyeingPrintingAreaInputProductionOrderModel(viewModel.Area, s.ProductionOrder.Id, s.ProductionOrder.No, s.ProductionOrder.Type, s.ProductionOrder.OrderQuantity, s.PackingInstruction, s.CartNo, s.Buyer, s.Construction, s.Unit, s.Color, s.Motif, s.UomUnit, s.InputQuantity, s.InputQuantity, s.HasOutputDocument, s.BuyerId, 0, s.Material.Id, s.Material.Name, s.MaterialConstruction.Id, s.MaterialConstruction.Name, s.MaterialWidth, s.ProcessType.Id, s.ProcessType.Name, s.YarnMaterial.Id, s.YarnMaterial.Name, s.InputQuantity, s.FinishWidth, viewModel.Date, s.MaterialOrigin) // s.Material.Name, s.MaterialConstruction.Id, s.MaterialConstruction.Name, s.MaterialWidth, s.ProcessType.Id, s.ProcessType.Name, s.YarnMaterial.Id, s.YarnMaterial.Name, s.InputQuantity, viewModel.Date) { Id = s.Id }).ToList()); Dictionary <int, double> dictBalance = new Dictionary <int, double>(); foreach (var item in dbModel.DyeingPrintingAreaInputProductionOrders) { var lclModel = model.DyeingPrintingAreaInputProductionOrders.FirstOrDefault(s => s.Id == item.Id); if (lclModel != null) { var diffBalance = lclModel.InputQuantity - item.InputQuantity; dictBalance.Add(lclModel.Id, diffBalance); } } var deletedData = dbModel.DyeingPrintingAreaInputProductionOrders.Where(s => !s.HasOutputDocument && !viewModel.InspectionMaterialProductionOrders.Any(d => d.Id == s.Id)).ToList(); if (deletedData.Any(item => !item.HasOutputDocument && item.BalanceRemains != item.Balance)) { throw new Exception("Ada SPP yang Sudah Dibuat di Pengeluaran Inspection Material!"); } result = await _repository.UpdateIMArea(id, model, dbModel); foreach (var item in dbModel.DyeingPrintingAreaInputProductionOrders.Where(d => !d.HasOutputDocument && !d.IsDeleted)) { double newBalance = 0; if (!dictBalance.TryGetValue(item.Id, out newBalance)) { newBalance = item.InputQuantity; } if (newBalance != 0) { var movementModel = new DyeingPrintingAreaMovementModel(dbModel.Date, item.MaterialOrigin, dbModel.Area, DyeingPrintingArea.IN, dbModel.Id, dbModel.BonNo, item.ProductionOrderId, item.ProductionOrderNo, item.CartNo, item.Buyer, item.Construction, item.Unit, item.Color, item.Motif, item.UomUnit, newBalance, item.Id, item.ProductionOrderType); result += await _movementRepository.InsertAsync(movementModel); } } foreach (var item in deletedData) { var movementModel = new DyeingPrintingAreaMovementModel(dbModel.Date, item.MaterialOrigin, dbModel.Area, DyeingPrintingArea.IN, dbModel.Id, dbModel.BonNo, item.ProductionOrderId, item.ProductionOrderNo, item.CartNo, item.Buyer, item.Construction, item.Unit, item.Color, item.Motif, item.UomUnit, item.Balance * -1, item.Id, item.ProductionOrderType); result += await _movementRepository.InsertAsync(movementModel); } return(result); }
public async Task <InputInspectionMaterialViewModel> ReadById(int id) { var model = await _repository.ReadByIdAsync(id); if (model == null) { return(null); } InputInspectionMaterialViewModel vm = MapToViewModel(model); return(vm); }
public async Task <int> Create(InputInspectionMaterialViewModel viewModel) { int result = 0; var model = _repository.GetDbSet().AsNoTracking() .FirstOrDefault(s => s.Area == DyeingPrintingArea.INSPECTIONMATERIAL && s.Date.Date == viewModel.Date.Date & s.Shift == viewModel.Shift); if (model == null) { int totalCurrentYearData = _repository.ReadAllIgnoreQueryFilter().Count(s => s.Area == DyeingPrintingArea.INSPECTIONMATERIAL && s.CreatedUtc.Year == viewModel.Date.Year); string bonNo = GenerateBonNo(totalCurrentYearData + 1, viewModel.Date); model = new DyeingPrintingAreaInputModel(viewModel.Date, viewModel.Area, viewModel.Shift, bonNo, viewModel.Group, viewModel.InspectionMaterialProductionOrders.Select(s => new DyeingPrintingAreaInputProductionOrderModel(viewModel.Area, s.ProductionOrder.Id, s.ProductionOrder.No, s.ProductionOrder.Type, s.ProductionOrder.OrderQuantity, s.PackingInstruction, s.CartNo, s.Buyer, s.Construction, s.Unit, s.Color, s.Motif, s.UomUnit, s.InputQuantity, s.InputQuantity, false, s.BuyerId, 0, s.Material.Id, s.Material.Name, s.MaterialConstruction.Id, s.MaterialConstruction.Name, s.MaterialWidth, s.ProcessType.Id, s.ProcessType.Name, s.YarnMaterial.Id, s.YarnMaterial.Name, s.InputQuantity, s.FinishWidth, viewModel.Date, s.MaterialOrigin)).ToList()); result = await _repository.InsertAsync(model); foreach (var item in model.DyeingPrintingAreaInputProductionOrders) { var movementModel = new DyeingPrintingAreaMovementModel(model.Date, item.MaterialOrigin, model.Area, DyeingPrintingArea.IN, model.Id, model.BonNo, item.ProductionOrderId, item.ProductionOrderNo, item.CartNo, item.Buyer, item.Construction, item.Unit, item.Color, item.Motif, item.UomUnit, item.InputQuantity, item.Id, item.ProductionOrderType); result += await _movementRepository.InsertAsync(movementModel); } } else { foreach (var item in viewModel.InspectionMaterialProductionOrders) { var modelItem = new DyeingPrintingAreaInputProductionOrderModel(viewModel.Area, item.ProductionOrder.Id, item.ProductionOrder.No, item.ProductionOrder.Type, item.ProductionOrder.OrderQuantity, item.PackingInstruction, item.CartNo, item.Buyer, item.Construction, item.Unit, item.Color, item.Motif, item.UomUnit, item.InputQuantity, item.InputQuantity, false, item.BuyerId, 0, item.Material.Id, item.Material.Name, item.MaterialConstruction.Id, item.MaterialConstruction.Name, item.MaterialWidth, item.ProcessType.Id, item.ProcessType.Name, item.YarnMaterial.Id, item.YarnMaterial.Name, item.InputQuantity, item.FinishWidth, viewModel.Date, item.MaterialOrigin); modelItem.DyeingPrintingAreaInputId = model.Id; result += await _productionOrderRepository.InsertAsync(modelItem); var movementModel = new DyeingPrintingAreaMovementModel(viewModel.Date, item.MaterialOrigin, viewModel.Area, DyeingPrintingArea.IN, model.Id, model.BonNo, item.ProductionOrder.Id, item.ProductionOrder.No, item.CartNo, item.Buyer, item.Construction, item.Unit, item.Color, item.Motif, item.UomUnit, item.InputQuantity, modelItem.Id, item.ProductionOrder.Type); result += await _movementRepository.InsertAsync(movementModel); } } return(result); }
private InputInspectionMaterialViewModel MapToViewModel(DyeingPrintingAreaInputModel model) { var vm = new InputInspectionMaterialViewModel() { Active = model.Active, Id = model.Id, Area = model.Area, Group = model.Group, BonNo = model.BonNo, CreatedAgent = model.CreatedAgent, CreatedBy = model.CreatedBy, CreatedUtc = model.CreatedUtc, Date = model.Date, DeletedAgent = model.DeletedAgent, DeletedBy = model.DeletedBy, DeletedUtc = model.DeletedUtc, IsDeleted = model.IsDeleted, LastModifiedAgent = model.LastModifiedAgent, LastModifiedBy = model.LastModifiedBy, LastModifiedUtc = model.LastModifiedUtc, Shift = model.Shift, InspectionMaterialProductionOrders = model.DyeingPrintingAreaInputProductionOrders.Select(s => new InputInspectionMaterialProductionOrderViewModel() { Active = s.Active, LastModifiedUtc = s.LastModifiedUtc, Balance = s.Balance, Buyer = s.Buyer, BuyerId = s.BuyerId, BalanceRemains = s.BalanceRemains, CartNo = s.CartNo, Color = s.Color, Construction = s.Construction, CreatedAgent = s.CreatedAgent, CreatedBy = s.CreatedBy, CreatedUtc = s.CreatedUtc, DeletedAgent = s.DeletedAgent, DeletedBy = s.DeletedBy, DeletedUtc = s.DeletedUtc, Grade = s.Grade, InitLength = s.InitLength, InputQuantity = s.InputQuantity, MaterialWidth = s.MaterialWidth, MaterialOrigin = s.MaterialOrigin, FinishWidth = s.FinishWidth, YarnMaterial = new CommonViewModelObjectProperties.YarnMaterial() { Id = s.YarnMaterialId, Name = s.YarnMaterialName }, ProcessType = new CommonViewModelObjectProperties.ProcessType() { Id = s.ProcessTypeId, Name = s.ProcessTypeName }, Material = new Material() { Id = s.MaterialId, Name = s.MaterialName }, MaterialConstruction = new MaterialConstruction() { Name = s.MaterialConstructionName, Id = s.MaterialConstructionId }, IsChecked = s.IsChecked, PackingInstruction = s.PackingInstruction, HasOutputDocument = s.HasOutputDocument, Id = s.Id, IsDeleted = s.IsDeleted, LastModifiedAgent = s.LastModifiedAgent, LastModifiedBy = s.LastModifiedBy, Motif = s.Motif, ProductionOrder = new ProductionOrder() { Id = s.ProductionOrderId, No = s.ProductionOrderNo, OrderQuantity = s.ProductionOrderOrderQuantity, Type = s.ProductionOrderType }, Unit = s.Unit, UomUnit = s.UomUnit }).ToList() }; return(vm); }