예제 #1
0
        // Add new key raw material
        public List <DTO.KeyRawMaterial> UpdateRawList(int userId, object dtoItem, out Library.DTO.Notification notification)
        {
            List <DTO.KeyRawMaterial> dtoKeyRawMaterialList = ((Newtonsoft.Json.Linq.JArray)dtoItem).ToObject <List <DTO.KeyRawMaterial> >();

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            List <DTO.KeyRawMaterial> DTOKeyRawMaterials = new List <DTO.KeyRawMaterial>();

            try
            {
                using (FactoryRawMaterialMngEntities context = CreateContext())
                {
                    var dbItemList = context.KeyRawMaterial.Select(o => o).ToList();
                    //converter.DTO2DB_KeyRawMaterial(dtoKeyRawMaterialList, ref dbItemList);

                    //Delete orphan
                    foreach (var item in dbItemList.ToArray())
                    {
                        if (!dtoKeyRawMaterialList.Select(o => o.KeyRawMaterialID).Contains(item.KeyRawMaterialID))
                        {
                            context.KeyRawMaterial.Remove(item);
                        }
                    }

                    //Map child row
                    foreach (var item in dtoKeyRawMaterialList)
                    {
                        KeyRawMaterial dbKeyRawMaterial;
                        if (item.KeyRawMaterialID <= 0)
                        {
                            dbKeyRawMaterial = new KeyRawMaterial();
                            context.KeyRawMaterial.Add(dbKeyRawMaterial);
                        }
                        else
                        {
                            dbKeyRawMaterial = context.KeyRawMaterial.FirstOrDefault(o => o.KeyRawMaterialID == item.KeyRawMaterialID);
                        }

                        if (dbKeyRawMaterial != null)
                        {
                            AutoMapper.Mapper.Map <DTO.KeyRawMaterial, KeyRawMaterial>(item, dbKeyRawMaterial);
                        }
                    }

                    context.SaveChanges();
                }
                DTOKeyRawMaterials = GetKeyMaterialList();
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
            }

            return(DTOKeyRawMaterials);
        }
예제 #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 (FactoryRawMaterialMngEntities context = CreateContext())
                {
                    FactoryRawMaterial dbItem = context.FactoryRawMaterial.FirstOrDefault(o => o.FactoryRawMaterialID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Sub Supplier not found!";
                        return(false);
                    }
                    else
                    {
                        context.FactoryRawMaterial.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }
예제 #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);
            }
        }