using BrightIdeasSoftware; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.Remoting.Metadata.W3cXsd2001; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DatenDB { public class Kunde { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public const string COLUMNS = "kunde_id, tour_id, kunde_nr, name1, name2, straße, plz, bezeichnung, land, aktiv, bettenanzahl, kunde_gruppe, service, region, waescheart, suchtext, aufgabe, anmerkung, aufgabe_count"; private const string RED_COLUMNS = "kunde_id, tour_id, kunde_nr, name1, name2, straße, plz, bezeichnung, land, aktiv, bettenanzahl, kunde_gruppe, service, region, waescheart, jahr, umsatz"; public const string TABLE = "kundenverwaltung.kunde"; public const string VIEW = "kundenverwaltung.jahresumsaetze"; //RICHTIGE VIEW AUSSUCHEN FÜR INAKTIVE MIT LETZTEN UMSATZ... public Umsatz umsatz; public Kunde() { } // LEER public static List GetTmpList(string text) { bool endsWithSpecial = !char.IsLetterOrDigit(text[text.Length-1]); DatenbankConnection.GetConnection().Open(); List resultList = new List(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (string.IsNullOrEmpty(text)) command.CommandText = $"select {COLUMNS} from {TABLE} where aktiv = {true}"; else { if (!endsWithSpecial) { if (int.TryParse(text, out _)) command.CommandText = $"select {COLUMNS} from {TABLE} where kunde_nr::varchar ~* '{text}'"; else command.CommandText = $"select {COLUMNS} from {TABLE} where suchtext ~* '{text}' or name1 ~* '{text}'"; } else if (int.TryParse(text, out _)) command.CommandText = $"select {COLUMNS} from {TABLE} where kunde_nr::varchar ~* '{text.Substring(0, 6)}'"; //TODO: Testen ob SWS-QRCodes gelesen werden können. Nur notwendig bis alle SWS-QRCodes richtig gedruckt und gelesen werden. } NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Kunde(reader)); reader.Close(); } DatenbankConnection.GetConnection().Close(); return resultList; } public static int GetKundeID(string kndnr, string text) { DatenbankConnection.GetConnection().Open(); int result = 0; using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (!string.IsNullOrEmpty(kndnr)) command.CommandText = $"select kunde_id from {TABLE} where kunde_nr = '{kndnr}'"; else command.CommandText = $"select kunde_id from {TABLE} where suchtext = '{text}'"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = reader.GetInt32(0); reader.Close(); } DatenbankConnection.GetConnection().Close(); return result; } public static List GetServiceDistinct() { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"SELECT distinct service FROM {TABLE} where service is not null"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(reader.GetString(0)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetList(bool? isaktiv, string text) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (isaktiv != null) command.CommandText = $"select {COLUMNS} from {TABLE} where aktiv = {isaktiv}"; if (!string.IsNullOrWhiteSpace(text))command.CommandText = $"select {COLUMNS} from {TABLE} where suchtext ~* '{text}'"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Kunde(reader, false)); reader.Close(); } DatenbankConnection.GetConnection().Close(); foreach(Kunde kunde in resultList) { List list = new List(); list = OLVKundenumsatz.GetJahresumsatz(kunde.KundeID); if(list.Count > 0) { kunde.Zeitraum = double.Parse(OLVKundenumsatz.GetJahresumsatz(kunde.KundeID).Last().Zeitraum); kunde.Umsatz1 = OLVKundenumsatz.GetJahresumsatz(kunde.KundeID).Last().Umsatz; } else { kunde.Zeitraum = 0; kunde.Umsatz1 = 0; } } return resultList; } //INAKTIV KUNDENLISTE LADEN... public static int GetBettenAnzahl(string bett) { DatenbankConnection.GetConnection().Open(); int betten = 0; using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (bett == "Miete") command.CommandText = $"select sum(bettenanzahl) from {TABLE} where aktiv = {true} and waescheart = '{bett}'"; else command.CommandText = $"select sum(bettenanzahl) from {TABLE} where aktiv = {true}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) betten = reader.IsDBNull(0) ? 0 : reader.GetInt16(0); reader.Close(); } DatenbankConnection.GetConnection().Close(); return betten; } public static Kunde GetKunde(string nr, int? id, string name) { DatenbankConnection.GetConnection().Open(); Kunde kunde = null; using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if(nr != null) { if (int.TryParse(nr, out int _)) { command.CommandText = $"select {COLUMNS} from {TABLE} where kunde_nr = '{nr}'"; } else command.CommandText = $"select {COLUMNS} from {TABLE} where suchtext = '{nr}'"; } if (id != null) command.CommandText = $"select {COLUMNS} from {TABLE} where kunde_id = {id}"; if (!string.IsNullOrEmpty(name)) { command.CommandText = $"select {COLUMNS} from {TABLE} where LOWER(name1) = '{name}'"; } NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) kunde = new Kunde(reader); reader.Close(); } DatenbankConnection.GetConnection().Close(); return kunde; } public Kunde(NpgsqlDataReader reader, bool mitUmsatz) { if (!mitUmsatz) { this.KundeID = reader.GetInt32(0); this.TourID = reader.IsDBNull(1) ? null : (int?)reader.GetInt32(1); this.KundeNummer = reader.GetString(2); this.KundeName = reader.IsDBNull(3) ? string.Empty : reader.GetString(3); this.KundeName2 = reader.IsDBNull(4) ? string.Empty : reader.GetString(4); this.Strasse = reader.IsDBNull(5) ? string.Empty : reader.GetString(5); this.PLZ = reader.IsDBNull(6) ? null : (int?)reader.GetInt32(6); this.Ort = reader.IsDBNull(7) ? string.Empty : reader.GetString(7); this.Land = reader.IsDBNull(8) ? string.Empty : reader.GetString(8); this.Aktiv = reader.GetBoolean(9); this.Bettenanzahl = reader.IsDBNull(10) ? null : (int?)reader.GetInt32(10); this.KundeGruppe = reader.IsDBNull(11) ? string.Empty : reader.GetString(11); this.Service = reader.IsDBNull(12) ? string.Empty : reader.GetString(12); this.Region = reader.IsDBNull(13) ? string.Empty : reader.GetString(13); this.Waescheart = reader.IsDBNull(14) ? string.Empty : reader.GetString(14); this.Suchtext = reader.IsDBNull(15) ? string.Empty : reader.GetString(15); this.Aufgabe = reader.IsDBNull(16) ? null : (int?)reader.GetInt32(16); this.Anmerkung = reader.IsDBNull(17) ? string.Empty : reader.GetString(17); this.AufgabeCount = reader.IsDBNull(18) ? 0 : reader.GetInt32(18); } } public Kunde(NpgsqlDataReader reader) { this.KundeID = reader.GetInt32(0); this.TourID = reader.IsDBNull(1) ? null : (int?)reader.GetInt32(1); this.KundeNummer = reader.GetString(2); this.KundeName = reader.IsDBNull(3) ? string.Empty : reader.GetString(3); this.KundeName2 = reader.IsDBNull(4) ? string.Empty : reader.GetString(4); this.Strasse = reader.IsDBNull(5) ? string.Empty : reader.GetString(5); this.PLZ = reader.IsDBNull(6) ? null : (int?)reader.GetInt32(6); this.Ort = reader.IsDBNull(7) ? string.Empty : reader.GetString(7); this.Land = reader.IsDBNull(8) ? string.Empty : reader.GetString(8); this.Aktiv = reader.GetBoolean(9); this.Bettenanzahl = reader.IsDBNull(10) ? null : (int?)reader.GetInt32(10); this.KundeGruppe = reader.IsDBNull(11) ? string.Empty : reader.GetString(11); this.Service = reader.IsDBNull(12) ? string.Empty : reader.GetString(12); this.Region = reader.IsDBNull(13) ? string.Empty : reader.GetString(13); this.Waescheart = reader.IsDBNull(14) ? string.Empty : reader.GetString(14); this.Suchtext = reader.IsDBNull(15) ? string.Empty : reader.GetString(15); this.Aufgabe = reader.IsDBNull(16) ? null : (int?)reader.GetInt32(16); this.Anmerkung = reader.IsDBNull(17) ? string.Empty : reader.GetString(17); this.AufgabeCount = reader.IsDBNull(18) ? 0 : reader.GetInt32(18); } public int Save() { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (this.KundeID.HasValue & this.KundeID != 0) { command.CommandText = $"update {TABLE} set tour_id = :p1, kunde_nr = :p2, name1 = :p3, name2 = :p4, straße = :p5, plz = :p6, bezeichnung = :p7, land = :p8, aktiv = :p9, bettenanzahl = :p10, kunde_gruppe = :p11, service = :p12, region = :p13, waescheart = :p14, suchtext = :p15, aufgabe = :p16, anmerkung = :p17, aufgabe_count = :p18 WHERE kunde_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.kunde_seq')"; this.KundeID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16, :p17, :p18)"; } command.Parameters.AddWithValue("p0", this.KundeID); command.Parameters.AddWithValue("p1", this.TourID ?? (object)DBNull.Value); command.Parameters.AddWithValue("p2", this.KundeNummer); command.Parameters.AddWithValue("p3", string.IsNullOrEmpty(this.KundeName) ? (object)DBNull.Value : this.KundeName); command.Parameters.AddWithValue("p4", string.IsNullOrEmpty(this.KundeName2) ? (object)DBNull.Value : this.KundeName2); command.Parameters.AddWithValue("p5", string.IsNullOrEmpty(this.Strasse) ? (object)DBNull.Value : this.Strasse); command.Parameters.AddWithValue("p6", this.PLZ ?? (object)DBNull.Value); command.Parameters.AddWithValue("p7", string.IsNullOrEmpty(this.Ort) ? (object)DBNull.Value : this.Ort); command.Parameters.AddWithValue("p8", string.IsNullOrEmpty(this.Land) ? (object)DBNull.Value : this.Land); command.Parameters.AddWithValue("p9", this.Aktiv); command.Parameters.AddWithValue("p10", this.Bettenanzahl ?? (object)DBNull.Value); command.Parameters.AddWithValue("p11", string.IsNullOrEmpty(this.KundeGruppe) ? (object)DBNull.Value : this.KundeGruppe); command.Parameters.AddWithValue("p12", string.IsNullOrEmpty(this.Service) ? (object)DBNull.Value : this.Service); command.Parameters.AddWithValue("p13", string.IsNullOrEmpty(this.Region) ? (object)DBNull.Value : this.Region); command.Parameters.AddWithValue("p14", string.IsNullOrEmpty(this.Waescheart) ? (object)DBNull.Value : this.Waescheart); command.Parameters.AddWithValue("p15", string.IsNullOrEmpty(this.Suchtext) ? (object)DBNull.Value : this.Suchtext); command.Parameters.AddWithValue("p16", this.Aufgabe ?? (object)DBNull.Value); command.Parameters.AddWithValue("p17", string.IsNullOrWhiteSpace(this.Anmerkung) ? (object)DBNull.Value : this.Anmerkung); command.Parameters.AddWithValue("p18", this.AufgabeCount ?? (object)DBNull.Value); int result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } } public int CheckKndNr(string v) { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select kunde_id from {TABLE} where kunde_nr = '{v}'"; int result = 0; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = reader.GetInt16(0); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; } } public static int GetAnzahlKnd(bool? aktiv) { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (aktiv != null) command.CommandText = $"select count (*) from {TABLE} where aktiv = '{aktiv}'"; else command.CommandText = $"select count (*) from {TABLE}"; int result = (int)(long)command.ExecuteScalar(); DatenbankConnection.GetConnection().Close(); return result; } } #region Properties [OLVIgnore] public int? KundeID { get; set; } [OLVIgnore] public int? TourID { get; set; } [OLVColumn(IsVisible = false)] public string KundeGruppe { get; set; } [OLVColumn("KndNr.", DisplayIndex = 0, IsTileViewColumn = true, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public string KundeNummer { get; set; } [OLVColumn("Name", DisplayIndex = 1)] public string KundeName { get; set; } [OLVColumn("Zusatz", DisplayIndex = 2)] public string KundeName2 { get; set; } [OLVColumn(DisplayIndex = 3)] public string Strasse { get; set; } [OLVColumn(DisplayIndex = 4)] public string HausNr { get; set; } [OLVColumn(DisplayIndex = 5, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public int? PLZ { get; set; } [OLVColumn(DisplayIndex = 6)] public string Ort { get; set; } [OLVColumn(DisplayIndex = 7, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public string Land { get; set; } [OLVColumn("Betten", DisplayIndex = 8, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public int? Bettenanzahl { get; set; } [OLVColumn(CheckBoxes = true, DisplayIndex = 9)] public bool Aktiv { get; set; } = true; [OLVColumn(DisplayIndex = 10)] public string Service { get; set; } [OLVColumn(DisplayIndex = 11)] public string Region { get; set; } [OLVColumn(DisplayIndex = 12)] public string Waescheart { get; set; } [OLVColumn("Umsatz", DisplayIndex = 15, TextAlign = System.Windows.Forms.HorizontalAlignment.Center, AspectToStringFormat ="{0:C}")] public double? Umsatz1 { get; set; } [OLVIgnore] public double? Umsatz2 { get; set; } [OLVIgnore] public double? DifferenzEUR { get; set; } [OLVIgnore] public double? DifferenzP { get; set; } [OLVIgnore] public double? UmsatzAnteil { get; set; } [OLVIgnore] public double? KostenAnteil { get; set; } [OLVIgnore] public double? GastUmsatz { get; set; } [OLVIgnore] public double? BewertungEUR { get; set; } [OLVIgnore] public string Bewertung { get; set; } [OLVColumn("Jahr", DisplayIndex = 14, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public double Zeitraum { get; set; } [OLVColumn(DisplayIndex = 13)] public string Suchtext { get; set; } [OLVIgnore] public int? Aufgabe { get; set; } [OLVIgnore] public int? AufgabeCount { get; set; } [OLVIgnore] public string Anmerkung { get; set; } #endregion } } //INSTALL-REMINDER: Spalte Anmerkung in DB hinzufügen. //INSTALL-REMINDER: Spalte Aufgabe Count in DB hinzufügen.