context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using InternalPayments.Classes;
  
  namespace InternalPayments.Dialogs
  {
      public partial class quickPayForm : Form
      {
          public quickPayForm()
          {
              InitializeComponent();
          }
          cadmaFunctions.NavFuncs myNav = new cadmaFunctions.NavFuncs();
  
          string errorMsgs = "";
  
          long PrsnID = -1;
          public long[] prsnIDs = new long[1];
  
          public long mspID = -1;
          bool obey_evnts = false;
          public bool txtChngd = false;
          public string srchWrd = "%";
          //Mass Pay Run Details
          long mspydt_cur_indx = 0;
          bool is_last_mspydt = false;
          long totl_mspydt = 0;
          long last_mspydt_num = 0;
          public string mspydt_SQL = "";
  
          private void msPyItmStButton_Click(object sender, EventArgs e)
          {
              string[] selVals = new string[1];
              selVals[0] = this.msPyItmStIDTextBox.Text;
              DialogResult dgRes = Global.mnFrm.cmCde.showPssblValDiag(
                  Global.mnFrm.cmCde.getLovID("Item Sets for Payments(Enabled)"), ref selVals,
                  true, true, Global.mnFrm.cmCde.Org_id, "", "",
               this.srchWrd, "Both", true, " and (tbl1.g IN (" + Global.concatCurRoleIDs() + "))");
              if (dgRes == DialogResult.OK)
              {
                  for (int i = 0; i < selVals.Length; i++)
                  {
                      this.msPyItmStIDTextBox.Text = selVals[i];
                      this.msPyItmStNmTextBox.Text = Global.mnFrm.cmCde.getItmStName(int.Parse(selVals[i]));
                      this.expctdTotalNumUpDown.Value = 0;
                      this.actualTotalNumUpDown.Value = 0;
                      this.totalDiffLabel.Text = "0.00";
                  }
              }
              if (this.trnsDateTextBox.Text != "" && this.msPyItmStIDTextBox.Text != ""
           && this.msPyItmStIDTextBox.Text != "-1")
              {
                  //
              }
          }
  
          private void trnsDateButton_Click(object sender, EventArgs e)
          {
              Global.mnFrm.cmCde.selectDate(ref this.trnsDateTextBox);
              if (this.glDateTextBox.Text == "")
              {
                  this.glDateTextBox.Text = this.trnsDateTextBox.Text;
              }
              if (this.trnsDateTextBox.Text != "")
              {
                  this.rfrsTrnsDates();
              }
          }
  
          private void rfrsTrnsDates()
          {
              for (int i = 0; i < this.trnsDataGridView.Rows.Count; i++)
              {
                  this.trnsDataGridView.Rows[i].Cells[5].Value = this.trnsDateTextBox.Text;
              }
          }
  
          private void gotoButton_Click(object sender, EventArgs e)
          {
              if (this.trnsDateTextBox.Text == "")
              {
                  Global.mnFrm.cmCde.showMsg("Please provide a Transaction Date First!", 0);
                  return;
              }
              if (this.msPyItmStIDTextBox.Text == "" || this.msPyItmStIDTextBox.Text == "-1")
              {
                  Global.mnFrm.cmCde.showMsg("Please select an Item Set First!", 0);
                  return;
              }
              this.gotoButton.Enabled = false;
              this.msPyDtListView.Items.Clear();
              this.trnsDataGridView.Rows.Clear();
              this.populateMsPyListVw();
              this.populateItms();
              this.gotoButton.Enabled = true;
          }
  
          private void trnsDataGridView_CellContentClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
          {
              if (e.ColumnIndex == 6)
              {
                  this.trnsDateTextBox.Text = this.trnsDataGridView.Rows[e.RowIndex].Cells[5].Value.ToString();
                  Global.mnFrm.cmCde.selectDate(ref this.trnsDateTextBox);
                  this.trnsDataGridView.Rows[e.RowIndex].Cells[5].Value = this.trnsDateTextBox.Text;
                  this.trnsDataGridView.EndEdit();
              }
          }
  
          private long[] getPrsnsInvolved()
          {
              string dateStr = DateTime.Parse(this.trnsDateTextBox.Text).ToString("yyyy-MM-dd HH:mm:ss");
              string extrWhr = "";
              if (long.Parse(this.cstmrIDTextBox.Text) > 0)
              {
                  extrWhr += " and (Select distinct z.lnkd_firm_org_id From prs.prsn_names_nos z where z.person_id=a.person_id)=" + this.cstmrIDTextBox.Text;
              }
              if (long.Parse(this.cstmrSiteIDTextBox.Text) > 0)
              {
                  extrWhr += " and (Select distinct z.lnkd_firm_site_id From prs.prsn_names_nos z where z.person_id=a.person_id)=" + this.cstmrSiteIDTextBox.Text;
              }
  
              string grpSQL = "";
              if (this.grpComboBox.Text == "Divisions/Groups")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_divs_groups a Where ((a.div_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Grade")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_grades a Where ((a.grade_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Job")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_jobs a Where ((a.job_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Position")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_positions a Where ((a.position_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Site/Location")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_locations a Where ((a.location_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Person Type")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_prsntyps a, prs.prsn_names_nos b " +
            "Where ((a.person_id = b.person_id) and (b.org_id = " + Global.mnFrm.cmCde.Org_id + ") and (a.prsn_type = '" +
            this.grpNmTextBox.Text.Replace("'", "''") + "') and (to_timestamp('" + dateStr +
            "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
              "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Working Hour Type")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_work_id a Where ((a.work_hour_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Gathering Type")
              {
                  grpSQL = "Select distinct a.person_id From pasn.prsn_gathering_typs a Where ((a.gatherng_typ_id = " +
                    int.Parse(this.grpNmIDTextBox.Text) + ") and (to_timestamp('" + dateStr +
                    "','YYYY-MM-DD HH24:MI:SS') between to_timestamp(a.valid_start_date|| ' 00:00:00','YYYY-MM-DD HH24:MI:SS') " +
                      "AND to_timestamp(a.valid_end_date || ' 23:59:59','YYYY-MM-DD HH24:MI:SS'))" + extrWhr + ") ORDER BY a.person_id";
              }
              else if (this.grpComboBox.Text == "Everyone")
              {
                  grpSQL = "Select distinct a.person_id From prs.prsn_names_nos a Where ((a.org_id = "
                    + Global.mnFrm.cmCde.Org_id + ")" + extrWhr + ") ORDER BY a.person_id";
              }
              else
              {
                  grpSQL = "Select distinct a.person_id From prs.prsn_names_nos a Where ((a.person_id = "
                    + Global.mnFrm.cmCde.getPrsnID(this.locIDTextBox.Text) + ")" + extrWhr + ") ORDER BY a.person_id";
              }
  
              DataSet dtst = Global.mnFrm.cmCde.selectDataNoParams(grpSQL);
              this.prsnIDs = new long[dtst.Tables[0].Rows.Count];
              for (int i = 0; i < dtst.Tables[0].Rows.Count; i++)
              {
                  this.prsnIDs[i] = long.Parse(dtst.Tables[0].Rows[i][0].ToString());
              }
              return this.prsnIDs;
          }
  
          private void populateItms()
          {
              if (this.grpComboBox.Text != "Everyone"
                && this.grpComboBox.Text != "Single Person")
              {
                  if (this.grpNmIDTextBox.Text == "-1"
                  || this.grpNmTextBox.Text == "")
                  {
                      Global.mnFrm.cmCde.showMsg("Please select a Group Name!", 0);
                      return;
                  }
              }
  
              string curid = Global.mnFrm.cmCde.getOrgFuncCurID(Global.mnFrm.cmCde.Org_id).ToString();
              string curcode = Global.mnFrm.cmCde.getPssblValNm(int.Parse(curid));
              int i = 0;
              this.prsnIDs = this.getPrsnsInvolved();
              this.trnsDataGridView.Rows.Clear();
              int rwidx = 0;
              this.saveLabel.Text = "Loading the Persons involved (" + this.prsnIDs.Length + ") and their Items...Please Wait...";
              this.saveLabel.Visible = true;
              System.Windows.Forms.Application.DoEvents();
  
              decimal ttlAmntLoaded = 0;
              decimal payItmAmnt = 0;
              decimal outstandgAdvcAmnt = 0;
              long advBlsItmID = Global.mnFrm.cmCde.getItmID("Total Advance Payments Balance", Global.mnFrm.cmCde.Org_id);
              long advApplyItmID = Global.mnFrm.cmCde.getItmID("Advance Payments Amount Applied", Global.mnFrm.cmCde.Org_id);
              long advKeptItmID = Global.mnFrm.cmCde.getItmID("Advance Payments Amount Kept", Global.mnFrm.cmCde.Org_id);
  
              long advApplyItmValID = Global.getFirstItmValID(advApplyItmID);
              long advKeptItmValID = Global.getFirstItmValID(advKeptItmID);
              string extrDesc = "";
              if (this.cheqNumTextBox.Text != "")
              {
                  extrDesc = " using Cheque#:" + this.cheqNumTextBox.Text;
              }
              //decimal ttlBillsCharges = 0;
              List<Object[]> advncsToApply = new List<Object[]>();
  
gt:             List<Object[]> advncsToBeKept = new List<Object[]>();