Example #1
0
        public ActionResult Save(Product product, ProductInclusion productInclusion, ProductPrice productPrice, ProductViewModel model, HttpPostedFileBase file)
        {
            /*var decodedString = Convert.ToBase64String(product.ProductImage)
             *  .Replace("-", "");
             *
             * <<<<<<< HEAD
             * public ActionResult Save(Product product, ProductInclusion productInclusion, ProductPrice productPrice/*, Product model, HttpPostedFileBase file*/
            /* var decodedString = Convert.ToBase64String(product.ProductImage).Replace("-", "");
             * =======
             * /* var decodedString = Convert.ToBase64String(product.ProductImage).Replace("-", "");
             *
             * >>>>>>> cc56dde8a2f77b13a4350898b5c9c2864e2e3be7
             * var bytes = Convert.FromBase64String(decodedString);
             *
             * var encodedString = Encoding.UTF8.GetString(bytes);
             * Console.WriteLine(encodedString);*/

            var isProductExist = _context.Products.Count(c => c.Name == product.Name);



            /* if (!ModelState.IsValid)
             * {
             *   var viewModel = new ProductViewModel
             *   {
             *       Product = product,
             *       Brands = _context.Brands.ToList(),
             *       Branches = _context.Branches.ToList(),
             *       ProductCategories = _context.ProductCategories.ToList(),
             *       ProductConditions = _context.ProductConditions.ToList(),
             *   };
             *
             *   return View("New", viewModel);
             * }*/
            /*   else
             * {
             *     var viewModel = new ProductViewModel
             *     {
             *         Product = product,
             *         Brands = _context.Brands.ToList(),
             *         Branches = _context.Branches.ToList(),
             *         ProductCategories = _context.ProductCategories.ToList(),
             *         ProductConditions = _context.ProductConditions.ToList(),
             *     };
             *     return View("New", viewModel);
             * }*/



            if (product.Id == 0)
            {
                String fileName = "";


                if (isProductExist == 0)
                {
                    if (productInclusion.FreeItem != null &&
                        (productInclusion.Quantity != 0 || productInclusion.Quantity != null) && file != null)
                    {
                        /*model.ProductImage = new byte[file.ContentLength];
                         * file.InputStream.Read(model.ProductImage, 0, file.ContentLength);*/
                        fileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetExtension(file.FileName);
                        string physicalPath = Server.MapPath("~/Images/Uploads" + fileName);
                        // save image in folder
                        file.SaveAs(physicalPath);


                        productInclusion.ProductId = product.Id;
                        _context.ProductInclusions.Add(productInclusion);
                    }
                    /*_context.Products.Add(model);*/
                    productPrice.ProductId   = product.Id;
                    productPrice.DateCreated = DateTime.Now;
                    productPrice.UnitPrice   = product.OriginalPrice;
                    _context.ProductPrices.Add(productPrice);

                    // product.Image = sadjkladskjasd;
                    product.Quantity    = product.AvailableForSelling + product.Reserved;
                    product.DateCreated = DateTime.Now;
                    _context.Products.Add(product);
                }
                else
                {
                    var viewModel = new ProductViewModel
                    {
                        Product           = product,
                        Brands            = _context.Brands.ToList(),
                        Branches          = _context.Branches.ToList(),
                        ProductCategories = _context.ProductCategories.ToList(),
                        ProductConditions = _context.ProductConditions.ToList(),
                    };
                    return(View("New", viewModel));
                }
            }
            else
            {
                var productInDb = _context.Products.Single(p => p.Id == product.Id);
                productInDb.Name        = product.Name;
                productInDb.Description = product.Description;
                // product.Image = sdcnskdfjskdj;
                /*productInDb.LastChangedEmployee = DateTime.Now;*/
                productInDb.BranchId            = product.BranchId;
                productInDb.BrandId             = product.BrandId;
                productInDb.ProductCategoryId   = product.ProductCategoryId;
                productInDb.IsReturned          = product.IsReturned;
                productInDb.Model               = product.Model;
                productInDb.OriginalPrice       = product.OriginalPrice;
                productInDb.ProductConditionId  = product.ProductConditionId;
                productInDb.SerialNumber        = product.SerialNumber;
                productInDb.Quantity            = product.AvailableForSelling + product.Reserved;
                productInDb.WarrantyId          = product.WarrantyId;
                productInDb.AvailableForSelling = product.AvailableForSelling;
                productInDb.Reserved            = product.Reserved;


                var productInclusionInDb = _context.ProductInclusions.Single(p => p.ProductId == product.Id);
                if (productInclusionInDb.ProductId != 0)
                {
                    productInclusionInDb.FreeItem = productInclusion.FreeItem;
                    productInclusion.Quantity     = productInclusion.Quantity;
                }
            }



            _context.SaveChanges();
            Dispose();
            return(RedirectToAction("Index"));
        }
        public ActionResult Save(Product product, ProductInclusion productInclusion, ProductPrice productPrice)
        {
            var isProductExist = _context.Products.Count(c => c.Name == product.Name);



            /* if (!ModelState.IsValid)
             * {
             *   var viewModel = new ProductViewModel
             *   {
             *       Product = product,
             *       Brands = _context.Brands.ToList(),
             *       Branches = _context.Branches.ToList(),
             *       ProductCategories = _context.ProductCategories.ToList(),
             *       ProductConditions = _context.ProductConditions.ToList(),
             *   };
             *
             *   return View("New", viewModel);
             * }*/
            /*   else
             * {
             *     var viewModel = new ProductViewModel
             *     {
             *         Product = product,
             *         Brands = _context.Brands.ToList(),
             *         Branches = _context.Branches.ToList(),
             *         ProductCategories = _context.ProductCategories.ToList(),
             *         ProductConditions = _context.ProductConditions.ToList(),
             *     };
             *     return View("New", viewModel);
             * }*/



            if (product.Id == 0)
            {
                if (isProductExist == 0)
                {
                    if (productInclusion.FreeItem != null &&
                        (productInclusion.Quantity != 0 || productInclusion.Quantity != null))
                    {
                        productInclusion.ProductId = product.Id;
                        _context.ProductInclusions.Add(productInclusion);
                    }
                    productPrice.ProductId   = product.Id;
                    productPrice.DateCreated = DateTime.Now;
                    productPrice.UnitPrice   = product.OriginalPrice;
                    _context.ProductPrices.Add(productPrice);

                    // product.Image = sadjkladskjasd;
                    product.Quantity    = product.AvailableForSelling + product.Reserved;
                    product.DateCreated = DateTime.Now;
                    _context.Products.Add(product);
                }
                else
                {
                    var viewModel = new ProductViewModel
                    {
                        Product           = product,
                        Brands            = _context.Brands.ToList(),
                        Branches          = _context.Branches.ToList(),
                        ProductCategories = _context.ProductCategories.ToList(),
                        ProductConditions = _context.ProductConditions.ToList(),
                    };
                    return(View("New", viewModel));
                }
            }
            else
            {
                var productInDb = _context.Products.Single(p => p.Id == product.Id);
                productInDb.Name        = product.Name;
                productInDb.Description = product.Description;
                // product.Image = sdcnskdfjskdj;
                /*productInDb.LastChangedEmployee = DateTime.Now;*/
                productInDb.BranchId            = product.BranchId;
                productInDb.BrandId             = product.BrandId;
                productInDb.ProductCategoryId   = product.ProductCategoryId;
                productInDb.IsReturned          = product.IsReturned;
                productInDb.Model               = product.Model;
                productInDb.OriginalPrice       = product.OriginalPrice;
                productInDb.ProductConditionId  = product.ProductConditionId;
                productInDb.SerialNumber        = product.SerialNumber;
                productInDb.Quantity            = product.AvailableForSelling + product.Reserved;
                productInDb.WarrantyId          = product.WarrantyId;
                productInDb.AvailableForSelling = product.AvailableForSelling;
                productInDb.Reserved            = product.Reserved;


                var productInclusionInDb = _context.ProductInclusions.Single(p => p.ProductId == product.Id);
                if (productInclusionInDb.ProductId != 0)
                {
                    productInclusionInDb.FreeItem = productInclusion.FreeItem;
                    productInclusion.Quantity     = productInclusion.Quantity;
                }
            }



            _context.SaveChanges();
            Dispose();
            return(RedirectToAction("Index"));
        }