/// <summary>
 /// Grid CellEndEdit for product details fill to curresponding row in grid
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvPurchaseReturn_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         string strBarcode2 = string.Empty;
         decimal decBatchId2 = 0;
         BatchBll BllBatch = new BatchBll();
         RackBll BllRack = new RackBll();
         PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
         PurchaseReturnBll BllPurchaseReturn = new PurchaseReturnBll();
         ProductCreationBll BllProductCreation = new ProductCreationBll();
         if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtproductName")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value.ToString().Trim() != string.Empty)
             {
                 ProductInfo infoProduct = BllProductCreation.ProductViewByName(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value.ToString());
                 if (infoProduct.ProductCode != null && infoProduct.ProductCode != string.Empty)
                 {
                     FillProductDetails(infoProduct.ProductCode.ToString(), e.RowIndex);
                 }
                 else
                 {
                     StringEmptyDetailsInGrid();
                 }
             }
             else
             {
                 dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value = string.Empty;
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtproductCode")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value.ToString().Trim() != string.Empty)
             {
                 ProductInfo infoProduct = BllProductCreation.ProductViewByCode(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value.ToString());
                 if (infoProduct.ProductName != null && infoProduct.ProductCode != string.Empty)
                 {
                     FillProductDetails(infoProduct.ProductCode.ToString(), e.RowIndex);
                 }
                 else
                 {
                     StringEmptyDetailsInGrid();
                 }
             }
             else
             {
                 dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value = string.Empty;
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtbarcode")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value.ToString().Trim() != string.Empty)
             {
                 string strBarcode = dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value.ToString();
                 decBatchId = BllProductCreation.BatchIdByPartNoOrBarcode(strBarcode, strBarcode);
                 if (decBatchId > 0)
                 {
                     List<DataTable> listObjBatchName = new List<DataTable>();
                     listObjBatchName = BllProductCreation.ProductCodeAndBarcodeByBatchId(decBatchId);
                     dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value = listObjBatchName[0].Rows[0]["productCode"].ToString();
                     if (listObjBatchName[0].Rows[0]["barcode"].ToString() != null && listObjBatchName[0].Rows[0]["barcode"].ToString() != string.Empty)
                     {
                         dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value = listObjBatchName[0].Rows[0]["barcode"].ToString();
                     }
                     decimal batchId = BllBatch.BatchViewByBarcode(strBarcode);
                     dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = batchId;
                     FillProductDetails(listObjBatchName[0].Rows[0]["productCode"].ToString(), e.RowIndex);
                 }
                 else
                 {
                     StringEmptyDetailsInGrid();
                 }
             }
             else
             {
                 dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value = string.Empty;
             }
         }
         if (e.ColumnIndex == dgvPurchaseReturn.Columns["dgvcmbUnit"].Index)
         {
             if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
             {
                 if ((dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value != null) && (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() != string.Empty))
                 {
                     UnitConversionCalc(e.RowIndex);
                     AmountCalculation("dgvtxtqty", e.RowIndex);
                 }
             }
         }
         if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtqty" && isAmountcalc)
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString() != string.Empty && Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString()) != 0)
             {
                 if (cmbInvoiceNo.SelectedValue == null || cmbInvoiceNo.SelectedValue.ToString() == string.Empty)
                 {
                     AmountCalculation("dgvtxtqty", e.RowIndex);
                 }
                 else
                 {
                     List<DataTable> ListObj= BllPurchaseInvoice.PurchaseDetailsViewByPurchaseMasterIdWithRemaining(Convert.ToDecimal(cmbInvoiceNo.SelectedValue.ToString()), decPurchaseReturnMasterId, decPurchaseReturnVoucherTypeId);
                     if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtPurchaseDetailsId"].Value != null)
                     {
                         if (Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtPurchaseDetailsId"].Value.ToString()) > 0)
                         {
                             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value != null)
                             {
                                 if (Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString()) > Convert.ToDecimal(ListObj[0].Rows[e.RowIndex]["qty"].ToString()))
                                 {
                                     dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value = Math.Round(Convert.ToDecimal(ListObj[0].Rows[e.RowIndex]["qty"].ToString()), PublicVariables._inNoOfDecimalPlaces);
                                 }
                             }
                         }
                     }
                     AmountCalculation("dgvtxtqty", e.RowIndex);
                 }
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtrate" && isAmountcalc)
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString() != string.Empty && Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString()) != 0)
             {
                 AmountCalculation("dgvtxtrate", e.RowIndex);
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtdiscount")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtdiscount"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtdiscount"].Value.ToString().Trim() != string.Empty && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtDiscount"].Value.ToString().Trim() != "0")
             {
                 AmountCalculation("dgvtxtrate", e.RowIndex);
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvcmbTax" && isAmountcalc)
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbTax"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbTax"].Value.ToString() != string.Empty)
             {
                 AmountCalculation("dgvtxtrate", e.RowIndex);
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvcmbBatch")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value.ToString() != string.Empty)
             {
                 decBatchId2 = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvcmbBatch"].Value);
                 strBarcode2 = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId2);
                 dgvPurchaseReturn.CurrentRow.Cells["dgvtxtbarcode"].Value = strBarcode2;
             }
         }
         CheckInvalidEntries(e);
     }
     catch (Exception ex)
     {
         MessageBox.Show("PR:80" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Function for Product details fill in grid corresponding to invoice number
        /// </summary>
        public void InvoiceDetailsFill()
        {
            PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
            try
            {
                if (!isEditFill)
                {
                    inMaxCount = 0;
                    string strTaxRate1 = string.Empty;
                    string strproductId = string.Empty;
                    decimal decTaxAmount = 0;
                    decimal decAmount = 0;
                    decimal decNetAmount = 0;
                    decimal decTaxRate = 0;
                    decimal decDiscountAmount = 0;
                    decimal decGrossAmount = 0;
                    int inRowIndex = 0;
                    UnitConvertionBll bllUnitConversion = new UnitConvertionBll();
                    PurchaseReturnBll BllPurchaseReturn = new PurchaseReturnBll();
                    List<DataTable> ListObjDetails = BllPurchaseInvoice.PurchaseDetailsViewByPurchaseMasterIdWithRemaining(Convert.ToDecimal(cmbInvoiceNo.SelectedValue.ToString()), decPurchaseReturnMasterId, decPurchaseReturnVoucherTypeId);
                    dgvPurchaseReturn.Rows.Clear();
                    foreach (DataRow drowDetails in ListObjDetails[0].Rows)
                    {
                        dgvPurchaseReturn.Rows.Add();
                        DGVUnitComboFill();
                        DGVBatchComboFill();
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtPurchaseDetailsId"].Value = drowDetails.ItemArray[0].ToString();
                        strproductId = drowDetails.ItemArray[2].ToString();
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductId"].Value = Convert.ToString(drowDetails.ItemArray[2]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductName"].Value = Convert.ToString(drowDetails.ItemArray[5]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductCode"].Value = Convert.ToString(drowDetails.ItemArray[6]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtVoucherTypeId"].Value = Convert.ToString(drowDetails.ItemArray[21]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtVoucherNo"].Value = Convert.ToString(drowDetails.ItemArray[23]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtInvoiceNo"].Value = Convert.ToString(drowDetails.ItemArray[22]);
                        decQty = Math.Round(Convert.ToDecimal(Convert.ToString(drowDetails.ItemArray[10])), PublicVariables._inNoOfDecimalPlaces);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtqty"].Value = decQty;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtrate"].Value = Convert.ToString(drowDetails.ItemArray[11]);
                        decRate = Math.Round(Convert.ToDecimal(Convert.ToString(drowDetails.ItemArray[11])), PublicVariables._inNoOfDecimalPlaces);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtbarcode"].Value = Convert.ToString(drowDetails.ItemArray[8]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtConversionRate"].Value = bllUnitConversion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(drowDetails.ItemArray[9].ToString()));
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drowDetails.ItemArray[9].ToString());
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbGodown"].Value = (Convert.ToString(drowDetails.ItemArray[17]) != null) ? Convert.ToDecimal(drowDetails.ItemArray[17].ToString()) : 0;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbRack"].Value = (drowDetails.ItemArray[18].ToString() != null) ? Convert.ToDecimal(drowDetails.ItemArray[18].ToString()) : 0;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbBatch"].Value = (drowDetails.ItemArray[19].ToString() != null) ? Convert.ToDecimal(drowDetails.ItemArray[19].ToString()) : 0;
                        decGrossAmount = decQty * decRate;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(drowDetails.ItemArray[12].ToString());
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbUnit"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drowDetails.ItemArray[9]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtgrossValue"].Value = Math.Round(decGrossAmount, PublicVariables._inNoOfDecimalPlaces);

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

                            if (strTaxRate1 != string.Empty)
                            {
                                decTaxRate = Convert.ToDecimal(strTaxRate1);
                                decTaxAmount = ((decNetAmount * decTaxRate) / 100);
                                dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxttaxAmount"].Value = Math.Round(decTaxAmount, PublicVariables._inNoOfDecimalPlaces);
                            }
                            else
                            {
                                dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxttaxAmount"].Value = 0;
                            }
                        }
                        else
                        {
                            dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxttaxAmount"].Value = 0;
                        }
                        decAmount = decNetAmount + decTaxAmount;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtAmount"].Value = Math.Round(decAmount, PublicVariables._inNoOfDecimalPlaces);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductCode"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtproductName"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtbarcode"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvcmbBatch"].ReadOnly = true;
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 2].Cells["dgvtxtInRowIndex"].Value = Convert.ToString(drowDetails["extra1"]);
                        dgvPurchaseReturn.Rows[dgvPurchaseReturn.Rows.Count - 1].ReadOnly = true;
                        int intIndex = 0;
                        int.TryParse(Convert.ToString(drowDetails["extra1"]), out intIndex);
                        if (inMaxCount < intIndex)
                            inMaxCount = intIndex;
                        inRowIndex++;
                    }
                    for (int i = inRowIndex; i < dgvPurchaseReturn.Rows.Count; ++i)
                        dgvPurchaseReturn["dgvtxtInRowIndex", i].Value = Convert.ToString(GetNextinRowIndex());
                    CalculateTotalAmount();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PR:42" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }