236 lines
7.2 KiB
C#
236 lines
7.2 KiB
C#
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
|
||
{
|
||
public partial class ZakForm : Form
|
||
{
|
||
private int? returnId = null;
|
||
private bool needReturn = false;
|
||
public ZakForm(bool needReturn = false)
|
||
{
|
||
InitializeComponent();
|
||
try
|
||
{
|
||
this.needReturn = needReturn;
|
||
Init();
|
||
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
private void ClearBoxes()
|
||
{
|
||
idLabel.Text = "";
|
||
nameBox.Text = "";
|
||
adressBox.Text = "";
|
||
phoneBox.Text = "";
|
||
emailBox.Text = "";
|
||
}
|
||
private void UpdateTable(Zakazchik[] arr, bool reset_cursor = false)
|
||
{
|
||
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());
|
||
}
|
||
private void Init()
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
private Task errDrop;
|
||
private void ShowError(string msg = null)
|
||
{
|
||
errorLabel.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
|
||
errorLabel.Visible = true;
|
||
errDrop = new Task(() =>
|
||
{
|
||
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();
|
||
}
|
||
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)
|
||
if (InvokeRequired) Invoke((Action)(() => { UpdateTable(WorkDB.ListZakazchik(searchBox.Text)); }));
|
||
else UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
|
||
});
|
||
filterDrop.Start();
|
||
}
|
||
private void zakGridView_CurrentCellChanged(object sender, EventArgs e)
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
private void createButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(nameBox.Text)) { ShowError("Нет названия."); return; }
|
||
if (WorkDB.GetZakazchikList().Contains(nameBox.Text)) { ShowError("Заказчик существует."); return; }
|
||
try
|
||
{
|
||
var r = new DB.Zakazchik()
|
||
{
|
||
Name = nameBox.Text,
|
||
Adress = adressBox.Text,
|
||
Phone = phoneBox.Text,
|
||
Email = emailBox.Text
|
||
};
|
||
WorkDB.AddZakazchik(r);
|
||
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
private void changeButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Заказчик не выбран."); return; }
|
||
if (string.IsNullOrEmpty(nameBox.Text)) { ShowError("Нет названия."); return; }
|
||
|
||
try
|
||
{
|
||
var zak = WorkDB.GetZakazchik(idRes);
|
||
if (zak == null) { ShowError("Нет заказчика в БД."); return; }
|
||
if (zak.Name != nameBox.Text && WorkDB.GetZakazchikList().Contains(nameBox.Text)) { ShowError("Заказчик существует."); return; }
|
||
zak.Name = nameBox.Text;
|
||
zak.Adress = adressBox.Text;
|
||
zak.Phone = phoneBox.Text;
|
||
zak.Email = emailBox.Text;
|
||
WorkDB.ChangeZakazchik(zak);
|
||
UpdateTable(WorkDB.ListZakazchik(searchBox.Text));
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
private void deleteButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Заказчик не выбран."); return; }
|
||
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));
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
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)
|
||
{
|
||
ClearBoxes();
|
||
}
|
||
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();
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|