269 lines
8.0 KiB
C#
269 lines
8.0 KiB
C#
using Diplom_B.DB;
|
||
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;
|
||
|
||
namespace Diplom_B
|
||
{
|
||
public partial class IzdForm : Form
|
||
{
|
||
public int? returnId = null;
|
||
private bool needReturn = false;
|
||
private void ClearBoxes()
|
||
{
|
||
idLable.Text = "";
|
||
nameBox.Text = "";
|
||
decBox.Text = "";
|
||
shifrBox.Text = "";
|
||
literaBox.Text = "";
|
||
cenaBox.Text = "";
|
||
otdRazBox.Text = "";
|
||
vedBox.Text = "";
|
||
glavKonstrBox.Text = "";
|
||
}
|
||
private void UpdateTable(Izdelie[] arr, bool reset_cursor = false)
|
||
{
|
||
var selected = (!reset_cursor && idzGridView.SelectedRows.Count > 0) ? idzGridView.SelectedRows[0].Index : -1;
|
||
{
|
||
var r = idzGridView.Rows;
|
||
while (r.Count > 0)
|
||
r.Remove(r[0]);
|
||
var c = idzGridView.Columns;
|
||
while (c.Count > 0)
|
||
c.Remove(c[0]);
|
||
}
|
||
{
|
||
var c = idzGridView.Columns;
|
||
c.Add("Id", "№");
|
||
c["Id"].Width = 40;
|
||
c.Add("Name", "Наим.");
|
||
c["Name"].Width = 60;
|
||
c.Add("DecNum", "Дец. №");
|
||
c["DecNum"].Width = 120;
|
||
c.Add("Shifr", "Шифр");
|
||
c["Shifr"].Width = 80;
|
||
c.Add("Litera", "Литера");
|
||
c["Litera"].Width = 50;
|
||
c.Add("Cena", "Цена");
|
||
c["Cena"].Width = 70;
|
||
c.Add("OtdelRazrab", "Отдел");
|
||
c["OtdelRazrab"].Width = 40;
|
||
c.Add("Ved", "Вед.");
|
||
c.Add("GlavKonstr", "Глав. констр.");
|
||
}
|
||
{
|
||
var r = idzGridView.Rows;
|
||
foreach (var izd in arr)
|
||
r.Add(new object[] {
|
||
izd.Id,
|
||
izd.Name,
|
||
izd.DecNum,
|
||
izd.Shifr,
|
||
izd.Litera,
|
||
izd.Cena.ToString("F2") + " Р",
|
||
izd.OtdelRazrab,
|
||
izd.Ved,
|
||
izd.GlavKonstr
|
||
});
|
||
}
|
||
if (idzGridView.Rows.Count > 0)
|
||
idzGridView.Rows[0].Selected = true;
|
||
if (selected != -1 && selected < idzGridView.Rows.Count)
|
||
for (var i = 0; i < idzGridView.Rows.Count; i++)
|
||
idzGridView.Rows[i].Selected = (i == selected);
|
||
izdGridView_CurrentCellChanged(this, new EventArgs());
|
||
}
|
||
|
||
private Task errDrop;
|
||
private void ShowError(string msg = null)
|
||
{
|
||
errorLable.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
|
||
errorLable.Visible = true;
|
||
errDrop = new Task(() =>
|
||
{
|
||
var fd = errDrop.Id;
|
||
Task.Delay(5000).Wait();
|
||
if (errDrop.Id == fd)
|
||
if (InvokeRequired) Invoke((Action)(() => { errorLable.Visible = false; }));
|
||
else errorLable.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.ListIzdelie(searchBox.Text)); }));
|
||
else UpdateTable(WorkDB.ListIzdelie(searchBox.Text));
|
||
|
||
});
|
||
filterDrop.Start();
|
||
}
|
||
|
||
public IzdForm(bool needReturn = false)
|
||
{
|
||
InitializeComponent();
|
||
try
|
||
{
|
||
this.needReturn = needReturn;
|
||
UpdateTable(WorkDB.ListIzdelie(searchBox.Text));
|
||
Init();
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
|
||
|
||
|
||
private void izdGridView_CurrentCellChanged(object sender, EventArgs e)
|
||
{
|
||
ClearBoxes();
|
||
if (idzGridView.SelectedRows.Count != 1)
|
||
return;
|
||
{
|
||
var izd = WorkDB.GetIzdelie((int)idzGridView.SelectedRows[0].Cells[0].Value);
|
||
if (izd == null)
|
||
return;
|
||
idLable.Text = izd.Id.ToString();
|
||
nameBox.Text = izd.Name;
|
||
decBox.Text = izd.DecNum;
|
||
shifrBox.Text = izd.Shifr;
|
||
literaBox.Text = izd.Litera;
|
||
cenaBox.Text = izd.Cena.ToString();
|
||
otdRazBox.Text = izd.OtdelRazrab.ToString();
|
||
vedBox.Text = izd.Ved;
|
||
glavKonstrBox.Text = izd.GlavKonstr;
|
||
}
|
||
}
|
||
private void createButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (!double.TryParse(cenaBox.Text, out double cena)) { ShowError("Ошибка цены."); return; }
|
||
if (!int.TryParse(otdRazBox.Text, out int otdRaz)) { ShowError("Ошибка отдела-разработчика."); return; }
|
||
try
|
||
{
|
||
var r = new Izdelie()
|
||
{
|
||
Name = nameBox.Text,
|
||
DecNum = decBox.Text,
|
||
Shifr = shifrBox.Text,
|
||
Litera = literaBox.Text,
|
||
Cena = cena,
|
||
OtdelRazrab = otdRaz,
|
||
Ved = vedBox.Text,
|
||
GlavKonstr = glavKonstrBox.Text
|
||
};
|
||
WorkDB.AddIzdelie(r);
|
||
UpdateTable(WorkDB.ListIzdelie(searchBox.Text));
|
||
}
|
||
catch { ShowError(); }
|
||
}
|
||
private void clearButton_Click(object sender, EventArgs e)
|
||
{
|
||
ClearBoxes();
|
||
}
|
||
private void resetSearchButton_Click(object sender, EventArgs e)
|
||
{
|
||
searchBox.Text = "";
|
||
filterDrop = new Task(() => { return; });
|
||
UpdateTable(WorkDB.ListIzdelie(searchBox.Text));
|
||
}
|
||
private void changeButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (!int.TryParse(idLable.Text, out int idRes)) { ShowError("Изделие не выбрано."); return; }
|
||
var izd = WorkDB.GetIzdelie(idRes);
|
||
if (izd == null) { ShowError("Нет изделия в БД."); return; }
|
||
if(!double.TryParse(cenaBox.Text, out double cena)) { ShowError("Ошибка цены."); return; }
|
||
if (!int.TryParse(otdRazBox.Text, out int otdRaz)) { ShowError("Ошибка отдела-разраб."); return; }
|
||
try
|
||
{
|
||
izd.Name = nameBox.Text;
|
||
izd.DecNum = decBox.Text;
|
||
izd.Shifr = shifrBox.Text;
|
||
izd.Litera = literaBox.Text;
|
||
izd.Cena = cena;
|
||
izd.OtdelRazrab = otdRaz;
|
||
izd.Ved = vedBox.Text;
|
||
izd.GlavKonstr = glavKonstrBox.Text;
|
||
WorkDB.ChangeIzdelie(izd);
|
||
}
|
||
catch { ShowError(); }
|
||
UpdateTable(WorkDB.ListIzdelie(searchBox.Text));
|
||
}
|
||
private void deleteButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (!int.TryParse(idLable.Text, out int idRes)) { ShowError("Изделие не выбрано."); return; }
|
||
var izd = WorkDB.GetIzdelie(idRes);
|
||
if (izd == null) { ShowError("Изделия не существует."); return; }
|
||
if (WorkDB.GetPostavkyFromIzdeliya(izd.Id).Length > 0) { ShowError("Есть связанные поставки."); return; }
|
||
if (WorkDB.GetDogovoryFromIzdeliya(izd.Id).Length > 0) { ShowError("Есть связанные договора."); return; }
|
||
|
||
try
|
||
{
|
||
WorkDB.DeleteIzdelie(izd);
|
||
}
|
||
catch { ShowError(); }
|
||
UpdateTable(WorkDB.ListIzdelie(searchBox.Text));
|
||
}
|
||
|
||
private void selectButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (int.TryParse(idLable.Text, out int idRes))
|
||
returnId = idRes;
|
||
this.Close();
|
||
}
|
||
|
||
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[4].Enabled = false;
|
||
}
|
||
{
|
||
deleteButton.Enabled = Program.user.Usr.Izd > 2;
|
||
createButton.Enabled = Program.user.Usr.Izd > 2;
|
||
changeButton.Enabled = Program.user.Usr.Izd > 1;
|
||
}
|
||
}
|
||
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();
|
||
}
|
||
}
|
||
}
|
||
}
|