private void buttonOk_Click(object sender, EventArgs e)
        {
            _addedFlight = new FlightConnectionList();

            foreach (ListViewItem itemSelected in listViewFlight.SelectedItems)
            {
                _addedFlight = _connectionList[itemSelected.Index];
            }

            DialogResult = DialogResult.OK;
            Close();
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            bool hasNoError = true;

            errorProvider.Clear();

            if (comboBoxCustomername.Text.Trim().Length == 0)
            {
                errorProvider.SetError(comboBoxCustomername, "Customer Name is required");
                hasNoError = false;
            }
            if (textBoxCity.Text.Trim().Length == 0)
            {
                errorProvider.SetError(textBoxCity, "City is required");
                hasNoError = false;
            }

            if (!_hasChanges)
            {
                errorProvider.SetError(dataGridViewEvent, "Not all event has flight information");
                hasNoError = false;
            }
            if (hasNoError)
            {
                #region verify item
                _itineraryList = new List <FlightConnectionList>();
                foreach (DataRow dr in dtEventFlight.Rows)
                {
                    FlightConnectionList itemList = new FlightConnectionList();
                    itemList.AgencyNumber           = dr["FlightAgency"].ToString();
                    itemList.FlightConnectionNumber = dr["FlightConnection"].ToString();
                    itemList.FlightDate             = dr["FlightDate"].ToString();
                    if (itemList.FlightDate.Length == 0)
                    {
                        errorProvider.SetError(dataGridViewEvent, "Not all event has flight information");
                        return;
                    }
                    itemList.EventID = Convert.ToInt32(dr["EventID"]);
                    _itineraryList.Add(itemList);
                }
                _country        = (string)comboBoxCountry.SelectedValue;
                _language       = (string)comboBoxLanguage.SelectedValue;
                _customerName   = comboBoxCustomername.Text.Trim();
                _customerNumber = string.Empty;

                if (comboBoxCustomername.SelectedIndex > -1 &&
                    comboBoxCustomername.SelectedIndex <
                    customerList.Count)
                {
                    if (comboBoxCustomername.SelectedValue != null)
                    {
                        _customerNumber = customerList[(string)comboBoxCustomername.SelectedValue].CUSTOMERID;
                    }
                }
                else if (comboBoxCustomername.Items.Contains(_customerName))
                {
                    comboBoxCustomername.SelectedIndex = comboBoxCustomername.Items.IndexOf(_customerName);
                    if (comboBoxCustomername.SelectedValue != null)
                    {
                        BAPISCUDAT itemSelected =
                            customerList[(string)comboBoxCustomername.SelectedValue];
                        _customerNumber = itemSelected.CUSTOMERID;
                    }
                }
                #endregion

                PseudoProgressForm progress = new PseudoProgressForm();
                progress.ProgressLabel = "Saving to SAP...";
                BackgroundWorker bgWorker = new BackgroundWorker();

                string dob =
                    string.Format("{0:yyyy-MM-dd}", dateTimePicker.Value);
                string city       = textBoxCity.Text.Trim();
                string address    = textboxAddress.Text.Trim();
                string phone      = textboxPhone.Text.Trim();
                string postalCode = textBoxPostalCode.Text.Trim();

                bgWorker.DoWork +=
                    delegate(object senderWorker, DoWorkEventArgs eWorker)
                {
                    #region savecustomer
                    SAPCustomer customer = new SAPCustomer(Config.SAPUserName, Config.SAPPassword);
                    if (_customerNumber.Length == 0)
                    {
                        if (customer.CreateFromData(
                                city,
                                _country,
                                _customerName,
                                "P",
                                "none",
                                _language,
                                address,
                                phone,
                                postalCode))
                        {
                            //get customer number of inserted customer
                            bool found = false;
                            while (!found)
                            {
                                customer.GetList();
                                if (customer._customerList.Length > 0)
                                {
                                    //search last 4
                                    for (int i = customer._customerList.Length - 1;
                                         i > (customer._customerList.Length - 5);
                                         i--)
                                    {
                                        if (customer._customerList[i].CUSTNAME.Trim() == _customerName.Trim())
                                        {
                                            _customerNumber = customer._customerList[i].CUSTOMERID;
                                            found           = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            _errorCustomer = customer._bapiReturn;
                            eWorker.Cancel = true;
                        }
                    }
                    #endregion

                    #region create flight trip
                    if (_customerNumber.Length > 0)
                    {
                        SAPFlightTrip flightTrip =
                            new SAPFlightTrip(Config.SAPUserName, Config.SAPPassword);
                        for (int index = 0; index < _itineraryList.Count; index++)
                        {
                            FlightConnectionList itemList = _itineraryList[index];
                            string classType = "Y";
                            if (dictPackageType.ContainsKey(itemList.EventID))
                            {
                                classType = dictPackageType[itemList.EventID];
                            }
                            string tripNumber;
                            string travelAgencyNumber;
                            if (flightTrip.CreateTrip(
                                    itemList.AgencyNumber,
                                    classType,
                                    _customerNumber,
                                    itemList.FlightConnectionNumber,
                                    "",
                                    itemList.FlightDate,
                                    "",
                                    "none",
                                    dob,
                                    _customerName,
                                    out travelAgencyNumber,
                                    out tripNumber))
                            {
                                itemList.TripNumber   = tripNumber;
                                _itineraryList[index] = itemList;
                            }
                            else
                            {
                                _errorFlight   = flightTrip._bapiFlTripReturn;
                                eWorker.Cancel = true;
                            }
                        }
                    }
                    #endregion
                };

                bgWorker.RunWorkerCompleted +=
                    delegate(object senderWorker, RunWorkerCompletedEventArgs eWorker)
                {
                    progress.Close();

                    int eventAttendeeID = 0;
                    SAPEventAttendeeReadWrite eventAttendee =
                        new SAPEventAttendeeReadWrite(Config._dbConnectionName);
                    DateTime dateCreate;
                    eventAttendee.Insert(
                        _packageID,
                        _customerNumber,
                        dateTimePicker.Value,
                        out dateCreate,
                        out eventAttendeeID);

                    foreach (FlightConnectionList item in _itineraryList)
                    {
                        int eventAttendMapID = 0;
                        SAPEventAttendeeAgencyMapReadWrite eventMap =
                            new SAPEventAttendeeAgencyMapReadWrite(Config._dbConnectionName);
                        eventMap.Insert(
                            eventAttendeeID,
                            item.EventID,
                            item.AgencyNumber,
                            item.TripNumber,
                            out eventAttendMapID);
                    }
                    Message.DisplayInfo("Thanks for purchasing the package.");     // Total Ticket Price: " + e.Result.ToString());
                    Close();
                };

                bgWorker.RunWorkerAsync();
                progress.ShowDialog();
            }
        }
        public void AddListItemMethod(BAPISCODAT item)
        {
            DateTime flightDate = FlightDate(item.FLIGHTDATE);

            if (flightDate.AddMonths(3) < _eventDate)
            {
                return;
            }
            else if (flightDate > _eventDate)
            {
                return;
            }
            else if (flightDate < DateTime.Now)
            {
                return;
            }
            else if (flightDate < _prevEventDate)
            {
                return;
            }

            //check availability first
            SAPFlightConnection flightConn = new SAPFlightConnection(Config.SAPUserName, Config.SAPPassword);

            if (!flightConn.GetDetail(
                    item.FLIGHTCONN,
                    item.FLIGHTDATE,
                    "",
                    item.AGENCYNUM))
            {
                return;
            }
            if (flightConn._bapiAvailability.Length > 0)
            {
                for (int counter = 0; counter < flightConn._bapiAvailability.Length; counter++)
                {
                    switch (_packageType)
                    {
                    case "Y":
                        if (flightConn._bapiAvailability[counter].ECONOFREE == 0)
                        {
                            return;
                        }
                        break;

                    case "C":
                        if (flightConn._bapiAvailability[counter].BUSINFREE == 0)
                        {
                            return;
                        }
                        break;

                    case "F":
                        if (flightConn._bapiAvailability[counter].FIRSTFREE == 0)
                        {
                            return;
                        }
                        break;
                    }
                }
            }
            else
            {
                return;
            }

            ListViewItem itemCreate = listViewFlight.Items.Add(item.FLIGHTDATE);

            itemCreate.SubItems.Add(item.DEPTIME);
            itemCreate.SubItems.Add(item.AIRPORTFR);
            itemCreate.SubItems.Add(item.ARRDATE);
            itemCreate.SubItems.Add(item.AIRPORTTO);
            itemCreate.SubItems.Add(item.ARRTIME);
            itemCreate.SubItems.Add(item.CITYFROM);
            itemCreate.SubItems.Add(item.CITYTO);

            FlightConnectionList itemList = new FlightConnectionList();

            itemList.AgencyNumber           = item.AGENCYNUM;
            itemList.AirportFrom            = item.AIRPORTFR;
            itemList.AirportTo              = item.AIRPORTTO;
            itemList.ArrivalDate            = item.ARRDATE;
            itemList.ArrivalTime            = item.ARRTIME;
            itemList.CityFrom               = item.CITYFROM;
            itemList.CityTo                 = item.CITYTO;
            itemList.DepartureTime          = item.DEPTIME;
            itemList.FlightConnectionNumber = item.FLIGHTCONN;
            itemList.FlightDate             = item.FLIGHTDATE;
            itemList.FlightTime             = item.FLIGHTTIME;
            itemList.NumberHops             = item.NUMHOPS;
            _connectionList.Add(itemList);

            labelNumRecords.Text = string.Format("{0} records", listViewFlight.Items.Count);
        }