public ActionResult CashPayment()
        {
            CashPayement model = new CashPayement();

            bool Exists = db.Voucher_Entry_Credit.Any(c => c.voucher_type.Equals("CashPayment"));

            if (Exists == true && db.Voucher_Entry_Credit.Select(p => p.record_no).Count() > 0)
            {
                var id = (from n in db.Voucher_Entry_Credit.Where(x => x.voucher_type == "CashPayment") select n.record_no).Max();

                int recno = Convert.ToInt32(id) + 1;

                model.RecordNo = recno.ToString();
            }
            else
            {
                model.RecordNo = "1";
            }

            return(View(model));
        }
        public ActionResult CashPaymentBill(CashPayement model)
        {
            try
            {
                int    CurrentYear  = DateTime.Today.Year;
                int    PreviousYear = DateTime.Today.Year - 1;
                int    NextYear     = DateTime.Today.Year + 1;
                string PreYear      = PreviousYear.ToString();
                string NexYear      = NextYear.ToString();
                string CurYear      = CurrentYear.ToString();
                string FinYear      = null;

                if (DateTime.Today.Month > 3)
                {
                    FinYear = CurYear + "-" + NexYear;
                }
                else
                {
                    FinYear = PreYear + "-" + CurYear;
                }

                for (int i = 0; i < model.ByAccount.Length; i++)
                {
                    Voucher_Entry_Debit tb1 = new Voucher_Entry_Debit();

                    tb1.voucher_no   = model.VoucherNo;
                    tb1.voucher_date = model.Date;

                    tb1.record_no = model.RecordNo.ToString();

                    tb1.record_date = model.Date;

                    tb1.voucher_sno = i + 1;

                    tb1.voucher_tb = "By";

                    tb1.voucher_type = "CashPayment";

                    tb1.voucher_year = FinYear;

                    tb1.from_form_name = "CashPayment";

                    tb1.userid = WebSecurity.CurrentUserId;

                    tb1.account_type = "CashPayment";


                    tb1.from_form_id = 0;

                    string accountname = model.ByAccount[i];
                    var    idl         = (from n in db.tblLedgerMasters.Where(x => x.LedgerName == accountname) select n.LedgerMasterId).SingleOrDefault();

                    tb1.voucher_ledger_accout_id = Convert.ToInt32(idl);
                    tb1.voucher_dbt_amount       = model.DrAmount[i];
                    tb1.voucher_narration        = model.DrNarration[i];
                    tb1.purchase_id = model.PurchaseId[i];
                    tb1.create_date = DateTime.Now;

                    db.Voucher_Entry_Debit.Add(tb1);
                    db.SaveChanges();
                }
                for (int j = 0; j < model.LedgerAccId.Length; j++)
                {
                    Voucher_Entry_Credit tb = new Voucher_Entry_Credit();

                    tb.voucher_no     = model.VoucherNo;
                    tb.Voucher_date   = model.Date;
                    tb.record_no      = model.RecordNo.ToString();
                    tb.record_date    = model.Date;
                    tb.voucher_sno    = j + 1;
                    tb.voucher_tb     = "To";
                    tb.voucher_type   = "CashPayment";
                    tb.voucher_year   = FinYear;
                    tb.from_form_name = "CashPayment";
                    tb.userid         = WebSecurity.CurrentUserId;
                    tb.account_type   = "CashPayment";
                    tb.from_form_id   = 0;
                    string name = model.LedgerAccId[j];
                    var    id   = (from n in db.tblLedgerMasters.Where(x => x.LedgerName == name) select n.LedgerMasterId).SingleOrDefault();
                    tb.voucher_ledger_accout_id = Convert.ToInt32(id);
                    tb.voucher_cr_amount        = model.CreditAmount[j];
                    tb.voucher_narration        = model.CrNarration[j];
                    //tb.purchase_id = model.PurchaseId[j];
                    tb.create_date = DateTime.Now;

                    db.Voucher_Entry_Credit.Add(tb);
                    db.SaveChanges();
                }

                TempData["Perror"] = "Inserted Successfully !";
                return(View("CashPayment"));
            }

            catch (Exception ex)
            {
                //return View(ex);

                TempData["Perror"] = "Please Enter the Details Correctlly !";
                return(RedirectToAction("CashPayment"));
            }
        }