private static MoveOrder GetEntityByModel(MoveOrderModel model)
        {
            if (model == null) return null;

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

            entity.DateRequired = model.DateRequired;
            entity.Description = model.Description;
            entity.Id = model.Id;
            entity.MoveOrderDate = model.MoveOrderDate;
            entity.MoveOrderNo = model.MoveOrderNo;
            entity.SOBId = model.SOBId;
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;
            return entity;
        }
        public ActionResult Create()
        {
            MoveOrderModel model = SessionHelper.MoveOrder;
            if (model == null)
            {
                model = new MoveOrderModel
                {
                    SOBId = SessionHelper.SOBId,
                    DateRequired = DateTime.Now,
                    MoveOrderDate = DateTime.Now,
                    MoveOrderNo = "New"
                };
                model.CompanyId = AuthenticationHelper.CompanyId.Value;
                ViewBag.SOBName = SetOfBookHelper.GetSetOfBook(SessionHelper.SOBId.ToString()).Name;

                SessionHelper.MoveOrder = model;
                if (SessionHelper.MoveOrder.MoveOrderDetail == null)
                    SessionHelper.MoveOrder.MoveOrderDetail = new List<MoveOrderDetailModel>();
            }
            
            return View(model);
        }
        public ActionResult Save(MoveOrderModel model)
        {
            string message = "";
            try
            {
                bool saved = false;
                if (SessionHelper.MoveOrder != null)
                {
                    SessionHelper.MoveOrder.DateRequired = model.DateRequired;
                    SessionHelper.MoveOrder.Description = model.Description;
                    SessionHelper.MoveOrder.Id = model.Id;
                    SessionHelper.MoveOrder.MoveOrderDate = model.MoveOrderDate;
                    if (model.Id > 0)
                        SessionHelper.MoveOrder.MoveOrderNo = model.MoveOrderNo;
                    else
                        SessionHelper.MoveOrder.MoveOrderNo = MoveOrderHelper.GetDocNo(model.CompanyId, model.SOBId);

                    MoveOrderHelper.Save(SessionHelper.MoveOrder);
                    SessionHelper.MoveOrder = null;
                    saved = true;

                }
                else
                    message = "No Move Order information available!";
                return Json(new { success = saved, message = message });
            }
            catch (Exception e)
            {
                message = e.Message;
                return Json(new { success = false, message = message });
            }
        }
        public static void Save(MoveOrderModel moveOrderModel)
        {
            MoveOrder entity = GetEntityByModel(moveOrderModel);

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

                if (!string.IsNullOrEmpty(result))
                {
                    var savedDetail = GetMoveOrderLines(result);
                    if (savedDetail.Count() > moveOrderModel.MoveOrderDetail.Count())
                    {
                        var tobeDeleted = savedDetail.Take(savedDetail.Count() - moveOrderModel.MoveOrderDetail.Count());
                        foreach (var item in tobeDeleted)
                        {
                            service.Delete(item.Id);
                            IEnumerable<LotNumber> lotNum = lotNumService.CheckLotNumAvailability(AuthenticationHelper.CompanyId.Value, item.LotNo, item.ItemId, SessionHelper.SOBId);
                            if (lotNum.Any())
                            {
                                LotNumberHelper.Delete(lotNum.FirstOrDefault().Id.ToString());
                            }

                            //IEnumerable<SerialNumber> serialNum = lotNumService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, item.LotNo, item.SerialNo);
                            //if (serialNum.Any())
                            //{
                            //    LotNumberHelper.DeleteSerialNumber(serialNum.FirstOrDefault().Id.ToString());
                            //}
                        }
                        savedDetail = GetMoveOrderLines(result);
                    }

                    foreach (var detail in moveOrderModel.MoveOrderDetail)
                    {
                        MoveOrderDetail detailEntity = GetEntityByModel(detail, savedDetail.Count());
                        if (detailEntity.IsValid())
                        {
                            detailEntity.MoveOrderId = Convert.ToInt64(result);
                            if (savedDetail.Count() > 0)
                            {
                                detailEntity.Id = savedDetail.FirstOrDefault().Id;
                                savedDetail.Remove(savedDetail.FirstOrDefault(rec => rec.Id == detailEntity.Id));
                                service.Update(detailEntity);
                                IEnumerable<LotNumber> lotNum = lotNumService.CheckLotNumAvailability(AuthenticationHelper.CompanyId.Value, detailEntity.LotNo, detailEntity.ItemId, SessionHelper.SOBId);
                                if (lotNum.Any())
                                {
                                    LotNumberHelper.Update(lotNum.FirstOrDefault());
                                }

                                //IEnumerable<SerialNumber> serialNum = lotNumService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, detailEntity.LotNo, detailEntity.SerialNo);
                                //if (serialNum.Any())
                                //{
                                //    LotNumberHelper.UpdateSerialNumber(serialNum.FirstOrDefault());
                                //}
                            }
                            else
                            {
                                service.Insert(detailEntity);
                                IEnumerable<LotNumber> lotNum = lotNumService.CheckLotNumAvailability(AuthenticationHelper.CompanyId.Value, detailEntity.LotNo, detailEntity.ItemId, SessionHelper.SOBId);
                                if (!lotNum.Any())
                                {
                                    LotNumberHelper.Insert(new MoveOrderDetailModel(detailEntity));
                                }

                                //IEnumerable<SerialNumber> serialNum = lotNumService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, detailEntity.LotNo, detailEntity.SerialNo);
                                //if (!serialNum.Any())
                                //{
                                //    LotNumberHelper.InsertSerialNumber(new MoveOrderDetailModel(detailEntity));
                                //}
                            }
                        }
                    }
                }
            }
        }
 public static MoveOrderModel GetMoveOrder(string id)
 {
     MoveOrderModel model = new MoveOrderModel(service.GetSingle(id, AuthenticationHelper.CompanyId.Value));
     return model;
 }