private void AddButton_Click(object sender, EventArgs e)
        {
            if (PODropdown.SelectedIndex == -1 || PartNumberDropdown.SelectedIndex == -1 || string.IsNullOrWhiteSpace(UnitPriceTextbox.Text))
            {
                return;
            }
            else if (QtyTextbox.Text == "0")
            {
                ClientHelper.ShowErrorMessage("No quantity to return.");
                return;
            }
            else if (int.Parse(QtyTextbox.Text) > QtyLimit)
            {
                QtyTextbox.Text = QtyLimit.ToString();
                ClientHelper.ShowErrorMessage("Return quantity is greater than original quantity.");
                QtyTextbox.Focus();

                return;
            }
            else if (!UserInfo.IsAdmin && !string.IsNullOrWhiteSpace(UnitPriceTextbox.Text) &&
                     _oldUnitPrice != UnitPriceTextbox.Text.ToDecimal())
            {
                ApprovalForm f = new ApprovalForm();
                f.ApprovalDone += new ApprovalDoneEventHandler(f_ApprovalDone);
                f.Show();
            }
            else
            {
                AddPurchaseReturn();
            }
        }
        private void PartNumberDropdown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PartNumberDropdown.SelectedIndex != -1 && purchaseDetails != null)
            {
                _oldUnitPrice = 0m;
                int id = (int)PartNumberDropdown.SelectedValue;

                PurchaseDetailViewModel details = purchaseDetails.FirstOrDefault(a => a.AutoPartDetailId == id);
                if (details != null)
                {
                    //Get Previously returned value of this invoice number
                    int    supplierId  = (int)SupplierDropdown.SelectedValue;
                    string poNumber    = PODropdown.SelectedItem.ToString();
                    int    qtyReturned = this.purchaseReturnController.FetchQuantityReturned(supplierId, poNumber, id, ReturnId);

                    //Get quantities in grid
                    int qtyInGrid = 0;
                    if (list.Any())
                    {
                        qtyInGrid = list.Where(a => a.PartDetailId == id && a.PONumber == poNumber).Sum(a => a.Quantity);
                    }

                    AutoPartTextbox.Text = details.AutoPartName;

                    //Qty that can be returned
                    int qtyLimit = details.Quantity - qtyReturned - qtyInGrid;
                    QtyTextbox.Text = qtyLimit.ToString();
                    QtyLimit        = qtyLimit;

                    _oldUnitPrice         = details.DiscountedPrice;
                    UnitPriceTextbox.Text = _oldUnitPrice.ToString("N2");
                }

                QtyTextbox.Focus();
            }
        }
        // Defining a Function that add current selection from item type and item size listbox to the summary listbox.
        private void TrackStockBalance()
        {
            Random rnd = new Random();

            quantity = int.Parse(QtyTextbox.Text);
            int transactNo = rnd.Next();

            transactNo = rnd.Next(100000, 999999);         // generates a 6 digit random no which serves the purpose for transaction no
            if (summaryListbox.Items.Contains(transactNo))
            {
                summaryListbox.Items.Add(transactNo);
            }

            string DccStockLocation = "DCC Stock.csv";      //defining the csv file's path
            //To read the stock file and store its values to list
            StreamReader LineRead    = new StreamReader(DccStockLocation);
            var          ReadLine    = new List <string[]>();
            int          DCCstockRow = 0;

            while (!LineRead.EndOfStream)
            {
                string[] TempLine = LineRead.ReadLine().Split(',');
                ReadLine.Add(TempLine);
                DCCstockRow++;
            }
            LineRead.Close();
            var StoremyData   = ReadLine.ToArray(); //The collected values to be stored in an array.
            int MealTypeIndex = itemListbox.SelectedIndex;
            int MealSizeIndex = typeListbox.SelectedIndex;

            // Condition when the quantity entered by the user is greater than the available stock

            if (quantity > int.Parse(StoremyData[MealSizeIndex + 1][MealTypeIndex + 1]))
            {
                //Event handling which displayes the available stock and suggesting the user to select lower quantity
                MessageBox.Show("The current available stock for this item is " + StoremyData[MealSizeIndex + 1][MealTypeIndex + 1] + ". Please select a lower quantity !", "Insufficient Quantity", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                QtyTextbox.Focus();
                QtyTextbox.Text = StoremyData[MealSizeIndex + 1][MealTypeIndex + 1]; // available stock level to get automatically pre-entered for processing
                QtyTextbox.SelectAll();
            }
            else
            {
                //Store the correspondant value of every selection into a new list under k that was defined globally, to save all the orders made in order to update the stock file whenever required.
                itemOrders.Add(new List <string>());
                itemOrders[k].Add(StoremyData[MealSizeIndex + 1][MealTypeIndex + 1]); ///Stores the value from the stock file to the list
                itemOrders[k].Add((MealSizeIndex + 1).ToString());                    //Stores the position of meal size selected by user
                itemOrders[k].Add((MealTypeIndex + 1).ToString());                    //Stores the position of meal type selected by user
                k++;
                //logic for getting total cost for selected product
                totalMealcost     = price * quantity;
                fulltotalMealcost = fulltotalMealcost + totalMealcost;

                displaylabel.Text = "The running total is " + totalMealcost.ToString("c", new CultureInfo("en-FR"));

                // displaying the corresponding values in the summary list box
                summaryListbox.Items.Add(transactNo + "      |  " + quantity + "  |  " + itemListbox.SelectedItem + " | " + typeListbox.SelectedItem + " | " + "€" + price);
                StreamWriter stockWtr = new StreamWriter(DccStockLocation);
                StoremyData[MealSizeIndex + 1][MealTypeIndex + 1] = (Int32.Parse(StoremyData[MealSizeIndex + 1][MealTypeIndex + 1]) - quantity).ToString();  // code to upadte stock in the csv file

                for (int i = 0; i < DCCstockRow; i++)
                {
                    string updates = "";
                    for (int j = 0; j < StoremyData[i].Length; j++)
                    {
                        updates += StoremyData[i][j] + ",";
                    }
                    stockWtr.WriteLine(updates);
                }
                stockWtr.Close();
            }
        }