public void SaveImage(EditItemFinalViewModel model)
 {
     if (model.ImageFile != null)
     {
         Console.WriteLine(model.ImageFile);
         string fileName  = Path.GetFileNameWithoutExtension(model.ImageFile.FileName);
         string extension = Path.GetExtension(model.ImageFile.FileName);
         fileName        = model.ItemCode.ToString() + ".JPG";
         model.ImagePath = "~/Images/" + fileName;
         fileName        = Path.Combine(Server.MapPath("~/Images/") + fileName);
         model.ImageFile.SaveAs(fileName);
     }
 }
 public Boolean ProcessItemPrice(EditItemFinalViewModel model)
 {
     try
     {
         if (model.SupplierName1 != null && model.SupplierName1 != "0" && model.SupplierUnitPrice1 != 0)
         {
             ItemPrice p = new ItemPrice();
             p.ItemCode         = itemService.FindItemByItemCode(model.ItemCode).ItemCode;
             p.SupplierCode     = model.SupplierName1;
             p.PrioritySequence = 1;
             p.Price            = model.SupplierUnitPrice1;
             p.CreatedBy        = userService.FindUserByEmail(System.Web.HttpContext.Current.User.Identity.GetUserName());
             p.CreatedDateTime  = DateTime.Now;
             itemPriceService.Save(p);
         }
         if (model.SupplierName2 != null && model.SupplierName2 != "0" && model.SupplierUnitPrice2 != 0)
         {
             ItemPrice p = new ItemPrice();
             p.ItemCode         = itemService.FindItemByItemCode(model.ItemCode).ItemCode;
             p.SupplierCode     = model.SupplierName2;
             p.PrioritySequence = 2;
             p.Price            = model.SupplierUnitPrice2;
             p.CreatedBy        = userService.FindUserByEmail(System.Web.HttpContext.Current.User.Identity.GetUserName());
             p.CreatedDateTime  = DateTime.Now;
             itemPriceService.Save(p);
         }
         if (model.SupplierName3 != null && model.SupplierName3 != "0" && model.SupplierUnitPrice3 != 0)
         {
             ItemPrice p = new ItemPrice();
             p.ItemCode         = itemService.FindItemByItemCode(model.ItemCode).ItemCode;
             p.SupplierCode     = model.SupplierName3;
             p.PrioritySequence = 3;
             p.Price            = model.SupplierUnitPrice3;
             p.CreatedBy        = userService.FindUserByEmail(System.Web.HttpContext.Current.User.Identity.GetUserName());
             p.CreatedDateTime  = DateTime.Now;
             itemPriceService.Save(p);
         }
     }
     catch (Exception e)
     {
         string error = "error in item price save";
         Console.WriteLine("An error occurred in Item Price Save: '{0}'", e);
         return(false);
     }
     return(true);
 }
        public ActionResult Save(EditItemFinalViewModel model)
        {
            Console.WriteLine(model);
            SaveImage(model);
            string    error        = "";
            Item      newItem      = new Item();
            ItemPrice newItemPrice = new ItemPrice();

            if (model.ItemCode != null)
            {
                if (itemService.FindItemByItemCode(model.ItemCode) == null)
                {
                    //for inventory
                    int quantity = 0;
                    //new item
                    newItem.ItemCode        = model.ItemCode;
                    newItem.CreatedDateTime = DateTime.Now;
                    newItem.CreatedBy       = userService.FindUserByEmail(System.Web.HttpContext.Current.User.Identity.GetUserName());
                    newItem.Name            = model.ItemName;
                    newItem.Description     = model.Description;
                    newItem.Uom             = model.Uom;
                    newItem.ItemCategory    = categoryService.FindItemCategoryByItemCategoryId(model.CategoryId);
                    newItem.Bin             = model.Bin;
                    newItem.ReorderLevel    = model.ReorderLevel;
                    newItem.ReorderQuantity = model.ReorderQuantity;
                    newItem.Status          = new StatusService(context).FindStatusByStatusId(1);
                    try
                    {
                        itemService.Save(newItem, quantity);
                        ProcessItemPrice(model);
                    }
                    catch (Exception e)
                    {
                        error = "Error in item save";
                        Console.WriteLine("An error occurred in Item Save: '{0}'", e);
                    }
                }
                else
                {
                    //update exiting item
                    Item             current = itemService.FindItemByItemCode(model.ItemCode);
                    List <ItemPrice> k       = itemPriceService.FindItemPriceByItemCode(current.ItemCode);
                    foreach (ItemPrice i in k)
                    {
                        itemPriceService.DeleteItemPrice(i);
                    }
                    current.Description     = model.Description;
                    current.ReorderLevel    = model.ReorderLevel;
                    current.ReorderQuantity = model.ReorderQuantity;
                    current.Bin             = model.Bin;
                    current.Uom             = model.Uom;
                    current.Status          = new StatusService(context).FindStatusByStatusId(1);
                    current.UpdatedBy       = userService.FindUserByEmail(System.Web.HttpContext.Current.User.Identity.GetUserName());
                    current.UpdatedDateTime = DateTime.Now;
                    int quantity = current.Inventory.Quantity;
                    try
                    {
                        itemService.Save(current, quantity);
                        if (!ProcessItemPrice(model))
                        {
                            //write error case
                        }
                    }
                    catch (Exception e)
                    {
                        error = "Error in item update";
                        Console.WriteLine("An error occurred in Item Update: '{0}'", e);
                    }
                }
            }
            else
            {
                //show erro bcuz no item code
                error = "Item code comes as null";
            }

            return(RedirectToAction("Index", "Inventory"));
            //return new JsonResult { };
        }