public static PaymentViewModel GetPayment(string id)
        {
            PaymentModel payment = new PaymentModel(service.GetSingle(id, AuthenticationHelper.CompanyId.Value));

            PaymentViewModel paymentView = new PaymentViewModel
            {
                Amount = payment.Amount,
                BankAccountId = payment.BankAccountId,
                BankAccountName = "",
                BankId = payment.BankId,
                CreateBy = payment.CreateBy,
                CreateDate = payment.CreateDate,
                Id = payment.Id,
                PaymentDate = payment.PaymentDate,
                PaymentNo = payment.PaymentNo,
                PeriodId = payment.PeriodId,
                SOBId = payment.SOBId,
                Status = payment.Status,
                UpdateBy = payment.UpdateBy,
                UpdateDate = payment.UpdateDate,
                VendorId = payment.VendorId,
                VendorSiteId = payment.VendorSiteId,
                VendorSiteName = ""
            };

            return paymentView;
        }
        public ActionResult Create()
        {
            PaymentViewModel model = new PaymentViewModel();
            model.Period = PayablePeriodHelper.GetPeriodList(SessionHelper.SOBId);
            model.PeriodId = model.Period.Any() ? Convert.ToInt64(model.Period.First().Value) : 0;
            if (model.Period != null && model.Period.Count() > 0)
            {
                SessionHelper.Calendar = CalendarHelper.GetCalendar(PayablePeriodHelper.GetPayablePeriod(model.PeriodId.ToString()).CalendarId.ToString());

                model.Bank = BankHelper.GetBankList(SessionHelper.SOBId);
                model.BankId = model.Bank.Any() ? Convert.ToInt64(model.Bank.First().Value) : 0;
                if (model.Bank != null && model.Bank.Count() > 0)
                {
                    model.BankAccount = BankHelper.GetBankAccountList(model.BankId);
                    model.BankAccountId = model.BankAccount.Any() ?
                        Convert.ToInt32(model.BankAccount.First().Value) : 0;
                }

                model.Vendor = VendorHelper.GetVendorList(SessionHelper.Calendar.StartDate, SessionHelper.Calendar.EndDate);
                model.VendorId = model.Vendor.Any() ? Convert.ToInt64(model.Vendor.First().Value) : 0;
                if (model.Vendor != null && model.Vendor.Count() > 0)
                {
                    model.VendorSite = VendorHelper.GetVendorSiteList(model.VendorId);
                    model.VendorSiteId = model.VendorSite.Any() ? Convert.ToInt64(model.VendorSite.First().Value) : 0;
                }
            }

            SessionHelper.Payment = model;
            if (SessionHelper.Payment.PaymentInvoiceLines == null)
                SessionHelper.Payment.PaymentInvoiceLines = new List<PaymentInvoiceLinesModel>();


            return View(model);
        }
        public ActionResult GetDatabyPeriod(long periodId)
        {
            PaymentViewModel model = new PaymentViewModel();
            SessionHelper.Calendar = CalendarHelper.GetCalendar(PayablePeriodHelper.GetPayablePeriod(periodId.ToString()).CalendarId.ToString());

            if (model.Vendor == null)
            {
                model.Vendor = VendorHelper.GetVendorList(SessionHelper.Calendar.StartDate, SessionHelper.Calendar.EndDate);
                model.VendorId = model.Vendor.Any() ? Convert.ToInt32(model.Vendor.First().Value) : 0;

                if (model.VendorId > 0)
                {
                    model.VendorSite = VendorHelper.GetVendorSiteList(model.VendorId);
                    model.VendorSiteId = model.VendorSite.Any() ? Convert.ToInt64(model.VendorSite.First().Value) : 0;
                }
            }

            if (model.Bank == null)
            {
                model.Bank = BankHelper.GetBankList(model.SOBId);
                model.BankId = model.Bank.Any() ? Convert.ToInt32(model.Bank.First().Value) : 0;

                if (model.BankId > 0)
                {
                    model.BankAccount = BankHelper.GetBankAccountList(model.BankId);
                    model.BankAccountId = model.BankAccount.Any() ? Convert.ToInt64(model.BankAccount.First().Value) : 0;
                }
            }

            return Json(model, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SavePayment(PaymentViewModel model)
        {
            string message = "";
            try
            {
                bool saved = false;
                if (SessionHelper.Payment != null)
                {
                    SessionHelper.Payment.Amount = model.Amount;
                    SessionHelper.Payment.BankAccountId = model.BankAccountId;
                    SessionHelper.Payment.BankId = model.BankId;
                    SessionHelper.Payment.PaymentDate = model.PaymentDate;
                    SessionHelper.Payment.PeriodId = model.PeriodId;
                    SessionHelper.Payment.SOBId = SessionHelper.SOBId;
                    SessionHelper.Payment.Status = model.Status;
                    SessionHelper.Payment.VendorId = model.VendorId;
                    SessionHelper.Payment.VendorSiteId = model.VendorSiteId;

                    if (SessionHelper.Payment.Id == 0)
                        SessionHelper.Payment.PaymentNo = PaymentHelper.GetPaymentNo(AuthenticationHelper.CompanyId.Value, model.VendorId, SessionHelper.SOBId, model.BankId, model.PeriodId);

                    PaymentHelper.Update(SessionHelper.Payment);
                    SessionHelper.Payment = null;
                    saved = true;

                }
                else
                    message = "No Payment 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 Update(PaymentViewModel payment)
        {
            PaymentHeader entity = getEntityByModel(payment);

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

                if (!string.IsNullOrEmpty(result))
                {
                    var savedLines = getpaymentLinesbyPaymentId(result);
                    if (savedLines.Count() > payment.PaymentInvoiceLines.Count())
                    {
                        var tobeDeleted = savedLines.Take(savedLines.Count() - payment.PaymentInvoiceLines.Count());
                        foreach (var item in tobeDeleted)
                        {
                            service.DeleteLine(item.Id, AuthenticationHelper.CompanyId.Value);
                        }
                        savedLines = getpaymentLinesbyPaymentId(result);
                    }

                    foreach (var line in payment.PaymentInvoiceLines)
                    {
                        PaymentInvoiceLines lineEntity = getEntityByModel(line);
                        if (lineEntity.IsValid())
                        {
                            lineEntity.PaymentId = Convert.ToInt64(result);
                            if (savedLines.Count() > 0)
                            {
                                lineEntity.Id = savedLines.FirstOrDefault().Id;
                                savedLines.Remove(savedLines.FirstOrDefault(rec => rec.Id == lineEntity.Id));
                                service.Update(lineEntity);
                            }
                            else
                                service.Insert(lineEntity);
                        }
                    }
                }
            }
        }
        private static PaymentHeader getEntityByModel(PaymentViewModel model)
        {
            if (model == null) return null;

            PaymentHeader entity = new PaymentHeader();

            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 = AuthenticationHelper.CompanyId.Value; //Not exist.. have to do this..
            }

            entity.Amount = model.Amount;
            entity.BankId = model.BankId;
            entity.Id = model.Id;
            entity.PaymentDate = model.PaymentDate;
            entity.PaymentNo = model.PaymentNo;
            entity.Status = model.Status;
            entity.SOBId = model.SOBId;
            entity.BankAccountId = model.BankAccountId;
            entity.VendorId = model.VendorId;
            entity.PeriodId = model.PeriodId;
            entity.VendorSiteId = model.VendorSiteId;
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;
            return entity;
        }