/// <summary>
 /// 
 /// </summary>
 /// <param name="paymentdetailsinfo"></param>
 /// <returns></returns>
 public decimal PaymentDetailsEdit(PaymentDetailsInfo paymentdetailsinfo)
 {
     decimal decCompanyId = 0;
     try
     {
         decCompanyId = SPPaymentDetails.PaymentDetailsEdit(paymentdetailsinfo);
     }
     catch (Exception ex)
     {
         MessageBox.Show("PVBLL:3" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     return decCompanyId;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="paymentdetailsinfo"></param>
 /// <returns></returns>
 public decimal PaymentDetailsEdit(PaymentDetailsInfo paymentdetailsinfo)
 {
     decimal decResult = 0;
        try
        {
        decResult = spPaymentDetails.PaymentDetailsEdit(paymentdetailsinfo);
        }
        catch (Exception ex)
        {
        MessageBox.Show("PD3:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        return decResult;
 }
 /// <summary>
 /// Function to insert values to PaymentDetails Table
 /// </summary>
 /// <param name="paymentdetailsinfo"></param>
 /// <returns></returns>
 public decimal PaymentDetailsAdd(PaymentDetailsInfo paymentdetailsinfo)
 {
     decimal decPaymentDetailsId = 0;
     try
     {
     if (sqlcon.State == ConnectionState.Closed)
     {
         sqlcon.Open();
     }
     SqlCommand sccmd = new SqlCommand("PaymentDetailsAdd", sqlcon);
     sccmd.CommandType = CommandType.StoredProcedure;
     SqlParameter sprmparam = new SqlParameter();
     sprmparam = sccmd.Parameters.Add("@paymentMasterId", SqlDbType.Decimal);
     sprmparam.Value = paymentdetailsinfo.PaymentMasterId;
     sprmparam = sccmd.Parameters.Add("@ledgerId", SqlDbType.Decimal);
     sprmparam.Value = paymentdetailsinfo.LedgerId;
     sprmparam = sccmd.Parameters.Add("@amount", SqlDbType.Decimal);
     sprmparam.Value = paymentdetailsinfo.Amount;
     sprmparam = sccmd.Parameters.Add("@exchangeRateId", SqlDbType.Decimal);
     sprmparam.Value = paymentdetailsinfo.ExchangeRateId;
     sprmparam = sccmd.Parameters.Add("@chequeNo", SqlDbType.VarChar);
     sprmparam.Value = paymentdetailsinfo.ChequeNo;
     sprmparam = sccmd.Parameters.Add("@chequeDate", SqlDbType.DateTime);
     sprmparam.Value = paymentdetailsinfo.ChequeDate;
     sprmparam = sccmd.Parameters.Add("@extra1", SqlDbType.VarChar);
     sprmparam.Value = paymentdetailsinfo.Extra1;
     sprmparam = sccmd.Parameters.Add("@extra2", SqlDbType.VarChar);
     sprmparam.Value = paymentdetailsinfo.Extra2;
     decPaymentDetailsId = Convert.ToDecimal(sccmd.ExecuteScalar().ToString());
     }
     catch (Exception ex)
     {
     MessageBox.Show(ex.ToString());
     }
     finally
     {
     sqlcon.Close();
     }
     return decPaymentDetailsId;
 }
 /// <summary>
 /// Function to get particular values from PaymentDetails Table based on the parameter
 /// </summary>
 /// <param name="paymentDetailsId"></param>
 /// <returns></returns>
 public PaymentDetailsInfo PaymentDetailsView(decimal paymentDetailsId )
 {
     PaymentDetailsInfo paymentdetailsinfo =new PaymentDetailsInfo();
     SqlDataReader sdrreader =null;
     try
     {
     if (sqlcon.State == ConnectionState.Closed)
     {
         sqlcon.Open();
     }
     SqlCommand sccmd = new SqlCommand("PaymentDetailsView", sqlcon);
     sccmd.CommandType = CommandType.StoredProcedure;
     SqlParameter sprmparam = new SqlParameter();
     sprmparam = sccmd.Parameters.Add("@paymentDetailsId", SqlDbType.Decimal);
     sprmparam.Value = paymentDetailsId;
      sdrreader = sccmd.ExecuteReader();
     while (sdrreader.Read())
     {
         paymentdetailsinfo.PaymentDetailsId=decimal.Parse(sdrreader[0].ToString());
         paymentdetailsinfo.PaymentMasterId=decimal.Parse(sdrreader[1].ToString());
         paymentdetailsinfo.LedgerId=decimal.Parse(sdrreader[2].ToString());
         paymentdetailsinfo.Amount=decimal.Parse(sdrreader[3].ToString());
         paymentdetailsinfo.ExchangeRateId = decimal.Parse(sdrreader["exchangeRateId"].ToString());
         paymentdetailsinfo.ChequeNo= sdrreader[4].ToString();
         paymentdetailsinfo.ChequeDate=DateTime.Parse(sdrreader[5].ToString());
         paymentdetailsinfo.ExtraDate=DateTime.Parse(sdrreader[6].ToString());
         paymentdetailsinfo.Extra1= sdrreader[7].ToString();
         paymentdetailsinfo.Extra2= sdrreader[8].ToString();
     }
     }
     catch (Exception ex)
     {
     MessageBox.Show(ex.ToString());
     }
     finally
     {           sdrreader.Close();
     sqlcon.Close();
     }
     return paymentdetailsinfo;
 }
        /// <summary>
        /// Edit Function
        /// </summary>
        /// <param name="decMasterId"></param>
        public void Edit(decimal decMasterId)
        {
            try
            {
                int inRowCount = dgvPaymentVoucher.RowCount;
                int inTableRowCount = dtblPartyBalance.Rows.Count;
                int inB = 0;
                PaymentMasterInfo InfoPaymentMaster = new PaymentMasterInfo();
                //PaymentMasterSP SpPaymentMaster = new PaymentMasterSP();
                PaymentDetailsInfo InfoPaymentDetails = new PaymentDetailsInfo();
                //PaymentDetailsSP SpPaymentDetails = new PaymentDetailsSP();
                PaymentVoucherBll BllPaymentVoucher = new PaymentVoucherBll();
                LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
                LedgerPostingInfo InfoLegerPosting = new LedgerPostingInfo();
                PartyBalanceInfo InfopartyBalance = new PartyBalanceInfo();
                PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
                BankReconciliationBll BllBankReconcilation = new BankReconciliationBll();
                InfoPaymentMaster.Date = dtpDate.Value;
                InfoPaymentMaster.PaymentMasterId = decMasterId;
                InfoPaymentMaster.Extra1 = string.Empty;
                InfoPaymentMaster.Extra2 = string.Empty;
                InfoPaymentMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                InfoPaymentMaster.InvoiceNo = txtVoucherNo.Text.Trim();
                InfoPaymentMaster.LedgerId = Convert.ToDecimal(cmbBankorCash.SelectedValue.ToString());
                InfoPaymentMaster.Narration = txtNarration.Text.Trim();
                InfoPaymentMaster.SuffixPrefixId = decDailySuffixPrefixId;
                decimal decTotalAmount = TotalAmountCalculation();
                InfoPaymentMaster.TotalAmount = decTotalAmount;
                InfoPaymentMaster.UserId = PublicVariables._decCurrentUserId;

                InfoPaymentMaster.VoucherNo = strVoucherNo;

                InfoPaymentMaster.VoucherTypeId = decPaymentVoucherTypeId;
                decimal decPaymentMasterId = BllPaymentVoucher.PaymentMasterEdit(InfoPaymentMaster);
                if (decPaymentmasterId != 0)
                {
                    MasterLedgerPostingEdit();
                }
                foreach (object obj in arrlstOfRemove)
                {
                    string str = Convert.ToString(obj);
                    BllPaymentVoucher.PaymentDetailsDelete(Convert.ToDecimal(str));
                    BllLedgerPosting.LedgerPostDeleteByDetailsId(Convert.ToDecimal(str), strVoucherNo, decPaymentVoucherTypeId);
                }
                BllLedgerPosting.LedgerPostingDeleteByVoucherNoVoucherTypeIdAndLedgerId(strVoucherNo, decPaymentVoucherTypeId, 12);
                decimal decPaymentDetailsId1 = 0;
                for (int inI = 0; inI < inRowCount - 1; inI++)
                {
                    InfoPaymentDetails.Amount = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                    InfoPaymentDetails.ExchangeRateId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString());
                    InfoPaymentDetails.Extra1 = string.Empty;
                    InfoPaymentDetails.Extra2 = string.Empty;
                    if (dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() != string.Empty)
                    {
                        InfoPaymentDetails.LedgerId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString());
                    }
                    if (dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                    {
                        InfoPaymentDetails.ChequeNo = dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString();
                        if (dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                        {
                            InfoPaymentDetails.ChequeDate = Convert.ToDateTime(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value);
                        }
                        else
                        {
                            InfoPaymentDetails.ChequeDate = DateTime.Now;
                        }
                    }
                    else
                    {
                        InfoPaymentDetails.ChequeNo = string.Empty;
                        InfoPaymentDetails.ChequeDate = DateTime.Now;
                    }
                    InfoPaymentDetails.PaymentMasterId = decPaymentMasterId;
                    if (dgvPaymentVoucher.Rows[inI].Cells["dgvtxtpaymentDetailsId"].FormattedValue.ToString() == "0")//if new rows are added
                    {
                        if (dgvPaymentVoucher.Rows[inI].HeaderCell.Value.ToString() != "X")//add new rows added which are completed
                        {
                            decimal decPaymentDetailsId = BllPaymentVoucher.PaymentDetailsAdd(InfoPaymentDetails);//to add new rows in payment details
                            if (decPaymentDetailsId != 0)
                            {
                                for (int inJ = 0; inJ < inTableRowCount; inJ++)
                                {
                                    if (dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() == dtblPartyBalance.Rows[inJ]["LedgerId"].ToString())
                                    {
                                        PartyBalanceAddOrEdit(inJ);
                                    }
                                }
                                inB++;
                                DetailsLedgerPosting(inI, decPaymentDetailsId);//to add new ledger posting
                            }
                        }
                    }
                    else
                    {
                        if (dgvPaymentVoucher.Rows[inI].HeaderCell.Value != null && dgvPaymentVoucher.Rows[inI].HeaderCell.Value.ToString() != "X")//add new rows updated which are completed
                        {
                            InfoPaymentDetails.PaymentDetailsId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtpaymentDetailsId"].Value.ToString());
                            decimal decPaymentDetailsId = BllPaymentVoucher.PaymentDetailsEdit(InfoPaymentDetails);//to edit rows
                            if (decPaymentDetailsId != 0)
                            {
                                for (int inJ = 0; inJ < inTableRowCount; inJ++)
                                {
                                    if (dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() == dtblPartyBalance.Rows[inJ]["LedgerId"].ToString())
                                    {
                                        PartyBalanceAddOrEdit(inJ);
                                    }
                                }
                                inB++;
                                decPaymentDetailsId1 = InfoPaymentDetails.PaymentDetailsId;
                                decimal decLedgerPostId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtLedgerPostingId"].Value.ToString());
                                DetailsLedgerPostingEdit(inI, decLedgerPostId, decPaymentDetailsId1);
                            }
                        }
                        else
                        {
                            decimal decDetailsId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtpaymentDetailsId"].Value.ToString());
                            BllPaymentVoucher.PaymentDetailsDelete(decDetailsId);
                            BllLedgerPosting.LedgerPostDeleteByDetailsId(decDetailsId, strVoucherNo, decPaymentVoucherTypeId);
                            for (int inJ = 0; inJ < dtblPartyBalance.Rows.Count; inJ++)
                            {
                                if (dtblPartyBalance.Rows.Count == inJ)
                                {
                                    break;
                                }
                                if (dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() != string.Empty)
                                {
                                    if (dtblPartyBalance.Rows[inJ]["LedgerId"].ToString() == dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString())
                                    {
                                        if (dtblPartyBalance.Rows[inJ]["PartyBalanceId"].ToString() != "0")
                                        {
                                            arrlstOfDeletedPartyBalanceRow.Add(dtblPartyBalance.Rows[inJ]["PartyBalanceId"]);
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
                DeletePartyBalanceOfRemovedRow();
                isUpdated = true;
                Messages.UpdatedMessage();
                if (cbxPrintafterSave.Checked)
                {
                    if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                    {
                        PrintForDotMatrix(decPaymentmasterId);
                    }
                    else
                    {
                        Print(decPaymentMasterId);
                    }
                }
                if (frmPaymentRegisterObj != null)
                {
                    this.Close();
                    frmPaymentRegisterObj.CallFromPaymentVoucher(this);
                }
                if (frmPaymentReportObj != null)
                {
                    this.Close();
                    frmPaymentReportObj.CallFromPaymentVoucher(this);
                }
                if (frmDayBookObj != null)
                {
                    this.Close();
                }
                if (frmBillallocationObj != null)
                {
                    this.Close();
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("PV18:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Save Function
        /// </summary>
        public void Save()
        {
            try
            {
                int inGridRowCount = dgvPaymentVoucher.RowCount;
                int inTableRowCount = dtblPartyBalance.Rows.Count;
                int inB = 0;
                PaymentMasterInfo InfoPaymentMaster = new PaymentMasterInfo();
                //PaymentMasterSP SpPaymentMaster = new PaymentMasterSP();
                PaymentDetailsInfo InfoPaymentDetails = new PaymentDetailsInfo();
                //PaymentDetailsSP SpPaymentDetails = new PaymentDetailsSP();
                PaymentVoucherBll BllPaymentVoucher = new PaymentVoucherBll();
                PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
                PartyBalanceInfo InfopartyBalance = new PartyBalanceInfo();
                DateValidation objVal = new DateValidation();
                TextBox txtDate1 = new TextBox();
                InfoPaymentMaster.Date = dtpDate.Value;
                InfoPaymentMaster.Extra1 = string.Empty;
                InfoPaymentMaster.Extra2 = string.Empty;
                InfoPaymentMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                InfoPaymentMaster.InvoiceNo = txtVoucherNo.Text.Trim(); ;
                InfoPaymentMaster.LedgerId = Convert.ToDecimal(cmbBankorCash.SelectedValue.ToString());
                InfoPaymentMaster.Narration = txtNarration.Text.Trim();
                InfoPaymentMaster.SuffixPrefixId = decDailySuffixPrefixId;
                decimal decTotalAmount = TotalAmountCalculation();
                InfoPaymentMaster.TotalAmount = decTotalAmount;
                InfoPaymentMaster.UserId = PublicVariables._decCurrentUserId;
                InfoPaymentMaster.VoucherNo = strVoucherNo;
                InfoPaymentMaster.VoucherTypeId = decPaymentVoucherTypeId;
                decimal decPaymentMasterId = BllPaymentVoucher.PaymentMasterAdd(InfoPaymentMaster);
                if (decPaymentMasterId != 0)
                {
                    MasterLedgerPosting();
                }
                for (int inI = 0; inI < inGridRowCount - 1; inI++)
                {
                    if (dgvPaymentVoucher.Rows[inI].HeaderCell.Value.ToString() != "X")
                    {
                        InfoPaymentDetails.Amount = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                        InfoPaymentDetails.ExchangeRateId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString());
                        InfoPaymentDetails.Extra1 = string.Empty;
                        InfoPaymentDetails.Extra2 = string.Empty;
                        InfoPaymentDetails.LedgerId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString());
                        InfoPaymentDetails.PaymentMasterId = decPaymentMasterId;
                        if (dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() != string.Empty)
                        {
                            InfoPaymentDetails.LedgerId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString());
                        }
                        if (dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                        {
                            InfoPaymentDetails.ChequeNo = dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString();

                            if (dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value != null && dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                            {
                                InfoPaymentDetails.ChequeDate = Convert.ToDateTime(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value);
                            }
                            else
                            {
                                InfoPaymentDetails.ChequeDate = DateTime.Now;
                            }
                        }
                        else
                        {
                            InfoPaymentDetails.ChequeNo = string.Empty;
                            InfoPaymentDetails.ChequeDate = DateTime.Now;
                        }
                        decimal decPaymentDetailsId = BllPaymentVoucher.PaymentDetailsAdd(InfoPaymentDetails);
                        if (decPaymentDetailsId != 0)
                        {
                            for (int inJ = 0; inJ < inTableRowCount; inJ++)
                            {
                                if (dgvPaymentVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() == dtblPartyBalance.Rows[inJ]["LedgerId"].ToString())
                                {
                                    PartyBalanceAddOrEdit(inJ);
                                }
                            }
                            inB++;
                            DetailsLedgerPosting(inI, decPaymentDetailsId);
                        }
                    }
                }
                Messages.SavedMessage();
                if (cbxPrintafterSave.Checked)
                {
                    if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                    {
                        PrintForDotMatrix(decPaymentmasterId);
                    }
                    else
                    {
                        Print(decPaymentMasterId);
                    }
                }
                Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("PV17:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }