Diplom_O/SupportForms/WorkerForm.cs

170 lines
4.1 KiB
C#
Raw Normal View History

2024-09-20 08:53:52 +05:00
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.SupportForms
{
public partial class WorkerForm : Form
{
private Chel chel = null;
private void ResetLinkFIO()
{
if (chel == null)
{
linkSelectFIO.Text = "Выбрать человека...";
return;
}
var res = chel.FName.ToString() + " " +
chel.SName[0].ToString() + "." +
((!string.IsNullOrEmpty(chel.TName)) ?
chel.TName[0].ToString() + "." : "");
linkSelectFIO.Text = res;
}
private int? shtatId = null;
private void ResetLinkShtat()
{
if (shtatId == null)
{
linkDolj.Text = "Выбрать должность...";
return;
}
var res = FuncDB.ShtatGetById(shtatId.Value)?.Doljnost;
linkDolj.Text = res;
}
public bool isCanceled = false;
private Rabotnik rabotnik = null;
public WorkerForm(Rabotnik rabotnik = null, bool create = false)
{
InitializeComponent();
try
{
ResetLinkFIO();
ResetLinkShtat();
tabNumBox.Text = "";
dateTimePicker1.Value = DateTime.Now;
dateTimePicker2.Value = DateTime.Now;
if (rabotnik == null) return;
chel = FuncDB.ChelGetById(rabotnik.ChelId);
ResetLinkFIO();
linkSelectFIO.Enabled = false;
if (create)
return;
this.rabotnik = rabotnik;
workButton.Text = "Изменить";
chel = FuncDB.ChelGetById(rabotnik.ChelId);
ResetLinkFIO();
if (checkBox2.Checked)
shtatId = null;
else
shtatId = FuncDB.ShtatGetById(rabotnik.ShtatId.Value).Id;
ResetLinkShtat();
tabNumBox.Text = rabotnik.TabNum;
dateTimePicker1.Value = rabotnik.Start;
if (rabotnik.End.HasValue)
{
checkBox1.Checked = false;
dateTimePicker2.Value = rabotnik.End.Value;
}
else
checkBox1.Checked = true;
}
catch (Exception e)
{
this.ShowError(errorLabel, e.Message);
workButton.Enabled = false;
}
}
private void linkSelectFIO_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var form = new ChelForm(true);
form.ShowDialog();
if (form.isCanceled)
chel = null;
else
chel = form.formResult;
ResetLinkFIO();
}
private void linkDolj_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var form = new ShtatForm(true);
form.ShowDialog();
if (form.isCanceled)
shtatId = null;
else
shtatId = form.formResult.Id;
ResetLinkShtat();
}
private void workButton_Click(object sender, EventArgs e)
{
try
{
if (chel == null)
throw new Exception("Не выбран человек.");
if (!checkBox2.Checked && !shtatId.HasValue)
throw new Exception("Не выбрана должность.");
DateTime start = dateTimePicker1.Value;
start = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0);
DateTime? end = null;
if (!checkBox1.Checked)
{
end = dateTimePicker2.Value;
end = new DateTime(end.Value.Year, end.Value.Month, end.Value.Day, 23, 59, 59);
}
var _rabotnik = new Rabotnik()
{
ChelId = chel.Id,
ShtatId = checkBox2.Checked ? null : shtatId,
AnotherWork = checkBox2.Checked,
TabNum = checkBox2.Checked ? "" : tabNumBox.Text,
Start = start,
End = end
};
if (rabotnik == null)
FuncDB.RabotnikAdd(_rabotnik);
else
{
_rabotnik.Id = rabotnik.Id;
FuncDB.RabotnikChange(_rabotnik);
}
this.Close();
}
catch (Exception ex)
{
this.ShowError(errorLabel, ex.Message);
}
}
private void canceledButton_Click(object sender, EventArgs e)
{
isCanceled = true;
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
dateTimePicker2.Enabled = !checkBox1.Checked;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
linkDolj.Enabled = !checkBox2.Checked;
tabNumBox.Enabled = !checkBox2.Checked;
}
}
}