public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { DTO.FactorySalesOrderDTO dtoFactorySaleOrder = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactorySalesOrderDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { // Validation for input ValidationContext validationDTO = new ValidationContext(dtoFactorySaleOrder, null, null); List <ValidationResult> results = new List <ValidationResult>(); bool valid = Validator.TryValidateObject(dtoFactorySaleOrder, validationDTO, results, true); if (!valid) { string error = ""; List <string> detailError = new List <string>(); foreach (ValidationResult vr in results) { notification.Type = NotificationType.Error; error = vr.ErrorMessage; notification.Message = "Value not valid"; return(false); } } using (FactorySalesOrderMngEntities context = CreateContext()) { //var lstSaleQuotation = converter.GetListFactorySaleQuotation(context.FactorySaleOrderMng_ListFactorySaleQuotion_View.ToList()); //var existQuotation = lstSaleQuotation.Where(x => x.FactorySaleQuotationID == dtoFactorySaleOrder.FactorySaleQuotationID && x.FactorySaleQuotationUD == dtoFactorySaleOrder.FactorySaleQuotationUD).FirstOrDefault(); //if (existQuotation == null) //{ // notification.Type = NotificationType.Error; // notification.Message = "Sales Quotation Code not exist"; // return false; //} var lstcustomer = converter.GetListRawMaterial(context.FactorySaleQuotationMng_FactorySaleQuotation_ListRawMaterial_View.ToList()); var existcustomer = lstcustomer.Where(x => x.FactoryRawMaterialID == dtoFactorySaleOrder.FactoryRawMaterialID && x.FactoryRawMaterialShortNM == dtoFactorySaleOrder.FactoryRawMaterialShortNM).FirstOrDefault(); if (existcustomer == null) { notification.Type = NotificationType.Error; notification.Message = "Customer not exist"; return(false); } //var existClientContact = converter.GetListClientContact(context.FactorySaleQuotationMng_FactorySaleQuotation_ClientContact_View.ToList()); //var existClientContacts = existClientContact.Where(x => x.FullName == dtoFactorySaleOrder.FactoryRawMaterialContactPersonNM).FirstOrDefault(); //if (existClientContacts == null) //{ // notification.Type = NotificationType.Error; // notification.Message = "Contact Person Name not exist"; // return false; //} //var existEmp = converter.GetListListEmployee(context.FactorySaleQuotationMng_Employee_View.ToList()); //var existEmps = existEmp.Where(x => x.EmployeeNM == dtoFactorySaleOrder.EmployeeNM).FirstOrDefault(); //if (existEmps == null) //{ // notification.Type = NotificationType.Error; // notification.Message = "Employee Name not exist"; // return false; //} //List<DTO.ProductionItem> data = new List<DTO.ProductionItem>(); //var lstchhooseProId = dtoFactorySaleOrder.LstFactorySaleOrderDetail.Select(x => x.ProductionItemID).ToList(); //if(lstchhooseProId == null || lstchhooseProId.Count <= 0) //{ // notification.Type = NotificationType.Error; // notification.Message = "Please choose at least one product Product"; // return false; //} var lstProDB = converter.GetListListProductionItem(context.FactorySaleQuotationMng_FactorySaleQuotation_ListProductionItem_View.Where(o => o.ProductionItemNM != null).ToList()); var lstchhooseProName = dtoFactorySaleOrder.LstFactorySaleOrderDetail.Select(x => x.ProductionItemNM).ToList(); if (lstchhooseProName != null && lstchhooseProName.Count > 0) { if (lstProDB != null && lstProDB.Count > 0) { foreach (var item in lstchhooseProName) { var existProductName = lstProDB.Where(x => x.ProductionItemNM.Contains(item)).FirstOrDefault(); if (existProductName == null) { notification.Type = NotificationType.Error; notification.Message = "Product Name not exist"; return(false); } } } } if (notification.Type == NotificationType.Error) { return(false); } ///end validation FactorySaleOrder dbItem = null; if (id == 0) { dbItem = new FactorySaleOrder(); context.FactorySaleOrder.Add(dbItem); } else { dbItem = context.FactorySaleOrder.FirstOrDefault(o => o.FactorySaleOrderID == id); } // process Attached files foreach (FactorySalesOrderAttachedFileDTO dtoattachedFile in dtoFactorySaleOrder.LstFactorySaleOrderAttachedFile) { if (dtoattachedFile.OtherFileHasChange) { if (string.IsNullOrEmpty(dtoattachedFile.NewOtherFile)) { fwFactory.RemoveFile(dtoattachedFile.OtherFile); } else { dtoattachedFile.FileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoattachedFile.NewOtherFile, dtoattachedFile.OtherFile, dtoattachedFile.OtherFileFriendlyName); } } } if (dbItem == null) { notification.Message = "Factory Sale Order not found!"; return(false); } else { var userInformation = _AccountMngFactory.GetUserInformation(userId, out notification); ///convert dto to db converter.updateFactorySaleOrder(dtoFactorySaleOrder, ref dbItem); context.FactorySaleOrderDetail.Local.Where(o => o.FactorySaleOrder == null).ToList().ForEach(o => context.FactorySaleOrderDetail.Remove(o)); dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = userId; dbItem.FactorySaleOrderStatusID = 1; dbItem.DocumentDate = dtoFactorySaleOrder.DocumentDate.ConvertStringToDateTime(); dbItem.ExpectedPaidDate = dtoFactorySaleOrder.ExpectedPaidDate.ConvertStringToDateTime(); dbItem.CreatedDate = dtoFactorySaleOrder.CreatedDate.ConvertStringToDateTime(); dbItem.ValidUntil = dtoFactorySaleOrder.ValidUntil.ConvertStringToDateTime(); if (userInformation != null) { dbItem.CompanyID = userInformation.CompanyID; } context.SaveChanges(); // Generate FactorySaleOrderUD. if (id == 0) { context.FactorySaleOrderMng_function_GenerateFactorySaleOrderUD(dbItem.FactorySaleOrderID, dbItem.CreatedDate.Value.Year, dbItem.CreatedDate.Value.Month); } context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactorySaleOrderID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Message = ex.Message; notification.Type = Library.DTO.NotificationType.Error; return(false); } }
// update Factory Sale Order public void updateFactorySaleOrder(DTO.FactorySalesOrderDTO dtoItem, ref FactorySaleOrder dbItem) { //Update Factory Sale Order Detail if (dtoItem.LstFactorySaleOrderDetail != null) { foreach (FactorySaleOrderDetail item in dbItem.FactorySaleOrderDetail.ToArray()) { if (!dtoItem.LstFactorySaleOrderDetail.Select(x => x.FactorySaleOrderDetailID).Contains(item.FactorySaleOrderDetailID)) { dbItem.FactorySaleOrderDetail.Remove(item); } } foreach (DTO.FactorySalesOrderDetailDTO dto in dtoItem.LstFactorySaleOrderDetail) { FactorySaleOrderDetail item; if (dto.FactorySaleOrderDetailID <= 0) { item = new FactorySaleOrderDetail(); dbItem.FactorySaleOrderDetail.Add(item); } else { item = dbItem.FactorySaleOrderDetail.FirstOrDefault(s => s.FactorySaleOrderDetailID == dto.FactorySaleOrderDetailID); } if (item != null) { AutoMapper.Mapper.Map <DTO.FactorySalesOrderDetailDTO, FactorySaleOrderDetail>(dto, item); } } } //update Factory Sale Order Attached file if (dtoItem.LstFactorySaleOrderAttachedFile != null) { foreach (FactorySaleOrderAttachedFile item in dbItem.FactorySaleOrderAttachedFile.ToArray()) { if (!dtoItem.LstFactorySaleOrderAttachedFile.Select(x => x.FactorySaleOrderAttachedFileID).Contains(item.FactorySaleOrderAttachedFileID)) { dbItem.FactorySaleOrderAttachedFile.Remove(item); } } foreach (var item in dtoItem.LstFactorySaleOrderAttachedFile) { FactorySaleOrderAttachedFile dbAttachedFile; if (item.FactorySaleOrderAttachedFileID < 0) { dbAttachedFile = new FactorySaleOrderAttachedFile(); dbItem.FactorySaleOrderAttachedFile.Add(dbAttachedFile); } else { dbAttachedFile = dbItem.FactorySaleOrderAttachedFile.Where(o => o.FactorySaleOrderAttachedFileID == item.FactorySaleOrderAttachedFileID).FirstOrDefault(); } if (dbAttachedFile != null) { AutoMapper.Mapper.Map <DTO.FactorySalesOrderAttachedFileDTO, FactorySaleOrderAttachedFile>(item, dbAttachedFile); } } } AutoMapper.Mapper.Map <DTO.FactorySalesOrderDTO, FactorySaleOrder>(dtoItem, dbItem); }