/// <summary>
 /// Function to fill Cash/Bank combobox
 /// </summary>
 public void BankOrCashComboFill()
 {
     try
     {
         //ContraMasterSP spContraMaster = new ContraMasterSP();
         ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
         List<DataTable> listObj = new List<DataTable>();
         listObj = bllContraVoucherDetails.CashOrBankComboFill();
         DataRow dr = listObj[0].NewRow();
         dr[0] = "ALL";
         listObj[0].Rows.InsertAt(dr, 0);
         cmbCashOrBank.DataSource = listObj[0];
         cmbCashOrBank.ValueMember = "ledgerId";
         cmbCashOrBank.DisplayMember = "ledgerName";
         cmbCashOrBank.SelectedIndex = 0;
     }
     catch (Exception ex)
     {
         MessageBox.Show("CRT:1" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Function to reset the form to make new service voucher
        /// </summary>
        public void Clear()
        {
            try
            {
                /*Automatic invoice number generation*/
                TransactionsGeneralFillBll obj = new TransactionsGeneralFillBll();
                VoucherDate();
                if (isAutomatic)    //  Checking voucher number generation is automatic or not
                {
                    ServicesBll BllService = new ServicesBll();
                    ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                    if (strVoucherNo == string.Empty)
                    {
                        strVoucherNo = "0";
                    }
                    strVoucherNo = obj.VoucherNumberAutomaicGeneration(DecServicetVoucherTypeId, Convert.ToDecimal(strVoucherNo), dtpVoucherDate.Value, tableName);
                    if (Convert.ToDecimal(strVoucherNo) != (BllService.ServiceMasterGetMax(DecServicetVoucherTypeId)) + 1)
                    {
                        strVoucherNo = BllService.ServiceMasterGetMax(DecServicetVoucherTypeId).ToString();
                        strVoucherNo = obj.VoucherNumberAutomaicGeneration(DecServicetVoucherTypeId, Convert.ToDecimal(strVoucherNo), dtpVoucherDate.Value, tableName);
                        if (BllService.ServiceMasterGetMax(DecServicetVoucherTypeId) == 0)
                        {
                            strVoucherNo = "0";
                            strVoucherNo = obj.VoucherNumberAutomaicGeneration(DecServicetVoucherTypeId, Convert.ToDecimal(strVoucherNo), dtpVoucherDate.Value, tableName);
                        }
                    }
                    SuffixPrefixSettingsBll BllSuffixPrefixSettings = new SuffixPrefixSettingsBll();

                    SuffixPrefixInfo infoSuffixPrefix = new SuffixPrefixInfo();
                    infoSuffixPrefix = BllSuffixPrefixSettings.GetSuffixPrefixDetails(DecServicetVoucherTypeId, dtpVoucherDate.Value);   //  Getting suffix-prefix settings
                    strPrefix = infoSuffixPrefix.Prefix;
                    strSuffix = infoSuffixPrefix.Suffix;
                    decServiceSuffixPrefixId = infoSuffixPrefix.SuffixprefixId;
                    strInvoiceNo = strPrefix + strVoucherNo + strSuffix;    // Generating invoice number with suffix and prefix
                    txtInvoiceNumber.Text = strInvoiceNo;
                    txtInvoiceNumber.ReadOnly = true;
                    txtVoucherDate.Select();
                }
                else
                {
                    txtInvoiceNumber.Text = string.Empty;
                    txtInvoiceNumber.ReadOnly = false;
                }
                ServiceAcoountComboFill();
                GridParticularComboFill();
                SalesmanComboFill();
                CashOrPartyComboFill();
                isEditMode = false;
                txtDiscount.Text = "0";
                txtTotalAmount.Text = "0";
                txtGrandTotal.Text = "0";
                txtCreditPeriod.Text = "0";
                txtCustomer.Text = string.Empty;
                txtNarration.Text = string.Empty;
                PrintCheck();
                int inCount = dgvServiceVoucher.RowCount;
                for (int i = 0; i < inCount; i++)
                {
                    dgvServiceVoucher.Rows[i].Cells["dgvcmbParticulars"].Value = null;
                    dgvServiceVoucher.Rows[i].Cells["dgvtxtMeasure"].Value = null;
                    dgvServiceVoucher.Rows[i].Cells["dgvtxtAmount"].Value = null;
                }
                dgvServiceVoucher.ClearSelection();
                btnDelete.Enabled = false;
                btnSave.Text = "Save";
                dgvServiceVoucher.Rows.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("SV 04 : " + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Its for using Crystal report printing
 /// </summary>
 /// <param name="decContraMasterId"></param>
 public void Print(decimal decContraMasterId)
 {
     try
     {
         //ContraMasterSP spContraMaster = new ContraMasterSP();
         ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
         DataSet dsContraVoucher = bllContraVoucherDetails.ContraVoucherPrinting(decContraMasterId, 1);
         frmReport frmReport = new frmReport();
         frmReport.MdiParent = formMDI.MDIObj;
         frmReport.ContraVoucherPrinting(dsContraVoucher);
     }
     catch (Exception ex)
     {
         MessageBox.Show("CV:26" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Combo fill function of CashorBank in Grid
 /// </summary>
 public void GridBankOrCashComboFill()
 {
     try
     {
         List<DataTable> listObj = new List<DataTable>();
         ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
         listObj = bllContraVoucherDetails.CashOrBankComboFill();
         DataRow drow = listObj[0].NewRow();
         drow["ledgerName"] = string.Empty;
         drow["ledgerId"] = 0;
         listObj[0].Rows.InsertAt(drow, 0);
         dgvcmbBankorCashAccount.DataSource = listObj[0];
         dgvcmbBankorCashAccount.DisplayMember = "ledgerName";
         dgvcmbBankorCashAccount.ValueMember = "ledgerId";
     }
     catch (Exception ex)
     {
         MessageBox.Show("CV:02" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Fill all the controlls while coming from other forms for update or delete
        /// </summary>
        public void FillFunction()
        {
            try
            {
                ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                ContraMasterInfo infoContraMaster = new ContraMasterInfo();
                LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
                VoucherTypeBll BllVOucherType = new VoucherTypeBll();
                infoContraMaster = bllContraVoucherDetails.ContraMasterView(decMasterId);
                txtVoucherNo.ReadOnly = false;
                strVoucherNo = infoContraMaster.VoucherNo;
                txtVoucherNo.Text = infoContraMaster.InvoiceNo;
                strInvoiceNo = infoContraMaster.InvoiceNo;
                strInvoiceNo = infoContraMaster.InvoiceNo;
                decContraSuffixPrefixId = Convert.ToDecimal(infoContraMaster.SuffixPrefixId.ToString());
                DecContraVoucherTypeId = Convert.ToDecimal(infoContraMaster.VoucherTypeId.ToString());
                int inDecimalPlace = PublicVariables._inNoOfDecimalPlaces;
                txtNarration.Text = infoContraMaster.Narration;
                txtContraVoucherDate.Text = Convert.ToString(infoContraMaster.date);
                cmbBankAccount.SelectedValue = infoContraMaster.LedgerId;
                strVoucherNo = infoContraMaster.VoucherNo;
                if (infoContraMaster.Type == "Deposit")
                {
                    rbtnDeposit.Checked = true;
                }
                else
                {
                    rbtnWithdrawal.Checked = true;
                }
                isAutomatic = BllVOucherType.CheckMethodOfVoucherNumbering(DecContraVoucherTypeId);
                if (isAutomatic)
                {
                    txtVoucherNo.Enabled = false;
                }
                else
                {
                    txtVoucherNo.Enabled = true;
                }

                List<DataTable> listObj = new List<DataTable>();
                listObj = bllContraVoucherDetails.ContraDetailsViewWithMasterId(decMasterId);
                for (int i = 0; i < listObj[0].Rows.Count; i++)
                {
                    dgvContraVoucher.Rows.Add();
                    dgvContraVoucher.Rows[i].Cells["dgvcmbBankorCashAccount"].Value = Convert.ToDecimal(listObj[0].Rows[i]["ledgerId"].ToString());
                    dgvContraVoucher.Rows[i].Cells["dgvcmbCurrency"].Value = Convert.ToDecimal(listObj[0].Rows[i]["exchangeRateId"].ToString());
                    dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value = Convert.ToDecimal(listObj[0].Rows[i]["amount"].ToString());

                    if (listObj[0].Rows[i]["chequeNo"].ToString() != string.Empty)
                    {
                        dgvContraVoucher.Rows[i].Cells["dgvtxtChequeNo"].Value = listObj[0].Rows[i]["chequeNo"].ToString();
                        if (listObj[0].Rows[i]["chequeDate"].ToString() == "01 Jan 1753")
                        {
                            dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value = null;
                        }
                        else
                        {
                            dgvContraVoucher.Rows[i].Cells["dgvtxtChequeDate"].Value = listObj[0].Rows[i]["chequeDate"].ToString();
                        }
                    }
                    dgvContraVoucher.Rows[i].Cells["dgvtxtDetailsId"].Value = listObj[0].Rows[i]["contraDetailsId"].ToString();
                    decimal decDetailsId1 = Convert.ToDecimal(listObj[0].Rows[i]["contraDetailsId"].ToString());
                    decimal decLedgerPostingId = BllLedgerPosting.LedgerPostingIdFromDetailsId(decDetailsId1, strVoucherNo, DecContraVoucherTypeId);
                    dgvContraVoucher.Rows[i].Cells["dgvtxtLedgerPostingId"].Value = decLedgerPostingId.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("CV:31" + 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();
                ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                ContraDetailsInfo infoCOntraDetails = new ContraDetailsInfo();
                ExchangeRateBll BllExchangeRate = new ExchangeRateBll();

                LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
                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);
                            bllContraVoucherDetails.ContraDetailsDelete(Convert.ToDecimal(decId));
                            BllLedgerPosting.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 = BllExchangeRate.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());
                                    bllContraVoucherDetails.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 = BllExchangeRate.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 = bllContraVoucherDetails.ContraDetailsAddReturnWithhIdentity(infoCOntraDetails);
                                    LedgerPosting(decLedgerId, decCredit, decDebit, decContraDetailsId, i);
                                }
                            }
                        }
                        bllContraVoucherDetails.ContraMasterEdit(infoContraMaster);
                        decLedgerPostId = BllLedgerPosting.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 (BllSettings.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>
 /// Delete Function
 /// </summary>
 /// <param name="decContraMasterId"></param>
 public void DeleteFunction(decimal decContraMasterId)
 {
     try
     {
         //ContraDetailsSP spContraDetails = new ContraDetailsSP();
         ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
         bllContraVoucherDetails.ContraVoucherDelete(decContraMasterId, DecContraVoucherTypeId, strVoucherNo);
         Messages.DeletedMessage();
         this.Close();
         if (frmsearch != null)
         {
             this.Close();
             frmsearch.GridFill();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("CV:18" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// These function for generating the voucher no automatically if its automatic with preffix and sufix
        /// </summary>
        public void VoucherNumberGeneration()
        {
            try
            {
                TransactionsGeneralFillBll obj = new TransactionsGeneralFillBll();
                //ContraMasterSP spContraMaster = new ContraMasterSP();
                ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                if (strVoucherNo == string.Empty)
                {
                    strVoucherNo = "0";
                }
                strVoucherNo = obj.VoucherNumberAutomaicGeneration(DecContraVoucherTypeId, Convert.ToDecimal(strVoucherNo), dtpContraVoucherDate.Value, tableName);
                if (Convert.ToDecimal(strVoucherNo) != bllContraVoucherDetails.ContraVoucherMasterGetMaxPlusOne(DecContraVoucherTypeId))
                {
                    strVoucherNo = bllContraVoucherDetails.ContraVoucherMasterGetMax(DecContraVoucherTypeId).ToString();
                    strVoucherNo = obj.VoucherNumberAutomaicGeneration(DecContraVoucherTypeId, Convert.ToDecimal(strVoucherNo), dtpContraVoucherDate.Value, tableName);
                    if (bllContraVoucherDetails.ContraVoucherMasterGetMax(DecContraVoucherTypeId) == "0")
                    {
                        strVoucherNo = "0";
                        strVoucherNo = obj.VoucherNumberAutomaicGeneration(DecContraVoucherTypeId, Convert.ToDecimal(strVoucherNo), dtpContraVoucherDate.Value, tableName);
                    }
                }
                if (isAutomatic)
                {
                    SuffixPrefixSettingsBll BllSuffixPrefixSettings = new SuffixPrefixSettingsBll();

                    SuffixPrefixInfo infoSuffixPrefix = new SuffixPrefixInfo();

                    infoSuffixPrefix = BllSuffixPrefixSettings.GetSuffixPrefixDetails(DecContraVoucherTypeId, dtpContraVoucherDate.Value);
                    strPrefix = infoSuffixPrefix.Prefix;
                    strSuffix = infoSuffixPrefix.Suffix;
                    decContraSuffixPrefixId = infoSuffixPrefix.SuffixprefixId;
                    strInvoiceNo = strPrefix + strVoucherNo + strSuffix;
                    txtVoucherNo.Text = strInvoiceNo;
                    txtVoucherNo.ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("CV:08" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Checking invalid entries for save or update and call its curresponding function
        /// </summary>
        public void SaveOrEditFuction()
        {
            try
            {
                decimal decCalcAmount = 0;
                decimal decBalance = 0;
                string strStatus = string.Empty;
                //ContraMasterSP spContraMaster = new ContraMasterSP();
                ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
                SettingsBll BllSettings = new SettingsBll();
                if (txtVoucherNo.Text == string.Empty)
                {
                    Messages.InformationMessage("Enter voucher number");
                    txtVoucherNo.Focus();
                }
                else if (cmbBankAccount.SelectedIndex == -1)
                {
                    Messages.InformationMessage("Select cash or bank account");
                    cmbBankAccount.Focus();
                }
                else
                {
                    if (RemoveIncompleteRowsFromGrid())
                    {
                        if (dtpContraVoucherDate.Value.ToString() != string.Empty)
                        {
                            strStatus = BllSettings.SettingsStatusCheck("NegativeCashTransaction");

                            if (rbtnWithdrawal.Checked)
                            {
                                decBalance = bllAccountLedger.CheckLedgerBalance(Convert.ToDecimal(cmbBankAccount.SelectedValue.ToString()));
                                decCalcAmount = decBalance - Convert.ToDecimal(txtTotal.Text);
                                if (decCalcAmount < 0)
                                {
                                    if (strStatus == "Warn")
                                    {
                                        if (MessageBox.Show("Negative balance exists,Do you want to Continue", "Openmiracle", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                                        {
                                            SaveOrEdit();
                                        }
                                    }
                                    else if (strStatus == "Block")
                                    {
                                        MessageBox.Show("Cannot continue ,due to negative balance", "Openmiracle", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                    }
                                    else
                                    {
                                        SaveOrEdit();
                                    }
                                }
                                else
                                {
                                    SaveOrEdit();
                                }
                            }
                            else
                            {
                                bool isNegativeLedger = false;
                                int inRowCount = dgvContraVoucher.RowCount;
                                for (int i = 0; i < inRowCount - 1; i++)
                                {
                                    decCalcAmount = 0;
                                    decimal decledgerId = 0;
                                    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());
                                    }
                                    decBalance = bllAccountLedger.CheckLedgerBalance(decledgerId);
                                    decCalcAmount = decBalance - Convert.ToDecimal(dgvContraVoucher.Rows[i].Cells["dgvtxtAmount"].Value.ToString());
                                    if (decCalcAmount < 0)
                                    {
                                        isNegativeLedger = true;
                                        break;
                                    }
                                }
                                if (isNegativeLedger)
                                {
                                    if (strStatus == "Warn")
                                    {
                                        if (MessageBox.Show("Negative balance exists,Do you want to Continue", "Openmiracle", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                                        {
                                            SaveOrEdit();
                                        }
                                    }
                                    else if (strStatus == "Block")
                                    {
                                        MessageBox.Show("Cannot continue ,due to negative balance", "Openmiracle", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                    }
                                    else
                                    {
                                        SaveOrEdit();
                                    }
                                }
                                else
                                {
                                    SaveOrEdit();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("CV:15" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        /// <summary>
        /// Checking VoucherNo existance for save or update and call its curresponding function
        /// </summary>
        public void SaveOrEdit()
        {
            try
            {
                //ContraMasterSP spContraMaster = new ContraMasterSP();
                ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                if (!isEditMode)
                {
                    if (PublicVariables.isMessageAdd)
                    {
                        if (Messages.SaveMessage())
                        {
                            if (!isAutomatic)
                            {
                                if (bllContraVoucherDetails.ContraVoucherCheckExistence(txtVoucherNo.Text.Trim(), DecContraVoucherTypeId, 0) == false)
                                {
                                    SaveFuction();
                                }
                                else
                                {
                                    Messages.InformationMessage("Voucher number already exist");
                                }
                            }
                            else
                            {
                                if (bllContraVoucherDetails.ContraVoucherCheckExistence(txtVoucherNo.Text.Trim(), DecContraVoucherTypeId, 0) == false)
                                {
                                    SaveFuction();
                                }
                                else
                                {
                                    Messages.InformationMessage("Voucher number already exist");
                                }

                            }
                        }
                    }
                    else
                    {
                        if (!isAutomatic)
                        {
                            if (bllContraVoucherDetails.ContraVoucherCheckExistence(txtVoucherNo.Text.Trim(), DecContraVoucherTypeId, 0) == false)
                            {
                                SaveFuction();
                            }
                            else
                            {
                                Messages.InformationMessage("Voucher number already exist");
                            }
                        }
                        else
                        {
                            SaveFuction();

                        }
                    }
                }
                else if (isEditMode)
                {

                    if (PublicVariables.isMessageEdit)
                    {
                        if (Messages.UpdateMessage())
                        {
                            if (!isAutomatic)
                            {
                                if (bllContraVoucherDetails.ContraVoucherCheckExistence(txtVoucherNo.Text.Trim(), DecContraVoucherTypeId, decMasterId) == false)
                                {
                                    EditFunction(decMasterId);
                                }
                                else
                                {
                                    Messages.InformationMessage("Voucher number already exist");
                                }
                            }
                            else
                            {
                                EditFunction(decMasterId);

                            }

                        }
                    }
                    else
                    {
                        if (!isAutomatic)
                        {
                            if (bllContraVoucherDetails.ContraVoucherCheckExistence(txtVoucherNo.Text.Trim(), DecContraVoucherTypeId, decMasterId) == false)
                            {
                                EditFunction(decMasterId);
                            }
                            else
                            {
                                Messages.InformationMessage("Voucher number already exist");
                            }
                        }
                        else
                        {
                            EditFunction(decMasterId);

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("CV:14" + 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();
                ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
                ContraDetailsInfo infoCOntraDetails = new ContraDetailsInfo();
                ExchangeRateBll BllExchangeRate = new ExchangeRateBll();
                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 = bllContraVoucherDetails.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 = bllContraVoucherDetails.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 = bllContraVoucherDetails.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 = BllExchangeRate.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 (BllSettings.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 'Print' button click
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnPrint_Click(object sender, EventArgs e)
 {
     try
     {
         if (dgvContraReport.RowCount > 0)
         {
             DataSet ds = new DataSet();
             //CompanySP spCompany = new CompanySP();
             CompanyCreationBll bllCompanyCreation = new CompanyCreationBll();
             frmReport reportobj = new frmReport();
             //ContraMasterSP spContraMaster = new ContraMasterSP();
             ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
             List<DataTable> listObjCompany = bllCompanyCreation.CompanyViewDataTable(1);
             List<DataTable> listObjCotraReport = bllContraVoucherDetails.ContraReport(Convert.ToDateTime(dtpFromDate.Value.ToString()), Convert.ToDateTime(dtpToDate.Value.ToString()), cmbVoucherType.Text, cmbCashOrBank.Text, strType);
             ds.Tables.Add(listObjCompany[0]);
             ds.Tables.Add(listObjCotraReport[0]);
             reportobj.MdiParent = formMDI.MDIObj;
             reportobj.ContraVoucherReport(ds);
         }
         else
         {
             MessageBox.Show("No data found", "Open Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("CRT:12" + ex.Message, "Open Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to fill Datagridview
 /// </summary>
 public void GridFill()
 {
     try
     {
         //ContraMasterSP spContraMaster = new ContraMasterSP();
         ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
         List<DataTable> listObj = new List<DataTable>();
         if (rbtnDeposit.Checked)
         {
             strType = "Deposit";
         }
         else
         {
             strType = "Withdraw";
         }
         listObj = bllContraVoucherDetails.ContraReport(Convert.ToDateTime(dtpFromDate.Value.ToString()), Convert.ToDateTime(dtpToDate.Value.ToString()), cmbVoucherType.Text, cmbCashOrBank.Text, strType);
         dgvContraReport.DataSource = listObj[0];
     }
     catch (Exception ex)
     {
         MessageBox.Show("CRT:3" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to fill Datagridview
 /// </summary>
 public void Gridfill()
 {
     try
     {
         string strType = string.Empty;
         if (rbtnDeposit.Checked)
         {
             strType = "Deposit";
         }
         else
         {
             strType = "Withdraw";
         }
         //ContraMasterSP spContraMaster = new ContraMasterSP();
         ContraVoucherDetailsBll bllContraVoucherDetails = new ContraVoucherDetailsBll();
         List<DataTable> listObj = new List<DataTable>();
         listObj = bllContraVoucherDetails.ContraVoucherRegisterSearch(Convert.ToDateTime(dtpFromDate.Value), Convert.ToDateTime(dtpToDate.Value), txtVoucherNo.Text.Trim(), cmbBankOrCash.Text, strType);
         dgvContraRegister.DataSource = listObj[0];
     }
     catch (Exception ex)
     {
         MessageBox.Show("CR:2" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }