/// <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>
 /// Remove all sales invoice additional cost details here
 /// </summary>
 public void removeSalesInvoiceAdditionalDetails()
 {
     AditionalCostBll bllAditionalCost = new AditionalCostBll();
     try
     {
         foreach (var strId in lstArrOfRemoveLedger)
         {
             decimal decDeleteId = Convert.ToDecimal(strId);
             bllAditionalCost.AdditionalCostDelete(decDeleteId);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SI: 80" + 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 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>
        /// Function to fill the details while calling from register or report 
        /// </summary>
        public void FillRegisterOrReport()
        {
            StockJournalBll BllStockJournal = new StockJournalBll();
            StockJournalMasterInfo infoStockJournalMaster = new StockJournalMasterInfo();
            StockJournalDetailsInfo infoStockJournalDetails = new StockJournalDetailsInfo();
            AditionalCostBll bllAdditionalCost = new AditionalCostBll();
            VoucherTypeBll BllVoucherType = new VoucherTypeBll();
            try
            {

                isFillMode = true;
                btnSave.Text = "Update";
                cmbFinishedGoods.Enabled = false;
                txtQty.Enabled = false;
                btnAdd.Enabled = false;
                dgvConsumption.Rows.Clear();
                dgvProduction.Rows.Clear();
                dgvAdditionalCost.Rows.Clear();
                List<DataTable> listObjMaster = BllStockJournal.StockJournalMasterFillForRegisterOrReport(decStockJournalMasterIdForEdit);
                StockJournalVoucherTypeId = Convert.ToDecimal(listObjMaster[0].Rows[0]["voucherTypeId"].ToString());
                VoucherTypeInfo infoVoucherType = new VoucherTypeInfo();
                infoVoucherType = BllVoucherType.VoucherTypeView(StockJournalVoucherTypeId);
                this.Text = infoVoucherType.VoucherTypeName;
                txtDate.Text = listObjMaster[0].Rows[0]["date"].ToString();
                strVoucherNo = listObjMaster[0].Rows[0]["voucherNo"].ToString();
                decSuffixPrefixId = Convert.ToDecimal(listObjMaster[0].Rows[0]["suffixPrefixId"].ToString());
                decVoucherTypeId = Convert.ToDecimal(listObjMaster[0].Rows[0]["voucherTypeId"].ToString());
                isAutomatic = BllVoucherType.CheckMethodOfVoucherNumbering(StockJournalVoucherTypeId);
                dtpDate.Value = Convert.ToDateTime(txtDate.Text);
                txtNarration.Text = listObjMaster[0].Rows[0]["narration"].ToString();
                cmbCurrency.SelectedValue = Convert.ToDecimal(listObjMaster[0].Rows[0]["exchangeRateId"].ToString());

                if (listObjMaster[0].Rows[0]["extra1"].ToString() == "Manufacturing")
                {
                    rbtnManufacturing.Checked = true;
                }
                if (listObjMaster[0].Rows[0]["extra1"].ToString() == "Transfer")
                {
                    rbtnTransfer.Checked = true;
                }
                if (listObjMaster[0].Rows[0]["extra1"].ToString() == "Stock Out")
                {
                    rbtnStockOut.Checked = true;
                }
                txtVoucherNo.Text = listObjMaster[0].Rows[0]["invoiceNo"].ToString();

                DataSet dsDetails = BllStockJournal.StockJournalDetailsForRegisterOrReport(decStockJournalMasterIdForEdit);

                DataTable dtblConsumption = dsDetails.Tables[0];
                if (dsDetails.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < dtblConsumption.Rows.Count; i++)
                    {
                        dgvConsumption.Rows.Add();
                        dgvConsumption.Rows[i].HeaderCell.Value = string.Empty;
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionStockJournalDetailsId"].Value = Convert.ToDecimal(dtblConsumption.Rows[i]["stockJournalDetailsId"].ToString());
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionSlNo"].Value = dtblConsumption.Rows[i]["slno"].ToString();
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionBarcode"].Value = dtblConsumption.Rows[i]["barcode"].ToString();
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductCode"].Value = dtblConsumption.Rows[i]["productCode"].ToString();
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductName"].Value = dtblConsumption.Rows[i]["productName"].ToString();
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductId"].Value = dtblConsumption.Rows[i]["productId"].ToString();
                        IsSetGridValueChange = true;
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionQty"].Value = dtblConsumption.Rows[i]["qty"].ToString();
                        IsSetGridValueChange = false;
                        dgvConsumption.Rows[i].Cells["dgvcmbConsumptionGodown"].Value = Convert.ToDecimal(dtblConsumption.Rows[i]["godownId"].ToString());
                        dgvConsumption.Rows[i].Cells["dgvcmbConsumptionRack"].Value = Convert.ToDecimal(dtblConsumption.Rows[i]["rackId"].ToString());
                        if (dtblConsumption.Rows[i]["batchId"] != null && dtblConsumption.Rows[i]["batchId"].ToString() != "0")
                        {
                            dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value = Convert.ToDecimal(dtblConsumption.Rows[i]["batchId"].ToString());
                        }
                        else
                        {
                            dgvConsumption.Rows[i].Cells["dgvcmbConsumptionBatch"].Value = string.Empty;
                        }
                        IsSetGridValueChange = true;
                        dgvConsumption.Rows[i].Cells["dgvcmbConsumptionunitId"].Value = Convert.ToDecimal(dtblConsumption.Rows[i]["unitId"].ToString());
                        IsSetGridValueChange = false;
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionUnitConversionId"].Value = dtblConsumption.Rows[i]["unitConversionId"].ToString();
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionBarcode"].ReadOnly = true;
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductCode"].ReadOnly = true;
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionProductName"].ReadOnly = true;
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionRate"].Value = dtblConsumption.Rows[i]["rate"].ToString();
                        dgvConsumption.Rows[i].Cells["dgvtxtConsumptionAmount"].Value = dtblConsumption.Rows[i]["amount"].ToString();
                        dgvConsumption.Rows[i].HeaderCell.Value = "";
                    }
                }
                DataTable dtblProduction = dsDetails.Tables[1];
                for (int i = 0; i < dtblProduction.Rows.Count; i++)
                {
                    dgvProduction.Rows.Add();
                    dgvProduction.Rows[i].HeaderCell.Value = string.Empty;
                    dgvProduction.Rows[i].Cells["dgvtxtProductionStockJournalDetailsId"].Value = Convert.ToDecimal(dtblProduction.Rows[i]["stockJournalDetailsId"].ToString());
                    dgvProduction.Rows[i].Cells["dgvtxtProductionSlNo"].Value = dtblProduction.Rows[i]["slno"].ToString();
                    dgvProduction.Rows[i].Cells["dgvtxtProductionBarcode"].Value = dtblProduction.Rows[i]["barcode"].ToString();
                    dgvProduction.Rows[i].Cells["dgvtxtProductionProductCode"].Value = dtblProduction.Rows[i]["productCode"].ToString();
                    dgvProduction.Rows[i].Cells["dgvtxtProductionProductName"].Value = dtblProduction.Rows[i]["productName"].ToString();
                    dgvProduction.Rows[i].Cells["dgvtxtProductionProductId"].Value = dtblProduction.Rows[i]["productId"].ToString();
                    IsSetGridValueChange = true;
                    dgvProduction.Rows[i].Cells["dgvtxtProductionQty"].Value = dtblProduction.Rows[i]["qty"].ToString();
                    IsSetGridValueChange = false;
                    if (rbtnManufacturing.Checked)
                    {
                        cmbFinishedGoods.SelectedValue = dgvProduction.Rows[i].Cells["dgvtxtProductionProductId"].Value;
                        if (cmbFinishedGoods.SelectedValue != null)
                        {
                            cmbFinishedGoods.Enabled = true;
                            txtQty.Enabled = true;
                            btnAdd.Enabled = true;
                            txtQty.Text = dtblProduction.Rows[i]["qty"].ToString();
                        }

                    }
                    dgvProduction.Rows[i].Cells["dgvcmbProductionGodown"].Value = Convert.ToDecimal(dtblProduction.Rows[i]["godownId"].ToString());
                    dgvProduction.Rows[i].Cells["dgvcmbProductionRack"].Value = Convert.ToDecimal(dtblProduction.Rows[i]["rackId"].ToString());
                    if (dtblProduction.Rows[i]["batchId"] != null && dtblProduction.Rows[i]["batchId"].ToString() != string.Empty)
                    {
                        dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value = Convert.ToDecimal(dtblProduction.Rows[i]["batchId"].ToString());
                    }
                    else
                    {
                        dgvProduction.Rows[i].Cells["dgvcmbProductionBatch"].Value = string.Empty;
                    }
                    IsSetGridValueChange = true;
                    dgvProduction.Rows[i].Cells["dgvcmbProductionunitId"].Value = Convert.ToDecimal(dtblProduction.Rows[i]["unitId"].ToString());
                    IsSetGridValueChange = false;
                    dgvProduction.Rows[i].Cells["dgvtxtProductionUnitConversionId"].Value = dtblProduction.Rows[i]["unitConversionId"].ToString();
                    dgvProduction.Rows[i].Cells["dgvtxtProductionBarcode"].ReadOnly = true;
                    dgvProduction.Rows[i].Cells["dgvtxtProductionProductCode"].ReadOnly = true;
                    dgvProduction.Rows[i].Cells["dgvtxtProductionProductName"].ReadOnly = true;
                    dgvProduction.Rows[i].Cells["dgvtxtProductionRate"].Value = dtblProduction.Rows[i]["rate"].ToString();
                    dgvProduction.Rows[i].Cells["dgvtxtProductionAmount"].Value = dtblProduction.Rows[i]["amount"].ToString();

                }
                DataSet dsAdditionalCost = bllAdditionalCost.StockJournalAdditionalCostForRegisteOrReport(strVoucherNo, decVoucherTypeId);
                if (dsAdditionalCost.Tables[0].Rows.Count > 0)
                {
                    DataTable dtblCashOrBank = dsAdditionalCost.Tables[0];
                    cmbCashOrBank.SelectedValue = Convert.ToDecimal(dtblCashOrBank.Rows[0]["ledgerId"]);
                }
                if (dsAdditionalCost.Tables[1].Rows.Count > 0)
                {
                    DataTable dtblIndirectExpenses = dsAdditionalCost.Tables[1];

                    int inRowIndexAdditional = 0;
                    for (int i = 0; i < dtblIndirectExpenses.Rows.Count; i++)
                    {
                        dgvAdditionalCost.Rows.Add();
                        dgvAdditionalCost.Rows[i].HeaderCell.Value = string.Empty;
                        dgvAdditionalCost.Rows[i].Cells["dgvtxtAdditionalCostSlNo"].Value = ++inRowIndexAdditional;

                        dgvAdditionalCost.Rows[i].Cells["dgvcmbAdditionalCostLedger"].Value = Convert.ToDecimal(dtblIndirectExpenses.Rows[i]["ledgerId"].ToString());
                        dgvAdditionalCost.Rows[i].Cells["dgvtxtAdditionalCostAmount"].Value = dtblIndirectExpenses.Rows[i]["debit"].ToString();
                    }
                }
                grandTotalAmountCalculationConsumption();
                grandTotalAmountCalculationProduction();
                TotalAdditionalCostAmount();
                btnDelete.Enabled = true;
                if (isAutomatic)
                {
                    txtVoucherNo.ReadOnly = true;
                }
                else
                {
                    txtVoucherNo.ReadOnly = false;
                }
                gbxTransactionType.Enabled = false;
                isFillMode = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("SJ:50" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Function to Remove Row from StockJournalAdditionalCostDetails
 /// </summary>
 public void RemoveRowStockJournalAdditionalCostDetails()
 {
     AditionalCostBll bllAdditionalCost = new AditionalCostBll();
     try
     {
         foreach (var strDetailsId in ArrlstOfAdditionalCostRemove)
         {
             decimal decDeleteId = Convert.ToDecimal(strDetailsId);
             bllAdditionalCost.AdditionalCostDelete(decDeleteId);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SJ:3" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to save purchase invoice
 /// </summary>
 public void Save()
 {
     decimal decPurchaseMasterId = 0;
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
     PurchaseDetailsInfo infoPurchaseDetails = new PurchaseDetailsInfo();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     MaterialReceiptBll bllMaterialReceiptMaster = new MaterialReceiptBll();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     PurchaseOrderBll BllPurchaseOrder = new PurchaseOrderBll();
     StockPostingInfo infoStockPosting = new StockPostingInfo();
     StockPostingBll BllStockPosting = new StockPostingBll();
     //StockPostingSP spStockPosting = new StockPostingSP();
     LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
     LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
     PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
     //PartyBalanceSP spPartyBalance = new PartyBalanceSP();
     PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
     AdditionalCostInfo infoAdditionalCost = new AdditionalCostInfo();
     AditionalCostBll bllAdditionalCost = new AditionalCostBll();
     PurchaseBillTaxInfo infoPurchaseBillTax = new PurchaseBillTaxInfo();
     AccountLedgerInfo infoAccountLedger = new AccountLedgerInfo();
     AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
     UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
     ExchangeRateBll BllExchangeRate = new ExchangeRateBll();
     try
     {
         /*-----------------------------------------Purchase Master Add----------------------------------------------------*/
         infoPurchaseMaster.AdditionalCost = Convert.ToDecimal(lblAdditionalCostAmount.Text);
         infoPurchaseMaster.BillDiscount = Convert.ToDecimal(txtBillDiscount.Text);
         infoPurchaseMaster.CreditPeriod = txtCreditPeriod.Text;
         infoPurchaseMaster.Date = Convert.ToDateTime(txtVoucherDate.Text);
         infoPurchaseMaster.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());
         infoPurchaseMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
         infoPurchaseMaster.GrandTotal = Convert.ToDecimal(txtGrandTotal.Text);
         infoPurchaseMaster.InvoiceNo = txtVoucherNo.Text.Trim();
         if (isAutomatic)
         {
             infoPurchaseMaster.SuffixPrefixId = decPurchaseInvoiceSuffixPrefixId;
             infoPurchaseMaster.VoucherNo = strVoucherNo;
         }
         else
         {
             infoPurchaseMaster.SuffixPrefixId = 0;
             infoPurchaseMaster.VoucherNo = strVoucherNo;
         }
         infoPurchaseMaster.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
         infoPurchaseMaster.LrNo = txtLRNo.Text;
         if (cmbPurchaseMode.Text == "Against MaterialReceipt")
         {
             infoPurchaseMaster.MaterialReceiptMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
         }
         else
         {
             infoPurchaseMaster.MaterialReceiptMasterId = 0;
         }
         infoPurchaseMaster.Narration = txtNarration.Text;
         infoPurchaseMaster.PurchaseAccount = Convert.ToDecimal(cmbPurchaseAccount.SelectedValue.ToString());
         if (cmbPurchaseMode.Text == "Against PurchaseOrder")
         {
             infoPurchaseMaster.PurchaseOrderMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
         }
         else
         {
             infoPurchaseMaster.PurchaseOrderMasterId = 0;
         }
         infoPurchaseMaster.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
         infoPurchaseMaster.TotalTax = Convert.ToDecimal(lblTaxAmount.Text);
         infoPurchaseMaster.TransportationCompany = txtTransportationCompany.Text;
         infoPurchaseMaster.UserId = PublicVariables._decCurrentUserId;
         infoPurchaseMaster.VendorInvoiceDate = Convert.ToDateTime(txtInvoiceDate.Text);
         infoPurchaseMaster.VendorInvoiceNo = txtVendorInvoiceNo.Text;
         infoPurchaseMaster.VoucherTypeId = decPurchaseInvoiceVoucherTypeId;
         infoPurchaseMaster.Extra1 = string.Empty;
         infoPurchaseMaster.Extra2 = string.Empty;
         infoPurchaseMaster.ExtraDate = Convert.ToDateTime(DateTime.Now);
         decPurchaseMasterId = BllPurchaseInvoice.PurchaseMasterAdd(infoPurchaseMaster);
         infoPurchaseOrderMaster = BllPurchaseOrder.PurchaseOrderMasterView(infoPurchaseMaster.PurchaseOrderMasterId);
         infoMaterialReceiptMaster = bllMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
         foreach (DataGridViewRow dgvrow in dgvProductDetails.Rows)
         {
             if (dgvrow.Cells["dgvtxtProductId"].Value != null)
             {
                 if (dgvrow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
                 {
                     /*-----------------------------------------Purchase Details Add----------------------------------------------------*/
                     infoPurchaseDetails.Amount = Convert.ToDecimal(dgvrow.Cells["dgvtxtAmount"].Value.ToString());
                     infoPurchaseDetails.BatchId = Convert.ToDecimal(dgvrow.Cells["dgvcmbBatch"].Value.ToString());
                     infoPurchaseDetails.Discount = Convert.ToDecimal(dgvrow.Cells["dgvtxtDiscount"].Value.ToString());
                     infoPurchaseDetails.GodownId = Convert.ToDecimal(dgvrow.Cells["dgvcmbGodown"].Value.ToString());
                     infoPurchaseDetails.GrossAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtGrossValue"].Value.ToString());
                     infoPurchaseDetails.NetAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtNetValue"].Value.ToString());
                     infoPurchaseDetails.OrderDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseOrderDetailsId"].Value.ToString());
                     infoPurchaseDetails.ProductId = Convert.ToDecimal(dgvrow.Cells["dgvtxtProductId"].Value.ToString());
                     infoPurchaseDetails.PurchaseMasterId = decPurchaseMasterId;
                     infoPurchaseDetails.Qty = Convert.ToDecimal(dgvrow.Cells["dgvtxtQuantity"].Value.ToString());
                     infoPurchaseDetails.RackId = Convert.ToDecimal(dgvrow.Cells["dgvcmbRack"].Value.ToString());
                     infoPurchaseDetails.Rate = Convert.ToDecimal(dgvrow.Cells["dgvtxtRate"].Value.ToString());
                     infoPurchaseDetails.ReceiptDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtMaterialReceiptDetailsId"].Value.ToString());
                     infoPurchaseDetails.SlNo = Convert.ToInt32(dgvrow.Cells["dgvtxtSlNo"].Value.ToString());
                     infoPurchaseDetails.TaxAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtTaxAmount"].Value.ToString());
                     infoPurchaseDetails.TaxId = Convert.ToDecimal(dgvrow.Cells["dgvcmbTax"].Value.ToString());
                     infoPurchaseDetails.UnitConversionId = Convert.ToDecimal(dgvrow.Cells["dgvtxtUnitConversionId"].Value.ToString());
                     infoPurchaseDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                     infoPurchaseDetails.Extra1 = string.Empty;
                     infoPurchaseDetails.Extra2 = string.Empty;
                     infoPurchaseDetails.ExtraDate = Convert.ToDateTime(DateTime.Today);
                     BllPurchaseInvoice.PurchaseDetailsAdd(infoPurchaseDetails);
                     /*-----------------------------------------Stock Posting----------------------------------------------------*/
                     infoStockPosting.BatchId = infoPurchaseDetails.BatchId;
                     infoStockPosting.Date = infoPurchaseMaster.Date;
                     infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                     infoStockPosting.GodownId = infoPurchaseDetails.GodownId;
                     infoStockPosting.InwardQty = infoPurchaseDetails.Qty; /// spUnitConvertion.UnitConversionRateByUnitConversionId(infoPurchaseDetails.UnitConversionId);
                     infoStockPosting.OutwardQty = 0;
                     infoStockPosting.ProductId = infoPurchaseDetails.ProductId;
                     infoStockPosting.RackId = infoPurchaseDetails.RackId;
                     infoStockPosting.Rate = infoPurchaseDetails.Rate;
                     infoStockPosting.UnitId = infoPurchaseDetails.UnitId;
                     if (infoPurchaseDetails.OrderDetailsId != 0)
                     {
                         infoStockPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = "NA";
                         infoStockPosting.AgainstVoucherNo = "NA";
                         infoStockPosting.AgainstVoucherTypeId = 0;
                     }
                     else if (infoPurchaseDetails.ReceiptDetailsId != 0)
                     {
                         infoStockPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = "NA";
                         infoStockPosting.AgainstVoucherNo = "NA";
                         infoStockPosting.AgainstVoucherTypeId = 0;
                     }
                     else if (infoPurchaseDetails.OrderDetailsId == 0 && infoPurchaseDetails.ReceiptDetailsId == 0)
                     {
                         infoStockPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = "NA";
                         infoStockPosting.AgainstVoucherNo = "NA";
                         infoStockPosting.AgainstVoucherTypeId = 0;
                     }
                     infoStockPosting.Extra1 = string.Empty;
                     infoStockPosting.Extra2 = string.Empty;
                     infoStockPosting.ExtraDate = Convert.ToDateTime(DateTime.Today);
                     BllStockPosting.StockPostingAdd(infoStockPosting);
                     if (infoPurchaseDetails.ReceiptDetailsId != 0)
                     {
                         infoStockPosting.InvoiceNo = infoMaterialReceiptMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoMaterialReceiptMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoMaterialReceiptMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.AgainstVoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.AgainstVoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.InwardQty = 0;
                         infoStockPosting.OutwardQty = infoPurchaseDetails.Qty;/// spUnitConvertion.UnitConversionRateByUnitConversionId(infoPurchaseDetails.UnitConversionId);
                         BllStockPosting.StockPostingAdd(infoStockPosting);
                     }
                 }
             }
         }
         /*-----------------------------------------Ledger Posting----------------------------------------------------*/
         infoLedgerPosting.Credit = Convert.ToDecimal(txtGrandTotal.Text) * BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
         infoLedgerPosting.Debit = 0;
         infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
         infoLedgerPosting.DetailsId = 0;
         infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
         infoLedgerPosting.LedgerId = infoPurchaseMaster.LedgerId;
         infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
         infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
         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);
         decimal decBilldiscount = Convert.ToDecimal(txtBillDiscount.Text.ToString());
         if (decBilldiscount > 0)
         {
             infoLedgerPosting.Credit = decBilldiscount * BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
             infoLedgerPosting.Debit = 0;
             infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
             infoLedgerPosting.DetailsId = 0;
             infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
             infoLedgerPosting.LedgerId = 9;//ledger id of discount received
             infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
             infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
             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);
         }
         infoLedgerPosting.Credit = 0;
         infoLedgerPosting.Debit = TotalNetAmount(); //* spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
         infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
         infoLedgerPosting.DetailsId = 0;
         infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
         infoLedgerPosting.LedgerId = infoPurchaseMaster.PurchaseAccount;//ledger posting of purchase account
         infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
         infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
         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);
         foreach (DataGridViewRow dgvrow in dgvAdditionalCost.Rows)
         {
             if (dgvrow.Cells["dgvcmbLedger"].Value != null)
             {
                 if (dgvrow.Cells["dgvcmbLedger"].Value.ToString() != string.Empty)
                 {
                     if (dgvrow.Cells["dgvtxtAdditionalCostAmount"].Value != null)
                     {
                         if (dgvrow.Cells["dgvtxtAdditionalCostAmount"].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["dgvcmbLedger"].Value.ToString());
                             infoAdditionalCost.VoucherNo = infoPurchaseMaster.VoucherNo;
                             infoAdditionalCost.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                             infoAdditionalCost.Extra1 = string.Empty;
                             infoAdditionalCost.Extra2 = string.Empty;
                             infoAdditionalCost.ExtraDate = DateTime.Now;
                             bllAdditionalCost.AdditionalCostAdd(infoAdditionalCost);
                             /*-----------------------------------------Additional Cost Ledger Posting----------------------------------------------------*/
                             infoLedgerPosting.Credit = 0;
                             infoLedgerPosting.Debit = infoAdditionalCost.Debit * BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                             infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
                             infoLedgerPosting.DetailsId = 0;
                             infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                             infoLedgerPosting.LedgerId = infoAdditionalCost.LedgerId;
                             infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                             infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                             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 (dgvTax.Visible)
         {
             foreach (DataGridViewRow dgvrow in dgvTax.Rows)
             {
                 if (dgvrow.Cells["dgvtxtTaxId"].Value != null)
                 {
                     if (dgvrow.Cells["dgvtxtTaxId"].Value.ToString() != string.Empty)
                     {
                         /*-----------------------------------------PurchaseBillTax Add----------------------------------------------------*/
                         infoPurchaseBillTax.PurchaseMasterId = decPurchaseMasterId;
                         infoPurchaseBillTax.TaxAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtTotalTax"].Value.ToString());
                         infoPurchaseBillTax.TaxId = Convert.ToDecimal(dgvrow.Cells["dgvtxtTaxId"].Value.ToString());
                         infoPurchaseBillTax.Extra1 = string.Empty;
                         infoPurchaseBillTax.Extra2 = string.Empty;
                         infoPurchaseBillTax.ExtraDate = DateTime.Now;
                         BllPurchaseInvoice.PurchaseBillTaxAdd(infoPurchaseBillTax);
                         /*-----------------------------------------Tax Ledger Posting----------------------------------------------------*/
                         infoLedgerPosting.Credit = 0;
                         infoLedgerPosting.Debit = infoPurchaseBillTax.TaxAmount * BllExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                         infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
                         infoLedgerPosting.DetailsId = 0;
                         infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoLedgerPosting.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvtxtLedgerId"].Value.ToString());
                         infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         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);
                     }
                 }
             }
         }
         /*-----------------------------------------PartyBalance Posting----------------------------------------------------*/
         infoAccountLedger = bllAccountLedger.AccountLedgerView(infoPurchaseMaster.LedgerId);
         if (infoAccountLedger.BillByBill == true)
         {
             infoPartyBalance.Credit = Convert.ToDecimal(txtGrandTotal.Text);
             infoPartyBalance.Debit = 0;
             if (txtCreditPeriod.Text != string.Empty)
             {
                 infoPartyBalance.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text);
             }
             infoPartyBalance.Date = Convert.ToDateTime(txtVoucherDate.Text);
             infoPartyBalance.ExchangeRateId = infoPurchaseMaster.ExchangeRateId;
             infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
             infoPartyBalance.LedgerId = infoPurchaseMaster.LedgerId;
             infoPartyBalance.ReferenceType = "NEW";
             infoPartyBalance.InvoiceNo = infoPurchaseMaster.InvoiceNo;
             infoPartyBalance.VoucherNo = infoPurchaseMaster.VoucherNo;
             infoPartyBalance.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
             infoPartyBalance.AgainstInvoiceNo = "NA";
             infoPartyBalance.AgainstVoucherNo = "NA";
             infoPartyBalance.AgainstVoucherTypeId = 0;
             infoPartyBalance.Extra1 = string.Empty;
             infoPartyBalance.Extra2 = string.Empty;
             infoPartyBalance.ExtraDate = DateTime.Now;
             BllPartyBalance.PartyBalanceAdd(infoPartyBalance);
         }
         Messages.SavedMessage();
         if (cbxPrintAfterSave.Checked)
         {
             if (BllSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
             {
                 PrintForDotMatrix(decPurchaseMasterId);
             }
             else
             {
                 Print(decPurchaseMasterId);
             }
         }
         Clear();
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI38:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// deleting the rows from tbl_PurchaseDetails removed by user from the grid while updating after reference check
 /// </summary>
 public void RemoveDelete()
 {
     PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
     AditionalCostBll bllAdditionalCost = new AditionalCostBll();
     decimal decPurchaseDetailsId = 0;
     decimal decAdditionalCostId = 0;
     int inRef = 0;
     try
     {
         foreach (var item in arrlstRemove)
         {
             decPurchaseDetailsId = Convert.ToDecimal(item);
             inRef = BllPurchaseInvoice.PurchaseMasterReferenceCheck(decPurchaseMasterId, decPurchaseDetailsId);
             if (inRef == 0)
             {
                 BllPurchaseInvoice.PurchaseDetailsDelete(decPurchaseDetailsId);
             }
         }
         foreach (var item1 in arrlstRemoveAdditionalCost)
         {
             decAdditionalCostId = Convert.ToDecimal(item1);
             bllAdditionalCost.AdditionalCostDelete(decAdditionalCostId);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI40:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to fill additional cost grid
 /// </summary>
 public void AdditionalCostGridFill()
 {
     List<DataTable> ListObjforaccountLedger = new List<DataTable>();
     PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
     AditionalCostBll bllAdditionalCost = new AditionalCostBll();
     try
     {
         ListObjforaccountLedger = BllPurchaseInvoice.AccountLedgerViewForAdditionalCost();
         DataRow dr = ListObjforaccountLedger[0].NewRow();
         dr["ledgerName"] = string.Empty;
         dr["ledgerId"] = 0;
         ListObjforaccountLedger[0].Rows.InsertAt(dr, 0);
         dgvcmbLedger.DataSource = ListObjforaccountLedger[0];
         dgvcmbLedger.DisplayMember = "ledgerName";
         dgvcmbLedger.ValueMember = "ledgerId";
         if (decPurchaseMasterId == 0)
         {
             AdditionalCostComboFill(0);
         }
         else
         {
             List<DataTable> ListObj = new List<DataTable>();
             ListObj = bllAdditionalCost.AdditionalCostViewAllByVoucherTypeIdAndVoucherNo(decPurchaseInvoiceVoucherTypeId, strVoucherNo);
             dgvAdditionalCost.DataSource = ListObj[0];
         }
         AdditionalCostSerialNo();
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI26:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 public void Delete()
 {
     PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
     AditionalCostBll bllAdditionalCost = new AditionalCostBll();
     AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
     StockPostingBll BllStockPosting = new StockPostingBll();
     // StockPostingSP spStockPosting = new StockPostingSP();
     LedgerPostingBll BllLedgerPosting = new LedgerPostingBll();
     PurchaseOrderBll BllPurchaseOrder = new PurchaseOrderBll();
     MaterialReceiptBll bllMaterialReceiptMaster = new MaterialReceiptBll();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     /*---------------------------------Deleting previous stock posting, Ledger posting, partybalanceposting---------------------------------------*/
     infoPurchaseMaster = BllPurchaseInvoice.PurchaseMasterView(decPurchaseMasterId);
     if (infoPurchaseMaster.MaterialReceiptMasterId != 0)
     {
         infoMaterialReceiptMaster = bllMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
         BllStockPosting.StockPostingDeleteForSalesInvoiceAgainstDeliveryNote
             (infoPurchaseMaster.VoucherTypeId, infoPurchaseMaster.VoucherNo,
             infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
     }
     BllStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
             (0, "NA", infoPurchaseMaster.VoucherNo, infoPurchaseMaster.VoucherTypeId);
     //-------------------------------------------------
     decimal decPurchaseDetailsId = 0;
     decimal decPurchaseOrderMasterId = 0;
     decimal decMaterialReceiptMasterId = 0;
     decimal decAdditionalCostId = 0;
     decimal decPurchaseBillTaxId = 0;
     int inRef = 0;
     bool isRef = false;
     try
     {
         foreach (DataGridViewRow dgvrow in dgvProductDetails.Rows)
         {
             if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value != null)
             {
                 if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() != string.Empty && dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() != "0")
                 {
                     decPurchaseDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString());
                     inRef = BllPurchaseInvoice.PurchaseMasterReferenceCheck(decPurchaseMasterId, decPurchaseDetailsId);
                     if (inRef > 0 && !isRef)
                     {
                         isRef = true;
                     }
                 }
             }
         }
         if (!isRef)
         {
             isRef = bllAccountLedger.PartyBalanceAgainstReferenceCheck(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
             if (!isRef)
             {
                 BllPurchaseInvoice.PurchaseMasterDelete(decPurchaseMasterId);
                 BllPurchaseInvoice.PurchaseDetailsDeleteByPurchaseMasterId(decPurchaseMasterId);
                 foreach (DataGridViewRow dgvrow in dgvAdditionalCost.Rows)
                 {
                     if (dgvrow.Cells["dgvtxtAdditionalCostId"].Value != null)
                     {
                         if (dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString() != string.Empty &&
                             dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString() != "0")
                         {
                             decAdditionalCostId = Convert.ToDecimal(dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString());
                             bllAdditionalCost.AdditionalCostDelete(decAdditionalCostId);
                         }
                     }
                 }
                 if (dgvTax.Visible)
                 {
                     foreach (DataGridViewRow dgvrow in dgvTax.Rows)
                     {
                         if (dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value != null)
                         {
                             if (dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString() != string.Empty &&
                                 dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString() != "0")
                             {
                                 decPurchaseBillTaxId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString());
                                 BllPurchaseInvoice.PurchaseBillTaxDelete(decPurchaseBillTaxId);
                             }
                         }
                     }
                 }
                 if (cmbPurchaseMode.Text == "Against PurchaseOrder")
                 {
                     decPurchaseOrderMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                     infoPurchaseOrderMaster = BllPurchaseOrder.PurchaseOrderMasterView(decPurchaseOrderMasterId);
                 }
                 else if (cmbPurchaseMode.Text == "Against MaterialReceipt")
                 {
                     decMaterialReceiptMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                     infoMaterialReceiptMaster = bllMaterialReceiptMaster.MaterialReceiptMasterView(decMaterialReceiptMasterId);
                 }
                 BllLedgerPosting.LedgerPostDelete(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 if (infoPurchaseOrderMaster.PurchaseOrderMasterId != 0)
                 {
                     BllStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (decPurchaseInvoiceVoucherTypeId, strVoucherNo,
                         infoPurchaseOrderMaster.VoucherNo, infoPurchaseOrderMaster.VoucherTypeId);
                 }
                 else if (infoMaterialReceiptMaster.MaterialReceiptMasterId != 0)
                 {
                     BllStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (decPurchaseInvoiceVoucherTypeId, strVoucherNo,
                         infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
                 }
                 BllStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                           (0, "NA", strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 bllAccountLedger.PartyBalanceDeleteByVoucherTypeVoucherNoAndReferenceType(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 Messages.DeletedMessage();
                 Clear();
                 this.Close();
             }
             else
             {
                 Messages.InformationMessage("Cannot delete purchase invoice because there is a payment voucher against this invoice");
             }
         }
         else
         {
             Messages.InformationMessage("Cannot delete purchase invoice because reference exists");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI41:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }