public void DTO2DB_TaskStepComment(DTO.TaskStepComment dtoItem, ref TaskStepComment dbItem, string _tempFolder) { // map fields AutoMapper.Mapper.Map <DTO.TaskStepComment, TaskStepComment>(dtoItem, dbItem); // map file if (dtoItem.TaskStepCommentFiles != null) { // check for child rows deleted foreach (TaskStepCommentFile dbFile in dbItem.TaskStepCommentFile.ToArray()) { if (!dtoItem.TaskStepCommentFiles.Select(o => o.TaskStepCommentFileID).Contains(dbFile.TaskStepCommentFileID)) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { fwFactory.RemoveFile(dbFile.FileUD); } dbItem.TaskStepCommentFile.Remove(dbFile); } } // map child rows foreach (DTO.TaskStepCommentFile dtoFile in dtoItem.TaskStepCommentFiles) { TaskStepCommentFile dbFile; if (dtoFile.TaskStepCommentFileID <= 0) { dbFile = new TaskStepCommentFile(); dbItem.TaskStepCommentFile.Add(dbFile); } else { dbFile = dbItem.TaskStepCommentFile.FirstOrDefault(o => o.TaskStepCommentFileID == dtoFile.TaskStepCommentFileID); } if (dbFile != null) { if (dtoFile.HasChanged) { dbFile.FileUD = fwFactory.CreateFilePointer(_tempFolder, dtoFile.NewFile, dtoFile.FileUD, dtoFile.FriendlyName); } AutoMapper.Mapper.Map <DTO.TaskStepCommentFile, TaskStepCommentFile>(dtoFile, dbFile); } } } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { ComplianceProcessDTO inputParam = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <ComplianceProcessDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory(); try { using (ComplianceMngEntities context = CreateContext()) { ComplianceProcess dbItem = null; if (id == 0) { dbItem = new ComplianceProcess(); context.ComplianceProcess.Add(dbItem); dbItem.ComplianceProcessUD = context.ComplianceMng_function_GenerateCode().FirstOrDefault(); } else { //get db item dbItem = context.ComplianceProcess.FirstOrDefault(o => o.ComplianceProcessID == id); } if (dbItem == null) { notification.Message = "Comliance not found!"; return(false); } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // process Detail foreach (DTO.ComplianceAttachedFileDTO dtoDetail in inputParam.ComplianceAttachedFileDTOs) { // FileUD if (dtoDetail.FileUDHasChange) { if (string.IsNullOrEmpty(dtoDetail.NewFileUD)) { if (dtoDetail.FileUD == dtoDetail.NewFileUD) { dtoDetail.FileUD = null; } else { fwFactory.RemoveFile(dtoDetail.FileUD); } } else { dtoDetail.FileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDetail.NewFileUD, dtoDetail.FileUD, dtoDetail.FileUDFriendlyName); } } } foreach (DTO.CompliancePICDTO dtoDetail in inputParam.CompliancePICDTOs) { // FileUD if (dtoDetail.FileUDHasChange) { if (string.IsNullOrEmpty(dtoDetail.NewFileUD)) { if (dtoDetail.FileUD == dtoDetail.NewFileUD) { dtoDetail.FileUD = null; } else { fwFactory.RemoveFile(dtoDetail.FileUD); } } else { dtoDetail.FileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDetail.NewFileUD, dtoDetail.FileUD, dtoDetail.FileUDFriendlyName); } } } //read dto to db converter.DTO2DB_ComplianceProcess(inputParam, ref dbItem, userId); //delete offerline is null context.ComplianceAttachedFile.Local.Where(o => o.ComplianceProcess == null).ToList().ForEach(o => context.ComplianceAttachedFile.Remove(o)); context.CompliancePIC.Local.Where(o => o.ComplianceProcess == null).ToList().ForEach(o => context.CompliancePIC.Remove(o)); //save data context.SaveChanges(); //reload data if (id > 0) { dtoItem = new ComplianceProcessDTO { ComplianceProcessID = id }; } else { dtoItem = new ComplianceProcessDTO { ComplianceProcessID = dbItem.ComplianceProcessID }; } return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.HandleExceptionSingleLine(ex); } return(false); }
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); } }
public bool Update(int userId, int id, ref object dto, out Notification notification) { DTO.QCReportDTO dtoItem = ((Newtonsoft.Json.Linq.JObject)dto).ToObject <DTO.QCReportDTO>(); notification = new Notification() { Type = NotificationType.Success }; try { using (QCReportEntities context = CreateContext()) { QCReport dbItem = null; if (id > 0) { dbItem = context.QCReport.FirstOrDefault(s => s.QCReportID == id); if (dbItem == null) { notification = new Notification() { Type = NotificationType.Error, Message = "Can not find data" }; return(false); } } else { dbItem = new QCReport(); context.QCReport.Add(dbItem); } // add QCReportImage foreach (var item in dtoItem.QCReportImageDTOs) { if (item.ScanHasChange) { if (string.IsNullOrEmpty(item.ScanNewFile)) { fwFactory.RemoveFile(item.FileUD); } else { item.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", item.ScanNewFile, item.FileUD); } } } converter.DTO2DB_QCReport(dtoItem, ref dbItem); // product image if (dtoItem.ScanHasChange) { dbItem.ProductImage = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoItem.ScanNewFile, dtoItem.ProductImage); } if (dtoItem.ReportScanHasChange) { dbItem.ReportFile = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoItem.ReportScanNewFile, dtoItem.ReportFile); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.QCReportSummary.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportSummary.Remove(s)); context.QCReportDetail.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportDetail.Remove(s)); context.QCReportDefect.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportDefect.Remove(s)); context.QCReportImage.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportImage.Remove(s)); context.QCReportTestEnvironment.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportTestEnvironment.Remove(s)); context.QCReportFactoryOrderDetail.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportFactoryOrderDetail.Remove(s)); context.SaveChanges(); //Create code of id dbItem.QCReportUD = "QC" + dbItem.QCReportID.ToString("D8"); context.SaveChanges(); if (id == 0) { string emailSubject = ""; string emailBody = ""; string url = ""; url = this.frontendURL + "QCReportMng/Edit/" + dbItem.QCReportID.ToString(); emailSubject += "New QC report - " + "[" + dtoItem.ClientUD + "]" + " / " + "[" + dtoItem.FactoryUD + "]" + " - " + dtoItem.ArticleCode + " - " + dtoItem.Description; emailBody += "New QC Report:"; emailBody += Environment.NewLine + "Click <a href='" + url + "'>here</a> to link to QC Report:" + url; var QCReportMenber = context.QCReportMng_QCReportGroupMember_View.ToList(); var ClientManager = context.QCReportMng_ClientManager_View.Where(o => o.ClientID == dtoItem.ClientID).FirstOrDefault(); List <int?> UserIDs = new List <int?>(); UserIDs.Add(ClientManager.AccManagerID); UserIDs.Add(ClientManager.AccManagerAssitantID); UserIDs.Add(ClientManager.VNMerchandiserID); UserIDs.Add(ClientManager.VNMerchandiserAssitantID); string sendToEmail = ""; if (QCReportMenber.Count() > 0) { foreach (var item in QCReportMenber) { if (sendToEmail != "") { sendToEmail += ";" + item.Email; } else { sendToEmail = item.Email; } UserIDs.Add(item.UserID); } } if (ClientManager.AccManagerEmail != "") { if (sendToEmail != "") { sendToEmail += ";" + ClientManager.AccManagerEmail; } else { sendToEmail = ClientManager.AccManagerEmail; } } if (ClientManager.AccManagerAssitantEmail != "") { if (sendToEmail != "") { sendToEmail += ";" + ClientManager.AccManagerAssitantEmail; } else { sendToEmail = ClientManager.AccManagerAssitantEmail; } } if (ClientManager.VNMerchandiserEmail != "") { if (sendToEmail != "") { sendToEmail += ";" + ClientManager.VNMerchandiserEmail; } else { sendToEmail = ClientManager.VNMerchandiserEmail; } } if (ClientManager.VNMerchandiserAssitantEmail != "") { if (sendToEmail != "") { sendToEmail += ";" + ClientManager.VNMerchandiserAssitantEmail; } else { sendToEmail = ClientManager.VNMerchandiserAssitantEmail; } } SendNotification(emailSubject, emailBody, sendToEmail, UserIDs); } dto = GetData(dbItem.QCReportID, dbItem.SaleOrderDetailID, dbItem.FactoryID, null, null, null, out notification).Data; } return(true); } catch (Exception ex) { notification = new Notification() { Type = NotificationType.Error, Message = ex.Message }; return(false); } }
public bool RemoveFile(string fileUD) { return(factory.RemoveFile(fileUD)); }
public override bool UpdateData(int id, ref DTO.BookingMng.Booking dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (BookingMngEntities context = CreateContext()) { Booking dbItem = null; if (id == 0) { dbItem = new Booking(); dbItem.CreatedBy = dtoItem.UpdatedBy; dbItem.CreatedDate = DateTime.Now; context.Booking.Add(dbItem); } else { dbItem = context.Booking.FirstOrDefault(o => o.BookingID == id); } if (dbItem == null) { throw new Exception("Booking not found!"); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoItem.ConcurrencyFlag_String))) { throw new Exception(DALBase.Helper.TEXT_CONCURRENCY_CONFLICT); } // check if status = CONFIRMED if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception(DALBase.Helper.TEXT_CONFIRMED_CONFLICT); } //file processing Library.FileHelper.FileManager fileMng = new Library.FileHelper.FileManager(FrameworkSetting.Setting.AbsoluteFileFolder); string fileNeedDeleted = string.Empty; string thumbnailFileNeedDeleted = string.Empty; if (dtoItem.BLFileHasChange) { //check to delete file does not exist in database if (!string.IsNullOrEmpty(dtoItem.BLFile)) { fwFactory.GetDBFileLocation(dtoItem.BLFile, out fileNeedDeleted, out thumbnailFileNeedDeleted); if (!string.IsNullOrEmpty(fileNeedDeleted)) { try { fileMng.DeleteFile(fileNeedDeleted); } catch { } } } if (string.IsNullOrEmpty(dtoItem.NewBLFile)) { // remove file registration in File table fwFactory.RemoveFile(dtoItem.BLFile); // reset file in table Contract dtoItem.BLFile = string.Empty; } else { string outDBFileLocation = ""; string outFileFullPath = ""; string outFilePointer = ""; // copy new file fileMng.StoreFile(_tempFolder + dtoItem.NewBLFile, out outDBFileLocation, out outFileFullPath); if (File.Exists(outFileFullPath)) { FileInfo info = new FileInfo(outFileFullPath); // insert/update file registration in database fwFactory.UpdateFile(dtoItem.BLFile, dtoItem.NewBLFile, outDBFileLocation, info.Extension, "", (int)info.Length, out outFilePointer); // set file database pointer dtoItem.BLFile = outFilePointer; } } } converter.DTO2DB(dtoItem, ref dbItem); dbItem.UpdatedBy = dtoItem.UpdatedBy; dbItem.UpdatedDate = DateTime.Now; // remove orphans context.BookingDetail.Local.Where(o => o.Booking == null).ToList().ForEach(o => context.BookingDetail.Remove(o)); context.SaveChanges(); // generate booking code if (id == 0) { //dbItem.BookingUD = supportFactory.GetSupplier().FirstOrDefault(o => o.SupplierID == dbItem.SupplierID).SupplierUD + "/" + dtoItem.ClientUD + "/" + dtoItem.Season.Substring(5, 4); //using (DbContextTransaction scope = context.Database.BeginTransaction()) //{ // context.Database.ExecuteSqlCommand("SELECT TOP 1 * FROM Booking WITH (TABLOCKX, HOLDLOCK)"); // try // { // dbItem.BookingUD = context.Booking.Count(o => o.SupplierID == dbItem.SupplierID && o.Season == dbItem.Season).ToString().AddPrefix("0", 4) + "/" + dbItem.BookingUD; // context.SaveChanges(); // } // catch { } // finally // { // scope.Commit(); // } //} context.BookingMng_function_UpdateBookingRef(dbItem.BookingID); } dtoItem = GetData(dbItem.BookingID, 0, 0, "", out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message)) { notification.DetailMessage.Add(ex.InnerException.Message); } return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { DTO.FactorySaleQuotationDTO dtoFactorySaleQuotation = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactorySaleQuotationDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { if (dtoFactorySaleQuotation.FactoryRawMaterialID == null) { notification.Type = NotificationType.Error; notification.Message = "Customer not exist"; return(false); } using (FactorySalesQuotationMngEntities context = CreateContext()) { FactorySaleQuotation dbItem = null; if (id == 0) { dbItem = new FactorySaleQuotation(); context.FactorySaleQuotation.Add(dbItem); } else { dbItem = context.FactorySaleQuotation.FirstOrDefault(o => o.FactorySaleQuotationID == id); } // process Attached files foreach (FactorySaleQuotationAttachedFileDTO dtoattachedFile in dtoFactorySaleQuotation.LstFactorySaleQuotationAttachedFile) { 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 Quotation not found!"; return(false); } else { ///convert dto to db var userInformation = _AccountMngFactory.GetUserInformation(userId, out notification); converter.updateFactorySaleQuotation(dtoFactorySaleQuotation, ref dbItem); context.FactorySaleQuotationDetail.Local.Where(o => o.FactorySaleQuotation == null).ToList().ForEach(o => context.FactorySaleQuotationDetail.Remove(o)); context.FactorySaleQuotationAttachedFile.Local.Where(o => o.FactorySaleQuotation == null).ToList().ForEach(o => context.FactorySaleQuotationAttachedFile.Remove(o)); dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = userId; dbItem.FactorySaleQuotationStatusID = 1; dbItem.DocumentDate = dtoFactorySaleQuotation.DocumentDate.ConvertStringToDateTime(); dbItem.ExpectedPaidDate = dtoFactorySaleQuotation.ExpectedPaidDate.ConvertStringToDateTime(); dbItem.CreatedDate = dtoFactorySaleQuotation.CreatedDate.ConvertStringToDateTime(); dbItem.ValidUntil = dtoFactorySaleQuotation.ValidUntil.ConvertStringToDateTime(); if (userInformation != null) { dbItem.CompanyID = userInformation.CompanyID; } // Generate FactorySaleQuotationUD. if (id == 0) { dbItem.FactorySaleQuotationUD = context.FactorySaleQuotationMng_function_GenerateFactorySaleQuotationUD(dbItem.CreatedDate.Value.Year, dbItem.CreatedDate.Value.Month).FirstOrDefault(); } context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactorySaleQuotationID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Message = ex.Message; notification.Type = Library.DTO.NotificationType.Error; return(false); } }