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

420 lines
14 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.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;
using Diplom_B.DB;
namespace Diplom_B
{
public partial class DogForm : 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 int? izdId = null;
private void ClearIzd()
{
izdId = null;
UpdateIzdLink();
kolvoBox.Text = "";
}
private void UpdateIzdLink()
{
var size = 23;
izdDecNumLinkLabel.Text = "Не выбран.";
if (!izdId.HasValue) return;
var f = WorkDB.GetIzdelie(izdId.Value);
if (f == null) return;
izdDecNumLinkLabel.Text = (f.DecNum.Length > size) ? f.DecNum.Substring(0, size - 3) + "..." : f.DecNum;
}
private void UpdateIzdTable()
{
var selected = (izdGridView.SelectedRows.Count > 0) ? izdGridView.SelectedRows[0].Index : -1;
{
var r = izdGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = izdGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = izdGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("DecNum", "Дец. №");
c["DecNum"].Width = 120;
c.Add("Kolvo", "Кол-во");
c["Kolvo"].Width = 40;
}
{
if (int.TryParse(idLabel.Text, out int idRes))
{
var arr = WorkDB.GetIzdelieDogIzdFromDogovor(idRes);
if (arr != null)
{
var r = izdGridView.Rows;
foreach (var izdDi in arr)
r.Add(new object[] {
izdDi.dogIzd.Id,
izdDi.izd.DecNum,
izdDi.dogIzd.Kolvo
});
}
}
}
{
if (izdGridView.Rows.Count > 0)
izdGridView.Rows[0].Selected = true;
if (selected != -1 && selected < izdGridView.Rows.Count)
for (var i = 0; i < izdGridView.Rows.Count; i++)
izdGridView.Rows[i].Selected = (i == selected);
}
izdGridView_CurrentCellChanged(this, new EventArgs());
}
private void izdGridView_CurrentCellChanged(object sender, EventArgs e)
{
if (izdGridView.SelectedRows.Count != 1) return;
var dogIzd = WorkDB.GetDogIzd((int)izdGridView.SelectedRows[0].Cells[0].Value);
if (dogIzd == null) return;
var izd = WorkDB.GetIzdelieFromDogIzd((int)izdGridView.SelectedRows[0].Cells[0].Value);
if (izd == null) return;
izdId = izd.Id;
UpdateIzdLink();
kolvoBox.Text = dogIzd.Kolvo.ToString();
}
private void addIzdButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (!izdId.HasValue) { ShowError("Изделие не выбрано."); return; }
if (!int.TryParse(kolvoBox.Text, out int kolviIzd)) { ShowError("Кол-во не указано."); return; }
if (kolviIzd <= 0) { ShowError("Кол-во <= 0."); return; }
if (WorkDB.GetDogIzd(dogId, izdId.Value) != null) { ShowError("Изделие уже в списке."); return; }
var di = new DogIzd() { DogovorId = dogId, IzdelieId = izdId.Value, Kolvo = kolviIzd };
WorkDB.AddDogIzd(di);
UpdateIzdTable();
}
private void changeIzdButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (!izdId.HasValue) { ShowError("Зделие не выбрано."); return; }
if (!int.TryParse(kolvoBox.Text, out int kolviIzd)) { ShowError("Кол-во не указано."); return; }
if (kolviIzd <= 0) { ShowError("Кол-во <= 0."); return; }
var di = WorkDB.GetDogIzd(dogId, izdId.Value);
if (di == null) { ShowError("Изделия нет в списке."); return; }
di.DogovorId = dogId;
di.IzdelieId = izdId.Value;
di.Kolvo = kolviIzd;
WorkDB.ChangeDogIzd(di);
UpdateIzdTable();
}
private void delIzdButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (!izdId.HasValue) { ShowError("Изделие не выбрано."); return; }
var di = WorkDB.GetDogIzd(dogId, izdId.Value);
if (di == null) { ShowError("Изделия нет в списке."); return; }
WorkDB.DeleteDogIzd(di);
UpdateIzdTable();
}
private void selectizd_Click(object sender, EventArgs e)
{
var form = new IzdForm(true);
form.ShowDialog();
izdId = form.returnId;
UpdateIzdLink();
}
private void ClearPlatej()
{
cenaGlobalLabel.Text = "0";
avansLabel.Text = "0";
ostalosLabel.Text = "0";
platejBox.Text = "";
}
private void UpdatePlatejTable()
{
var selected = (platejGridView.SelectedRows.Count > 0) ? platejGridView.SelectedRows[0].Index : -1;
{
var r = platejGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = platejGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = platejGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("Platej", "Платёж");
c["Platej"].Width = 120;
}
{
if (int.TryParse(idLabel.Text, out int idRes))
{
var arr = WorkDB.GetPlatejyFromDogovor(idRes);
if (arr != null)
{
var r = platejGridView.Rows;
foreach (var pl in arr)
r.Add(new object[] {
pl.Id,
pl.Summa.ToString("X2")
});
}
{
double cena = 0;
double avans = 0;
double ostalos = 0;
var cenaArr = WorkDB.GetIzdelieDogIzdFromDogovor(idRes);
if(cenaArr != null)
for(var i = 0; i < cenaArr.Length; i++)
cena += cenaArr[i].izd.Cena * cenaArr[i].dogIzd.Kolvo;
ostalos = cena;
for(var i = 0; i < arr.Length; i++)
{
if (i == 0) avans = arr[i].Summa;
ostalos -= arr[i].Summa;
}
cenaGlobalLabel.Text = cena.ToString("X2");
avansLabel.Text = avans.ToString("X2");
ostalosLabel.Text = ostalos.ToString("X2");
}
}
}
{
if (platejGridView.Rows.Count > 0)
platejGridView.Rows[0].Selected = true;
if (selected != -1 && selected < platejGridView.Rows.Count)
for (var i = 0; i < platejGridView.Rows.Count; i++)
platejGridView.Rows[i].Selected = (i == selected);
}
platejGridView_CurrentCellChanged(this, new EventArgs());
}
private void platejGridView_CurrentCellChanged(object sender, EventArgs e)
{
if (platejGridView.SelectedRows.Count != 1) return;
var platej = WorkDB.GetPlatej((int)platejGridView.SelectedRows[0].Cells[0].Value);
if (platej == null) return;
platejBox.Text = platej.Summa.ToString("X2");
}
private void addPlatejBox_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (!double.TryParse(platejBox.Text, out double platej)) { ShowError("Сумма не корректна."); return; }
if (platej < 0) { ShowError("Платёж < 0."); return; }
var pl = new Platej() { DogovorId = dogId, Summa = platej };
WorkDB.AddPlatej(pl);
UpdatePlatejTable();
}
private void changePlatejBox_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (!double.TryParse(platejBox.Text, out double platej)) { ShowError("Сумма не корректна."); return; }
if (platej < 0) { ShowError("Платёж < 0."); return; }
if (platejGridView.SelectedRows.Count != 1) { ShowError("Платёж не выбран."); return; }
var pl = WorkDB.GetPlatej((int)platejGridView.SelectedRows[0].Cells[0].Value);
if (pl == null) { ShowError("Платежа нет в БД."); return; }
pl.DogovorId = dogId;
pl.Summa = platej;
WorkDB.ChangePlatej(pl);
UpdatePlatejTable();
}
private void delPlatejBox_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (platejGridView.SelectedRows.Count != 1) { ShowError("Платёж не выбран."); return; }
var pl = WorkDB.GetPlatej((int)platejGridView.SelectedRows[0].Cells[0].Value);
if (pl == null) { ShowError("Платежа нет в БД."); return; }
WorkDB.DeletePlatej(pl);
UpdatePlatejTable();
}
private int? postId = null;
private void ClearPostavki()
{
postId = null;
UpdatePostLink();
}
private void UpdatePostLink()
{
var size = 23;
postZavNumLinkLabel.Text = "Не выбран.";
if (!postId.HasValue) return;
var f = WorkDB.GetPostavka(izdId.Value);
if (f == null) return;
postZavNumLinkLabel.Text = (f.ZavNum.Length > size) ? f.ZavNum.Substring(0, size - 3) + "..." : f.ZavNum;
}
private void UpdatePostTable()
{
var selected = (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 = 120;
}
{
if (int.TryParse(idLabel.Text, out int idRes))
{
var arr = WorkDB.GetPostavkyFromDogovor(idRes);
if (arr != null)
{
var r = postGridView.Rows;
foreach (var post in arr)
r.Add(new object[] {
post.Id,
post.ZavNum
});
}
}
}
{
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 void postGridView_CurrentCellChanged(object sender, EventArgs e)
{
if (postGridView.SelectedRows.Count != 1) return;
var post = WorkDB.GetPostavka((int)postGridView.SelectedRows[0].Cells[0].Value);
if (post == null) return;
postId = post.Id;
UpdatePostLink();
}
private void addPostButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; }
if (!double.TryParse(platejBox.Text, out double platej)) { ShowError("Сумма не корректна."); return; }
if (platej < 0) { ShowError("Платёж < 0."); return; }
var pl = new Platej() { DogovorId = dogId, Summa = platej };
WorkDB.AddPlatej(pl);
UpdatePlatejTable();
}
private void changePostButton_Click(object sender, EventArgs e)
{
}
private void delPostButton_Click(object sender, EventArgs e)
{
}
private void selectPost_Click(object sender, EventArgs e)
{
}
public DogForm(bool needReturn = false)
{
this.needReturn = needReturn;
InitializeComponent();
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;
}
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[0].Enabled = false;
}
{
zakDecNumLinkLabel.Enabled = Program.user.Usr.Dog > 2;
createButton.Enabled = Program.user.Usr.Dog > 2;
changeButton.Enabled = Program.user.Usr.Dog > 1;
deleteButton.Enabled = Program.user.Usr.Dog > 2;
izdDecNumLinkLabel.Enabled = Program.user.Usr.Dog > 1;
addIzdButton.Enabled = Program.user.Usr.Dog > 1;
changeIzdButton.Enabled = Program.user.Usr.Dog > 1;
delIzdButton.Enabled = Program.user.Usr.Dog > 1;
addPlatejBox.Enabled = Program.user.Usr.Dog > 1;
changePlatejBox.Enabled = Program.user.Usr.Dog > 1;
delPlatejBox.Enabled = Program.user.Usr.Dog > 1;
postZavNumLinkLabel.Enabled = Program.user.Usr.Dog > 1;
addPostButton.Enabled = Program.user.Usr.Dog > 1;
changePostButton.Enabled = Program.user.Usr.Dog > 1;
delPostButton.Enabled = Program.user.Usr.Dog > 1;
docDecNumLinkLabel.Enabled = Program.user.Usr.Dog > 1;
addDocButton.Enabled = Program.user.Usr.Dog > 1;
changeDocButton.Enabled = Program.user.Usr.Dog > 1;
delDocButton.Enabled = Program.user.Usr.Dog > 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();
}
}
}
}