Exemplo n.º 1
0
        public void Save(EditReturnableProductViewModelIn vm)
        {
            if (vm.brandCode != null && vm.subBrandCode != null && vm.productTypeCode != null && vm.packTypeCode != null && vm.packCode != null && vm.vatClass != null)
            {
                ProductBrand pd = _productBrandRepository.GetAll().FirstOrDefault(n => n.Code == vm.brandCode);
                ProductFlavour pf = _productFlavourRepository.GetAll().FirstOrDefault(n => n.Code == vm.subBrandCode);
                ProductType pt = _productTypeRepository.GetAll().FirstOrDefault(n => n.Code == vm.productTypeCode);
                ProductPackaging ppg = _productPackagingRepository.GetAll().FirstOrDefault(n => n.Code == vm.packCode);
                ProductPackagingType ppgT = _productPackagingTypeRepository.GetAll().FirstOrDefault(n => n.Code == vm.packTypeCode);
                VATClass vc = _vatClassRepository.GetAll().FirstOrDefault(n => n.VatClass == vm.vatClass);
                ReturnableType rt = (ReturnableType)Enum.Parse(typeof(ReturnableType), vm.RetunableTypeName);

                ReturnableProduct rp = _productRepository.GetById(vm.Id) as ReturnableProduct;
                if (rp==null)
                    rp = _productFactory.CreateReturnableProduct(rp.Id);
                  
                rp.Brand = _productBrandRepository.GetById(pd.Id);
                rp.Description = vm.Description;
                rp.ExFactoryPrice = vm.ExFactoryPrice;
                rp.Packaging = _productPackagingRepository.GetById(ppg.Id);
                rp.PackagingType = _productPackagingTypeRepository.GetById(ppgT.Id);
                rp.ProductCode = vm.ProductCode;
                rp.Flavour = _productFlavourRepository.GetById(pf.Id);
                // = _productTypeRepository.GetById(pt.Id);
                rp.VATClass = _vatClassRepository.GetById(vc.Id);
                rp.ReturnableType = (ReturnableType)rt;
                rp.Capacity = vm.Capacity;
                _productRepository.Save(rp);
                if (vm.Id == Guid.Empty)
                {
                    addDefaultPriceForReturnable();
                }
            }
            else
            {
                ReturnableProduct rp = _productRepository.GetById(vm.Id) as ReturnableProduct;
                if (rp==null)
                    rp = _productFactory.CreateReturnableProduct(rp.Id);

              
                   
                rp.Brand = _productBrandRepository.GetById(vm.BrandId);
                rp.Flavour = _productFlavourRepository.GetById(vm.FlavourID);
                rp.Description = vm.Description;
                rp.Packaging = _productPackagingRepository.GetById(vm.PackagingId);
                rp.PackagingType = _productPackagingTypeRepository.GetById(vm.PackagingTypeId);
                rp.ProductCode = vm.ProductCode;
                rp.ReturnableType = (ReturnableType)vm.ReturnableType;
                rp.VATClass = _vatClassRepository.GetById(vm.VatClassId);
                rp.Capacity = vm.Capacity;
                if(rp.ReturnAbleProduct!=null)
                rp.ReturnAbleProduct = _productRepository.GetById(vm.ReturnableProductId.Value)as ReturnableProduct;

                _productRepository.Save(rp);
                if (vm.Id == Guid.Empty)
                {
                    addDefaultPriceForReturnable();
                }
            }
        }
Exemplo n.º 2
0
        public ActionResult ImportProduct(HttpPostedFileBase file)
        {

            try
            {

                var fileName = Path.GetFileName(file.FileName);


                var directory = Server.MapPath("~/Uploads");
                if (Directory.Exists(directory) == false)
                {
                    Directory.CreateDirectory(directory);
                }
                var path = Server.MapPath("~/Uploads") + "\\" + fileName;


                file.SaveAs(path);


                string fileExtension = Path.GetExtension(fileName);
                if (fileExtension == ".xlsx")
                {
                    ViewBag.msg = "Please wait. Upload in progress";

                    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;'";

                    OleDbConnection conn = new OleDbConnection(connectionString);
                    try
                    {
                        conn.Open();
                        string product = "";
                        OleDbCommand commProduct = new OleDbCommand("SELECT product FROM [Sheet1$]", conn);
                        OleDbDataReader readProduct = commProduct.ExecuteReader();
                        while (readProduct.Read())
                        {
                            product = readProduct["product"].ToString();
                        }
                        if (product == "Sale Product")
                        {
                            OleDbCommand command = new OleDbCommand("SELECT brandCode,subBrandCode,packagingtypeCode,packagingCode,productTypeCode,vatClass,code,description FROM [Sheet1$]", conn);
                            OleDbDataReader reader = command.ExecuteReader();
                            SaleProductViewModel pdvm = new SaleProductViewModel();
                            while (reader.Read())
                            {

                                string brandCode = reader["brandCode"].ToString();
                                string subBrandCode = reader["subBrandCode"].ToString();
                                string packagingTypeCode = reader["packagingTypeCode"].ToString();
                                string packagingCode = reader["packagingCode"].ToString();
                                string productTypeCode = reader["productTypeCode"].ToString();
                                string vatClass = reader["vatClass"].ToString();
                                string productCode = reader["code"].ToString();
                                string description = reader["description"].ToString();

                                pdvm.brandCode = brandCode;
                                pdvm.subBrandCode = subBrandCode;
                                pdvm.packTypeCode = packagingTypeCode;
                                pdvm.packCode = packagingCode;
                                pdvm.productTypeCode = productTypeCode;
                                pdvm.vatClass = vatClass;
                                pdvm.ProductCode = productCode;
                                pdvm.Description = description;
                                _productViewModelBuilder.SaveSaleProduct(pdvm);

                            }
                        }
                        else if (product == "Returnable Product")
                        {
                            OleDbCommand command = new OleDbCommand("SELECT Brand,SubBrand,PackagingType,Packaging,ProductType,VatClass,ProductCode,Description,returnableType FROM [Sheet1$]", conn);
                            OleDbDataReader reader = command.ExecuteReader();
                            EditReturnableProductViewModelIn pdvm = new EditReturnableProductViewModelIn();
                            while (reader.Read())
                            {

                                string brandCode = reader["brand"].ToString();
                                string subBrandCode = reader["subBrand"].ToString();
                                string packagingTypeCode = reader["packagingType"].ToString();
                                string packagingCode = reader["packaging"].ToString();
                                string productTypeCode = reader["productType"].ToString();
                                string vatClass = reader["vatClass"].ToString();
                                string productCode = reader["productcode"].ToString();
                                string description = reader["description"].ToString();
                                string returnableType = reader["returnableType"].ToString();

                                pdvm.brandCode = brandCode;
                                pdvm.subBrandCode = subBrandCode;
                                pdvm.packTypeCode = packagingTypeCode;
                                pdvm.packCode = packagingCode;
                                pdvm.productTypeCode = productTypeCode;
                                pdvm.vatClass = vatClass;
                                pdvm.ProductCode = productCode;
                                pdvm.Description = description;
                                pdvm.RetunableTypeName = returnableType;
                                _productViewModelBuilder.Save(pdvm);

                            }
                        }
                        else if (product == "Consolidated Product")
                        {
                            OleDbCommand command = new OleDbCommand("SELECT brandCode,packagingtypeCode,packagingCode,code,description FROM [Sheet1$]", conn);
                            OleDbDataReader reader = command.ExecuteReader();
                            EditConsolidatedProductOut vm = new EditConsolidatedProductOut();

                            string brandCode = reader["brandCode"].ToString();
                            string packagingTypeCode = reader["packagingTypeCode"].ToString();
                            string packagingCode = reader["packagingCode"].ToString();
                            string code = reader["Code"].ToString();
                            string description = reader["description"].ToString();

                            vm.brandCode = brandCode;
                            vm.packagingTypeCode = packagingTypeCode;
                            vm.packagingCode = packagingCode;
                            vm.ProductCode = code;
                            vm.Description = description;
                            _productViewModelBuilder.Save(vm);
                        }
                    }
                    catch (OleDbException ex)
                    {
                        ViewBag.msg = ex.ToString();
                        try
                        {
                            HQMailerViewModelBuilder hqm = new HQMailerViewModelBuilder(ConfigurationSettings.AppSettings["ServerIP"], ConfigurationSettings.AppSettings["UserName"], ConfigurationSettings.AppSettings["Password"]);


                            hqm.Send(ConfigurationSettings.AppSettings["ServerEmail"], ConfigurationSettings.AppSettings["MailGroup"], "Test", "editing sale product error:" + ex.Message);
                        }
                        catch (Exception exx)
                        { }
                        return View();
                    }

                    finally
                    {
                        conn.Close();

                    }

                    fi = new FileInfo(path);

                    fi.Delete();
                    _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Import", "Product", DateTime.Now);
                    ViewBag.msg = "Upload Successful";
                    return RedirectToAction("Index");
                }

                else
                {
                    fi = new FileInfo(path);

                    fi.Delete();
                    ViewBag.msg = "Please upload excel file with extension .xlsx";
                    return View();
                }
            }
            catch (Exception ex)
            {

                ViewBag.msg = ex.ToString();
                try
                {
                    HQMailerViewModelBuilder hqm = new HQMailerViewModelBuilder(ConfigurationSettings.AppSettings["ServerIP"], ConfigurationSettings.AppSettings["UserName"], ConfigurationSettings.AppSettings["Password"]);


                    hqm.Send(ConfigurationSettings.AppSettings["ServerEmail"], ConfigurationSettings.AppSettings["MailGroup"], "Test", "editing sale product error:" + ex.Message);
                }
                catch (Exception exx)
                { }
                return View();
            }


        }