Diplom_O/MainForms/StajForm.cs
2024-09-20 06:53:52 +03:00

299 lines
7.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Diplom_O.DataBase;
namespace Diplom_O
{
public partial class StajForm : Form
{
private readonly ColumnConf[] tableStajColumns = {
new ColumnConf(name: "№"),
new ColumnConf(name: "Названиe", size: 140, warp: true),
new ColumnConf(name: "Лет", size: 60),
new ColumnConf(name: "Месяцев", size: 60),
new ColumnConf(name: "Дней", size: 60)
};
private Rabotnik rabotnik = null;
private void linkRabLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var form = new RabForm(true);
form.ShowDialog();
if (form.isCanceled)
rabotnik = null;
else
rabotnik = form.formResult;
ResetLinkRabotnik();
resetStajTable();
}
private void ResetLinkRabotnik()
{
if (rabotnik == null)
{
linkRabLabel.Text = "Выбрать сотрудника...";
return;
}
var chel = FuncDB.ChelGetById(rabotnik.ChelId);
if (chel == null)
{
linkRabLabel.Text = "Выбрать сотрудника...";
return;
}
var res = chel.FName.ToString() + " " +
chel.SName[0].ToString() + "." +
((!string.IsNullOrEmpty(chel.TName)) ?
chel.TName[0].ToString() + "." : "");
linkRabLabel.Text = res;
}
private void resetStajTable()
{
(int year, int month, int day) Sum(
(int year, int month, int day) t1,
(int year, int month, int day) t2
)
{
var year = t1.year + t2.year;
var month = t1.month + t2.month;
var day = t1.day + t2.day;
month += day / 30;
day %= 30;
year += month / 12;
month %= 12;
return (year, month, day);
}
(int year, int month, int day) Sub(
(int year, int month, int day) t1,
(int year, int month, int day) t2
)
{
var year = t1.year - t2.year;
var month = t1.month - t2.month;
var day = t1.day - t2.day;
while (day < 0)
{
day += 30;
month--;
}
while (month < 0)
{
month += 12;
year--;
}
month += day / 30;
day %= 30;
year += month / 12;
month %= 12;
return (year, month, day);
}
try
{
var res = new List<string[]>();
if (rabotnik == null)
{
this.ResetTable(
stajGridView,
tableStajColumns,
res.ToArray()
);
return;
}
var count = new Dictionary<int, (int year, int month, int day)>();
var listRab = FuncDB.RabotnikHistoryByChelId(rabotnik.ChelId);
foreach (var rab in listRab)
{
var size = FuncDB.RabotnikGetTotalTime(rab, dateTimePicker1.Value);
var staj = FuncDB.RabotnikTypeStajList(rab.Id);
if (count.ContainsKey(-1))
count[-1] = Sum(count[-1], size);
else
count[-1] = size;
foreach(var st in staj)
if (count.ContainsKey(st.TypeStajId))
count[st.TypeStajId] = Sum(count[st.TypeStajId], size);
else
count[st.TypeStajId] = size;
}
var listCorrStaj = FuncDB.CorrStajList(rabotnik.ChelId);
foreach (var corr in listCorrStaj)
{
var ts = FuncDB.TypeStajGetById(corr.TypeStajId);
if (ts != null && ts.Name.CompareTo("Общий") == 0)
corr.TypeStajId = -1;
var size = (corr.Year, corr.Month, corr.Day);
if (!count.ContainsKey(corr.TypeStajId))
count[corr.TypeStajId] = (0, 0, 0);
if (corr.AddFlag)
count[corr.TypeStajId] = Sum(count[corr.TypeStajId], size);
else
count[corr.TypeStajId] = Sub(count[corr.TypeStajId], size);
}
foreach (var r in count)
{
if (r.Value.year < 0 || r.Value.year + r.Value.month + r.Value.day == 0)
continue;
var sr = new List<string>();
sr.Add(r.Key.ToString());
if (r.Key == -1)
sr.Add("Общий");
else
sr.Add(FuncDB.TypeStajGetById(r.Key).Name);
sr.Add(r.Value.year.ToString());
sr.Add(r.Value.month.ToString());
sr.Add(r.Value.day.ToString());
res.Add(sr.ToArray());
}
this.ResetTable(
stajGridView,
tableStajColumns,
res.ToArray()
);
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
}
}
public StajForm()
{
InitializeComponent();
this.correctUpperMenu(menuStrip, 3);
dateTimePicker1.Value = DateTime.Now;
ResetLinkRabotnik();
resetTypeStajComboBox();
linkRabLabel.Enabled = false;
addStajButton.Enabled = false;
var user = this.GetUser();
if (user == null)
return;
linkRabLabel.Enabled =
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 3, 0) != null;
addStajButton.Enabled =
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 3, 2) != null ||
FuncDB.AccessGetByUserIdFormIdAccessId(user.Id, 3, 3) != null;
}
private void resetTypeStajComboBox()
{
try
{
var list = new List<string>();
list.Add("Общий");
list.AddRange(FuncDB.TypeStajListString());
comboBox1.Items.Clear();
comboBox1.Items.AddRange(list.ToArray());
comboBox1.SelectedIndex = -1;
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
}
}
private void upperMenuClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.upperMenuClicked((MenuStrip)sender);
if (this.GetNextForm() != -1)
Close();
}
private void typeStajChanged(object sender, EventArgs e)
{
try
{
addRadioButton.Checked = ! (delRadioButton.Checked = false);
yearTB.Text = monthTB.Text = dayTB.Text = "";
if (rabotnik == null)
return;
var staj = FuncDB.TypeStajGetByName(comboBox1.Text);
if (staj == null)
return;
var corrStaj = FuncDB.CorrStaj(rabotnik.ChelId, staj.Name);
if (corrStaj == null)
return;
yearTB.Text = corrStaj.Year.ToString();
monthTB.Text = corrStaj.Month.ToString();
dayTB.Text = corrStaj.Day.ToString();
delRadioButton.Checked = !(addRadioButton.Checked = corrStaj.AddFlag);
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void addStajButton_Click(object sender, EventArgs e)
{
try
{
if (rabotnik == null)
throw new Exception("Не выбран сотрудник.");
if (!int.TryParse(yearTB.Text, out int year) || year < 0)
throw new Exception("Не верно указан год.");
if (!int.TryParse(monthTB.Text, out int month) || month < 0)
throw new Exception("Не верно указан месяц.");
if (!int.TryParse(dayTB.Text, out int day) || day < 0)
throw new Exception("Не верно указан день.");
var typeStaj = FuncDB.TypeStajGetByName(comboBox1.Text);
if(typeStaj == null)
{
FuncDB.TypeStajAdd(new TypeStaj() { Name = comboBox1.Text });
typeStaj = FuncDB.TypeStajGetByName(comboBox1.Text);
}
var corrStaj = FuncDB.CorrStaj(rabotnik.ChelId, typeStaj.Name);
if (corrStaj == null)
{
FuncDB.CorrStajAdd(new CorrStaj()
{
Year = year,
Month = month,
Day = day,
ChelId = rabotnik.ChelId,
TypeStajId = typeStaj.Id,
AddFlag = addRadioButton.Checked
});
}
else
{
corrStaj.ChelId = rabotnik.ChelId;
corrStaj.TypeStajId = typeStaj.Id;
corrStaj.AddFlag = addRadioButton.Checked;
corrStaj.Year = year;
corrStaj.Month = month;
corrStaj.Day = day;
FuncDB.CorrStajChange(corrStaj);
}
resetStajTable();
resetTypeStajComboBox();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
resetStajTable();
}
}
}