Diplom_B/IzvForm.cs

352 lines
11 KiB
C#
Raw Permalink Normal View History

2021-07-16 16:43:55 +05:00
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
{
2021-07-17 22:32:53 +05:00
public int? returnId = null;
private bool needReturn = false;
2021-07-16 16:43:55 +05:00
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();
2021-07-18 22:17:04 +05:00
UpdateDocTable();
2021-07-16 16:43:55 +05:00
}
2021-07-17 22:32:53 +05:00
private void UpdateTable(Izveschenie[] arr, bool reset_cursor = false)
2021-07-16 16:43:55 +05:00
{
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());
}
2021-07-18 22:17:04 +05:00
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)
2021-07-16 16:43:55 +05:00
{
InitializeComponent();
2021-07-18 22:17:04 +05:00
this.needReturn = needReturn;
2021-07-17 22:32:53 +05:00
try
{
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
Init();
}
2021-07-16 16:43:55 +05:00
catch { ShowError(); }
}
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)
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
selectButton.Visible = true;
mainMenuStrip.Visible = false;
2021-07-18 22:17:04 +05:00
groupBox3.Visible = false;
this.Height = 360;
2021-07-17 22:32:53 +05:00
}
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;
2021-07-18 22:17:04 +05:00
addDocButton.Enabled = Program.user.Usr.Izv > 1;
deleteDocButton.Enabled = Program.user.Usr.Izv > 1;
2021-07-16 16:43:55 +05:00
}
2021-07-17 22:32:53 +05:00
}
private void fileLoadButton_Click(object sender, EventArgs e)
{
2021-07-16 16:43:55 +05:00
try
{
2021-07-17 22:32:53 +05:00
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);
}
2021-07-16 16:43:55 +05:00
}
catch { ShowError(); }
2021-07-17 22:32:53 +05:00
}
private void resetFileButton_Click(object sender, EventArgs e)
{
UpdateLink();
2021-07-16 16:43:55 +05:00
}
private void linkFileLabel_Click(object sender, EventArgs e)
{
2021-07-17 22:32:53 +05:00
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(() =>
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
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();
2021-07-16 16:43:55 +05:00
}
2021-07-17 22:32:53 +05:00
private void resetSearchButton_Click(object sender, EventArgs e)
2021-07-16 16:43:55 +05:00
{
2021-07-17 22:32:53 +05:00
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
2021-07-16 16:43:55 +05:00
}
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);
2021-07-18 22:17:04 +05:00
UpdateDocTable();
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 (!int.TryParse(invNumBox.Text, out int invNum)) { ShowError("Инв № не верен."); return; }
if (!int.TryParse(izmNumBox.Text, out int izmNum)) { ShowError("Изм № не верен."); return; }
2021-07-16 16:43:55 +05:00
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);
2021-07-17 22:32:53 +05:00
UpdateTable(WorkDB.ListIzveschenie(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 (!int.TryParse(invNumBox.Text, out int invNum)) { ShowError("Инв № не верен."); return; }
if (!int.TryParse(izmNumBox.Text, out int izmNum)) { ShowError("Изм № не верен."); return; }
2021-07-16 16:43:55 +05:00
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(); }
2021-07-17 22:32:53 +05:00
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
2021-07-16 16:43:55 +05:00
}
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
if (WorkDB.GetDocumentyFromIzvechenie(idRes).Length > 0) { ShowError("Есть связанные документы."); return; }
2021-07-16 16:43:55 +05:00
var izv = WorkDB.GetIzveschenie(idRes);
2021-07-17 22:32:53 +05:00
if (izv == null) { ShowError("Поставки не существует."); return; }
2021-07-16 16:43:55 +05:00
try
{
2021-07-17 22:32:53 +05:00
WorkDB.DeleteIzveschenie(izv);
2021-07-16 16:43:55 +05:00
}
catch { ShowError(); }
2021-07-17 22:32:53 +05:00
UpdateTable(WorkDB.ListIzveschenie(searchBox.Text));
2021-07-16 16:43:55 +05:00
}
2021-07-17 22:32:53 +05:00
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 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-18 22:17:04 +05:00
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();
}
2021-07-17 22:32:53 +05:00
2021-07-18 22:17:04 +05:00
}
2021-07-16 16:43:55 +05:00
}