private Requisition getEntityByModel(RequisitionModel model)
        {
            if (model == null) return null;

            Requisition entity = new Requisition();

            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.BuyerId = model.BuyerId;
            entity.CompanyId = model.CompanyId;
            entity.Description = model.Description;
            entity.Id = model.Id;
            entity.RequisitionDate = model.RequisitionDate;
            entity.RequisitionNo = model.RequisitionNo;
            entity.SOBId = model.SOBId;

            return entity;
        }
        private string generateReqNum(RequisitionModel model)
        {
            var currentDocument = service.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId).OrderByDescending(rec => rec.Id).FirstOrDefault();
            string newDocNo = "";
            if (currentDocument != null)
            {
                int outVal;
                bool isNumeric = int.TryParse(currentDocument.RequisitionNo, out outVal);
                if (isNumeric && currentDocument.RequisitionNo.Length == 8)
                {
                    newDocNo = (int.Parse(currentDocument.RequisitionNo) + 1).ToString();
                    return newDocNo;
                }
            }

            //Create New DocNum..
            string yearDigit = model.RequisitionDate.ToString("yy");
            string monthDigit = model.RequisitionDate.ToString("MM");
            string docNo = int.Parse("1").ToString().PadLeft(4, '0');

            return yearDigit + monthDigit + docNo;
        }
        public ActionResult Create()
        {
            RequisitionModel requisition = SessionHelper.Requisition;
            if (requisition == null)
            {
                requisition = new RequisitionModel
                {
                    RequisitionDate = DateTime.Now,
                    RequisitionNo = "New",
                    CompanyId = AuthenticationHelper.CompanyId.Value,
                    SOBId = SessionHelper.SOBId
                };
                SessionHelper.Requisition = requisition;
            }

            requisition.Buyers = buyerService.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, requisition.RequisitionDate, requisition.RequisitionDate).
                Select(x => new SelectListItem { 
                     Text = x.Name,
                     Value = x.Id.ToString()
                }).ToList();

            if (requisition.Buyers != null && requisition.Buyers.Count() > 0)
                requisition.BuyerId = requisition.BuyerId > 0 ? requisition.BuyerId : Convert.ToInt64(requisition.Buyers.FirstOrDefault().Value);

            return View("Edit", requisition);
        }
        public ActionResult Edit(string id)
        {
            RequisitionModel requisition = new RequisitionModel(service.GetSingle(id, AuthenticationHelper.CompanyId.Value));
            requisition.RequisitionDetail = service.GetAllRequisitionDetail(requisition.Id).Select(x => new RequisitionDetailModel(x)).ToList();
            SessionHelper.Requisition = requisition;

            requisition.Buyers = buyerService.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, requisition.RequisitionDate, requisition.RequisitionDate).
                Select(x => new SelectListItem { 
                     Value = x.Id.ToString(),
                      Text = x.Name
                }).ToList();

            return View("Edit", requisition);
        }
        private void save(RequisitionModel model)
        {
            Requisition entity = getEntityByModel(model);

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

                if (!string.IsNullOrEmpty(result))
                {
                    var savedLines = getRequisitionDetail(result);
                    if (savedLines.Count() > model.RequisitionDetail.Count())
                    {
                        var tobeDeleted = savedLines.Take(savedLines.Count() - model.RequisitionDetail.Count());
                        foreach (var item in tobeDeleted)
                        {
                            service.DeleteRequisitionDetail(item.Id);
                        }
                        savedLines = getRequisitionDetail(result);
                    }

                    foreach (var detail in model.RequisitionDetail)
                    {
                        RequisitionDetail detailEntity = getEntityByModel(detail);
                        if (detailEntity.IsValid())
                        {
                            detailEntity.RequisitionId = Convert.ToInt64(result);
                            if (savedLines.Count() > 0)
                            {
                                detailEntity.Id = savedLines.FirstOrDefault().Id;
                                savedLines.Remove(savedLines.FirstOrDefault(rec => rec.Id == detailEntity.Id));
                                service.Update(detailEntity);
                            }
                            else
                                service.Insert(detailEntity);
                        }
                    }
                }
            }
        }