private void PopulateRecBatchTables()
        {
            if (dtRecDocuments.Rows.Count > 0)
            {
                var selectedBatches = receiptBatchDataSources.Where(x => x.ItemCode == currentRecItemCode && x.WhsCode == currentRecWhsCode && x.Index == currentRecDocumentIndex);
                double totalSelected = Convert.ToDouble(dtRecDocuments.GetValue("TotalSelected", currentRecDocumentIndex));
                if (selectedBatches.Count() > 0)
                {
                    selectedBatches.First().SelectedBatches = CopySelectedBatchesDataTable(dtRecBatches);
                    selectedBatches.First().TotalSelected = totalSelected;
                }
                else
                {
                    double avgPrice = Convert.ToDouble(dtRecDocuments.GetValue("AvgPrice", currentRecDocumentIndex));
                    double addAmount = Convert.ToDouble(dtRecDocuments.GetValue("AddAmount", currentRecDocumentIndex));
                    double quantity = Convert.ToDouble(dtRecDocuments.GetValue("Quantity", currentRecDocumentIndex));
                    double totalNeeded = Convert.ToDouble(dtRecDocuments.GetValue("TotalNeeded", currentRecDocumentIndex));


                    var sbds = new SelectedBatchDataSources()
                    {
                        Quantity = Convert.ToDouble(quantity),
                        AddAmount = addAmount,
                        AvgPrice = avgPrice,
                        Index = currentRecDocumentIndex,
                        ItemCode = currentRecItemCode,
                        WhsCode = currentRecWhsCode,
                        SelectedBatches = CopySelectedBatchesDataTable(dtRecBatches),
                        TotalNeeded = totalNeeded,
                        TotalSelected = totalNeeded
                    };
                    receiptBatchDataSources.Add(sbds);
                }
            }
        }
        private void gvRecDocuments_ClickAfter(object sboObject, SAPbouiCOM.SBOItemEventArg pVal)
        {
            if (currentRecItemCode != string.Empty)
            {
                PopulateRecBatchTables();
            }
            if (pVal.Row >= 0)
            {
                string itemCode = dtRecDocuments.GetValue("ItemCode", pVal.Row).ToString();
                string whsCode = dtRecDocuments.GetValue("WhsCode", pVal.Row).ToString();
                double avgPrice = Convert.ToDouble(dtRecDocuments.GetValue("AvgPrice", pVal.Row));
                double addAmount = Convert.ToDouble(dtRecDocuments.GetValue("AddAmount", pVal.Row));
                double quantity = Convert.ToDouble(dtRecDocuments.GetValue("Quantity", pVal.Row));

                currentRecDocumentIndex = pVal.Row;
                currentRecItemCode = itemCode;
                currentRecWhsCode = whsCode;

                //CHECK IF THE SELECTED BATCHES TABLE HAS BEEN POPULATED BEFORE
                var selectedBatches = receiptBatchDataSources.Where(x => x.ItemCode == currentRecItemCode && x.WhsCode == currentRecWhsCode && x.Index == currentRecDocumentIndex);
                if (selectedBatches.Count() > 0)
                {
                    dtRecBatches.Rows.Clear();
                    this.UIAPIRawForm.Freeze(true);
                    for (int x = 0; x < selectedBatches.First().SelectedBatches.Rows.Count; x++)
                    {
                        dtRecBatches.Rows.Add(1);
                        dtRecBatches.SetValue("Batch", x, selectedBatches.First().SelectedBatches.Rows[x]["Batch"]);
                        dtRecBatches.SetValue("Selected Qty", x, selectedBatches.First().SelectedBatches.Rows[x]["Selected Qty"]);

                    }
                    this.UIAPIRawForm.Freeze(false);
                }
                else
                {
                    dtRecBatches.Rows.Clear();
                    //System.Data.DataTable dt = CopyBatchesDataTable(dtRecBatches);
                    var bds = new SelectedBatchDataSources()
                    {
                        AddAmount = addAmount,
                        AvgPrice = Convert.ToDouble(avgPrice),
                        Quantity = Convert.ToDouble(quantity),
                        Index = currentRecDocumentIndex,
                        ItemCode = currentRecItemCode,
                        WhsCode = currentRecWhsCode,
                        SelectedBatches = CopySelectedBatchesDataTable(dtRecBatches)
                    };
                    receiptBatchDataSources.Add(bds);
                }
            }
            gvRecBatches.AddLine();
        }
        /// <summary>
        /// THIS METHOD IS USED TO GET THE USER ENTERED DATA AND POPULATE THE RELATED BATCHES DATA SOURCES ON ITEM SELECTION CHANGED
        /// </summary>
        private void PopulateBatchTables()
        {
            if (dtDocuments.Rows.Count > 0)
            {
                var availableBatches = batchDataSources.Where(x => x.ItemId == currentItemCode && x.WhsCode == currentWhsCode);
                var selectedbatches = issueBatchDataSources.Where(x => x.ItemCode == currentItemCode && x.WhsCode == currentWhsCode && x.Index == currentDocumentIndex);

                if (availableBatches.Count() > 0)
                {
                    availableBatches.First().AvailableBatches = CopyBatchesDataTable(dtBatches);
                }
                else
                {
                    var bds = new BatchDataSources()
                    {
                        ItemId = currentItemCode,
                        WhsCode = currentWhsCode,
                        AvailableBatches = CopyBatchesDataTable(dtBatches)
                    };
                    batchDataSources.Add(bds);
                }
                if (selectedbatches.Count() > 0)
                {
                    selectedbatches.First().SelectedBatches = CopySelectedBatchesDataTable(dtSelected);
                }
                else
                {

                    double avgPrice = Convert.ToDouble(dtDocuments.GetValue("AvgPrice", currentDocumentIndex));
                    double addAmount = Convert.ToDouble(dtDocuments.GetValue("AddAmount", currentDocumentIndex));
                    double quantity = Convert.ToDouble(dtDocuments.GetValue("Quantity", currentDocumentIndex));
                    double totalNeeded = Convert.ToDouble(dtDocuments.GetValue("TotalNeeded", currentRecDocumentIndex));
                    var sbds = new SelectedBatchDataSources()
                    {
                        Quantity = Convert.ToDouble(quantity),
                        AddAmount = addAmount,
                        AvgPrice = Convert.ToDouble(avgPrice),
                        Index = currentDocumentIndex,
                        ItemCode = currentItemCode,
                        WhsCode = currentWhsCode,
                        SelectedBatches = CopySelectedBatchesDataTable(dtSelected),
                        TotalNeeded = totalNeeded
                    };
                    issueBatchDataSources.Add(sbds);
                }
            }
        }
        private void gvDocuments_ClickAfter(object sboObject, SAPbouiCOM.SBOItemEventArg pVal)
        {
            gvDocuments.Rows.IsSelected(pVal.Row);
            if (currentItemCode != string.Empty)
            {
                PopulateBatchTables();
            }

            if (pVal.Row >= 0)
            {
                string itemCode = dtDocuments.GetValue("ItemCode", pVal.Row).ToString();
                string whsCode = dtDocuments.GetValue("WhsCode", pVal.Row).ToString();
                double avgPrice = Convert.ToDouble(dtDocuments.GetValue("AvgPrice", pVal.Row));
                double addAmount = Convert.ToDouble(dtDocuments.GetValue("AddAmount", pVal.Row));
                double quantity = Convert.ToDouble(dtDocuments.GetValue("Quantity", pVal.Row));

                currentDocumentIndex = pVal.Row;
                currentItemCode = itemCode;
                currentWhsCode = whsCode;

                //CHECK IF THE CURRENT SELECTED ITEM HAS BATCHES TABLE POPULATED BEFORE
                var availableBatches = batchDataSources.Where(x => x.ItemId == currentItemCode && x.WhsCode == currentWhsCode);
                if (availableBatches.Count() > 0)
                {
                    dtBatches.Rows.Clear();
                    var tempBatches = availableBatches.First().AvailableBatches;

                    this.UIAPIRawForm.Freeze(true);
                    for (int x = 0; x < tempBatches.Rows.Count; x++)
                    {
                        dtBatches.Rows.Add(1);
                        dtBatches.SetValue("ItemCode", x, tempBatches.Rows[x]["ItemCode"]);
                        dtBatches.SetValue("DistNumber", x, tempBatches.Rows[x]["DistNumber"]);
                        dtBatches.SetValue("Quantity", x, tempBatches.Rows[x]["Quantity"]);
                        dtBatches.SetValue("WhsCode", x, tempBatches.Rows[x]["WhsCode"]);
                        dtBatches.SetValue("AvgPrice", x, tempBatches.Rows[x]["AvgPrice"]);
                        dtBatches.SetValue("Selected Qty", x, tempBatches.Rows[x]["Selected Qty"]);
                    }
                    this.UIAPIRawForm.Freeze(false);
                }
                else
                {
                    PopulateBatchesGrid(pVal.Row);
                    dtSelected.Rows.Clear();
                    System.Data.DataTable dt = CopyBatchesDataTable(dtBatches);
                    var bds = new BatchDataSources() { ItemId = currentItemCode, WhsCode = currentWhsCode, AvailableBatches = CopyBatchesDataTable(dtBatches) };
                    batchDataSources.Add(bds);
                }

                //CHECK IF THE SELECTED BATCHES TABLE HAS BEEN POPULATED BEFORE
                var selectedBatches = issueBatchDataSources.Where(x => x.ItemCode == currentItemCode && x.WhsCode == currentWhsCode && x.Index == currentDocumentIndex);
                if (selectedBatches.Count() > 0)
                {
                    dtSelected.Rows.Clear();
                    this.UIAPIRawForm.Freeze(true);
                    for (int x = 0; x < selectedBatches.First().SelectedBatches.Rows.Count; x++)
                    {
                        dtSelected.Rows.Add(1);
                        dtSelected.SetValue("Batch", x, selectedBatches.First().SelectedBatches.Rows[x]["Batch"]);
                        dtSelected.SetValue("Selected Qty", x, selectedBatches.First().SelectedBatches.Rows[x]["Selected Qty"]);

                    }
                    this.UIAPIRawForm.Freeze(false);
                }
                else
                {
                    dtSelected.Rows.Clear();
                    System.Data.DataTable dt = CopyBatchesDataTable(dtBatches);
                    var bds = new SelectedBatchDataSources()
                    {
                        AddAmount = addAmount,
                        AvgPrice = Convert.ToDouble(avgPrice),
                        Quantity = Convert.ToDouble(quantity),
                        Index = currentDocumentIndex,
                        ItemCode = currentItemCode,
                        WhsCode = currentWhsCode,
                        SelectedBatches = CopySelectedBatchesDataTable(dtSelected)
                    };
                    issueBatchDataSources.Add(bds);
                }
            }
        }