public void DTO2DB_PurchaseQuatation(DTO.PurchaseQuotationDTO dtoItem, ref PurchaseQuotation dbItem) { if (dtoItem.PurchaseQuotationDetailDTOs != null) { foreach (var item in dbItem.PurchaseQuotationDetail.ToArray()) { if (!dtoItem.PurchaseQuotationDetailDTOs.Select(s => s.PurchaseQuotationDetailID).Contains(item.PurchaseQuotationDetailID)) { dbItem.PurchaseQuotationDetail.Remove(item); } } foreach (var item in dtoItem.PurchaseQuotationDetailDTOs) { PurchaseQuotationDetail dbDetail = new PurchaseQuotationDetail(); if (item.PurchaseQuotationDetailID < 0) { dbItem.PurchaseQuotationDetail.Add(dbDetail); } else { dbDetail = dbItem.PurchaseQuotationDetail.Where(s => s.PurchaseQuotationDetailID == item.PurchaseQuotationDetailID).FirstOrDefault(); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.PurchaseQuotationDetailDTO, PurchaseQuotationDetail>(item, dbDetail); } } } AutoMapper.Mapper.Map <DTO.PurchaseQuotationDTO, PurchaseQuotation>(dtoItem, dbItem); dbItem.ValidFrom = dtoItem.ValidFrom.ConvertStringToDateTime(); dbItem.ValidTo = dtoItem.ValidTo.ConvertStringToDateTime(); }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Notification { Type = NotificationType.Success }; DTO.PurchaseQuotationDTO dtoPurchaseQuotation = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PurchaseQuotationDTO>(); try { // 1. Check validation validFrom and validTo var validFrom = dtoPurchaseQuotation.ValidFrom.ConvertStringToDateTime(); var validTo = dtoPurchaseQuotation.ValidTo.ConvertStringToDateTime(); // 1.1. Case validFrom not value if (!validFrom.HasValue) { notification.Type = NotificationType.Error; notification.Message = "Valid From is invalid!"; return(false); } // 1.2. Compare if (validTo.HasValue) { if (validTo.Value.CompareTo(validFrom.Value) < 0) { notification.Type = NotificationType.Error; notification.Message = "Valid From is less than Valid To!"; return(false); } } using (var context = CreatContext()) { PurchaseQuotation dbItem; if (id == 0) { dbItem = new PurchaseQuotation(); context.PurchaseQuotation.Add(dbItem); } else { dbItem = context.PurchaseQuotation.Where(o => o.PurchaseQuotationID == id).FirstOrDefault(); } if (dbItem == null) { notification.Type = NotificationType.Error; notification.Message = "Data Not Found !"; return(false); } else { //converter converter.DTO2DB_PurchaseQuatation(dtoPurchaseQuotation, ref dbItem); //upload file Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory(); string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"; if (dtoPurchaseQuotation.File_HasChange.HasValue && dtoPurchaseQuotation.File_HasChange.Value) { dbItem.AttachedFile = fwFactory.CreateFilePointer(tempFolder, dtoPurchaseQuotation.File_NewFile, dtoPurchaseQuotation.AttachedFile, dtoPurchaseQuotation.FriendlyName); } //Remove context.PurchaseQuotationDetail.Local.Where(o => o.PurchaseQuotation == null).ToList().ForEach(o => context.PurchaseQuotationDetail.Remove(o)); int?companyID = fwFactory.GetCompanyID(userId); dbItem.CompanyID = companyID; dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; if (id == 0) { dbItem.PurchaseQuotationUD = context.PurchaseQuotationMng_function_GeneratePurchaseQuotationUD().FirstOrDefault(); dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; } /// cuong.tran: ProductionFrameWeight - start foreach (var item in dtoPurchaseQuotation.PurchaseQuotationDetailDTOs) { /// Only material is component if (item.ProductionItemTypeID == 1 && item.FrameWeight.HasValue) { var dtoFrameWeight = context.ProductionFrameWeight.FirstOrDefault(o => o.ProductionItemID == item.ProductionItemID); if (dtoFrameWeight == null) { // Insert table ProductionFrameWeight ProductionFrameWeight dbFrameWeight = new ProductionFrameWeight(); context.ProductionFrameWeight.Add(dbFrameWeight); dbFrameWeight.ProductionItemID = item.ProductionItemID; dbFrameWeight.FrameWeight = item.FrameWeight; dbFrameWeight.UpdatedBy = userId; dbFrameWeight.UpdatedDate = DateTime.Now; context.SaveChanges(); // Insert table ProductionFrameWeightHistory ProductionFrameWeightHistory dbFrameWeightHistory = new ProductionFrameWeightHistory(); context.ProductionFrameWeightHistory.Add(dbFrameWeightHistory); dbFrameWeightHistory.ProductionFrameWeightID = dbFrameWeight.ProductionFrameWeightID; dbFrameWeightHistory.FrameWeight = item.FrameWeight; dbFrameWeightHistory.UpdatedBy = userId; dbFrameWeightHistory.UpdatedDate = DateTime.Now; context.SaveChanges(); } } } /// cuong.tran: ProductionFrameWeight - e n d //Save context.SaveChanges(); dtoItem = GetData(dbItem.PurchaseQuotationID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = Library.Helper.GetInnerException(ex).Message; return(false); } }