Esempio n. 1
0
        // Timer tick
        private void timer_Tick(object sender, EventArgs e)
        {
            seconds++;
            if (seconds == 60)
            {
                minutes++;
                if (minutes == 60)
                {
                    minutes = 0;
                    hours++;
                }
                seconds = 0;
            }

            currentTime_lb.Text = (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
            if (current_entry.hourly_rate > 0)
            {
                entryEarnings_lb.Text = Convert.ToString(current_entry.CalculateEarnings()) + current_entry.currency;
            }
            else
            {
                entryEarnings_lb.Text = "No hourly rate set";
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds one entry to FlowLayoutPanel.
        /// </summary>
        /// <param name="entry">Entry to be added</param>
        private void AddEntry(Entry entry)
        {
            // Make card to hold the entry
            Panel card = new Panel();

            card.Location    = new Point(entriesWrap_panel.Padding.Left, card.Location.Y);
            card.Size        = new Size(entriesWrap_panel.Width - 24, 130);
            card.Padding     = new Padding(10);
            card.BorderStyle = BorderStyle.FixedSingle;
            card.Margin      = new Padding(3, 5, 3, 5);
            card.BackColor   = Color.WhiteSmoke;

            // Start time
            Label lb1 = new Label();

            lb1.Text     = "Start time: ";
            lb1.Location = new Point(card.Padding.Left, card.Padding.Top);
            card.Controls.Add(lb1);

            DateTimePicker start_time = new DateTimePicker();

            start_time.Value        = entry.start_time;
            start_time.Location     = CalculateUnderLabelPos(lb1);
            start_time.Format       = DateTimePickerFormat.Custom;
            start_time.CustomFormat = "hh:mm:ss, dd/MMM/yyyy";
            card.Controls.Add(start_time);

            // End time
            Label lb2 = new Label();

            lb2.Text     = "End time: ";
            lb2.Location = new Point(start_time.Location.X + start_time.Width + 15, lb1.Location.Y);
            card.Controls.Add(lb2);

            DateTimePicker end_time = new DateTimePicker();

            end_time.Value        = (DateTime)entry.end_time;
            end_time.Location     = CalculateUnderLabelPos(lb2);
            end_time.Format       = DateTimePickerFormat.Custom;
            end_time.CustomFormat = "hh:mm:ss, dd/MMM/yyyy";
            card.Controls.Add(end_time);

            // Duration in hours
            Label lb3 = new Label();

            lb3.Text     = "Duration in hours:";
            lb3.Location = new Point(end_time.Location.X + end_time.Width + 15, lb2.Location.Y);
            card.Controls.Add(lb3);

            Label duration_lb = new Label();

            duration_lb.AutoSize = true;
            duration_lb.Text     = Math.Round(((DateTime)entry.end_time).Subtract(entry.start_time).TotalHours, 2).ToString("F2");
            duration_lb.Location = CalculateUnderLabelPos(lb3);
            card.Controls.Add(duration_lb);

            // Description
            Label lb4 = new Label();

            lb4.Text     = "Description:";
            lb4.Location = new Point(lb3.Location.X + lb3.Width + 15, lb3.Location.Y);
            card.Controls.Add(lb4);

            TextBox descUpdate_tb = new TextBox();

            descUpdate_tb.MaxLength = 50;
            descUpdate_tb.Width     = 170;
            if (entry.description.Trim() != "")
            {
                descUpdate_tb.Text = entry.description;
            }
            descUpdate_tb.Location = CalculateUnderLabelPos(lb4);
            card.Controls.Add(descUpdate_tb);

            // Project combobox / hourly rate / currency info (below start time datetimepicker control)
            // -- Current entry project
            Project entryProject = entry.Project();

            // -- Project update combobox
            Label pucb_lb = new Label();

            pucb_lb.Text     = "Project:";
            pucb_lb.Location = new Point(start_time.Location.X, start_time.Location.Y + start_time.Height + 15);
            card.Controls.Add(pucb_lb);

            ComboBox projectUpdate_cb = new ComboBox();

            projectUpdate_cb.Width = 200;
            List <Project> all_projects = (new ProjectsController()).GetAllProjects();

            projectUpdate_cb.Items.Add("");
            for (int i = 0; i < all_projects.Count; i++)
            {
                projectUpdate_cb.Items.Add(all_projects[i].project_name);
                if (entryProject != null && all_projects[i].project_name == entryProject.project_name)
                {
                    projectUpdate_cb.SelectedIndex = i + 1;
                }
            }
            projectUpdate_cb.Location = CalculateUnderLabelPos(pucb_lb);
            card.Controls.Add(projectUpdate_cb);

            // -- Hourly rate numeric up and down
            Label hru_lb = new Label();

            hru_lb.Text     = "Hourly rate:";
            hru_lb.Location = new Point(projectUpdate_cb.Location.X + projectUpdate_cb.Width + 15, pucb_lb.Location.Y);
            card.Controls.Add(hru_lb);

            NumericUpDown hourlyRateUpdate_nud = new NumericUpDown();

            hourlyRateUpdate_nud.Maximum  = 100000;
            hourlyRateUpdate_nud.Location = CalculateUnderLabelPos(hru_lb);
            hourlyRateUpdate_nud.Value    = entry.hourly_rate;
            card.Controls.Add(hourlyRateUpdate_nud);

            // -- Currency textbox
            Label curr_lb = new Label();

            curr_lb.Text     = "Currency:";
            curr_lb.Location = new Point(hourlyRateUpdate_nud.Location.X + hourlyRateUpdate_nud.Width + 15, hru_lb.Location.Y);
            card.Controls.Add(curr_lb);

            TextBox currencyUpdate_tb = new TextBox();

            if (entry.currency != null)
            {
                currencyUpdate_tb.Text = entry.currency;
            }
            currencyUpdate_tb.Location = CalculateUnderLabelPos(curr_lb);
            card.Controls.Add(currencyUpdate_tb);

            // -- Earnings
            Label elb = new Label();

            elb.Location = new Point(currencyUpdate_tb.Location.X + currencyUpdate_tb.Width + 15, curr_lb.Location.Y);
            elb.Text     = "Earnings:";
            card.Controls.Add(elb);

            Label earnings_lb = new Label();

            earnings_lb.Location = CalculateUnderLabelPos(elb);
            if (entry.hourly_rate > 0)
            {
                earnings_lb.Text = entry.CalculateEarnings().ToString() + entry.currency;
            }
            else
            {
                earnings_lb.Text = "No hourly rate set";
            }
            card.Controls.Add(earnings_lb);

            // Delete entry button
            Button deleteEntry_btn = new Button();

            deleteEntry_btn.Text     = "Delete";
            deleteEntry_btn.Location = new Point(card.Width - deleteEntry_btn.Width - 15, projectUpdate_cb.Location.Y);
            deleteEntry_btn.Click   += new EventHandler(delegate(Object o, EventArgs a)
            {
                DialogResult diag = MessageBox.Show("This will delete the selected entry.", "Are you sure?", MessageBoxButtons.YesNo);
                if (diag == DialogResult.Yes)
                {
                    card.Dispose();
                    entry.Delete();
                }
            });
            card.Controls.Add(deleteEntry_btn);

            // Update entry button
            Button updateEntry_btn = new Button();

            updateEntry_btn.Text     = "Update";
            updateEntry_btn.Location = new Point(deleteEntry_btn.Location.X - updateEntry_btn.Width - 10, deleteEntry_btn.Location.Y);
            updateEntry_btn.Click   += new EventHandler(delegate(Object o, EventArgs a)
            {
                // Update entry object
                bool originalRateRemoved = false;
                entry.start_time         = start_time.Value;
                entry.end_time           = end_time.Value;
                entry.description        = Helper.TrimToLen(descUpdate_tb.Text, 50);
                int hr_rate = Convert.ToInt32(hourlyRateUpdate_nud.Value);
                if (hr_rate > 0 && currencyUpdate_tb.Text.Trim() != "")
                {
                    entry.hourly_rate = hr_rate;
                    entry.currency    = Helper.TrimToLen(currencyUpdate_tb.Text, 50);
                }
                else if (hr_rate == 0)
                {
                    entry.hourly_rate   = hr_rate;
                    entry.currency      = null;
                    originalRateRemoved = true;
                }
                else if (hr_rate > 0 && currencyUpdate_tb.Text.Trim() == "")
                {
                    MessageBox.Show("Please select currency too");
                }

                // Find project by name
                bool projectRateApplied = false;
                string project_name     = projectUpdate_cb.SelectedItem != null && projectUpdate_cb.SelectedItem.ToString().Trim() != "" ? projectUpdate_cb.SelectedItem.ToString() : "";
                Project found           = (new ProjectsController()).FindProjectByName(project_name);
                if (found != null)
                {
                    if ((entry.project_id > 0 && entry.project_id != found.id) || entry.project_id == 0)
                    {
                        // Apply new project to entry
                        entry.project_id = found.id;

                        // Apply projects' hourly rate and currency to entry if user agrees
                        DialogResult dg = MessageBox.Show("Do you want to apply project's hourly rate and currency to this entry?", "Confirm", MessageBoxButtons.YesNo);
                        if (dg == DialogResult.Yes)
                        {
                            if (found.hourly_rate > 0)
                            {
                                projectRateApplied = true;
                                entry.hourly_rate  = found.hourly_rate;
                                if (found.currency != null && found.currency != "")
                                {
                                    entry.currency = Helper.TrimToLen(found.currency, 50);
                                }
                                else
                                {
                                    entry.currency = (new SettingsController()).GetSetting("currency").value;
                                }
                            }
                        }
                    }
                }
                else
                {
                    entry.project_id = 0;
                }

                // If rate is removed from entry
                if (!projectRateApplied && originalRateRemoved)
                {
                    DialogResult dg = MessageBox.Show("This entry will be left without an horly rate and currency, so it will not be charged. Do you want to apply hourly rate/currency from global settings?", "Warning", MessageBoxButtons.YesNo);
                    if (dg == DialogResult.Yes)
                    {
                        SettingsController settings = new SettingsController();
                        entry.hourly_rate           = Convert.ToInt32(settings.GetSetting("hourly_rate").value);
                        entry.currency = settings.GetSetting("currency").value;
                    }
                }

                // Update entry
                entry.Save();

                // Update UI back
                hourlyRateUpdate_nud.Value = entry.hourly_rate;
                currencyUpdate_tb.Text     = entry.currency != null ? entry.currency : "";
                if (entry.hourly_rate > 0)
                {
                    earnings_lb.Text = entry.CalculateEarnings().ToString() + entry.currency;
                }
                else
                {
                    earnings_lb.Text = "No hourly rate set";
                }

                duration_lb.Text = Math.Round(((DateTime)entry.end_time).Subtract(entry.start_time).TotalHours, 2).ToString("F2");

                // Notify user about the update
                MessageBox.Show("Entry updated");
            });
            card.Controls.Add(updateEntry_btn);

            // Add card to flowlayourpanel
            entriesWrap_panel.Controls.Add(card);
            entriesWrap_panel.Controls.SetChildIndex(card, 0); // Add new card to top
        }