using Npgsql; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.UI.WebControls; using static System.Net.Mime.MediaTypeNames; namespace DatenDB { public class Umsatz { private const string COLUMNS = "umsatz_id, kunde_id, datum, betrag"; public const string SUBCOLUMNS = "kunde_id, date_part('year', datum) as jahr, sum(betrag) as betrag"; private const string TABLE = "kundenverwaltung.umsatz"; public Umsatz() { } public int GetUmsatzID(int? kundeid, DateTime datum) { DatenbankConnection.GetConnection().Open(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select umsatz_id from {TABLE} where kunde_id = '{kundeid}' and datum = '{datum}'"; int result = 0; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = reader.GetInt32(0); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; }//IMPORT DATUM KEINE VERWENDUNG ODER public static double GetUmsatz(int? id, int? year, int? month, int[] quartale) { DatenbankConnection.GetConnection().Open(); double umsatz = 0; NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (quartale == null || quartale.Length == 0) { if (id == null) { if (month <= 12) command.CommandText = $"select sum(betrag) from {TABLE} where date_part('year', datum) = {year} and date_part('month', datum) = {month}"; if (month == null || month == 0) command.CommandText = $"select sum(betrag) from {TABLE} where date_part('year', datum) = {year}"; } else { command.CommandText = $"select sum(betrag) from {TABLE} where kunde_id = {id} and date_part('year', datum) = {year}"; } } else { if (id == null) { if (quartale.Length == 1) command.CommandText = $"select sum(betrag) from {TABLE} where date_part('year', datum) = {year} and date_part('quarter', datum) = {quartale[0]}"; if (quartale.Length == 2) command.CommandText = $"select sum(betrag) from {TABLE} where date_part('year', datum) = {year} and date_part('quarter', datum) between '{quartale[1]}' and '{quartale[0]}'"; if (quartale.Length == 3) command.CommandText = $"select sum(betrag) from {TABLE} where date_part('year', datum) = {year} and date_part('quarter', datum) between '{quartale[2]}' and '{quartale[0]}'"; if (quartale.Length == 4) command.CommandText = $"select sum(betrag) from {TABLE} where date_part('year', datum) = {year} and date_part('quarter', datum) between '{quartale[3]}' and '{quartale[0]}'"; } else { command.CommandText = $"select sum(betrag) from {TABLE} where "; //if (quartale.Length == 1) command.CommandText = $"select sum(betrag) from {TABLE} where kunde_id = {id} date_part('year', datum) = {year} and date_part('quarter', datum) = {quartale[0]}"; //if (quartale.Length == 2) command.CommandText = $"select sum(betrag) from {TABLE} where kunde_id = {id} date_part('year', datum) = {year} and date_part('quarter', datum) between '{quartale[1]}' and '{quartale[0]}'"; //if (quartale.Length == 3) command.CommandText = $"select sum(betrag) from {TABLE} where kunde_id = {id} and date_part('year', datum) = {year} and date_part('quarter', datum) between '{quartale[2]}' and '{quartale[0]}'"; //if (quartale.Length == 4) command.CommandText = $"select sum(betrag) from {TABLE} where kunde_id = {id} and date_part('year', datum) = {year} and date_part('quarter', datum) between '{quartale[3]}' and '{quartale[0]}'"; } } NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) umsatz = reader.IsDBNull(0) ? 0 : reader.GetDouble(0); reader.Close(); DatenbankConnection.GetConnection().Close(); return umsatz; } public Umsatz(NpgsqlDataReader reader, bool columns) { if (columns) { this.UmsatzID = reader.GetInt32(0); this.KundeID = reader.GetInt32(1); this.Datum = reader.GetDateTime(2); this.Betrag = reader.IsDBNull(3) ? 0 : reader.GetDouble(3); } else { this.KundeID = reader.GetInt32(0); this.Year = (int)reader.GetDouble(1); this.Betrag = reader.IsDBNull(2) ? 0 : reader.GetDouble(2); } } public int Save() { DatenbankConnection.GetConnection().Open(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (this.UmsatzID.HasValue & this.UmsatzID != 0) { command.CommandText = $"update {TABLE} set kunde_id = :p1, datum = :p2, betrag = :p3 WHERE umsatz_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.umsatz_seq')"; this.UmsatzID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3)"; } command.Parameters.AddWithValue("p0", this.UmsatzID); command.Parameters.AddWithValue("p1", this.KundeID); command.Parameters.AddWithValue("p2", this.Datum); command.Parameters.AddWithValue("p3", this.Betrag); int result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } public static List GetJahre() { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select distinct date_part('year', datum) from {TABLE} where date_part('year', datum) is not null order by date_part('year', datum);"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(reader.GetDouble(0).ToString()); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } //public static double GetQUmsatz(int? kndid, int? aktjahr, int? monat, int? aktquartal, bool v, int vpjahr) //{ // DatenbankConnection.GetConnection().Open(); // double umsatz = 0; // NpgsqlCommand command = new NpgsqlCommand(); // command.Connection = DatenbankConnection.GetConnection(); // if (aktquartal == null || aktquartal == 0) // { // //command.CommandText = $"select sum(umsatz) as umsatz from kundenverwaltung.q_umsatz where kunde_id = {kndid} and jahr = {vpjahr}"; // } // else // { // if (aktquartal == 4) command.CommandText = $"select umsatz from kundenverwaltung.jahresumsaetze where kunde_id = {kndid} and jahr = {vpjahr}"; // else command.CommandText = $"select umsatz from kundenverwaltung.q_umsatz where kunde_id = {kndid} and quartal = {aktquartal} and jahr = {vpjahr}"; // } // NpgsqlDataReader reader = command.ExecuteReader(); // while (reader.Read()) umsatz = reader.IsDBNull(0) ? 0 : reader.GetDouble(0); // reader.Close(); // DatenbankConnection.GetConnection().Close(); // return umsatz; // } public static List GetList(int? aktjahr, int? monat, int? aktquartal, bool v, int vpjahr, int[] quartale) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); 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 {SUBCOLUMNS} from {TABLE} where date_part('year', datum) = {vpjahr} group by umsatz.kunde_id, date_part('year', datum)"; } else { command.CommandText = $"select {SUBCOLUMNS} from {TABLE} where date_part('year', datum) = {vpjahr} and date_part('quarter', datum) between {quartale.Min()} and {quartale.Max()} group by umsatz.kunde_id, jahr"; } } else { if (aktquartal == 4) command.CommandText = $"select {SUBCOLUMNS} from {TABLE} where date_part('year', datum) = {vpjahr} group by umsatz.kunde_id, date_part('year', datum)"; else command.CommandText = $"select {SUBCOLUMNS} from {TABLE} where date_part('quarter', datum) = {aktquartal} and date_part('year', datum) = {vpjahr} group by umsatz.kunde_id, date_part('year', datum), date_part('quarter', datum)"; } } else command.CommandText = $"select {SUBCOLUMNS} from {TABLE} where date_part('month', datum) = {monat} and date_part('year', datum) = {vpjahr} group by umsatz.kunde_id, date_part('year', datum), date_part('month', datum)"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Umsatz(reader, false)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public int? UmsatzID { get; set; } public int? KundeID { get; set; } public DateTime Datum { get; set; } public double Betrag { get; set; } public int Year { get; set; } } }