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

403 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Diplom_B.DB;
namespace Diplom_B
{
public partial class DocForm : 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 = "";
invNumBox.Text = "";
decNumBox.Text = "";
nameBox.Text = "";
primechanieBox.Text = "";
UpdateLink();
UpdateDogTable();
UpdateIzvTable();
}
private void UpdateTable(Document[] arr, bool reset_cursor = false)
{
var selected = (!reset_cursor && docGridView.SelectedRows.Count > 0) ? docGridView.SelectedRows[0].Index : -1;
{
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("InvNum", "Инв. №");
c["InvNum"].Width = 100;
c.Add("DecNum", "Дец. №");
c["DecNum"].Width = 100;
c.Add("Name", "Название");
c["Name"].Width = 100;
c.Add("Prim", "Примечание");
c["Prim"].Width = 150;
c["Prim"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}
{
var r = docGridView.Rows;
foreach (var doc in arr)
r.Add(new object[] {
doc.Id,
doc.InvNum,
doc.DecNum,
doc.Name,
doc.Primechanie
});
}
if (docGridView.Rows.Count > 0)
docGridView.Rows[0].Selected = true;
if (selected != -1 && selected < docGridView.Rows.Count)
for (var i = 0; i < docGridView.Rows.Count; i++)
docGridView.Rows[i].Selected = (i == selected);
docGridView_CurrentCellChanged(this, new EventArgs());
}
private void UpdateDogTable()
{
{
var r = dogGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = dogGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = dogGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("DogNum", "Дог. №");
c["DogNum"].Width = 120;
}
{
if (!int.TryParse(idLabel.Text, out int idRes)) return;
var arr = WorkDB.GetDogovorFromDocumenty(idRes);
var r = dogGridView.Rows;
foreach (var dog in arr)
r.Add(new object[] {
dog.Id,
dog.DogNum
});
}
}
private void UpdateIzvTable()
{
{
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 = 120;
}
{
if (!int.TryParse(idLabel.Text, out int idRes)) return;
var arr = WorkDB.GetIzvechenieFromDocumenty(idRes);
var r = izvGridView.Rows;
foreach (var izv in arr)
r.Add(new object[] {
izv.Id,
izv.IzvNum
});
}
}
public DocForm(bool needReturn = false)
{
InitializeComponent();
this.needReturn = needReturn;
try
{
UpdateTable(WorkDB.ListDocumenty(searchBox.Text));
Init();
}
catch { throw; }
}
private void Init()
{
if (Program.user == null) this.Close();
if (this.needReturn)
{
selectButton.Visible = true;
mainMenuStrip.Visible = false;
groupBox3.Visible = false;
groupBox4.Visible = false;
this.Height = 385;
}
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[1].Enabled = false;
}
{
deleteButton.Enabled = Program.user.Usr.Doc > 2;
createButton.Enabled = Program.user.Usr.Doc > 2;
changeButton.Enabled = Program.user.Usr.Doc > 1;
fileLoadButton.Enabled = Program.user.Usr.Doc > 1;
resetFileButton.Enabled = Program.user.Usr.Doc > 1;
addDogButton.Enabled = Program.user.Usr.Doc > 1;
delDogButton.Enabled = Program.user.Usr.Doc > 1;
addIzvButton.Enabled = Program.user.Usr.Doc > 1;
delIzvButton.Enabled = Program.user.Usr.Doc > 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.ListDocumenty(searchBox.Text)); }));
else UpdateTable(WorkDB.ListDocumenty(searchBox.Text));
});
filterDrop.Start();
}
private void resetSearchButton_Click(object sender, EventArgs e)
{
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateTable(WorkDB.ListDocumenty(searchBox.Text));
}
private void docGridView_CurrentCellChanged(object sender, EventArgs e)
{
ClearBoxes();
if (docGridView.SelectedRows.Count != 1)
return;
{
var doc = WorkDB.GetDocument((int)docGridView.SelectedRows[0].Cells[0].Value);
if (doc == null)
return;
idLabel.Text = doc.Id.ToString();
invNumBox.Text = doc.InvNum;
decNumBox.Text = doc.DecNum;
nameBox.Text = doc.Name;
primechanieBox.Text = doc.Primechanie;
UpdateLink(doc.FileName, doc.FileStruct);
UpdateDogTable();
UpdateIzvTable();
}
}
private void createButton_Click(object sender, EventArgs e)
{
try
{
var r = new Document()
{
InvNum = invNumBox.Text,
DecNum = decNumBox.Text,
Name = nameBox.Text,
Primechanie = primechanieBox.Text,
FileName = fileName,
FileStruct = fileStruct
};
WorkDB.AddDocument(r);
UpdateTable(WorkDB.ListDocumenty(searchBox.Text));
}
catch { ShowError(); }
}
private void changeButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Документ не выбрано."); return; }
var doc = WorkDB.GetDocument(idRes);
if (doc == null) { ShowError("Нет документа в БД."); return; }
try
{
doc.InvNum = invNumBox.Text;
doc.DecNum = decNumBox.Text;
doc.Name = nameBox.Text;
doc.Primechanie = primechanieBox.Text;
doc.FileName = fileName;
doc.FileStruct = fileStruct;
WorkDB.ChangeDocument(doc);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListDocumenty(searchBox.Text));
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Документ не выбран."); return; }
if (WorkDB.GetDogovorFromDocumenty(idRes).Length > 0) { ShowError("Есть связанные договора."); return; }
if (WorkDB.GetIzvechenieFromDocumenty(idRes).Length > 0) { ShowError("Есть связанные извещения."); return; }
var doc = WorkDB.GetDocument(idRes);
if (doc == null) { ShowError("Документа не существует."); return; }
try
{
WorkDB.DeleteDocument(doc);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListDocumenty(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 addDogButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int docId)) { ShowError("Документ не выбран."); return; }
var form = new DogForm(true);
form.ShowDialog();
var dogId = form.returnId;
if (!dogId.HasValue) { return; }
if (WorkDB.GetDogDoc(dogId.Value, docId) != null) { ShowError("Договор уже в списке."); return; }
var dd = new DogDoc() { DogovorId = dogId.Value, DocumentId = docId };
WorkDB.AddDogDoc(dd);
UpdateDogTable();
}
private void delDogButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int docId)) { ShowError("Документ не выбран."); return; }
if (dogGridView.SelectedRows.Count != 1) { return; }
var dogId = (int)dogGridView.SelectedRows[0].Cells[0].Value;
var dd = WorkDB.GetDogDoc(dogId, docId);
if (dd == null) { ShowError("Записи нет в БД."); return; }
WorkDB.DeleteDogDoc(dd);
UpdateDogTable();
}
private void addIzvButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int docId)) { ShowError("Документ не выбран."); return; }
var form = new IzvForm(true);
form.ShowDialog();
var izvId = form.returnId;
if (!izvId.HasValue) { return; }
if (WorkDB.GetDocIzv(docId, izvId.Value) != null) { ShowError("Извещение уже в списке."); return; }
var di = new DocIzv() { DocumentId = docId, IzveschenieId = izvId.Value };
WorkDB.AddDocIzv(di);
UpdateIzvTable();
}
private void delIzvButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int docId)) { ShowError("Документ не выбран."); return; }
if (izvGridView.SelectedRows.Count != 1) { return; }
var izvId = (int)izvGridView.SelectedRows[0].Cells[0].Value;
var di = WorkDB.GetDocIzv(docId, izvId);
if (di == null) { ShowError("Записи нет в БД."); return; }
WorkDB.DeleteDocIzv(di);
UpdateIzvTable();
}
private void selectButton_Click(object sender, EventArgs e)
{
if (int.TryParse(idLabel.Text, out int idRes))
returnId = idRes;
this.Close();
}
}
}