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);
     }
 }