using BrightIdeasSoftware; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DatenDB { public enum AuftragStatus { Abgeholt = 0, Aufgelegt = 1, Finisching = 2, Herrichten = 3, Vorbereitet = 4, Fertig = 7, Ausgeliefert = 8, AufAbruf = 9 } public enum AuftragTyp { Unbekannt = 0, Standart = 1, Sonder = 2 } public class Auftrag { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public const string COLUMNS = "auftrag_id, benutzer_id, aufgabe_id, kunde_id, zusatz, liefertag, gedruckt, gedruckt_von, erledigt, erledigt_von, erstellt, erstellt_von, status, tour_id, container_dirt, maschine_id, container_clean, typ"; private const string TABLE = "kundenverwaltung.auftrag"; public const string ASPECTS = "FahrerID,AufgabeID,KundeID,ZusatzInfo,Wann,Gedruckt,Erledigt,Erstellt von,Erstellt Am"; public Auftrag() { } public static List GetAuftragList(string s, Auftrag auftrag) { int? aID = null; int? mID = null; //Get MaschineID und AuftragID für Aufträge vor und nach Auftrag if (auftrag != null) { mID = auftrag.MaschineID; aID = auftrag.AuftragID; } //Get AufgabeID für Anzeige von Standerhöhungen if (s == "STH") aID = (int)Aufgabe.GetAufgabe(s, null).AufgabeID; DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (s == "Expedit") command.CommandText = $"select {COLUMNS} from {TABLE} where status = 3 order by liefertag asc"; if (s == string.Empty) command.CommandText = $"select {COLUMNS} from {TABLE} where gedruckt is null and erledigt is null"; if (s == "davor") command.CommandText = $"select {COLUMNS} from {TABLE} where maschine_id = {mID} and auftrag_id < {aID} order by liefertag asc limit 3"; if (s == "danach") command.CommandText = $"select {COLUMNS} from {TABLE} where maschine_id = {mID} and auftrag_id > {aID} order by liefertag asc limit 3"; if (s == "STH") command.CommandText = $"select {COLUMNS} from {TABLE} where status = 3 and aufgabe_id = {aID} order by liefertag"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Auftrag(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetAuftragListToday(DateTime produktion) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where typ = 1 and erstellt::date = '{produktion.Date}' or typ = 1 and status < 7 order by liefertag asc"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Auftrag(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetAuftragStatsListToday(DateTime produktion) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where typ = 1 and erstellt::date = '{produktion.Date}' order by liefertag asc"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Auftrag(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetExpeditLists(int? typ) { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (typ == null) command.CommandText = $"select {COLUMNS} from {TABLE} where status <= 7 order by status desc, liefertag asc"; else if (typ <= 2) command.CommandText = $"select {COLUMNS} from {TABLE} where typ = {typ} and status < 7 order by status desc, liefertag asc"; else command.CommandText = $"select {COLUMNS} from {TABLE} where status = {typ} order by status desc, liefertag asc"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Auftrag(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static Auftrag GetAuftrag(int? auftragID) { DatenbankConnection.GetConnection().Open(); Auftrag result = new Auftrag(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where auftrag_id = {auftragID}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = new Auftrag(reader); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; } public static Auftrag GetLastAuftrag(int? benutzerID) { DatenbankConnection.GetConnection().Open(); Auftrag result = new Auftrag(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where erstellt_von = {benutzerID} order by erstellt desc limit 1"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = new Auftrag(reader); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; } public bool FindeAuftrag(Auftrag auftrag) { int result = 0; DatenbankConnection.GetConnection().Open(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select auftrag_id from {TABLE} where kunde_id = {auftrag.KundeID} and erstellt::date = '{auftrag.Erstellt.Value.Date}' and liefertag = '{auftrag.Liefertag}'"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = reader.GetInt16(0); reader.Close(); DatenbankConnection.GetConnection().Close(); if(result > 0) return true; else return false; } public Auftrag(NpgsqlDataReader reader) { this.AuftragID = reader.GetInt32(0); this.ArbeiterID = reader.IsDBNull(1) ? null : (int?)reader.GetInt32(1); this.AufgabeID = reader.IsDBNull(2) ? null : (int?)reader.GetInt32(2); this.KundeID = reader.GetInt32(3); this.ZusatzInfo = reader.IsDBNull(4) ? string.Empty : reader.GetString(4); this.Liefertag = reader.GetDateTime(5); this.Gedruckt = reader.IsDBNull(6) ? null : (DateTime?)reader.GetDateTime(6); this.GedrucktVon = reader.IsDBNull(7) ? null : (int?)reader.GetInt32(7); this.Erledigt = reader.IsDBNull(8) ? null : (DateTime?)reader.GetDateTime(8); this.ErledigtVon = reader.IsDBNull(9) ? null : (int?)reader.GetInt32(9); this.Erstellt = reader.IsDBNull(10) ? null : (DateTime?)reader.GetDateTime(10); this.ErstelltVon = reader.GetInt32(11); this.Status = (AuftragStatus)reader.GetInt32(12); this.TourID = reader.IsDBNull(13) ? null : (int?)reader.GetInt32(13); this.Container = reader.IsDBNull(14) ? 0 : reader.GetInt32(14); this.MaschineID = reader.IsDBNull(15) ? null : (int?)reader.GetInt32(15); this.ContainerClean = reader.IsDBNull(16) ? 0 : reader.GetInt32(16); this.Typ =reader.IsDBNull(17) ? AuftragTyp.Unbekannt : (AuftragTyp)reader.GetInt32(17); } /// /// CheckBoxen zur Auftragsauswahl als erste Spalte. /// [OLVColumn("Auswählen", CheckBoxes = true, DisplayIndex = 0)] public bool IsChecked { get; set; } /// /// Liefertag mit ausgeschriebenen Tag als zweite Spalte. /// [OLVColumn("Liefertag", DisplayIndex = 2, AspectToStringFormat = "{0:ddd dd.MM.}")] public DateTime Liefertag { get; set; } /// /// Die Aufgabe (STV, STH, INV) wird angezeigt wenn vorhanden. /// private string _auftragaufgabe; [OLVColumn("Aufgabe", DisplayIndex = 6, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public string AuftragAufgabe { get { if (this.Typ == AuftragTyp.Sonder) return _auftragaufgabe = Aufgabe.GetAufgabe(null, AufgabeID).Bezeichnung; else return null; } } /// /// Der Status wird angezeigt. /// [OLVColumn("Status", DisplayIndex = 5)] public AuftragStatus Status { get; set; } /// /// Der Kundename wird angezeigt /// private string _kundename; [OLVColumn("Kunde", DisplayIndex = 1)] public string Kundename { get { if (string.IsNullOrWhiteSpace(Kunde.GetKunde(null, KundeID, null).Suchtext)) return _kundename = Kunde.GetKunde(null, KundeID, null).KundeName; else return _kundename = Kunde.GetKunde(null, KundeID, null).Suchtext; } set { } } /// /// Containeranzahl schmutzig /// [OLVColumn("schmutzig", DisplayIndex = 3, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public int Container { get; set; } /// /// Containeranzahl sauber /// [OLVColumn("sauber", DisplayIndex = 4, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public int ContainerClean { get; set; } /// /// Die "Erstellt von" Spalte mit Datum wird erstellt /// private string _auftragerstellt; [OLVColumn("Erstellt", DisplayIndex = 7)] public string AuftragErstellt { get { if (this.Erstellt != null) return _auftragerstellt = Benutzer.GetBenutzer(null, this.ErstelltVon).BenutzerName + " von " + this.Erstellt.ToString(); else return null; } } /// /// Die "Gedruckt von" Spalte mit Datum wird erstellt /// private string _auftraggedruckt; [OLVColumn("Gedruckt", DisplayIndex = 8)] public string AuftragGedruckt { get { if(this.Gedruckt != null) return _auftraggedruckt = Benutzer.GetBenutzer(null, this.GedrucktVon).BenutzerName + " von " + this.Gedruckt.ToString(); else return null; } } /// /// Die "Erledigt von" Spalte mit Datum wird erstellt /// private string _auftragerledigt; [OLVColumn("Erledigt", DisplayIndex = 9)] public string AuftragErledigt { get { if (this.Erledigt != null) return _auftragerledigt = Benutzer.GetBenutzer(null, this.ErledigtVon).BenutzerName + " von " + this.Erledigt.ToString(); else return null; } } private string _fertig; [OLVColumn("Abschließen", DisplayIndex = 10)] public string Fertig { get { if(Typ == AuftragTyp.Sonder) { if (Status >= AuftragStatus.Fertig) return _fertig = string.Empty; else return _fertig = "Auftrag abschließen"; } else { if (Status == AuftragStatus.Vorbereitet) return _fertig = "Auftrag abschließen"; else { return _fertig = string.Empty; ; } } } } /// /// Zusatz und Typ Properties werden ausgeblendet. /// [OLVColumn("Zusatz", IsVisible = false)] public string ZusatzInfo { get; set; } [OLVColumn("Typ", IsVisible = false)] public AuftragTyp Typ { get; set; } /// /// Properties von ObjectListView ignoriert. /// [OLVIgnore] public int? AuftragID { get; set; } [OLVIgnore] public int? GedrucktVon { get; set; } [OLVIgnore] public DateTime? Gedruckt { get; set; } [OLVIgnore] public DateTime? Erledigt { get; set; } [OLVIgnore] public int? ErledigtVon { get; set; } [OLVIgnore] public DateTime? Erstellt { get; set; } [OLVIgnore] public int ErstelltVon { get; set; } [OLVIgnore] public int? MaschineID { get; set; } [OLVIgnore] public int KundeID { get; set; } [OLVIgnore] public int? TourID { get; set; } /// /// BenutzerID des ausführenden Users /// [OLVIgnore] public int? ArbeiterID { get; set; } [OLVIgnore] public int? AufgabeID { get; set; } public int[] Save() { DatenbankConnection.GetConnection().Open(); int[] result = new int[2]; NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (this.AuftragID.HasValue & this.AuftragID != 0) { command.CommandText = $"update {TABLE} set benutzer_id = :p1, aufgabe_id = :p2, kunde_id = :p3, zusatz = :p4, liefertag = :p5, gedruckt = :p6, gedruckt_von = :p7, erledigt = :p8, erledigt_von = :p9, erstellt = :p10, erstellt_von = :p11, status = :p12, tour_id = :p13, container_dirt = :p14, maschine_id = :p15, container_clean = :p16, typ = :p17 WHERE auftrag_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.auftrag_seq')"; this.AuftragID = result[1] = (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)"; } command.Parameters.AddWithValue("p0", this.AuftragID); command.Parameters.AddWithValue("p1", this.ArbeiterID.HasValue ? this.ArbeiterID.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p2", this.AufgabeID.HasValue ? this.AufgabeID.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p3", this.KundeID); command.Parameters.AddWithValue("p4", string.IsNullOrEmpty(this.ZusatzInfo) ? (object)DBNull.Value : this.ZusatzInfo); command.Parameters.AddWithValue("p5", this.Liefertag); command.Parameters.AddWithValue("p6", this.Gedruckt.HasValue ? this.Gedruckt.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p7", this.GedrucktVon.HasValue ? this.GedrucktVon.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p8", this.Erledigt.HasValue ? this.Erledigt.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p9", this.ErledigtVon.HasValue ? this.ErledigtVon.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p10", this.Erstellt); command.Parameters.AddWithValue("p11", this.ErstelltVon); command.Parameters.AddWithValue("p12", (int)this.Status); command.Parameters.AddWithValue("p13", this.TourID.HasValue ? this.TourID.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p14", this.Container); command.Parameters.AddWithValue("p15", this.MaschineID.HasValue ? this.MaschineID.Value : 0); command.Parameters.AddWithValue("p16", this.ContainerClean); command.Parameters.AddWithValue("p17", (int)this.Typ); result[0] = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } } }