Ejemplo n.º 1
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (CatalogFileMngEntities context = CreateContext())
                {
                    CatalogFile dbItem = context.CatalogFile.FirstOrDefault(o => o.CatalogFileID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("Catalog file not found!");
                    }

                    // remove file
                    if (!string.IsNullOrEmpty(dbItem.FileUD))
                    {
                        fwFactory.RemoveFile(dbItem.FileUD);
                    }
                    context.CatalogFile.Remove(dbItem);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (OnlineFileMngEntities context = CreateContext())
                {
                    // check if can delete
                    OnlineFile dbItem = context.OnlineFile.FirstOrDefault(o => o.OnlineFileID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("File not found");
                    }

                    // everything ok, delete
                    // remove attached file
                    if (!string.IsNullOrEmpty(dbItem.FileUD))
                    {
                        fwFactory.RemoveFile(dbItem.FileUD);
                    }
                    context.OnlineFile.Remove(dbItem);
                    context.OnlineFilePermission.Local.Where(o => o.OnlineFile == null).ToList().ForEach(o => context.OnlineFilePermission.Remove(o));
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.PriceListFile dtoPriceListFile = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PriceListFile>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            List <DTO.PriceListFile> PriceListFileDTOs = new List <DTO.PriceListFile>();

            try
            {
                using (PriceListFileEntities context = CreateContext())
                {
                    PriceListFile dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new PriceListFile();
                        context.PriceListFile.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.PriceListFile.FirstOrDefault(o => o.PriceListFileID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Data not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoPriceListFile.ConcurrencyFlag_String)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        // processing pdf
                        if (dtoPriceListFile.PDFFileLocation_HasChange)
                        {
                            if (string.IsNullOrEmpty(dtoPriceListFile.NewPDFFile))
                            {
                                fwFactory.RemoveFile(dtoPriceListFile.PDFFileUD);
                            }
                            else
                            {
                                dtoPriceListFile.PDFFileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoPriceListFile.NewPDFFile, dtoPriceListFile.PDFFileUD, dtoPriceListFile.PDFFriendlyName);
                            }
                        }

                        // processing excel
                        if (dtoPriceListFile.ExcelFileLocation_HasChange)
                        {
                            if (string.IsNullOrEmpty(dtoPriceListFile.NewExcelFile))
                            {
                                fwFactory.RemoveFile(dtoPriceListFile.ExcelFileUD);
                            }
                            else
                            {
                                dtoPriceListFile.ExcelFileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoPriceListFile.NewExcelFile, dtoPriceListFile.ExcelFileUD, dtoPriceListFile.ExcelFriendlyName);
                            }
                        }
                        converter.DTO2DB(dtoPriceListFile, ref dbItem);

                        dbItem.UpdatedDate = DateTime.Now;
                        dbItem.UpdatedBy   = userId;

                        context.SaveChanges();

                        dtoItem = GetData(dbItem.PriceListFileID, out notification).Data;

                        return(true);
                    }
                }
            }

            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }