private int serveSelectedItems(ServeItemsForm.InputFields mfields)
        {
            string invoiceNoTxt = input_invoiceNo.Text.Trim().ToUpper();

            foreach (DataGridViewRow r in disp_grid_inv.SelectedRows)
            {
                DataGridViewCellCollection c = r.Cells;

                if (c["invoice_orderStatus"].Value.ToString().Equals("SERVED"))
                {
                    return -1;
                }

                RecordsDataSet.InvoiceItemRow invoice =
                    input_invoiceNo.SelectedItem as RecordsDataSet.InvoiceItemRow;                

                int itemId = int.Parse(c["invoice_itemId"].Value.ToString());
                int qty = int.Parse(c["invoice_quantity"].Value.ToString());
                int? stock = dbm.getItemQuantity(mfields.warehouse.id, itemId);

                if (stock < qty || stock == null)
                {
                    showErrorMessage("Not enough stock for item '" + c["invoice_itemName"].Value + "'");
                    return -1;
                }

                db.ItemServed.AddItemServedRow(
                    invoiceNoTxt, itemId, mfields.warehouse.id, qty, mfields.date);
            }
           
            return disp_grid_inv.SelectedRows.Count;
        }
        private void serveWRet_option_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(input_invoiceNo.Text))
            {
                showErrorMessage("No Invoice Selected.");
                return;
            }

            if (disp_grid_inv.SelectedRows.Count == 0)
            {
                showErrorMessage("No Rows Selected.");
            }
            else
            {
                ServeItemsForm.InputFields inf = new ServeItemsForm.InputFields();
                DialogResult res;

                using (ServeItemsForm sf = new ServeItemsForm(inf, this))
                {
                    sf.setInvoice(input_invoiceNo.Text);
                    res = sf.ShowDialog();
                }

                if (res.Equals(DialogResult.OK))
                {
                    try
                    {
                        int rowsAff = serveWRetSelectedItems(inf);

                        if (rowsAff > 0)
                        {
                            dbm.dbmgr.UpdateAll(dbm.db);
                            dbmgr.ReturnsInventoryTableAdapter.Fill(db.ReturnsInventory);
                            dbmgr.InvoiceItemTableAdapter.Fill(db.InvoiceItem);

                            refreshWarehouseComboBox();
                            invoice_fillDataGrid(input_invoiceNo.SelectedItem as RecordsDataSet.InvoiceRow);

                            showSuccessMessage("Operation successful. " + rowsAff + " affected.");
                        }
                        else
                        {
                            showErrorMessage("Can't perform action on some selected items. Operation Aborted.");
                        }
                    }
                    catch (DBConcurrencyException ex)
                    {
                        DialogResult r = showErrorPrompt(
                            "An error occured while trying to sync with database.\n\n" +
                            "Details:\n" + ex.Message);

                        if (r.Equals(DialogResult.Retry))
                        {
                            serve_items_option_Click(sender, e);
                        }
                        else
                        {
                            db.RejectChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        db.RejectChanges();
                        showErrorMessage("An error occured.\n\nDetails:\n" + ex.Message);
                    }
                }
            }
        }