/// <summary>
        /// Function to fill purchase details
        /// </summary>
        public void PurchaseDetailsFill()
        {
            DataTable dtbl = new DataTable();
            PurchaseDetailsSP spPurchaseDetails = new PurchaseDetailsSP();
            PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
            try
            {
                GridComboFill();
                dtbl = spPurchaseDetails.PurchaseDetailsViewByPurchaseMasterId(decPurchaseMasterId);
                int i = 0;

                foreach (DataRow dr in dtbl.Rows)
                {
                    dgvProductDetails.Rows.Add();
                    dgvProductDetails.Rows[i].Cells["dgvtxtPurchaseDetailsId"].Value = dr["purchaseDetailsId"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtPurchaseOrderDetailsId"].Value = dr["purchaseOrderDetailsId"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtMaterialReceiptDetailsId"].Value = dr["materialReceiptDetailsId"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtProductId"].Value = dr["productId"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtBarcode"].Value = dr["barcode"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtProductCode"].Value = dr["productCode"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtProductName"].Value = dr["productName"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtQuantity"].Value = dr["qty"].ToString();
                    UnitComboFill(Convert.ToDecimal(dr["productId"].ToString()), i, dgvProductDetails.Rows[i].Cells["dgvcmbUnit"].ColumnIndex);
                    dgvProductDetails.Rows[i].Cells["dgvtxtUnitConversionId"].Value = dr["unitConversionId"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(dr["unitId"].ToString());

                    dgvProductDetails.Rows[i].Cells["dgvcmbGodown"].Value = 1m;
                    dgvProductDetails.Rows[i].Cells["dgvcmbRack"].Value = 1m;
                    BatchComboFill(Convert.ToDecimal(dr["productId"].ToString()), i, dgvProductDetails.Rows[i].Cells["dgvcmbBatch"].ColumnIndex);
                    dgvProductDetails.Rows[i].Cells["dgvcmbBatch"].Value = Convert.ToDecimal(dr["batchId"].ToString());
                    dgvProductDetails.Rows[i].Cells["dgvtxtRate"].Value = dr["rate"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtGrossValue"].Value = dr["grossValue"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtDiscountPercent"].Value = dr["discountPercent"].ToString();

                    dgvProductDetails.Rows[i].Cells["dgvtxtDiscount"].Value = dr["discount"].ToString();

                    dgvProductDetails.Rows[i].Cells["dgvtxtNetValue"].Value = dr["netvalue"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvcmbTax"].Value = Convert.ToDecimal(dr["taxId"].ToString());
                    dgvProductDetails.Rows[i].Cells["dgvtxtTaxAmount"].Value = dr["taxAmount"].ToString();
                    dgvProductDetails.Rows[i].Cells["dgvtxtAmount"].Value = dr["Amount"].ToString();

                    int inRef = spPurchaseMaster.PurchaseMasterReferenceCheck(decPurchaseMasterId, Convert.ToDecimal(dr["purchaseDetailsId"].ToString()));
                    if (Convert.ToDecimal(dr["purchaseOrderDetailsId"].ToString()) != 0 || Convert.ToDecimal(dr["materialReceiptDetailsId"].ToString()) != 0 || inRef == 1)
                    {
                        dgvProductDetails.Rows[i].Cells["dgvcmbUnit"].ReadOnly = true;
                    }
                    i++;

                }
                if (cmbPurchaseMode.SelectedIndex > 1)
                {
                    dgvProductDetails.AllowUserToAddRows = false;
                }
                SerialNo();
            }
            catch (Exception ex)
            {
                MessageBox.Show("PI56:" + 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()
 {
     PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
     PurchaseDetailsSP spPurchaseDetails = new PurchaseDetailsSP();
     AdditionalCostSP spAdditionalCost = new AdditionalCostSP();
     decimal decPurchaseDetailsId = 0;
     decimal decAdditionalCostId = 0;
     int inRef = 0;
     try
     {
         foreach (var item in arrlstRemove)
         {
             decPurchaseDetailsId = Convert.ToDecimal(item);
             inRef = spPurchaseMaster.PurchaseMasterReferenceCheck(decPurchaseMasterId, decPurchaseDetailsId);
             if (inRef == 0)
             {
                 spPurchaseDetails.PurchaseDetailsDelete(decPurchaseDetailsId);
             }
         }
         foreach (var item1 in arrlstRemoveAdditionalCost)
         {
             decAdditionalCostId = Convert.ToDecimal(item1);
             spAdditionalCost.AdditionalCostDelete(decAdditionalCostId);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI40:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 public void Delete()
 {
     PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
     PurchaseDetailsSP spPurchaseDetails = new PurchaseDetailsSP();
     PurchaseBillTaxSP spPurchaseBillTax = new PurchaseBillTaxSP();
     AdditionalCostSP spAdditionalCost = new AdditionalCostSP();
     AccountLedgerSP spAccountLedger = new AccountLedgerSP();
     StockPostingSP spStockPosting = new StockPostingSP();
     LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
     PurchaseOrderMasterSP spPurchaseOrderMaster = new PurchaseOrderMasterSP();
     MaterialReceiptMasterSP spMaterialReceiptMaster = new MaterialReceiptMasterSP();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     /*---------------------------------Deleting previous stock posting, Ledger posting, partybalanceposting---------------------------------------*/
     infoPurchaseMaster = spPurchaseMaster.PurchaseMasterView(decPurchaseMasterId);
     if (infoPurchaseMaster.MaterialReceiptMasterId != 0)
     {
         infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
         spStockPosting.StockPostingDeleteForSalesInvoiceAgainstDeliveryNote
             (infoPurchaseMaster.VoucherTypeId, infoPurchaseMaster.VoucherNo,
             infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
     }
     spStockPosting.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 = spPurchaseMaster.PurchaseMasterReferenceCheck(decPurchaseMasterId, decPurchaseDetailsId);
                     if (inRef > 0 && !isRef)
                     {
                         isRef = true;
                     }
                 }
             }
         }
         if (!isRef)
         {
             isRef = spAccountLedger.PartyBalanceAgainstReferenceCheck(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
             if (!isRef)
             {
                 spPurchaseMaster.PurchaseMasterDelete(decPurchaseMasterId);
                 spPurchaseDetails.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());
                             spAdditionalCost.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());
                                 spPurchaseBillTax.PurchaseBillTaxDelete(decPurchaseBillTaxId);
                             }
                         }
                     }
                 }
                 if (cmbPurchaseMode.Text == "Against PurchaseOrder")
                 {
                     decPurchaseOrderMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                     infoPurchaseOrderMaster = spPurchaseOrderMaster.PurchaseOrderMasterView(decPurchaseOrderMasterId);
                 }
                 else if (cmbPurchaseMode.Text == "Against MaterialReceipt")
                 {
                     decMaterialReceiptMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                     infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(decMaterialReceiptMasterId);
                 }
                 spLedgerPosting.LedgerPostDelete(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 if (infoPurchaseOrderMaster.PurchaseOrderMasterId != 0)
                 {
                     spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (decPurchaseInvoiceVoucherTypeId, strVoucherNo,
                         infoPurchaseOrderMaster.VoucherNo, infoPurchaseOrderMaster.VoucherTypeId);
                 }
                 else if (infoMaterialReceiptMaster.MaterialReceiptMasterId != 0)
                 {
                     spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (decPurchaseInvoiceVoucherTypeId, strVoucherNo,
                         infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
                 }
                 spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (0, "NA", strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 spAccountLedger.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);
     }
 }
 /// <summary>
 /// Function to check quantity with reference
 /// </summary>
 /// <returns></returns>
 public int QuantityCheckWithReference()
 {
     decimal decQtyPurchaseInvoice = 0;
     decimal decQtyPurchaseReturn = 0;
     int inRef = 0;
     int inF1 = 1;
     decimal decPurchaseDetailsId = 0;
     PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
     PurchaseReturnDetailsSP spPurchaseReturnDetails = new PurchaseReturnDetailsSP();
     try
     {
         foreach (DataGridViewRow dgvrow in dgvProductDetails.Rows)
         {
             if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value != null)
             {
                 if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() != "0" || dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() != string.Empty)
                 {
                     decPurchaseDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString());
                     inRef = spPurchaseMaster.PurchaseMasterReferenceCheck(decPurchaseMasterId, decPurchaseDetailsId);
                     if (inRef == 1)
                     {
                         if (inF1 == 1)
                         {
                             if (dgvrow.Cells["dgvtxtQuantity"].Value != null)
                             {
                                 if (dgvrow.Cells["dgvtxtQuantity"].Value.ToString() != "0" && dgvrow.Cells["dgvtxtQuantity"].Value.ToString() != string.Empty)
                                 {
                                     decQtyPurchaseInvoice = Convert.ToDecimal(dgvrow.Cells["dgvtxtQuantity"].Value.ToString());
                                     decQtyPurchaseReturn = Math.Round(spPurchaseReturnDetails.PurchaseReturnDetailsQtyViewByPurchaseDetailsId(decPurchaseDetailsId), PublicVariables._inNoOfDecimalPlaces);
                                     if (decQtyPurchaseInvoice >= decQtyPurchaseReturn)
                                     {
                                         inF1 = 1;
                                     }
                                     else
                                     {
                                         inF1 = 0;
                                         Messages.InformationMessage("Quantity in row " + (dgvrow.Index + 1) + " should be greater than " + decQtyPurchaseReturn);
                                     }
                                 }
                                 else
                                 {
                                     inF1 = 0;
                                     Messages.InformationMessage("Quantity in row " + (dgvrow.Index + 1) + " should be greater than " + decQtyPurchaseReturn);
                                 }
                             }
                             else
                             {
                                 inF1 = 0;
                                 Messages.InformationMessage("Quantity in row " + (dgvrow.Index + 1) + " should be greater than " + decQtyPurchaseReturn);
                             }
                         }
                     }
                     else
                     {
                         inF1 = 1;
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI35:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     return inF1;
 }