public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryRawMaterial dtoFactoryRawMaterial = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryRawMaterial>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { //Get CompanyID int?companyID = fwFactory.GetCompanyID(userId); using (FactoryRawMaterialMngEntities context = CreateContext()) { FactoryRawMaterial dbItem = null; if (id == 0) { dbItem = new FactoryRawMaterial(); context.FactoryRawMaterial.Add(dbItem); var checkUD = context.FactoryRawMaterial.ToList(); foreach (var item in checkUD) { if (item.FactoryRawMaterialUD == dtoFactoryRawMaterial.FactoryRawMaterialUD) { throw new Exception("Sub Supplier code exists !"); } } } else { dbItem = context.FactoryRawMaterial.FirstOrDefault(o => o.FactoryRawMaterialID == id); } if (dbItem == null) { notification.Message = "Factory Raw Material not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryRawMaterial.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } //file processing Library.FileHelper.FileManager fileMng = new Library.FileHelper.FileManager(FrameworkSetting.Setting.AbsoluteFileFolder); string fileNeedDeleted = string.Empty; string thumbnailFileNeedDeleted = string.Empty; foreach (var contractItem in dtoFactoryRawMaterial.SubSupplierContracts.Where(o => o.ContractFileHasChange)) { if (!string.IsNullOrEmpty(contractItem.ContractFile)) { fwFactory.GetDBFileLocation(contractItem.ContractFile, out fileNeedDeleted, out thumbnailFileNeedDeleted); if (!string.IsNullOrEmpty(fileNeedDeleted)) { try { fileMng.DeleteFile(fileNeedDeleted); } catch { } } } if (string.IsNullOrEmpty(contractItem.NewContractFile)) { // remove file registration in File table fwFactory.RemoveFile(contractItem.ContractFile); // reset file in table Contract contractItem.ContractFile = string.Empty; } else { string outDBFileLocation = ""; string outFileFullPath = ""; string outFilePointer = ""; // copy new file fileMng.StoreFile(this._tempFolder + contractItem.NewContractFile, out outDBFileLocation, out outFileFullPath); if (File.Exists(outFileFullPath)) { FileInfo info = new FileInfo(outFileFullPath); // insert/update file registration in database fwFactory.UpdateFile(contractItem.ContractFile, contractItem.NewContractFile, outDBFileLocation, info.Extension, "", (int)info.Length, out outFilePointer); // set file database pointer contractItem.ContractFile = outFilePointer; } } } // processing certificate file foreach (DTO.FactoryRawMaterialCertificate dtoCertificate in dtoFactoryRawMaterial.FactoryRawMaterialCertificates) { if (dtoCertificate.CertificateFileHasChange) { if (string.IsNullOrEmpty(dtoCertificate.NewCertificateFile)) { fwFactory.RemoveImageFile(dtoCertificate.CertificateFile); } else { dtoCertificate.CertificateFile = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoCertificate.NewCertificateFile, dtoCertificate.CertificateFile); } } } // Process business card image foreach (DTO.FactoryRawMaterialBusinessCardDTO dtoCard in dtoFactoryRawMaterial.FactoryRawMaterialBusinessCardDTO) { if (dtoCard.FrontHasChange) { dtoCard.FrontFileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoCard.FrontNewFile, dtoCard.FrontFileUD); } if (dtoCard.BehindHasChange) { dtoCard.BehindFileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoCard.BehindNewFile, dtoCard.BehindFileUD); } } // processing logo image if (dtoFactoryRawMaterial.LogoFile_HasChange) { dtoFactoryRawMaterial.LogoFile = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoFactoryRawMaterial.LogoFile_NewFile, dtoFactoryRawMaterial.LogoFile); } // process image foreach (DTO.FactoryRawMaterialImage dtoImage in dtoFactoryRawMaterial.FactoryRawMaterialImages) { if (dtoImage.HasChange) { dtoImage.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoImage.NewFile, dtoImage.FileUD); } } if (dtoFactoryRawMaterial.FactoryRawMaterialGalleryDTO != null) { // Pre-event update FactoryGallery foreach (DTO.FactoryRawMaterialGalleryDTO dtoFactoryGallery in dtoFactoryRawMaterial.FactoryRawMaterialGalleryDTO.Where(o => o.FactoryGalleryHasChange)) { dtoFactoryGallery.FactoryRawMaterialGalleryUD = fwFactory.CreateNoneImageFilePointer(this._tempFolder, dtoFactoryGallery.FactoryGalleryNewFile, dtoFactoryGallery.FactoryRawMaterialGalleryUD); } } // remove Technical Image foreach (FactoryRawMaterialImage dbImage in context.FactoryRawMaterialImage.Local.Where(o => o.FactoryRawMaterial == null).ToList()) { if (!string.IsNullOrEmpty(dbImage.FileUD)) { fwFactory.RemoveImageFile(dbImage.FileUD); } } //attach file foreach (var item in dtoFactoryRawMaterial.materialsPrices.Where(o => o.AttachFileHasChange)) { item.AttachFile = string.Empty; string outDBFileLocation = ""; string outFileFullPath = ""; string outFilePointer = ""; // copy new file fileMng.StoreFile(this._tempFolder + item.NewAttachFile, out outDBFileLocation, out outFileFullPath); if (File.Exists(outFileFullPath)) { FileInfo info = new FileInfo(outFileFullPath); // insert/update file registration in database fwFactory.UpdateFile(item.AttachFile, item.NewAttachFile, outDBFileLocation, info.Extension, "", (int)info.Length, out outFilePointer); // set file database pointer item.AttachFile = outFilePointer; } } foreach (DTO.MaterialsPrice item in dtoFactoryRawMaterial.materialsPrices) { if (item.IsChange == true) { MaterialPriceHistory dbItemPriceHistory = new MaterialPriceHistory(); dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.Price = (decimal)item.OldPrice; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.Qty = (decimal)item.OldQty; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.StatusID = (int)item.OldStatusID; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.ValidFrom = Convert.ToDateTime(item.OldValidFrom); dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.AttachFileHistory = item.OldAttachFile; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.RemarkHistory = item.OldRemark; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; context.MaterialPriceHistory.Add(dbItemPriceHistory); } else if (item.AttachFile != item.OldAttachFile) { MaterialPriceHistory dbItemPriceHistory = new MaterialPriceHistory(); dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.Price = (decimal)item.OldPrice; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.Qty = (decimal)item.OldQty; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.StatusID = (int)item.OldStatusID; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.ValidFrom = Convert.ToDateTime(item.OldValidFrom); dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.AttachFileHistory = item.OldAttachFile; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; dbItemPriceHistory.MaterialsPriceID = (int)item.MaterialsPriceID; dbItemPriceHistory.RemarkHistory = item.OldRemark; dbItemPriceHistory.UpdatedBy = userId; dbItemPriceHistory.UpdatedDate = DateTime.Now; context.MaterialPriceHistory.Add(dbItemPriceHistory); } } //convert dto to db converter.DTO2BD(dtoFactoryRawMaterial, ref dbItem); if (!string.IsNullOrEmpty(dbItem.WebAddress)) { dbItem.WebAddress = dbItem.WebAddress.ToLower().Replace("http://", "").Replace("https://", ""); } // FactoryRawMaterialCertificate context.FactoryRawMaterialCertificate.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.FactoryRawMaterialCertificate.Remove(o)); // FactoryInHouseTest context.FactoryRawMaterialTest.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.FactoryRawMaterialTest.Remove(o)); // FactoryRawMaterialPricingPerson context.FactoryRawMaterialPricingPerson.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.FactoryRawMaterialPricingPerson.Remove(o)); // FactoryRawMaterialPricingPerson context.FactoryRawMaterialQualityPerson.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.FactoryRawMaterialQualityPerson.Remove(o)); // FactoryRawMaterialPaymentTerm context.FactoryRawMaterialPaymentTerm.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.FactoryRawMaterialPaymentTerm.Remove(o)); // FactoryRawMaterialMng_SupplierContactQuickInfo_View context.SupplierContactQuickInfo.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.SupplierContactQuickInfo.Remove(o)); context.SupplierDirector.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.SupplierDirector.Remove(o)); context.SupplierManager.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.SupplierManager.Remove(o)); context.SupplierSampleTechnical.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.SupplierSampleTechnical.Remove(o)); context.MaterialsPrice.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.MaterialsPrice.Remove(o)); context.MaterialPriceHistory.Local.Where(o => o.MaterialsPrice == null).ToList().ForEach(o => context.MaterialPriceHistory.Remove(o)); context.FactoryRawMaterialBusinessCard.Local.Where(o => o.FactoryRawMaterial == null).ToList().ForEach(o => context.FactoryRawMaterialBusinessCard.Remove(o)); // FactoryGallery for Factory(remove value Factory is null) foreach (FactoryRawMaterialGallery dbFactoryGallery in context.FactoryRawMaterialGallery.Where(o => o.FactoryRawMaterial == null).ToList()) { fwFactory.RemoveImageFile(dbFactoryGallery.FactoryRawMaterialGalleryUD); context.FactoryRawMaterialGallery.Remove(dbFactoryGallery); } dbItem.CompanyID = companyID; dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = userId; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryRawMaterialID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
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); } }