Diplom_B/IzvForm.cs
2021-07-18 22:17:04 +05:00

352 lines
11 KiB
C#
Raw 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.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Diplom_B.DB;
namespace Diplom_B
{
public partial class IzvForm : Form
{
public int? returnId = null;
private bool needReturn = false;
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 string fileName = "";
private byte[] fileStruct = null;
private void UpdateLink(string name = null, byte[] strct = null)
{
var size = 23;
linkFileLabel.Text = "";
linkFileLabel.Visible = false;
fileName = name;
fileStruct = strct;
if (string.IsNullOrEmpty(fileName) || fileStruct == null || fileStruct.Length <= 0)
return;
linkFileLabel.Text = (fileName.Length > size) ? fileName.Substring(0, size - 3) + "..." : fileName;
linkFileLabel.Visible = true;
}
private void ClearBoxes()
{
idLabel.Text = "";
izvNumBox.Text = "";
invNumBox.Text = "";
izmNumBox.Text = "";
ukazZadBox.Text = "";
ukazVnedrBox.Text = "";
UpdateLink();
UpdateDocTable();
}
private void UpdateTable(Izveschenie[] arr, bool reset_cursor = false)
{
var selected = (!reset_cursor && izvGridView.SelectedRows.Count > 0) ? izvGridView.SelectedRows[0].Index : -1;
{
var r = izvGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = izvGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = izvGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("IzvNum", "Извещение №");
c["IzvNum"].Width = 90;
c.Add("InvNum", "Инв. №");
c["InvNum"].Width = 60;
c.Add("IzmNum", "Изм. №");
c["IzmNum"].Width = 60;
c.Add("UkazZad", "Указания о заделе");
c["UkazZad"].Width = 130;
c.Add("UkazVnedr", "Указания о внедр.");
c["UkazVnedr"].Width = 130;
}
{
var r = izvGridView.Rows;
foreach (var izv in arr)
r.Add(new object[] {
izv.Id,
izv.IzvNum,
izv.InvNum,
izv.IzmNum,
izv.UkazZad,
izv.UkazVnedr
});
}
if (izvGridView.Rows.Count > 0)
izvGridView.Rows[0].Selected = true;
if (selected != -1 && selected < izvGridView.Rows.Count)
for (var i = 0; i < izvGridView.Rows.Count; i++)
izvGridView.Rows[i].Selected = (i == selected);
izvGridView_CurrentCellChanged(this, new EventArgs());
}
private void UpdateDocTable()
{
{
var r = docGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = docGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = docGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("DecNum", "Дец. №");
c["DecNum"].Width = 120;
}
{
if (!int.TryParse(idLabel.Text, out int idRes)) return;
var arr = WorkDB.GetDocumentyFromIzvechenie(idRes);
var r = docGridView.Rows;
foreach (var doc in arr)
r.Add(new object[] {
doc.Id,
doc.DecNum
});
}
}
public IzvForm(bool needReturn = false)
{
InitializeComponent();
this.needReturn = needReturn;
try
{
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
Init();
}
catch { ShowError(); }
}
private void Init()
{
if (Program.user == null) this.Close();
if (this.needReturn)
{
selectButton.Visible = true;
mainMenuStrip.Visible = false;
groupBox3.Visible = false;
this.Height = 360;
}
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[2].Enabled = false;
}
{
deleteButton.Enabled = Program.user.Usr.Izv > 2;
createButton.Enabled = Program.user.Usr.Izv > 2;
changeButton.Enabled = Program.user.Usr.Izv > 1;
fileLoadButton.Enabled = Program.user.Usr.Izv > 1;
resetFileButton.Enabled = Program.user.Usr.Izv > 1;
addDocButton.Enabled = Program.user.Usr.Izv > 1;
deleteDocButton.Enabled = Program.user.Usr.Izv > 1;
}
}
private void fileLoadButton_Click(object sender, EventArgs e)
{
try
{
if (selectFileDialog.ShowDialog() != DialogResult.Cancel)
if (!string.IsNullOrEmpty(selectFileDialog.FileName))
{
var fn = Path.GetFileName(selectFileDialog.FileName);
var fb = File.ReadAllBytes(selectFileDialog.FileName);
UpdateLink(fn, fb);
}
}
catch { ShowError(); }
}
private void resetFileButton_Click(object sender, EventArgs e)
{
UpdateLink();
}
private void linkFileLabel_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
if (!string.IsNullOrEmpty(saveFileDialog.FileName))
File.WriteAllBytes(saveFileDialog.FileName, fileStruct);
}
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.ListIzveschenie(searchBox.Text)); }));
else UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
});
filterDrop.Start();
}
private void resetSearchButton_Click(object sender, EventArgs e)
{
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
}
private void izvGridView_CurrentCellChanged(object sender, EventArgs e)
{
ClearBoxes();
if (izvGridView.SelectedRows.Count != 1)
return;
{
var izv = WorkDB.GetIzveschenie((int)izvGridView.SelectedRows[0].Cells[0].Value);
if (izv == null)
return;
idLabel.Text = izv.Id.ToString();
izvNumBox.Text = izv.IzvNum;
invNumBox.Text = izv.InvNum.ToString();
izmNumBox.Text = izv.IzmNum.ToString();
ukazZadBox.Text = izv.UkazZad;
ukazVnedrBox.Text = izv.UkazVnedr;
UpdateLink(izv.FileName, izv.FileStruct);
UpdateDocTable();
}
}
private void createButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(invNumBox.Text, out int invNum)) { ShowError("Инв № не верен."); return; }
if (!int.TryParse(izmNumBox.Text, out int izmNum)) { ShowError("Изм № не верен."); return; }
try
{
var r = new Izveschenie()
{
IzvNum = izvNumBox.Text,
InvNum = invNum,
IzmNum = izmNum,
UkazZad = ukazZadBox.Text,
UkazVnedr = ukazVnedrBox.Text,
FileName = fileName,
FileStruct = fileStruct
};
WorkDB.AddIzveschenie(r);
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
}
catch { ShowError(); }
}
private void changeButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Извещение не выбрано."); return; }
if (!int.TryParse(invNumBox.Text, out int invNum)) { ShowError("Инв № не верен."); return; }
if (!int.TryParse(izmNumBox.Text, out int izmNum)) { ShowError("Изм № не верен."); return; }
var izv = WorkDB.GetIzveschenie(idRes);
if (izv == null) { ShowError("Нет извещения в БД."); return; }
try
{
izv.IzvNum = izvNumBox.Text;
izv.InvNum = invNum;
izv.IzmNum = izmNum;
izv.UkazZad = ukazZadBox.Text;
izv.UkazVnedr = ukazVnedrBox.Text;
izv.FileName = fileName;
izv.FileStruct = fileStruct;
WorkDB.ChangeIzveschenie(izv);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Извещение не выбрано."); return; }
if (WorkDB.GetDocumentyFromIzvechenie(idRes).Length > 0) { ShowError("Есть связанные документы."); return; }
var izv = WorkDB.GetIzveschenie(idRes);
if (izv == null) { ShowError("Поставки не существует."); return; }
try
{
WorkDB.DeleteIzveschenie(izv);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
}
private void resetButton_Click(object sender, EventArgs e)
{
ClearBoxes();
}
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();
}
}
private void addDocButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int izvId)) { ShowError("Извещение не выбрано."); return; }
var form = new DocForm(true);
form.ShowDialog();
var docId = form.returnId;
if (!docId.HasValue) { return; }
if (WorkDB.GetDocIzv(docId.Value, izvId) != null) { ShowError("Документ уже в списке."); return; }
var di = new DocIzv() { DocumentId = docId.Value, IzveschenieId = izvId };
WorkDB.AddDocIzv(di);
UpdateDocTable();
}
private void deleteDocButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int izvId)) { ShowError("Извещение не выбрано."); return; }
if (docGridView.SelectedRows.Count != 1) { return; }
var docId = (int)docGridView.SelectedRows[0].Cells[0].Value;
var di = WorkDB.GetDocIzv(docId, izvId);
if (di == null) { ShowError("Записи нет в БД."); return; }
WorkDB.DeleteDocIzv(di);
UpdateDocTable();
}
private void selectButton_Click(object sender, EventArgs e)
{
if (int.TryParse(idLabel.Text, out int idRes))
returnId = idRes;
this.Close();
}
}
}