Programm_Wirl/AA-Forms/FormKundeVW.cs

541 lines
19 KiB
C#

using AForge.Video.DirectShow;
using BrightIdeasSoftware;
using DatenDB;
using Spire.Barcode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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 System.Windows.Forms.DataVisualization.Charting;
using ZXing;
namespace Deckungsbeitrag
{
public partial class FormKundeVW : Form
{
private FilterInfoCollection videoDevices;
List<Sortiment> sortimentListe = null;
List<Sortiment> resultList = null;
List<KundeArtikel> standListe = null;
Sortiment result = null;
Kunde kunde = null;
Bitmap bitmap = null;
Image qrcode = null;
Meldungen meldung = new Meldungen();
Benutzer benutzer;
int beforeedit = 0;
int[] saveindex = null;
private bool anmerkungChanged = false;
private bool formloading = false;
private bool aufgabeChanged = false;
private bool serviceChanged = false;
public FormKundeVW()
{
InitializeComponent();
}
public FormKundeVW(Kunde kunde) :this()
{
this.kunde = kunde;
}
public FormKundeVW(Benutzer benutzer) : this()
{
this.benutzer = benutzer;
}
private void KundeDaten_Load(object sender, EventArgs e)
{
formloading = true;
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//Sortiment Liste erstellen.
sortimentListe = Funktionen.SortimentLesen("Liste");
//Alle TextBoxen deaktivieren.
TextBoxes_Unable();
labelAnzahlChars.Text = textBoxAnmerkung.TextLength.ToString() + "/" + textBoxAnmerkung.MaxLength.ToString();
this.labelAnzahlChars.Location = new Point(this.textBoxAnmerkung.Right - this.labelAnzahlChars.Width, this.labelAnzahlChars.Location.Y);
this.tabControlKunde.Location = new Point(this.textBoxSuchtext.Right + 10, 11);
ComboBox_Laden();
this.kunde = Funktionen.KundenAuswahl();
if (this.kunde != null) DatenLesen();
formloading = false;
}
private void ComboBox_Laden()
{
// Service Liste erstellen und mit ComboBox verknüpfen.
this.comboBoxService.Enabled = false;
List<string> list = Kunde.GetServiceDistinct();
this.comboBoxService.DataSource = list;
//Aufgaben Liste erstellen und mit ComboBox verknüpfen.
List<Aufgabe> aufgabenlist1 = Aufgabe.GetList(2);
this.comboBoxAufgabe.DataSource = aufgabenlist1;
this.comboBoxAufgabe.DisplayMember = "Bezeichnung";
this.comboBoxAufgabe.SelectedIndex = -1;
this.comboBoxAufgabe.Enabled = false;
}
private void DatenLesen()
{
//TextBoxen deaktivieren.
TextBoxes_Unable();
//Alle Einträge der DataGridView entfernen. Davor muss die DataSource entfernt werden.
this.dGArtikel.DataSource = null;
this.dGArtikel.Rows.Clear();
//ComboBox für Sonderaufgaben wie "In Säcke Verpacken".
this.comboBoxAufgabe.Enabled = true;
//Properties für Standliste.
//sortNr ist falls noch kein Stand hinterlegt wurde
string sortNr = string.Empty;
//kundeid ist falls bereits ein Stand existiert und die Artikel gespeichert wurden.
string kundeid = this.kunde.KundeID.ToString();
// Wenn bereits ein Stand gespeichert ist, kommt er aus der DB. Wenn kein Stand gespeichert kommt Liste aus Sortiment.
if (KundeArtikel.GetList(kundeid).Count > 0)
{
this.standListe = KundeArtikel.GetList(kundeid);
Load_Gridview(this.standListe);
}
else
{
//Jedes Innsbrucker Soziale Dienste Sortiment ist gleich.
if (this.kunde.KundeNummer == "010069" | this.kunde.KundeNummer == "010071") sortNr = "010068";
else sortNr = this.kunde.KundeNummer;
//Sortiment holen und Standliste filtern.
standListe = new List<KundeArtikel>();
foreach(Sortiment sort in sortimentListe.FindAll(Sortiment => Sortiment.KundeNummer == sortNr))
{
KundeArtikel item = new KundeArtikel();
item.ArtikelNR = sort.ArtNr;
item.ArtikelName = sort.ArtName;
item.KundeID = Kunde.GetKundeID(sort.KundeNummer, null);
standListe.Add(item);
}
//GridView mit der jeweiligen Liste füllen.
Load_Gridview(this.standListe);
}
// QR-Code wird erstellt
BarcodeWriter code = new BarcodeWriter();
code.Format = BarcodeFormat.QR_CODE;
qrcode = code.Write(this.kunde.KundeNummer);
this.pictureBoxQRCode.Image = qrcode;
this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.Zoom;
// SWS-Entwurf wird erstellt und angezeigt
this.pictureBoxEntwurf.Image = Funktionen.SWS_Entwurf(null, this.kunde);
this.pictureBoxEntwurf.Visible = true;
// Restliche TextBoxen werden befüllt
this.textBoxKndNr.Text = this.kunde.KundeNummer;
this.textBoxKundeName.Text = this.kunde.KundeName;
this.textBoxKndName2.Text = this.kunde.KundeName2;
this.textBoxStraße.Text = this.kunde.Strasse;
this.textBoxPLZ.Text = this.kunde.PLZ.ToString();
this.textBoxOrt.Text = this.kunde.Ort;
this.textBoxSuchtext.Text = this.kunde.Suchtext;
this.textBoxAnmerkung.Text = this.kunde.Anmerkung;
// ComboBox für Aufgabe wird geladen
foreach (Aufgabe auf in comboBoxAufgabe.Items) if (auf.AufgabeID == this.kunde.Aufgabe) this.comboBoxAufgabe.SelectedItem = auf;
if (this.comboBoxAufgabe.SelectedItem != null) this.buttonAufgEnt.Enabled = true;
if (!string.IsNullOrWhiteSpace(this.kunde.Service))
{
foreach (string s in comboBoxService.Items) if (s == this.kunde.Service) this.comboBoxService.SelectedItem = s;
}
// numericUpDown für AufgabeCount befüllen
this.numericUpDownAufgabeCount.Value = (decimal)this.kunde.AufgabeCount;
// Wird noch nicht verwendet
this.buttonProgramm.Text = "WP 01";
// Buttons und ähnliches aktivieren.
this.buttonProgramm.Enabled = true;
this.buttonBewertung.Enabled = true;
this.tabControlKunde.Enabled = true;
OLV_Load();
Chart_Load();
}
/// <summary>
/// TextBox Events
/// </summary>
private void TextBoxes_Unable()
{
foreach (Control ctr in this.Controls) if (ctr is TextBox) { if (!ctr.Name.Contains("Anmerkung")) ctr.Enabled = false; }
}
private void TextBoxKndNr_Leave(object sender, EventArgs e)
{
}
private void TSBBeenden_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void ButtonDrucken_Click(object sender, EventArgs e)
{
Funktionen.SWS_Drucken(null, this.kunde, this);
}
private void TSBNext_Click(object sender, EventArgs e)
{
// Kundenliste öffnen und Kunde auswählen.
this.kunde = Funktionen.KundenAuswahl();
if (kunde != null) DatenLesen();
}
private void ButtonProgramm_Click(object sender, EventArgs e)
{
FormProgrammauswahl auswahl = new FormProgrammauswahl();
if (auswahl.ShowDialog() == DialogResult.OK)
{
DatenLesen();
}
}
private void buttonAufgEnt_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Möchtest du die Aufgabe wirklich löschen?", "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.comboBoxAufgabe.SelectedIndex = -1;
this.kunde.Aufgabe = null;
this.kunde.Save();
}
}
private void OLV_Load()
{
foreach(ObjectListView olv in this.Controls.OfType<ObjectListView>())
{
olv.HeaderFormatStyle = Funktionen.GetHeader();
olv.OwnerDraw = true;
Generator.GenerateColumns(olv, typeof(OLVKundenumsatz), true);
if (olv.Name.Contains("Jahresumsatz"))
{
foreach (OLVColumn c in olv.Columns) if (c.Name == "Zeitraum") c.Text = "Jahr";
olv.SetObjects(OLVKundenumsatz.GetJahresumsatz(this.kunde.KundeID));
}
if (olv.Name.Contains("Quartalsumsatz"))
{
foreach (OLVColumn c in olv.Columns) if (c.Name == "Zeitraum") c.Text = "Quartal";
olv.SetObjects(OLVKundenumsatz.GetQuartalsumsatz(this.kunde.KundeID));
}
if (olv.Name.Contains("Monatsumsatz"))
{
foreach (OLVColumn c in olv.Columns) if (c.Name == "Zeitraum") c.Text = "Monat";
olv.SetObjects(OLVKundenumsatz.GetMonatsumsatz(this.kunde.KundeID));
}
}
}
private void Chart_Load()
{
foreach (Chart chart in this.Controls.OfType<Chart>()) chart.Visible = true;
//IMPLEMENTIEREN
ClassChart ch = ClassChart.FillChart(this.kunde.KundeID);
//Chart Jahresumsatz
List<OLVKundenumsatz> olv = OLVKundenumsatz.GetJahresumsatz(this.kunde.KundeID);
string[] x = new string[olv.Count()];
double[] y = new double[olv.Count()];
for (int i = 0; i < y.Length; i++)
{
y[i] = olv[i].Umsatz;
x[i] = olv[i].Zeitraum;
}
this.chartJahresumsatz.Series[0].LegendText = "Umsatz";
this.chartJahresumsatz.Series[0].ChartType = SeriesChartType.Column;
this.chartJahresumsatz.Series[0].IsValueShownAsLabel = true;
this.chartJahresumsatz.Series[0].Points.DataBindXY(x, y);
//Chart Quartalsumsatz
List<OLVKundenumsatz> olvq = OLVKundenumsatz.GetQuartalsumsatz(this.kunde.KundeID);
string[] xq = new string[olvq.Count()];
double[] yq = new double[olvq.Count()];
for (int i = 0; i < olvq.Count(); i++)
{
yq[i] = olvq[i].Umsatz;
xq[i] = olvq[i].Zeitraum;
}
this.chartQuartalsumsatz.Series[0].LegendText = "Umsatz";
this.chartQuartalsumsatz.Series[0].ChartType = SeriesChartType.Bar;
this.chartQuartalsumsatz.Series[0].IsValueShownAsLabel = true;
this.chartQuartalsumsatz.Series[0].Points.DataBindXY(xq, yq);
}
private void comboBoxAufgabe_DropDownClosed(object sender, EventArgs e)
{
}
private void KundeDaten_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.End) TSBNext_Click(this, e);
}
/// <summary>
/// Events für ShortCut zu "Nächster Kunde"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void nextToolStripMenuItem_Click(object sender, EventArgs e)
{
TSBNext_Click(this, e);
}
private void nächsterKundeToolStripMenuItem_Click(object sender, EventArgs e)
{
TSBNext_Click(this, e);
}
/// <summary>
/// Laden der DataGridView zur Stand und Fehlmengenverwaltung.
/// </summary>
/// <param name="objectListe"></param>
private void Load_Gridview(List<KundeArtikel> objectListe)
{
if (objectListe.Count > 0)
{
this.dGArtikel.AutoGenerateColumns = false;
foreach (DataGridViewColumn col in this.dGArtikel.Columns)
{
col.DataPropertyName = col.Name;
}
// DataGridView mit ArtikelStand Liste verbinden und Columns automatisch generieren.
this.dGArtikel.DataSource = objectListe;
this.dGArtikel.ClearSelection();
this.dGArtikel.CurrentCell = this.dGArtikel.Rows[0].Cells[3];
// Alle Spaltenbreiten an den Zellinhalt anpassen
this.dGArtikel.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
// Alle Zeilenhöhen an Zellinhalt anpassen
this.dGArtikel.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}
}
/// <summary>
/// Speichern von Stand und Fehlmengenänderungen.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void standSpeichernToolStripMenuItem_Click(object sender, EventArgs e)
{
int tosave = 0;
int toupdate = 0;
int saved = 0;
this.dGArtikel.EndEdit();
// Speichern der Zeilen aus der DataGridView
foreach(DataGridViewRow row in this.dGArtikel.Rows)
{
KundeArtikel item = KundeArtikel.GetItemIfAvailable(int.Parse(row.Cells[1].Value.ToString()), int.Parse(row.Cells[2].Value.ToString()));
if(item != null)
{
++toupdate;
if (int.Parse(row.Cells[4].Value.ToString()) != 0)
{
item.Stand = int.Parse(row.Cells[4].Value.ToString());
item.StandBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (int.Parse(row.Cells[5].Value.ToString()) != 0)
{
item.Fehlmenge = int.Parse(row.Cells[5].Value.ToString());
item.FehlmengeBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (int.Parse(row.Cells[6].Value.ToString()) != 0)
{
item.Korrektur = int.Parse(row.Cells[6].Value.ToString());
item.KorrekturBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (item.Save() == 1) saved++;
}
else
{
++tosave;
item.KundeArtikelID = null;
item.KundeID = int.Parse(row.Cells[1].Value.ToString());
item.ArtikelNR = int.Parse(row.Cells[2].Value.ToString());
item.ArtikelName = row.Cells[3].Value.ToString();
if (int.Parse(row.Cells[4].Value.ToString()) != 0)
{
item.Stand = int.Parse(row.Cells[4].Value.ToString());
item.StandBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (int.Parse(row.Cells[5].Value.ToString()) != 0)
{
item.Fehlmenge = int.Parse(row.Cells[5].Value.ToString());
item.FehlmengeBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (int.Parse(row.Cells[6].Value.ToString()) != 0)
{
item.Korrektur = int.Parse(row.Cells[6].Value.ToString());
item.KorrekturBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (item.Save() == 1) saved++;
}
}
// Überprüfung ob alle Zeilen gespeichert wurden.
if ((toupdate + tosave) == saved) meldung.Gespeichert();
else meldung.Speicherfehler();
Load_Gridview(this.standListe);
}
private void dGArtikel_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView dataGrid = sender as DataGridView;
beforeedit = int.Parse(dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
private void dGArtikel_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridView dg = sender as DataGridView;
DataGridViewRow row = dg.Rows[e.RowIndex];
DataGridViewCell cell = dg.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell != null)
{
// Wenn der Zellenwert sich geändert hat, wird die entsprechende Zahl in der DB upgedatet.
if (beforeedit != int.Parse(cell.Value.ToString()))
{
if (row.Cells[0].Value != null)
{
KundeArtikel item = KundeArtikel.GetItem(int.Parse(row.Cells[0].Value.ToString()));
item.KundeArtikelID = int.Parse(row.Cells[0].Value.ToString());
if (row.Cells[e.ColumnIndex].OwningColumn.Name == "Stand")
{
item.Stand = int.Parse(row.Cells[e.ColumnIndex].Value.ToString());
item.StandBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (row.Cells[e.ColumnIndex].OwningColumn.Name == "Fehlmenge")
{
item.Fehlmenge = int.Parse(row.Cells[e.ColumnIndex].Value.ToString());
item.FehlmengeBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (row.Cells[e.ColumnIndex].OwningColumn.Name == "Korrektur")
{
item.Korrektur = int.Parse(row.Cells[e.ColumnIndex].Value.ToString());
item.KorrekturBearbeitet = this.benutzer.BenutzerName + " am " + DateTime.Now.ToString();
}
if (item.Save() != 1) meldung.Speicherfehler();
}
}
}
}
}
private void buttonServiceBearbeiten_Click(object sender, EventArgs e)
{
if(this.comboBoxService.Enabled == false) this.comboBoxService.Enabled = true;
}
private void textBoxAnmerkung_Leave(object sender, EventArgs e)
{
if (textBoxAnmerkung.TextLength > 0) if (meldung.GetFrage(this, "Möchtest du die Anmerkung speichern?", false) == DialogResult.Yes) { kunde.Anmerkung = textBoxAnmerkung.Text; kunde.Save(); }
}
private void numericUpDownAufgabeCount_Leave(object sender, EventArgs e)
{
if (meldung.GetFrage(this, $"Möchtest du die Aufgabe für die nächsten {numericUpDownAufgabeCount.Value.ToString()} Aufträge speichern?", false) == DialogResult.Yes)
{
if (this.comboBoxAufgabe.SelectedItem == null) { this.kunde.Aufgabe = null; this.kunde.AufgabeCount = null; }
else { this.kunde.Aufgabe = ((Aufgabe)this.comboBoxAufgabe.SelectedItem).AufgabeID; this.kunde.AufgabeCount = (int)numericUpDownAufgabeCount.Value; }
this.kunde.Save();
}
}
/// <summary>
/// ValueChanged Events zum Speichern wenn Changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxAnmerkung_TextChanged(object sender, EventArgs e)
{
if (formloading) return;
anmerkungChanged = true;
labelAnzahlChars.Text = textBoxAnmerkung.TextLength.ToString() + "/" + textBoxAnmerkung.MaxLength.ToString();
}
private void comboBoxAufgabe_SelectedIndexChanged(object sender, EventArgs e)
{
if (formloading) return;
aufgabeChanged = true;
}
private void numericUpDownAufgabeCount_ValueChanged(object sender, EventArgs e)
{
if (formloading) return;
aufgabeChanged = true;
}
private void comboBoxService_SelectedValueChanged(object sender, EventArgs e)
{
if ((formloading)) return;
serviceChanged = true;
}
/// <summary>
/// FormClosing: Wenn Form wird geschlossen, Kontrolle ob Änderungen gemacht wurden.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormKundeVW_FormClosing(object sender, FormClosingEventArgs e)
{
if (anmerkungChanged) textBoxAnmerkung_Leave(this, null);
if (this.comboBoxAufgabe.SelectedItem != null && this.numericUpDownAufgabeCount.Value == 0) { meldung.GetFehler(this, $"Du hast für deine Aufgabe noch keine Auftraganzahl eingegeben. Hole das bitte jetzt nach.", false); e.Cancel = true; return; }
if (aufgabeChanged) numericUpDownAufgabeCount_Leave(this, null);
if (serviceChanged)
{
if (this.comboBoxService.Enabled == true)
{
this.kunde.Service = this.comboBoxService.SelectedValue.ToString();
this.kunde.Save();
}
}
}
}
}