Programm_Wirl/AA-Forms/FormAuftragDetail.cs

642 lines
22 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 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<string, int[]> dictionary = new Dictionary<string, int[]>();
public Auftrag auftrag;
public Benutzer benutzer;
public List<AuftragArtikel> 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)
{
buttonFertig.Enabled = true;
buttonFertig.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);
}
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) buttonFertig.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<Benutzer> benutzerliste = Benutzer.GetFahrerList();
this.comboBoxFahrer.DataSource = benutzerliste;
this.comboBoxFahrer.ValueMember = "BenutzerName";
this.comboBoxPriority.DataSource = Enum.GetValues(typeof(Priority));
}
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; }
}
//ÄNDERUNGEN SPEICHERN
private void AuftragSpeichern()
{
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 ? Priority.None : ((Priority)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;
}
//SCHMUTZWÄSCHESCHEIN DRUCKEN
private void EtikettDrucken()
{
//DONE: Bestand von allen Artikeln speichern und für Druck vorbereiten.
//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;
}
}
#region 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; }
}
#endregion
#region Button-Click
//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)
{
EtikettDrucken();
if (Tosave) AuftragSpeichern();
if (Toclose)
{
if (int.Parse(this.labelContainer.Text) == 0)
{
switch (auftrag.Typ)
{
case AuftragTyp.Geschäft:
meldung.NoCleanContainer();
return;
case AuftragTyp.Standart:
meldung.NoCleanContainer();
return;
case AuftragTyp.Inventur:
break;
case AuftragTyp.SOLL_Erhoehung:
break;
case AuftragTyp.SOLL_Verminderung:
break;
case AuftragTyp.Regelmäßig:
break;
case AuftragTyp.AufAbruf:
meldung.NoCleanContainer();
return;
case AuftragTyp.Laufzettel:
break;
default:
break;
}
}
if (int.Parse(this.labelContainer.Text) != auftrag.ContainerClean)
{
switch (meldung.DifferentCleanContainer())
{
case DialogResult.Cancel:
return;
case DialogResult.Yes:
{
auftrag.ContainerClean = int.Parse(this.labelContainer.Text);
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;
Funktionen.Etikett_Drucken(auftrag, nocont);
}
break;
case DialogResult.No:
{
auftrag.ContainerClean = int.Parse(this.labelContainer.Text);
auftrag.Erstellt = DateTime.Now;
auftrag.ErstelltVon = (int)benutzer.BenutzerID;
auftrag.Status = AuftragStatus.Fertig;
}
break;
default:
break;
}
}
else
{
auftrag.Erledigt = DateTime.Now;
auftrag.ErledigtVon = (int)benutzer.BenutzerID;
auftrag.Status = AuftragStatus.Fertig;
}
auftrag.Save();
// Wenn Inventur wird Fertig-File erstellt.
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();
}
#endregion
/// <summary>
/// TOCLOSE
/// </summary>
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();
}
}
/// <summary>
/// SonderAuftrag in CSV-Liste eintragen. Jede Änderung wird in eine Zeile eingetragen. Rek./NW/Rest
/// </summary>
/// <param name="kndart"></param>
private void WriteToCSV(Dictionary<string, int[]> dictionary)
{
Kunde _knd = Kunde.GetKunde(null, auftrag.KundeID, null);
Benutzer _user = this.benutzer;
List<KundeArtikel> _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 ArtikelSpalten
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 ArtikelSpalte
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);
}
//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