private PurchaseOrder getEntityByModel(PurchaseOrderModel model)
        {
            if (model == null) return null;

            PurchaseOrder entity = new PurchaseOrder();

            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.CreateBy = model.CreateBy;
            entity.CreateDate = model.CreateDate;
            entity.Description = model.Description;
            entity.Id = model.Id;
            entity.PODate = model.PODate;
            entity.PONo = model.PONo;
            entity.SOBId = model.SOBId;
            entity.Status = model.Status;
            entity.UpdateBy = model.UpdateBy;
            entity.UpdateDate = model.UpdateDate;
            entity.VendorId = model.VendorId;
            entity.VendorSiteId = model.VendorSiteId;

            return entity;
        }
        private string generatePONum(PurchaseOrderModel 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.PONo, out outVal);
                if (isNumeric && currentDocument.PONo.Length == 8)
                {
                    newDocNo = (int.Parse(currentDocument.PONo) + 1).ToString();
                    return newDocNo;
                }
            }

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

            return yearDigit + monthDigit + docNo;
        }
        public ActionResult Create()
        {
            PurchaseOrderModel po = SessionHelper.PurchaseOrder;
            if (po == null)
            {
                po = new PurchaseOrderModel
                {
                    CompanyId = AuthenticationHelper.CompanyId.Value,
                    PODate = DateTime.Now,
                    PONo = "New",
                    SOBId = SessionHelper.SOBId
                };
                SessionHelper.PurchaseOrder = po;
            }

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

            po.Vendors = vendorService.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, po.PODate, po.PODate).
                Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.Id.ToString()
                }).ToList();

            if (po.Vendors != null && po.Vendors.Count() > 0)
            {
                po.VendorId = po.VendorId > 0 ? po.VendorId : Convert.ToInt64(po.Vendors.FirstOrDefault().Value);
                po.VendorSites = vendorService.GetAllSites(po.VendorId, AuthenticationHelper.CompanyId.Value).
                    Select(x => new SelectListItem
                    {
                        Text = x.Name,
                        Value = x.Id.ToString()
                    }).ToList();

                if(po.VendorSites != null && po.VendorSites.Count()>0)
                    po.VendorSiteId = po.VendorSiteId > 0 ? po.VendorSiteId : Convert.ToInt64(po.VendorSites.FirstOrDefault().Value);
            }

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

            return View("Edit", po);
        }
        public ActionResult Edit(string id)
        {
            PurchaseOrderModel po = new PurchaseOrderModel(service.GetSingle(id, AuthenticationHelper.CompanyId.Value));
            po.PurchaseOrderDetail = service.GetAllPODetail(po.Id).Select(x => new PurchaseOrderDetailModel(x)).ToList();
            SessionHelper.PurchaseOrder = po;

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

            po.Vendors = vendorService.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, po.PODate, po.PODate).
                Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.Id.ToString()
                }).ToList();

            if (po.Vendors != null && po.Vendors.Count() > 0)
            {
                po.VendorSites = vendorService.GetAllSites(po.VendorId, AuthenticationHelper.CompanyId.Value).
                    Select(x => new SelectListItem
                    {
                        Text = x.Name,
                        Value = x.Id.ToString()
                    }).ToList();
            }

            return View("Edit", po);
        }
        private void save(PurchaseOrderModel model)
        {
            PurchaseOrder 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 = getPODetail(result);
                    if (savedLines.Count() > model.PurchaseOrderDetail.Count())
                    {
                        var tobeDeleted = savedLines.Take(savedLines.Count() - model.PurchaseOrderDetail.Count());
                        foreach (var item in tobeDeleted)
                        {
                            service.DeletePODetail(item.Id);
                        }
                        savedLines = getPODetail(result);
                    }

                    foreach (var detail in model.PurchaseOrderDetail)
                    {
                        PurchaseOrderDetail detailEntity = getEntityByModel(detail);
                        if (detailEntity.IsValid())
                        {
                            detailEntity.POId = 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);
                        }
                    }
                }
            }
        }