/// <summary>
 /// Displays the cost of the item selected in the combo box
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cbItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     try
     {
         txtCost.Text = ((clsItem)cbItems.SelectedItem).ICost.ToString();
     }
     catch (Exception ex)
     {
         HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                 MethodInfo.GetCurrentMethod().Name, ex.Message);
     }
 }
 /// <summary>
 /// Edit the invoice Information
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnEditInvoice_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         enableUI();
         bIsEditInvoice = true;
     }
     catch (Exception ex)
     {
         HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                 MethodInfo.GetCurrentMethod().Name, ex.Message);
     }
 }
 /// <summary>
 /// Opens up the Window Items Form
 /// Look at items.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void miDefTable_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         this.Hide();
         wndItemsForm.ShowDialog();
         this.Show();
     }
     catch (Exception ex)
     {
         HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                 MethodInfo.GetCurrentMethod().Name, ex.Message);
     }
 }
 /// <summary>
 /// Add the item selected in the combobox
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnAddItem_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         //add the item from the combobox to the invoice
         if (cbItems.SelectedItem != null)
         {
             clsItem selectedItem = new clsItem((clsItem)cbItems.SelectedItem);
             oMainLogic.addItem(selectedItem);
             resetUI();
         }
     }
     catch (Exception ex)
     {
         HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                 MethodInfo.GetCurrentMethod().Name, ex.Message);
     }
 }
        /// <summary>
        /// Loads up the main Window
        /// </summary>
        public wndMain()
        {
            try
            {
                InitializeComponent();

                //allow application to close
                Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
                //Instantiate Window Objects
                wndItemsForm  = new wndItems();
                wndSearchForm = new wndSearch();
                oMainLogic    = new clsMainLogic();
            }
            catch (Exception ex)
            {
                HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                        MethodInfo.GetCurrentMethod().Name, ex.Message);
            }
        }
 /// <summary>
 /// Delete the item selected in the datagrid
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnDeleteItem_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         //Delete the item from the combobox and the invoice
         if (dgItemList.SelectedCells != null && dgItemList.SelectedIndex != -1)
         {
             var     currentRowIndex = dgItemList.SelectedIndex;
             clsItem selectedItem    = ((clsItem)dgItemList.SelectedCells[currentRowIndex].Item);
             oMainLogic.deleteItem(selectedItem);
             resetUI();
         }
     }
     catch (Exception ex)
     {
         HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                 MethodInfo.GetCurrentMethod().Name, ex.Message);
     }
 }
        /// <summary>
        /// .Delete The current invoice
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteInvoice_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ////delete object from database
                oMainLogic.delInvoiceFromDB();
                MessageBox.Show("Invoice Deleted!");

                //reset ui
                dateInvoiceDate.SelectedDate = null;
                bIsNewInvoice = true;
                updateUI();
                btnEditInvoice.IsEnabled   = false;
                btnDeleteInvoice.IsEnabled = false;
                btnCreateInvoice.IsEnabled = true;
                txtInvoiceNumber.Text      = "";
            }
            catch (Exception ex)
            {
                HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                        MethodInfo.GetCurrentMethod().Name, ex.Message);
            }
        }
        /// <summary>
        /// Saves User input Into Invoice
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveInvoice_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dateInvoiceDate.SelectedDate != null && txtTotalCost.Text != "0")
                {
                    MessageBox.Show("Invoice Created");
                    //Get date Value and update invoice date
                    DateTime InvoiceDate = dateInvoiceDate.SelectedDate.Value.Date;
                    oMainLogic.OInvoice.DateInvoiceDate = InvoiceDate;

                    if (bIsEditInvoice)
                    {
                        //update invoice in db
                        oMainLogic.updateInvoiceFromDB();
                    }
                    else
                    {
                        //Add invoice to Database
                        oMainLogic.addInvoiceToDB();
                        //update the invoice number
                        txtInvoiceNumber.Text = oMainLogic.OInvoice.IInvoiceNumber.ToString();
                    }

                    disableUI();
                }
                else
                {
                    MessageBox.Show("Missing Invoice Info");
                }
            }
            catch (Exception ex)
            {
                HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                        MethodInfo.GetCurrentMethod().Name, ex.Message);
            }
        }
        /// <summary>
        /// Opens up the Window Search Form
        /// Search for invoices.
        /// Selects
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miSearchInv_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Hide();
                wndSearchForm.updateSearchOptions();
                wndSearchForm.ShowDialog();
                //Populate the invoice with oldInvoice
                if (wndSearchForm.getSelectedInvoiceId() != -1)
                {
                    bIsNewInvoice = false;
                    updateUI();
                }
                disableUI();

                //Finished retreive
                this.Show();
            }
            catch (Exception ex)
            {
                HandleError.handleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                                        MethodInfo.GetCurrentMethod().Name, ex.Message);
            }
        }