using BrightIdeasSoftware; using DatenDB; using Deckungsbeitrag.UserControls; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics.Eventing.Reader; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Windows.Forms; using ZXing; namespace Deckungsbeitrag { public partial class FormAuftragDetail : Form { private Dictionary dictionary = new Dictionary(); public Auftrag auftrag; public Benutzer benutzer; public List artikelList; public Meldungen meldung = new Meldungen(); private UCInventur uci; private UCFeedback fb; public Kunde kunde; private int diff = 0; int[] bestand; // Reklamation/Fehlmenge/Standveränderung/Inventurausgleich bool etikettgedruckt = false; //WENN AUFTRAG SPEICHERN private bool _tosave = false; public bool tosave { get { return _tosave; } set { _tosave = value; if (_tosave) { buttonSpeichern.Enabled = true; buttonSpeichern.BackColor = Color.FromArgb(0,192,0); } } } //WENN AUFTRAG ABZUSCHLIESSEN private bool _toclose = false; public bool toclose { get { return _toclose; } set { _toclose = value; if (_toclose) { buttonFertig.Enabled = true; buttonFertig.BackColor = Color.FromArgb(0, 192, 0); } } } public FormAuftragDetail() { InitializeComponent(); //this.BackColor = Properties.Settings.Default.Wirlblau; labelContainer_TextChanged(this.labelContainer, EventArgs.Empty); //SPEICHERN UND ABSCHLIESSEN BUTTONS WERDEN FUNKTIONSLOS UND GRAU buttonSpeichern.Enabled = buttonFertig.Enabled = false; buttonSpeichern.BackColor = buttonFertig.BackColor = Color.Gray; } public FormAuftragDetail(Auftrag auftrag, Benutzer benutzer) : this() { this.auftrag = auftrag; this.benutzer = benutzer; } private void FormAuftragDetail_Load(object sender, EventArgs e) { LadeComboBoxen(); this.Text = this.auftrag.Typ.ToString() + "-Auftrag"; this.kunde = Kunde.GetKunde(string.Empty, auftrag.KundeID, string.Empty); this.dTPLiefertag.Value = auftrag.Liefertag; this.labelKundeName.Text = string.IsNullOrWhiteSpace(this.kunde.Suchtext) ? this.kunde.KundeName : this.kunde.Suchtext; this.labelRegion.Text = this.kunde.Region; this.labelContainer.Text = auftrag.ContainerClean.ToString(); this.labelContDirt.Text = auftrag.Container.ToString(); this.textBoxZusatz.Text = $"{auftrag.ZusatzInfo}\n{this.kunde.Anmerkung}\n{Aufgabe.GetAufgabe(null, this.kunde.Aufgabe).Bezeichnung}"; if (auftrag.Fahrer == null) this.comboBoxFahrer.SelectedIndex = -1; else this.comboBoxFahrer.SelectedValue = auftrag.Fahrer; this.comboBoxPriority.SelectedItem = auftrag.Priority; // Events nach dem Laden der Daten wieder den ComboBoxen zuweisen. this.comboBoxPriority.SelectedValueChanged += comboBox_SelectedValueChanged; this.comboBoxFahrer.SelectedValueChanged += comboBox_SelectedValueChanged; if (this.auftrag.Typ != AuftragTyp.Inventur || (this.auftrag.Typ == AuftragTyp.Inventur & this.auftrag.Status == AuftragStatus.Herrichten)) Load_OLV(); else { if (this.auftrag.Status == AuftragStatus.Warten) buttonSpeichern.Visible = buttonFertig.Visible = buttonSWS.Visible = false; Load_UCInventur(this.auftrag); } this.ClientSize = new Size(this.oLVArtikel.Right + 10, this.ClientSize.Height); //WENN STATUS VORBEREITET BUTTON FERTIG ENABLEN if (auftrag.Status == AuftragStatus.Vorbereitet) { buttonFertig.Enabled = true; buttonFertig.BackColor = Color.FromArgb(0, 192, 0); toclose = true; } } private void Load_UCInventur(Auftrag _auftrag) { Point loc = this.oLVArtikel.Location; this.oLVArtikel.Visible = false; string loadPath = Path.Combine(ConfigurationManager.AppSettings["DateiSavePfad"], "Listen\\Inventuren") ?? "Inventuren"; if (ConfigurationManager.AppSettings["ConnectionString"].Split(';')[4].Split('=')[1] == "Test_Wirl") loadPath = Path.Combine(ConfigurationManager.AppSettings["DateiSavePfad"], "Test\\Inventuren") ?? "Inventuren"; // Alle "Herrichten" Inventur Files auflisten. string[] invFiles = Directory.GetFiles(loadPath, $"Warten-{_auftrag.AuftragID.ToString()}*.csv"); if (invFiles.Length == 0) return; if (invFiles.Length > 1) { MessageBox.Show("Es wurden mehr Inventuren gefunden. Besprich das mit deinem Vorgesetzten.", "FEHLER", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string[] lines = File.ReadAllLines(Path.Combine(loadPath, invFiles[0]), Encoding.UTF8); uci = new UCInventur(lines, this.benutzer, this); uci.Visible = false; uci.SizeChangedForParent += () => { DoResize(); }; this.Controls.Add(uci); } private void DoResize() { if (uci != null) { uci.Location = this.oLVArtikel.Location = new Point(this.oLVArtikel.Location.X, fb.Bottom + 2); uci.Visible = true; this.ClientSize = new Size(uci.Right, uci.Bottom + buttonAbbrechen.Height + 25); } else { this.oLVArtikel.Height = this.textBoxZusatz.Bottom - this.oLVArtikel.Top; this.ClientSize = new Size(oLVArtikel.Right + 10, oLVArtikel.Bottom + buttonAbbrechen.Height + 25); } if (fb != null) fb.Location = new Point(this.ClientSize.Width - fb.Width - 7, 0); } private void Load_OLV() { Generator.GenerateColumns(this.oLVArtikel, typeof(AuftragArtikel), true); this.artikelList = AuftragArtikel.GetList(auftrag.AuftragID); this.oLVArtikel.SetObjects(this.artikelList); this.oLVArtikel = Funktionen.Columns_Resize(null, this.oLVArtikel).olv; // Wenn alle Artikel Checked sind TOCLOSE auslösen. oLVArtikel_ItemChecked(this.oLVArtikel, null); } private void LadeComboBoxen() { this.comboBoxFahrer.SelectedValueChanged -= comboBox_SelectedValueChanged; this.comboBoxPriority.SelectedValueChanged -= comboBox_SelectedValueChanged; List benutzerliste = Benutzer.GetFahrerList(); this.comboBoxFahrer.DataSource = benutzerliste; this.comboBoxFahrer.ValueMember = "BenutzerName"; this.comboBoxPriority.DataSource = Enum.GetValues(typeof(AuftragPriority)); } private void comboBox_SelectedValueChanged(object sender, EventArgs e) { tosave = true; } private void labelContainer_TextChanged(object sender, EventArgs e) { if (this.labelContainer.Text == "0") { this.pictureBoxMinus.Enabled = this.pictureBoxDirtMinus.Enabled = false; this.pictureBoxMinus.BackColor = this.pictureBoxDirtMinus.BackColor = Color.Gray; } else { this.pictureBoxMinus.Enabled = this.pictureBoxDirtMinus.Enabled = true; this.pictureBoxMinus.BackColor = this.pictureBoxDirtMinus.BackColor = Color.Red; } } /// /// TOSAVE /// //BEI CLICK AUF CONTAINER +/- SPEICHERN WIRD AKITV private void Container_Click(object sender, EventArgs e) { PictureBox pb = (PictureBox)sender; int cont = 0; if (pb.Name.Contains("Dirt")) { cont = int.Parse(this.labelContDirt.Text); if (pb.Name.ToString().Contains("Plus")) cont++; if (pb.Name.ToString().Contains("Minus")) cont--; this.labelContDirt.Text = cont.ToString(); } else { cont = int.Parse(this.labelContainer.Text); if (pb.Name.ToString().Contains("Plus")) cont++; if (pb.Name.ToString().Contains("Minus")) cont--; this.labelContainer.Text = cont.ToString(); } tosave = true; } //BEI CLICK AUF DATUM SPEICHERN WIRD AKTIV private void FormAuftragDetail_MouseClick(object sender, MouseEventArgs e) { Control ctr = this.GetChildAtPoint(e.Location); if (ctr != null) { if (ctr.Name.Contains("dTP")) { ctr.Enabled = true; tosave = true; } } } //WENN LISTVIEW NICHT FOCUSED (FORM.LOAD) TOSAVE BLEIBT FALSE private void oLVArtikel_ItemCheck(object sender, ItemCheckEventArgs e) { ObjectListView olv = (ObjectListView)sender; if (e.NewValue != e.CurrentValue) { tosave = true; } } /// /// TOCLOSE /// private void oLVArtikel_ItemChecked(object sender, ItemCheckedEventArgs e) { ObjectListView olv = (ObjectListView)sender; if (olv.Items.Count == olv.CheckedItems.Count) toclose = true; foreach(OLVListItem item in olv.CheckedItems) { AuftragArtikel auftragArtikel = (AuftragArtikel)item.RowObject; auftragArtikel.Erledigt = true; auftragArtikel.Save(); } } /// /// BUTTONS /// //ÄNDERUNGEN SPEICHERN private void buttonSpeichern_Click(object sender, EventArgs e) { if (tosave) { auftrag.Liefertag = dTPLiefertag.Value; auftrag.ContainerClean = int.Parse(this.labelContainer.Text); auftrag.Container = int.Parse(this.labelContDirt.Text); auftrag.ArbeiterID = this.comboBoxFahrer.SelectedIndex == -1 ? null : ((Benutzer)this.comboBoxFahrer.SelectedItem).BenutzerID; auftrag.Priority = this.comboBoxPriority.SelectedIndex == -1 ? AuftragPriority.None : ((AuftragPriority)this.comboBoxPriority.SelectedItem); auftrag.ZusatzInfo = this.textBoxZusatz.Text; auftrag.Save(); if (auftrag.Typ == AuftragTyp.Regelmäßig || (auftrag.Typ == AuftragTyp.Inventur & auftrag.Status == AuftragStatus.Herrichten)) { foreach (OLVListItem item in this.oLVArtikel.Items) { AuftragArtikel art = (AuftragArtikel)item.RowObject; if (item.Checked) art.Erledigt = true; else art.Erledigt = false; art.Save(); } if (auftrag.Typ == AuftragTyp.Inventur) { WriteToCSV(dictionary); } } tosave = false; } this.DialogResult = DialogResult.OK; this.Close(); } //AUFTRAG ABBRECHEN private void buttonAbbrechen_Click(object sender, EventArgs e) { //if (_tosave) //{ // if (MessageBox.Show("Möchtest du die Änderungen wirklich verwerfen?", "ÄNDERUNGEN VERWERFEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) // { // this.DialogResult = DialogResult.Cancel; // this.Close(); // } // else return; //} //else //{ // this.DialogResult = DialogResult.Cancel; // this.Close(); //} this.DialogResult = DialogResult.Cancel; this.Close(); } //AUFTRAG ABSCHLIESSEN private void buttonFertig_Click(object sender, EventArgs e) { if (toclose) { if (auftrag.Typ < AuftragTyp.Standerhöhung || auftrag.Typ > AuftragTyp.Regelmäßig) if (int.Parse(this.labelContainer.Text) == 0) if (meldung.NoCleanContainer() == DialogResult.OK) return; if (int.Parse(this.labelContainer.Text) != auftrag.ContainerClean) { switch (meldung.DifferentCleanContainer()) { case DialogResult.Cancel: break; case DialogResult.Yes: { auftrag.ContainerClean = int.Parse(this.labelContainer.Text); auftrag.Liefertag = dTPLiefertag.Value; //WENN BEARBEITET UND AUSGEDRUCKT WIRD GEDRUCKT VERÄNDERT UND ERSTELLT IST URSPRUNGSDATUM auftrag.Gedruckt = DateTime.Now; auftrag.GedrucktVon = benutzer.BenutzerID; auftrag.Status = AuftragStatus.Fertig; bool nocont = false; if (MessageBox.Show("Möchtest du die Containeranzahl am Etikett anzeigen?", "FRAGE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { nocont = true; } else nocont = false; DialogResult result = Funktionen.Etikett_Drucken(auftrag, nocont); if (result == DialogResult.OK) { this.auftrag.Save(); this.Close(); } } break; case DialogResult.No: { auftrag.ContainerClean = int.Parse(this.labelContainer.Text); auftrag.Liefertag = dTPLiefertag.Value; //WENN BEARBEITET UND NICHT GEDRUCKT WIRD ERSTELLT VERÄNDERT UND GEDRUCKT IST URSPRUNGSDATUM auftrag.Erstellt = DateTime.Now; auftrag.ErstelltVon = (int)benutzer.BenutzerID; auftrag.Status = AuftragStatus.Fertig; this.auftrag.Save(); this.Close(); } break; default: break; } } else { auftrag.Erledigt = DateTime.Now; auftrag.ErledigtVon = (int)benutzer.BenutzerID; auftrag.Status = AuftragStatus.Fertig; auftrag.Save(); } if (auftrag.Typ == AuftragTyp.Inventur) { string loadPath = Path.Combine(ConfigurationManager.AppSettings["DateiSavePfad"], "Listen\\Inventuren") ?? "Inventuren"; // Inventur File des aktuellen Auftrags auflisten. string[] invFiles = Directory.GetFiles(loadPath, $"Herrichten-{auftrag.AuftragID.ToString()}-{kunde.KundeNummer}.csv"); if (invFiles.Length == 1) { string ausgeliefertFile = $"Fertig-{auftrag.AuftragID.ToString()}-{kunde.KundeNummer}.csv"; File.Copy(Path.Combine(loadPath, invFiles[0]), Path.Combine(loadPath, ausgeliefertFile)); File.Delete(Path.Combine(loadPath, invFiles[0])); } } toclose = tosave = false; } this.DialogResult = DialogResult.OK; this.Close(); } //SCHMUTZWÄSCHESCHEIN DRUCKEN private void buttonSWS_Click(object sender, EventArgs e) { //TODO: Bestand von allen Artikeln speichern und für Druck vorbereiten. //TODO: Eventuell die Zahlen in CSV speichern und von dort für Druck verwenden?! //TODO: Erstellen von SonderAuftrag und Drucken von Etikett testen. if (MessageBox.Show("Möchtest du Etiketten für die Änderung drucken?", "FRAGE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Funktionen.SonderEtikett_Drucken(this.auftrag, dictionary); etikettgedruckt = true; } } /// /// SonderAuftrag in CSV-Liste eintragen. Jede Änderung wird in eine Zeile eingetragen. Rek./NW/Rest /// /// private void WriteToCSV(Dictionary dictionary) { Kunde _knd = Kunde.GetKunde(null, auftrag.KundeID, null); Benutzer _user = this.benutzer; List _artikelliste = KundeArtikel.GetList(_knd.KundeID.ToString()); string artikel = string.Empty; string artikelStand = string.Empty; string[] bestandNamen = new string[4] { "Rek.", "NW", "Rest", "INV" }; string fileName = $"{_knd.KundeNummer}_{_knd.Suchtext}.csv"; // Immer gleiche Datei! string savePath = ConfigurationManager.AppSettings["DateiSavePfad"]; string inventurOrdner = Path.Combine(savePath, "Listen\\Kundenstand") ?? "Kundenstand"; string csvPath = Path.Combine(inventurOrdner, fileName); // Prüfen ob Header schon existiert bool headerExists = File.Exists(csvPath); // Artikelbezeichnungen in string convertieren foreach (KundeArtikel art in _artikelliste) { artikel += ";" + art.ArtikelName; artikelStand += ";" + art.Stand.ToString(); } // Nur Header bei NEUER Datei if (!headerExists) { var csv = new StringBuilder(); csv.AppendLine("=== STANDVERÄNDERUNG ==="); csv.AppendLine($"KundeNr: {_knd.KundeNummer}"); csv.AppendLine($"Kunde: {_knd.KundeName}"); csv.AppendLine(new string('=', 30)); csv.AppendLine(); csv.AppendLine($"Erstellt am;Erstellt von;Hergerichtet am;Veränderung;{artikel}"); File.WriteAllText(csvPath, csv.ToString(), Encoding.UTF8); } // NEUE Artikel ANHÄNGEN (ohne Header) var appendCsv = new StringBuilder(); string difftrennzeichen = string.Empty; var artikelArray = artikel.Split(';'); foreach (var kvp in dictionary) { string artikelKey = kvp.Key; // z.B. "ART1" int[] artbestand = kvp.Value; // z.B. [3, 0, 5] int artikelIndex = Array.IndexOf(artikelArray, artikelKey); if (artikelIndex < 0) continue; for (int idxInBestand = 0; idxInBestand < artbestand.Length; idxInBestand++) { int art = artbestand[idxInBestand]; if (art == 0) continue; // Spalten: Datum, User, Heute, Bestandsname, dann alle Artikel‑Spalten int totalColumns = 4 + artikelArray.Length; var line = new string[totalColumns]; line[0] = auftrag.Erstellt.Value.Date.ToString("yyyy-MM-dd"); line[1] = _user.Vorname + " " + _user.Nachname; line[2] = DateTime.Today.ToString("yyyy-MM-dd"); line[3] = bestandNamen[idxInBestand]; // z.B. "Lager A" // Wert in der richtigen Artikel‑Spalte line[4 + artikelIndex] = art.ToString(); appendCsv.AppendLine(string.Join(";", line)); } } appendCsv.AppendLine($"{DateTime.Today.Date.ToString("yyyy-MM-dd")};;;Stand;{artikelStand}{difftrennzeichen}"); // ANHÄNGEN statt überschreiben File.AppendAllText(csvPath, appendCsv.ToString(), Encoding.UTF8); } /// /// UNUSED THINGS /// //PRINT PAGE FÜR DRUCK private void FormAuftragDetail_Shown(object sender, EventArgs e) { fb = new UCFeedback(benutzer); Button btn = fb.Controls[0] as Button; btn.BackColor = Program.Wirlblau; btn.ForeColor = Color.White; btn.FlatAppearance.BorderColor = Color.White; this.Controls.Add(fb); fb.BringToFront(); if (uci != null) { uci.Location = this.oLVArtikel.Location = new Point(this.oLVArtikel.Location.X, fb.Bottom + 2); uci.Visible = true; this.ClientSize = new Size(uci.Right, uci.Bottom + buttonAbbrechen.Height + 25); } else { this.oLVArtikel.Height = this.textBoxZusatz.Bottom - this.oLVArtikel.Top; this.ClientSize = new Size(oLVArtikel.Right + 10, oLVArtikel.Bottom + buttonAbbrechen.Height + 25); } if (fb != null) fb.Location = new Point(this.ClientSize.Width - fb.Width - 7, 0); while (this.Right > Screen.PrimaryScreen.WorkingArea.Width) { this.Width--; } //if (oLVArtikel != null) this.ClientSize = new Size(oLVArtikel.Right + 10, oLVArtikel.Bottom + buttonAbbrechen.Height + 25); } private void oLVArtikel_CellEditStarting(object sender, CellEditEventArgs e) { } private void oLVArtikel_CellEditFinished(object sender, CellEditEventArgs e) { this.Cursor = Cursors.WaitCursor; this.SuspendLayout(); ObjectListView olv = (ObjectListView)sender; AuftragArtikel auftragArtikel = (AuftragArtikel)e.RowObject; if (e.Column.Text == "Anzahl") diff = (int)e.Value - ((int)e.NewValue); if (diff > 0) tosave = true; auftragArtikel.Anzahl = (int)e.NewValue; auftragArtikel.Save(); if (auftragArtikel.Anzahl == 0) e.ListViewItem.Checked = true; KundeArtikel kndart = KundeArtikel.GetKundeArtikelVonArtikelID(this.kunde.KundeID, auftragArtikel.ArtikelID); int reklamation = kndart.Reklamation; int fehlmenge = kndart.Fehlmenge; int stand = kndart.Stand; int korrektur = kndart.Korrektur; KorrigiereBestand(diff, ref reklamation, ref fehlmenge, ref stand, ref korrektur); kndart.Reklamation = reklamation; kndart.Fehlmenge = fehlmenge; kndart.Stand = stand; kndart.Korrektur = korrektur; kndart.Save(); if (dictionary.TryGetValue(kndart.ArtikelName, out int[] value)) { for (int i = 0; i < value.Length; i++) { value[i] += bestand[i]; } } else dictionary.Add(kndart.ArtikelName, bestand); //WriteToCSV(kndart); this.ResumeLayout(); this.Cursor = Cursors.Default; } public void KorrigiereBestand(int diff, ref int reklamation, ref int fehlmenge, ref int stand, ref int korrektur) { bestand = new int[4]; if (diff == 0) return; if (diff < 0) { bestand[2] = diff; stand += diff; return; } int rest = diff * 1; // 1) Reklamation nur korrigieren, wenn sie <= diff ist if (reklamation > 0 && reklamation <= rest) { rest -= reklamation; bestand[0] = reklamation; reklamation = 0; } // 2) Fehlmenge korrigieren if (fehlmenge > 0) { int abbau = Math.Min(fehlmenge, rest); bestand[1] = abbau; fehlmenge -= abbau; rest -= abbau; } // 3) Inventurausgleich korrigieren if (korrektur > 0) { int korr = Math.Min(korrektur, rest); bestand[3] = korr; korrektur -= korr; rest -= korr; } // 4) Rest auf Stand buchen if (rest > 0) stand += rest; bestand[2] = rest; } private void oLVArtikel_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { tosave = true; } private void FormAuftragDetail_Resize(object sender, EventArgs e) { if (fb != null) fb.Location = new Point(this.ClientSize.Width - fb.Width - 7, 0); if (uci != null) this.ClientSize = new Size(uci.Right, uci.Bottom + buttonAbbrechen.Height + 25); if (oLVArtikel != null) this.ClientSize = new Size(oLVArtikel.Right + 10, oLVArtikel.Bottom + buttonAbbrechen.Height + 25); } private void oLVArtikel_SizeChanged(object sender, EventArgs e) { this.ClientSize = new Size(oLVArtikel.Right + 10, oLVArtikel.Bottom + buttonAbbrechen.Height + 25); } private void FormAuftragDetail_FormClosing(object sender, FormClosingEventArgs e) { if (tosave) { if (MessageBox.Show("Möchtest du die Änderungen wirklich verwerfen?", "ÄNDERUNGEN VERWERFEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.DialogResult = DialogResult.Cancel; this.Close(); } else { e.Cancel = true; return; } } } } } //DONE: Korrigiere Bestand: Wenn -Zahl dann Stand - Zahl. //DONE: Speichern etc. durchgehen und adaptieren //DONE: Feedback Button hinzufügen //DONE: ComboBox Fahrer und Priorität implementieren //DONE: Container schmutzig bearbeitbar machen