public int PartyBalanceCheckWithReference()
 {
     int inF1 = 0;
     decimal decPartyBalanceAmount = 0;
     decimal decGrandTotal = 0;
     try
     {
         bool isRef = false;
         AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
         PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
         isRef = bllAccountLedger.PartyBalanceAgainstReferenceCheck(strVoucherNo, DecSalesInvoiceVoucherTypeId);
         if (isRef)
         {
             decPartyBalanceAmount = BllPartyBalance.PartyBalanceAmountViewForSalesInvoice(strVoucherNo, DecSalesInvoiceVoucherTypeId, "Against");
             decGrandTotal = Convert.ToDecimal(txtGrandTotal.Text);
             if (decGrandTotal >= decPartyBalanceAmount)
             {
                 inF1 = 1;
             }
             else
             {
                 inF1 = 0;
                 Messages.InformationMessage("There is a Receipt voucher against this invoice so grand total should not be less than " + decPartyBalanceAmount);
             }
         }
         else
         {
             inF1 = 1;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SI36:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     return inF1;
 }
 /// <summary>
 /// Fill function from coming from register or report or other forms
 /// </summary>
 public void FillRegisterOrReport()
 {
     //SalesMasterSP spSalesMaster = new SalesMasterSP();
     //SalesDetailsSP spSalesDetails = new SalesDetailsSP();
     VoucherTypeBll BllVoucherType = new VoucherTypeBll();
     //SalesBillTaxSP spSalesBillTax = new SalesBillTaxSP();
     SalesInvoiceBll BllSalesInvoice = new SalesInvoiceBll();
     try
     {
         isFromEditMode = true;
         btnSave.Text = "Update";
         btnDelete.Enabled = true;
         txtInvoiceNo.ReadOnly = true;
         List<DataTable> listMaster = BllSalesInvoice.SalesInvoiceSalesMasterViewBySalesMasterId(decSalesInvoiceIdToEdit);
         DecSalesInvoiceVoucherTypeId = Convert.ToDecimal(listMaster[0].Rows[0]["voucherTypeId"].ToString());
         VoucherTypeInfo infoVoucherType = new VoucherTypeInfo();
         infoVoucherType = BllVoucherType.VoucherTypeView(DecSalesInvoiceVoucherTypeId);
         this.Text = infoVoucherType.VoucherTypeName;
         txtDate.Text = listMaster[0].Rows[0]["date"].ToString();
         dtpDate.Value = DateTime.Parse(txtDate.Text);
         CurrencyComboFill();
         txtInvoiceNo.Text = listMaster[0].Rows[0]["invoiceNo"].ToString();
         txtCreditPeriod.Text = listMaster[0].Rows[0]["creditPeriod"].ToString();
         strVoucherNo = listMaster[0].Rows[0]["voucherNo"].ToString();
         decSalseInvoiceSuffixPrefixId = Convert.ToDecimal(listMaster[0].Rows[0]["suffixPrefixId"].ToString());
         isAutomatic = BllVoucherType.CheckMethodOfVoucherNumbering(DecSalesInvoiceVoucherTypeId);
         cmbCashOrParty.SelectedValue = listMaster[0].Rows[0]["ledgerId"].ToString();
         cmbSalesAccount.SelectedValue = listMaster[0].Rows[0]["salesAccount"].ToString();
         cmbSalesMan.SelectedValue = listMaster[0].Rows[0]["employeeId"].ToString();
         txtCustomer.Text = listMaster[0].Rows[0]["customerName"].ToString();
         txtTransportCompany.Text = listMaster[0].Rows[0]["transportationCompany"].ToString();
         txtVehicleNo.Text = listMaster[0].Rows[0]["lrNo"].ToString();
         txtNarration.Text = listMaster[0].Rows[0]["narration"].ToString();
         cmbCurrency.SelectedValue = Convert.ToDecimal(listMaster[0].Rows[0]["exchangeRateId"].ToString());
         txtTotalAmount.Text = listMaster[0].Rows[0]["totalAmount"].ToString();
         lblTaxTotalAmount.Text = listMaster[0].Rows[0]["taxAmount"].ToString();
         cmbPricingLevel.SelectedValue = Convert.ToDecimal(listMaster[0].Rows[0]["pricingLevelId"].ToString());
         if (listMaster[0].Rows[0]["quotationMasterId"].ToString() != "0")
         {
             cmbSalesMode.Text = "Against Quotation";
             againstOrderComboFill();
             cmbSalesModeOrderNo.SelectedValue = listMaster[0].Rows[0]["quotationMasterId"].ToString();
             lblSalesModeOrderNo.Text = "Quotation No";
             cmbCurrency.Enabled = false;
             cmbPricingLevel.Enabled = false;
         }
         else if (listMaster[0].Rows[0]["orderMasterId"].ToString() != "0")
         {
             cmbSalesMode.Text = "Against SalesOrder";
             againstOrderComboFill();
             cmbSalesModeOrderNo.SelectedValue = listMaster[0].Rows[0]["orderMasterId"].ToString();
             lblSalesModeOrderNo.Text = "Order No";
             cmbCurrency.Enabled = false;
             cmbPricingLevel.Enabled = false;
         }
         else if (listMaster[0].Rows[0]["deliveryNoteMasterId"].ToString() != "0")
         {
             cmbSalesMode.Text = "Against Delivery Note";
             againstOrderComboFill();
             cmbSalesModeOrderNo.SelectedValue = listMaster[0].Rows[0]["deliveryNoteMasterId"].ToString();
             lblSalesModeOrderNo.Text = "Delivery Note No";
             cmbCurrency.Enabled = false;
             cmbPricingLevel.Enabled = false;
         }
         else
         {
             cmbSalesMode.SelectedText = "NA";
         }
         if (txtInvoiceNo.Enabled)
         {
             txtDate.Focus();
         }
         else
         {
             txtInvoiceNo.Focus();
         }
         //DataTable dtblDetails = new DataTable();
         List<DataTable> listObj = new List<DataTable>();
         listObj = BllSalesInvoice.SalesInvoiceSalesDetailsViewBySalesMasterId(decSalesInvoiceIdToEdit);
         dgvSalesInvoiceTaxComboFill();
         dgvSalesInvoice.Rows.Clear();
         for (int i = 0; i < listObj[0].Rows.Count; i++)
         {
             dgvSalesInvoice.Rows.Add();
             IsSetGridValueChange = false;
             dgvSalesInvoice.Rows[i].HeaderCell.Value = string.Empty;
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceSalesDetailsId"].Value = Convert.ToDecimal(listObj[0].Rows[i]["salesDetailsId"].ToString());
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceSlno"].Value = listObj[0].Rows[i]["slNo"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceBarcode"].Value = listObj[0].Rows[i]["barcode"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceProductCode"].Value = listObj[0].Rows[i]["productCode"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceProductName"].Value = listObj[0].Rows[i]["productName"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceProductId"].Value = listObj[0].Rows[i]["productId"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceBrand"].Value = listObj[0].Rows[i]["brandName"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceQty"].Value = listObj[0].Rows[i]["qty"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoicePurchaseRate"].Value = listObj[0].Rows[i]["purchaseRate"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceAmount"].Value = listObj[0].Rows[i]["amount"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceGrossValue"].Value = listObj[0].Rows[i]["grossAmount"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoicembUnitName"].Value = Convert.ToDecimal(listObj[0].Rows[i]["unitId"].ToString());
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceRate"].Value = listObj[0].Rows[i]["rate"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceGodown"].Value = Convert.ToDecimal(listObj[0].Rows[i]["godownId"].ToString());
             RackComboFill(Convert.ToDecimal(dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceGodown"].Value.ToString()), i, dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceRack"].ColumnIndex);
             dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceRack"].Value = Convert.ToDecimal(listObj[0].Rows[i]["rackId"].ToString());
             if (listObj[0].Rows[i]["batchId"] != null && listObj[0].Rows[i]["batchId"].ToString() != string.Empty)
             {
                 dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceBatch"].Value = Convert.ToDecimal(listObj[0].Rows[i]["batchId"].ToString());
             }
             else
             {
                 dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceBatch"].Value = string.Empty;
             }
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceMrp"].Value = listObj[0].Rows[i]["mrp"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceSalesRate"].Value = listObj[0].Rows[i]["salesRate"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceDiscountAmount"].Value = listObj[0].Rows[i]["discount"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceNetAmount"].Value = listObj[0].Rows[i]["netAmount"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceTaxName"].Value = Convert.ToDecimal(listObj[0].Rows[i]["taxId"].ToString());
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceTaxAmount"].Value = listObj[0].Rows[i]["taxAmount"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceUnitConversionId"].Value = listObj[0].Rows[i]["unitConversionId"].ToString();
             lblTotalQuantitydisplay.Text = listObj[0].Rows[i]["qty"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSISalesOrderDetailsId"].Value = Convert.ToDecimal(listObj[0].Rows[i]["orderDetailsId"].ToString());  // here get fill the grid colum for the editing prps
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value = listObj[0].Rows[i]["deliveryNoteDetailsId"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceQuotationDetailsId"].Value = listObj[0].Rows[i]["quotationDetailsId"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceVoucherTypeId"].Value = Convert.ToDecimal(listObj[0].Rows[i]["voucherTypeRefNo"].ToString());  // here get fill the grid colum for the editing prps
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceVoucherNo"].Value = listObj[0].Rows[i]["voucherRefNo"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceInvoiceNo"].Value = listObj[0].Rows[i]["invoiceRefNo"].ToString();
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceBarcode"].ReadOnly = true;
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceProductCode"].ReadOnly = true;
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceProductName"].ReadOnly = true;
             dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceBrand"].ReadOnly = true;
             dgvSalesInvoice.Rows[i].Cells["dgvcmbSalesInvoiceBatch"].ReadOnly = true;
             if (cmbSalesMode.SelectedIndex != 0)
             {
                 strVoucherNoTostockPost = dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceVoucherNo"].Value.ToString();
                 strInvoiceNoTostockPost = dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceInvoiceNo"].Value.ToString();
                 decVouchertypeIdTostockPost = Convert.ToDecimal(dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceVoucherTypeId"].Value);
             }
             GrossValueCalculation(i);
             DiscountCalculation(i, dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceDiscountAmount"].ColumnIndex);
             taxAndGridTotalAmountCalculation(i);
             decCurrentRate = Convert.ToDecimal(dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
             decCurrentConversionRate = Convert.ToDecimal(listObj[0].Rows[i]["conversionRate"].ToString());
             UnitConversionCalc(i);
             if (cmbSalesModeOrderNo.Visible == true)
             {
                 dgvSalesInvoice.Rows[i].Cells["dgvtxtSalesInvoicembUnitName"].ReadOnly = true;
             }
         }
         List<DataTable> listAdditionalCost = new List<DataTable>();
         listAdditionalCost = BllSalesInvoice.SalesInvoiceAdditionalCostViewByVoucherNoUnderVoucherType(DecSalesInvoiceVoucherTypeId, strVoucherNo);
         for (int i = 0; i < listAdditionalCost[0].Rows.Count; i++)
         {
             dgvSalesInvoiceLedger.Rows.Add();
             dgvSalesInvoiceLedger.Rows[i].Cells["dgvtxtAdditionalCostId"].Value = listAdditionalCost[0].Rows[i]["additionalCostId"].ToString();
             dgvSalesInvoiceLedger.Rows[i].Cells["dgvCmbAdditionalCostledgerName"].Value = Convert.ToDecimal(listAdditionalCost[0].Rows[i]["ledgerId"].ToString());
             dgvSalesInvoiceLedger.Rows[i].Cells["dgvtxtAdditionalCoastledgerAmount"].Value = listAdditionalCost[0].Rows[i]["amount"].ToString();
             List<DataTable> ListObj = new List<DataTable>();
             AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
             ListObj = bllAccountLedger.AccountLedgerViewForAdditionalCost();
             DataGridViewComboBoxCell dgvccVoucherType = (DataGridViewComboBoxCell)dgvSalesInvoiceLedger[dgvSalesInvoiceLedger.Columns["dgvCmbAdditionalCostledgerName"].Index, i];
             dgvccVoucherType.DataSource = ListObj[0];
             dgvccVoucherType.ValueMember = "ledgerId";
             dgvccVoucherType.DisplayMember = "ledgerName";
         }
         List<DataTable> ListDrOrCr = BllSalesInvoice.salesinvoiceAdditionalCostCheckdrOrCrforSiEdit(DecSalesInvoiceVoucherTypeId, strVoucherNo);
         if (ListDrOrCr[0].Rows.Count > 0)
         {
             if (Convert.ToDecimal(ListDrOrCr[0].Rows[0]["credit"].ToString()) != 0)
             {
                 cmbCashOrbank.SelectedValue = Convert.ToDecimal(ListDrOrCr[0].Rows[0]["ledgerId"].ToString());
                 cmbCashOrbank.Visible = true;
                 lblcashOrBank.Visible = true;
                 cmbDrorCr.SelectedIndex = 0;
                 decBankOrCashIdForEdit = Convert.ToDecimal(cmbCashOrbank.SelectedValue.ToString());
             }
             else
             {
                 cmbDrorCr.SelectedIndex = 1;
             }
         }
         taxGridFill();
         List<DataTable> listTax = new List<DataTable>();
         listTax = BllSalesInvoice.SalesInvoiceSalesBillTaxViewAllBySalesMasterId(decSalesInvoiceIdToEdit);
         foreach (DataGridViewRow dgvrowTax in dgvSalesInvoiceTax.Rows)
         {
             for (int ini = 0; ini < listTax[0].Rows.Count; ini++)
             {
                 if (dgvrowTax.Cells["dgvtxtTtaxId"].Value != null && dgvrowTax.Cells["dgvtxtTtaxId"].Value.ToString() != string.Empty)
                 {
                     decimal decId = Convert.ToDecimal(listTax[0].Rows[ini]["taxId"].ToString());
                     if (dgvrowTax.Cells["dgvtxtTtaxId"].Value.ToString() == decId.ToString())
                     {
                         dgvrowTax.Cells["dgvtxtTtaxAmount"].Value = listTax[0].Rows[ini]["taxAmount"].ToString();
                         break;
                     }
                     else
                     {
                         dgvrowTax.Cells["dgvtxtTtaxAmount"].Value = "0.00";
                     }
                 }
             }
         }
         LedgerGridTotalAmountCalculation();
         SiGridTotalAmountCalculation();
         txtGrandTotal.Text = listMaster[0].Rows[0]["grandTotal"].ToString();
         txtBillDiscount.Text = listMaster[0].Rows[0]["billDiscount"].ToString();
         bool isPartyBalanceRef = false;
         AccountLedgerBll bllAccountLedger1 = new AccountLedgerBll();
         PartyBalanceBll BllPartyBalance = new PartyBalanceBll();
         isPartyBalanceRef = bllAccountLedger1.PartyBalanceAgainstReferenceCheck(strVoucherNo, DecSalesInvoiceVoucherTypeId);
         if (isPartyBalanceRef)
         {
             cmbCashOrParty.Enabled = false;
         }
         else
         {
             cmbCashOrParty.Enabled = true;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SI: 77" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to fill controls to update
 /// </summary>
 public void FillControls()
 {
     try
     {
         AccountLedgerInfo infoAccountledger = new AccountLedgerInfo();
         AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
         infoAccountledger = bllAccountLedger.AccountLedgerViewForCustomer(decLedgerIdForEdit);
         txtCustomerName.Text = infoAccountledger.LedgerName;
         txtMailingName.Text = infoAccountledger.MailingName;
         txtOpeningBalance.Text = infoAccountledger.OpeningBalance.ToString();
         cmbDrorCr.Text = infoAccountledger.CrOrDr.ToString();
         txtAccountNo.Text = infoAccountledger.BankAccountNumber;
         txtBranchName.Text = infoAccountledger.BranchName;
         txtBranchCode.Text = infoAccountledger.BranchCode;
         txtMobile.Text = infoAccountledger.Mobile.ToString();
         txtphone.Text = infoAccountledger.Phone.ToString();
         txtAddress.Text = infoAccountledger.Address;
         txtEmail.Text = infoAccountledger.Email;
         cmbPricingLevel.SelectedValue = infoAccountledger.PricinglevelId.ToString();
         if (infoAccountledger.BillByBill)
         {
             cmbBillbyBill.Text = "Yes";
         }
         else
         {
             cmbBillbyBill.Text = "No";
         }
         if (bllAccountLedger.PartyBalanceAgainstReferenceCheck(decLedgerIdForEdit.ToString(), 1))
         {
             txtOpeningBalance.Enabled = false;
             cmbDrorCr.Enabled = false;
             cmbBillbyBill.Enabled = false;
         }
         txtCreditPeriod.Text = infoAccountledger.CreditPeriod.ToString();
         txtCreditLimit.Text = infoAccountledger.CreditLimit.ToString();
         txtTin.Text = infoAccountledger.Tin;
         txtPan.Text = infoAccountledger.Pan;
         txtCST.Text = infoAccountledger.Cst;
         cmbArea.SelectedValue = infoAccountledger.AreaId.ToString();
         cmbRoute.SelectedValue = infoAccountledger.RouteId.ToString();
         txtNarration.Text = infoAccountledger.Narration;
         decLedger = infoAccountledger.LedgerId;
     }
     catch (Exception ex)
     {
         MessageBox.Show("Cus11:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 ///Function to fill data in to controls when click on datagridview
 /// </summary>        
 public void FillControls()
 {
     try
     {
         AccountLedgerInfo infoAccountLedger = new AccountLedgerInfo();
         AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
         infoAccountLedger = bllAccountLedger.AccountLedgerViewForEdit(decAccountLedgerId);
         txtAccountNo.Text = infoAccountLedger.BankAccountNumber;
         txtAcNo.Text = infoAccountLedger.BankAccountNumber;
         txtAddress.Text = infoAccountLedger.Address;
         txtBranchName.Text = infoAccountLedger.BranchName;
         txtBranchCode.Text = infoAccountLedger.BranchCode;
         txtCst.Text = infoAccountLedger.Cst;
         txtEmail.Text = infoAccountLedger.Email;
         txtLedgerName.Text = infoAccountLedger.LedgerName;
         txtMailingName.Text = infoAccountLedger.MailingName;
         txtMobile.Text = infoAccountLedger.Mobile;
         txtNarration.Text = infoAccountLedger.Narration;
         txtPan.Text = infoAccountLedger.Pan;
         txtPhone.Text = infoAccountLedger.Phone;
         txtTin.Text = infoAccountLedger.Tin;
         txtCreditLimit.Text = infoAccountLedger.CreditLimit.ToString();
         txtCreditPeriod.Text = infoAccountLedger.CreditPeriod.ToString();
         AreaComboFill();
         cmbArea.SelectedValue = infoAccountLedger.AreaId.ToString();
         if (infoAccountLedger.BillByBill)
         {
             cmbBillByBill.Text = "Yes";
         }
         else
         {
             cmbBillByBill.Text = "No";
         }
         cmbGroup.SelectedValue = infoAccountLedger.AccountGroupId.ToString();
         PrlicingLevelComboFill();
         cmbPricingLevel.SelectedValue = infoAccountLedger.PricinglevelId;
         cmbRoute.SelectedValue = infoAccountLedger.RouteId.ToString();
         decimal decBalance = infoAccountLedger.OpeningBalance;
         txtOpeningBalance.Text = decBalance.ToString();
         cmbOpeningBalanceCrOrDr.Text = infoAccountLedger.CrOrDr.ToString();
         if (infoAccountLedger.IsDefault == true)
         {
             cmbGroup.Enabled = false;
             txtLedgerName.ReadOnly = true;
             btnAccountGroupAdd.Enabled = false;
             txtLedgerName.BackColor = Color.WhiteSmoke;
         }
         else if (infoAccountLedger.IsDefault == false && decBalance > 0)
         {
             cmbGroup.Enabled = false;
             btnAccountGroupAdd.Enabled = false;
             txtLedgerName.ReadOnly = false;
             txtLedgerName.BackColor = Color.White;
         }
         else
         {
             cmbGroup.Enabled = true;
             txtLedgerName.ReadOnly = false;
             txtLedgerName.BackColor = Color.White;
             btnAccountGroupAdd.Enabled = true;
         }
         if (bllAccountLedger.PartyBalanceAgainstReferenceCheck(decAccountLedgerId.ToString(), 1))
         {
             cmbGroup.Enabled = false;
             txtLedgerName.ReadOnly = false;
             txtOpeningBalance.Enabled = false;
             cmbOpeningBalanceCrOrDr.Enabled = false;
             cmbBillByBill.Enabled = false;
             txtLedgerName.BackColor = Color.White;
         }
         decLedger = infoAccountLedger.LedgerId;
         isDefault = infoAccountLedger.IsDefault;
         tbctrlLedger.SelectedTab = tbMainDetails;
     }
     catch (Exception ex)
     {
         MessageBox.Show("AL26:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to fill the details while calling from register or report
 /// </summary>
 public void FillRegisterOrReport()
 {
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     PurchaseOrderBll BllPurchaseOrder = new PurchaseOrderBll();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     MaterialReceiptBll bllMaterialReceiptMaster = new MaterialReceiptBll();
     VoucherTypeBll BllVoucherType = new VoucherTypeBll();
     VoucherTypeInfo infoVoucherType = new VoucherTypeInfo();
     AccountLedgerBll bllAccountLedger = new AccountLedgerBll();
     bool isPartyBalanceRef = false;
     try
     {
         isEditFill = true;
         btnSave.Text = "Update";
         btnDelete.Enabled = true;
         txtVoucherNo.ReadOnly = true;
         infoPurchaseMaster = BllPurchaseInvoice.PurchaseMasterView(decPurchaseMasterId);
         strVoucherNo = infoPurchaseMaster.VoucherNo;
         decPurchaseInvoiceVoucherTypeId = infoPurchaseMaster.VoucherTypeId;
         decPurchaseInvoiceSuffixPrefixId = infoPurchaseMaster.SuffixPrefixId;
         isAutomatic = BllVoucherType.CheckMethodOfVoucherNumbering(decPurchaseInvoiceVoucherTypeId);
         infoVoucherType = BllVoucherType.VoucherTypeView(decPurchaseInvoiceVoucherTypeId);
         this.Text = infoVoucherType.VoucherTypeName;
         if (isAutomatic)
         {
             txtVoucherDate.Focus();
         }
         else
         {
             txtVoucherNo.Focus();
         }
         txtVoucherNo.Text = infoPurchaseMaster.InvoiceNo;
         txtVendorInvoiceNo.Text = infoPurchaseMaster.VendorInvoiceNo;
         dtpVoucherDate.Value = infoPurchaseMaster.Date;
         dtpInvoiceDate.Value = infoPurchaseMaster.VendorInvoiceDate;
         cmbCashOrParty.SelectedValue = infoPurchaseMaster.LedgerId;
         if (infoPurchaseMaster.PurchaseOrderMasterId == 0 && infoPurchaseMaster.MaterialReceiptMasterId == 0)
         {
             cmbPurchaseMode.SelectedItem = "NA";
         }
         else if (infoPurchaseMaster.PurchaseOrderMasterId != 0 && infoPurchaseMaster.MaterialReceiptMasterId == 0)
         {
             cmbPurchaseMode.SelectedItem = "Against PurchaseOrder";
             infoPurchaseOrderMaster = BllPurchaseOrder.PurchaseOrderMasterView(infoPurchaseMaster.PurchaseOrderMasterId);
             cmbVoucherType.SelectedValue = infoPurchaseOrderMaster.VoucherTypeId;
             OrderComboFill();
             cmbOrderNo.SelectedValue = infoPurchaseMaster.PurchaseOrderMasterId;
         }
         else if (infoPurchaseMaster.PurchaseOrderMasterId == 0 && infoPurchaseMaster.MaterialReceiptMasterId != 0)
         {
             cmbPurchaseMode.SelectedItem = "Against MaterialReceipt";
             infoMaterialReceiptMaster = bllMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
             cmbVoucherType.SelectedValue = infoMaterialReceiptMaster.VoucherTypeId;
             OrderComboFill();
             cmbOrderNo.SelectedValue = infoPurchaseMaster.MaterialReceiptMasterId;
         }
         cmbPurchaseAccount.SelectedValue = infoPurchaseMaster.PurchaseAccount;
         txtCreditPeriod.Text = infoPurchaseMaster.CreditPeriod;
         cmbCurrency.SelectedValue = infoPurchaseMaster.ExchangeRateId;
         txtNarration.Text = infoPurchaseMaster.Narration;
         lblAdditionalCostAmount.Text = Math.Round(infoPurchaseMaster.AdditionalCost, PublicVariables._inNoOfDecimalPlaces).ToString();
         lblTaxAmount.Text = Math.Round(infoPurchaseMaster.TotalTax, PublicVariables._inNoOfDecimalPlaces).ToString();
         txtTotalAmount.Text = Math.Round(infoPurchaseMaster.TotalAmount, PublicVariables._inNoOfDecimalPlaces).ToString();
         txtBillDiscount.Text = Math.Round(infoPurchaseMaster.BillDiscount, PublicVariables._inNoOfDecimalPlaces).ToString();
         txtLRNo.Text = infoPurchaseMaster.LrNo;
         txtTransportationCompany.Text = infoPurchaseMaster.TransportationCompany;
         txtGrandTotal.Text = Math.Round(infoPurchaseMaster.GrandTotal, PublicVariables._inNoOfDecimalPlaces).ToString();
         PurchaseDetailsFill();
         TaxGridFill();
         AdditionalCostGridFill();
         isPartyBalanceRef = bllAccountLedger.PartyBalanceAgainstReferenceCheck(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
         if (isPartyBalanceRef)
         {
             cmbCashOrParty.Enabled = false;
         }
         else
         {
             cmbCashOrParty.Enabled = true;
         }
         isEditFill = false;
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI55:" + 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);
     }
 }