public async Task<ActionResult> LoadDetail(string dataDetail, string act)
        {
            string[] data = dataDetail.Split('|');

            if (data[0].Length > 0)
            { 
                var model = new accountPayableHeader();
                for (int i = 0; i < data.Count(); i++)
                {
                    string[] value = data[i].Split(';');

                    if (value[1].ToString() == "")
                        break;

                    string t_invoiceNo = value[1].ToString();
                    decimal t_amountDebt = Decimal.Parse(value[2].ToString());
                    decimal t_amountPayment = Decimal.Parse(value[3].ToString());

                    var editor = new accountPayableHeader.accountPayableDetailView()
                    {
                        invoiceNo = t_invoiceNo,
                        amountDebt = t_amountDebt,
                        amountPayment = t_amountPayment
                    };
                    model.viewDetailAccountPayable.Add(editor);
                }
                ViewData["dataDetail"] = model.viewDetailAccountPayable.ToList();

            }
            else
            {
                ViewData["dataDetail"] = null;
            }
            return PartialView("_PartialPageAP1");
        }
        public void loadDetailView(accountPayableHeader accountpayable)
        {
            ViewBag.Bank = ccm.ddlBank(accountpayable.bankId.ToString());
            ViewBag.DDLpaymentType = ccm.ddlPaymentType(accountpayable.paymentType);
            ViewBag.VendorPopUp = db.vendors.ToList();
            ViewBag.totalRowsVendor = db.vendors.Count();

            var apd = db.accountPayableDetails.Where(x => x.voucherNo == accountpayable.voucherNo).ToList();

            var model = new accountPayableHeader();
            foreach(var a in apd as IEnumerable<accountPayableHeader.accountPayableDetail>)
            {
                string sql = "SELECT a.invoiceNo, c.total, a.amountPayment, b.voucherNo ";
                sql += "FROM [dbo].[AccountPayableDetails] a ";
                sql += "INNER JOIN [dbo].[AccountPayableHeaders] b ON b.voucherNo = a.voucherNo ";
                sql += "INNER JOIN [dbo].[PurchaseInvoices] c ON c.purchaseInvoiceNo = a.invoiceNo ";
                sql += "WHERE a.invoiceNo = '" + a.invoiceNo + "'";

                DataTable dataResult = con.executeReader(sql);
                decimal amountDebt = 0;
                foreach(DataRow dr in dataResult.Rows)
                {
                    amountDebt = decimal.Parse(dr["total"].ToString());
                }      
                var editor = new accountPayableHeader.accountPayableDetailView()
                    {
                        invoiceNo = a.invoiceNo,
                        amountDebt = amountDebt,
                        amountPayment = a.amountPayment
                    };
                model.viewDetailAccountPayable.Add(editor);
            }
            ViewData["dataDetail"] = model.viewDetailAccountPayable.ToList();
        }
        public ActionResult Edit(accountPayableHeader accountpayable)
        {
            if (acm.cekSession() == false)
                return RedirectToAction("Logout", "Account");

            lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
            if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
                return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

            #region collect detail account payable
            var countChk = 0;
            for (int i = 0; i < Request.Form.Count; i++)
            {
                if (Request.Form.AllKeys.ToList()[i].Contains("txtInvoiceNo_"))
                    countChk++;
            }

            var model = new accountPayableHeader();
            for (int i = 1; i <= countChk; i++)
            {
                var colVal = Request.Form["txtInvoiceNo_" + i];
                if (colVal == "")
                    break;

                string t_invoiceNo = Request.Form["txtInvoiceNo_" + i].ToString();
                decimal t_amountDebt = decimal.Parse(Request.Form["txtAmount_" + i].ToString());
                decimal t_amountPayment = decimal.Parse(Request.Form["txtPaymentAmount_" + i].ToString());

                var editor = new accountPayableHeader.accountPayableDetailView()
                {
                    invoiceNo = t_invoiceNo,
                    amountDebt = t_amountDebt,
                    amountPayment = t_amountPayment
                };
                model.viewDetailAccountPayable.Add(editor);
            }
            ViewData["dataDetail"] = model.viewDetailAccountPayable.ToList();
            #endregion

            ViewBag.Bank = ccm.ddlBank(accountpayable.bankId.ToString());
            ViewBag.DDLpaymentType = ccm.ddlPaymentType(accountpayable.paymentType);
            ViewBag.VendorPopUp = db.vendors.ToList();
            ViewBag.totalRowsVendor = db.vendors.Count();
            if (ModelState.IsValid)
            {
                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        db.Entry(accountpayable).State = EntityState.Modified;

                        #region insertDetail
                        bool runFirst = true;
                        for (int i = 1; i <= countChk; i++)
                        {
                            if (runFirst == true)
                            {
                                db.accountPayableDetails.RemoveRange(db.accountPayableDetails.Where(x => x.voucherNo == accountpayable.voucherNo));
                                runFirst = false;
                            }

                            var colVal = Request.Form["txtInvoiceNo_" + i];
                            if (colVal == "")
                                break;

                            string t_invoiceNo = Request.Form["txtInvoiceNo_" + i].ToString();
                            decimal t_amountDebt = decimal.Parse(Request.Form["txtAmount_" + i].ToString());
                            decimal t_amountPayment = decimal.Parse(Request.Form["txtPaymentAmount_" + i].ToString());

                            var editor = new accountPayableHeader.accountPayableDetail()
                            {
                                voucherNo = accountpayable.voucherNo,
                                invoiceNo = t_invoiceNo,
                                amountPayment = t_amountPayment
                            };
                            db.accountPayableDetails.Add(editor);
                        }
                        #endregion

                        db.SaveChanges();
                        ts.Complete();
                    }

                    return RedirectToAction("Index");
                }
                catch (Exception exc)
                {
                    string a = exc.Message;
                }
            }

            if (ModelState.IsValid)
            {
                
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(accountpayable);
        }
        public ActionResult Create(accountPayableHeader accountpayable)
        {
            if (acm.cekSession() == false)
                return RedirectToAction("Logout", "Account");

            lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
            if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
                return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

            #region collect detail account payable
            var countChk = 0;
            for (int i = 0; i < Request.Form.Count; i++)
            {
                if (Request.Form.AllKeys.ToList()[i].Contains("txtInvoiceNo_"))
                    countChk++;
            }

            var model = new accountPayableHeader();
            for (int i = 1; i <= countChk; i++)
            {
                var colVal = Request.Form["txtInvoiceNo_" + i];
                if (colVal == "")
                    break;

                string t_invoiceNo = Request.Form["txtInvoiceNo_" + i].ToString();
                decimal t_amountDebt = decimal.Parse(Request.Form["txtAmount_" + i].ToString());
                decimal t_amountPayment = decimal.Parse(Request.Form["txtPaymentAmount_" + i].ToString());

                var editor = new accountPayableHeader.accountPayableDetailView()
                {
                    invoiceNo = t_invoiceNo,
                    amountDebt = t_amountDebt,
                    amountPayment = t_amountPayment
                };
                model.viewDetailAccountPayable.Add(editor);
            }
            ViewData["dataDetail"] = model.viewDetailAccountPayable.ToList();
            #endregion 
                        
            ViewBag.Bank = ccm.ddlBank(accountpayable.bankId.ToString());
            ViewBag.DDLpaymentType = ccm.ddlPaymentType(accountpayable.paymentType);
            ViewBag.VendorPopUp = db.vendors.ToList();
            ViewBag.totalRowsVendor = db.vendors.Count();
            if (ModelState.IsValid)
            {
                try
                {
                    using(TransactionScope ts = new TransactionScope())
                    {
                        string generateID = prefix + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("d2");
                        var prIDCreated = db.accountPayableHeaders.Where(x => x.voucherNo.Contains(generateID)).OrderByDescending(x => x.createdDate).Select(x => x.voucherNo).ToList();
                        if (prIDCreated.Count == 0)
                        {
                            generateID = generateID + "0001";
                        }
                        else
                        {
                            generateID = generateID + (Convert.ToInt32(prIDCreated[0].Substring((prIDCreated[0].Length - 4))) + 1).ToString().PadLeft(4, '0');
                        }

                        accountpayable.voucherNo = generateID;
                        accountpayable.createdUser = lvm.userID;
                        accountpayable.createdDate = DateTime.Now;
                        db.accountPayableHeaders.Add(accountpayable);

                        #region insertDetail
                        for (int i = 1; i <= countChk; i++)
                        {
                            var colVal = Request.Form["txtInvoiceNo_" + i];
                            if (colVal == "")
                                break;

                            string t_invoiceNo = Request.Form["txtInvoiceNo_" + i].ToString();
                            decimal t_amountDebt = decimal.Parse(Request.Form["txtAmount_" + i].ToString());
                            decimal t_amountPayment = decimal.Parse(Request.Form["txtPaymentAmount_" + i].ToString());

                            var editor = new accountPayableHeader.accountPayableDetail()
                            {
                                voucherNo = accountpayable.voucherNo,
                                invoiceNo = t_invoiceNo,
                                amountPayment = t_amountPayment
                            };
                            db.accountPayableDetails.Add(editor);
                        }
                        #endregion

                        db.SaveChanges();
                        ts.Complete();
                    }

                    return RedirectToAction("Index");
                }
                catch (Exception exc)
                {
                    string a = exc.Message;
                }
            }

            return View(accountpayable);
        }