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 { 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(); UpdatePlatejTable(); } 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(); UpdatePlatejTable(); } 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(); UpdatePlatejTable(); } 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("F2") }); } { 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("F2"); avansLabel.Text = avans.ToString("F2"); ostalosLabel.Text = ostalos.ToString("F2"); } } } { 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("F2"); } 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 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); } } private void addPostButton_Click(object sender, EventArgs e) { var form = new PostForm(true); form.ShowDialog(); var postId = form.returnId; if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; } if (!postId.HasValue) { ShowError("Поставка не выбрана."); return; } var post = WorkDB.GetPostavka(postId.Value); if (post == null) { ShowError("Поставки не существует."); return; } if (post.DogovorId.HasValue) { ShowError("Поставка связана с договором."); return; } post.DogovorId = dogId; WorkDB.ChangePostavka(post); UpdatePostTable(); } private void delPostButton_Click(object sender, EventArgs e) { if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; } if (postGridView.SelectedRows.Count != 1) { ShowError("Поставка не выбрана."); return; } var post = WorkDB.GetPostavka((int)postGridView.SelectedRows[0].Cells[0].Value); if (post == null) { ShowError("Поставки не существует."); return; } if (!post.DogovorId.HasValue) { ShowError("Поставка не связана с договором."); return; } post.DogovorId = null; WorkDB.ChangePostavka(post); UpdatePostTable(); } private void UpdateDocTable() { var selected = (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("DecNum", "Дец. №"); c["DecNum"].Width = 120; } { if (int.TryParse(idLabel.Text, out int idRes)) { var arr = WorkDB.GetDocumentyDogDocFromDogovor(idRes); if (arr != null) { var r = docGridView.Rows; foreach (var dd in arr) r.Add(new object[] { dd.dogDoc.Id, dd.doc.DecNum, }); } } } { 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); } } private void addDocButton_Click(object sender, EventArgs e) { var form = new DocForm(true); form.ShowDialog(); var docId = form.returnId; if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; } if (!docId.HasValue) { ShowError("Документ не выбран."); return; } var doc = WorkDB.GetDocument(docId.Value); if (doc == null) { ShowError("Документа не существует."); return; } var dogDoc = WorkDB.GetDogDoc(dogId, docId.Value); if (dogDoc != null) { ShowError("Документ связан с договором."); return; } dogDoc = new DogDoc() { DogovorId = dogId, DocumentId = docId.Value}; WorkDB.AddDogDoc(dogDoc); UpdateDocTable(); } private void delDocButton_Click(object sender, EventArgs e) { if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; } if (docGridView.SelectedRows.Count != 1) { ShowError("Договор не выбран."); return; } var dogDoc = WorkDB.GetDogDoc((int)docGridView.SelectedRows[0].Cells[0].Value); if (dogDoc == null) { ShowError("Документ не связана с договором."); return; } var doc = WorkDB.GetDocument(dogDoc.DocumentId); if (doc == null) { ShowError("Документ не существует."); return; } WorkDB.DeleteDogDoc(dogDoc); UpdateDocTable(); } 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 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.ListDogovor(searchBox.Text)); })); else UpdateTable(WorkDB.ListDogovor(searchBox.Text)); }); filterDrop.Start(); } private void resetSearchButton_Click(object sender, EventArgs e) { searchBox.Text = ""; filterDrop = new Task(() => { return; }); UpdateTable(WorkDB.ListDogovor(searchBox.Text)); } public DogForm(bool needReturn = false) { this.needReturn = needReturn; InitializeComponent(); try { UpdateTable(WorkDB.ListDogovor(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; addPostButton.Enabled = Program.user.Usr.Dog > 1; delPostButton.Enabled = Program.user.Usr.Dog > 1; addDocButton.Enabled = Program.user.Usr.Dog > 1; delDocButton.Enabled = Program.user.Usr.Dog > 1; } } private void ClearBoxes() { idLabel.Text = ""; dogNumBox.Text = ""; zakId = null; UpdateZakDecNumLink(); datePicker.Value = DateTime.Now; prikZapBox.Text = ""; garantiiBox.Text = ""; primechanieBox.Text = ""; ClearIzd(); UpdateIzdTable(); ClearPlatej(); UpdatePlatejTable(); UpdatePostTable(); UpdateDocTable(); } int? zakId = null; private void UpdateZakDecNumLink() { var size = 23; zakDecNumLinkLabel.Text = "Не выбран."; if (!zakId.HasValue) return; var f = WorkDB.GetZakazchik(zakId.Value); if (f == null) return; zakDecNumLinkLabel.Text = (f.Name.Length > size) ? f.Name.Substring(0, size - 3) + "..." : f.Name; } private void selectZak_Click(object sender, EventArgs e) { var form = new ZakForm(true); form.ShowDialog(); zakId = form.returnId; UpdateZakDecNumLink(); } private void UpdateTable(Dogovor[] arr, bool reset_cursor = false) { var selected = (!reset_cursor && dogGridView.SelectedRows.Count > 0) ? dogGridView.SelectedRows[0].Index : -1; { 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; c.Add("ZakName", "Заказчик"); c["ZakName"].Width = 120; c.Add("DataPost", "Дата пост."); c["DataPost"].Width = 80; c.Add("PrikoZap", "Приказ о зап."); c["PrikoZap"].Width = 120; c.Add("Garantii", "Гарантии."); c["Garantii"].Width = 200; c["Garantii"].DefaultCellStyle.WrapMode = DataGridViewTriState.True; c.Add("Primechanie", "Примечание."); c["Primechanie"].Width = 200; c["Primechanie"].DefaultCellStyle.WrapMode = DataGridViewTriState.True; } { var r = dogGridView.Rows; foreach (var dog in arr) r.Add(new object[] { dog.Id, dog.DogNum, WorkDB.GetZakazchik(dog.ZakazchikId).Name, dog.DataPostavky.ToString("yyyy.MM.dd"), dog.PrikazZapusk, dog.Garantiy, dog.Primechanie }); } if (dogGridView.Rows.Count > 0) dogGridView.Rows[0].Selected = true; if (selected != -1 && selected < dogGridView.Rows.Count) for (var i = 0; i < dogGridView.Rows.Count; i++) dogGridView.Rows[i].Selected = (i == selected); dogGridView_CurrentCellChanged(this, new EventArgs()); } private void dogGridView_CurrentCellChanged(object sender, EventArgs e) { ClearBoxes(); if (dogGridView.SelectedRows.Count != 1) return; { var dog = WorkDB.GetDogovor((int)dogGridView.SelectedRows[0].Cells[0].Value); if (dog == null) return; idLabel.Text = dog.Id.ToString(); dogNumBox.Text = dog.DogNum; zakId = dog.ZakazchikId; UpdateZakDecNumLink(); datePicker.Value = dog.DataPostavky; prikZapBox.Text = dog.PrikazZapusk; garantiiBox.Text = dog.Garantiy; primechanieBox.Text = dog.Primechanie; UpdateIzdTable(); UpdatePlatejTable(); UpdatePostTable(); UpdateDocTable(); } } private void createButton_Click(object sender, EventArgs e) { if (WorkDB.ListDogovor().Where(x => x.DogNum == dogNumBox.Text).Count() > 0 ) { ShowError("Номер договора дублируется."); return; } if (!zakId.HasValue) { ShowError("Заказчик не выбран."); return; } var r = new Dogovor() { DogNum = dogNumBox.Text, ZakazchikId = zakId.Value, DataPostavky = datePicker.Value, PrikazZapusk = prikZapBox.Text, Garantiy = garantiiBox.Text, Primechanie = primechanieBox.Text }; WorkDB.AddDogovor(r); UpdateTable(WorkDB.ListDogovor(searchBox.Text)); } private void changeButton_Click(object sender, EventArgs e) { if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; } var d = WorkDB.GetDogovor(dogId); if (d == null) { ShowError("Договора нет в БД."); return; } if (dogNumBox.Text != d.DogNum && WorkDB.ListDogovor().Where(x => x.DogNum == dogNumBox.Text).Count() > 0) { ShowError("Номер договора дублируется."); return; } if (!zakId.HasValue) { ShowError("Заказчик не выбран."); return; } d.DogNum = dogNumBox.Text; d.ZakazchikId = zakId.Value; d.DataPostavky = datePicker.Value; d.PrikazZapusk = prikZapBox.Text; d.Garantiy = garantiiBox.Text; d.Primechanie = primechanieBox.Text; WorkDB.ChangeDogovor(d); UpdateTable(WorkDB.ListDogovor(searchBox.Text)); } private void deleteButton_Click(object sender, EventArgs e) { if (!int.TryParse(idLabel.Text, out int dogId)) { ShowError("Договор не выбран."); return; } var d = WorkDB.GetDogovor(dogId); if (d == null) { ShowError("Договора нет в БД."); return; } if (WorkDB.GetIzdelieDogIzdFromDogovor(d.Id).Length > 0) { ShowError("Есть связанные изделия."); return; } if (WorkDB.GetPlatejyFromDogovor(d.Id).Length > 0) { ShowError("Есть связанные платежи."); return; } if (WorkDB.GetPostavkyFromDogovor(d.Id).Length > 0) { ShowError("Есть связанные поставки."); return; } if (WorkDB.GetDocumentyDogDocFromDogovor(d.Id).Length > 0) { ShowError("Есть связанные документы."); return; } WorkDB.DeleteDogovor(d); UpdateTable(WorkDB.ListDogovor(searchBox.Text)); } private void resetButton_Click(object sender, EventArgs e) { ClearBoxes(); } 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(); } } } }