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.ProductionPriceDTO dtoProductionPrice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductionPriceDTO>(); try { int?companyID = fw_factory.GetCompanyID(userId); using (ProductionPriceMngEntities context = CreateContext()) { ProductionPrice dbItem = null; if (id == 0) { dbItem = new ProductionPrice(); context.ProductionPrice.Add(dbItem); } else { dbItem = context.ProductionPrice.Where(o => o.ProductionPriceID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //convert dto to db converter.DTO2DB_ProductionPrice(dtoProductionPrice, ref dbItem, userId, companyID); //remove orphan context.ProductionPriceDetail.Local.Where(o => o.ProductionPrice == null).ToList().ForEach(o => context.ProductionPriceDetail.Remove(o)); //save data context.SaveChanges(); // Run automatic update unit price if (dtoProductionPrice.UpdateTypeID.HasValue && dtoProductionPrice.UpdateTypeID.Value == 2) { context.ProductionPriceMng_function_UpdatePriceReceivingNote(dbItem.ProductionPriceID); } //get return data dtoItem = GetData(dbItem.ProductionPriceID, out notification).Data; return(true); } } } catch (Exception ex) { Exception iEx = Library.Helper.GetInnerException(ex); notification.Type = NotificationType.Error; notification.Message = iEx.Message; return(false); } }
private bool SaveDataBeforeLock(int userId, int id, DTO.ProductionPriceDTO dtoProductionPrice, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { int?companyID = fw_factory.GetCompanyID(userId); using (ProductionPriceMngEntities context = CreateContext()) { ProductionPrice dbItem = null; if (id == 0) { dbItem = new ProductionPrice(); context.ProductionPrice.Add(dbItem); } else { dbItem = context.ProductionPrice.Where(o => o.ProductionPriceID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //convert dto to db converter.DTO2DB_ProductionPrice(dtoProductionPrice, ref dbItem, userId, companyID); //remove orphan context.ProductionPriceDetail.Local.Where(o => o.ProductionPrice == null).ToList().ForEach(o => context.ProductionPriceDetail.Remove(o)); //save data context.SaveChanges(); return(true); } } } catch (Exception ex) { Exception iEx = Library.Helper.GetInnerException(ex); notification.Type = NotificationType.Error; notification.Message = iEx.Message; return(false); } }
public void DTO2DB_ProductionPrice(DTO.ProductionPriceDTO dtoItem, ref ProductionPrice dbItem, int?userId, int?companyID) { if (dtoItem.ProductionPriceDetailDTOs != null) { //delete item in db that exist in dto but not exist in db foreach (var item in dbItem.ProductionPriceDetail.ToArray()) { if (!dtoItem.ProductionPriceDetailDTOs.Select(s => s.ProductionPriceDetailID).Contains(item.ProductionPriceDetailID)) { dbItem.ProductionPriceDetail.Remove(item); } } //read from dto to db ProductionPriceDetail dbPurchaseDetail; foreach (var item in dtoItem.ProductionPriceDetailDTOs) { if (item.ProductionPriceDetailID > 0) { dbPurchaseDetail = dbItem.ProductionPriceDetail.Where(o => o.ProductionPriceDetailID == item.ProductionPriceDetailID).FirstOrDefault(); } else { dbPurchaseDetail = new ProductionPriceDetail(); dbItem.ProductionPriceDetail.Add(dbPurchaseDetail); } //read purchase request detail dto to db if (dbPurchaseDetail != null) { AutoMapper.Mapper.Map <DTO.ProductionPriceDetailDTO, ProductionPriceDetail>(item, dbPurchaseDetail); } } } AutoMapper.Mapper.Map <DTO.ProductionPriceDTO, ProductionPrice>(dtoItem, dbItem); dbItem.CompanyID = companyID; dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; if (!dtoItem.ProductionPriceID.HasValue) { dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; } }