private void button4_Click(object sender, RoutedEventArgs e)
        {
            // back
            int DocNum;

            if (int.TryParse(textBox1.Text, out DocNum))
            {
                DocNum = DocNum - 1;

                if (DocNum > 0)
                {
                    idx = DocNum;
                    TransactionDataHandler handler = new TransactionDataHandler();
                    handler.conn = conn;
                    if (handler.LoadTransaction(DocNum))
                    {
                        DOC_NUM       = handler._DOC_NUM;
                        textBox1.Text = Convert.ToString(handler._DOC_NUM);
                        string driver = dct_drv_licen.SingleOrDefault(r => r.Value == handler._DRV_COD).Key;
                        textBox3.Text  = driver;
                        comboBox1.Text = dct_drivers[driver];

                        string company = dct_companies.SingleOrDefault(r => r.Value == handler._CMP_COD).Key;
                        comboBox2.Text           = company;
                        textBox4.Text            = handler._Destination;
                        comboBox3.Text           = handler._Serv_Name;
                        comboBox4.Text           = handler._Doc_Type_Name;
                        datePicker1.SelectedDate = handler._Doc_Date;
                        textBox5.Text            = string.Format("{0:0.00}", handler._Trans_Value);
                        textBox6.Text            = string.Format("{0:0.00}", handler._Trans_Reduct);
                        textBox7.Text            = string.Format("{0:0.00}", handler._Trans_Final);
                        textBox8.Text            = handler._Voucher_Num;
                        textBox9.Text            = Convert.ToString(handler._No_Pax);
                        textBox10.Text           = handler._Comments;

                        DeleteTransaction.Opacity      = 1;
                        DeleteTransaction.IsEnabled    = true;
                        button2.IsEnabled              = true;
                        CopyTransaction.Opacity        = 1;
                        CopyTransaction.IsEnabled      = true;
                        PrintLastTransaction.Opacity   = 1;
                        PrintLastTransaction.IsEnabled = true;
                        this.Title         = "Edit Transaction";
                        label16.Visibility = Visibility.Hidden;
                        if (!handler.isValid)
                        {
                            label16.Visibility             = Visibility.Visible;
                            DeleteTransaction.Opacity      = 0.4;
                            DeleteTransaction.IsEnabled    = false;
                            button2.IsEnabled              = false;
                            CopyTransaction.Opacity        = 1;
                            CopyTransaction.IsEnabled      = true;
                            button1.IsEnabled              = false;
                            PrintLastTransaction.IsEnabled = false;
                            PrintLastTransaction.Opacity   = 0.4;
                            SaveTransaction.IsEnabled      = false;
                            SaveTransaction.Opacity        = 0.4;
                            CancelTransaction.IsEnabled    = false;
                            CancelTransaction.Opacity      = 0.4;
                        }

                        if (mode.Equals(Mode.MODE_VIEWRO))
                        {
                            MakeFormRO(true);
                            this.Title = "View Transaction";
                            if (!handler.isValid)
                            {
                                button2.IsEnabled           = false;
                                DeleteTransaction.IsEnabled = false;
                                DeleteTransaction.Opacity   = 0.4;
                            }
                        }
                    }
                }
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                label16.Visibility = Visibility.Hidden;
                DOC_NUM            = null;
                SqlCeDataReader myReader = null;
                SqlCeCommand    command  = new SqlCeCommand("select UniqueValue from UniqueNumbers Where UniqueName='Receipt'", conn);
                myReader = command.ExecuteReader();
                if (myReader.Read())
                {
                    textBox1.Text = myReader["UniqueValue"].ToString();
                    textBox2.Text = DateTime.Today.ToShortDateString();
                }
                myReader.Close();
                myReader.Dispose();

                command  = new SqlCeCommand("select DRV_COD, DRV_LICENSE, DRV_NAME from Driver", conn);
                myReader = command.ExecuteReader();
                while (myReader.Read())
                {
                    int    Code    = Convert.ToInt32(myReader["DRV_COD"].ToString());
                    string license = myReader["DRV_LICENSE"].ToString();
                    string name    = myReader["DRV_NAME"].ToString();
                    if (!dct_drivers.ContainsKey(license))
                    {
                        dct_drivers.Add(license, name);
                    }
                    if (!drivers.Contains(name))
                    {
                        drivers.Add(name);
                    }
                    if (!dct_drv_licen.ContainsKey(license))
                    {
                        dct_drv_licen.Add(license, Code);
                    }
                }
                myReader.Close();
                myReader.Dispose();

                command  = new SqlCeCommand("select CMP_COD, CMP_NAME from Company", conn);
                myReader = command.ExecuteReader();
                while (myReader.Read())
                {
                    int    Code = Convert.ToInt32(myReader["CMP_COD"].ToString());
                    string name = myReader["CMP_NAME"].ToString();
                    if (!companies.Contains(name))
                    {
                        companies.Add(name);
                    }
                    if (!dct_companies.ContainsKey(name))
                    {
                        dct_companies.Add(name, Code);
                    }
                }
                myReader.Close();
                myReader.Dispose();

                command  = new SqlCeCommand("select SERV_NAME from Service_Type", conn);
                myReader = command.ExecuteReader();
                while (myReader.Read())
                {
                    string name = myReader["SERV_NAME"].ToString();
                    if (!servicetype.Contains(name))
                    {
                        servicetype.Add(name);
                    }
                }
                myReader.Close();
                myReader.Dispose();

                command  = new SqlCeCommand("select DOC_TYPE_NAME from Document_Type", conn);
                myReader = command.ExecuteReader();
                while (myReader.Read())
                {
                    string name = myReader["DOC_TYPE_NAME"].ToString();
                    if (!documenttype.Contains(name))
                    {
                        documenttype.Add(name);
                    }
                }
                myReader.Close();
                myReader.Dispose();

                comboBox1.ItemsSource = drivers;
                comboBox2.ItemsSource = companies;
                comboBox3.ItemsSource = servicetype;
                comboBox4.ItemsSource = documenttype;

                if (Mode.MODE_CREATE.Equals(mode))
                {
                    datePicker1.DisplayDate  = DateTime.Today;
                    datePicker1.SelectedDate = DateTime.Today;
                    textBox5.Text            = "0.00";
                    textBox6.Text            = "0.00";
                    textBox7.Text            = "0.00";
                    textBox9.Text            = "0";

                    DeleteTransaction.IsEnabled    = false;
                    DeleteTransaction.Opacity      = 0.4;
                    CopyTransaction.IsEnabled      = false;
                    CopyTransaction.Opacity        = 0.4;
                    PrintLastTransaction.IsEnabled = false;
                    PrintLastTransaction.Opacity   = 0.4;
                    this.Title         = "Add Transaction";
                    label16.Visibility = Visibility.Hidden;
                }
                else if (Mode.MODE_VIEWRO.Equals(mode))
                {
                    if (idx != null)
                    {
                        MakeFormRO(true);
                        TransactionDataHandler handler = new TransactionDataHandler();
                        handler.conn = conn;
                        if (!handler.LoadTransaction(idx.Value))
                        {
                            Config.ShowErrorMessage(string.Format("Failed to load the data associated to document number: {0}", idx.Value));
                            this.Close();
                        }
                        else
                        {
                            DOC_NUM       = handler._DOC_NUM;
                            textBox1.Text = Convert.ToString(handler._DOC_NUM);
                            string driver = dct_drv_licen.SingleOrDefault(r => r.Value == handler._DRV_COD).Key;
                            textBox3.Text  = driver;
                            comboBox1.Text = dct_drivers[driver];

                            string company = dct_companies.SingleOrDefault(r => r.Value == handler._CMP_COD).Key;
                            comboBox2.Text           = company;
                            textBox4.Text            = handler._Destination;
                            comboBox3.Text           = handler._Serv_Name;
                            comboBox4.Text           = handler._Doc_Type_Name;
                            datePicker1.SelectedDate = handler._Doc_Date;
                            textBox5.Text            = string.Format("{0:0.00}", handler._Trans_Value);
                            textBox6.Text            = string.Format("{0:0.00}", handler._Trans_Reduct);
                            textBox7.Text            = string.Format("{0:0.00}", handler._Trans_Final);
                            textBox8.Text            = handler._Voucher_Num;
                            textBox9.Text            = Convert.ToString(handler._No_Pax);
                            textBox10.Text           = handler._Comments;
                            if (!handler.isValid)
                            {
                                label16.Visibility          = Visibility.Visible;
                                DeleteTransaction.Opacity   = 0.4;
                                DeleteTransaction.IsEnabled = false;
                                button2.IsEnabled           = false;
                            }
                            this.Title = "View Transaction";
                        }
                    }
                    else
                    {
                        datePicker1.DisplayDate  = DateTime.Today;
                        datePicker1.SelectedDate = DateTime.Today;
                        textBox5.Text            = "0.00";
                        textBox6.Text            = "0.00";
                        textBox7.Text            = "0.00";
                        textBox9.Text            = "0";

                        DeleteTransaction.IsEnabled    = false;
                        DeleteTransaction.Opacity      = 0.4;
                        CopyTransaction.IsEnabled      = false;
                        CopyTransaction.Opacity        = 0.4;
                        PrintLastTransaction.IsEnabled = false;
                        PrintLastTransaction.Opacity   = 0.4;
                        this.Title         = "Add Transaction";
                        mode               = Mode.MODE_CREATE;
                        label16.Visibility = Visibility.Hidden;
                    }
                }
                else if (Mode.MODE_UPDATE.Equals(mode))
                {
                    if (idx != null)
                    {
                        //MakeFormRO(true);
                        TransactionDataHandler handler = new TransactionDataHandler();
                        handler.conn = conn;
                        if (!handler.LoadTransaction(idx.Value))
                        {
                            Config.ShowErrorMessage(string.Format("Failed to load the data associated to document number: {0}", idx.Value));
                            this.Close();
                        }
                        else
                        {
                            DOC_NUM       = handler._DOC_NUM;
                            textBox1.Text = Convert.ToString(handler._DOC_NUM);
                            string driver = dct_drv_licen.SingleOrDefault(r => r.Value == handler._DRV_COD).Key;
                            textBox3.Text  = driver;
                            comboBox1.Text = dct_drivers[driver];

                            string company = dct_companies.SingleOrDefault(r => r.Value == handler._CMP_COD).Key;
                            comboBox2.Text           = company;
                            textBox4.Text            = handler._Destination;
                            comboBox3.Text           = handler._Serv_Name;
                            comboBox4.Text           = handler._Doc_Type_Name;
                            datePicker1.SelectedDate = handler._Doc_Date;
                            textBox5.Text            = string.Format("{0:0.00}", handler._Trans_Value);
                            textBox6.Text            = string.Format("{0:0.00}", handler._Trans_Reduct);
                            textBox7.Text            = string.Format("{0:0.00}", handler._Trans_Final);
                            textBox8.Text            = handler._Voucher_Num;
                            textBox9.Text            = Convert.ToString(handler._No_Pax);
                            textBox10.Text           = handler._Comments;
                            if (!handler.isValid)
                            {
                                label16.Visibility             = Visibility.Visible;
                                DeleteTransaction.Opacity      = 0.4;
                                DeleteTransaction.IsEnabled    = false;
                                button2.IsEnabled              = false;
                                button1.IsEnabled              = false;
                                PrintLastTransaction.IsEnabled = false;
                                PrintLastTransaction.Opacity   = 0.4;
                                CopyTransaction.IsEnabled      = true;
                                CopyTransaction.Opacity        = 0.4;
                                SaveTransaction.IsEnabled      = false;
                                SaveTransaction.Opacity        = 0.4;
                                CancelTransaction.IsEnabled    = false;
                                CancelTransaction.Opacity      = 0.4;
                            }
                            this.Title = "Edit Transaction";
                        }
                    }
                }
                else if (Mode.MODE_VOID.Equals(mode))
                {
                    if (idx != null)
                    {
                        MakeFormRO(true);
                        TransactionDataHandler handler = new TransactionDataHandler();
                        handler.conn = conn;
                        if (!handler.LoadTransaction(idx.Value))
                        {
                            Config.ShowErrorMessage(string.Format("Failed to load the data associated to document number: {0}", idx.Value));
                            this.Close();
                        }
                        else
                        {
                            DOC_NUM       = handler._DOC_NUM;
                            textBox1.Text = Convert.ToString(handler._DOC_NUM);
                            string driver = dct_drv_licen.SingleOrDefault(r => r.Value == handler._DRV_COD).Key;
                            textBox3.Text  = driver;
                            comboBox1.Text = dct_drivers[driver];

                            string company = dct_companies.SingleOrDefault(r => r.Value == handler._CMP_COD).Key;
                            comboBox2.Text           = company;
                            textBox4.Text            = handler._Destination;
                            comboBox3.Text           = handler._Serv_Name;
                            comboBox4.Text           = handler._Doc_Type_Name;
                            datePicker1.SelectedDate = handler._Doc_Date;
                            textBox5.Text            = string.Format("{0:0.00}", handler._Trans_Value);
                            textBox6.Text            = string.Format("{0:0.00}", handler._Trans_Reduct);
                            textBox7.Text            = string.Format("{0:0.00}", handler._Trans_Final);
                            textBox8.Text            = handler._Voucher_Num;
                            textBox9.Text            = Convert.ToString(handler._No_Pax);
                            textBox10.Text           = handler._Comments;
                            if (!handler.isValid)
                            {
                                label16.Visibility          = Visibility.Visible;
                                DeleteTransaction.Opacity   = 0.4;
                                DeleteTransaction.IsEnabled = false;
                                button2.IsEnabled           = false;
                            }
                            this.Title = "Void Transaction";
                        }
                        VoidTrans();
                        this.Close();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else if (Mode.MODE_COPYC.Equals(mode))
                {
                    if (idx != null)
                    {
                        TransactionDataHandler handler = new TransactionDataHandler();
                        handler.conn = conn;
                        if (!handler.LoadTransaction(idx.Value))
                        {
                            Config.ShowErrorMessage(string.Format("Failed to load the data associated to document number: {0}", idx.Value));
                            this.Close();
                        }
                        else
                        {
                            int?dn = handler.GetNewDocNum();
                            if (dn != null)
                            {
                                DOC_NUM       = handler._DOC_NUM;
                                textBox1.Text = Convert.ToString(dn.Value);
                                string driver = dct_drv_licen.SingleOrDefault(r => r.Value == handler._DRV_COD).Key;
                                textBox3.Text  = driver;
                                comboBox1.Text = dct_drivers[driver];

                                string company = dct_companies.SingleOrDefault(r => r.Value == handler._CMP_COD).Key;
                                comboBox2.Text           = company;
                                textBox4.Text            = handler._Destination;
                                comboBox3.Text           = handler._Serv_Name;
                                comboBox4.Text           = handler._Doc_Type_Name;
                                datePicker1.SelectedDate = DateTime.Today;
                                datePicker1.DisplayDate  = DateTime.Today;
                                textBox5.Text            = string.Format("{0:0.00}", handler._Trans_Value);
                                textBox6.Text            = string.Format("{0:0.00}", handler._Trans_Reduct);
                                textBox7.Text            = string.Format("{0:0.00}", handler._Trans_Final);
                                //textBox8.Text = handler._Voucher_Num;
                                textBox9.Text               = Convert.ToString(handler._No_Pax);
                                textBox10.Text              = handler._Comments;
                                this.Title                  = "Copy Transaction";
                                CopyTransaction.IsEnabled   = false;
                                CopyTransaction.Opacity     = 0.4;
                                DeleteTransaction.IsEnabled = false;
                                DeleteTransaction.Opacity   = 0.4;
                            }
                            else
                            {
                                Config.ShowErrorMessage(string.Format("Failed to load the data associated to document number: {0}", idx.Value));
                                this.Close();
                            }
                        }
                    }
                    else
                    {
                        datePicker1.DisplayDate  = DateTime.Today;
                        datePicker1.SelectedDate = DateTime.Today;
                        textBox5.Text            = "0.00";
                        textBox6.Text            = "0.00";
                        textBox7.Text            = "0.00";
                        textBox9.Text            = "0";

                        DeleteTransaction.IsEnabled    = false;
                        DeleteTransaction.Opacity      = 0.4;
                        CopyTransaction.IsEnabled      = false;
                        CopyTransaction.Opacity        = 0.4;
                        PrintLastTransaction.IsEnabled = false;
                        PrintLastTransaction.Opacity   = 0.4;
                        this.Title = "Add Transaction";
                        mode       = Mode.MODE_CREATE;
                    }
                }
            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                Config.FatalError();
            }
        }