public MainPage(UIElement baseControl, string username, string password, string userrole, string employeeID)
        {
            InitializeComponent();
            page_container      = Container;
            entitlementsViewer  = new EntitlementsViewer(this);
            entitlementsAdder   = new EntitlementsAdder(this);
            leaveAssignmentPage = new LeaveAssignmentPage(this);
            sendApplication     = new SendApplication(this);
            viewApplication     = new ViewApplication(this);
            jobStack            = new Stack <Page>( );
            this.baseControl    = baseControl;
            this.username       = username;
            this.password       = password;
            this.userrole       = userrole;
            this.employeeID     = employeeID;
            this.userIsLoggedIn = true;
            MainWindow mainWindow = baseControl as MainWindow;

            this.Height = mainWindow.Frame.Height;
            this.Width  = mainWindow.Frame.Width;
            (baseControl as MainWindow).Frame.NavigationUIVisibility = NavigationUIVisibility.Visible;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainPage     mainPage = baseControl as MainPage;
            Stack <Page> jobStack = mainPage.JobStack;

            if (lb_message.Visibility == System.Windows.Visibility.Visible)
            {
                MessageBox.Show(lb_message.ToString( ), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (lb_message2.Visibility == System.Windows.Visibility.Visible)
            {
                MessageBox.Show(lb_message2.ToString( ), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (icb_employees.Text.Length == 0 || icb_leaveTypes.Text.Length == 0 || tb_balance.Text.Length == 0 || icb_validFrom.Text.Length == 0 || icb_validTo.Text.Length == 0)
            {
                MessageBox.Show("Please enter the required fields.");
                return;
            }
            if (icb_employeeID.Text.Length == 0)
            {
                MessageBox.Show("No employee named '" + icb_employees.Text + "' found!", "Invalid name", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            StringBuilder values  = new StringBuilder(icb_employeeID.Text + ", '" + icb_leaveTypes.Text + "', '" + icb_validFrom + "', '" + icb_validTo + "', " + tb_balance.Text);
            StringBuilder columns = new StringBuilder("EmployeeID, LeaveType, ValidFrom, ValidTo, Balance");

            if (icb_entitlementType.Text.Length != 0)
            {
                values.Append(", '" + icb_entitlementType.Text + "'");
                columns.Append(", EntitlementType");
            }
            try     {
                Entitlements.AddEntitlementInfo(values.ToString( ), columns.ToString( ));
                MessageBox.Show("Entitlements added successfully!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            }       catch (SqlException exception)   {
                if (exception.Message.ToLower( ).Contains("primary key"))
                {
                    if (MessageBox.Show("An entitlement of type " + icb_leaveTypes.Text + " already exists for " + icb_employees.Text + ".\nDo you want to update the balance?", "Entitlements exists", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                    {
                        EntitlementsViewer entitlementsViewer = mainPage.EntitlementsViewer;
                        mainPage.Container.Navigate(entitlementsViewer);
                        entitlementsViewer.icb_employees.Text       = icb_employees.Text;
                        entitlementsViewer.icb_employeeID.Text      = icb_employeeID.Text;
                        entitlementsViewer.icb_entitlementType.Text = icb_entitlementType.Text;
                        entitlementsViewer.icb_validFrom.Text       = icb_validFrom.Text;
                        entitlementsViewer.icb_validTo.Text         = icb_validTo.Text;
                        entitlementsViewer.tb_balance.Text          = tb_balance.Text;
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("Entitlements could not be added.\nAdditional information: " + exception.Message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            if (jobStack.Count > 0)
            {
                mainPage.Container.Navigate(jobStack.Pop( ));
            }
        }
Example #3
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainPage     mainPage = baseControl as MainPage;
            Stack <Page> jobStack = mainPage.JobStack;

            if (icb_employeeID.Text.Length == 0 || icb_leaveTypes.Text.Length == 0 || dp_leavingDate.SelectedDate == null || dp_joiningDate.SelectedDate == null)
            {
                MessageBox.Show("Please fill in the required fields.", "Incomplete", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                return;
            }
            if (lb_message.Visibility == System.Windows.Visibility.Visible)
            {
                MessageBox.Show(lb_message.Content.ToString( ) + "!", "Invalid Interval", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (dp_leavingDate.SelectedDate.Value < DateTime.Today)
            {
                MessageBox.Show("Leaving date cannot be a past date.", "Date Past", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            DataTable data               = LeavePeriods.GetLeavePeriods( );
            string    startingDate       = "";
            string    endingDate         = "";
            bool      leavingDateIsValid = false;
            bool      joiningDateIsValid = false;

            foreach (DataRow row in data.Rows)
            {
                if (dp_leavingDate.SelectedDate.Value >= new DateString(row[0]))
                {
                    startingDate       = row[0].ToString( );
                    leavingDateIsValid = true;
                    break;
                }
            }
            foreach (DataRow row in data.Rows)
            {
                if (dp_joiningDate.SelectedDate.Value.AddDays(-1) <= new DateString(row[1]))
                {
                    endingDate         = row[1].ToString( );
                    joiningDateIsValid = true;
                    break;
                }
            }
            if (!leavingDateIsValid || !joiningDateIsValid)
            {
                MessageBox.Show("The leave period is not valid. Please enter a valid one or reconfigure.", "Invalid Leave Period", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            if (lb_balance.Content.ToString( ) == "!!")
            {
                MessageBox.Show("The interval you entered exceeds the bounds!", "Too Big Interval", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (lb_balance.Content.ToString( ).Length == 0)
            {
                if (MessageBox.Show("No balance available. Do you wish to entitle?", "No Balance", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                {
                    EntitlementsAdder entitlementsAdder = mainPage.EntitlementsAdder;
                    jobStack.Push(this);
                    mainPage.Container.Navigate(entitlementsAdder);
                    entitlementsAdder.icb_employees.Text  = icb_employees.Text;
                    entitlementsAdder.icb_employeeID.Text = icb_employeeID.Text;
                    entitlementsAdder.icb_leaveTypes.Text = icb_leaveTypes.Text;
                    entitlementsAdder.icb_validFrom.Text  = startingDate;
                    entitlementsAdder.icb_validTo.Text    = endingDate;
                }
                else
                {
                    MessageBox.Show("Leave Not Assigned!", "Failure", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                return;
            }
            if (double.Parse(lb_balance.Content.ToString( )) < 0)
            {
                MessageBoxResult result = MessageBox.Show("Balance is insufficient! Do you wish to update entitlement?", "Insufficient Balance", MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);
                if (result != MessageBoxResult.No)
                {
                    if (result == MessageBoxResult.Yes)
                    {
                        EntitlementsViewer entitlementsViewer = mainPage.EntitlementsViewer;
                        mainPage.JobStack.Push(this);
                        mainPage.Container.Navigate(entitlementsViewer);
                        entitlementsViewer.icb_employeeID.Text = icb_employeeID.Text;
                        entitlementsViewer.icb_employees.Text  = icb_employees.Text;
                        entitlementsViewer.icb_leaveTypes.Text = icb_leaveTypes.Text;
                        entitlementsViewer.icb_validFrom.Text  = startingDate;
                        entitlementsViewer.icb_validTo.Text    = endingDate;
                    }
                    return;
                }
            }
            string        values          = icb_employeeID.Text + ", '" + icb_leaveTypes.Text + "', '" + new DateString(dp_leavingDate.SelectedDate.Value) + "', '" + new DateString(dp_joiningDate.SelectedDate.Value) + "', " + lostBalance + ", '" + tb_commentBox.Text.Replace("'", "''") + "'";
            string        changes         = "Balance = " + lb_balance.Content;
            StringBuilder rowConstraints  = new StringBuilder("EmployeeID = " + icb_employeeID.Text + " AND LeaveType = '" + icb_leaveTypes.Text + "'");
            StringBuilder dateConstraints = new StringBuilder( );

            try     {
                Entitlements.UpdateEntitlementInfo(changes, rowConstraints.ToString( ));
                LeaveList.AssignLeave(values);
                data = LeaveApplications.FetchDetails("LeavingDate, JoiningDate", rowConstraints.ToString( ));
                foreach (DataRow row in data.Rows)
                {
                    if (new DateString(row[0].ToString( )) >= dp_leavingDate.SelectedDate && new DateString(row[1].ToString( )) <= dp_joiningDate.SelectedDate)
                    {
                        dateConstraints.Append("LeavingDate = '" + row[0] + "' AND JoiningDate = '" + row[1] + "' OR ");
                    }
                }
                dateConstraints.Append("1 = 2");
                rowConstraints.Append(" AND (" + dateConstraints.ToString( ) + ")");
                LeaveApplications.RejectApplications(rowConstraints.ToString( ));
                MessageBox.Show("Leave Assigned Successfully!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            }       catch (System.Data.SqlClient.SqlException exception)     {
                MessageBox.Show(exception.Message);
            }
            if (jobStack.Count > 0)
            {
                mainPage.Container.Navigate(jobStack.Pop( ));
            }
        }