using BrightIdeasSoftware; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DatenDB { public enum KostenKat { FixLohn = 0, Variabel = 1, FixMiete = 2, Rest = 3 } public class Kosten { private const string COLUMNS = "kosten_id, konto_nr, bezeichnung, datum, kategorie, betrag"; private const string SUMCOLUMNS = "min(kosten_id), konto_nr, min(bezeichnung) as bezeichnung, min(datum) as datum, min(kategorie) as kategorie, sum(betrag) as betrag"; private const string TABLE = "kundenverwaltung.kosten"; public Kosten() { } public static List GetList(int? aktjahr, int? monat, int? aktquartal, bool aktiv, int vpjahr, int[] quartale) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (monat == null || monat == 0) { if (aktquartal == null || aktquartal == 0) { if (quartale == null || quartale.Length == 0) { command.CommandText = $"select {SUMCOLUMNS} from {TABLE} where date_part('year', datum) = {aktjahr} group by konto_nr order by konto_nr, datum"; } else { command.CommandText = $"select {SUMCOLUMNS} from {TABLE} where date_part('year', datum) = {aktjahr} and date_part('quarter', datum) between {quartale.Min()} and {quartale.Max()} group by konto_nr order by konto_nr, datum"; } } else { if (aktquartal == 4) command.CommandText = $"select {SUMCOLUMNS} from {TABLE} where date_part('year', datum) = {aktjahr} group by konto_nr order by konto_nr, datum"; else command.CommandText = $"select {SUMCOLUMNS} from {TABLE} where date_part('quarter', datum) = {aktquartal} and date_part('year', datum) = {aktjahr} group by konto_nr order by konto_nr, datum"; } } else command.CommandText = $"select {SUMCOLUMNS} from {TABLE} where date_part('month', datum) = {monat} and date_part('year', datum) = {aktjahr} group by konto_nr, datum order by konto_nr, datum"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Kosten(reader)); reader.Close(); } DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetUpdateList(string kontonr) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if(kontonr != null) command.CommandText = $"select {COLUMNS} from {TABLE} where konto_nr = {kontonr}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Kosten(reader)); reader.Close(); } DatenbankConnection.GetConnection().Close(); return resultList; } public static double GetSummeKategorie() { DatenbankConnection.GetConnection().Open(); double kosten = 0; using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) kosten = reader.IsDBNull(0) ? 0 : reader.GetDouble(0); reader.Close(); } DatenbankConnection.GetConnection().Close(); return kosten; } public static Kosten GetKosten(int? kostenid, string bez) { DatenbankConnection.GetConnection().Open(); Kosten kosten = new Kosten(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (!string.IsNullOrEmpty(bez)) command.CommandText = $"select {COLUMNS} from {TABLE} where LOWER(bezeichnung) = '{bez}'"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) kosten = new Kosten(reader); reader.Close(); } DatenbankConnection.GetConnection().Close(); return kosten; } public Kosten(NpgsqlDataReader reader) { this.KostenID = reader.GetInt32(0); this.KontoNr = reader.GetInt32(1); this.Bezeichnung = reader.IsDBNull(2) ? string.Empty : reader.GetString(2); this.Datum = reader.GetDateTime(3); this.Kategorie = (KostenKat)reader.GetInt16(4); this.Betrag = reader.IsDBNull(5) ? null : (double?)reader.GetDouble(5); } public int GetKostenID(int kontonr, DateTime datum) { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select kosten_id from {TABLE} where konto_nr = '{kontonr}' and datum = '{datum}'"; int result = 0; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = reader.GetInt16(0); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; } } public static int GetKontoNr(string bez) { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select konto_nr from {TABLE} where bezeichnung = {bez}"; int result = 0; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = reader.GetInt16(0); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; } } public int Save() { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (this.KostenID.HasValue & this.KostenID != 0) { command.CommandText = $"update {TABLE} set konto_nr = :p1, bezeichnung = :p2, datum = :p3, kategorie = :p4, betrag = :p5 WHERE kosten_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.kosten_seq')"; this.KostenID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5)"; } command.Parameters.AddWithValue("p0", this.KostenID); command.Parameters.AddWithValue("p1", this.KontoNr); command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(this.Bezeichnung) ? (object)DBNull.Value : this.Bezeichnung); command.Parameters.AddWithValue("p3", this.Datum); command.Parameters.AddWithValue("p4", (int)this.Kategorie); command.Parameters.AddWithValue("p5", this.Betrag.HasValue ? (object)this.Betrag.Value : (object)DBNull.Value); int result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } } [OLVIgnore] public int? KostenID { get; set; } = null; [OLVColumn(DisplayIndex = 0, TextAlign = System.Windows.Forms.HorizontalAlignment.Right, IsEditable = false)] public int KontoNr { get; set; } [OLVColumn(DisplayIndex = 1, IsEditable = false)] public string Bezeichnung { get; set; } [OLVColumn(DisplayIndex = 2, TextAlign = System.Windows.Forms.HorizontalAlignment.Center, AspectToStringFormat = "{0:d}", IsEditable = false)] public DateTime Datum { get; set; } [OLVColumn(DisplayIndex = 3, TextAlign = System.Windows.Forms.HorizontalAlignment.Center, IsEditable = true)] public KostenKat Kategorie { get; set; } [OLVColumn(DisplayIndex = 4, TextAlign = System.Windows.Forms.HorizontalAlignment.Right, AspectToStringFormat = "{0:c}", IsEditable = true)] public double? Betrag { get; set; } = null; } }