Diplom_B/ZakForm.cs

236 lines
7.2 KiB
C#
Raw Permalink Normal View History

2021-07-16 16:43:55 +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_B.DB;
namespace Diplom_B
{
2021-07-17 09:33:09 +05:00
public partial class ZakForm : Form
2021-07-16 16:43:55 +05:00
{
2021-07-19 14:12:18 +05:00
public int? returnId = null;
2021-07-17 22:32:53 +05:00
private bool needReturn = false;
public ZakForm(bool needReturn = false)
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
InitializeComponent();
try
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
this.needReturn = needReturn;
Init();
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
}
catch { ShowError(); }
2021-07-16 16:43:55 +05:00
}
private void ClearBoxes()
{
idLabel.Text = "";
nameBox.Text = "";
adressBox.Text = "";
phoneBox.Text = "";
emailBox.Text = "";
}
2021-07-17 22:32:53 +05:00
private void UpdateTable(Zakazchik[] arr, bool reset_cursor = false)
2021-07-16 16:43:55 +05:00
{
var selected = (!reset_cursor && zakGridView.SelectedRows.Count > 0) ? zakGridView.SelectedRows[0].Index : -1;
{
var r = zakGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = zakGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = zakGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("Name", "Наименование");
c["Name"].Width = 120;
c.Add("Adress", "Адрес");
c["Adress"].Width = 120;
c["Adress"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
c.Add("Phone", "Телефон");
c["Phone"].Width = 100;
c.Add("Email", "Эл. почта");
c["Email"].Width = 100;
}
{
var r = zakGridView.Rows;
foreach (var zak in arr)
r.Add(new object[] {
zak.Id,
zak.Name,
zak.Adress,
zak.Phone,
zak.Email
});
}
if (zakGridView.Rows.Count > 0)
zakGridView.Rows[0].Selected = true;
if (selected != -1 && selected < zakGridView.Rows.Count)
for (var i = 0; i < zakGridView.Rows.Count; i++)
zakGridView.Rows[i].Selected = (i == selected);
zakGridView_CurrentCellChanged(this, new EventArgs());
}
2021-07-17 22:32:53 +05:00
private void Init()
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
if (Program.user == null) this.Close();
if (this.needReturn)
{
selectButton.Visible = true;
mainMenuStrip.Visible = false;
}
else
{
mainMenuStrip.Items[0].Enabled = Program.user.Usr.Dog > 0;
mainMenuStrip.Items[1].Enabled = Program.user.Usr.Doc > 0;
mainMenuStrip.Items[2].Enabled = Program.user.Usr.Izv > 0;
mainMenuStrip.Items[3].Enabled = Program.user.Usr.Post > 0;
mainMenuStrip.Items[4].Enabled = Program.user.Usr.Izd > 0;
mainMenuStrip.Items[5].Enabled = Program.user.Usr.Zak > 0;
mainMenuStrip.Items[6].Enabled = Program.user.Usr.Set > 0;
mainMenuStrip.Items[5].Enabled = false;
}
{
deleteButton.Enabled = Program.user.Usr.Zak > 2;
createButton.Enabled = Program.user.Usr.Zak > 2;
changeButton.Enabled = Program.user.Usr.Zak > 1;
}
2021-07-16 16:43:55 +05:00
}
2021-07-17 22:32:53 +05:00
private Task errDrop;
private void ShowError(string msg = null)
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
errorLabel.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
errorLabel.Visible = true;
errDrop = new Task(() =>
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
var fd = errDrop.Id;
Task.Delay(5000).Wait();
if (errDrop.Id == fd)
if (InvokeRequired) Invoke((Action)(() => { errorLabel.Visible = false; }));
else errorLabel.Visible = false;
});
errDrop.Start();
2021-07-16 16:43:55 +05:00
}
private Task filterDrop;
private void searchBox_TextChanged(object sender, EventArgs e)
{
filterDrop = new Task(() =>
{
var fd = filterDrop.Id;
Task.Delay(1000).Wait();
if (filterDrop.Id == fd)
2021-07-17 22:32:53 +05:00
if (InvokeRequired) Invoke((Action)(() => { UpdateTable(WorkDB.ListZakazchik(searchBox.Text)); }));
else UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
2021-07-16 16:43:55 +05:00
});
filterDrop.Start();
}
2021-07-17 22:32:53 +05:00
private void zakGridView_CurrentCellChanged(object sender, EventArgs e)
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
ClearBoxes();
if (zakGridView.SelectedRows.Count != 1)
return;
{
var zak = WorkDB.GetZakazchik((int)zakGridView.SelectedRows[0].Cells[0].Value);
if (zak == null)
return;
idLabel.Text = zak.Id.ToString();
nameBox.Text = zak.Name;
adressBox.Text = zak.Adress;
phoneBox.Text = zak.Phone;
emailBox.Text = zak.Email;
}
2021-07-16 16:43:55 +05:00
}
2021-07-17 22:32:53 +05:00
private void createButton_Click(object sender, EventArgs e)
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
if (string.IsNullOrEmpty(nameBox.Text)) { ShowError("Нет названия."); return; }
if (WorkDB.GetZakazchikList().Contains(nameBox.Text)) { ShowError("Заказчик существует."); return; }
2021-07-16 16:43:55 +05:00
try
{
2021-07-17 22:32:53 +05:00
var r = new DB.Zakazchik()
2021-07-16 16:43:55 +05:00
{
Name = nameBox.Text,
Adress = adressBox.Text,
Phone = phoneBox.Text,
Email = emailBox.Text
};
WorkDB.AddZakazchik(r);
2021-07-17 22:32:53 +05:00
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
2021-07-16 16:43:55 +05:00
}
catch { ShowError(); }
}
2021-07-17 22:32:53 +05:00
private void changeButton_Click(object sender, EventArgs e)
2021-07-16 16:43:55 +05:00
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Заказчик не выбран."); return; }
2021-07-17 22:32:53 +05:00
if (string.IsNullOrEmpty(nameBox.Text)) { ShowError("Нет названия."); return; }
2021-07-16 16:43:55 +05:00
try
{
2021-07-17 22:32:53 +05:00
var zak = WorkDB.GetZakazchik(idRes);
if (zak == null) { ShowError("Нет заказчика в БД."); return; }
if (zak.Name != nameBox.Text && WorkDB.GetZakazchikList().Contains(nameBox.Text)) { ShowError("Заказчик существует."); return; }
2021-07-16 16:43:55 +05:00
zak.Name = nameBox.Text;
zak.Adress = adressBox.Text;
zak.Phone = phoneBox.Text;
zak.Email = emailBox.Text;
WorkDB.ChangeZakazchik(zak);
2021-07-17 22:32:53 +05:00
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
2021-07-16 16:43:55 +05:00
}
catch { ShowError(); }
}
2021-07-17 22:32:53 +05:00
private void deleteButton_Click(object sender, EventArgs e)
2021-07-16 16:43:55 +05:00
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Заказчик не выбран."); return; }
2021-07-17 22:32:53 +05:00
try
{
var zak = WorkDB.GetZakazchik(idRes);
if (zak == null) { ShowError("Нет заказчика в БД."); return; }
if (WorkDB.GetDogovoryFromZakazchik(idRes).Length > 0) { ShowError("Есть связь с договором."); return; }
WorkDB.DeleteZakazchik(zak);
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
}
2021-07-16 16:43:55 +05:00
catch { ShowError(); }
}
2021-07-17 22:32:53 +05:00
private void resetSearchButton_Click(object sender, EventArgs e)
{
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
}
private void resetButton_Click(object sender, EventArgs e)
2021-07-16 16:43:55 +05:00
{
ClearBoxes();
}
2021-07-17 22:32:53 +05:00
private void selectButton_Click(object sender, EventArgs e)
{
if (int.TryParse(idLabel.Text, out int idRes))
returnId = idRes;
this.Close();
}
private void MenuItem_Click(object sender, EventArgs e)
{
object form = null;
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[0]) { form = new DogForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[1]) { form = new DocForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[2]) { form = new IzvForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[3]) { form = new PostForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[4]) { form = new IzdForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[5]) { form = new ZakForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[6]) { form = new SetForm(); }
if (form != null)
{
this.Hide();
((Form)form).Closed += (s, args) => this.Close();
((Form)form).Show();
}
}
}
2021-07-16 16:43:55 +05:00
}