public void DeleteItemPriceTest()
        {
            //Arrange
            Item it = new Item
            {
                ItemCode        = "BBB",
                CreatedDateTime = DateTime.Now
            };

            new ItemRepository(context).Save(it);

            ItemPrice i = new ItemPrice
            {
                ItemCode         = it.ItemCode,
                SupplierCode     = "ALPA",
                PrioritySequence = 3,
                Price            = 20.30M,
                CreatedDateTime  = DateTime.Now
            };

            //Act
            var result1 = itemPriceService.Save(i);

            //Act2
            itemPriceService.DeleteItemPrice(i);

            //Arrange
            Assert.AreEqual(itemPriceService.FindItemPriceByItemCode(it.ItemCode).Count, 0);

            //clean
            //itemPriceRepository.Delete(i);
            itemRepository.Delete(it);
        }
        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 { };
        }