Diplom_B/PostForm.cs

288 lines
9.0 KiB
C#
Raw Normal View History

2021-07-16 16:43:55 +05:00
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;
2021-07-17 22:32:53 +05:00
using Diplom_B.DB;
2021-07-16 16:43:55 +05:00
namespace Diplom_B
{
public partial class PostForm : Form
{
2021-07-17 22:32:53 +05:00
public int? returnId = null;
private bool needReturn = false;
private int? dogovorId = null;
private int? izdelieId = null;
private void ClearBoxes()
{
idLabel.Text = "";
zavNumBox.Text = "";
izdelieId = null;
izdNumLabel.Text = "";
dogovorId = null;
dogNumLabel.Text = "";
datePicker.Value = DateTime.Now;
statusBox.Items.Clear();
statusBox.Items.AddRange(WorkDB.GetStatusList());
statusBox.SelectedIndex = 0;
primechanieBox.Text = "";
}
private void UpdateTable(Postavka[] arr, bool reset_cursor = false)
{
var selected = (!reset_cursor && postGridView.SelectedRows.Count > 0) ? postGridView.SelectedRows[0].Index : -1;
{
var r = postGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = postGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = postGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("ZavNum", "Зав №");
c["ZavNum"].Width = 80;
c.Add("IzdNum", "Изд. №");
c["IzdNum"].Width = 80;
c.Add("DogNum", "Дог. №");
c["DogNum"].Width = 80;
c.Add("DataPostavki", "Дата пост.");
c["DataPostavki"].Width = 100;
c.Add("Status", "Статус");
c["Status"].Width = 80;
c.Add("Primechanie", "Примечание");
c["Primechanie"].Width = 120;
c["Primechanie"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}
{
var r = postGridView.Rows;
foreach (var post in arr)
r.Add(new object[] {
post.Id,
post.ZavNum,
post.IzdelieId.HasValue ? WorkDB.GetIzdelie(post.IzdelieId.Value).DecNum : "",
post.DogovorId.HasValue ? "ПОПРАВИТЬ КОД" : "",
post.DataPostavki.ToString("yyyy.MM.dd"),
WorkDB.GetStatus(post.StatusId).Stat,
post.Primechanie
});
}
if (postGridView.Rows.Count > 0)
postGridView.Rows[0].Selected = true;
if (selected != -1 && selected < postGridView.Rows.Count)
for (var i = 0; i < postGridView.Rows.Count; i++)
postGridView.Rows[i].Selected = (i == selected);
postGridView_CurrentCellChanged(this, new EventArgs());
}
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.ListPostavka(searchBox.Text)); }));
else UpdateTable(WorkDB.ListPostavka(searchBox.Text));
});
filterDrop.Start();
}
public PostForm(bool needReturn = false)
2021-07-16 16:43:55 +05:00
{
InitializeComponent();
2021-07-17 22:32:53 +05:00
try
{
this.needReturn = needReturn;
UpdateTable(WorkDB.ListPostavka(searchBox.Text));
Init();
}
catch { ShowError(); }
}
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[3].Enabled = false;
}
{
deleteButton.Enabled = Program.user.Usr.Post > 2;
createButton.Enabled = Program.user.Usr.Post > 2;
changeButton.Enabled = Program.user.Usr.Post > 1;
selectIzdButton.Enabled = Program.user.Usr.Post > 1;
selectDogButton.Enabled = Program.user.Usr.Post > 1;
}
}
private void postGridView_CurrentCellChanged(object sender, EventArgs e)
{
ClearBoxes();
if (postGridView.SelectedRows.Count != 1)
return;
{
var post = WorkDB.GetPostavka((int)postGridView.SelectedRows[0].Cells[0].Value);
if (post == null)
return;
idLabel.Text = post.Id.ToString();
zavNumBox.Text = post.ZavNum;
if(post.IzdelieId.HasValue)
{
izdNumLabel.Text = WorkDB.GetIzdelie(post.IzdelieId.Value).DecNum;
izdelieId = post.IzdelieId;
}
if (post.DogovorId.HasValue)
{
dogNumLabel.Text = "ПОПРАВИТЬ КОД";
dogovorId = post.DogovorId;
}
datePicker.Value = post.DataPostavki;
statusBox.SelectedIndex = statusBox.Items.IndexOf(WorkDB.GetStatus(post.StatusId).Stat);
primechanieBox.Text = post.Primechanie;
}
}
private void createButton_Click(object sender, EventArgs e)
{
if (WorkDB.GetPostavkiZavNum().Contains(zavNumBox.Text)) { ShowError("Зав № не уникален."); return; }
if (statusBox.SelectedIndex < 0) { ShowError("Не выбран статус."); return; }
try
{
var r = new Postavka()
{
ZavNum = zavNumBox.Text,
IzdelieId = izdelieId,
DogovorId = dogovorId,
DataPostavki = datePicker.Value,
StatusId = WorkDB.GetIdStatus((string)statusBox.SelectedItem).Value,
Primechanie = primechanieBox.Text
};
WorkDB.AddPostavka(r);
UpdateTable(WorkDB.ListPostavka(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.ListPostavka(searchBox.Text));
}
private void changeButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Поставка не выбрана."); return; }
var post = WorkDB.GetPostavka(idRes);
if (post == null) { ShowError("Нет поставки в БД."); return; }
if (post.ZavNum != zavNumBox.Text && WorkDB.GetPostavkiZavNum().Contains(zavNumBox.Text)) { ShowError("Зав № не уникален."); return; }
if (statusBox.SelectedIndex < 0) { ShowError("Не выбран статус."); return; }
try
{
post.ZavNum = zavNumBox.Text;
post.IzdelieId = izdelieId;
post.DogovorId = dogovorId;
post.DataPostavki = datePicker.Value;
post.StatusId = WorkDB.GetIdStatus(statusBox.SelectedText).Value;
post.Primechanie = primechanieBox.Text;
WorkDB.ChangePostavka(post);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListPostavka(searchBox.Text));
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Поставка не выбрана."); return; }
var post = WorkDB.GetPostavka(idRes);
if (post == null) { ShowError("Поставки не существует."); return; }
try
{
WorkDB.DeletePostavka(post);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListPostavka(searchBox.Text));
}
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();
}
}
private void selectIzdButton_Click(object sender, EventArgs e)
{
var form = new IzdForm(true);
form.ShowDialog();
izdelieId = form.returnId;
if (izdelieId.HasValue)
izdNumLabel.Text = WorkDB.GetIzdelie(izdelieId.Value).DecNum;
else
izdNumLabel.Text = "";
}
private void selectDogButton_Click(object sender, EventArgs e)
{
var form = new DogForm(true);
form.ShowDialog();
dogovorId = form.returnId;
if (dogovorId.HasValue)
dogNumLabel.Text = "НАДО ПОПРАВИТЬ КОД";
else
dogNumLabel.Text = "";
2021-07-16 16:43:55 +05:00
}
}
}