Exemple #1
0
        public bool UpdateProductStatus2(int userId, int id, int statusId, string file, out Library.DTO.Notification notification)
        {
            // FINISH STATUS
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (SampleItemMngEntities context = CreateContext())
                {
                    SampleProduct dbItem = context.SampleProduct.FirstOrDefault(o => o.SampleProductID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Sample Product not found!";
                        return(false);
                    }
                    else
                    {
                        dbItem.SampleProductStatusID = statusId;
                        dbItem.StatusUpdatedBy       = userId;
                        dbItem.StatusUpdatedDate     = DateTime.Now;

                        // update file
                        if (!string.IsNullOrEmpty(file))
                        {
                            dbItem.FinishedImage = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", file, dbItem.FinishedImage);
                        }

                        // add product item
                        int totalExistingItem = dbItem.SampleProductItem.Count();
                        for (int index = 1; index <= dbItem.Quantity.Value - totalExistingItem; index++)
                        {
                            SampleProductItem dbProductItem = new SampleProductItem();
                            dbProductItem.SampleProductItemUD = dbItem.SampleProductUD + "-" + index.ToString("D2");
                            dbProductItem.CreatedDate         = DateTime.Now;
                            dbItem.SampleProductItem.Add(dbProductItem);
                        }

                        //// notification
                        //SendNotification(context);

                        context.SaveChanges();

                        // add item to quotation if needed
                        context.FW_Quotation_function_AddSampleItem(null, id); // table lockx and also check if item is available on sql server side

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Exemple #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.ProductionItem dtoProductionItem = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductionItem>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            string countryCode = null;
            int    number;
            string indexName;
            bool   permissionApprove = false;

            if (fwBll.CanPerformAction(userId, moduleCode, Library.DTO.ModuleAction.CanApprove))
            {
                permissionApprove = true;
            }
            try
            {
                using (ProductionItemEntities context = CreateContext())
                {
                    ProductionItem dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new ProductionItem();
                        context.ProductionItem.Add(dbItem);
                        // Auto generate code
                        switch (dtoProductionItem.CountryID)
                        {
                        case 1:
                            countryCode = "VN";
                            break;

                        case 2:
                            countryCode = "IN";
                            break;

                        case 3:
                            countryCode = "HL";
                            break;
                        }

                        if (dtoProductionItem.ProductionItemUD == null)
                        {
                            dtoProductionItem.ProductionItemUD = context.ProductionItemMng_function_GenerateItemCode(dtoProductionItem.CountryID, countryCode, dtoProductionItem.ProductionItemGroupID, dtoProductionItem.ProductionItemMaterialTypeID, dtoProductionItem.ProductionItemTypeID).FirstOrDefault();

                            // Check length of production item code enough 12 characters
                            if (dtoProductionItem.ProductionItemUD.Length < 12)
                            {
                                notification.Type    = Library.DTO.NotificationType.Error;
                                notification.Message = "Create Production Item is error. Please connect to Mr. Hanh to help!";

                                return(false);
                            }
                        }
                    }
                    else
                    {
                        dbItem = context.ProductionItem.FirstOrDefault(o => o.ProductionItemID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Production Item not found!";
                        return(false);
                    }
                    else
                    {
                        // processing image
                        if (dtoProductionItem.FileLocation_HasChange)
                        {
                            dtoProductionItem.ProductImage = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoProductionItem.FileLocation_NewFile, dtoProductionItem.ProductImage);
                        }

                        //only one Defaut warehouse accepted
                        int c = dtoProductionItem.ProductionItemWarehouses.Where(o => o.IsDefault.Equals(true)).Count();
                        if (c == 0 && dtoProductionItem.ProductionItemTypeID != 7)
                        {
                            throw new System.Data.DataException("Please select at leat one default warehouse!");
                        }
                        //if (c > 1)
                        //{
                        //    throw new System.Data.DataException("Only one warehouse can be a default!");
                        //}

                        //only one Defaut vender accepted
                        int v = dtoProductionItem.ProductionItemVenders.Where(o => o.IsDefault.Equals(true)).Count();
                        if (v == 0)
                        {
                            //throw new System.Data.DataException("Please select at leat one default vender!");
                        }
                        if (v > 1)
                        {
                            throw new System.Data.DataException("Only one vender can be default option");
                        }

                        foreach (var item in dtoProductionItem.productionItemSubUnitDTOs)
                        {
                            if (!item.ConversionFactor.HasValue)
                            {
                                throw new Exception("You need fill-in conversion factory for unit");
                            }
                            if (item.ValidFrom == null || item.ValidFrom.Equals(""))
                            {
                                throw new Exception("You need fill-in validDate for sub unit");
                            }

                            if (item.ProductionItemUnitID > 0)
                            {
                                if (permissionApprove)
                                {
                                    var    getInforItem = context.ProductionItemUnit.FirstOrDefault(o => o.ProductionItemUnitID == item.ProductionItemUnitID);
                                    string validDateStr = getInforItem.ValidFrom != null?getInforItem.ValidFrom.Value.ToString("dd/MM/yyyy") : "";

                                    if (item.ConversionFactor != getInforItem.ConversionFactor || item.ValidFrom != validDateStr)
                                    {
                                        ProductionItemUnitHistory productionItemUnitHistory;
                                        productionItemUnitHistory = new ProductionItemUnitHistory();
                                        //Get data;
                                        ProductionItemUnit xxx = dbItem.ProductionItemUnit.FirstOrDefault(o => o.ProductionItemUnitID == item.ProductionItemUnitID);
                                        xxx.ProductionItemUnitHistory.Add(productionItemUnitHistory);
                                        //Mapping Data
                                        productionItemUnitHistory.Remark           = getInforItem.Remark != null ?getInforItem.Remark : item.Remark;
                                        productionItemUnitHistory.UpdateDate       = DateTime.Now;
                                        productionItemUnitHistory.UpdatedBy        = userId;
                                        productionItemUnitHistory.HsqdHistory      = getInforItem.ConversionFactor != null ? getInforItem.ConversionFactor : item.ConversionFactor;
                                        productionItemUnitHistory.ValidFromHistory = getInforItem.ValidFrom != null ? getInforItem.ValidFrom : item.ValidFrom.ConvertStringToDateTime();
                                    }
                                }
                            }
                        }

                        converter.DTO2BD(dtoProductionItem, ref dbItem, permissionApprove, userId);

                        //update hidden data
                        int?companyID = fwFactory.GetCompanyID(userId);
                        dbItem.CompanyID = companyID;

                        // ProductionItemWarehouse
                        context.ProductionItemWarehouse.Local.Where(o => o.ProductionItem == null).ToList().ForEach(o => context.ProductionItemWarehouse.Remove(o));
                        // ProductionItemWarehouse
                        context.ProductionItemVender.Local.Where(o => o.ProductionItem == null).ToList().ForEach(o => context.ProductionItemVender.Remove(o));
                        //ProductionItemUnit
                        context.ProductionItemUnit.Local.Where(o => o.ProductionItem == null).ToList().ForEach(o => context.ProductionItemUnit.Remove(o));

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

                        // Update by hand. (unneccessary)
                        dbItem.CountryID = dtoProductionItem.CountryID;
                        dbItem.ProductionItemMaterialTypeID = dtoProductionItem.ProductionItemMaterialTypeID;
                        dbItem.ProductionItemTypeID         = dtoProductionItem.ProductionItemTypeID;

                        context.SaveChanges();

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

                        return(true);
                    }
                }
            }

            catch (System.Data.DataException dEx)
            {
                notification.Type = Library.DTO.NotificationType.Error;
                Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName);
                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    switch (indexName)
                    {
                    case "ProductItemFactoryWarehouseUnique":
                        notification.Message = "Duplicate Factory Warehouse ";
                        break;

                    case "IX_ProductionItemVender_FactoryRawMaterial":
                        notification.Message = "Duplicate Vender";
                        break;
                    }
                }
                else
                {
                    notification.Message = dEx.Message;
                }
                return(false);
            }
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.Factory dtoFactory = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.Factory>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (FactoryMng2Entities context = CreateContext())
                {
                    Factory dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new Factory();
                        context.Factory.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.Factory.FirstOrDefault(o => o.FactoryID == id);
                    }

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

                        // process image
                        foreach (DTO.FactoryImage dtoImage in dtoFactory.FactoryImages)
                        {
                            if (dtoImage.HasChange)
                            {
                                dtoImage.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoImage.NewFile, dtoImage.FileUD);
                            }
                        }

                        // Process business card image
                        foreach (DTO.FactoryBusinessCard dtoCard in dtoFactory.FactoryBusinessCard)
                        {
                            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 certificate file
                        foreach (DTO.FactoryCertificate dtoCertificate in dtoFactory.FactoryCertificates)
                        {
                            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);
                                }
                            }
                        }

                        // remove Technical Image
                        foreach (FactoryImage dbImage in context.FactoryImage.Local.Where(o => o.Factory == null).ToList())
                        {
                            if (!string.IsNullOrEmpty(dbImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbImage.FileUD);
                            }
                        }

                        // processing image
                        if (dtoFactory.LogoImage_HasChange)
                        {
                            dtoFactory.LogoImage = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoFactory.LogoImage_NewFile, dtoFactory.LogoImage);
                        }

                        if (dtoFactory.FactoryGalleries != null)
                        {
                            // Pre-event update FactoryGallery
                            foreach (DTO.FactoryGalleryDTO dtoFactoryGallery in dtoFactory.FactoryGalleries.Where(o => o.FactoryGalleryHasChange))
                            {
                                dtoFactoryGallery.FactoryGalleryUD = fwFactory.CreateNoneImageFilePointer(this._tempFolder, dtoFactoryGallery.FactoryGalleryNewFile, dtoFactoryGallery.FactoryGalleryUD);
                            }
                        }

                        //foreach (DTO.FactoryDocumentDTO dtoDocumentFile in dtoFactory.factoryDocuments)
                        //{
                        //    if (dtoDocumentFile.FactoryDocumentHasChange)
                        //    {
                        //        if (string.IsNullOrEmpty(dtoDocumentFile.FactoryDocumentNewFile))
                        //        {
                        //            fwFactory.RemoveFile(dtoDocumentFile.FactoryDocumentFile);
                        //        }
                        //        else
                        //        {
                        //            dtoDocumentFile.FactoryDocumentFile = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDocumentFile.FactoryDocumentNewFile, dtoDocumentFile.FactoryDocumentFile, dtoDocumentFile.FriendlyName);
                        //        }
                        //    }
                        //}

                        converter.DTO2BD(dtoFactory, ref dbItem, userId);

                        //
                        //remove orphan item
                        //

                        // FactoryImage
                        context.FactoryImage.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryImage.Remove(o));

                        // FactoryBusinessCard
                        context.FactoryBusinessCard.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryBusinessCard.Remove(o));

                        // FactoryDirector
                        context.FactoryDirector.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryDirector.Remove(o));

                        // FactoryManager
                        context.FactoryManager.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryManager.Remove(o));

                        // FactoryPricing
                        context.FactoryPricing.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryPricing.Remove(o));

                        // FactoryResponsiblePerson
                        context.FactoryResponsiblePerson.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryResponsiblePerson.Remove(o));

                        // SampleTechnical
                        context.FactorySampleTechnical.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactorySampleTechnical.Remove(o));

                        // FactorySupplier
                        //context.FactoryRawMaterialSupplier.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryRawMaterialSupplier.Remove(o));
                        context.FactorySupplier.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactorySupplier.Remove(o));

                        // FactoryInHouseTest
                        context.FactoryInHouseTest.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryInHouseTest.Remove(o));

                        // FactoryCertificate
                        context.FactoryCertificate.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryCertificate.Remove(o));

                        //FactoryCapacity By Weeks
                        context.FactoryCapacity.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryCapacity.Remove(o));

                        //FactoryContactQuickInfo
                        context.FactoryContactQuickInfo.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryContactQuickInfo.Remove(o));

                        //FactoryProductGroup
                        context.FactoryProductGroup.Local.Where(o => o.Factory == null).ToList().ForEach(o => context.FactoryProductGroup.Remove(o));

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

                        // FactoryGallery for Factory(remove value Factory is null)
                        foreach (FactoryGallery dbFactoryGallery in context.FactoryGallery.Where(o => o.Factory == null).ToList())
                        {
                            fwFactory.RemoveImageFile(dbFactoryGallery.FactoryGalleryUD);
                            context.FactoryGallery.Remove(dbFactoryGallery);
                        }

                        context.SaveChanges();

                        //Update Permission Factory
                        if (id == 0)
                        {
                            context.Factory2Mng_function_AddFactoryPermissionNew(dbItem.FactoryID);
                            dtoItem = GetData(userId, dbItem.FactoryID, 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 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);
            }
        }
Exemple #5
0
        public void DTO2DB_ProductBreakDownPAL2(DTO.ProductBreakDownPALData dtoItem, ref ProductBreakDownPAL dbItem, string tempFile, int userId)
        {
            AutoMapper.Mapper.Map <DTO.ProductBreakDownPALData, ProductBreakDownPAL>(dtoItem, dbItem);

            foreach (var dbCategory in dbItem.ProductBreakDownCategoryPAL.ToArray())
            {
                if (!dtoItem.ProductBreakDownCategoryPAL.Select(o => o.ProductBreakDownCategoryID).Contains(dbCategory.ProductBreakDownCategoryID))
                {
                    foreach (var dbCategoryImage in dbCategory.ProductBreakDownCategoryImagePAL.ToArray())
                    {
                        if (!string.IsNullOrEmpty(dbCategoryImage.FileUD))
                        {
                            fwFactory.RemoveImageFile(dbCategoryImage.FileUD);
                        }

                        dbCategory.ProductBreakDownCategoryImagePAL.Remove(dbCategoryImage);
                    }

                    foreach (var dbCategoryType in dbCategory.ProductBreakDownCategoryTypePAL.ToArray())
                    {
                        foreach (var dbDetail in dbCategoryType.ProductBreakDownDetailPAL.ToArray())
                        {
                            dbCategoryType.ProductBreakDownDetailPAL.Remove(dbDetail);
                        }

                        dbCategory.ProductBreakDownCategoryTypePAL.Remove(dbCategoryType);
                    }

                    dbItem.ProductBreakDownCategoryPAL.Remove(dbCategory);
                }
            }

            foreach (var dtoCategory in dtoItem.ProductBreakDownCategoryPAL)
            {
                if (string.IsNullOrEmpty(dtoCategory.ProductBreakDownCategoryNM))
                {
                    continue;
                }

                if (dtoCategory.ProductBreakDownCalculationTypeID == 1 && dtoCategory.UnitPrice == 0)
                {
                    continue;
                }

                ProductBreakDownCategoryPAL dbCategory;

                if (dtoCategory.ProductBreakDownCategoryID < 0)
                {
                    dbCategory = new ProductBreakDownCategoryPAL();
                    dbItem.ProductBreakDownCategoryPAL.Add(dbCategory);
                }
                else
                {
                    dbCategory = dbItem.ProductBreakDownCategoryPAL.FirstOrDefault(o => o.ProductBreakDownCategoryID == dtoCategory.ProductBreakDownCategoryID);
                }

                if (dbCategory != null)
                {
                    AutoMapper.Mapper.Map <DTO.ProductBreakDownCategoryPALData, ProductBreakDownCategoryPAL>(dtoCategory, dbCategory);

                    foreach (var dbCategoryImage in dbCategory.ProductBreakDownCategoryImagePAL.ToArray())
                    {
                        if (!dtoCategory.ProductBreakDownCategoryImagePAL.Select(o => o.ProductBreakDownCategoryImageID).Contains(dbCategoryImage.ProductBreakDownCategoryImageID))
                        {
                            if (!string.IsNullOrEmpty(dbCategoryImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbCategoryImage.FileUD);
                            }
                            dbCategory.ProductBreakDownCategoryImagePAL.Remove(dbCategoryImage);
                        }
                    }

                    foreach (var dbCategoryType in dbCategory.ProductBreakDownCategoryTypePAL.ToArray())
                    {
                        if (!dtoCategory.ProductBreakDownCategoryTypePAL.Select(o => o.ProductBreakDownCategoryTypeID).Contains(dbCategoryType.ProductBreakDownCategoryTypeID))
                        {
                            foreach (var dbDetail in dbCategoryType.ProductBreakDownDetailPAL.ToArray())
                            {
                                dbCategoryType.ProductBreakDownDetailPAL.Remove(dbDetail);
                            }

                            dbCategory.ProductBreakDownCategoryTypePAL.Remove(dbCategoryType);
                        }
                    }

                    foreach (var dtoCategoryImage in dtoCategory.ProductBreakDownCategoryImagePAL)
                    {
                        ProductBreakDownCategoryImagePAL dbCategoryImage;

                        if (dtoCategoryImage.ProductBreakDownCategoryImageID < 0)
                        {
                            dbCategoryImage = new ProductBreakDownCategoryImagePAL();
                            dbCategory.ProductBreakDownCategoryImagePAL.Add(dbCategoryImage);
                        }
                        else
                        {
                            dbCategoryImage = dbCategory.ProductBreakDownCategoryImagePAL.FirstOrDefault(o => o.ProductBreakDownCategoryImageID == dtoCategoryImage.ProductBreakDownCategoryImageID);
                        }

                        if (dbCategoryImage != null)
                        {
                            if (dtoCategoryImage.HasChange.HasValue && dtoCategoryImage.HasChange.Value)
                            {
                                dbCategoryImage.FileUD = fwFactory.CreateFilePointer(tempFile, dtoCategoryImage.NewFile, dtoCategoryImage.FileUD, dtoCategoryImage.FriendlyName);
                            }

                            AutoMapper.Mapper.Map <DTO.ProductBreakDownCategoryImagePALData, ProductBreakDownCategoryImagePAL>(dtoCategoryImage, dbCategoryImage);
                        }
                    }

                    foreach (var dtoCategoryType in dtoCategory.ProductBreakDownCategoryTypePAL)
                    {
                        ProductBreakDownCategoryTypePAL dbCategoryType;

                        if (dtoCategoryType.ProductBreakDownCategoryTypeID < 0)
                        {
                            dbCategoryType = new ProductBreakDownCategoryTypePAL();
                            dbCategory.ProductBreakDownCategoryTypePAL.Add(dbCategoryType);
                        }
                        else
                        {
                            dbCategoryType = dbCategory.ProductBreakDownCategoryTypePAL.FirstOrDefault(o => o.ProductBreakDownCategoryTypeID == dtoCategoryType.ProductBreakDownCategoryTypeID);
                        }

                        if (dbCategoryType != null)
                        {
                            AutoMapper.Mapper.Map <DTO.ProductBreakDownCategoryTypePALData, ProductBreakDownCategoryTypePAL>(dtoCategoryType, dbCategoryType);

                            foreach (var dbDetail in dbCategoryType.ProductBreakDownDetailPAL.ToArray())
                            {
                                if (!dtoCategoryType.ProductBreakDownDetailPAL.Select(s => s.ProductBreakDownDetailID).Contains(dbDetail.ProductBreakDownDetailID))
                                {
                                    dbCategoryType.ProductBreakDownDetailPAL.Remove(dbDetail);
                                }
                            }

                            foreach (var dtoDetail in dtoCategoryType.ProductBreakDownDetailPAL)
                            {
                                ProductBreakDownDetailPAL dbDetail;

                                if (dtoDetail.ProductBreakDownDetailID <= 0)
                                {
                                    dbDetail = new ProductBreakDownDetailPAL();
                                    dbCategoryType.ProductBreakDownDetailPAL.Add(dbDetail);
                                }
                                else
                                {
                                    dbDetail = dbCategoryType.ProductBreakDownDetailPAL.FirstOrDefault(o => o.ProductBreakDownDetailID == dtoDetail.ProductBreakDownDetailID);
                                }

                                if (dbDetail != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.ProductBreakDownDetailPALData, ProductBreakDownDetailPAL>(dtoDetail, dbDetail);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #6
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;

            try
            {
                CompanyDTO dtoCompany = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <CompanyDTO>();

                using (CompanyBranchMngEntities context = CreateContext())
                {
                    Company dbCompany;

                    if (id > 0)
                    {
                        dbCompany = context.Company.FirstOrDefault(o => o.CompanyID == id);
                    }
                    else
                    {
                        dbCompany = new Company();
                        context.Company.Add(dbCompany);
                    }

                    if (dbCompany == null)
                    {
                        notification.Type    = NotificationType.Error;
                        notification.Message = "Can not find data";

                        return(false);
                    }

                    if (dtoCompany.HasChanged)
                    {
                        Framework.DAL.DataFactory framworkFactory = new Framework.DAL.DataFactory();
                        dtoCompany.Logo = framworkFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoCompany.NewFile, dtoCompany.Logo);
                    }

                    dataConverter.DTO2DB_Company(dtoCompany, ref dbCompany);

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

                    context.Branch.Local.Where(o => o.Company == null).ToList().ForEach(o => context.Branch.Remove(o));
                    context.SaveChanges();

                    // refresh cache
                    Library.CacheHelper.ClearCache("SUPPORT_INTERNAL_COMPANY");

                    dtoItem = GetData(userId, dbCompany.CompanyID, new Hashtable(), out notification);
                }

                return(true);
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;

                return(false);
            }
        }
Exemple #7
0
        public void DTO2DB_QualityInspection(DTO.QualityInspectionData dtoItem, ref QualityInspection dbItem, string templateFile, int userID)
        {
            Mapper.Map <DTO.QualityInspectionData, QualityInspection>(dtoItem, dbItem);

            dbItem.WorkCenterWorkingDate = dtoItem.WorkCenterWorkingDate.ConvertStringToDateTime();
            dbItem.InspectionDate        = dtoItem.InspectionDate.ConvertStringToDateTime();
            dbItem.ReceivedDate          = dtoItem.ReceivedDate.ConvertStringToDateTime();

            foreach (var dbItemCategory in dbItem.QualityInspectionCategory.ToArray())
            {
                if (!dtoItem.QualityInspectionCategories.Select(o => o.QualityInspectionCategoryID).Contains(dbItemCategory.QualityInspectionCategoryID))
                {
                    foreach (var dbItemCategoryImage in dbItemCategory.QualityInspectionCategoryImage.ToArray())
                    {
                        if (!string.IsNullOrEmpty(dbItemCategoryImage.FileUD))
                        {
                            fwFactory.RemoveImageFile(dbItemCategoryImage.FileUD);
                        }

                        dbItemCategory.QualityInspectionCategoryImage.Remove(dbItemCategoryImage);
                    }

                    dbItem.QualityInspectionCategory.Remove(dbItemCategory);
                }
            }

            foreach (var dtoItemCategory in dtoItem.QualityInspectionCategories)
            {
                QualityInspectionCategory dbItemCategory;

                if (dtoItemCategory.QualityInspectionCategoryID < 0)
                {
                    dbItemCategory = new QualityInspectionCategory();
                    dbItem.QualityInspectionCategory.Add(dbItemCategory);
                }
                else
                {
                    dbItemCategory = dbItem.QualityInspectionCategory.FirstOrDefault(o => o.QualityInspectionCategoryID == dtoItemCategory.QualityInspectionCategoryID);
                }

                if (dbItemCategory != null)
                {
                    Mapper.Map <DTO.QualityInspectionCategoryData, QualityInspectionCategory>(dtoItemCategory, dbItemCategory);

                    foreach (var dbItemCategoryImage in dbItemCategory.QualityInspectionCategoryImage.ToArray())
                    {
                        if (!dtoItemCategory.QualityInpsectionCategoryImages.Select(s => s.QualityInspectionCategoryImageID).Contains(dbItemCategoryImage.QualityInspectionCategoryImageID))
                        {
                            if (!string.IsNullOrEmpty(dbItemCategoryImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbItemCategoryImage.FileUD);
                            }

                            dbItemCategory.QualityInspectionCategoryImage.Remove(dbItemCategoryImage);
                        }
                    }

                    foreach (var dtoItemCategoryImage in dtoItemCategory.QualityInpsectionCategoryImages)
                    {
                        QualityInspectionCategoryImage dbItemCategoryImage;

                        if (dtoItemCategoryImage.QualityInspectionCategoryImageID < 0)
                        {
                            dbItemCategoryImage = new QualityInspectionCategoryImage();
                            dbItemCategory.QualityInspectionCategoryImage.Add(dbItemCategoryImage);
                        }
                        else
                        {
                            dbItemCategoryImage = dbItemCategory.QualityInspectionCategoryImage.FirstOrDefault(o => o.QualityInspectionCategoryImageID == dtoItemCategoryImage.QualityInspectionCategoryImageID);
                        }

                        if (dbItemCategoryImage != null)
                        {
                            Mapper.Map <DTO.QualityInspectionCategoryImageData, QualityInspectionCategoryImage>(dtoItemCategoryImage, dbItemCategoryImage);

                            if (dtoItemCategoryImage.HasChange.HasValue && dtoItemCategoryImage.HasChange.Value)
                            {
                                dbItemCategoryImage.FileUD = fwFactory.CreateFilePointer(templateFile, dtoItemCategoryImage.NewFile, dtoItemCategoryImage.FileUD, dtoItemCategoryImage.FriendlyName);
                            }
                        }
                    }
                }
            }
        }
Exemple #8
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            CashBookData dataItem = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <CashBookData>();

            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            try
            {
                using (var context = CreateContext())
                {
                    CashBook dbItem;

                    if (id > 0)
                    {
                        dbItem = context.CashBook.FirstOrDefault(o => o.CashBookID == id);

                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not found data!";

                            return(false);
                        }
                    }
                    else
                    {
                        dbItem = context.CashBook.FirstOrDefault(o => o.CashBookUD == dataItem.CashBookUD);

                        if (dbItem != null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "CashBook receipt is exist in database!";

                            return(false);
                        }

                        DateTime bookDate = new DateTime();

                        if (!string.IsNullOrEmpty(dataItem.BookDate))
                        {
                            if (DateTime.TryParse(dataItem.BookDate, nl, System.Globalization.DateTimeStyles.None, out DateTime tmpDate))
                            {
                                bookDate = tmpDate;
                            }
                        }

                        var cashBalance = context.CashBookBalanceMng_CashBookBalance_View.OrderByDescending(o => o.UpdatedDate).FirstOrDefault();

                        if (!IsValidCreateCashBook(bookDate, cashBalance, out string messageError, out NotificationType typeError))
                        {
                            notification.Type    = typeError;
                            notification.Message = messageError;

                            return(false);
                        }

                        dbItem = new CashBook();
                        context.CashBook.Add(dbItem);
                    }

                    foreach (CashBookDocumentData item in dataItem.CashBookDocuments)
                    {
                        Module.Framework.DAL.DataFactory factory = new Framework.DAL.DataFactory();

                        if (item.HasChange)
                        {
                            if (string.IsNullOrEmpty(item.NewFile))
                            {
                                factory.RemoveImageFile(item.FileUD);
                            }
                            else
                            {
                                item.FileUD = factory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", item.NewFile, item.FileUD);
                            }
                        }
                    }

                    converter.DTO2DB_CashBook(dataItem, ref dbItem);
                    dbItem.UpdatedBy   = userId;
                    dbItem.UpdatedDate = DateTime.Now;

                    context.CashBookDocument.Local.Where(o => o.CashBook == null).ToList().ForEach(o => context.CashBookDocument.Remove(o));

                    context.SaveChanges();

                    dtoItem = converter.DB2DTO_CashBook(context.CashBookMng_CashBook_View.FirstOrDefault(o => o.CashBookID == dbItem.CashBookID));
                }

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

                return(false);
            }
        }
        public bool UpdateData(int userID, int id, ref object viewItem, out Library.DTO.Notification notification)
        {
            notification      = new Library.DTO.Notification();
            notification.Type = Library.DTO.NotificationType.Success;

            DTO.BifaCompany dtoItem = ((Newtonsoft.Json.Linq.JObject)viewItem).ToObject <DTO.BifaCompany>();

            try
            {
                BifaCompany dbItem;

                using (var context = CreateContext())
                {
                    if (id == 0)
                    {
                        dbItem = new BifaCompany();
                        context.BifaCompany.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.BifaCompany.FirstOrDefault(o => o.BifaCompanyID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Type    = Library.DTO.NotificationType.Error;
                        notification.Message = "Can not find Bifa company!";

                        return(false);
                    }
                    else
                    {
                        // Process upload logo Bifa company
                        Framework.DAL.DataFactory fwFactory = new Framework.DAL.DataFactory();
                        if (dtoItem.Logo_HasChange)
                        {
                            if (string.IsNullOrEmpty(dtoItem.Logo_NewFile))
                            {
                                fwFactory.RemoveImageFile(dtoItem.Logo);
                            }
                            else
                            {
                                dtoItem.Logo = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userID.ToString() + @"\", dtoItem.Logo_NewFile, dtoItem.Logo);
                            }
                        }

                        converter.DTO2DB_BifaCompany(dtoItem, ref dbItem);
                        dbItem.UpdatedBy   = userID;
                        dbItem.UpdatedDate = System.DateTime.Now;

                        context.SaveChanges();

                        viewItem = GetData(dbItem.BifaCompanyID, out notification);

                        return(true);
                    }
                }
            }
            catch (System.Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Helper.GetInnerException(ex).Message;

                return(false);
            }
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactorySpecification dtoFactorySpecification = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactorySpecification>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (FactorySpecificationMngEntities context = CreateContext())
                {
                    FactorySpecification dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactorySpecification();
                        context.FactorySpecification.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactorySpecification.FirstOrDefault(o => o.FactorySpecificationID == 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(dtoFactorySpecification.ConcurrencyFlag_String)))
                        //{
                        //    throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        //}

                        // processing logo image
                        if (dtoFactorySpecification.FactoryFileLocation_HasChange)
                        {
                            dtoFactorySpecification.FactorySpecificationFileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoFactorySpecification.NewFile, dtoFactorySpecification.FactorySpecificationFileUD);
                        }

                        //convert dto to db
                        converter.DTO2BD(dtoFactorySpecification, ref dbItem);

                        context.FactorySpecificationComment.Local.Where(o => o.FactorySpecification == null).ToList().ForEach(o => context.FactorySpecificationComment.Remove(o));

                        dbItem.FactorySpecificationRemark = dtoFactorySpecification.FactorySpecificationRemark;

                        dbItem.FactorySpecificationUpdatedDate = DateTime.Now;
                        dbItem.FactorySpecificationUpdatedBy   = userId;

                        context.SaveChanges();

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

                        return(true);
                    }
                }
            }

            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Exemple #11
0
        public void DTO2DB_ClientComplaint(DTO.ClientComplaint dtoItem, ref ClientComplaint dbItem, int createdBy, string tempFolder)
        {
            // client complain items
            foreach (ClientComplaintItem dbComplaintItem in dbItem.ClientComplaintItems.ToArray())
            {
                if (!dtoItem.ClientComplaintItems.Select(o => o.ClientComplaintItemID).Contains(dbComplaintItem.ClientComplaintItemID))
                {
                    // remove image
                    foreach (ClientComplaintItemImage dbComplaintImage in dbComplaintItem.ClientComplaintItemImages.ToArray())
                    {
                        if (!string.IsNullOrEmpty(dbComplaintImage.ImageFile))
                        {
                            // remove image file
                            fwFactory.RemoveImageFile(dbComplaintImage.ImageFile);
                        }
                        dbComplaintItem.ClientComplaintItemImages.Remove(dbComplaintImage);
                    }
                    dbItem.ClientComplaintItems.Remove(dbComplaintItem);
                }
            }


            ////ClientComplaintItems
            //var willDeletedItems = new List<ClientComplaintItem>();
            //List<ClientComplaintItemImage> willDeletedItemImages;

            if (dtoItem.ClientComplaintItems != null && dtoItem.ClientComplaintItems.Count > 0)
            {
                ////CHECK
                //foreach (var dbComplaintItem in dbItem.ClientComplaintItems)
                //{
                //    //DB NOT EXIST IN DTO
                //    if (!dtoItem.ClientComplaintItems.Select(s => s.ClientComplaintItemID).Contains(dbComplaintItem.ClientComplaintItemID))
                //    {
                //        willDeletedItems.Add(dbComplaintItem);
                //    }
                //    else //DB IS EXIST IN DB
                //    {
                //        willDeletedItemImages = new List<ClientComplaintItemImage>();
                //        foreach (var dbComplaintItemImage in dbComplaintItem.ClientComplaintItemImages)
                //        {
                //            var clientComplaintItem = dtoItem.ClientComplaintItems
                //                .FirstOrDefault(o => o.ClientComplaintItemID == dbComplaintItem.ClientComplaintItemID);

                //            if (clientComplaintItem != null && !clientComplaintItem
                //                    .ClientComplaintItemImages.Select(x => x.ClientComplaintItemImageID)
                //                    .Contains(dbComplaintItemImage.ClientComplaintItemImageID))
                //            {
                //                willDeletedItemImages.Add(dbComplaintItemImage);
                //            }
                //        }

                //        foreach (var dbItemImage in willDeletedItemImages)
                //        {
                //            dbComplaintItem.ClientComplaintItemImages.Remove(dbItemImage);
                //        }
                //    }
                //}

                //// Delete Parent and child items
                //foreach (var dbParentItem in willDeletedItems)
                //{
                //    var deletedChildItems = dbParentItem.ClientComplaintItemImages.ToList();

                //    foreach (var item in deletedChildItems)
                //    {
                //        var clientComplaintItem = dbItem.ClientComplaintItems
                //            .FirstOrDefault(o => o.ClientComplaintItemID == dbParentItem.ClientComplaintItemID);

                //        if (clientComplaintItem != null)
                //            clientComplaintItem.ClientComplaintItemImages.Remove(item);
                //    }
                //    dbItem.ClientComplaintItems.Remove(dbParentItem);
                //}

                //MAP
                foreach (var dtoItemM in dtoItem.ClientComplaintItems)
                {
                    ClientComplaintItem      _dbItem;
                    ClientComplaintItemImage _dbItemImage;

                    if (dtoItemM.ClientComplaintItemID < 0)
                    {
                        _dbItem = new ClientComplaintItem();

                        if ((dtoItemM.ClientComplaintItemImages != null) && (dtoItemM.ClientComplaintItemImages.Count > 0))
                        {
                            foreach (var dtoItemImageM in dtoItemM.ClientComplaintItemImages)
                            {
                                // create new
                                if (dtoItemImageM.ImageFile_HasChange.HasValue && dtoItemImageM.ImageFile_HasChange.Value)
                                {
                                    dtoItemImageM.ImageFile = fwFactory.CreateFilePointer(tempFolder, dtoItemImageM.ImageFile_NewFile, dtoItemImageM.ImageFile);
                                }

                                _dbItemImage = new ClientComplaintItemImage();
                                _dbItem.ClientComplaintItemImages.Add(_dbItemImage);
                                AutoMapper.Mapper.Map <DTO.ClientComplaintItemImage, ClientComplaintItemImage>(dtoItemImageM, _dbItemImage);
                            }
                        }

                        dbItem.ClientComplaintItems.Add(_dbItem);
                    }
                    else
                    {
                        _dbItem = dbItem.ClientComplaintItems.FirstOrDefault(o => o.ClientComplaintItemID == dtoItemM.ClientComplaintItemID);

                        if (_dbItem != null &&
                            dtoItemM.ClientComplaintItemImages != null &&
                            dtoItemM.ClientComplaintItemImages.Count > 0)
                        {
                            foreach (var itemImage in dtoItemM.ClientComplaintItemImages)
                            {
                                //modify dto image field
                                if (itemImage.ImageFile_HasChange.HasValue && itemImage.ImageFile_HasChange.Value)
                                {
                                    itemImage.ImageFile = fwFactory.CreateFilePointer(tempFolder, itemImage.ImageFile_NewFile, itemImage.ImageFile);
                                }

                                if (itemImage.ClientComplaintItemImageID < 0)
                                {
                                    _dbItemImage = new ClientComplaintItemImage();
                                    _dbItem.ClientComplaintItemImages.Add(_dbItemImage);
                                }
                                else
                                {
                                    _dbItemImage = _dbItem.ClientComplaintItemImages
                                                   .FirstOrDefault(o => o.ClientComplaintItemImageID == itemImage.ClientComplaintItemImageID);
                                }

                                if (_dbItemImage != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.ClientComplaintItemImage, ClientComplaintItemImage>(itemImage, _dbItemImage);
                                }
                            }
                        }
                    }

                    if (_dbItem != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ClientComplaintItem, ClientComplaintItem>(dtoItemM, _dbItem);
                    }
                }
            }

            // ClientComplaintUsers
            if (dtoItem.ClientComplaintUsers != null && dtoItem.ClientComplaintUsers.Count > 0)
            {
                foreach (var item in dbItem.ClientComplaintUsers.ToArray())
                {
                    if (!dtoItem.ClientComplaintUsers.Select(o => o.ClientComplaintUserID).Contains(item.ClientComplaintUserID))
                    {
                        dbItem.ClientComplaintUsers.Remove(item);
                    }
                }

                //map child row
                foreach (var item in dtoItem.ClientComplaintUsers)
                {
                    ClientComplaintUser dbClientComplaintUser;
                    if (item.ClientComplaintUserID <= 0)
                    {
                        dbClientComplaintUser = new ClientComplaintUser();
                        dbItem.ClientComplaintUsers.Add(dbClientComplaintUser);
                    }
                    else
                    {
                        dbClientComplaintUser = dbItem.ClientComplaintUsers.FirstOrDefault(o => o.ClientComplaintUserID == item.ClientComplaintUserID);
                    }

                    if (dbClientComplaintUser != null)
                    {
                        Mapper.Map(item, dbClientComplaintUser);
                    }
                }
            }

            // ClientComplaintCommunications
            if (dtoItem.ClientComplaintCommunications != null && dtoItem.ClientComplaintCommunications.Count > 0)
            {
                foreach (var item in dbItem.ClientComplaintCommunications.ToArray())
                {
                    if (!dtoItem.ClientComplaintCommunications.Select(o => o.ClientComplaintCommunicationID).Contains(item.ClientComplaintCommunicationID))
                    {
                        dbItem.ClientComplaintCommunications.Remove(item);
                    }
                }

                //map child row
                foreach (var item in dtoItem.ClientComplaintCommunications)
                {
                    ClientComplaintCommunication dbClientComplaintCommunication;
                    if (item.ClientComplaintCommunicationID <= 0)
                    {
                        dbClientComplaintCommunication = new ClientComplaintCommunication();
                        dbItem.ClientComplaintCommunications.Add(dbClientComplaintCommunication);
                        dbClientComplaintCommunication.CreatedBy = createdBy;

                        if (DateTime.TryParse(item.CreatedDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                        {
                            dbClientComplaintCommunication.CreatedDate = tmpDate;
                        }
                    }
                    else
                    {
                        dbClientComplaintCommunication = dbItem.ClientComplaintCommunications.FirstOrDefault(o => o.ClientComplaintCommunicationID == item.ClientComplaintCommunicationID);
                    }

                    if (dbClientComplaintCommunication != null)
                    {
                        Mapper.Map(item, dbClientComplaintCommunication);
                    }
                }
            }

            Mapper.Map(dtoItem, dbItem);
            dbItem.UpdatedDate        = DateTime.Now;
            dbItem.DeliveryDateClient = dtoItem.DeliveryDateClient.ConvertStringToDateTime();
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.QCReport dtoQCReport = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.QCReport>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (QCReportMngEntities context = CreateContext())
                {
                    QCReport dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new QCReport();
                        context.QCReport.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.QCReport.FirstOrDefault(o => o.QCReportID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "QC Report not found!";
                        return(false);
                    }
                    else
                    {
                        // process image
                        foreach (DTO.QCReportImage dtoImage in dtoQCReport.QCReportImages)
                        {
                            if (dtoImage.HasChange)
                            {
                                dtoImage.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoImage.NewFile, dtoImage.FileUD);
                            }
                        }

                        // process qc report document
                        foreach (DTO.QCReportDocument dtoDocument in dtoQCReport.QCReportDocuments)
                        {
                            if (dtoDocument.QCReportDocument_HasChange)
                            {
                                dtoDocument.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDocument.QCReportDocument_NewFile, dtoDocument.FileUD);
                            }
                        }

                        // remove Technical Image
                        foreach (QCReportImage dbImage in context.QCReportImage.Local.Where(o => o.QCReport == null).ToList())
                        {
                            if (!string.IsNullOrEmpty(dbImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbImage.FileUD);
                            }
                        }

                        // remove Technical Document
                        foreach (QCReportDocument dbDocument in context.QCReportDocument.Local.Where(o => o.QCReport == null).ToList())
                        {
                            if (!string.IsNullOrEmpty(dbDocument.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbDocument.FileUD);
                            }
                        }

                        context.QCReportImage.Local.Where(o => o.QCReport == null).ToList().ForEach(o => context.QCReportImage.Remove(o));
                        context.QCReportDocument.Local.Where(o => o.QCReport == null).ToList().ForEach(o => context.QCReportDocument.Remove(o));

                        //convert dto to db
                        converter.DTO2DB(dtoQCReport, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        context.SaveChanges();
                        dtoItem = GetData(userId, dbItem.QCReportID, dbItem.FactoryOrderDetailID, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Exemple #13
0
        public bool UpdateData(int id, ref object dtoItem, int iRequesterID, out Library.DTO.Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;

            int    number;
            string indexName;

            DTO.CushionColor dtoCushionColor = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.CushionColor>();
            try
            {
                using (var context = CreateContext())
                {
                    CushionColor cushionColor = null;

                    if (id == 0)
                    {
                        dtoCushionColor.CreatedBy = iRequesterID;
                        cushionColor = new CushionColor();

                        context.CushionColor.Add(cushionColor);
                    }
                    else
                    {
                        var item = context.CushionColorMng_CushionColorCheck_View.Where(o => o.CushionColorID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't update because it used in item other!");
                        }
                        dtoCushionColor.UpdatedBy = iRequesterID;
                        cushionColor = context.CushionColor.FirstOrDefault(o => o.CushionColorID == id);
                    }

                    if (cushionColor == null)
                    {
                        notification.Message = "Cushion Color not found!";

                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (cushionColor.ConcurrencyFlag != null && !cushionColor.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoCushionColor.ConcurrencyFlag_String)))
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = Library.Helper.TEXT_CONCURRENCY_CONFLICT;

                            return(false);
                        }

                        converter.DTO2BD(dtoCushionColor, _TempFolder, ref cushionColor);

                        // processing image
                        if (dtoCushionColor.ImageFile_HasChange)
                        {
                            cushionColor.ImageFile = fwFactory.CreateFilePointer(this._TempFolder, dtoCushionColor.ImageFile_NewFile, dtoCushionColor.ImageFile);
                        }

                        // processing test report file 1
                        if (dtoCushionColor.TestReportFile_HasChange1)
                        {
                            cushionColor.TestReportFile1 = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoCushionColor.TestReportFile_NewFile1, dtoCushionColor.TestReportFile1);
                        }

                        // processing test report file 2
                        if (dtoCushionColor.TestReportFile_HasChange2)
                        {
                            cushionColor.TestReportFile2 = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoCushionColor.TestReportFile_NewFile2, dtoCushionColor.TestReportFile2);
                        }

                        // processing test report file 3
                        if (dtoCushionColor.TestReportFile_HasChange3)
                        {
                            cushionColor.TestReportFile3 = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoCushionColor.TestReportFile_NewFile3, dtoCushionColor.TestReportFile3);
                        }

                        if (id <= 0)
                        {
                            // Generate code.
                            using (var trans = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT * FROM CushionColor WITH (TABLOCKX, HOLDLOCK)");

                                try
                                {
                                    var newCode = context.CushionColorMng_function_GenerateCode().FirstOrDefault();

                                    if (!"**".Equals(newCode))
                                    {
                                        cushionColor.CushionColorUD = newCode;

                                        context.SaveChanges();
                                    }
                                    else
                                    {
                                        notification.Type    = NotificationType.Error;
                                        notification.Message = "Auto generated code exceed maximum option: [ZZ]";
                                    }
                                }
                                catch (Exception ex)
                                {
                                    trans.Rollback();
                                    throw ex;
                                }
                                finally
                                {
                                    trans.Commit();
                                }
                            }
                        }
                        else
                        {
                            context.CushionColorProductGroup.Local.Where(o => o.CushionColor == null).ToList().ForEach(o => context.CushionColorProductGroup.Remove(o));
                            context.CushionColorTestReport.Local.Where(o => o.CushionColor == null).ToList().ForEach(o => context.CushionColorTestReport.Remove(o));

                            context.SaveChanges();
                        }

                        // Handle notification missing information.
                        string emailSubject = (id == 0) ? "TASK REQUEST [CREATE CUSHION COLOR]" : "TASK REQUEST [UPDATE CUSHION COLOR]";
                        string emailBody    = string.Empty;

                        if (!IsNullPropertiesCushionColor(cushionColor, ref emailBody))
                        {
                            SendToEmailNotification(context, emailSubject, emailBody);
                        }

                        dtoItem = GetData(cushionColor.CushionColorID, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (DataException exData)
            {
                notification.Type = NotificationType.Error;
                ErrorHelper.DataExceptionParser(exData, out number, out indexName);

                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    if ("CushionColorUDUnique".Equals(indexName))
                    {
                        notification.Message = "The Cushion Color Code is already exists.";
                    }
                }
                else
                {
                    notification.Message = exData.Message;
                }

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

                return(false);
            }
            //notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success };
            //try
            //{
            //    using (CushionColorMngEntities context = CreateContext())
            //    {
            //        CushionColor dbItem = null;
            //        if (id == 0)
            //        {
            //            dbItem = new CushionColor();
            //            context.CushionColor.Add(dbItem);
            //        }
            //        else
            //        {
            //            dbItem = context.CushionColor.FirstOrDefault(o => o.CushionColorID == id);
            //        }

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

            //            converter.DTO2BD(dtoItem, ref dbItem);
            //            context.SaveChanges();

            //            // processing image
            //            if (dtoItem.ImageFile_HasChange)
            //            {
            //                dbItem.ImageFile = fwFactory.CreateFilePointer(this._TempFolder, dtoItem.ImageFile_NewFile, dtoItem.ImageFile);
            //            }

            //            // processing test report file 1
            //            if (dtoItem.TestReportFile_HasChange1)
            //            {
            //                dbItem.TestReportFile1 = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoItem.TestReportFile_NewFile1, dtoItem.TestReportFile1);
            //            }

            //            // processing test report file 2
            //            if (dtoItem.TestReportFile_HasChange2)
            //            {
            //                dbItem.TestReportFile2 = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoItem.TestReportFile_NewFile2, dtoItem.TestReportFile2);
            //            }

            //            // processing test report file 3
            //            if (dtoItem.TestReportFile_HasChange3)
            //            {
            //                dbItem.TestReportFile3 = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoItem.TestReportFile_NewFile3, dtoItem.TestReportFile3);
            //            }
            //            context.SaveChanges();

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

            //            return true;
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    notification.Type = Library.DTO.NotificationType.Error;
            //    notification.Message = ex.Message;
            //    return false;
            //}
        }
Exemple #14
0
        public void DTO2DB_BifaEvent(int userID, DTO.BifaEvent dtoItem, ref BifaEvent dbItem)
        {
            AutoMapper.Mapper.Map <DTO.BifaEvent, BifaEvent>(dtoItem, dbItem);

            dbItem.BifaCompanyID = dtoItem.BifaCompanyID;
            dbItem.BifaEventUD   = dtoItem.BifaEventUD;
            dbItem.StartDate     = dtoItem.StartDate.ConvertStringToDateTime();
            dbItem.EndDate       = dtoItem.EndDate.ConvertStringToDateTime();
            dbItem.UpdatedBy     = userID;
            dbItem.UpdatedDate   = System.DateTime.Now;

            if (dtoItem.BifaEventParticipants != null)
            {
                foreach (var item in dbItem.BifaEventParticipant.ToArray())
                {
                    if (!dtoItem.BifaEventParticipants.Select(s => s.BifaEventParticipantID).Contains(item.BifaEventParticipantID))
                    {
                        dbItem.BifaEventParticipant.Remove(item);
                    }
                }

                foreach (var item in dtoItem.BifaEventParticipants.ToArray())
                {
                    BifaEventParticipant dbItemDetail;

                    if (item.BifaEventParticipantID < 0)
                    {
                        dbItemDetail = new BifaEventParticipant();
                        dbItem.BifaEventParticipant.Add(dbItemDetail);
                    }
                    else
                    {
                        dbItemDetail = dbItem.BifaEventParticipant.FirstOrDefault(s => s.BifaEventParticipantID == item.BifaEventParticipantID);
                    }

                    if (dbItemDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.BifaEventParticipant, BifaEventParticipant>(item, dbItemDetail);

                        dbItemDetail.UpdatedBy   = userID;
                        dbItemDetail.UpdatedDate = System.DateTime.Now;
                    }
                }
            }

            if (dtoItem.BifaEventFiles != null)
            {
                Framework.DAL.DataFactory fwFactory = new Framework.DAL.DataFactory();

                foreach (var item in dbItem.BifaEventFile.ToList())
                {
                    if (!dtoItem.BifaEventFiles.Select(s => s.BifaEventFileID).Contains(item.BifaEventFileID))
                    {
                        dbItem.BifaEventFile.Remove(item);
                    }
                }

                foreach (var item in dtoItem.BifaEventFiles.ToList())
                {
                    BifaEventFile dbItemDetail;

                    if (item.BifaEventFileID < 0)
                    {
                        dbItemDetail = new BifaEventFile();
                        dbItem.BifaEventFile.Add(dbItemDetail);
                    }
                    else
                    {
                        dbItemDetail = dbItem.BifaEventFile.FirstOrDefault(s => s.BifaEventFileID == item.BifaEventFileID);
                    }

                    if (dbItemDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.BifaEventFile, BifaEventFile>(item, dbItemDetail);

                        if (item.HasChange)
                        {
                            if (string.IsNullOrEmpty(item.NewFile))
                            {
                                fwFactory.RemoveImageFile(item.FileUD);
                            }
                            else
                            {
                                dbItemDetail.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userID.ToString() + @"\", item.NewFile, item.FileUD);
                            }
                        }
                    }
                }
            }
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            notification = new Notification()
            {
                Type = NotificationType.Success
            };
            DTO.SpecificationOfProductDTO dtoItemSpec = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SpecificationOfProductDTO>();
            try
            {
                using (var context = CreateContext())
                {
                    ProductSpecification dbItem;
                    if (id == 0)
                    {
                        dbItem = new ProductSpecification();
                        context.ProductSpecification.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.ProductSpecification.Where(o => o.ProductSpecificationID == id).FirstOrDefault();
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "Data Not Found !";
                        return(false);
                    }
                    else
                    {
                        //Upload File
                        Module.Framework.DAL.DataFactory fwFactory = new Framework.DAL.DataFactory();
                        string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\";
                        //for SpecificationImage
                        foreach (DTO.SpecificationCushionImageDTO dtoSpecImage in dtoItemSpec.specificationCushionImageDTOs.Where(o => o.ScanHasChange))
                        {
                            dtoSpecImage.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoSpecImage.ScanNewFile, dtoSpecImage.FileUD, dtoSpecImage.FriendlyName);
                        }
                        //for SpecificationImage
                        foreach (DTO.SpecificationImageDTO dtoSpecImage in dtoItemSpec.specificationImageDTOs.Where(o => o.ScanHasChange))
                        {
                            dtoSpecImage.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoSpecImage.ScanNewFile, dtoSpecImage.FileUD, dtoSpecImage.FriendlyName);
                        }

                        //for SpecWeavingFile
                        foreach (DTO.SpecificationWeavingFileDTO dtoWeavingFile in dtoItemSpec.specificationWeavingFileDTOs.Where(o => o.ScanHasChange))
                        {
                            dtoWeavingFile.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoWeavingFile.ScanNewFile, dtoWeavingFile.FileUD, dtoWeavingFile.FriendlyName);
                        }

                        converter.DTO2DB_UpdateSpecificationOfProduct(dtoItemSpec, ref dbItem);

                        //Remove
                        context.ProductSpecificationCushionImage.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationCushionImage.Remove(o));
                        context.ProductSpecificationImage.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationImage.Remove(o));
                        context.ProductSpecificationPacking.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationPacking.Remove(o));
                        context.ProductSpecificationWeavingFile.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationWeavingFile.Remove(o));
                        context.ProductSpecificationWoodenPart.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationWoodenPart.Remove(o));

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

                        context.SaveChanges();

                        dtoItem = GetDataSpecification(userId, dbItem.ProductSpecificationID, dbItem.ProductID, null, out notification).Data;
                        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);
            }
        }