public DataTable SaveVendorPaymentBySC(ReceivePaymentBLL receivePayment, LumexDBPlayer db)
        {
            try
            {
                db.AddParameters("@PurchaseRecordId", receivePayment.PurchaseRecordId.Trim());
                db.AddParameters("@VendorId", receivePayment.VendorId.Trim());
                db.AddParameters("@SalesCenterId", receivePayment.SalesCenterId.Trim());
                db.AddParameters("@Amount", receivePayment.Amount.Trim());
                db.AddParameters("@CurrentPayable", receivePayment.CurrentPayable.Trim());
                db.AddParameters("@ApprovedBy", LumexSessionManager.Get("ActiveUserId").ToString());
                db.AddParameters("@ApprovedFrom", LumexLibraryManager.GetTerminal());

                DataTable dt = db.ExecuteDataTable("INSERT_VENDOR_PAYMENT_PURCHASE_WISE", true);

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void getCustomerTotalReceivableByCustomerId()
        {
            ReceivePaymentBLL receivePayment = new ReceivePaymentBLL();

            try
            {
                DataTable dt = receivePayment.GetCustomerTotalReceivable(customerIdDropDownList.SelectedValue, ddlSelectWareHouseOrSalesCenter.SelectedValue);

                if (dt.Rows.Count > 0)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[0]["TotalDue"].ToString()))
                    {
                        previousDueTextBox.Text = dt.Rows[0]["TotalDue"].ToString();
                        previousDueDiv.Visible = true;
                        DuesAmoutPayTextBox.ReadOnly = false;
                    }
                    else
                    {
                        previousDueTextBox.Text = "0.00";
                        previousDueDiv.Visible = false;
                        DuesAmoutPayTextBox.ReadOnly = true;
                    }
                }
                else
                {
                    //string message = "Customer <span class='actionTopic'>Due</span> Data not found.";
                    //MyAlertBox("WarningAlert(\"" + "Process Succeed" + "\", \"" + message + "\", \"\");");

                    previousDueTextBox.Text = "0.00";
                    previousDueDiv.Visible = false;
                    DuesAmoutPayTextBox.ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null) { message += " --> " + ex.InnerException.Message; }
                MyAlertBox("ErrorAlert(\"" + ex.GetType() + "\", \"" + message + "\", \"\");");
            }
            finally
            {
                receivePayment = null;
            }
        }
        public DataTable SaveVendorPayment(ReceivePaymentBLL receivePayment, string accountOn, string whscId,DataTable adata, LumexDBPlayer db)
        {
            try
            {
                decimal PaidAmount = Convert.ToDecimal(receivePayment.Amount.Trim());
                DataTable dt = new DataTable();

                if (adata.Rows.Count > 0)
                {
                    for (int i = 0; i < adata.Rows.Count; i++)
                    {
                        db.ClearParameters();
                        if (PaidAmount > 0 && Convert.ToDecimal(adata.Rows[i]["Due"].ToString()) > 0)
                        {
                            db.AddParameters("@RecordId", adata.Rows[i]["PurchaseRecordId"].ToString());

                            if (Convert.ToDecimal(adata.Rows[i]["Due"].ToString()) >= PaidAmount)
                            {
                                db.AddParameters("@Amount", PaidAmount);
                            }
                            else
                            {
                                PaidAmount = PaidAmount - Convert.ToDecimal(adata.Rows[i]["Due"].ToString());
                                db.AddParameters("@Amount", Convert.ToDecimal(adata.Rows[i]["Due"].ToString()));
                            }
                            db.AddParameters("@AccountId", receivePayment.AccountId.Trim());
                            db.AddParameters("@VendorId", receivePayment.VendorId.Trim());
                            db.AddParameters("@SalesCenterId", whscId);
                            db.AddParameters("@CashCheque", receivePayment.CashCheque.Trim());
                            //db.AddParameters("@Amount", receivePayment.Amount.Trim());
                            db.AddParameters("@CurrentPayable", receivePayment.CurrentPayable.Trim());
                            db.AddParameters("@CreatedBy", LumexSessionManager.Get("ActiveUserId").ToString());
                            db.AddParameters("@CreatedFrom", LumexLibraryManager.GetTerminal());
                            db.AddParameters("@Bank", receivePayment.Bank.Trim());
                            db.AddParameters("@BankBrach", receivePayment.BankBrach.Trim());
                            db.AddParameters("@ChequeNo", receivePayment.ChequeNo.Trim());
                            db.AddParameters("@ChequeDate", LumexLibraryManager.ParseAppDate(receivePayment.ChequeDate));

                            dt = db.ExecuteDataTable("[INSERT_VENDOR_PAYMENT_BY_RECORD_ID]", true);
                        }
                        else
                        {
                            //  break;
                        }
                    }
                }
                else
                {


                    db.AddParameters("@AccountId", receivePayment.AccountId.Trim());
                    //     db.AddParameters("@WHSCId", whscId.Trim());
                    db.AddParameters("@VendorId", receivePayment.VendorId.Trim());
                    db.AddParameters("@CashCheque", receivePayment.CashCheque.Trim());
                    db.AddParameters("@Amount", receivePayment.Amount.Trim());
                    db.AddParameters("@CurrentPayable", receivePayment.CurrentPayable.Trim());
                    db.AddParameters("@CreatedBy", LumexSessionManager.Get("ActiveUserId").ToString());
                    db.AddParameters("@CreatedFrom", LumexLibraryManager.GetTerminal());
                    db.AddParameters("@Bank", receivePayment.Bank.Trim());
                    db.AddParameters("@BankBrach", receivePayment.BankBrach.Trim());
                    db.AddParameters("@ChequeNo", receivePayment.ChequeNo.Trim());
                    db.AddParameters("@ChequeDate", LumexLibraryManager.ParseAppDate(receivePayment.ChequeDate));
                    db.AddParameters("@WHSCId", whscId);

                   
                    dt = db.ExecuteDataTable("[INSERT_VENDOR_PAYMENT]", true);
                }

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public DataTable SaveCustomerPayment(ReceivePaymentBLL receivePayment, string accountOn, DataTable adata, LumexDBPlayer db)
        {
            try
            {
                decimal ReceiveAmount = Convert.ToDecimal(receivePayment.Amount.Trim());
                DataTable dt = new DataTable();

                if (adata.Rows.Count > 0)
                {
                    for (int i = 0; i < adata.Rows.Count; i++)
                    {
                        db.ClearParameters();
                        if (ReceiveAmount > 0 && Convert.ToDecimal(adata.Rows[i]["Due"].ToString())>0)
                        {
                            db.AddParameters("@RecordId", adata.Rows[i]["RecordId"].ToString());

                            if (Convert.ToDecimal(adata.Rows[i]["Due"].ToString()) >= ReceiveAmount)
                            {
                                db.AddParameters("@Amount", ReceiveAmount);
                            }
                            else
                            {
                                ReceiveAmount = ReceiveAmount - Convert.ToDecimal(adata.Rows[i]["Due"].ToString());
                                db.AddParameters("@Amount", Convert.ToDecimal(adata.Rows[i]["Due"].ToString()));
                            }
                            db.AddParameters("@AccountId", receivePayment.AccountId.Trim());
                            db.AddParameters("@CustomerId", receivePayment.CustomerId.Trim());
                            db.AddParameters("@SalesCenterId", accountOn);//(string)LumexSessionManager.Get("UserSalesCenterId"));
                            db.AddParameters("@CashCheque", receivePayment.CashCheque.Trim());
                            //db.AddParameters("@Amount", receivePayment.Amount.Trim());
                            db.AddParameters("@CurrentReceivable", receivePayment.CurrentReceivable.Trim());
                            db.AddParameters("@CreatedBy", LumexSessionManager.Get("ActiveUserId").ToString());
                            db.AddParameters("@CreatedFrom", LumexLibraryManager.GetTerminal());
                            db.AddParameters("@Bank", receivePayment.Bank.Trim());
                            db.AddParameters("@BankBrach", receivePayment.BankBrach.Trim());
                            db.AddParameters("@ChequeNo", receivePayment.ChequeNo.Trim());
                            db.AddParameters("@ChequeDate", LumexLibraryManager.ParseAppDate(receivePayment.ChequeDate));

                            dt = db.ExecuteDataTable("INSERT_CUSTOMER_PAYMENT_BY_RECORD_ID", true);
                        }
                        else
                        {
                          //  break;
                        }
                    }
                }
                else
                {
                    db.ClearParameters();
                    db.AddParameters("@SalesCenterId", accountOn);
                    db.AddParameters("@AccountId", receivePayment.AccountId.Trim());
                    db.AddParameters("@CustomerId", receivePayment.CustomerId.Trim());
                    //  db.AddParameters("@SalesCenterId", (string)LumexSessionManager.Get("UserSalesCenterId"));
                    db.AddParameters("@CashCheque", receivePayment.CashCheque.Trim());
                    db.AddParameters("@Amount", receivePayment.Amount.Trim());
                    db.AddParameters("@CurrentReceivable", receivePayment.CurrentReceivable.Trim());
                    db.AddParameters("@CreatedBy", LumexSessionManager.Get("ActiveUserId").ToString());
                    db.AddParameters("@CreatedFrom", LumexLibraryManager.GetTerminal());
                    db.AddParameters("@Bank", receivePayment.Bank.Trim());
                    db.AddParameters("@BankBrach", receivePayment.BankBrach.Trim());
                    db.AddParameters("@ChequeNo", receivePayment.ChequeNo.Trim());
                    db.AddParameters("@ChequeDate", LumexLibraryManager.ParseAppDate(receivePayment.ChequeDate));

                    dt = db.ExecuteDataTable("INSERT_CUSTOMER_PAYMENT", true);
                }


                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //protected void LoadVendors()
        //{
        //    VendorBLL vendor = new VendorBLL();

        //    try
        //    {
        //        DataTable dt = vendor.GetActiveVendors();

        //        vendorDropDownList.DataSource = dt;
        //        vendorDropDownList.DataValueField = "VendorId";
        //        vendorDropDownList.DataTextField = "VendorName";
        //        vendorDropDownList.DataBind();
        //        vendorDropDownList.Items.Insert(0, "");
        //        vendorDropDownList.SelectedIndex = 0;
        //    }
        //    catch (Exception ex)
        //    {
        //        msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = ex.Message;
        //    }
        //    finally
        //    {
        //        vendor = null;
        //    }
        //}

        protected void GetPaymentInfo()
        {
            ReceivePaymentBLL receivePayment = new ReceivePaymentBLL();
            PurchaseRecordBLL purchaseRecord = new PurchaseRecordBLL();

            try
            {
                DataTable dtInfo = purchaseRecord.GetPurchaseRecordById(purchaseRecordIdTextBox.Text.Trim());

                if (dtInfo.Rows.Count > 0)
                {
                    //salesCenterIdLabel.Text = dtInfo.Rows[0]["SalesCenterId"].ToString();
                    //salesCenterNameLabel.Text = dtInfo.Rows[0]["SalesCenterName"].ToString();
                    recordIdLabel.Text = dtInfo.Rows[0]["PurchaseRecordId"].ToString();
                    recordDateLabel.Text = dtInfo.Rows[0]["RecordDate"].ToString();
                    vendorIdLabel.Text = dtInfo.Rows[0]["VendorId"].ToString();
                    vendorNameLabel.Text = dtInfo.Rows[0]["VendorName"].ToString();
                    warehouseIdLabel.Text = dtInfo.Rows[0]["WarehouseId"].ToString();
                    warehouseNameLabel.Text = dtInfo.Rows[0]["WarehouseName"].ToString();
                    //salesCenterIdLabel.Text = dtInfo.Rows[0]["WarehouseId"].ToString();
                    //salesCenterNameLabel.Text = dtInfo.Rows[0]["WarehouseName"].ToString();
                    totalAmountLabel.Text = dtInfo.Rows[0]["TotalAmount"].ToString();
                    //vatLabel.Text = dtInfo.Rows[0]["VAT"].ToString();
                    transportCostLabel.Text = dtInfo.Rows[0]["TransportCost"].ToString();
                    totalPayableLabel.Text = dtInfo.Rows[0]["TotalPayable"].ToString();
                }

                DataTable dt = receivePayment.GetVendorPayments(vendorIdLabel.Text.Trim(), purchaseRecordIdTextBox.Text.Trim(), "PA");
                paymentDetailsGridView.DataSource = dt;
                paymentDetailsGridView.DataBind();

                decimal amt = 0;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["Status"].ToString() == "Approved")
                    {
                        amt = amt + decimal.Parse(dt.Rows[i]["Amount"].ToString());
                    }
                }

                totalReceivedLabel.Text = amt.ToString();
                currentPayableLabel.Text = (decimal.Parse(totalPayableLabel.Text) - decimal.Parse(totalReceivedLabel.Text)).ToString();

                if (paymentDetailsGridView.Rows.Count > 0)
                {
                    paymentDetailsGridView.UseAccessibleHeader = true;
                    paymentDetailsGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null) { message += " --> " + ex.InnerException.Message; }
                MyAlertBox("ErrorAlert(\"" + ex.GetType() + "\", \"" + message + "\", \"\");");
            }
            finally
            {
                receivePayment = null;
                purchaseRecord = null;
            }
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            ReceivePaymentBLL receivePayment = new ReceivePaymentBLL();

            try
            {
                if (decimal.Parse(currentPayableLabel.Text) > 0)
                {
                    receivePayment.PurchaseRecordId = recordIdLabel.Text;
                    receivePayment.VendorId = vendorIdLabel.Text;
                    receivePayment.SalesCenterId = "";//salesCenterIdLabel.Text;
                    receivePayment.Amount = amountTextBox.Text.Trim();
                    receivePayment.CurrentPayable = currentPayableLabel.Text;

                    receivePayment.SaveVendorPaymentBySC();
                    GetPaymentInfo();

                    string message = "";
                    MyAlertBox("SuccessAlert(\"" + "Process Succeed" + "\", \"" + message + "\", callbackOk);");

                    MyAlertBox("alert(\"Saved Successfully.\");");
                }
                else
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Exception!!!"; msgDetailLabel.Text = "No Payable to Payment.";
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null) { message += " --> " + ex.InnerException.Message; }
                MyAlertBox("ErrorAlert(\"" + ex.GetType() + "\", \"" + message + "\", \"\");");
            }
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            ReceivePaymentBLL receivePayment = new ReceivePaymentBLL();
            DataRow dr = null;
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("RecordDate"));
            dt.Columns.Add(new DataColumn("PurchaseRecordId"));
            dt.Columns.Add(new DataColumn("SalesCenterName"));
            dt.Columns.Add(new DataColumn("TotalAmount"));
            dt.Columns.Add(new DataColumn("DiscountAmount"));
            dt.Columns.Add(new DataColumn("TotalPayable"));
            dt.Columns.Add(new DataColumn("PaidAmount"));
            dt.Columns.Add(new DataColumn("Due"));
         

            try
            {
                if (amountTextBox.Text == "")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Amount field is required.";
                }
                else if (paymentModeDropDownList.SelectedValue == "")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Select Payment field is required.";
                }
                else if (accountHeadDropDownList.SelectedValue == "")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Select Account Head field is required.";
                }
                else if (paymentModeDropDownList.SelectedValue == "Cheque")
                {
                    if (chequeNumberTextBox.Text == "")
                    {
                        msgbox.Visible = true;
                        msgTitleLabel.Text = "Validation!!!";
                        msgDetailLabel.Text = "Cheque Number field is required.";

                    }
                    else if (chequeDateTextBox.Text == "")
                    {
                        if (chequeNumberTextBox.Text == "")
                        {
                            msgbox.Visible = true;
                            msgTitleLabel.Text = "Validation!!!";
                            msgDetailLabel.Text = "Cheque Date field is required.";
                        }
                    }
                }
                else
                {

                    if (decimal.Parse(currentPayableLabel.Text) > 0)
                    {
                        if (decimal.Parse(currentPayableLabel.Text) >= decimal.Parse(amountTextBox.Text.Trim()))
                        {
                            receivePayment.AccountId = accountHeadDropDownList.SelectedValue.Trim();
                            receivePayment.VendorId = vendorDropDownList.SelectedValue.Trim();
                            receivePayment.CashCheque = paymentModeDropDownList.SelectedValue.Trim();
                            receivePayment.CurrentPayable = currentPayableLabel.Text;
                            receivePayment.Amount = amountTextBox.Text.Trim();
                            receivePayment.ChequeNo = chequeNumberTextBox.Text;
                            receivePayment.ChequeDate = chequeDateTextBox.Text;
                            receivePayment.Bank = bankDropDownList.Text;
                            receivePayment.BankBrach = bankBranchTextBox.Text;

                            for (int i = 0; i < payableListGridView.Rows.Count; i++)
                            {
                                CheckBox chkbx = (CheckBox)payableListGridView.Rows[i].FindControl("selectCheckBox");

                                if (chkbx.Checked)
                                {
                                    dr = dt.NewRow();

                                    // dr["Barcode"] = productListGridView.Rows[i].Cells[0].Text.ToString();
                                    dr["RecordDate"] = payableListGridView.Rows[i].Cells[0].Text.ToString();
                                    dr["PurchaseRecordId"] = payableListGridView.Rows[i].Cells[1].Text.ToString();
                                    dr["SalesCenterName"] = payableListGridView.Rows[i].Cells[2].Text.ToString();
                                    dr["TotalAmount"] = payableListGridView.Rows[i].Cells[3].Text.ToString();
                                    dr["DiscountAmount"] = payableListGridView.Rows[i].Cells[4].Text.ToString();
                                    dr["TotalPayable"] = payableListGridView.Rows[i].Cells[5].Text.ToString();
                                    dr["PaidAmount"] = payableListGridView.Rows[i].Cells[6].Text.ToString();
                                    dr["Due"] = payableListGridView.Rows[i].Cells[7].Text.ToString();


                                    dt.Rows.Add(dr);
                                }
                            }


                            receivePayment.SaveVendorPayment("", drpdwnSalesCenterOrWarehouse.SelectedValue,dt);
                                //drpdwnAccountOn.SelectedValue, drpdwnSalesCenterOrWarehouse.SelectedValue);

                            //string message = "Vendor Payment <span class='actionTopic'>Saved</span> Successfully.";
                            //MyAlertBox("SuccessAlert(\"" + "Process Succeed" + "\", \"" + message + "\", \"\");");
                            initializeFields();
                            string message = "Vendor Payment <span class='actionTopic'>" + receivePayment.Amount +
                                             " Tk Saved</span> Successfully.";
                            MyAlertBox("SuccessAlert(\"" + "Process Succeed" + "\", \"" + message + "\", \"\");");


                            GetVendorPayableList();
                            //MyAlertBox("alert(\"Saved Successfully.\");");
                        }
                        else
                        {
                            msgbox.Visible = true;
                            msgTitleLabel.Text = "Exception!!!";
                            msgDetailLabel.Text = "Amount must be equal or less of Current Payable Amount.";
                        }
                    }
                    else
                    {
                        msgbox.Visible = true;
                        msgTitleLabel.Text = "Exception!!!";
                        msgDetailLabel.Text = "No Payable to Payment.";
                    }
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null) { message += " --> " + ex.InnerException.Message; }
                MyAlertBox("ErrorAlert(\"" + ex.GetType() + "\", \"" + message + "\", \"\");");
            }
        }
        protected void GetVendorPayableList()
        {
            ReceivePaymentBLL receivePayment = new ReceivePaymentBLL();

            try
            {
                if (vendorDropDownList.SelectedValue.Trim() == "")
                {
                    msgbox.Visible = true; msgTitleLabel.Text = "Validation!!!"; msgDetailLabel.Text = "Vendor Name field is required.";
                }
                else
                {
                    DataTable dt = receivePayment.GetVendorPayableList(vendorDropDownList.SelectedValue, "Warehouse", drpdwnSalesCenterOrWarehouse.SelectedValue);

                    payableListGridView.DataSource = dt;
                    payableListGridView.DataBind();

                    if (payableListGridView.Rows.Count > 0)
                    {
                        payableListGridView.UseAccessibleHeader = true;
                        payableListGridView.HeaderRow.TableSection = TableRowSection.TableHeader;

                        decimal currentPayable = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            currentPayable += decimal.Parse(dt.Rows[i]["Due"].ToString());
                        }

                        currentPayableLabel.Text = currentPayable.ToString();
                    }
                    else
                    {
                        msgbox.Visible = true; msgTitleLabel.Text = "Vendor Payable List Data Not Found!!!"; msgDetailLabel.Text = "";
                        msgbox.Attributes.Add("class", "alert alert-info");
                        currentPayableLabel.Text = "0.00";
                    }
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null) { message += " --> " + ex.InnerException.Message; }
                MyAlertBox("ErrorAlert(\"" + ex.GetType() + "\", \"" + message + "\", \"\");");
            }
            finally
            {
                receivePayment = null;
                MyAlertBox("MyOverlayStop();");
            }
        }