public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactoryFinishedProductReceipt dtoFactoryFinishedProductReceipt = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryFinishedProductReceipt>(); dtoFactoryFinishedProductReceipt.UpdatedBy = userId; try { using (FactoryFinishedProductReceiptEntities context = CreateContext()) { FactoryFinishedProductReceipt dbItem = null; if (id == 0) { dbItem = new FactoryFinishedProductReceipt(); context.FactoryFinishedProductReceipt.Add(dbItem); } else { dbItem = context.FactoryFinishedProductReceipt.Where(o => o.FactoryFinishedProductReceiptID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { if (dtoFactoryFinishedProductReceipt.ReceiptTypeID == 1) { if (dtoFactoryFinishedProductReceipt.InProgressOrBuyDirectlyID == 1) { if (!dtoFactoryFinishedProductReceipt.ImportFromTeamID.HasValue) { throw new Exception("Team is empty. You should fill-in Team"); } } } else if (dtoFactoryFinishedProductReceipt.ReceiptTypeID == 2) { if (!dtoFactoryFinishedProductReceipt.ExportToTeamID.HasValue) { throw new Exception("Team is empty. You should fill-in Team"); } } if (!dtoFactoryFinishedProductReceipt.FactoryStepID.HasValue) { throw new Exception("Step is empty. You should fill-in Step"); } if (!dtoFactoryFinishedProductReceipt.FactoryGoodsProcedureID.HasValue) { throw new Exception("Step is empty. You should fill-in goods procedure"); } //validate quantity //ValidateQuantity(dtoFactoryFinishedProductReceipt); //convert dto to db converter.DTO2DB_FactoryFinishedProductReceipt(dtoFactoryFinishedProductReceipt, ref dbItem); //remove orphan item context.FactoryFinishedProductReceiptDetail.Local.Where(o => o.FactoryFinishedProductReceipt == null).ToList().ForEach(o => context.FactoryFinishedProductReceiptDetail.Remove(o)); //save data context.SaveChanges(); //update receipt no context.FactoryFinishedProductReceiptMng_function_GenerateReceipNo(dbItem.FactoryFinishedProductReceiptID, dbItem.Season, dbItem.ReceiptTypeID); //get return data dtoItem = GetData(dbItem.FactoryFinishedProductReceiptID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public void DTO2DB_FactoryFinishedProductReceipt(DTO.FactoryFinishedProductReceipt dtoItem, ref FactoryFinishedProductReceipt dbItem) { if (dtoItem.FactoryFinishedProductReceiptDetails != null) { foreach (var item in dbItem.FactoryFinishedProductReceiptDetail.ToArray()) { if (!dtoItem.FactoryFinishedProductReceiptDetails.Select(s => s.FactoryFinishedProductReceiptDetailID).Contains(item.FactoryFinishedProductReceiptDetailID)) { dbItem.FactoryFinishedProductReceiptDetail.Remove(item); } } foreach (var item in dtoItem.FactoryFinishedProductReceiptDetails) { FactoryFinishedProductReceiptDetail dbDetail; if (item.FactoryFinishedProductReceiptDetailID < 0) { dbDetail = new FactoryFinishedProductReceiptDetail(); dbItem.FactoryFinishedProductReceiptDetail.Add(dbDetail); } else { dbDetail = dbItem.FactoryFinishedProductReceiptDetail.Where(o => o.FactoryFinishedProductReceiptDetailID == item.FactoryFinishedProductReceiptDetailID).FirstOrDefault(); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.FactoryFinishedProductReceiptDetail, FactoryFinishedProductReceiptDetail>(item, dbDetail); } } } AutoMapper.Mapper.Map <DTO.FactoryFinishedProductReceipt, FactoryFinishedProductReceipt>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = dtoItem.UpdatedBy; dbItem.ReceiptDate = dtoItem.ReceiptDate.ConvertStringToDateTime(); if (dtoItem.FactoryFinishedProductReceiptID == null) { dbItem.CreatedDate = DateTime.Now; dbItem.CreatedBy = dtoItem.UpdatedBy; } }