private static Item getEntityByModel(ItemModel model)
        {
            if (model == null)
                return null;

            Item entity = new Item();

            if (model.Id == 0)
            {
                entity.CompanyId = AuthenticationHelper.CompanyId.Value;
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CreateDate = DateTime.Now;
            }
            else
            {
                entity.CompanyId = model.CompanyId;
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
            }

            entity.COGSCodeCombinationId = model.COGSCodeCombinationId;
            entity.DefaultBuyer = model.DefaultBuyer;
            entity.Description = model.Description;
            entity.Id = model.Id;
            entity.ItemName = model.ItemName;
            entity.LotControl = model.LotControl;
            entity.Orderable = model.Orderable;
            entity.Purchaseable = model.Purchaseable;
            entity.ReceiptRouting = model.ReceiptRouting;
            entity.SalesCodeCombinationId = model.SalesCodeCombinationId;
            entity.SerialControl = model.SerialControl;
            entity.Shipable = model.Shipable;
            entity.SOBId = model.SOBId;
            entity.Status = model.Status;
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;
            return entity;
        }
        public static void Save(ItemModel itemModel)
        {
            Item entity = getEntityByModel(itemModel);

            string result = string.Empty;
            if (entity.IsValid())
            {
                if (itemModel.Id > 0)
                    result = service.Update(entity);
                else
                    result = service.Insert(entity);

                if (!string.IsNullOrEmpty(result))
                {
                    var savedDetail = GetItemWarehouses(result);
                    if (savedDetail.Count() > itemModel.ItemWarehouses.Count())
                    {
                        var tobeDeleted = savedDetail.Take(savedDetail.Count() - itemModel.ItemWarehouses.Count());
                        foreach (var item in tobeDeleted)
                        {
                            service.DeleteItemWarehouse(item.Id);
                        }
                        savedDetail = GetItemWarehouses(result);
                    }

                    foreach (var detail in itemModel.ItemWarehouses)
                    {
                        ItemWarehouse detailEntity = getEntityByModel(detail, savedDetail.Count());
                        if (detailEntity.IsValid())
                        {
                            detailEntity.ItemId = Convert.ToInt64(result);
                            if (savedDetail.Count() > 0)
                            {
                                detailEntity.Id = savedDetail.FirstOrDefault().Id;
                                savedDetail.Remove(savedDetail.FirstOrDefault(rec => rec.Id == detailEntity.Id));
                                service.Update(detailEntity);
                            }
                            else
                                service.Insert(detailEntity);
                        }
                    }
                }
            }
        }
        public ActionResult Create()
        {
            ItemModel model = SessionHelper.Item;
            if (model == null)
            {
                model = new ItemModel
                {
                    SOBId = SessionHelper.SOBId
                };
                if (model.COGSCodeCombination == null)
                {
                    model.COGSCodeCombination = CodeCombinationHelper.GetCodeCombinations(SessionHelper.SOBId, AuthenticationHelper.CompanyId.Value)
                        .Select(x => new SelectListItem
                        {
                            Text = Utility.Stringize(".", x.Segment1, x.Segment2, x.Segment3, x.Segment4, x.Segment5, x.Segment6, x.Segment7, x.Segment8),
                            Value = x.Id.ToString()
                        }).ToList();
                    model.COGSCodeCombinationId = model.COGSCodeCombination.Any() ?
                        Convert.ToInt32(model.COGSCodeCombination.First().Value) : 0;
                }

                if (model.SalesCodeCombination == null)
                {
                    model.SalesCodeCombination = CodeCombinationHelper.GetCodeCombinations(SessionHelper.SOBId, AuthenticationHelper.CompanyId.Value)
                        .Select(x => new SelectListItem
                        {
                            Text = Utility.Stringize(".", x.Segment1, x.Segment2, x.Segment3, x.Segment4, x.Segment5, x.Segment6, x.Segment7, x.Segment8),
                            Value = x.Id.ToString()
                        }).ToList();
                    model.SalesCodeCombinationId = model.SalesCodeCombination.Any() ?
                        Convert.ToInt32(model.SalesCodeCombination.First().Value) : 0;
                }
                model.CompanyId = AuthenticationHelper.CompanyId.Value;
                ViewBag.SOBName = SetOfBookHelper.GetSetOfBook(SessionHelper.SOBId.ToString()).Name;

                SessionHelper.Item = model;
                if (SessionHelper.Item.ItemWarehouses == null)
                    SessionHelper.Item.ItemWarehouses = new List<ItemWarehouseModel>();
            }
            
            return View(model);
        }
 public static ItemModel GetItem(string id)
 {
     ItemModel model = new ItemModel(service.GetSingle(id, AuthenticationHelper.CompanyId.Value));
     return model;
 }
        public ActionResult Save(ItemModel model)
        {
            string message = "";
            try
            {
                bool saved = false;
                if (SessionHelper.Item != null)
                {
                    SessionHelper.Item.COGSCodeCombinationId = model.COGSCodeCombinationId;
                    SessionHelper.Item.CompanyId = model.CompanyId;
                    SessionHelper.Item.DefaultBuyer = model.DefaultBuyer;
                    SessionHelper.Item.Description = model.Description;
                    SessionHelper.Item.Id = model.Id;
                    SessionHelper.Item.ItemName = model.ItemName;
                    SessionHelper.Item.LotControl = model.LotControl;
                    SessionHelper.Item.Orderable = model.Orderable;
                    SessionHelper.Item.Purchaseable = model.Purchaseable;
                    SessionHelper.Item.ReceiptRouting = model.ReceiptRouting;
                    SessionHelper.Item.SalesCodeCombinationId = model.SalesCodeCombinationId;
                    SessionHelper.Item.SerialControl = model.SerialControl;
                    SessionHelper.Item.Shipable = model.Shipable;
                    SessionHelper.Item.SOBId = SessionHelper.SOBId;
                    SessionHelper.Item.Status = model.Status;

                    ItemHelper.Save(SessionHelper.Item);
                    SessionHelper.Item = null;
                    saved = true;

                }
                else
                    message = "No Item information available!";
                return Json(new { success = saved, message = message });
            }
            catch (Exception e)
            {
                message = e.Message;
                return Json(new { success = false, message = message });
            }
        }