/// <summary>
 /// TotalAmountCalculation
 /// </summary>
 /// <returns></returns>
 public decimal TotalAmountCalculation()
 {
     decimal decTotal = 0;
     decimal decSelectedCurrencyRate = 0;
     ExchangeRateSP SpExchangRate = new ExchangeRateSP();
     try
     {
         for (int inI = 0; inI < dgvPaymentVoucher.RowCount - 1; inI++)
         {
             if (dgvPaymentVoucher.Rows[inI].HeaderCell.Value.ToString() != "X")
             {
                 decSelectedCurrencyRate = SpExchangRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString()));//Exchange rate of grid's row
                 decTotal = decTotal + (Convert.ToDecimal(dgvPaymentVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString()) * decSelectedCurrencyRate);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PV16:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     return decTotal;
 }
 /// <summary>
 /// Total Amount Calculation
 /// </summary>
 public void TotalAmount()
 {
     try
     {
         decimal decTotalAmount = 0;
         decimal decSelectedCurrencyRate = 0;
         ExchangeRateSP SpExchangRate = new ExchangeRateSP();
         foreach (DataGridViewRow dr in dgvPaymentVoucher.Rows)
         {
             if (dr.Cells["dgvtxtAmount"].Value != null && dr.Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
             {
                 if (dr.Cells["dgvcmbCurrency"].Value != null)
                 {
                     decSelectedCurrencyRate = SpExchangRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dr.Cells["dgvcmbCurrency"].Value.ToString()));//Exchange rate of grid's row
                     decTotalAmount = decTotalAmount + (Convert.ToDecimal(dr.Cells["dgvtxtAmount"].Value.ToString()) * decSelectedCurrencyRate);
                 }
                 else
                 {
                     decTotalAmount = decTotalAmount + Convert.ToDecimal(dr.Cells["dgvtxtAmount"].Value.ToString());
                 }
             }
         }
         txtTotal.Text = Math.Round(decTotalAmount, Convert.ToInt32(PublicVariables._inNoOfDecimalPlaces.ToString())).ToString();
     }
     catch (Exception ex)
     {
         MessageBox.Show("PV20:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Details ledger posting edit
        /// </summary>
        /// <param name="inA"></param>
        /// <param name="decLedgerPostingId"></param>
        /// <param name="decPaymentDetailsId"></param>
        public void DetailsLedgerPostingEdit(int inA, decimal decLedgerPostingId, decimal decPaymentDetailsId)
        {
            LedgerPostingInfo InfoLedgerPosting = new LedgerPostingInfo();
            LedgerPostingSP SpLedgerPosting = new LedgerPostingSP();
            ExchangeRateSP SpExchangRate = new ExchangeRateSP();
            decimal decOldExchange = 0;
            decimal decNewExchangeRate = 0;
            decimal decNewExchangeRateId = 0;
            decimal decOldExchangeId = 0;
            try
            {
                if (!dgvPaymentVoucher.Rows[inA].Cells["dgvtxtAmount"].ReadOnly)
                {
                    decSelectedCurrencyRate = SpExchangRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvPaymentVoucher.Rows[inA].Cells["dgvcmbCurrency"].Value.ToString()));
                    decAmount = Convert.ToDecimal(dgvPaymentVoucher.Rows[inA].Cells["dgvtxtAmount"].Value.ToString());
                    decConvertRate = decAmount * decSelectedCurrencyRate;
                    InfoLedgerPosting.Credit = 0;
                    InfoLedgerPosting.Date = dtpDate.Value;
                    InfoLedgerPosting.Debit = decConvertRate;
                    InfoLedgerPosting.DetailsId = decPaymentDetailsId;
                    InfoLedgerPosting.Extra1 = string.Empty;
                    InfoLedgerPosting.Extra2 = string.Empty;
                    InfoLedgerPosting.InvoiceNo = strInvoiceNo;
                    InfoLedgerPosting.LedgerId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inA].Cells["dgvcmbAccountLedger"].Value.ToString());
                    if (!isAutomatic)
                    {
                        InfoLedgerPosting.VoucherNo = txtVoucherNo.Text.Trim();
                    }
                    else
                    {
                        InfoLedgerPosting.VoucherNo = strVoucherNo;
                    }
                    if (dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value != null && dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                    {
                        InfoLedgerPosting.ChequeNo = dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString();
                        if (dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value != null && dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                        {
                            InfoLedgerPosting.ChequeDate = Convert.ToDateTime(dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString());
                        }
                        else
                            InfoLedgerPosting.ChequeDate = DateTime.Now;
                    }
                    else
                    {
                        InfoLedgerPosting.ChequeNo = string.Empty;
                        InfoLedgerPosting.ChequeDate = DateTime.Now;
                    }

                    InfoLedgerPosting.VoucherTypeId = decPaymentVoucherTypeId;
                    InfoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    InfoLedgerPosting.LedgerPostingId = decLedgerPostingId;
                    SpLedgerPosting.LedgerPostingEdit(InfoLedgerPosting);
                }
                else
                {
                    InfoLedgerPosting.Date = dtpDate.Value;

                    InfoLedgerPosting.Extra1 = string.Empty;
                    InfoLedgerPosting.Extra2 = string.Empty;
                    InfoLedgerPosting.InvoiceNo = strInvoiceNo;
                    InfoLedgerPosting.VoucherTypeId = decPaymentVoucherTypeId;
                    InfoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    InfoLedgerPosting.Credit = 0;
                    InfoLedgerPosting.LedgerId = Convert.ToDecimal(dgvPaymentVoucher.Rows[inA].Cells["dgvcmbAccountLedger"].Value.ToString());
                    InfoLedgerPosting.VoucherNo = strVoucherNo;
                    InfoLedgerPosting.DetailsId = decPaymentDetailsId;
                    InfoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                    if (dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value != null && dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                    {
                        InfoLedgerPosting.ChequeNo = dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString();
                        if (dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value != null && dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                        {
                            InfoLedgerPosting.ChequeDate = Convert.ToDateTime(dgvPaymentVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString());
                        }
                        else
                            InfoLedgerPosting.ChequeDate = DateTime.Now;
                    }
                    else
                    {
                        InfoLedgerPosting.ChequeNo = string.Empty;
                        InfoLedgerPosting.ChequeDate = DateTime.Now;
                    }

                    foreach (DataRow dr in dtblPartyBalance.Rows)
                    {
                        if (InfoLedgerPosting.LedgerId == Convert.ToDecimal(dr["LedgerId"].ToString()))
                        {
                            decOldExchange = Convert.ToDecimal(dr["OldExchangeRate"].ToString());
                            decNewExchangeRateId = Convert.ToDecimal(dr["CurrencyId"].ToString());
                            decSelectedCurrencyRate = SpExchangRate.GetExchangeRateByExchangeRateId(decOldExchange);
                            decAmount = Convert.ToDecimal(dr["Amount"].ToString());
                            decConvertRate = decConvertRate + (decAmount * decSelectedCurrencyRate);

                        }
                    }
                    InfoLedgerPosting.Debit = decConvertRate;
                    InfoLedgerPosting.LedgerPostingId = decLedgerPostingId;
                    SpLedgerPosting.LedgerPostingEdit(InfoLedgerPosting);
                    InfoLedgerPosting.LedgerId = 12;
                    foreach (DataRow dr in dtblPartyBalance.Rows)
                    {
                        if (Convert.ToDecimal(dgvPaymentVoucher.Rows[inA].Cells["dgvcmbAccountLedger"].Value.ToString()) == Convert.ToDecimal(dr["LedgerId"].ToString()))
                        {
                            if (dr["ReferenceType"].ToString() == "Against")
                            {
                                decNewExchangeRateId = Convert.ToDecimal(dr["CurrencyId"].ToString());
                                decNewExchangeRate = SpExchangRate.GetExchangeRateByExchangeRateId(decNewExchangeRateId);
                                decOldExchangeId = Convert.ToDecimal(dr["OldExchangeRate"].ToString());
                                decOldExchange = SpExchangRate.GetExchangeRateByExchangeRateId(decOldExchangeId);
                                decAmount = Convert.ToDecimal(dr["Amount"].ToString());
                                decimal decForexAmount = (decAmount * decNewExchangeRate) - (decAmount * decOldExchange);
                                if (decForexAmount >= 0)
                                {

                                    InfoLedgerPosting.Debit = decForexAmount;
                                    InfoLedgerPosting.Credit = 0;
                                }
                                else
                                {
                                    InfoLedgerPosting.Credit = -1 * decForexAmount;
                                    InfoLedgerPosting.Debit = 0;
                                }
                                SpLedgerPosting.LedgerPostingAdd(InfoLedgerPosting);
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PV24:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function to calculate total debit and credit
        /// </summary>
        public void DebitAndCreditTotal()
        {
            ExchangeRateSP spExchangeRate = new ExchangeRateSP();
            int inRowCount = dgvJournalVoucher.RowCount;
            decimal decTxtTotalDebit = 0;
            decimal decTxtTotalCredit = 0;
            try
            {
                for (int inI = 0; inI < inRowCount; inI++)
                {
                    if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value.ToString() != string.Empty)
                    {
                        if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
                        {
                            if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString() != string.Empty)
                            {
                                if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString() != ".")
                                {
                                    if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value.ToString() == "Dr")
                                    {

                                        //--------Currency conversion--------------//
                                        decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString()));
                                        decAmount = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                                        decConvertRate = decAmount * decSelectedCurrencyRate;
                                        //===========================================//
                                        decTxtTotalDebit = decTxtTotalDebit + decConvertRate;


                                    }
                                    else
                                    {
                                        //--------Currency conversion--------------//
                                        decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString()));
                                        decAmount = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                                        decConvertRate = decAmount * decSelectedCurrencyRate;
                                        //===========================================//
                                        decTxtTotalCredit = decTxtTotalCredit + decConvertRate;
                                    }
                                }
                            }
                        }
                    }

                    txtDebitTotal.Text = Math.Round(decTxtTotalDebit, PublicVariables._inNoOfDecimalPlaces).ToString();
                    txtCreditTotal.Text = Math.Round(decTxtTotalCredit, Convert.ToInt16(PublicVariables._inNoOfDecimalPlaces)).ToString();

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("JV12:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        /// <summary>
        /// Function to Save the voucher
        /// </summary>
        public void Save()
        {
            try
            {
                decimal decTotalDebit = 0;
                decimal decTotalCredit = 0;

                decTotalDebit = Convert.ToDecimal(txtDebitTotal.Text.Trim());
                decTotalCredit = Convert.ToDecimal(txtCreditTotal.Text.Trim());

                JournalMasterSP spJournalMaster = new JournalMasterSP();
                JournalDetailsSP spJournalDetails = new JournalDetailsSP();
                JournalMasterInfo infoJournalMaster = new JournalMasterInfo();
                JournalDetailsInfo infoJournalDetails = new JournalDetailsInfo();
                PartyBalanceSP SpPartyBalance = new PartyBalanceSP();
                PartyBalanceInfo InfopartyBalance = new PartyBalanceInfo();
                ExchangeRateSP spExchangeRate = new ExchangeRateSP();

                infoJournalMaster.VoucherNo = strVoucherNo;
                infoJournalMaster.InvoiceNo = txtVoucherNo.Text;
                infoJournalMaster.SuffixPrefixId = decJournalSuffixPrefixId;
                infoJournalMaster.Date = Convert.ToDateTime(txtDate.Text);
                infoJournalMaster.Narration = txtNarration.Text.Trim();
                infoJournalMaster.UserId = PublicVariables._decCurrentUserId;
                infoJournalMaster.VoucherTypeId = decJournalVoucherTypeId;
                infoJournalMaster.FinancialYearId = Convert.ToDecimal(PublicVariables._decCurrentFinancialYearId.ToString());

                infoJournalMaster.Extra1 = string.Empty;
                infoJournalMaster.Extra2 = string.Empty;
                infoJournalMaster.ExtraDate = DateTime.Now;


                infoJournalMaster.TotalAmount = decTotalDebit;
                decimal decJournalMasterId = spJournalMaster.JournalMasterAdd(infoJournalMaster);

                /*******************JournalDetailsAdd and LedgerPosting*************************/
                infoJournalDetails.JournalMasterId = decJournalMasterId;
                infoJournalDetails.ExtraDate = DateTime.Now;
                infoJournalDetails.Extra1 = string.Empty;
                infoJournalDetails.Extra2 = string.Empty;


                decimal decLedgerId = 0;
                decimal decDebit = 0;
                decimal decCredit = 0;
                int inRowCount = dgvJournalVoucher.RowCount;
                for (int inI = 0; inI < inRowCount - 1; inI++)
                {
                    if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() != string.Empty)
                    {
                        infoJournalDetails.LedgerId = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString());
                        decLedgerId = infoJournalDetails.LedgerId;
                    }
                    if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value.ToString() != string.Empty)
                    {
                        if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
                        {
                            //--------Currency conversion--------------//
                            decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString()));
                            decAmount = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                            decConvertRate = decAmount * decSelectedCurrencyRate;
                            //===========================================//
                            if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value.ToString() == "Dr")
                            {
                                infoJournalDetails.Debit = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                                infoJournalDetails.Credit = 0;
                                decDebit = decConvertRate;
                                decCredit = infoJournalDetails.Credit;
                            }
                            else
                            {
                                infoJournalDetails.Credit = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                                infoJournalDetails.Debit = 0;
                                decDebit = infoJournalDetails.Debit;
                                decCredit = decConvertRate;
                            }
                        }
                        infoJournalDetails.ExchangeRateId = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString());
                        if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                        {
                            infoJournalDetails.ChequeNo = dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString();
                        }
                        else
                        {
                            infoJournalDetails.ChequeNo = string.Empty;
                        }
                        if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                        {
                            infoJournalDetails.ChequeDate = Convert.ToDateTime(dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value.ToString());
                        }
                        else
                        {
                            infoJournalDetails.ChequeDate = DateTime.Now;
                        }
                        decimal decJournalDetailsId = spJournalDetails.JournalDetailsAdd(infoJournalDetails);
                        if (decJournalDetailsId != 0)
                        {
                            PartyBalanceAddOrEdit(inI);
                            LedgerPosting(decLedgerId, decCredit, decDebit, decJournalDetailsId, inI);
                        }
                    }

                }

                Messages.SavedMessage();

                //----------------If print after save is enable-----------------------//
                if (cbxPrintAfterSave.Checked)
                {
                    if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                    {
                        PrintForDotMatrix(decJournalMasterId);
                    }
                    else
                    {
                        Print(decJournalMasterId);
                    }
                }

                //===================================================================//
            }
            catch (Exception ex)
            {
                MessageBox.Show("JV14:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function to edit the debitnote voucher
        /// </summary>
        /// <param name="decJournalMasterId"></param>
        public void Edit(decimal decJournalMasterId)
        {
            try
            {
                JournalMasterSP spJournalMaster = new JournalMasterSP();
                JournalMasterInfo infoJournalMaster = new JournalMasterInfo();
                JournalDetailsSP spJournalDetails = new JournalDetailsSP();
                JournalDetailsInfo infoJournalDetails = new JournalDetailsInfo();
                ExchangeRateSP spExchangeRate = new ExchangeRateSP();

                /*****************Update in JournalMaster table *************/

                decimal decTotalDebit = 0;
                decimal decTotalCredit = 0;

                infoJournalMaster.JournalMasterId = decJournalMasterId;
                infoJournalMaster.VoucherNo = strVoucherNo;
                infoJournalMaster.InvoiceNo = txtVoucherNo.Text.Trim();
                infoJournalMaster.SuffixPrefixId = decJournalSuffixPrefixId;
                infoJournalMaster.Date = Convert.ToDateTime(txtDate.Text);
                infoJournalMaster.Narration = txtNarration.Text.Trim();
                infoJournalMaster.UserId = PublicVariables._decCurrentUserId;
                infoJournalMaster.VoucherTypeId = decJournalVoucherTypeId;
                infoJournalMaster.FinancialYearId = Convert.ToDecimal(PublicVariables._decCurrentFinancialYearId.ToString());
                infoJournalMaster.ExtraDate = DateTime.Now;
                infoJournalMaster.Extra1 = string.Empty;
                infoJournalMaster.Extra2 = string.Empty;


                decTotalDebit = Convert.ToDecimal(txtDebitTotal.Text.Trim());
                decTotalCredit = Convert.ToDecimal(txtCreditTotal.Text.Trim());

                infoJournalMaster.TotalAmount = decTotalDebit;
                decimal decEffectRow = spJournalMaster.JournalMasterEdit(infoJournalMaster);

                /**********************JournalDetails Edit********************/
                if (decEffectRow > 0)
                {
                    infoJournalDetails.JournalMasterId = decJournalMasterId;
                    infoJournalDetails.ExtraDate = DateTime.Now;
                    infoJournalDetails.Extra1 = string.Empty;
                    infoJournalDetails.Extra2 = string.Empty;

                    //-----------to delete details, LedgerPosting and bankReconciliation of removed rows--------------// 
                    LedgerPostingSP spLedgerPosting = new LedgerPostingSP();

                    foreach (object obj in arrlstOfRemove)
                    {
                        string str = Convert.ToString(obj);
                        spJournalDetails.JournalDetailsDelete(Convert.ToDecimal(str));
                        spLedgerPosting.LedgerPostDeleteByDetailsId(Convert.ToDecimal(str), strVoucherNo, decJournalVoucherTypeId);
                    }
                    spLedgerPosting.LedgerPostingDeleteByVoucherNoVoucherTypeIdAndLedgerId(strVoucherNo, decJournalVoucherTypeId, 12);
                    //=============================================================================================//

                    decimal decLedgerId = 0;
                    decimal decDebit = 0;
                    decimal decCredit = 0;
                    decimal decJournalDetailsId = 0;
                    int inRowCount = dgvJournalVoucher.RowCount;
                    for (int inI = 0; inI < inRowCount; inI++)
                    {
                        if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString() != string.Empty)
                        {
                            infoJournalDetails.LedgerId = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbAccountLedger"].Value.ToString());
                            decLedgerId = infoJournalDetails.LedgerId;
                        }
                        if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value.ToString() != string.Empty)
                        {
                            //------------------Currency conversion------------------//
                            decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value));
                            decAmount = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                            decConvertRate = decAmount * decSelectedCurrencyRate;
                            //======================================================//

                            if (dgvJournalVoucher.Rows[inI].Cells["dgvcmbDrOrCr"].Value.ToString() == "Dr")
                            {
                                infoJournalDetails.Debit = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                                infoJournalDetails.Credit = 0;

                                decDebit = decConvertRate;
                                decCredit = infoJournalDetails.Credit;
                            }
                            else
                            {
                                infoJournalDetails.Credit = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtAmount"].Value.ToString());
                                infoJournalDetails.Debit = 0;
                                decDebit = infoJournalDetails.Debit;
                                decCredit = decConvertRate;
                            }
                            infoJournalDetails.ExchangeRateId = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvcmbCurrency"].Value.ToString());
                            if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                            {
                                infoJournalDetails.ChequeNo = dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeNo"].Value.ToString();
                            }
                            else
                            {
                                infoJournalDetails.ChequeNo = string.Empty;
                            }
                            if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                            {
                                infoJournalDetails.ChequeDate = Convert.ToDateTime(dgvJournalVoucher.Rows[inI].Cells["dgvtxtChequeDate"].Value.ToString());
                            }
                            else
                            {
                                infoJournalDetails.ChequeDate = DateTime.Now;
                            }
                            if (dgvJournalVoucher.Rows[inI].Cells["dgvtxtDetailsId"].Value != null && dgvJournalVoucher.Rows[inI].Cells["dgvtxtDetailsId"].Value.ToString() != string.Empty)
                            {
                                infoJournalDetails.JournalDetailsId = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtDetailsId"].Value.ToString());
                                spJournalDetails.JournalDetailsEdit(infoJournalDetails);
                                PartyBalanceAddOrEdit(inI);
                                decJournalDetailsId = infoJournalDetails.JournalDetailsId;
                                decimal decLedgerPostId = Convert.ToDecimal(dgvJournalVoucher.Rows[inI].Cells["dgvtxtLedgerPostingId"].Value.ToString());
                                LedgerPostingEdit(decLedgerPostId, decLedgerId, decCredit, decDebit, decJournalDetailsId, inI);

                            }
                            else
                            {
                                decJournalDetailsId = spJournalDetails.JournalDetailsAdd(infoJournalDetails);
                                PartyBalanceAddOrEdit(inI);
                                LedgerPosting(decLedgerId, decCredit, decDebit, decJournalDetailsId, inI);
                            }

                        }

                    }
                    DeletePartyBalanceOfRemovedRow();
                    Messages.UpdatedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                        {
                            PrintForDotMatrix(infoJournalMaster.JournalMasterId);
                        }
                        else
                        {
                            Print(infoJournalMaster.JournalMasterId);
                        }
                    }
                    if (journalRegisterObj != null)
                    {
                        this.Close();
                        journalRegisterObj.Enabled = true;
                    }
                    else if (frmJournalReportObj != null)
                    {
                        this.Close();
                        frmJournalReportObj.Enabled = true;
                    }
                    else if (frmDayBookObj != null)
                    {
                        this.Close();

                    }
                    else if (frmBillallocationObj != null)
                    {
                        this.Close();

                    }
                }
                //----------------If print after save is enable-----------------------//


                //===================================================================//
            }
            catch (Exception ex)
            {
                MessageBox.Show("JV16:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function for ledger posting
        /// </summary>
        /// <param name="decId"></param>
        /// <param name="decCredit"></param>
        /// <param name="decDebit"></param>
        /// <param name="decDetailsId"></param>
        /// <param name="inA"></param>
        public void LedgerPosting(decimal decId, decimal decCredit, decimal decDebit, decimal decDetailsId, int inA)
        {
            LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
            LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
            ExchangeRateSP SpExchangRate = new ExchangeRateSP();
            decimal decOldExchange = 0;
            decimal decNewExchangeRate = 0;
            decimal decNewExchangeRateId = 0;
            decimal decOldExchangeId = 0;
            try
            {

                if (!dgvJournalVoucher.Rows[inA].Cells["dgvtxtAmount"].ReadOnly)
                {

                    infoLedgerPosting.Date = PublicVariables._dtCurrentDate;
                    infoLedgerPosting.VoucherTypeId = decJournalVoucherTypeId;
                    infoLedgerPosting.VoucherNo = strVoucherNo;
                    infoLedgerPosting.DetailsId = decDetailsId;
                    infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();

                    if (dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value != null && dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                    {
                        infoLedgerPosting.ChequeNo = dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString();
                        if (dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value != null && dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                        {
                            infoLedgerPosting.ChequeDate = Convert.ToDateTime(dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString());
                        }
                        else
                            infoLedgerPosting.ChequeDate = DateTime.Now;

                    }
                    else
                    {
                        infoLedgerPosting.ChequeNo = string.Empty;
                        infoLedgerPosting.ChequeDate = DateTime.Now;
                    }


                    infoLedgerPosting.ExtraDate = DateTime.Now;
                    infoLedgerPosting.Extra1 = string.Empty;
                    infoLedgerPosting.Extra2 = string.Empty;

                    infoLedgerPosting.LedgerId = decId;
                    infoLedgerPosting.Credit = decCredit;
                    infoLedgerPosting.Debit = decDebit;

                    spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                }
                else
                {
                    infoLedgerPosting.Date = PublicVariables._dtCurrentDate;
                    infoLedgerPosting.VoucherTypeId = decJournalVoucherTypeId;
                    infoLedgerPosting.VoucherNo = strVoucherNo;
                    infoLedgerPosting.DetailsId = decDetailsId;
                    infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();

                    if (dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value != null && dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                    {
                        infoLedgerPosting.ChequeNo = dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeNo"].Value.ToString();
                        if (dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value != null && dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                        {
                            infoLedgerPosting.ChequeDate = Convert.ToDateTime(dgvJournalVoucher.Rows[inA].Cells["dgvtxtChequeDate"].Value.ToString());
                        }
                        else
                            infoLedgerPosting.ChequeDate = DateTime.Now;

                    }
                    else
                    {
                        infoLedgerPosting.ChequeNo = string.Empty;
                        infoLedgerPosting.ChequeDate = DateTime.Now;
                    }


                    infoLedgerPosting.ExtraDate = DateTime.Now;
                    infoLedgerPosting.Extra1 = string.Empty;
                    infoLedgerPosting.Extra2 = string.Empty;
                    infoLedgerPosting.LedgerId = decId;

                    foreach (DataRow dr in dtblPartyBalance.Rows)
                    {
                        if (infoLedgerPosting.LedgerId == Convert.ToDecimal(dr["LedgerId"].ToString()))
                        {
                            decOldExchange = Convert.ToDecimal(dr["OldExchangeRate"].ToString());
                            decNewExchangeRateId = Convert.ToDecimal(dr["CurrencyId"].ToString());
                            decSelectedCurrencyRate = SpExchangRate.GetExchangeRateByExchangeRateId(decOldExchange);
                            decAmount = Convert.ToDecimal(dr["Amount"].ToString());
                            decConvertRate = decConvertRate + (decAmount * decSelectedCurrencyRate);

                        }
                    }

                    if (decCredit == 0)
                    {
                        infoLedgerPosting.Credit = 0;
                        infoLedgerPosting.Debit = decConvertRate;
                    }
                    else
                    {
                        infoLedgerPosting.Debit = 0;
                        infoLedgerPosting.Credit = decConvertRate;
                    }
                    spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);

                    infoLedgerPosting.LedgerId = 12;
                    foreach (DataRow dr in dtblPartyBalance.Rows)
                    {
                        if (Convert.ToDecimal(dgvJournalVoucher.Rows[inA].Cells["dgvcmbAccountLedger"].Value.ToString()) == Convert.ToDecimal(dr["LedgerId"].ToString()))
                        {
                            if (dr["ReferenceType"].ToString() == "Against")
                            {
                                decNewExchangeRateId = Convert.ToDecimal(dr["CurrencyId"].ToString());
                                decNewExchangeRate = SpExchangRate.GetExchangeRateByExchangeRateId(decNewExchangeRateId);
                                decOldExchangeId = Convert.ToDecimal(dr["OldExchangeRate"].ToString());
                                decOldExchange = SpExchangRate.GetExchangeRateByExchangeRateId(decOldExchangeId);
                                decAmount = Convert.ToDecimal(dr["Amount"].ToString());
                                decimal decForexAmount = (decAmount * decNewExchangeRate) - (decAmount * decOldExchange);
                                if (dr["DebitOrCredit"].ToString() == "Dr")
                                {
                                    if (decForexAmount >= 0)
                                    {

                                        infoLedgerPosting.Debit = decForexAmount;
                                        infoLedgerPosting.Credit = 0;
                                    }
                                    else
                                    {
                                        infoLedgerPosting.Credit = -1 * decForexAmount;
                                        infoLedgerPosting.Debit = 0;
                                    }
                                }
                                else
                                {
                                    if (decForexAmount >= 0)
                                    {

                                        infoLedgerPosting.Credit = decForexAmount;
                                        infoLedgerPosting.Debit = 0;
                                    }
                                    else
                                    {
                                        infoLedgerPosting.Debit = -1 * decForexAmount;
                                        infoLedgerPosting.Credit = 0;
                                    }
                                }
                                spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("JV20:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }
 /// <summary>
 /// Function to save the service voucher
 /// </summary>
 public void SaveFunction()
 {
     try
     {
         ServiceMasterInfo infoServiceMaster = new ServiceMasterInfo();
         ServiceMasterSP spServiceMaster = new ServiceMasterSP();
         ServiceDetailsInfo infoServiceDetails = new ServiceDetailsInfo();
         ServiceDetailsSP spServiceDetails = new ServiceDetailsSP();
         LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
         LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
         ExchangeRateSP spExchangeRate = new ExchangeRateSP();
         int inRowCount = dgvServiceVoucher.RowCount;
         int inValue = 0;
         for (int ini = 0; ini < inRowCount - 1; ini++)
         {
             if (dgvServiceVoucher.Rows[ini].Cells["dgvcmbParticulars"].Value != null && dgvServiceVoucher.Rows[ini].Cells["dgvcmbParticulars"].Value.ToString() != string.Empty)
             {
                 inValue++;
             }
         }
         if (inValue > 0)
         {
             txtDiscount.Enabled = true;
             infoServiceMaster.InvoiceNo = txtInvoiceNumber.Text;
             if (isAutomatic)
             {
                 infoServiceMaster.VoucherNo = strVoucherNo;
             }
             else
             {
                 infoServiceMaster.VoucherNo = Convert.ToString(spServiceMaster.ServiceMasterGetMax(DecServicetVoucherTypeId) + 1);
             }
             infoServiceMaster.SuffixPrefixId = decServiceSuffixPrefixId;
             infoServiceMaster.Date = Convert.ToDateTime(txtVoucherDate.Text);
             infoServiceMaster.LedgerId = Convert.ToDecimal(cmbCashParty.SelectedValue.ToString());
             infoServiceMaster.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
             infoServiceMaster.Narration = txtNarration.Text.Trim();
             infoServiceMaster.UserId = PublicVariables._decCurrentUserId;
             infoServiceMaster.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text);
             infoServiceMaster.ServiceAccount = Convert.ToDecimal(cmbServiceAC.SelectedValue.ToString());
             decimal decExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());//spExchangeRate.GetExchangeRateByCurrencyId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
             infoServiceMaster.ExchangeRateId = decExchangeRateId;
             infoServiceMaster.EmployeeId = Convert.ToDecimal(cmbSalesman.SelectedValue.ToString());
             infoServiceMaster.Customer = txtCustomer.Text.Trim();
             infoServiceMaster.Discount = Convert.ToDecimal(txtDiscount.Text.Trim());
             infoServiceMaster.GrandTotal = Convert.ToDecimal(txtGrandTotal.Text);
             infoServiceMaster.VoucherTypeId = DecServicetVoucherTypeId;
             infoServiceMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
             infoServiceMaster.ExtraDate = PublicVariables._dtCurrentDate;
             infoServiceMaster.Extra1 = string.Empty;
             infoServiceMaster.Extra2 = string.Empty;
             decServiceMasterId = spServiceMaster.ServiceMasterAddReturnWithIdentity(infoServiceMaster);
             infoServiceDetails.ServiceMasterId = decServiceMasterId;
             infoServiceDetails.Extra1 = string.Empty;
             infoServiceDetails.Extra2 = string.Empty;
             infoServiceDetails.ExtraDate = PublicVariables._dtCurrentDate;
             for (int i = 0; i < inRowCount - 1; i++)
             {
                 if (dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value.ToString() != string.Empty)
                 {
                     infoServiceDetails.ServiceId = Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value.ToString());
                 }
                 if (dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value.ToString() != string.Empty)
                 {
                     infoServiceDetails.Measure = dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value.ToString();
                 }
                 if (dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
                 {
                     infoServiceDetails.Amount = Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                     decAmount += Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value);
                 }
                 decServiceDetailsId = spServiceDetails.ServiceDetailsAddReturnWithIdentity(infoServiceDetails);
             }
             decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(infoServiceMaster.ExchangeRateId);
             decConvertRate = decAmount * decSelectedCurrencyRate;
             decCredit = 0;
             decDebit = decConvertRate;
             decLedgerId = Convert.ToDecimal(cmbCashParty.SelectedValue.ToString());
             LedgerPosting(decLedgerId, decCredit, decDebit, decServiceDetailsId, infoServiceMaster.VoucherNo);
             AccountLedgerSP spAccountLedger = new AccountLedgerSP();
             decimal decI = Convert.ToDecimal(spAccountLedger.AccountGroupIdCheck(cmbCashParty.Text));
             if (decI > 0)
             {
                 PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
                 PartyBalanceSP spPartyBalance = new PartyBalanceSP();
                 infoPartyBalance.Date = Convert.ToDateTime(txtVoucherDate.Text);
                 infoPartyBalance.LedgerId = Convert.ToDecimal(cmbCashParty.SelectedValue.ToString());
                 infoPartyBalance.VoucherTypeId = DecServicetVoucherTypeId;
                 infoPartyBalance.VoucherNo = strVoucherNo;
                 infoPartyBalance.AgainstVoucherTypeId = 0;
                 infoPartyBalance.AgainstVoucherNo = "0";
                 infoPartyBalance.InvoiceNo = strInvoiceNo;
                 infoPartyBalance.AgainstInvoiceNo = "0";
                 infoPartyBalance.ReferenceType = "New";
                 infoPartyBalance.Debit = decAmount;
                 infoPartyBalance.Credit = 0;
                 infoPartyBalance.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text);
                 infoPartyBalance.ExchangeRateId = decExchangeRateId;
                 infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                 infoPartyBalance.Extra1 = string.Empty;
                 infoPartyBalance.Extra2 = string.Empty;
                 spPartyBalance.PartyBalanceAdd(infoPartyBalance);
             }
             Messages.SavedMessage();
             if (cbxPrintAfterSave.Checked)
             {
                 if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                 {
                     PrintForDotMatrix(decServiceMasterId);
                 }
                 else
                 {
                     Print(decServiceMasterId, infoServiceMaster.ExchangeRateId);
                 }
             }
             Clear();
         }
         else
         {
             Messages.InformationMessage("Can't save Service Voucher without atleast one ledger with complete details");
             dgvServiceVoucher.ClearSelection();
             dgvServiceVoucher.CurrentCell = dgvServiceVoucher.Rows[0].Cells["dgvcmbParticulars"];
             dgvServiceVoucher.Rows[0].Cells["dgvcmbParticulars"].Selected = true;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SV 19 : " + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// To Calculate TotalAmount
 /// </summary>
 private void CalculateTotalAmount()
 {
     try
     {
         ExchangeRateSP spExchangeRate = new ExchangeRateSP();
         decimal decTotal = 0;
         foreach (DataGridViewRow dgvrow in dgvProduct.Rows)
         {
             if (dgvrow.Cells["dgvtxtAmount"].Value != null && dgvrow.Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
             {
                 decTotal = decTotal + Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtAmount"].Value));
                 decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                 decTotal = decTotal * decSelectedCurrencyRate;
                 decTotal = Math.Round(decTotal, PublicVariables._inNoOfDecimalPlaces);
                 txtTotalAmount.Text = Convert.ToString(decTotal);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("RO20:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to edit the service voucher
 /// </summary>
 /// <param name="decServiceMasterId"></param>
 public void EditFunction(decimal decServiceMasterId)
 {
     try
     {
         ServiceMasterInfo infoServiceMaster = new ServiceMasterInfo();
         ServiceMasterSP spServiceMaster = new ServiceMasterSP();
         ServiceDetailsInfo infoServiceDetails = new ServiceDetailsInfo();
         ServiceDetailsSP spServiceDetails = new ServiceDetailsSP();
         LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
         LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
         ExchangeRateSP spExchangeRate = new ExchangeRateSP();
         int inRowCount = dgvServiceVoucher.RowCount;
         int inValue = 0;
         decimal decLedgerPostingId1 = 0;
         decimal decLedgerPostingId2 = 0;
         DataTable dtblLedgerPostingId = new DataTable();
         dtblLedgerPostingId = spServiceMaster.LedgerPostingIdByServiceMaasterId(decServiceMasterId);
         decLedgerPostingId1 = Convert.ToDecimal(dtblLedgerPostingId.Rows[0]["ledgerPostingId"].ToString());
         decLedgerPostingId2 = Convert.ToDecimal(dtblLedgerPostingId.Rows[1]["ledgerPostingId"].ToString());
         for (int ini = 0; ini < inRowCount - 1; ini++)
         {
             if (dgvServiceVoucher.Rows[ini].Cells["dgvcmbParticulars"].Value != null && dgvServiceVoucher.Rows[ini].Cells["dgvcmbParticulars"].Value.ToString() != string.Empty)
             {
                 inValue++;
             }
         }
         if (inValue > 0)
         {
             infoServiceMaster.ServiceMasterId = decServiceMasterId;
             infoServiceMaster.SuffixPrefixId = decServiceSuffixPrefixId;
             infoServiceMaster.Date = Convert.ToDateTime(txtVoucherDate.Text);
             infoServiceMaster.LedgerId = Convert.ToDecimal(cmbCashParty.SelectedValue.ToString());
             infoServiceMaster.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
             infoServiceMaster.Narration = txtNarration.Text.Trim();
             infoServiceMaster.UserId = PublicVariables._decCurrentUserId;
             infoServiceMaster.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text);
             infoServiceMaster.ServiceAccount = Convert.ToDecimal(cmbServiceAC.SelectedValue.ToString());
             decimal decExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());//spExchangeRate.GetExchangeRateByCurrencyId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
             infoServiceMaster.ExchangeRateId = decExchangeRateId;
             infoServiceMaster.EmployeeId = Convert.ToDecimal(cmbSalesman.SelectedValue.ToString());
             infoServiceMaster.Customer = txtCustomer.Text.Trim();
             infoServiceMaster.Discount = Convert.ToDecimal(txtDiscount.Text);
             infoServiceMaster.GrandTotal = Convert.ToDecimal(txtGrandTotal.Text);
             infoServiceMaster.VoucherTypeId = DecServicetVoucherTypeId;
             infoServiceMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
             infoServiceMaster.ExtraDate = PublicVariables._dtCurrentDate;
             infoServiceMaster.Extra1 = string.Empty;
             infoServiceMaster.Extra2 = string.Empty;
             //------------------deleting removed rows----------------------------------------//
             BankReconciliationSP spBankReconciliation = new BankReconciliationSP();
             foreach (object obj in strArrOfRemove)
             {
                 string str = Convert.ToString(obj);
                 spServiceDetails.ServiceDetailsDelete(Convert.ToDecimal(str));
             }
             spServiceMaster.ServiceMasterEdit(infoServiceMaster);
             infoServiceDetails.ServiceMasterId = decServiceMasterId;
             infoServiceDetails.Extra1 = string.Empty;
             infoServiceDetails.Extra2 = string.Empty;
             infoServiceDetails.ExtraDate = PublicVariables._dtCurrentDate;
             for (int i = 0; i < inRowCount - 1; i++)
             {
                 if (dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value.ToString() != string.Empty)
                 {
                     infoServiceDetails.ServiceId = Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value.ToString());
                 }
                 if (dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value.ToString() != string.Empty)
                 {
                     infoServiceDetails.Measure = dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value.ToString();
                 }
                 if (dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
                 {
                     infoServiceDetails.Amount = Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                     if (dgvServiceVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value != null && dgvServiceVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value.ToString() != string.Empty)
                     {
                         infoServiceDetails.ServiceDetailsId = Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value.ToString());
                         infoServiceDetails.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());
                         spServiceDetails.ServiceDetailsEdit(infoServiceDetails);
                     }
                     else
                     {
                         infoServiceDetails.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());
                         decServiceDetailsId = spServiceDetails.ServiceDetailsAddReturnWithIdentity(infoServiceDetails);
                     }
                     decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                     decAmount = Convert.ToDecimal(dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value);
                     decConvertRate += decAmount * decSelectedCurrencyRate;
                 }
             }
             decCredit = 0;
             decDebit = decConvertRate;
             decLedgerId = Convert.ToDecimal(cmbCashParty.SelectedValue.ToString());
             LedgerPostingEdit(decLedgerPostingId1, decLedgerId, decCredit, decDebit);
             decCredit = decConvertRate;
             decDebit = 0;
             decLedgerId = Convert.ToDecimal(cmbServiceAC.SelectedValue.ToString());
             LedgerPostingEdit(decLedgerPostingId2, decLedgerId, decCredit, decDebit);
             AccountLedgerSP spAccountLedger = new AccountLedgerSP();
             decimal decI = Convert.ToDecimal(spAccountLedger.AccountGroupIdCheck(cmbCashParty.Text));
             if (decI > 0)
             {
                 PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
                 PartyBalanceSP spPartyBalance = new PartyBalanceSP();
                 infoPartyBalance.PartyBalanceId = decPartyBalanceId;
                 infoPartyBalance.Date = Convert.ToDateTime(txtVoucherDate.Text);
                 infoPartyBalance.LedgerId = Convert.ToDecimal(cmbCashParty.SelectedValue.ToString());
                 infoPartyBalance.VoucherTypeId = DecServicetVoucherTypeId;
                 infoPartyBalance.VoucherNo = strVoucherNo;
                 infoPartyBalance.AgainstVoucherTypeId = 0;
                 infoPartyBalance.AgainstVoucherNo = "0";
                 infoPartyBalance.InvoiceNo = strInvoiceNo;
                 infoPartyBalance.AgainstInvoiceNo = "0";
                 infoPartyBalance.ReferenceType = "New";
                 infoPartyBalance.Debit = decAmount;
                 infoPartyBalance.Credit = 0;
                 infoPartyBalance.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text);
                 infoPartyBalance.ExchangeRateId = decExchangeRateId;
                 infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                 infoPartyBalance.Extra1 = string.Empty;
                 infoPartyBalance.Extra2 = string.Empty;
                 spPartyBalance.PartyBalanceEdit(infoPartyBalance);
             }
             Messages.UpdatedMessage();
             if (cbxPrintAfterSave.Checked)
             {
                 if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                 {
                     PrintForDotMatrix(decServiceMasterId);
                 }
                 else
                 {
                     Print(decServiceMasterId, infoServiceMaster.ExchangeRateId);
                 }
             }
             this.Close();
             if (frmServiceVoucherRegisterObj != null)
             {
                 frmServiceVoucherRegisterObj.Show();
                 frmServiceVoucherRegisterObj.GridFill();
             }
             else if (frmServiceReportObj != null)
             {
                 frmServiceReportObj.Show();
                 frmServiceReportObj.GridFill();
             }
         }
         else
         {
             Messages.InformationMessage("Can't save Service Voucher without atleast one ledger with complete details");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SV 24 : " + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Ledger posting Save function
        /// </summary>
        public void LedgerPosting()
        {
            LedgerPostingInfo InfoPosting = new LedgerPostingInfo();
            LedgerPostingSP SpLedgerPosting = new LedgerPostingSP();
            AccountLedgerSP SpLedger = new AccountLedgerSP();
            LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
            ExchangeRateSP SpExchangRate = new ExchangeRateSP();
            decimal decOldExchange = 0;
            decimal decNewExchangeRate = 0;
            decimal decNewExchangeRateId = 0;
            decimal decSelectedCurrencyRate = 0;
            decimal decAmount = 0;
            decimal decConvertRate = 0;
            string strReferenceType = string.Empty;
            decimal decOldExchangeId = 0;
            try
            {
                if (!btnAgainRef.Enabled)
                {
                    infoLedgerPosting.VoucherTypeId = decPDCReceivableVoucherTypeId;
                    infoLedgerPosting.VoucherNo = strVoucherNo;
                    infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                    infoLedgerPosting.Date = PublicVariables._dtCurrentDate;
                    infoLedgerPosting.LedgerId = Convert.ToDecimal(cmbAccountLedger.SelectedValue.ToString());
                    infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    infoLedgerPosting.Debit = 0;
                    infoLedgerPosting.Credit = Convert.ToDecimal(txtAmount.Text);
                    infoLedgerPosting.ChequeDate = Convert.ToDateTime(txtCheckDate.Text);
                    infoLedgerPosting.ChequeNo = txtcheckNo.Text.Trim();
                    infoLedgerPosting.ExtraDate = PublicVariables._dtCurrentDate;
                    infoLedgerPosting.Extra1 = string.Empty;
                    infoLedgerPosting.Extra2 = string.Empty;
                    SpLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                }
                else
                {
                    infoLedgerPosting.VoucherTypeId = decPDCReceivableVoucherTypeId;
                    infoLedgerPosting.VoucherNo = strVoucherNo;
                    infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                    infoLedgerPosting.Date = PublicVariables._dtCurrentDate;
                    infoLedgerPosting.LedgerId = Convert.ToDecimal(cmbAccountLedger.SelectedValue.ToString());
                    infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    infoLedgerPosting.Debit = 0;
                    foreach (DataRow dr in dtblPartyBalance.Rows)
                    {
                        if (infoLedgerPosting.LedgerId == Convert.ToDecimal(dr["LedgerId"].ToString()))
                        {
                            decOldExchange = Convert.ToDecimal(dr["OldExchangeRate"].ToString());
                            decNewExchangeRateId = Convert.ToDecimal(dr["CurrencyId"].ToString());
                            decSelectedCurrencyRate = SpExchangRate.GetExchangeRateByExchangeRateId(decOldExchange);
                            decAmount = Convert.ToDecimal(dr["Amount"].ToString());
                            decConvertRate = decConvertRate + (decAmount * decSelectedCurrencyRate);

                        }
                    }
                    infoLedgerPosting.Credit = decConvertRate;

                    
                    infoLedgerPosting.ChequeDate = Convert.ToDateTime(txtCheckDate.Text);
                    infoLedgerPosting.ChequeNo = txtcheckNo.Text.Trim();
                    infoLedgerPosting.ExtraDate = PublicVariables._dtCurrentDate;
                    infoLedgerPosting.Extra1 = string.Empty;
                    infoLedgerPosting.Extra2 = string.Empty;
                    SpLedgerPosting.LedgerPostingAdd(infoLedgerPosting);

                    infoLedgerPosting.LedgerId = 12;
                    foreach (DataRow dr in dtblPartyBalance.Rows)
                    {
                        if (Convert.ToDecimal(cmbAccountLedger.SelectedValue.ToString()) == Convert.ToDecimal(dr["LedgerId"].ToString()))
                        {
                            if (dr["ReferenceType"].ToString() == "Against")
                            {
                                decNewExchangeRateId = Convert.ToDecimal(dr["CurrencyId"].ToString());
                                decNewExchangeRate = SpExchangRate.GetExchangeRateByExchangeRateId(decNewExchangeRateId);
                                decOldExchangeId = Convert.ToDecimal(dr["OldExchangeRate"].ToString());
                                decOldExchange = SpExchangRate.GetExchangeRateByExchangeRateId(decOldExchangeId);
                                decAmount = Convert.ToDecimal(dr["Amount"].ToString());
                                decimal decForexAmount = (decAmount * decNewExchangeRate) - (decAmount * decOldExchange);
                                if (decForexAmount >= 0)
                                {

                                    infoLedgerPosting.Credit = decForexAmount;
                                    infoLedgerPosting.Debit = 0;
                                }
                                else
                                {
                                    infoLedgerPosting.Debit = -1 * decForexAmount;
                                    infoLedgerPosting.Credit = 0;
                                }
                                SpLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                            }
                        }

                    }
                }
                infoLedgerPosting.LedgerId = 7;
                infoLedgerPosting.VoucherNo = strVoucherNo;
                infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                infoLedgerPosting.Date = PublicVariables._dtCurrentDate;
                infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                infoLedgerPosting.Debit = Convert.ToDecimal(txtAmount.Text);
                infoLedgerPosting.Credit = 0;
                infoLedgerPosting.ChequeDate = Convert.ToDateTime(txtCheckDate.Text);
                infoLedgerPosting.ChequeNo = txtcheckNo.Text.Trim();
                infoLedgerPosting.ExtraDate = PublicVariables._dtCurrentDate;
                infoLedgerPosting.Extra1 = string.Empty;
                infoLedgerPosting.Extra2 = string.Empty;
                SpLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
            }
            catch (Exception ex)
            {
                MessageBox.Show("PR11:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Edit function Call the ledger posting Edit function also
 /// </summary>
 /// <param name="decMasterId"></param>
 public void EditFunction(decimal decMasterId)
 {
     try
     {
         ContraMasterSP spContraMaster = new ContraMasterSP();
         ContraMasterInfo infoContraMaster = new ContraMasterInfo();
         ContraDetailsSP spContraDetails = new ContraDetailsSP();
         ContraDetailsInfo infoCOntraDetails = new ContraDetailsInfo();
         ExchangeRateSP spExchangeRate = new ExchangeRateSP();
         LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
         decimal decContraDetailsId = 0;
         decimal decLedgerPostId = 0;
         infoContraMaster.ContraMasterId = decMasterId;
         infoContraMaster.LedgerId = Convert.ToDecimal(cmbBankAccount.SelectedValue.ToString());
         if (isAutomatic)
         {
             infoContraMaster.VoucherNo = strVoucherNo;
         }
         if (isAutomatic)
         {
             infoContraMaster.InvoiceNo = strInvoiceNo;
         }
         else
         {
             infoContraMaster.InvoiceNo = txtVoucherNo.Text;
         }
         infoContraMaster.Date = Convert.ToDateTime(dtpContraVoucherDate.Text.ToString());
         infoContraMaster.Narration = txtNarration.Text.Trim();
         infoContraMaster.TotalAmount = Convert.ToDecimal(txtTotal.Text);
         if (rbtnDeposit.Checked)
             infoContraMaster.Type = "Deposit";
         else
             infoContraMaster.Type = "Withdraw";
         infoContraMaster.SuffixPrefixId = decContraSuffixPrefixId;
         infoContraMaster.VoucherTypeId = DecContraVoucherTypeId;
         infoContraMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
         infoContraMaster.UserId = PublicVariables._decCurrentUserId;
         infoContraMaster.Extra1 = string.Empty;
         infoContraMaster.Extra2 = string.Empty;
         infoContraMaster.Date = Convert.ToDateTime(txtContraVoucherDate.Text);
         int inCount = dgvContraVoucher.RowCount;
         int inValue = 0;
         for (int i = 0; i < inCount - 1; i++)
         {
             if (dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value != null &&
                 dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString() != null)
             {
                 inValue++;
             }
         }
         if (inValue > 0)
         {
             if (Convert.ToDecimal(txtTotal.Text) != 0)
             {
                 infoCOntraDetails.ContraMasterId = decMasterId;
                 infoCOntraDetails.Extra1 = string.Empty;
                 infoCOntraDetails.Extra2 = string.Empty;
                 //------------------deleting removed rows----------------------------------------//
                 foreach (var item in arrlstOfRemove)
                 {
                     decimal decId = Convert.ToDecimal(item);
                     spContraDetails.ContraDetailsDelete(Convert.ToDecimal(decId));
                     spLedgerPosting.LedgerPostDeleteByDetailsId(Convert.ToDecimal(decId), strVoucherNo, DecContraVoucherTypeId);
                 }
                 //--------------------------End---------------------------------------------------//
                 int inRowCount = dgvContraVoucher.RowCount;
                 decimal decLedgerId = 0;
                 decimal decLedger1Id = 0;
                 decimal decDebit = 0;
                 decimal decCredit = 0;
                 decLedger1Id = Convert.ToDecimal(cmbBankAccount.SelectedValue.ToString());
                 for (int i = 0; i < inRowCount; i++)
                 {
                     if (dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString() != "")
                     {
                         decLedgerId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString());
                         infoCOntraDetails.LedgerId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString());
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value.ToString() != "")
                     {
                         infoCOntraDetails.ChequeNo = dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value.ToString();
                     }
                     else
                     {
                         infoCOntraDetails.ChequeNo = string.Empty;
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value.ToString() != "")
                     {
                         infoCOntraDetails.ChequeDate = Convert.ToDateTime(dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value.ToString());
                     }
                     else
                     {
                         infoCOntraDetails.ChequeDate = Convert.ToDateTime("1/1/1753");
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString() != "")
                     {
                         infoCOntraDetails.Amount = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                         if (dgvContraVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value.ToString() != "")
                         {
                             infoCOntraDetails.ContraDetailsId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value.ToString());
                             decContraDetailsId = infoCOntraDetails.ContraDetailsId;
                             decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value.ToString()));
                             decAmount = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                             decConvertRate = decAmount * decSelectedCurrencyRate;
                             if (rbtnDeposit.Checked)
                             {
                                 decCredit = decConvertRate;
                                 decDebit = 0;
                             }
                             else
                             {
                                 decDebit = decConvertRate;
                                 decCredit = 0;
                             }
                             infoCOntraDetails.ContraMasterId = decMasterId;
                             infoCOntraDetails.ExchangeRateId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value.ToString());
                             spContraDetails.ContraDetailsEdit(infoCOntraDetails);
                             decLedgerPostId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtLedgerPostingId"].Value.ToString());
                             LedgerPostingEdit(decLedgerPostId, decLedgerId, decCredit, decDebit, decContraDetailsId, i);
                         }
                         else
                         {
                             infoCOntraDetails.ExchangeRateId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value.ToString());
                             decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value.ToString()));
                             decAmount = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                             decConvertRate = decAmount * decSelectedCurrencyRate;
                             if (rbtnDeposit.Checked)
                             {
                                 decCredit = decConvertRate;
                                 decDebit = 0;
                             }
                             else
                             {
                                 decDebit = decConvertRate;
                                 decCredit = 0;
                             }
                             decContraDetailsId = spContraDetails.ContraDetailsAddReturnWithhIdentity(infoCOntraDetails);
                             LedgerPosting(decLedgerId, decCredit, decDebit, decContraDetailsId, i);
                         }
                     }
                 }
                 spContraMaster.ContraMasterEdit(infoContraMaster);
                 decLedgerPostId = spLedgerPosting.LedgerPostingIdForTotalAmount(strVoucherNo, DecContraVoucherTypeId);
                 decAmount = Convert.ToDecimal(txtTotal.Text);
                 decContraDetailsId = 0;
                 if (rbtnDeposit.Checked)
                 {
                     decDebit = decAmount;
                     decCredit = 0;
                     LedgerPostingEdit(decLedgerPostId, decLedger1Id, decCredit, decDebit, decContraDetailsId, -1);
                 }
                 else
                 {
                     decCredit = decAmount;
                     decDebit = 0;
                     LedgerPostingEdit(decLedgerPostId, decLedger1Id, decCredit, decDebit, decContraDetailsId, -1);
                 }
                 Messages.UpdatedMessage();
                 if (cbxPrintafterSave.Checked)
                 {
                     if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                     {
                         PrintForDotMatrix(decMasterId);
                     }
                     else
                     {
                         Print(decMasterId);
                     }
                 }
                 this.Close();
             }
             else
             {
                 Messages.InformationMessage("Cannot save total amount as 0");
                 dgvContraVoucher.Focus();
             }
         }
         else
         {
             Messages.InformationMessage("Cant update contra voucher without atleast one ledger with complete details");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("CV:17" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Save function Call the ledger posting function also
 /// </summary>
 public void SaveFuction()
 {
     try
     {
         decimal decContraDetailsId = 0;
         ContraMasterSP spContraMaster = new ContraMasterSP();
         ContraMasterInfo infoContraMaster = new ContraMasterInfo();
         ContraDetailsSP spContraDetails = new ContraDetailsSP();
         ContraDetailsInfo infoCOntraDetails = new ContraDetailsInfo();
         ExchangeRateSP spExchangeRate = new ExchangeRateSP();
         decimal decIdentity = 0;
         decimal decLedgerId = 0;
         decimal decDebit = 0;
         decimal decCredit = 0;
         int inCount = dgvContraVoucher.RowCount;
         int inValue = 0;
         for (int i = 0; i < inCount - 1; i++)
         {
             if (dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value != null &&
                 dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString() != null)
             {
                 inValue++;
             }
         }
         if (inValue > 0)
         {
             if (Convert.ToDecimal(txtTotal.Text) != 0)
             {
                 infoContraMaster.LedgerId = Convert.ToDecimal(cmbBankAccount.SelectedValue.ToString());
                 if (isAutomatic)
                 {
                     infoContraMaster.VoucherNo = strVoucherNo;
                 }
                 else
                 {
                     decimal decVoucherNo = spContraMaster.ContraVoucherMasterGetMaxPlusOne(DecContraVoucherTypeId) + 1;
                     infoContraMaster.VoucherNo = Convert.ToString(decVoucherNo);
                 }
                 infoContraMaster.Date = Convert.ToDateTime(dtpContraVoucherDate.Text.ToString());
                 infoContraMaster.Narration = txtNarration.Text.Trim();
                 infoContraMaster.TotalAmount = Convert.ToDecimal(txtTotal.Text);
                 infoContraMaster.Extra1 = string.Empty;
                 infoContraMaster.Extra2 = string.Empty;
                 if (rbtnDeposit.Checked)
                 {
                     infoContraMaster.Type = "Deposit";
                 }
                 else
                 {
                     infoContraMaster.Type = "Withdraw";
                 }
                 infoContraMaster.SuffixPrefixId = decContraSuffixPrefixId;
                 infoContraMaster.VoucherTypeId = DecContraVoucherTypeId;
                 infoContraMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                 infoContraMaster.UserId = PublicVariables._decCurrentUserId;
                 if (isAutomatic)
                 {
                     infoContraMaster.InvoiceNo = strInvoiceNo;
                 }
                 else
                 {
                     infoContraMaster.InvoiceNo = txtVoucherNo.Text;
                 }
                 decIdentity = spContraMaster.ContraMasterAdd(infoContraMaster);
                 infoCOntraDetails.ContraMasterId = decIdentity;
                 infoCOntraDetails.Extra1 = string.Empty;
                 infoCOntraDetails.Extra2 = string.Empty;
                 int inRowCount = dgvContraVoucher.RowCount;
                 //-------------------------------Saving grid details--------------------------------------------------------------------
                 for (int i = 0; i < inRowCount - 1; i++)
                 {
                     if (dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString() != string.Empty)
                     {
                         infoCOntraDetails.LedgerId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString());
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
                     {
                         infoCOntraDetails.Amount = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value.ToString() != string.Empty)
                     {
                         infoCOntraDetails.ChequeNo = dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value.ToString();
                     }
                     else
                     {
                         infoCOntraDetails.ChequeNo = string.Empty;
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value.ToString() != string.Empty)
                     {
                         infoCOntraDetails.ChequeDate = Convert.ToDateTime(dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value.ToString());
                     }
                     else
                     {
                         infoCOntraDetails.ChequeDate = Convert.ToDateTime("1/1/1753");
                     }
                     infoCOntraDetails.ExchangeRateId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value.ToString());
                     decContraDetailsId = spContraDetails.ContraDetailsAddReturnWithhIdentity(infoCOntraDetails);
                     //---------------------------------------------------------Ledger Posting-----------------------------------------/---------------------------------------------------/
                     if (dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString() != string.Empty)
                     {
                         decLedgerId = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value.ToString());
                     }
                     if (dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value != null && dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString() != string.Empty)
                     {
                         decSelectedCurrencyRate = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value));
                         decAmount = Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                         decConvertRate = decAmount * decSelectedCurrencyRate;
                         if (rbtnDeposit.Checked)
                         {
                             decCredit = decConvertRate;
                             decDebit = 0;
                             LedgerPosting(decLedgerId, decCredit, decDebit, decContraDetailsId, i);
                         }
                         else
                         {
                             decDebit = decConvertRate;
                             decCredit = 0;
                             LedgerPosting(decLedgerId, decCredit, decDebit, decContraDetailsId, i);
                         }
                     }
                 }
                 decAmount = Convert.ToDecimal(txtTotal.Text);
                 decContraDetailsId = 0;
                 if (rbtnDeposit.Checked)
                 {
                     decDebit = decAmount;
                     decCredit = 0;
                     LedgerPosting(infoContraMaster.LedgerId, decCredit, decDebit, decContraDetailsId, -1);
                 }
                 else
                 {
                     decCredit = decAmount;
                     decDebit = 0;
                     LedgerPosting(infoContraMaster.LedgerId, decCredit, decDebit, decContraDetailsId, -1);
                 }
                 //------------------------------------------------------------------Ledger Posting---end---------------------------------------------------------------------//
                 Messages.SavedMessage();
                 if (cbxPrintafterSave.Checked)
                 {
                     if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                     {
                         PrintForDotMatrix(decIdentity);
                     }
                     else
                     {
                         Print(decIdentity);
                     }
                 }
                 Clear();
             }
             else
             {
                 Messages.InformationMessage("Cannot save total debit and credit as 0");
                 dgvContraVoucher.Focus();
             }
         }
         else
         {
             Messages.InformationMessage("Cant save contra voucher without atleast one ledger with complete details");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("CV:16" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// On cellLeave of dgvProduct
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvProduct_CellLeave(object sender, DataGridViewCellEventArgs e)
 {
     BatchSP spbatch = new BatchSP();
     PurchaseOrderMasterSP SPPurchaseOrderMaster = new PurchaseOrderMasterSP();
     StockPostingSP spStockPosting = new StockPostingSP();
     try
     {
         if (isValueChange)
         {
             string strBarcode = string.Empty;
             string strProductCode = string.Empty;
             if (e.RowIndex > -1 && e.ColumnIndex > -1)
             {
                 CheckInvalidEntries(e);
                 if (e.ColumnIndex == dgvProduct.Columns["dgvcmbBatch"].Index)
                 {
                     if (dgvProduct.Rows[e.RowIndex].Cells["productId"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString() != string.Empty)
                     {
                         if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value.ToString() != string.Empty)
                         {
                             if (Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value) != string.Empty &&
                                Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value) != "0")
                             {
                                 decBatchId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value);
                                 strProductCode = dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString();
                                 strBarcode = spbatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                                 dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                                 dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = strProductCode;
                             }
                         }
                     }
                 }
                 else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                 {
                     if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() != string.Empty)
                     {
                         UnitConvertionSP SpUnitConvertion = new UnitConvertionSP();
                         DataTable dtblUnitByProduct = new DataTable();
                         dtblUnitByProduct = SpUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString());
                         foreach (DataRow drUnitByProduct in dtblUnitByProduct.Rows)
                         {
                             if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                             {
                                 dgvProduct.Rows[e.RowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                                 dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                                 if (isDoAfterGridFill)
                                 {
                                     decimal decNewConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                     decimal decNewRate = (decCurrentRate * decCurrentConversionRate) / decNewConversionRate;
                                     dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(decNewRate, 2);
                                     NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                     CalculateTotalAmount();
                                 }
                             }
                         }
                         CheckInvalidEntries(e);
                     }
                 }
                 else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbGodown")
                 {
                     if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value.ToString() != string.Empty)
                     {
                         decGodownId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value);
                         RackComboFill(decGodownId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].ColumnIndex);
                         DataTable dtbl = new DataTable();
                         RackSP spRack = new RackSP();
                         dtbl = spRack.RackNamesCorrespondingToGodownId(decGodownId);
                         dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = Convert.ToDecimal(dtbl.Rows[0]["rackId"].ToString());
                     }
                     CheckInvalidEntries(e);
                 }
                 else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvCmbCurrency" && isAmountcalc)
                 {
                     ExchangeRateSP spExchangeRate = new ExchangeRateSP();
                     if (dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value != null && Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value) != string.Empty)
                     {
                         dgvProduct.Rows[e.RowIndex].Cells["dgvtxtExchangeRate"].Value = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value)));
                     }
                     else
                     {
                         dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value = SPPurchaseOrderMaster.ExchangeRateIdByCurrencyId(PublicVariables._decCurrencyId);
                         dgvProduct.Rows[e.RowIndex].Cells["dgvtxtExchangeRate"].Value = spExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value)));
                     }
                     CheckInvalidEntries(e);
                 }
                 //----------while changing Qty,corresponding change in amount--------
                 else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtQty" && isAmountcalc)
                 {
                     if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value != null)
                     {
                         if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString() != string.Empty && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value.ToString() != string.Empty)
                         {
                             NewAmountCalculation("dgvtxtQty", e.RowIndex);
                             CalculateTotalAmount();
                         }
                     }
                     CheckInvalidEntries(e);
                 }
                 //---------------while changing Qty,corresponding change in amount----
                 else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtRate" && isAmountcalc)
                 {
                     NewAmountCalculation("dgvtxtRate", e.RowIndex);
                     CalculateTotalAmount();
                     CheckInvalidEntries(e);
                 }
                 //----while changing amount ,corresponding chnage in total amount-------
                 else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtAmount" && isAmountcalc)
                 {
                     if (cmbcurrency.Text != string.Empty)
                     {
                         CalculateTotalAmount();
                     }
                 }
                 //}
             }
         }
         CheckInvalidEntries(e);
     }
     catch (Exception ex)
     {
         MessageBox.Show("MR72:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }