/// <summary>
 ///  Save function
 /// </summary>
 public void SaveFunction()
 {
     StockPostingInfo infoStockPosting = new StockPostingInfo();
     SalesMasterInfo InfoSalesMaster = new SalesMasterInfo();
     SalesDetailsInfo InfoSalesDetails = new SalesDetailsInfo();
     StockPostingBll BllStockPosting = new StockPostingBll();
     AdditionalCostInfo infoAdditionalCost = new AdditionalCostInfo();
     AditionalCostBll bllAditionalCost = new AditionalCostBll();
     SalesBillTaxInfo infoSalesBillTax = new SalesBillTaxInfo();
     SalesInvoiceBll BllSalesInvoice = new SalesInvoiceBll();
     UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
     try
     {
         InfoSalesMaster.AdditionalCost = Convert.ToDecimal(lblLedgerTotalAmount.Text);
         InfoSalesMaster.BillDiscount = Convert.ToDecimal(txtBillDiscount.Text.Trim());
         InfoSalesMaster.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text.Trim().ToString());
         InfoSalesMaster.CustomerName = txtCustomer.Text.Trim();
         InfoSalesMaster.Date = Convert.ToDateTime(txtDate.Text.ToString());
         InfoSalesMaster.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());
         InfoSalesMaster.EmployeeId = Convert.ToDecimal(cmbSalesMan.SelectedValue.ToString());
         InfoSalesMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
         InfoSalesMaster.GrandTotal = Convert.ToDecimal(txtGrandTotal.Text.Trim());
         InfoSalesMaster.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
         InfoSalesMaster.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
         if (isAutomatic)
         {
             InfoSalesMaster.InvoiceNo = txtInvoiceNo.Text.Trim();
             InfoSalesMaster.VoucherNo = strVoucherNo;
             InfoSalesMaster.SuffixPrefixId = decSalseInvoiceSuffixPrefixId;
         }
         else
         {
             InfoSalesMaster.InvoiceNo = txtInvoiceNo.Text.Trim();
             InfoSalesMaster.VoucherNo = strVoucherNo;
             InfoSalesMaster.SuffixPrefixId = 0;
         }
         if (cmbSalesMode.Text == "Against SalesOrder")
         {
             InfoSalesMaster.OrderMasterId = Convert.ToDecimal(cmbSalesModeOrderNo.SelectedValue.ToString());
         }
         else
         {
             InfoSalesMaster.OrderMasterId = 0;
         }
         if (cmbSalesMode.Text == "Against Delivery Note")
         {
             InfoSalesMaster.DeliveryNoteMasterId = Convert.ToDecimal(cmbSalesModeOrderNo.SelectedValue.ToString());
         }
         else
         {
             InfoSalesMaster.DeliveryNoteMasterId = 0;
         }
         if (cmbSalesMode.Text == "Against Quotation")
         {
             InfoSalesMaster.QuotationMasterId = Convert.ToDecimal(cmbSalesModeOrderNo.SelectedValue.ToString());
         }
         else
         {
             InfoSalesMaster.QuotationMasterId = 0;
         }
         InfoSalesMaster.Narration = txtNarration.Text.Trim();
         InfoSalesMaster.PricinglevelId = Convert.ToDecimal(cmbPricingLevel.SelectedValue.ToString());
         InfoSalesMaster.SalesAccount = Convert.ToDecimal(cmbSalesAccount.SelectedValue.ToString());
         InfoSalesMaster.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text.Trim());
         if (dgvSalesInvoice.Columns["dgvcmbSalesInvoiceTaxName"].Visible)
         {
             InfoSalesMaster.TaxAmount = Convert.ToDecimal(lblTaxTotalAmount.Text.Trim());
         }
         else
         {
             InfoSalesMaster.TaxAmount = 0;
         }
         InfoSalesMaster.UserId = PublicVariables._decCurrentUserId;
         InfoSalesMaster.LrNo = txtVehicleNo.Text;
         InfoSalesMaster.TransportationCompany = txtTransportCompany.Text.Trim();
         InfoSalesMaster.POS = false;
         InfoSalesMaster.CounterId = 0;
         InfoSalesMaster.ExtraDate = DateTime.Now;
         InfoSalesMaster.Extra1 = string.Empty;
         InfoSalesMaster.Extra2 = string.Empty;
         decimal decSalesMasterId = BllSalesInvoice.SalesMasterAdd(InfoSalesMaster);
         int inRowCount = dgvSalesInvoice.RowCount;
         InfoSalesDetails.SalesMasterId = decSalesMasterId;
         InfoSalesDetails.ExtraDate = DateTime.Now;
         InfoSalesDetails.Extra1 = string.Empty;
         InfoSalesDetails.Extra2 = string.Empty;
         string strAgainstInvoiceN0 = txtInvoiceNo.Text.Trim();
         for (int inI = 0; inI < inRowCount - 1; inI++)
         {
             if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductName"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductName"].Value.ToString() != string.Empty)
             {
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value.ToString() != string.Empty)
                 {
                     if (cmbSalesMode.Text == "Against SalesOrder")
                     {
                         InfoSalesDetails.OrderDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSISalesOrderDetailsId"].Value.ToString());
                     }
                     else
                     {
                         InfoSalesDetails.OrderDetailsId = 0;
                     }
                     if (cmbSalesMode.Text == "Against Delivery Note")
                     {
                         InfoSalesDetails.DeliveryNoteDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value.ToString());
                     }
                     else
                     {
                         InfoSalesDetails.DeliveryNoteDetailsId = 0;
                     }
                     if (cmbSalesMode.Text == "Against Quotation")
                     {
                         InfoSalesDetails.QuotationDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQuotationDetailsId"].Value.ToString());
                     }
                     else
                     {
                         InfoSalesDetails.QuotationDetailsId = 0;
                     }
                     InfoSalesDetails.SlNo = Convert.ToInt32(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSlno"].Value.ToString());
                     InfoSalesDetails.ProductId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductId"].Value.ToString());
                     InfoSalesDetails.Qty = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value.ToString());
                     InfoSalesDetails.Rate = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
                     InfoSalesDetails.UnitId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoicembUnitName"].Value.ToString());
                     InfoSalesDetails.UnitConversionId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceUnitConversionId"].Value.ToString());
                     InfoSalesDetails.Discount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDiscountAmount"].Value.ToString());
                     InfoSalesDetails.BatchId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString());
                     if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString() != string.Empty)
                     {
                         InfoSalesDetails.GodownId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString());
                     }
                     else
                     {
                         InfoSalesDetails.GodownId = 0;
                     }
                     if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString() != string.Empty)
                     {
                         InfoSalesDetails.RackId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString());
                     }
                     else
                     {
                         InfoSalesDetails.RackId = 0;
                     }
                     if (dgvSalesInvoice.Columns["dgvcmbSalesInvoiceTaxName"].Visible)
                     {
                         InfoSalesDetails.TaxId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceTaxName"].Value.ToString());
                         InfoSalesDetails.TaxAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceTaxAmount"].Value.ToString());
                     }
                     else
                     {
                         InfoSalesDetails.TaxId = 1;
                         InfoSalesDetails.TaxAmount = 0;
                     }
                     InfoSalesDetails.GrossAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceGrossValue"].Value.ToString());
                     InfoSalesDetails.NetAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceNetAmount"].Value.ToString());
                     InfoSalesDetails.Amount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceAmount"].Value.ToString());
                     BllSalesInvoice.SalesDetailsAdd(InfoSalesDetails);
                     infoStockPosting.Date = Convert.ToDateTime(txtDate.Text.Trim().ToString());
                     infoStockPosting.ProductId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductId"].Value.ToString());
                     infoStockPosting.BatchId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString());
                     infoStockPosting.UnitId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoicembUnitName"].Value.ToString());
                     if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString() != string.Empty)
                     {
                         infoStockPosting.GodownId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString());
                     }
                     else
                     {
                         infoStockPosting.GodownId = 0;
                     }
                     if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString() != string.Empty)
                     {
                         infoStockPosting.RackId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString());
                     }
                     else
                     {
                         infoStockPosting.RackId = 0;
                     }
                     infoStockPosting.Rate = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
                     infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                     infoStockPosting.ExtraDate = DateTime.Now;
                     infoStockPosting.Extra1 = string.Empty;
                     infoStockPosting.Extra2 = string.Empty;
                     if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value != null)
                     {
                         if (Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value.ToString()) != 0)
                         {
                             infoStockPosting.InwardQty = InfoSalesDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(InfoSalesDetails.UnitConversionId);
                             infoStockPosting.OutwardQty = 0;
                             infoStockPosting.VoucherNo = strVoucherNoTostockPost;
                             infoStockPosting.AgainstVoucherNo = strVoucherNo;
                             infoStockPosting.InvoiceNo = strInvoiceNoTostockPost;
                             infoStockPosting.AgainstInvoiceNo = strAgainstInvoiceN0;
                             infoStockPosting.VoucherTypeId = decVouchertypeIdTostockPost;
                             infoStockPosting.AgainstVoucherTypeId = DecSalesInvoiceVoucherTypeId;
                             BllStockPosting.StockPostingAdd(infoStockPosting);
                         }
                     }
                     infoStockPosting.InwardQty = 0;
                     infoStockPosting.OutwardQty = InfoSalesDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(InfoSalesDetails.UnitConversionId);
                     infoStockPosting.VoucherNo = InfoSalesMaster.VoucherNo; ;
                     infoStockPosting.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
                     infoStockPosting.InvoiceNo = InfoSalesMaster.InvoiceNo;
                     infoStockPosting.AgainstInvoiceNo = "NA";
                     infoStockPosting.AgainstVoucherNo = "NA";
                     infoStockPosting.AgainstVoucherTypeId = 0;
                     infoStockPosting.Extra1 = string.Empty;
                     infoStockPosting.Extra2 = string.Empty;
                     BllStockPosting.StockPostingAdd(infoStockPosting);
                 }
             }
         }
         if (dgvSalesInvoice.Columns["dgvcmbSalesInvoiceTaxName"].Visible)
         {
             int inTaxRowCount = dgvSalesInvoiceTax.RowCount;
             infoSalesBillTax.SalesMasterId = decSalesMasterId;
             infoSalesBillTax.ExtraDate = DateTime.Now;
             infoSalesBillTax.Extra1 = string.Empty;
             infoSalesBillTax.Extra2 = string.Empty;
             for (int inI = 0; inI < inTaxRowCount; inI++)
             {
                 if (dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxId"].Value != null && dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxId"].Value.ToString() != string.Empty)
                 {
                     if (dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxAmount"].Value != null && dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxAmount"].Value.ToString() != string.Empty)
                     {
                         decimal decAmount = Convert.ToDecimal(dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxAmount"].Value);
                         if (decAmount > 0)
                         {
                             infoSalesBillTax.TaxId = Convert.ToInt32(dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxId"].Value.ToString());
                             infoSalesBillTax.TaxAmount = Convert.ToDecimal(dgvSalesInvoiceTax.Rows[inI].Cells["dgvtxtTtaxAmount"].Value.ToString());
                             BllSalesInvoice.SalesBillTaxAdd(infoSalesBillTax);
                         }
                     }
                 }
             }
         }
         int inAddRowCount = dgvSalesInvoiceLedger.RowCount;
         infoAdditionalCost.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
         infoAdditionalCost.VoucherNo = strVoucherNo;
         infoAdditionalCost.ExtraDate = DateTime.Now;
         infoAdditionalCost.Extra1 = string.Empty;
         infoAdditionalCost.Extra2 = string.Empty;
         for (int inI = 0; inI < inAddRowCount; inI++)
         {
             if (dgvSalesInvoiceLedger.Rows[inI].Cells["dgvCmbAdditionalCostledgerName"].Value != null && dgvSalesInvoiceLedger.Rows[inI].Cells["dgvCmbAdditionalCostledgerName"].Value.ToString() != string.Empty)
             {
                 if (dgvSalesInvoiceLedger.Rows[inI].Cells["dgvtxtAdditionalCoastledgerAmount"].Value != null && dgvSalesInvoiceLedger.Rows[inI].Cells["dgvtxtAdditionalCoastledgerAmount"].Value.ToString() != string.Empty)
                 {
                     infoAdditionalCost.LedgerId = Convert.ToInt32(dgvSalesInvoiceLedger.Rows[inI].Cells["dgvCmbAdditionalCostledgerName"].Value.ToString());
                     if (!cmbCashOrbank.Visible)
                     {
                         infoAdditionalCost.Debit = 0;
                         infoAdditionalCost.Credit = Convert.ToDecimal(dgvSalesInvoiceLedger.Rows[inI].Cells["dgvtxtAdditionalCoastledgerAmount"].Value.ToString());
                     }
                     else
                     {
                         infoAdditionalCost.Debit = Convert.ToDecimal(dgvSalesInvoiceLedger.Rows[inI].Cells["dgvtxtAdditionalCoastledgerAmount"].Value.ToString());
                         infoAdditionalCost.Credit = 0;
                     }
                     bllAditionalCost.AdditionalCostAdd(infoAdditionalCost);
                 }
             }
         }
         if (!cmbCashOrbank.Visible)
         {
             decimal decCAshOrBankId = 0;
             decCAshOrBankId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
             decimal decTotalAddAmount = Convert.ToDecimal(lblLedgerTotalAmount.Text.Trim().ToString());
             if (decTotalAddAmount > 0)
             {
                 infoAdditionalCost.Debit = decTotalAddAmount;
                 infoAdditionalCost.Credit = 0;
                 infoAdditionalCost.LedgerId = decCAshOrBankId;
                 infoAdditionalCost.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
                 infoAdditionalCost.VoucherNo = strVoucherNo;
                 infoAdditionalCost.ExtraDate = DateTime.Now;
                 infoAdditionalCost.Extra1 = string.Empty;
                 infoAdditionalCost.Extra2 = string.Empty;
                 bllAditionalCost.AdditionalCostAdd(infoAdditionalCost);
             }
         }
         else
         {
             if (cmbCashOrbank.Visible)
             {
                 decimal decCAshOrBankId = 0;
                 decCAshOrBankId = Convert.ToDecimal(cmbCashOrbank.SelectedValue.ToString());
                 decimal decTotalAddAmount = Convert.ToDecimal(lblLedgerTotalAmount.Text.Trim().ToString());
                 if (decTotalAddAmount > 0)
                 {
                     infoAdditionalCost.Debit = 0;
                     infoAdditionalCost.Credit = decTotalAddAmount;
                     infoAdditionalCost.LedgerId = decCAshOrBankId;
                     infoAdditionalCost.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
                     infoAdditionalCost.VoucherNo = strVoucherNo;
                     infoAdditionalCost.ExtraDate = DateTime.Now;
                     infoAdditionalCost.Extra1 = string.Empty;
                     infoAdditionalCost.Extra2 = string.Empty;
                     bllAditionalCost.AdditionalCostAdd(infoAdditionalCost);
                 }
             }
         }
         ledgerPostingAdd();
         if (BllSalesInvoice.SalesInvoiceInvoicePartyCheckEnableBillByBillOrNot(Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString())))
         {
             partyBalanceAdd();
         }
         Messages.SavedMessage();
         if (cbxPrintAfterSave.Checked == true)
         {
             SettingsBll BllSettings = new SettingsBll();
             if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
             {
                 PrintForDotMatrix(decSalesMasterId);
             }
             else
             {
                 Print(decSalesMasterId);
             }
         }
         Clear();
     }
     catch (Exception ex)
     {
         MessageBox.Show("SI: 70" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to save An items to table 
 /// </summary>
 public void SaveFunction()
 {
     LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
     LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
     UnitConvertionBll bllUnitConversion = new UnitConvertionBll();
     ExchangeRateBll BllExchangeRate = new ExchangeRateBll();
     try
     {
         InfoSalesMaster.InvoiceNo = txtVoucherNo.Text.Trim();
         InfoSalesMaster.AdditionalCost = 0;
         InfoSalesMaster.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
         InfoSalesMaster.BillDiscount = Convert.ToDecimal(txtBillDiscount.Text);
         if (cmbCounter.SelectedIndex > -1)
         {
             InfoSalesMaster.CounterId = Convert.ToDecimal(cmbCounter.SelectedValue.ToString());
         }
         else
         {
             InfoSalesMaster.CounterId = 0;
         }
         InfoSalesMaster.CreditPeriod = 0;
         InfoSalesMaster.CustomerName = string.Empty;
         InfoSalesMaster.Date = Convert.ToDateTime(txtDate.Text);
         InfoSalesMaster.DeliveryNoteMasterId = 0;
         if (cmbSalesMan.SelectedValue.ToString() != null)
         {
             InfoSalesMaster.EmployeeId = Convert.ToDecimal(cmbSalesMan.SelectedValue.ToString());
         }
         else
         {
             InfoSalesMaster.EmployeeId = 0;
         }
         decimal decExachangeRateId = BllExchangeRate.ExchangerateViewByCurrencyId(PublicVariables._decCurrencyId);
         InfoSalesMaster.ExchangeRateId = decExachangeRateId;
         InfoSalesMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
         InfoSalesMaster.GrandTotal = Convert.ToDecimal(txtGrandTotal.Text.Trim());
         InfoSalesMaster.LrNo = string.Empty;
         InfoSalesMaster.Narration = txtNarration.Text.Trim();
         InfoSalesMaster.OrderMasterId = 0;
         InfoSalesMaster.POS = true;
         if (cmbPricingLevel.SelectedValue.ToString() != null)
         {
             InfoSalesMaster.PricinglevelId = Convert.ToDecimal(cmbPricingLevel.SelectedValue.ToString());
         }
         else
         {
             InfoSalesMaster.PricinglevelId = 0;
         }
         InfoSalesMaster.QuotationMasterId = 0;
         InfoSalesMaster.SalesAccount = Convert.ToDecimal(cmbSalesAccount.SelectedValue.ToString());
         InfoSalesMaster.TaxAmount = Convert.ToDecimal(lblTaxTotalAmount.Text.ToString());
         InfoSalesMaster.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
         InfoSalesMaster.TransportationCompany = string.Empty;
         InfoSalesMaster.UserId = PublicVariables._decCurrentUserId;
         InfoSalesMaster.VoucherTypeId = DecPOSVoucherTypeId;
         if (isAutomatic)
         {
             InfoSalesMaster.SuffixPrefixId = decPOSSuffixPrefixId;
             InfoSalesMaster.VoucherNo = strVoucherNo;
         }
         else
         {
             InfoSalesMaster.SuffixPrefixId = 0;
             InfoSalesMaster.VoucherNo = txtVoucherNo.Text;
         }
         InfoSalesMaster.ExtraDate = DateTime.Now;
         InfoSalesMaster.Extra1 = string.Empty;
         InfoSalesMaster.Extra2 = string.Empty;
         decSalesMasterId = BllSalesInvoice.SalesMasterAdd(InfoSalesMaster);
         int inRowCount = dgvPointOfSales.RowCount;
         InfoSalesDetails.SalesMasterId = decSalesMasterId;
         InfoSalesDetails.ExtraDate = DateTime.Now;
         InfoSalesDetails.Extra1 = string.Empty;
         InfoSalesDetails.Extra2 = string.Empty;
         for (int inI = 0; inI < inRowCount; inI++)
         {
             if (dgvPointOfSales.Rows[inI].Cells["dgvtxtProductName"].Value != null && dgvPointOfSales.Rows[inI].Cells["dgvtxtProductName"].Value.ToString() != string.Empty)
             {
                 if (dgvPointOfSales.Rows[inI].Cells["dgvtxtQuantity"].Value != null && dgvPointOfSales.Rows[inI].Cells["dgvtxtQuantity"].Value.ToString() != string.Empty)
                 {
                     InfoSalesDetails.SlNo = Convert.ToInt32(dgvPointOfSales.Rows[inI].Cells["dgvtxtSlNo"].Value.ToString());
                     InfoSalesDetails.ProductId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtProductId"].Value.ToString());
                     InfoSalesDetails.Qty = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtQuantity"].Value.ToString());
                     InfoSalesDetails.Rate = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtRate"].Value.ToString());
                     InfoSalesDetails.UnitId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtUnitId"].Value.ToString());
                     InfoSalesDetails.UnitConversionId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtunitconversionId"].Value.ToString());
                     InfoSalesDetails.Discount = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtDiscount"].Value.ToString());
                     InfoSalesDetails.TaxId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxttaxid"].Value.ToString());
                     InfoSalesDetails.BatchId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtBatchId"].Value.ToString());
                     InfoSalesDetails.GodownId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtGodownId"].Value.ToString());
                     InfoSalesDetails.RackId = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtRackId"].Value.ToString());
                     InfoSalesDetails.TaxAmount = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtTaxAmount"].Value.ToString());
                     InfoSalesDetails.GrossAmount = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtGrossValue"].Value.ToString());
                     InfoSalesDetails.NetAmount = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtNetAmount"].Value.ToString());
                     InfoSalesDetails.Amount = Convert.ToDecimal(dgvPointOfSales.Rows[inI].Cells["dgvtxtTotalAmount"].Value.ToString());
                     BllSalesInvoice.SalesDetailsAdd(InfoSalesDetails);
                     infoStockPosting.Date = InfoSalesMaster.Date;
                     infoStockPosting.VoucherTypeId = DecPOSVoucherTypeId;
                     infoStockPosting.VoucherNo = strVoucherNo;
                     infoStockPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                     infoStockPosting.AgainstVoucherTypeId = 0;
                     infoStockPosting.AgainstVoucherNo = "NA";
                     infoStockPosting.AgainstInvoiceNo = "NA";
                     infoStockPosting.ProductId = InfoSalesDetails.ProductId;
                     infoStockPosting.BatchId = InfoSalesDetails.BatchId;
                     infoStockPosting.UnitId = InfoSalesDetails.UnitId;
                     infoStockPosting.GodownId = InfoSalesDetails.GodownId;
                     infoStockPosting.RackId = InfoSalesDetails.RackId;
                     infoStockPosting.InwardQty = 0;
                     infoStockPosting.OutwardQty = InfoSalesDetails.Qty / bllUnitConversion.UnitConversionRateByUnitConversionId(InfoSalesDetails.UnitConversionId); ;
                     infoStockPosting.Rate = InfoSalesDetails.Rate;
                     infoStockPosting.FinancialYearId = InfoSalesMaster.FinancialYearId;
                     infoStockPosting.Extra1 = string.Empty;
                     infoStockPosting.Extra2 = string.Empty;
                     BllStockPosting.StockPostingAdd(infoStockPosting);
                 }
             }
         }
         int inTaxRowCount = dgvPOSTax.RowCount;
         InfoSalesBillTax.SalesMasterId = decSalesMasterId;
         InfoSalesBillTax.ExtraDate = DateTime.Now;
         InfoSalesBillTax.Extra1 = string.Empty;
         InfoSalesBillTax.Extra2 = string.Empty;
         for (int inI = 0; inI < inTaxRowCount; inI++)
         {
             if (dgvPOSTax.Rows[inI].Cells["dgvtxttax"].Value != null && dgvPOSTax.Rows[inI].Cells["dgvtxttax"].Value.ToString() != string.Empty)
             {
                 if (dgvPOSTax.Rows[inI].Cells["dgvtxtTaxAmt"].Value != null && dgvPOSTax.Rows[inI].Cells["dgvtxtTaxAmt"].Value.ToString() != string.Empty)
                 {
                     InfoSalesBillTax.TaxId = Convert.ToInt32(dgvPOSTax.Rows[inI].Cells["dgvtxttax"].Value.ToString());
                     InfoSalesBillTax.TaxAmount = Convert.ToDecimal(dgvPOSTax.Rows[inI].Cells["dgvtxtTaxAmt"].Value.ToString());
                     BllSalesInvoice.SalesBillTaxAdd(InfoSalesBillTax);
                 }
             }
         }
         ledgerPostingAdd();
         if (BllSalesInvoice.SalesInvoiceInvoicePartyCheckEnableBillByBillOrNot(Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString())))
         {
             partyBalanceAdd();
         }
         Messages.SavedMessage();
         if (cbxPrintAfterSave.Checked)
         {
             SettingsBll BllSettings = new SettingsBll();
             if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
             {
                 PrintForDotMatrix(decSalesMasterId);
             }
             else
             {
                 Print(decSalesMasterId);
             }
         }
         ClearFunctions();
     }
     catch (Exception ex)
     {
         MessageBox.Show("POS:44" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function for fill details in Purchase Return when coming from Register or Report
 /// </summary>
 public void FillRegisterOrReport()
 {
     try
     {
         btnSave.Text = "Update";
         btnDelete.Enabled = true;
         txtReturnNo.ReadOnly = true;
         VoucherTypeBll BllVoucherType = new VoucherTypeBll();
         List<DataTable> ListObjPurchaseReturnMaster = new List<DataTable>();
         List<DataTable> ListObjPurchaseMasterViewById = new List<DataTable>();
         UnitConvertionBll BllUnitConvertion = new UnitConvertionBll();
         PurchaseReturnBll BllPurchaseReturn = new PurchaseReturnBll();
         BatchBll BllBatch = new BatchBll();
         List<DataTable> ListObj = new List<DataTable>();
         decimal decPurchaseReturnDetailsId = 0;
         decimal decTotalValue = 0;
         decimal decRegisterTotalAmount = 0;
         decimal decInvoiceNo = 0;
         ListObjPurchaseReturnMaster = BllPurchaseReturn.PurchaseReturnViewByPurchaseReturnMasterId(decPurchaseReturnMasterId);
         if (ListObjPurchaseReturnMaster[0].Rows.Count > 0)
         {
             txtReturnNo.Text = ListObjPurchaseReturnMaster[0].Rows[0]["invoiceNo"].ToString();
             strReturnNo = ListObjPurchaseReturnMaster[0].Rows[0]["invoiceNo"].ToString();
             strVoucherNo = ListObjPurchaseReturnMaster[0].Rows[0]["voucherNo"].ToString();
             decPurchaseReturnSuffixPrefixId = Convert.ToDecimal(ListObjPurchaseReturnMaster[0].Rows[0]["suffixPrefixId"].ToString());
             decPurchaseReturnVoucherTypeId = Convert.ToDecimal(ListObjPurchaseReturnMaster[0].Rows[0]["voucherTypeId"].ToString());
             VoucherTypeInfo infoVoucherType = new VoucherTypeInfo();
             infoVoucherType = BllVoucherType.VoucherTypeView(decPurchaseReturnVoucherTypeId);
             this.Text = infoVoucherType.VoucherTypeName;
             isAutomatic = BllVoucherType.CheckMethodOfVoucherNumbering(decPurchaseReturnVoucherTypeId);
             txtReturnNo.ReadOnly = (isAutomatic) ? true : false;
             decPurchaseReturnTypeId = decPurchaseReturnVoucherTypeId;
             dtpDate.Text = ListObjPurchaseReturnMaster[0].Rows[0]["date"].ToString();
             cmbCashOrParty.SelectedValue = ListObjPurchaseReturnMaster[0].Rows[0]["ledgerId"].ToString();
             cmbPurchaseAccount.SelectedValue = ListObjPurchaseReturnMaster[0].Rows[0]["purchaseAccount"].ToString();
             txtTransportationCompany.Text = ListObjPurchaseReturnMaster[0].Rows[0]["transportationCompany"].ToString();
             txtNarration.Text = ListObjPurchaseReturnMaster[0].Rows[0]["narration"].ToString();
             txtLrlNo.Text = ListObjPurchaseReturnMaster[0].Rows[0]["lrNo"].ToString();
             cmbCurrency.SelectedValue = ListObjPurchaseReturnMaster[0].Rows[0]["exchangeRateId"].ToString();
             if (ListObjPurchaseReturnMaster[0].Rows[0]["voucherTypeId1"].ToString() != string.Empty)
             {
                 cmbVoucherType.SelectedValue = ListObjPurchaseReturnMaster[0].Rows[0]["voucherTypeId1"].ToString();
             }
             else
             {
                 cmbVoucherType.SelectedValue = 0;
             }
             cmbInvoiceNo.Text = ListObjPurchaseReturnMaster[0].Rows[0]["invoiceNo1"].ToString();
             if (ListObjPurchaseReturnMaster[0].Rows[0]["totalAmount1"].ToString() != string.Empty)
             {
                 decRegisterTotalAmount = Convert.ToDecimal(ListObjPurchaseReturnMaster[0].Rows[0]["totalAmount"].ToString());
             }
             txtTotalAmount.Text = ListObjPurchaseReturnMaster[0].Rows[0]["totalAmount"].ToString();
             if (ListObjPurchaseReturnMaster[0].Rows[0]["PurchaseMasterId"].ToString() != string.Empty && Convert.ToInt32(ListObjPurchaseReturnMaster[0].Rows[0]["PurchaseMasterId"]) != 0)
             {
                 InvoiceNoComboFillInRegister();
                 cmbInvoiceNo.SelectedValue = Convert.ToDecimal(ListObjPurchaseReturnMaster[0].Rows[0]["PurchaseMasterId"].ToString());
                 decInvoiceNo = Convert.ToDecimal(cmbInvoiceNo.SelectedValue.ToString());
                 ListObjPurchaseMasterViewById = BllPurchaseReturn.PurchaseReturnMasterViewByPurchaseMasterId(Convert.ToDecimal(ListObjPurchaseReturnMaster[0].Rows[0]["PurchaseMasterId"].ToString()));
             }
             else
             {
                 cmbInvoiceNo.SelectedValue = ListObjPurchaseReturnMaster[0].Rows[0]["PurchaseMasterId"];
             }
             strInvoiceNo = ListObjPurchaseReturnMaster[0].Rows[0]["invoiceNo1"].ToString();
             decimal decDiscnt = Math.Round(Convert.ToDecimal(ListObjPurchaseReturnMaster[0].Rows[0]["discount"].ToString()), PublicVariables._inNoOfDecimalPlaces);
             ListObj = BllPurchaseReturn.PurchaseReturnDetailsViewByMasterId(decPurchaseReturnMasterId);
             dgvPurchaseReturn.Rows.Clear();
             for (int i = 0; i < ListObj[0].Rows.Count; i++)
             {
                 isAmountcalc = false;
                 dgvPurchaseReturn.Rows.Add();
                 int ini = dgvPurchaseReturn.Rows.Count;
                 decPurchaseReturnDetailsId = Convert.ToDecimal(ListObj[0].Rows[i]["purchaseReturnDetailsId"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtSlNo"].Value = ListObj[0].Rows[i]["slno"].ToString();
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtproductCode"].Value = ListObj[0].Rows[i]["productCode"].ToString();
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtproductName"].Value = ListObj[0].Rows[i]["productName"].ToString();
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtproductId"].Value = ListObj[0].Rows[i]["productId"].ToString();
                 decimal decProductId = Convert.ToDecimal(dgvPurchaseReturn.Rows[i].Cells["dgvtxtproductId"].Value.ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtConversionRate"].Value = BllUnitConvertion.UnitConversionRateByUnitConversionId(decimal.Parse(ListObj[0].Rows[i]["unitConversionId"].ToString()));
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtUnitConversionId"].Value = decimal.Parse(ListObj[0].Rows[i]["unitConversionId"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtqty"].Value = (ListObj[0].Rows[i]["qty"].ToString() != string.Empty) ? Convert.ToDecimal(ListObj[0].Rows[i]["qty"].ToString()) : 0;
                 if (ListObj[0].Rows[i]["unitName"].ToString() != string.Empty)
                 {
                     UnitComboFill(decProductId, dgvPurchaseReturn.Rows[i].Index, dgvPurchaseReturn.Rows[i].Cells["dgvcmbUnit"].ColumnIndex);
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbUnit"].Value = ListObj[0].Rows[i]["unitName"].ToString();
                 }
                 else
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbUnit"].Value = "NA";
                 }
                 dgvPurchaseReturn.Rows[i].Cells["dgvcmbUnit"].ReadOnly = (ListObjPurchaseReturnMaster[0].Rows[0]["voucherTypeId1"].ToString() != string.Empty) ? true : false;
                 if (ListObj[0].Rows[i]["godownId"].ToString() != string.Empty)
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbGodown"].Value = Convert.ToDecimal(ListObj[0].Rows[i]["godownId"].ToString());
                 }
                 else
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbGodown"].Value = "NA";
                 }
                 if (ListObj[0].Rows[i]["rackId"].ToString() != string.Empty)
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvCmbRack"].Value = Convert.ToDecimal(ListObj[0].Rows[i]["rackId"].ToString());
                 }
                 else
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvCmbRack"].Value = "NA";
                 }
                 if (ListObj[0].Rows[i]["batchId"].ToString() != string.Empty)
                 {
                     BatchComboFill(decProductId, dgvPurchaseReturn.Rows[i].Index, dgvPurchaseReturn.Rows[i].Cells["dgvcmbBatch"].ColumnIndex);
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbBatch"].Value = Convert.ToDecimal(ListObj[0].Rows[i]["batchId"].ToString());
                 }
                 else
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbBatch"].Value = "NA";
                 }
                 if (ListObj[0].Rows[i]["taxId"].ToString() != string.Empty && ListObj[0].Rows[i]["taxId"].ToString() != "NA")
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbTax"].Value = Convert.ToDecimal(ListObj[0].Rows[i]["taxId"].ToString());
                 }
                 else
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbTax"].Value = "NA";
                 }
                 if (ListObjPurchaseReturnMaster[0].Rows[0]["voucherTypeId1"].ToString() != string.Empty)
                 {
                     dgvPurchaseReturn.Rows[i].Cells["dgvcmbBatch"].ReadOnly = true;
                 }
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtPurchaseDetailsId"].Value = ListObj[0].Rows[i]["purchaseDetailsId"].ToString();
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtbarcode"].Value = ListObj[0].Rows[i]["barcode"].ToString();
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtPurchaseReturnDetailsId"].Value = decPurchaseReturnDetailsId;
                 decimal decRate = Convert.ToDecimal(ListObj[0].Rows[i]["rate"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtrate"].Value = Math.Round(decRate, PublicVariables._inNoOfDecimalPlaces);
                 decimal decGross = Convert.ToDecimal(ListObj[0].Rows[i]["grossAmount"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtgrossValue"].Value = Math.Round(decGross, PublicVariables._inNoOfDecimalPlaces);
                 decimal decDis = Convert.ToDecimal(ListObj[0].Rows[i]["discount"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtdiscount"].Value = Math.Round(decDis, PublicVariables._inNoOfDecimalPlaces);
                 decimal decNet = Convert.ToDecimal(ListObj[0].Rows[i]["netAmount"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtNetAmount"].Value = Math.Round(decNet, PublicVariables._inNoOfDecimalPlaces);
                 decimal decTax = Convert.ToDecimal(ListObj[0].Rows[i]["taxAmount"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxttaxAmount"].Value = Math.Round(decTax, PublicVariables._inNoOfDecimalPlaces);
                 decimal decTotal = Convert.ToDecimal(ListObj[0].Rows[i]["amount"].ToString());
                 dgvPurchaseReturn.Rows[i].Cells["dgvtxtAmount"].Value = Math.Round(decTotal, PublicVariables._inNoOfDecimalPlaces);
                 decTotalValue = decTotalValue + Convert.ToDecimal(ListObj[0].Rows[i]["amount"].ToString());
                 if (cmbInvoiceNo.Visible == true)
                 {
                     dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 1].ReadOnly = true;
                 }
             }
             lblTaxAmount.Text = Math.Round(decimal.Parse(ListObjPurchaseReturnMaster[0].Rows[0]["totalTax"].ToString()), 2).ToString(); ;
             txtTotalAmount.Text = decTotalValue.ToString();
             txtBillDiscount.Text = decDiscnt.ToString();
             DGVPurchaseReturnTaxFill();
             DGVTaxCombofill();
             Calculate();
             isAmountcalc = true;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PR:55" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Fill function for edit
 /// </summary>
 public void SalesInvoiceDetailsEditFill()
 {
     //SalesMasterSP spSalesMaster = new SalesMasterSP();
     //SalesDetailsSP spSalesDetails = new SalesDetailsSP();
     PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
     SalesDetailsInfo InfoSalesDetails = new SalesDetailsInfo();
     StockPostingInfo infoStockPosting = new StockPostingInfo();
     SalesMasterInfo InfoSalesMaster = new SalesMasterInfo();
     StockPostingBll BllStockPosting = new StockPostingBll();
     PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
     AdditionalCostInfo infoAdditionalCost = new AdditionalCostInfo();
     AditionalCostBll bllAditionalCost = new AditionalCostBll();
     SalesInvoiceBll BllSalesInvoice = new SalesInvoiceBll();
     SalesBillTaxInfo infoSalesBillTax = new SalesBillTaxInfo();
     UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
     try
     {
         string strAgainstInvoiceN0 = txtInvoiceNo.Text.Trim();
         for (int inI = 0; inI < dgvSalesInvoice.Rows.Count - 1; inI++)
         {
             decimal decRefStatus = BllSalesInvoice.SalesInvoiceReferenceCheckForEdit(decSalesInvoiceIdToEdit);
             if (decRefStatus != 0)
             {
                 dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductCode"].ReadOnly = true;
                 dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductName"].ReadOnly = true;
                 dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceBarcode"].ReadOnly = true;
                 dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].ReadOnly = true;
                 dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].ReadOnly = true;
             }
             if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSalesDetailsId"].Value == null || dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSalesDetailsId"].Value.ToString() == string.Empty || dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSalesDetailsId"].Value.ToString() == "0")   // here check the  row added or editing current row
             {
                 InfoSalesDetails.SalesMasterId = decSalesInvoiceIdToEdit;
                 InfoSalesDetails.ExtraDate = DateTime.Now;
                 InfoSalesDetails.Extra1 = string.Empty;
                 InfoSalesDetails.Extra2 = string.Empty;
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductName"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductName"].Value.ToString() != string.Empty)
                 {
                     if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value.ToString() != string.Empty)
                     {
                         if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSISalesOrderDetailsId"].Value != null)
                         {
                             InfoSalesDetails.OrderDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSISalesOrderDetailsId"].Value.ToString());
                         }
                         else
                         {
                             InfoSalesDetails.OrderDetailsId = 0;
                         }
                         if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value != null)
                         {
                             InfoSalesDetails.DeliveryNoteDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value.ToString());
                         }
                         else
                         {
                             InfoSalesDetails.DeliveryNoteDetailsId = 0;
                         }
                         if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQuotationDetailsId"].Value != null)
                         {
                             InfoSalesDetails.QuotationDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQuotationDetailsId"].Value.ToString());
                         }
                         else
                         {
                             InfoSalesDetails.QuotationDetailsId = 0;
                         }
                         InfoSalesDetails.SlNo = Convert.ToInt32(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSlno"].Value.ToString());
                         InfoSalesDetails.ProductId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductId"].Value.ToString());
                         decimal decQty = BllSalesInvoice.SalesInvoiceQuantityDetailsAgainstSalesReturn(DecSalesInvoiceVoucherTypeId, strVoucherNo);
                         if (Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value.ToString()) < decQty)
                         {
                             dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value = 0;
                             decRefStatus = 1;
                             MessageBox.Show("Quantity should be greater than " + decQty, "Open_Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             dgvSalesInvoice.Focus();
                         }
                         else
                         {
                             InfoSalesDetails.Qty = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value.ToString());
                             InfoSalesDetails.Rate = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
                             InfoSalesDetails.UnitId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoicembUnitName"].Value.ToString());
                             InfoSalesDetails.UnitConversionId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceUnitConversionId"].Value.ToString());
                             InfoSalesDetails.Discount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDiscountAmount"].Value.ToString());
                             if (dgvSalesInvoice.Columns["dgvcmbSalesInvoiceTaxName"].Visible)
                             {
                                 InfoSalesDetails.TaxId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceTaxName"].Value.ToString());
                                 InfoSalesDetails.TaxAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceTaxAmount"].Value.ToString());
                             }
                             else
                             {
                                 InfoSalesDetails.TaxId = 0;
                                 InfoSalesDetails.TaxAmount = 0;
                             }
                             if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString() != string.Empty)
                             {
                                 InfoSalesDetails.BatchId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString());
                             }
                             else
                             {
                                 InfoSalesDetails.BatchId = 0;
                             }
                             if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString() != string.Empty)
                             {
                                 InfoSalesDetails.GodownId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString());
                             }
                             else
                             {
                                 InfoSalesDetails.GodownId = 0;
                             }
                             if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString() != string.Empty)
                             {
                                 InfoSalesDetails.RackId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString());
                             }
                             else
                             {
                                 InfoSalesDetails.RackId = 0;
                             }
                             InfoSalesDetails.GrossAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceGrossValue"].Value.ToString());
                             InfoSalesDetails.NetAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceNetAmount"].Value.ToString());
                             InfoSalesDetails.Amount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceAmount"].Value.ToString());
                             BllSalesInvoice.SalesDetailsAdd(InfoSalesDetails);
                         }
                     }
                 }
             }
             else
             {
                 InfoSalesDetails.SalesMasterId = decSalesInvoiceIdToEdit;
                 InfoSalesDetails.SalesDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSalesDetailsId"].Value);
                 InfoSalesDetails.SlNo = Convert.ToInt32(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceSlno"].Value.ToString());
                 InfoSalesDetails.ProductId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductId"].Value.ToString());
                 InfoSalesDetails.Qty = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQty"].Value.ToString());
                 InfoSalesDetails.UnitId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoicembUnitName"].Value.ToString());
                 InfoSalesDetails.UnitConversionId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceUnitConversionId"].Value.ToString());
                 InfoSalesDetails.Rate = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
                 InfoSalesDetails.Discount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDiscountAmount"].Value.ToString());
                 if (dgvSalesInvoice.Columns["dgvcmbSalesInvoiceTaxName"].Visible)
                 {
                     InfoSalesDetails.TaxId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceTaxName"].Value.ToString());
                     InfoSalesDetails.TaxAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceTaxAmount"].Value.ToString());
                 }
                 else
                 {
                     InfoSalesDetails.TaxId = 0;
                     InfoSalesDetails.TaxAmount = 0;
                 }
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString() != string.Empty)
                 {
                     InfoSalesDetails.BatchId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString());
                 }
                 else
                 {
                     InfoSalesDetails.BatchId = 0;
                 }
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString() != string.Empty)
                 {
                     InfoSalesDetails.GodownId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString());
                     RackComboFill(InfoSalesDetails.GodownId, inI, dgvSalesInvoice.Columns["dgvcmbSalesInvoiceRack"].Index);
                 }
                 else
                 {
                     InfoSalesDetails.GodownId = 0;
                 }
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString() != string.Empty)
                 {
                     InfoSalesDetails.RackId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString());
                 }
                 else
                 {
                     InfoSalesDetails.RackId = 0;
                 }
                 InfoSalesDetails.GrossAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceGrossValue"].Value.ToString());
                 InfoSalesDetails.NetAmount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceNetAmount"].Value.ToString());
                 InfoSalesDetails.Amount = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceAmount"].Value.ToString());
                 InfoSalesDetails.ExtraDate = DateTime.Now;
                 InfoSalesDetails.Extra1 = string.Empty;
                 InfoSalesDetails.Extra2 = string.Empty;
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSISalesOrderDetailsId"].Value != null)
                 {
                     InfoSalesDetails.OrderDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSISalesOrderDetailsId"].Value.ToString());
                 }
                 else
                 {
                     InfoSalesDetails.OrderDetailsId = 0;
                 }
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value != null)
                 {
                     InfoSalesDetails.DeliveryNoteDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value.ToString());
                 }
                 else
                 {
                     InfoSalesDetails.DeliveryNoteDetailsId = 0;
                 }
                 if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQuotationDetailsId"].Value != null)
                 {
                     InfoSalesDetails.QuotationDetailsId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceQuotationDetailsId"].Value.ToString());
                 }
                 else
                 {
                     InfoSalesDetails.QuotationDetailsId = 0;
                 }
                 BllSalesInvoice.SalesDetailsEdit(InfoSalesDetails);
             }
             infoStockPosting.Date = Convert.ToDateTime(txtDate.Text.Trim().ToString());
             infoStockPosting.ProductId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceProductId"].Value.ToString());
             infoStockPosting.BatchId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceBatch"].Value.ToString());
             infoStockPosting.UnitId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoicembUnitName"].Value.ToString());
             if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString() != string.Empty)
             {
                 infoStockPosting.GodownId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString());
             }
             else
             {
                 infoStockPosting.GodownId = 0;
             }
             if (dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value != null && dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString() != string.Empty)
             {
                 infoStockPosting.RackId = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvcmbSalesInvoiceRack"].Value.ToString());
             }
             else
             {
                 infoStockPosting.RackId = 0;
             }
             if (Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceVoucherTypeId"].Value) == 0)
             {
                 decimal decResult =BllStockPosting.StockPostingDeleteForSalesInvoiceAgainstDeliveryNote(0, "NA", strVoucherNo, DecSalesInvoiceVoucherTypeId);
             }
             else
             {
                 decimal decResult = BllStockPosting.StockPostingDeleteForSalesInvoiceAgainstDeliveryNote(DecSalesInvoiceVoucherTypeId, strVoucherNo, strVoucherNoTostockPost, decVouchertypeIdTostockPost);
             }
             infoStockPosting.Rate = Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
             infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
             infoStockPosting.ExtraDate = DateTime.Now;
             infoStockPosting.Extra1 = string.Empty;
             infoStockPosting.Extra2 = string.Empty;
             if (dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value != null)
             {
                 if (Convert.ToDecimal(dgvSalesInvoice.Rows[inI].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value.ToString()) != 0)
                 {
                     infoStockPosting.InwardQty = InfoSalesDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(InfoSalesDetails.UnitConversionId);
                     infoStockPosting.OutwardQty = 0;
                     infoStockPosting.VoucherNo = strVoucherNoTostockPost;
                     infoStockPosting.AgainstVoucherNo = strVoucherNo;
                     infoStockPosting.InvoiceNo = strInvoiceNoTostockPost;
                     infoStockPosting.AgainstInvoiceNo = strAgainstInvoiceN0;
                     infoStockPosting.VoucherTypeId = decVouchertypeIdTostockPost;
                     infoStockPosting.AgainstVoucherTypeId = DecSalesInvoiceVoucherTypeId;
                     BllStockPosting.StockPostingAdd(infoStockPosting);
                 }
             }
             infoStockPosting.InwardQty = 0;
             infoStockPosting.OutwardQty = InfoSalesDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(InfoSalesDetails.UnitConversionId);
             infoStockPosting.VoucherNo = strVoucherNo;
             infoStockPosting.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
             infoStockPosting.InvoiceNo = strAgainstInvoiceN0;
             infoStockPosting.AgainstInvoiceNo = "NA";
             infoStockPosting.AgainstVoucherNo = "NA";
             infoStockPosting.AgainstVoucherTypeId = 0;
             BllStockPosting.StockPostingAdd(infoStockPosting);
         }
         int inAddRowCount = dgvSalesInvoiceLedger.RowCount;
         infoAdditionalCost.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
         if (isAutomatic)
         {
             infoAdditionalCost.VoucherNo = strVoucherNo;
         }
         else
         {
             infoAdditionalCost.VoucherNo = txtInvoiceNo.Text;
         }
         infoAdditionalCost.ExtraDate = DateTime.Now;
         infoAdditionalCost.Extra1 = string.Empty;
         infoAdditionalCost.Extra2 = string.Empty;
         for (int inIAdc = 0; inIAdc < inAddRowCount; inIAdc++)
         {
             if (dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvCmbAdditionalCostledgerName"].Value != null && dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvCmbAdditionalCostledgerName"].Value.ToString() != string.Empty)
             {
                 if (dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvtxtAdditionalCoastledgerAmount"].Value != null && dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvtxtAdditionalCoastledgerAmount"].Value.ToString() != string.Empty)
                 {
                     infoAdditionalCost.LedgerId = Convert.ToInt32(dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvCmbAdditionalCostledgerName"].Value.ToString());
                     if (cmbDrorCr.SelectedItem.ToString() != "Dr")
                     {
                         infoAdditionalCost.Debit = 0;
                         infoAdditionalCost.Credit = Convert.ToDecimal(dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvtxtAdditionalCoastledgerAmount"].Value.ToString());
                     }
                     else
                     {
                         infoAdditionalCost.Debit = Convert.ToDecimal(dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvtxtAdditionalCoastledgerAmount"].Value.ToString());
                         infoAdditionalCost.Credit = 0;
                     }
                     if (dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvtxtAdditionalCostId"].Value != null && dgvSalesInvoiceLedger.Rows[inIAdc].Cells["dgvtxtAdditionalCostId"].Value.ToString() != string.Empty)
                     {
                         bllAditionalCost.AdditionalCostEditByVoucherTypeIdAndVoucherNo(infoAdditionalCost);
                     }
                     else
                     {
                         bllAditionalCost.AdditionalCostAdd(infoAdditionalCost);
                     }
                 }
             }
         }
         if (cmbDrorCr.SelectedItem.ToString() != "Dr")               // here we are debit the cash or bank
         {
             decimal decCAshOrPartyId = 0;
             decCAshOrPartyId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
             decimal decTotalAddAmount = Convert.ToDecimal(lblLedgerTotalAmount.Text.Trim().ToString());
             infoAdditionalCost.Debit = decTotalAddAmount;
             infoAdditionalCost.Credit = 0;
             infoAdditionalCost.LedgerId = decCAshOrPartyId;
             infoAdditionalCost.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
             infoAdditionalCost.VoucherNo = strVoucherNo;
             infoAdditionalCost.ExtraDate = DateTime.Now;
             infoAdditionalCost.Extra1 = string.Empty;
             infoAdditionalCost.Extra2 = string.Empty;
             bllAditionalCost.AdditionalCostEditByVoucherTypeIdAndVoucherNo(infoAdditionalCost);
         }
         else
         {
             decimal decCAshOrBankId = 0;                    // here we are credit the cash or bank
             decCAshOrBankId = Convert.ToDecimal(cmbCashOrbank.SelectedValue.ToString());
             decimal decTotalAddAmount = Convert.ToDecimal(lblLedgerTotalAmount.Text.Trim().ToString());
             infoAdditionalCost.Debit = 0;
             infoAdditionalCost.Credit = decTotalAddAmount;
             infoAdditionalCost.LedgerId = decCAshOrBankId;
             infoAdditionalCost.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
             infoAdditionalCost.VoucherNo = strVoucherNo;
             infoAdditionalCost.ExtraDate = DateTime.Now;
             infoAdditionalCost.Extra1 = string.Empty;
             infoAdditionalCost.Extra2 = string.Empty;
             bllAditionalCost.AdditionalCostEditByVoucherTypeIdAndVoucherNo(infoAdditionalCost);
         }
         removeSalesInvoiceAdditionalDetails();
         if (dgvSalesInvoice.Columns["dgvcmbSalesInvoiceTaxName"].Visible)
         {
             int inTaxRowCount = dgvSalesInvoiceTax.RowCount;
             infoSalesBillTax.SalesMasterId = decSalesInvoiceIdToEdit;
             infoSalesBillTax.ExtraDate = DateTime.Now;
             infoSalesBillTax.Extra1 = string.Empty;
             infoSalesBillTax.Extra2 = string.Empty;
             for (int inTax = 0; inTax < inTaxRowCount; inTax++)
             {
                 if (dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxId"].Value != null && dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxId"].Value.ToString() != string.Empty)
                 {
                     if (dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxAmount"].Value != null && dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxAmount"].Value.ToString() != string.Empty)
                     {
                         decimal decAmount = Convert.ToDecimal(dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxAmount"].Value);
                         if (decAmount > 0)
                         {
                             infoSalesBillTax.TaxId = Convert.ToInt32(dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxId"].Value.ToString());
                             infoSalesBillTax.TaxAmount = Convert.ToDecimal(dgvSalesInvoiceTax.Rows[inTax].Cells["dgvtxtTtaxAmount"].Value.ToString());
                             BllSalesInvoice.SalesBillTaxEditBySalesMasterIdAndTaxId(infoSalesBillTax);
                         }
                     }
                 }
             }
         }
         if (BllSalesInvoice.SalesInvoiceInvoicePartyCheckEnableBillByBillOrNot(Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString())))
         {
             infoPartyBalance.Date = Convert.ToDateTime(txtDate.Text.ToString());
             infoPartyBalance.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
             infoPartyBalance.VoucherNo = strVoucherNo;
             infoPartyBalance.InvoiceNo = txtInvoiceNo.Text.Trim();
             infoPartyBalance.VoucherTypeId = DecSalesInvoiceVoucherTypeId;
             infoPartyBalance.AgainstVoucherTypeId = 0;
             infoPartyBalance.AgainstVoucherNo = "0";
             infoPartyBalance.AgainstInvoiceNo = "0";
             infoPartyBalance.ReferenceType = "New";
             infoPartyBalance.Debit = Convert.ToDecimal(txtGrandTotal.Text.Trim().ToString());
             decimal decBalAmount = BllSalesInvoice.SalesInvoiceReciptVoucherDetailsAgainstSI(DecSalesInvoiceVoucherTypeId, strVoucherNo);
             decimal decCurrentAmount = Convert.ToDecimal(txtGrandTotal.Text.ToString());
             if (decCurrentAmount < decBalAmount)
             {
                 MessageBox.Show("Amount should be greater than " + decBalAmount, "Open_Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 dgvSalesInvoice.Focus();
             }
             else
             {
                 infoPartyBalance.Credit = 0;
                 infoPartyBalance.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text.ToString());
                 infoPartyBalance.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue);
                 infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                 infoPartyBalance.ExtraDate = DateTime.Now;
                 infoPartyBalance.Extra1 = string.Empty;
                 infoPartyBalance.Extra2 = string.Empty;
                 BllPartyBalance.PartyBalanceEditByVoucherNoVoucherTypeIdAndReferenceType(infoPartyBalance);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SI: 81" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function for Unit Conversion Calculation
 /// </summary>
 /// <param name="inIndexOfRow"></param>
 public void UnitConversionCalc(int inIndexOfRow)
 {
     try
     {
         decimal decOldConversionRate = 0, decOldUnitConversionId = 0, decUnitId = 0, decProductId = 0;
         decimal decNewConversionRate = 0, decNewUnitConversionId = 0;
         decimal decProductRate = 0;
         UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
         List<DataTable> ListObj = new List<DataTable>();
         UnitBll bllUnit = new UnitBll();
         if (dgvPurchaseReturn.Rows[inIndexOfRow].Cells["dgvtxtproductId"].Value != null)
         {
             if (dgvPurchaseReturn.CurrentRow.Cells["dgvtxtproductId"].Value.ToString() != string.Empty &&
                            dgvPurchaseReturn.CurrentRow.Cells["dgvtxtproductId"].Value.ToString() != "0")
             {
                 decOldUnitConversionId = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvtxtUnitConversionId"].Value.ToString());
                 decOldConversionRate = bllUnitConvertion.UnitConversionRateByUnitConversionId(decOldUnitConversionId);
                 decUnitId = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvcmbUnit"].Value.ToString());
                 decProductId = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvtxtproductId"].Value.ToString());
                 decNewUnitConversionId = bllUnitConvertion.UnitconversionIdViewByUnitIdAndProductId(decUnitId, decProductId);
                 decNewConversionRate = bllUnitConvertion.UnitConversionRateByUnitConversionId(decNewUnitConversionId);
                 dgvPurchaseReturn.CurrentRow.Cells["dgvtxtUnitConversionId"].Value = decNewUnitConversionId;
                 if (dgvPurchaseReturn.CurrentRow.Cells["dgvtxtrate"].Value != null && dgvPurchaseReturn.CurrentRow.Cells["dgvtxtrate"].Value.ToString() != string.Empty)
                 {
                     decProductRate = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvtxtrate"].Value.ToString());
                     decProductRate = decProductRate * decOldConversionRate / decNewConversionRate;
                     dgvPurchaseReturn.CurrentRow.Cells["dgvtxtrate"].Value = Math.Round(decProductRate, PublicVariables._inNoOfDecimalPlaces);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PR:35" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Function to Fill Product Details in grid
        /// </summary>
        /// <param name="strProductCode"></param>
        /// <param name="rowIndex"></param>
        private void FillProductDetails(string strProductCode, int rowIndex)
        {
            try
            {
                ProductInfo infoProductFill = new ProductInfo();
                infoProductFill = new ProductCreationBll().ProductViewByCode(strProductCode);
                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                BatchBll BllBatch = new BatchBll();
                ProductCreationBll BllProductCreation = new ProductCreationBll();
                StockPostingBll BllStockPosting = new StockPostingBll();

                List<DataTable> list = new List<DataTable>();
                decimal decCurrentConversionRate = 0;
                if (infoProductFill.ProductId != 0)
                {
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtproductId"].Value = infoProductFill.ProductId;
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtproductCode"].Value = infoProductFill.ProductCode;
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtproductName"].Value = infoProductFill.ProductName;
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvcmbGodown"].Value = infoProductFill.GodownId;
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvcmbRack"].Value = infoProductFill.RackId;
                    UnitComboFill(infoProductFill.ProductId, rowIndex, dgvPurchaseReturn.CurrentRow.Cells["dgvcmbUnit"].ColumnIndex);
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvcmbUnit"].Value = infoProductFill.UnitId;
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtrate"].Value = Math.Round(infoProductFill.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProductFill.UnitId, infoProductFill.ProductId));
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtUnitConversionId"].Value.ToString()));
                    BatchComboFill(infoProductFill.ProductId, rowIndex, dgvPurchaseReturn.CurrentRow.Cells["dgvcmbBatch"].ColumnIndex);
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvcmbBatch"].Value = BllStockPosting.BatchViewByProductId(Convert.ToDecimal(dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtproductId"].Value));
                    list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProductFill.UnitId, infoProductFill.ProductName);
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtUnitConversionId"].Value = list[0].Rows[0]["unitconversionId"].ToString();
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
                    decCurrentConversionRate = Convert.ToDecimal(dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                    //dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtqty"].Value = 0;
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvcmbTax"].Value = infoProductFill.TaxId;
                    AmountCalculation("dgvtxtqty", rowIndex);
                    decimal decProdtId = infoProductFill.ProductId;
                    decBatchId = BllStockPosting.BatchViewByProductId(decProdtId);
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvcmbBatch"].Value = decBatchId;
                    string strBarcode = Convert.ToString(BllProductCreation.BarcodeViewByBatchId(decBatchId));
                    dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtbarcode"].Value = strBarcode;
                    if (dgvPurchaseReturn.Rows[rowIndex + 1].Cells["dgvtxtproductCode"].Selected == true)
                    {
                        dgvPurchaseReturn.Rows[rowIndex].Cells["dgvtxtqty"].Selected = true;
                        dgvPurchaseReturn.Rows[rowIndex + 1].Selected = false;
                        dgvPurchaseReturn.Rows[rowIndex].HeaderCell.Value = "X";
                        dgvPurchaseReturn.Rows[rowIndex].HeaderCell.Style.ForeColor = Color.Red;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PR:22" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Function to fill the product details in dataGridView from productCreationPopup
 /// </summary>
 /// <param name="productcreation"></param>
 /// <param name="decproductId"></param>
 /// <param name="decCurrentRowIndex"></param>
 public void productDetailsFillFromProductCreation(frmProductCreation productcreation, decimal decproductId, decimal decCurrentRowIndex)
 {
     try
     {
         decimal decCurrentConversionRate = 0;
         UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
         List<DataTable> list = new List<DataTable>();
         BatchBll BllBatch = new BatchBll();
         ProductInfo infoProductFill = new ProductInfo();
         ProductCreationBll BllProductCreation = new ProductCreationBll();
         ProductInfo infoProduct = new ProductInfo();
         int inI = dgvPurchaseReturn.CurrentRow.Index;
         if (inI == dgvPurchaseReturn.Rows.Count - 1)
         {
             dgvPurchaseReturn.Rows.Add();
         }
         if (decproductId != 0)
         {
             infoProduct = BllProductCreation.ProductView(decproductId);
             SerialNo();
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtproductCode"].Value = infoProduct.ProductCode;
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtproductId"].Value = decproductId.ToString();
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtproductName"].Value = infoProduct.ProductName;
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbRack"].Value = infoProduct.RackId;
             UnitComboFill(infoProduct.ProductId, dgvPurchaseReturn.CurrentRow.Index, dgvPurchaseReturn.CurrentRow.Cells["dgvcmbUnit"].ColumnIndex);
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtrate"].Value = Math.Round(infoProduct.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProduct.UnitId, infoProduct.ProductId));
             dgvPurchaseReturn.CurrentRow.Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvPurchaseReturn.Rows[inI].Cells["dgvtxtUnitConversionId"].Value.ToString()));
             BatchComboFill(decproductId, inI, dgvPurchaseReturn.Rows[inI].Cells["dgvcmbBatch"].ColumnIndex);
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
             RackComboFill(infoProduct.GodownId, inI, dgvPurchaseReturn.Rows[inI].Cells["dgvcmbRack"].ColumnIndex);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtqty"].Value = "0";
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtbarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvPurchaseReturn.Rows[inI].Cells["dgvcmbBatch"].Value.ToString()));
             list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
             decCurrentConversionRate = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());
             AmountCalculation("dgvtxtqty", inI);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtqty"].Selected = true;
             dgvPurchaseReturn.Rows[inI + 1].Selected = false;
         }
         dgvPurchaseReturn.Rows[inI].HeaderCell.Value = "X";
         dgvPurchaseReturn.Rows[inI].HeaderCell.Style.ForeColor = Color.Red;
     }
     catch (Exception ex)
     {
         MessageBox.Show("PR:24" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Function for Save and Edit
        /// </summary>  
        public void SaveOrEdit()
        {
            try
            {
                PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
                PurchaseReturnBll BllPurchaseReturn = new PurchaseReturnBll();
                PurchaseReturnDetailsInfo infoPurchaseReturnDetails = new PurchaseReturnDetailsInfo();
                StockPostingInfo infoStockPosting = new StockPostingInfo();
                //StockPostingSP spStockPosting = new StockPostingSP();
                StockPostingBll BllStockPosting = new StockPostingBll();
                UnitConvertionBll BllUnitConvertion = new UnitConvertionBll();
                LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
                LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
                ExchangeRateBll BllExchangeRate = new ExchangeRateBll();
                PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
                PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
                AccountLedgerInfo infoAccountLedger = new AccountLedgerInfo();
                AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
                PurchaseReturnBilltaxInfo infoPurchaseReturnBillTax = new PurchaseReturnBilltaxInfo();

                SettingsBll BllSettings = new SettingsBll();
                UnitBll bllUnit = new UnitBll();
                List<DataTable> ListObj = new List<DataTable>();
                string strAgainstVoucherNo = string.Empty;
                string strAgainstInvoiceNo = string.Empty;
                decimal decPurchaseReturnMasterIds = 0;
                decimal decPurchaseMasterId = 0;
                decimal decDiscount = 0;
                decimal decExchangeRate = 0;
                decimal decDis = 0;

                if (isAutomatic)
                {
                    if (strVoucherNo != string.Empty)
                    {
                        infoPurchaseReturnMaster.VoucherNo = strVoucherNo;
                    }
                    if (txtReturnNo.Text != string.Empty)
                    {
                        infoPurchaseReturnMaster.InvoiceNo = txtReturnNo.Text;
                    }
                }
                else
                {
                    infoPurchaseReturnMaster.VoucherNo = strVoucherNo;
                    infoPurchaseReturnMaster.InvoiceNo = txtReturnNo.Text;
                }
                if (decPurchaseReturnVoucherTypeId != 0)
                {
                    infoPurchaseReturnMaster.VoucherTypeId = decPurchaseReturnVoucherTypeId;
                }
                infoPurchaseReturnMaster.SuffixPrefixId = (decPurchaseReturnSuffixPrefixId != 0) ? decPurchaseReturnSuffixPrefixId : 0;
                infoPurchaseReturnMaster.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
                infoPurchaseReturnMaster.PurchaseAccount = Convert.ToDecimal(cmbPurchaseAccount.SelectedValue.ToString());
                if (cmbInvoiceNo.SelectedValue != null && cmbInvoiceNo.Visible == true)
                {
                    infoPurchaseReturnMaster.PurchaseMasterId = Convert.ToDecimal((cmbInvoiceNo.SelectedValue.ToString()));
                    decPurchaseMasterId = Convert.ToDecimal((cmbInvoiceNo.SelectedValue.ToString()));
                }
                else
                {
                    infoPurchaseReturnMaster.PurchaseMasterId = 0;
                }
                infoPurchaseReturnMaster.ExchangeRateId = (cmbCurrency.SelectedValue != null) ? Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()) : 0;
                infoPurchaseReturnMaster.Narration = txtNarration.Text.Trim();
                infoPurchaseReturnMaster.UserId = PublicVariables._decCurrentUserId;
                infoPurchaseReturnMaster.LrNo = txtLrlNo.Text.Trim();
                infoPurchaseReturnMaster.TransportationCompany = txtTransportationCompany.Text.Trim();
                infoPurchaseReturnMaster.Date = Convert.ToDateTime(txtDate.Text);
                infoPurchaseReturnMaster.TotalAmount = (txtTotalAmount.Text != string.Empty) ? Convert.ToDecimal(txtTotalAmount.Text) : 0;
                infoPurchaseReturnMaster.TotalTax = (lblTaxAmount.Text != string.Empty) ? Convert.ToDecimal(lblTaxAmount.Text) : 0;
                infoPurchaseReturnMaster.Discount = (txtBillDiscount.Text != string.Empty) ? Convert.ToDecimal(txtBillDiscount.Text) : 0;
                infoPurchaseReturnMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                infoPurchaseReturnMaster.Extra1 = string.Empty;
                infoPurchaseReturnMaster.Extra2 = string.Empty;
                infoPurchaseReturnMaster.ExtraDate = DateTime.Now;
                infoPurchaseReturnMaster.GrandTotal = (txtGrandTotal.Text != string.Empty) ? Convert.ToDecimal(txtGrandTotal.Text) : 0;
                if (btnSave.Text == "Save")
                {
                    decPurchaseReturnMasterIds = BllPurchaseReturn.PurchaseReturnMasterAddWithReturnIdentity(infoPurchaseReturnMaster);
                }
                else
                {
                    infoPurchaseReturnMaster.PurchaseReturnMasterId = decPurchaseReturnMasterId;
                    decExchangeRate = BllExchangeRate.ExchangeRateViewByExchangeRateId(infoPurchaseReturnMaster.ExchangeRateId);
                    BllPurchaseReturn.PurchaseReturnMasterEdit(infoPurchaseReturnMaster);
                    infoPurchaseMaster = BllPurchaseInvoice.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                    BllLedgerPosting.LedgerPostDelete(strVoucherNo, decPurchaseReturnVoucherTypeId);
                    bllAccountLedger.PartyBalanceDeleteByVoucherTypeVoucherNoAndReferenceType(strVoucherNo, decPurchaseReturnVoucherTypeId);
                }

                infoLedgerPosting.Date = infoPurchaseReturnMaster.Date;
                infoLedgerPosting.VoucherTypeId = infoPurchaseReturnMaster.VoucherTypeId;
                infoLedgerPosting.VoucherNo = infoPurchaseReturnMaster.VoucherNo;
                infoLedgerPosting.ChequeNo = string.Empty;
                infoLedgerPosting.ChequeDate = DateTime.Now;
                infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                infoLedgerPosting.InvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                infoLedgerPosting.Extra1 = string.Empty;
                infoLedgerPosting.Extra2 = string.Empty;

                infoLedgerPosting.LedgerId = infoPurchaseReturnMaster.PurchaseAccount;
                infoLedgerPosting.Debit = 0;
                if (btnSave.Text == "Save")
                {
                    infoLedgerPosting.Credit = TotalNetAmountCalculation() * BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                    infoLedgerPosting.ExtraDate = DateTime.Now;
                }
                else
                {
                    infoLedgerPosting.Credit = TotalNetAmountCalculation();
                }
                BllLedgerPosting.LedgerPostingAdd(infoLedgerPosting);

                infoLedgerPosting.LedgerId = infoPurchaseReturnMaster.LedgerId;
                if (btnSave.Text == "Save")
                {
                    infoLedgerPosting.Debit = Convert.ToDecimal(txtGrandTotal.Text) * BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                    infoLedgerPosting.ExtraDate = DateTime.Now;
                }
                else
                {
                    infoLedgerPosting.Debit = Convert.ToDecimal(txtGrandTotal.Text);
                }
                infoLedgerPosting.Credit = 0;
                BllLedgerPosting.LedgerPostingAdd(infoLedgerPosting);

                foreach (DataGridViewRow dgvrow in dgvPurchaseReturnTax.Rows)
                {
                    if (dgvrow.Cells["dgvtxtTaxId"].Value != null && dgvrow.Cells["dgvtxtTaxId"].Value.ToString() != string.Empty)
                    {
                        infoLedgerPosting.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvtxtledgerId"].Value.ToString());
                        infoLedgerPosting.Credit = (dgvrow.Cells["dgvtxtAmounts"].Value == null) ? 0 : Convert.ToDecimal(dgvrow.Cells["dgvtxtAmounts"].Value.ToString());
                        infoLedgerPosting.Debit = 0;
                        infoLedgerPosting.ExtraDate = DateTime.Now;
                        BllLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                    }
                }

                if (txtBillDiscount.Text.Trim() != string.Empty)
                {
                    decDis = Convert.ToDecimal(txtBillDiscount.Text);
                }
                if (decDis >= 0)
                {
                    infoLedgerPosting.Debit = 0;
                    infoLedgerPosting.Credit = decDis;
                    infoLedgerPosting.LedgerId = 9;
                    infoLedgerPosting.ExtraDate = DateTime.Now;
                    BllLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                }

                if (btnSave.Text == "Update")
                {
                    infoPurchaseReturnMaster = BllPurchaseReturn.PurchaseReturnMasterView(decPurchaseReturnMasterId);
                    if (infoPurchaseReturnMaster.PurchaseMasterId == 0)
                    {
                        BllStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType(0, "NA", infoPurchaseReturnMaster.VoucherNo, infoPurchaseReturnMaster.VoucherTypeId);
                    }
                    if (infoPurchaseReturnMaster.PurchaseMasterId != 0)
                    {
                        infoPurchaseMaster = BllPurchaseInvoice.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                        BllStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType(infoPurchaseMaster.VoucherTypeId, strInvoiceNo, strVoucherNo, infoPurchaseReturnMaster.VoucherTypeId);
                    }
                }

                foreach (DataGridViewRow dgvrow in dgvPurchaseReturn.Rows)
                {
                    if (dgvrow.Cells["dgvtxtProductId"].Value != null && dgvrow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
                    {
                        infoPurchaseReturnDetails.ExtraDate = DateTime.Now;
                        infoPurchaseReturnDetails.Extra1 = string.Empty;
                        infoPurchaseReturnDetails.Extra2 = string.Empty;
                        infoPurchaseReturnDetails.PurchaseReturnMasterId = (btnSave.Text == "Save") ? decPurchaseReturnMasterIds : decPurchaseReturnMasterId;
                        infoPurchaseReturnDetails.ProductId = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtproductId"].Value));
                        infoPurchaseReturnDetails.Qty = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtqty"].Value));
                        infoPurchaseReturnDetails.Rate = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtrate"].Value));
                        if (btnSave.Text == "Save")
                        {
                            infoPurchaseReturnDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                            infoPurchaseReturnDetails.UnitConversionId = BllUnitConvertion.UnitconversionIdViewByUnitIdAndProductId(infoPurchaseReturnDetails.UnitId, infoPurchaseReturnDetails.ProductId);
                        }
                        else
                        {
                            if (Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value) == 0)
                            {
                                try
                                {
                                    infoPurchaseReturnDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                                }
                                catch
                                {
                                    infoPurchaseReturnDetails.UnitId = bllUnit.UnitIdByUnitName(Convert.ToString(dgvrow.Cells["dgvcmbUnit"].Value.ToString()));
                                }
                                infoPurchaseReturnDetails.UnitConversionId = BllUnitConvertion.UnitconversionIdViewByUnitIdAndProductId(infoPurchaseReturnDetails.UnitId, infoPurchaseReturnDetails.ProductId);
                            }
                            else
                            {
                                try
                                {
                                    infoPurchaseReturnDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                                }
                                catch
                                {
                                    infoPurchaseReturnDetails.UnitId = bllUnit.UnitIdByUnitName(Convert.ToString(dgvrow.Cells["dgvcmbUnit"].Value.ToString()));
                                }
                            }
                            infoPurchaseReturnDetails.UnitConversionId = Convert.ToDecimal(dgvrow.Cells["dgvtxtUnitConversionId"].Value);
                        }
                        infoPurchaseReturnDetails.Discount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtdiscount"].Value));
                        if (dgvrow.Cells["dgvcmbTax"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbTax"].Value) != string.Empty && dgvrow.Cells["dgvcmbTax"].Value as string != "NA")
                        {
                            infoPurchaseReturnDetails.TaxId = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvcmbTax"].Value));
                            if (strTaxComboFill != string.Empty)
                            {
                                infoPurchaseReturnDetails.TaxAmount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtTaxAmount"].Value));
                            }
                        }
                        else
                        {
                            infoPurchaseReturnDetails.TaxId = 0;
                        }
                        if (dgvrow.Cells["dgvcmbBatch"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbBatch"].Value) != string.Empty)
                        {
                            infoPurchaseReturnDetails.BatchId = Convert.ToDecimal(dgvrow.Cells["dgvcmbBatch"].Value);
                        }
                        else
                        {
                            infoPurchaseReturnDetails.GodownId = 0;
                        }
                        if (dgvrow.Cells["dgvcmbGodown"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbGodown"].Value) != string.Empty)
                        {
                            infoPurchaseReturnDetails.GodownId = Convert.ToDecimal(dgvrow.Cells["dgvcmbGodown"].Value);
                        }
                        else
                        {
                            infoPurchaseReturnDetails.RackId = 0;
                        }
                        if (dgvrow.Cells["dgvcmbRack"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbRack"].Value) != string.Empty)
                        {
                            infoPurchaseReturnDetails.RackId = Convert.ToDecimal(dgvrow.Cells["dgvcmbRack"].Value);
                        }
                        infoPurchaseReturnDetails.GrossAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtgrossValue"].Value.ToString());
                        infoPurchaseReturnDetails.NetAmount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtNetAmount"].Value));
                        infoPurchaseReturnDetails.Amount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtAmount"].Value));
                        infoPurchaseReturnDetails.SlNo = Convert.ToInt32(Convert.ToString(dgvrow.Cells["dgvtxtSlNo"].Value));
                        infoPurchaseReturnDetails.PurchaseDetailsId = (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value != null) ? Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value)) : 0;
                        if (dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value != null)
                        {
                            if (dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value.ToString() == "0" || dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value.ToString() == string.Empty)
                            {
                                BllPurchaseReturn.PurchaseReturnDetailsAddWithReturnIdentity(infoPurchaseReturnDetails);
                            }
                            else
                            {
                                infoPurchaseReturnDetails.PurchaseReturnDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value.ToString());
                                BllPurchaseReturn.PurchaseReturnDetailsEdit(infoPurchaseReturnDetails);
                            }
                        }
                        else
                        {
                            BllPurchaseReturn.PurchaseReturnDetailsAddWithReturnIdentity(infoPurchaseReturnDetails);
                        }

                        if (btnSave.Text == "Save")
                        {
                            infoPurchaseMaster = BllPurchaseInvoice.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                        }
                        infoStockPosting.Date = infoPurchaseReturnMaster.Date;
                        infoStockPosting.ProductId = infoPurchaseReturnDetails.ProductId;
                        infoStockPosting.BatchId = infoPurchaseReturnDetails.BatchId;
                        infoStockPosting.UnitId = infoPurchaseReturnDetails.UnitId;
                        infoStockPosting.GodownId = infoPurchaseReturnDetails.GodownId;
                        infoStockPosting.RackId = infoPurchaseReturnDetails.RackId;
                        decimal decConversionId = BllUnitConvertion.UnitConversionRateByUnitConversionId(infoPurchaseReturnDetails.UnitConversionId);
                        //infoStockPosting.OutwardQty = infoPurchaseReturnDetails.Qty / (decConversionId == 0 ? 1 : decConversionId);
                        infoStockPosting.OutwardQty = infoPurchaseReturnDetails.Qty;
                        infoStockPosting.InwardQty = 0;
                        infoStockPosting.Rate = infoPurchaseReturnDetails.Rate;
                        infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                        infoStockPosting.Extra1 = string.Empty;
                        infoStockPosting.Extra2 = string.Empty;
                        if (infoPurchaseReturnDetails.PurchaseDetailsId != 0)
                        {
                            infoStockPosting.AgainstVoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                            infoStockPosting.AgainstVoucherNo = infoPurchaseMaster.VoucherNo;
                            infoStockPosting.AgainstInvoiceNo = infoPurchaseMaster.InvoiceNo;
                            infoStockPosting.VoucherNo = strVoucherNo;
                            infoStockPosting.InvoiceNo = txtReturnNo.Text.Trim();
                            infoStockPosting.VoucherTypeId = decPurchaseReturnVoucherTypeId;
                            decAgainstVoucherTypeId = infoStockPosting.VoucherTypeId;
                        }
                        else
                        {
                            infoStockPosting.AgainstVoucherTypeId = 0;
                            infoStockPosting.AgainstVoucherNo = "NA";
                            infoStockPosting.AgainstInvoiceNo = "NA";
                            infoStockPosting.VoucherNo = infoPurchaseReturnMaster.VoucherNo;
                            infoStockPosting.InvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                            infoStockPosting.VoucherTypeId = decPurchaseReturnVoucherTypeId;
                            decAgainstVoucherTypeId = 0;
                        }
                        BllStockPosting.StockPostingAdd(infoStockPosting);
                    }
                }
                if (btnSave.Text == "Update")
                {
                    removePurchaseReturnDetails();
                }

                infoAccountLedger = bllAccountLedger.AccountLedgerView(infoPurchaseReturnMaster.LedgerId);
                if (infoAccountLedger.BillByBill == true)
                {
                    infoPartyBalance.Date = infoPurchaseReturnMaster.Date;
                    infoPartyBalance.LedgerId = infoPurchaseReturnMaster.LedgerId;
                    if (decAgainstVoucherTypeId != 0)
                    {
                        infoPartyBalance.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                        infoPartyBalance.VoucherNo = infoPurchaseMaster.VoucherNo;
                        infoPartyBalance.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                        infoPartyBalance.AgainstVoucherTypeId = infoPurchaseReturnMaster.VoucherTypeId;
                        infoPartyBalance.AgainstVoucherNo = infoPurchaseReturnMaster.VoucherNo;
                        infoPartyBalance.AgainstInvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                        infoPartyBalance.ReferenceType = "Against";
                    }
                    else
                    {
                        infoPartyBalance.VoucherTypeId = infoPurchaseReturnMaster.VoucherTypeId;
                        infoPartyBalance.VoucherNo = infoPurchaseReturnMaster.VoucherNo;
                        infoPartyBalance.InvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                        infoPartyBalance.AgainstVoucherTypeId = 0;
                        infoPartyBalance.AgainstVoucherNo = "NA";
                        infoPartyBalance.AgainstInvoiceNo = "NA";
                        infoPartyBalance.ReferenceType = "New";
                    }
                    infoPartyBalance.Debit = infoPurchaseReturnMaster.TotalAmount;
                    infoPartyBalance.Credit = 0;
                    infoPartyBalance.CreditPeriod = 0;
                    infoPartyBalance.ExchangeRateId = infoPurchaseReturnMaster.ExchangeRateId;
                    infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                    infoPartyBalance.Extra1 = string.Empty;
                    infoPartyBalance.Extra2 = string.Empty;
                    BllPartyBalance.PartyBalanceAdd(infoPartyBalance);
                }

                foreach (DataGridViewRow item in dgvPurchaseReturnTax.Rows)
                {
                    if (item.Cells["dgvtxtTaxId"].Value != null)
                    {
                        if (Convert.ToDecimal(item.Cells["dgvtxtAmounts"].Value.ToString()) > 0)
                        {
                            infoPurchaseReturnBillTax.PurchaseReturnMasterId = (btnSave.Text == "Save") ? decPurchaseReturnMasterIds : decPurchaseReturnMasterIds;
                            infoPurchaseReturnBillTax.TaxId = Convert.ToDecimal(item.Cells["dgvtxtTaxId"].Value.ToString());
                            infoPurchaseReturnBillTax.TaxAmount = (item.Cells["dgvtxtAmounts"].Value == null) ? 0 : Convert.ToDecimal(item.Cells["dgvtxtAmounts"].Value.ToString());
                            infoPurchaseReturnBillTax.Extra1 = string.Empty;
                            infoPurchaseReturnBillTax.Extra2 = string.Empty;
                            BllPurchaseReturn.PurchaseReturnBilltaxAdd(infoPurchaseReturnBillTax);
                        }
                    }
                }
                if (btnSave.Text == "Save")
                {
                    Messages.SavedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                        {
                            PrintForDotMatrix(decPurchaseReturnMasterIds);
                        }
                        else
                        {
                            Print(decPurchaseReturnMasterIds);
                        }
                    }
                    Clear();
                }
                else
                {
                    decDiscount = Convert.ToDecimal(txtBillDiscount.Text);
                    Messages.UpdatedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                        {
                            PrintForDotMatrix(decPurchaseReturnMasterId);
                        }
                        else
                        {
                            Print(decPurchaseReturnMasterId);
                        }
                    }
                    if (frmPurchaseReturnRegisterObj != null)
                    {
                        frmPurchaseReturnRegisterObj.GridFill();
                        frmPurchaseReturnRegisterObj.Enabled = true;
                    }

                    if (ObjPurchaseReturnReport != null)
                    {
                        ObjPurchaseReturnReport.PurchaseReturnReportGridFill();
                        ObjPurchaseReturnReport.Enabled = true;
                    }
                    if (frmLedgerDetailsObj != null)
                    {
                        frmLedgerDetailsObj.LedgerDetailsView();
                        frmLedgerDetailsObj.Enabled = true;
                    }
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PR:49" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function for Product details fill in grid corresponding to invoice number
        /// </summary>
        public void InvoiceDetailsFill()
        {
            PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
            try
            {
                if (!isEditFill)
                {
                    inMaxCount = 0;
                    string strTaxRate1 = string.Empty;
                    string strproductId = string.Empty;
                    decimal decTaxAmount = 0;
                    decimal decAmount = 0;
                    decimal decNetAmount = 0;
                    decimal decTaxRate = 0;
                    decimal decDiscountAmount = 0;
                    decimal decGrossAmount = 0;
                    int inRowIndex = 0;
                    UnitConvertionBll bllUnitConversion = new UnitConvertionBll();
                    PurchaseReturnBll BllPurchaseReturn = new PurchaseReturnBll();
                    List<DataTable> ListObjDetails = BllPurchaseInvoice.PurchaseDetailsViewByPurchaseMasterIdWithRemaining(Convert.ToDecimal(cmbInvoiceNo.SelectedValue.ToString()), decPurchaseReturnMasterId, decPurchaseReturnVoucherTypeId);
                    dgvPurchaseReturn.Rows.Clear();
                    foreach (DataRow drowDetails in ListObjDetails[0].Rows)
                    {
                        dgvPurchaseReturn.Rows.Add();
                        DGVUnitComboFill();
                        DGVBatchComboFill();
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtPurchaseDetailsId"].Value = drowDetails.ItemArray[0].ToString();
                        strproductId = drowDetails.ItemArray[2].ToString();
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductId"].Value = Convert.ToString(drowDetails.ItemArray[2]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductName"].Value = Convert.ToString(drowDetails.ItemArray[5]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductCode"].Value = Convert.ToString(drowDetails.ItemArray[6]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtVoucherTypeId"].Value = Convert.ToString(drowDetails.ItemArray[21]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtVoucherNo"].Value = Convert.ToString(drowDetails.ItemArray[23]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtInvoiceNo"].Value = Convert.ToString(drowDetails.ItemArray[22]);
                        decQty = Math.Round(Convert.ToDecimal(Convert.ToString(drowDetails.ItemArray[10])), PublicVariables._inNoOfDecimalPlaces);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtqty"].Value = decQty;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtrate"].Value = Convert.ToString(drowDetails.ItemArray[11]);
                        decRate = Math.Round(Convert.ToDecimal(Convert.ToString(drowDetails.ItemArray[11])), PublicVariables._inNoOfDecimalPlaces);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtbarcode"].Value = Convert.ToString(drowDetails.ItemArray[8]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtConversionRate"].Value = bllUnitConversion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(drowDetails.ItemArray[9].ToString()));
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drowDetails.ItemArray[9].ToString());
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbGodown"].Value = (Convert.ToString(drowDetails.ItemArray[17]) != null) ? Convert.ToDecimal(drowDetails.ItemArray[17].ToString()) : 0;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbRack"].Value = (drowDetails.ItemArray[18].ToString() != null) ? Convert.ToDecimal(drowDetails.ItemArray[18].ToString()) : 0;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbBatch"].Value = (drowDetails.ItemArray[19].ToString() != null) ? Convert.ToDecimal(drowDetails.ItemArray[19].ToString()) : 0;
                        decGrossAmount = decQty * decRate;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(drowDetails.ItemArray[12].ToString());
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbUnit"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drowDetails.ItemArray[9]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtgrossValue"].Value = Math.Round(decGrossAmount, PublicVariables._inNoOfDecimalPlaces);

                        if (Convert.ToString(drowDetails.ItemArray[7]) != string.Empty)
                        {
                            dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtdiscount"].Value = Convert.ToString(drowDetails.ItemArray[7]);
                            decDiscountAmount = Math.Round(Convert.ToDecimal(Convert.ToString(drowDetails.ItemArray[7])), PublicVariables._inNoOfDecimalPlaces);
                        }
                        else
                        {
                            decDiscountAmount = 0;
                            dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtdiscount"].Value = 0;
                        }
                        decNetAmount = decGrossAmount - decDiscountAmount;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtNetAmount"].Value = (decNetAmount != 0) ? Math.Round(decNetAmount, PublicVariables._inNoOfDecimalPlaces) : 0;
                        if (strTaxComboFill != string.Empty)
                        {
                            if (drowDetails.ItemArray[13].ToString() != string.Empty && Convert.ToDecimal(drowDetails.ItemArray[13].ToString()) != 0)
                            {
                                dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbTax"].Value = Convert.ToDecimal(drowDetails.ItemArray[13].ToString());
                                strTaxRate1 = BllPurchaseReturn.TaxRateFromPurchaseDetails(Convert.ToDecimal(Convert.ToString(drowDetails.ItemArray[13])));
                            }
                            else
                            {
                                dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbTax"].Value = "NA";
                                strTaxRate1 = string.Empty;
                            }

                            if (strTaxRate1 != string.Empty)
                            {
                                decTaxRate = Convert.ToDecimal(strTaxRate1);
                                decTaxAmount = ((decNetAmount * decTaxRate) / 100);
                                dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxttaxAmount"].Value = Math.Round(decTaxAmount, PublicVariables._inNoOfDecimalPlaces);
                            }
                            else
                            {
                                dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxttaxAmount"].Value = 0;
                            }
                        }
                        else
                        {
                            dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxttaxAmount"].Value = 0;
                        }
                        decAmount = decNetAmount + decTaxAmount;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtAmount"].Value = Math.Round(decAmount, PublicVariables._inNoOfDecimalPlaces);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductCode"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductName"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtbarcode"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbBatch"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtInRowIndex"].Value = Convert.ToString(drowDetails["extra1"]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 1].ReadOnly = true;
                        int intIndex = 0;
                        int.TryParse(Convert.ToString(drowDetails["extra1"]), out intIndex);
                        if (inMaxCount < intIndex)
                            inMaxCount = intIndex;
                        inRowIndex++;
                    }
                    for (int i = inRowIndex; i < dgvPurchaseReturn.Rows.Count; ++i)
                        dgvPurchaseReturn["dgvtxtInRowIndex", i].Value = Convert.ToString(GetNextinRowIndex());
                    CalculateTotalAmount();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PR:42" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// Function to save stockjournal
        /// </summary>
        public void Save()
        {
            try
            {
                StockJournalMasterInfo infoStockJournalMaster = new StockJournalMasterInfo();
                StockJournalDetailsInfo infoStockJournalDetails = new StockJournalDetailsInfo();
                StockJournalBll BllStockJournal = new StockJournalBll();
                LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
                LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
                AdditionalCostInfo infoAdditionalCost = new AdditionalCostInfo();
                AditionalCostBll bllAdditionalCost = new AditionalCostBll();
                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                if (isAutomatic == true)
                {
                    infoStockJournalMaster.SuffixPrefixId = decSuffixPrefixId;
                    infoStockJournalMaster.VoucherNo = strVoucherNo;
                }
                else
                {
                    infoStockJournalMaster.SuffixPrefixId = 0;
                    infoStockJournalMaster.VoucherNo = strVoucherNo;
                }
                infoStockJournalMaster.ExtraDate = DateTime.Now;
                infoStockJournalMaster.InvoiceNo = txtVoucherNo.Text.Trim();
                infoStockJournalMaster.Date = Convert.ToDateTime(txtDate.Text);
                infoStockJournalMaster.AdditionalCost = Convert.ToDecimal(lblAdditionalCostAmount.Text);
                infoStockJournalMaster.VoucherNo = strVoucherNo;
                infoStockJournalMaster.VoucherTypeId = decVoucherTypeId;
                infoStockJournalMaster.Narration = txtNarration.Text.Trim();
                infoStockJournalMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                infoStockJournalMaster.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue);
                if (rbtnManufacturing.Checked)
                {
                    infoStockJournalMaster.Extra1 = "Manufacturing";
                }
                if (rbtnTransfer.Checked)
                {
                    infoStockJournalMaster.Extra1 = "Transfer";
                }
                if (rbtnStockOut.Checked)
                {
                    infoStockJournalMaster.Extra1 = "Stock Out";
                }
                infoStockJournalMaster.Extra2 = string.Empty;
                if (btnSave.Text == "Save")
                {
                    decStockMasterId = BllStockJournal.StockJournalMasterAdd(infoStockJournalMaster);
                }
                else
                {
                    infoStockJournalMaster.StockJournalMasterId = decStockJournalMasterIdForEdit;
                    BllStockJournal.StockJournalMasterEdit(infoStockJournalMaster);
                    RemoveRowStockJournalConsumptionDetails();
                    RemoveRowStockJournalProductionDetails();
                    RemoveRowStockJournalAdditionalCostDetails();
                    BllStockPosting.DeleteStockPostingForStockJournalEdit(strVoucherNo, decVoucherTypeId);
                }

                if (dgvConsumption.Rows.Count > 0)
                {
                    int inCount = dgvConsumption.Rows.Count;

                    for (int i = 0; i < inCount - 1; i++)
                    {
                        if (btnSave.Text == "Save")
                        {
                            infoStockJournalDetails.StockJournalMasterId = decStockMasterId;
                        }
                        else
                        {
                            infoStockJournalMaster.StockJournalMasterId = decStockJournalMasterIdForEdit;
                        }
                        infoStockJournalDetails.Extra1 = string.Empty;
                        infoStockJournalDetails.Extra2 = string.Empty;
                        infoStockJournalDetails.ExtraDate = DateTime.Now;
                        infoStockJournalDetails.ProductId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductId"].Value);
                        infoStockJournalDetails.Qty = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionQty"].Value);
                        infoStockJournalDetails.Rate = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionRate"].Value);
                        infoStockJournalDetails.UnitId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionunitId"].Value);
                        infoStockJournalDetails.UnitConversionId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionunitConversionId"].Value);
                        if (btnSave.Text == "Update")
                        {
                            infoStockJournalDetails.StockJournalMasterId = decStockJournalMasterIdForEdit;
                            if (dgvConsumption.Rows[i].Cells["dgvtxtConsumptionStockJournalDetailsId"].Value == null || dgvConsumption.Rows[i].Cells["dgvtxtConsumptionStockJournalDetailsId"].Value.ToString() == string.Empty)
                            {

                                if (dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value != null && dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.BatchId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.BatchId = 0;
                                }
                                if (dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value != null && dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.GodownId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.GodownId = 0;
                                }
                                if (dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value != null && dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.RackId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.RackId = 0;
                                }
                                infoStockJournalDetails.Amount = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionAmount"].Value);
                                infoStockJournalDetails.ConsumptionOrProduction = "Consumption";
                                infoStockJournalDetails.Slno = Convert.ToInt32(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionSlNo"].Value);
                                BllStockJournal.StockJournalDetailsAdd(infoStockJournalDetails);
                            }
                            else
                            {
                                infoStockJournalDetails.StockJournalDetailsId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionStockJournalDetailsId"].Value);
                                if (dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value != null && dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.BatchId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.BatchId = 0;
                                }
                                if (dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value != null && dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.GodownId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.GodownId = 0;
                                }
                                if (dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value != null && dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.RackId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.RackId = 0;
                                }
                                infoStockJournalDetails.Amount = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionAmount"].Value);
                                infoStockJournalDetails.ConsumptionOrProduction = "Consumption";
                                infoStockJournalDetails.Slno = Convert.ToInt32(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionSlNo"].Value);
                                BllStockJournal.StockJournalDetailsEdit(infoStockJournalDetails);
                            }
                        }
                        else
                        {
                            infoStockJournalDetails.BatchId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value);
                            infoStockJournalDetails.GodownId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value);
                            infoStockJournalDetails.RackId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value);
                            infoStockJournalDetails.Amount = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionAmount"].Value);
                            infoStockJournalDetails.ConsumptionOrProduction = "Consumption";
                            infoStockJournalDetails.Slno = Convert.ToInt32(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionSlNo"].Value);
                            BllStockJournal.StockJournalDetailsAdd(infoStockJournalDetails);
                        }
                        //Stock Posting Add
                        if (btnSave.Text == "Update")
                        {
                            infoStockPosting.BatchId = infoStockJournalDetails.BatchId;
                            infoStockPosting.Date = Convert.ToDateTime(txtDate.Text);
                            infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                            infoStockPosting.GodownId = infoStockJournalDetails.GodownId;
                            infoStockPosting.InwardQty = 0;
                            infoStockPosting.OutwardQty = infoStockJournalDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(infoStockJournalDetails.UnitConversionId);
                            infoStockPosting.ProductId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductId"].Value);
                            infoStockPosting.RackId = infoStockJournalDetails.RackId;
                            infoStockPosting.Rate = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionRate"].Value);
                            infoStockPosting.UnitId = infoStockJournalDetails.UnitId;
                            infoStockPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                            infoStockPosting.VoucherNo = strVoucherNo;
                            infoStockPosting.VoucherTypeId = decVoucherTypeId;
                            infoStockPosting.AgainstVoucherTypeId = 0;
                            infoStockPosting.AgainstInvoiceNo = "NA";
                            infoStockPosting.AgainstVoucherNo = "NA";
                            infoStockPosting.Extra1 = string.Empty;
                            infoStockPosting.Extra2 = string.Empty;
                            BllStockPosting.StockPostingAdd(infoStockPosting);
                        }
                        else
                        {
                            infoStockPosting.BatchId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value);
                            infoStockPosting.Date = Convert.ToDateTime(txtDate.Text);
                            infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                            infoStockPosting.GodownId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value);
                            infoStockPosting.InwardQty = 0;
                            infoStockPosting.OutwardQty = infoStockJournalDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(infoStockJournalDetails.UnitConversionId);
                            infoStockPosting.ProductId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductId"].Value);
                            infoStockPosting.RackId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value);
                            infoStockPosting.Rate = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvtxtConsumptionRate"].Value);
                            infoStockPosting.UnitId = Convert.ToDecimal(dgvConsumption.Rows[i].Cells["dgvcmbConsumptionunitId"].Value);
                            infoStockPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                            infoStockPosting.VoucherNo = strVoucherNo;
                            infoStockPosting.VoucherTypeId = decVoucherTypeId;
                            infoStockPosting.AgainstVoucherTypeId = 0;
                            infoStockPosting.AgainstInvoiceNo = "NA";
                            infoStockPosting.AgainstVoucherNo = "NA";
                            infoStockPosting.Extra1 = string.Empty;
                            infoStockPosting.Extra2 = string.Empty;
                            BllStockPosting.StockPostingAdd(infoStockPosting);
                        }
                    }

                }
                if (dgvProduction.Rows.Count > 0)
                {
                    int inCount = dgvProduction.Rows.Count;

                    for (int i = 0; i < inCount - 1; i++)
                    {

                        if (btnSave.Text == "Update")
                        {
                            infoStockJournalMaster.StockJournalMasterId = decStockJournalMasterIdForEdit;
                        }
                        else
                        {
                            infoStockJournalDetails.StockJournalMasterId = decStockMasterId;
                        }
                        infoStockJournalDetails.Extra1 = string.Empty;
                        infoStockJournalDetails.Extra2 = string.Empty;
                        infoStockJournalDetails.ExtraDate = DateTime.Now;
                        infoStockJournalDetails.ProductId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionProductId"].Value);
                        infoStockJournalDetails.Qty = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionQty"].Value);
                        infoStockJournalDetails.Rate = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionRate"].Value);
                        infoStockJournalDetails.UnitId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionunitId"].Value);
                        infoStockJournalDetails.UnitConversionId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionunitConversionId"].Value);
                        if (btnSave.Text == "Update")
                        {
                            infoStockJournalDetails.StockJournalMasterId = decStockJournalMasterIdForEdit;
                            if (dgvProduction.Rows[i].Cells["dgvtxtProductionStockJournalDetailsId"].Value == null || dgvProduction.Rows[i].Cells["dgvtxtProductionStockJournalDetailsId"].Value.ToString() == string.Empty)
                            {

                                if (dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value != null && dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.BatchId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.BatchId = 0;
                                }
                                if (dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value != null && dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.GodownId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.GodownId = 0;
                                }
                                if (dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value != null && dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.RackId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.RackId = 0;
                                }
                                infoStockJournalDetails.Amount = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionAmount"].Value);
                                infoStockJournalDetails.ConsumptionOrProduction = "Production";
                                infoStockJournalDetails.Slno = Convert.ToInt32(dgvProduction.Rows[i].Cells["dgvtxtProductionSlNo"].Value);
                                BllStockJournal.StockJournalDetailsAdd(infoStockJournalDetails);
                            }
                            else
                            {
                                infoStockJournalDetails.StockJournalDetailsId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionStockJournalDetailsId"].Value);
                                if (dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value != null && dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.BatchId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.BatchId = 0;
                                }
                                if (dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value != null && dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.GodownId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.GodownId = 0;
                                }
                                if (dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value != null && dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value.ToString() != string.Empty)
                                {
                                    infoStockJournalDetails.RackId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value);
                                }
                                else
                                {
                                    infoStockJournalDetails.RackId = 0;
                                }
                                infoStockJournalDetails.Amount = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionAmount"].Value);
                                infoStockJournalDetails.ConsumptionOrProduction = "Production";
                                infoStockJournalDetails.Slno = Convert.ToInt32(dgvProduction.Rows[i].Cells["dgvtxtProductionSlNo"].Value);
                                BllStockJournal.StockJournalDetailsEdit(infoStockJournalDetails);
                            }
                        }
                        else
                        {
                            infoStockJournalDetails.BatchId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value);
                            infoStockJournalDetails.GodownId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value);
                            infoStockJournalDetails.RackId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value);
                            infoStockJournalDetails.Amount = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionAmount"].Value);
                            infoStockJournalDetails.ConsumptionOrProduction = "Production";
                            infoStockJournalDetails.Slno = Convert.ToInt32(dgvProduction.Rows[i].Cells["dgvtxtProductionSlNo"].Value);
                            BllStockJournal.StockJournalDetailsAdd(infoStockJournalDetails);
                        }
                        //Stock Posting Add
                        if (btnSave.Text == "Save")
                        {
                            infoStockPosting.BatchId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value);
                            infoStockPosting.Date = Convert.ToDateTime(txtDate.Text);
                            infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                            infoStockPosting.GodownId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value);
                            infoStockPosting.InwardQty = infoStockJournalDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(infoStockJournalDetails.UnitConversionId);
                            infoStockPosting.OutwardQty = 0;
                            infoStockPosting.ProductId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionProductId"].Value);
                            infoStockPosting.RackId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value);
                            infoStockPosting.Rate = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionRate"].Value);
                            infoStockPosting.UnitId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvcmbProductionunitId"].Value);
                            infoStockPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                            infoStockPosting.VoucherNo = strVoucherNo;
                            infoStockPosting.VoucherTypeId = decVoucherTypeId;
                            infoStockPosting.AgainstVoucherTypeId = 0;
                            infoStockPosting.AgainstInvoiceNo = "NA";
                            infoStockPosting.AgainstVoucherNo = "NA";
                            infoStockPosting.Extra1 = string.Empty;
                            infoStockPosting.Extra2 = string.Empty;
                            BllStockPosting.StockPostingAdd(infoStockPosting);
                        }
                        else
                        {
                            infoStockPosting.BatchId = infoStockJournalDetails.BatchId;
                            infoStockPosting.Date = Convert.ToDateTime(txtDate.Text);
                            infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                            infoStockPosting.GodownId = infoStockJournalDetails.GodownId;
                            infoStockPosting.InwardQty = infoStockJournalDetails.Qty / bllUnitConvertion.UnitConversionRateByUnitConversionId(infoStockJournalDetails.UnitConversionId);
                            infoStockPosting.OutwardQty = 0;
                            infoStockPosting.ProductId = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionProductId"].Value);
                            infoStockPosting.RackId = infoStockJournalDetails.RackId;
                            infoStockPosting.Rate = Convert.ToDecimal(dgvProduction.Rows[i].Cells["dgvtxtProductionRate"].Value);
                            infoStockPosting.UnitId = infoStockJournalDetails.UnitId;
                            infoStockPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                            infoStockPosting.VoucherNo = strVoucherNo;
                            infoStockPosting.VoucherTypeId = decVoucherTypeId;
                            infoStockPosting.AgainstVoucherTypeId = 0;
                            infoStockPosting.AgainstInvoiceNo = "NA";
                            infoStockPosting.AgainstVoucherNo = "NA";
                            infoStockPosting.Extra1 = string.Empty;
                            infoStockPosting.Extra2 = string.Empty;
                            BllStockPosting.StockPostingAdd(infoStockPosting);
                        }
                    }
                }
                //....Additional Cost Add...////
                if (btnSave.Text == "Update")
                {
                    BllLedgerPosting.DeleteLedgerPostingForStockJournalEdit(strVoucherNo, decVoucherTypeId);//Delete
                    bllAdditionalCost.DeleteAdditionalCostForStockJournalEdit(strVoucherNo, decVoucherTypeId);//Delete
                }
                decimal decGrandTotal = 0;
                decimal decRate = 0;
                ExchangeRateBll BllExchangeRate = new ExchangeRateBll();
                if (dgvAdditionalCost.Rows.Count > 1)
                {
                    infoAdditionalCost.Credit = Convert.ToDecimal(lblAdditionalCostAmount.Text);
                    infoAdditionalCost.Debit = 0;
                    infoAdditionalCost.LedgerId = Convert.ToDecimal(cmbCashOrBank.SelectedValue);
                    infoAdditionalCost.VoucherNo = strVoucherNo;
                    infoAdditionalCost.VoucherTypeId = decVoucherTypeId;
                    infoAdditionalCost.Extra1 = string.Empty;
                    infoAdditionalCost.Extra2 = string.Empty;
                    infoAdditionalCost.ExtraDate = DateTime.Now;
                    bllAdditionalCost.AdditionalCostAdd(infoAdditionalCost);
                    //....Ledger Posting Add...///
                    //-------------------  Currency Conversion-----------------------------
                    decGrandTotal = Convert.ToDecimal(lblAdditionalCostAmount.Text.Trim());
                    decRate = BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                    decGrandTotal = decGrandTotal * decRate;
                    //---------------------------------------------------------------
                    infoLedgerPosting.Credit = decGrandTotal;
                    infoLedgerPosting.Debit = 0;
                    infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
                    infoLedgerPosting.DetailsId = 0;
                    infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                    infoLedgerPosting.LedgerId = Convert.ToDecimal(cmbCashOrBank.SelectedValue);
                    infoLedgerPosting.VoucherNo = strVoucherNo;
                    infoLedgerPosting.VoucherTypeId = decVoucherTypeId;
                    infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                    infoLedgerPosting.ChequeDate = DateTime.Now;
                    infoLedgerPosting.ChequeNo = string.Empty;
                    infoLedgerPosting.Extra1 = string.Empty;
                    infoLedgerPosting.Extra2 = string.Empty;
                    if (btnSave.Text == "Save")
                    {
                        infoLedgerPosting.ExtraDate = DateTime.Now;
                    }
                    BllLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                    foreach (DataGridViewRow dgvrow in dgvAdditionalCost.Rows)
                    {
                        if (dgvrow.Cells["dgvcmbAdditionalCostLedger"].Value != null)
                        {
                            if (dgvrow.Cells["dgvcmbAdditionalCostLedger"].Value.ToString() != string.Empty)
                            {
                                /*-----------------------------------------Additional Cost Add----------------------------------------------------*/
                                infoAdditionalCost.Credit = 0;
                                infoAdditionalCost.Debit = Convert.ToDecimal(dgvrow.Cells["dgvtxtAdditionalCostAmount"].Value.ToString());
                                infoAdditionalCost.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvcmbAdditionalCostLedger"].Value.ToString());
                                infoAdditionalCost.VoucherNo = strVoucherNo;
                                infoAdditionalCost.VoucherTypeId = decVoucherTypeId;
                                infoAdditionalCost.Extra1 = string.Empty;
                                infoAdditionalCost.Extra2 = string.Empty;
                                infoAdditionalCost.ExtraDate = DateTime.Now;
                                bllAdditionalCost.AdditionalCostAdd(infoAdditionalCost);
                                /*-----------------------------------------Additional Cost Ledger Posting----------------------------------------------------*/
                                decimal decTotal = 0;
                                //-------------------  Currency Conversion------------------------
                                decTotal = Convert.ToDecimal(infoAdditionalCost.Debit);
                                decRate = BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                                decTotal = decTotal * decRate;
                                //---------------------------------------------------------------
                                infoLedgerPosting.Credit = 0;
                                infoLedgerPosting.Debit = decTotal;
                                infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
                                infoLedgerPosting.DetailsId = 0;
                                infoLedgerPosting.InvoiceNo = txtVoucherNo.Text.Trim();
                                infoLedgerPosting.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvcmbAdditionalCostLedger"].Value.ToString());
                                infoLedgerPosting.VoucherNo = strVoucherNo;
                                infoLedgerPosting.VoucherTypeId = decVoucherTypeId;
                                infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                                infoLedgerPosting.ChequeDate = DateTime.Now;
                                infoLedgerPosting.ChequeNo = string.Empty;
                                infoLedgerPosting.Extra1 = string.Empty;
                                infoLedgerPosting.Extra2 = string.Empty;
                                infoLedgerPosting.ExtraDate = DateTime.Now;
                                BllLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                            }
                        }
                    }
                }
                if (btnSave.Text == "Save")
                {
                    Messages.SavedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        Print(decStockMasterId);
                    }
                    Clear();
                }
                else
                {
                    Messages.UpdatedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        Print(decStockJournalMasterIdForEdit);
                    }
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("SJ:36" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// To select the product from ProductSearchPopup
        /// </summary>
        /// <param name="frmProductSearchPopup"></param>
        /// <param name="decproductId"></param>
        /// <param name="decCurrentRowIndex"></param>
        public void CallFromProductSearchPopup(frmProductSearchPopup frmProductSearchPopup, decimal decproductId, decimal decCurrentRowIndex)
        {
            ProductInfo infoProduct = new ProductInfo();
            ProductCreationBll BllProductCreation = new ProductCreationBll();
            UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
            BatchBll BllBatch = new BatchBll();
            try
            {
                base.Show();
                this.frmProductSearchPopupObj = frmProductSearchPopup;
                infoProduct = BllProductCreation.ProductView(decproductId);
                int inRowcount = dgvProduct.Rows.Count;

                for (int i = 0; i < inRowcount; i++)
                {
                    if (i == inRowcount - 1)
                    {
                        dgvProduct.Rows.Add();
                    }
                    if (i == decCurrentRowIndex)
                    {
                        SerialNo();
                        dgvProduct.Rows[i].Cells["dgvtxtproductCode"].Value = infoProduct.ProductCode;
                        dgvProduct.Rows[i].Cells["productId"].Value = decproductId.ToString();
                        dgvProduct.Rows[i].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
                        dgvProduct.Rows[i].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
                        dgvProduct.Rows[i].Cells["dgvCmbRack"].Value = infoProduct.RackId;
                        UnitComboFill(decproductId, i, dgvProduct.Rows[i].Cells["dgvcmbUnit"].ColumnIndex);
                        dgvProduct.Rows[i].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;

                        List<DataTable> list = new List<DataTable>();
                        list = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[i].Cells["productId"].Value.ToString());

                        dgvProduct.Rows[i].Cells["dgvtxtRate"].Value = Math.Round(infoProduct.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
                        dgvProduct.Rows[i].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProduct.UnitId, infoProduct.ProductId));
                        dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvProduct.Rows[i].Cells["dgvtxtUnitConversionId"].Value.ToString()));
                        BatchComboFill(decproductId, i, dgvProduct.Rows[i].Cells["dgvcmbBatch"].ColumnIndex);
                        dgvProduct.Rows[i].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
                        RackComboFill(infoProduct.GodownId, i, dgvProduct.Rows[i].Cells["dgvCmbRack"].ColumnIndex);
                        dgvProduct.Rows[i].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[i].Cells["dgvcmbBatch"].Value.ToString()));
                        list= bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
                        dgvProduct.Rows[i].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
                        decCurrentConversionRate = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());
                        NewAmountCalculation("dgvtxtQty", i);
                        CalculateTotalAmount();

                    }
                }
                frmProductSearchPopupObj.Close();
                frmProductSearchPopupObj = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("MR4:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        ///  Function to fill the product details in dataGridView from productCreationPopup
        /// </summary>
        /// <param name="productcreation"></param>
        /// <param name="decproductId"></param>
        /// <param name="decCurrentRowIndex"></param>
        public void productDetailsFillFromProductCreation(frmProductCreation productcreation, decimal decproductId, decimal decCurrentRowIndex)
        {
            try
            {
                decimal decCurrentConversionRate = 0;
                DataTable dtbl = new DataTable();
                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                BatchBll BllBatch = new BatchBll();
                ProductInfo infoProductFill = new ProductInfo();
                ProductCreationBll BllProductCreation = new ProductCreationBll();
                ProductInfo infoProduct = new ProductInfo();
                int inI = dgvProduct.CurrentRow.Index;
                if (inI == dgvProduct.Rows.Count - 1)
                {
                    dgvProduct.Rows.Add();
                }
                  if (decproductId != 0)
                    {
                        infoProduct = BllProductCreation.ProductView(decproductId);
                        SerialNo();
                        dgvProduct.Rows[inI].Cells["dgvtxtproductCode"].Value = infoProduct.ProductCode;
                        dgvProduct.Rows[inI].Cells["productId"].Value = decproductId.ToString();
                        dgvProduct.Rows[inI].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
                        dgvProduct.Rows[inI].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
                        dgvProduct.Rows[inI].Cells["dgvCmbRack"].Value = infoProduct.RackId;
                        UnitComboFill(decproductId, inI, dgvProduct.Rows[inI].Cells["dgvcmbUnit"].ColumnIndex);
                        dgvProduct.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;

                        List<DataTable> list = new  List<DataTable>();
                        list = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[inI].Cells["productId"].Value.ToString());
                        //UnitComboFill(infoProduct.ProductId, dgvProduct.CurrentRow.Index, dgvProduct.CurrentRow.Cells["dgvcmbUnit"].ColumnIndex);
                        //dgvProduct.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
                        dgvProduct.Rows[inI].Cells["dgvtxtRate"].Value = Math.Round(infoProduct.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
                        dgvProduct.Rows[inI].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProduct.UnitId, infoProduct.ProductId));
                        dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvtxtUnitConversionId"].Value.ToString()));
                        BatchComboFill(decproductId, inI, dgvProduct.Rows[inI].Cells["dgvcmbBatch"].ColumnIndex);
                        dgvProduct.Rows[inI].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
                        RackComboFill(infoProduct.GodownId, inI, dgvProduct.Rows[inI].Cells["dgvCmbRack"].ColumnIndex);
                        dgvProduct.Rows[inI].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvcmbBatch"].Value.ToString()));
                        list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
                        dgvProduct.Rows[inI].Cells["dgvtxtConversionRate"].Value = dtbl.Rows[0]["conversionRate"].ToString();
                        decCurrentConversionRate = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());

                        NewAmountCalculation("dgvtxtQty", inI);
                        CalculateTotalAmount();
                    }

            }
            catch (Exception ex)
            {
                MessageBox.Show("MR6:new" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// On cell end edit of dgvProductDetails
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvProductDetails_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     string strProductCode = string.Empty;
     string strProductName = string.Empty;
     string strBarcode = string.Empty;
     decimal decOldUnitConversionId = 0;
     decimal decNewUnitConversionId = 0;
     decimal decProductRate = 0;
     decimal decOldConversionRate = 0;
     decimal decNewConversionRate = 0;
     decimal decUnitId = 0;
     decimal decProductId = 0;
     UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
     try
     {
         if (e.ColumnIndex == dgvProductDetails.Columns["dgvcmbGodown"].Index)
         {
             if (dgvProductDetails.CurrentRow.Cells["dgvcmbRack"].Value != null)
             {
                 dgvProductDetails.CurrentRow.Cells["dgvcmbRack"].Value = Convert.ToDecimal("1");
             }
         }
         if (e.ColumnIndex == dgvProductDetails.Columns["dgvcmbUnit"].Index)
         {
             if (dgvProductDetails.CurrentRow.Cells["dgvcmbUnit"].Value != null)
             {
                 if (dgvProductDetails.CurrentRow.Cells["dgvcmbUnit"].Value.ToString() != string.Empty &&
                    dgvProductDetails.CurrentRow.Cells["dgvcmbUnit"].Value.ToString() != "0")
                 {
                     if (dgvProductDetails.CurrentRow.Cells["dgvtxtProductId"].Value != null)
                     {
                         if (dgvProductDetails.CurrentRow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty &&
                            dgvProductDetails.CurrentRow.Cells["dgvtxtProductId"].Value.ToString() != "0")
                         {
                             decOldUnitConversionId = Convert.ToDecimal(dgvProductDetails.CurrentRow.Cells["dgvtxtUnitConversionId"].Value.ToString());
                             decOldConversionRate = bllUnitConvertion.UnitConversionRateByUnitConversionId(decOldUnitConversionId);
                             decUnitId = Convert.ToDecimal(dgvProductDetails.CurrentRow.Cells["dgvcmbUnit"].Value.ToString());
                             decProductId = Convert.ToDecimal(dgvProductDetails.CurrentRow.Cells["dgvtxtProductId"].Value.ToString());
                             decNewUnitConversionId = bllUnitConvertion.UnitconversionIdViewByUnitIdAndProductId(decUnitId, decProductId);
                             decNewConversionRate = bllUnitConvertion.UnitConversionRateByUnitConversionId(decNewUnitConversionId);
                             dgvProductDetails.CurrentRow.Cells["dgvtxtUnitConversionId"].Value = decNewUnitConversionId;
                             if (dgvProductDetails.CurrentRow.Cells["dgvtxtRate"].Value != null)
                             {
                                 if (dgvProductDetails.CurrentRow.Cells["dgvtxtRate"].Value.ToString() != string.Empty)
                                 {
                                     decProductRate = Convert.ToDecimal(dgvProductDetails.CurrentRow.Cells["dgvtxtRate"].Value.ToString());
                                     decProductRate = decProductRate * decOldConversionRate / decNewConversionRate;
                                     dgvProductDetails.CurrentRow.Cells["dgvtxtRate"].Value = Math.Round(decProductRate, PublicVariables._inNoOfDecimalPlaces);
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if (e.ColumnIndex == dgvProductDetails.Columns["dgvtxtProductCode"].Index)
         {
             if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value != null)
             {
                 if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString() != string.Empty)
                 {
                     strProductCode = dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString();
                     ProductDetailsFill(strProductCode, e.RowIndex, "ProductCode");
                 }
             }
         }
         if (e.ColumnIndex == dgvProductDetails.Columns["dgvtxtProductName"].Index)
         {
             if (dgvProductDetails.CurrentRow.Cells["dgvcmbBatch"].Value != null)
             {
                 dgvProductDetails.CurrentRow.Cells["dgvcmbBatch"].Value = 1;
                 dgvProductDetails.CurrentRow.Cells["dgvtxtBarcode"].Value = string.Empty;
             }
             if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value != null)
             {
                 if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value.ToString() != string.Empty)
                 {
                     strProductName = dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value.ToString();
                     ProductDetailsFill(strProductName, e.RowIndex, "ProductName");
                 }
             }
         }
         if (e.ColumnIndex == dgvProductDetails.Columns["dgvtxtBarcode"].Index)
         {
             if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value != null)
             {
                 if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value.ToString() != string.Empty)
                 {
                     strBarcode = dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value.ToString();
                     ProductDetailsFill(strBarcode, e.RowIndex, "Barcode");
                 }
             }
         }
         Calculate(e.RowIndex);
         CalculateTotalAmount();
         if (dgvTax.Visible)
         {
             TotalTaxAmount();
         }
         CalculateGrandTotal();
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI82:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }