239 lines
11 KiB
C#
239 lines
11 KiB
C#
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.UI.WebControls;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
|
namespace DatenDB
|
|
{
|
|
public class KundeArtikel
|
|
{
|
|
// 0 1 2 3 4 5 6 7 8 9 10 11 12
|
|
private static string COLUMNS = "kunde_artikel_id, kunde_id, artikel_nr, artikel_name, stand, fehlmenge, stand_bearbeitet, fehlmenge_bearbeitet, korrektur, korrektur_bearbeitet, reklamation, artikel_id, reihung";
|
|
private static string TABLE = "kundenverwaltung.kunde_artikel";
|
|
|
|
public KundeArtikel() { }
|
|
|
|
public static List<KundeArtikel> GetList(string kundeid)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
List<KundeArtikel> resultList = new List<KundeArtikel>();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select {COLUMNS} from {TABLE} where kunde_id = {kundeid} order by reihung asc";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) resultList.Add(new KundeArtikel(reader));
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return resultList;
|
|
}
|
|
public static KundeArtikel GetItem(int i)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
KundeArtikel result = new KundeArtikel();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"SELECT {COLUMNS} from {TABLE} where kunde_artikel_id = {i}";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) result = new KundeArtikel(reader);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
|
|
return result;
|
|
}
|
|
public static KundeArtikel GetItemIfAvailable(int kndid, int artnr)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
KundeArtikel result = null;
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"SELECT {COLUMNS} from {TABLE} where kunde_id = {kndid} and artikel_nr = {artnr}";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) result = new KundeArtikel(reader);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
|
|
return result;
|
|
}
|
|
public static KundeArtikel GetKundeArtikelVonArtikelID(int? kndid, int? artikelid)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
KundeArtikel result = null;
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"SELECT {COLUMNS} from {TABLE} where kunde_id = {kndid} and artikel_id = {artikelid}";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) result = new KundeArtikel(reader);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
|
|
return result;
|
|
}
|
|
public static List<KundeArtikel> GetFehlmengeList(string kundeid)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
List<KundeArtikel> resultList = new List<KundeArtikel>();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select {COLUMNS} from {TABLE} where kunde_id = {kundeid} and SUBSTRING(artikel_nr::text, 1, 1) = '6' order by reihung asc";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) resultList.Add(new KundeArtikel(reader));
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return resultList;
|
|
}
|
|
internal static bool HatKategorieArtikel(int kundeid, ArtikelKategorie frottee)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM {TABLE} a inner join kundenverwaltung.artikel b on a.artikel_id = b.artikel_id WHERE a.kunde_id = {kundeid} and b.kategorie = {(int)frottee}";
|
|
|
|
int result = (int)command.ExecuteScalar();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result > 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Save()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
if (this.KundeArtikelID.HasValue & this.KundeArtikelID != 0)
|
|
{
|
|
command.CommandText = $"update {TABLE} set kunde_id = :p1, artikel_nr = :p2, artikel_name = :p3, stand = :p4, fehlmenge = :p5, stand_bearbeitet = :p6, fehlmenge_bearbeitet = :p7, korrektur = :p8, korrektur_bearbeitet = :p9, reklamation = :p10, artikel_id = :p11, reihung = :p12 WHERE kunde_artikel_id = :p0";
|
|
}
|
|
else
|
|
{
|
|
command.CommandText = "select nextval('kundenverwaltung.kunde_artikel_seq')";
|
|
this.KundeArtikelID = (int)(long)command.ExecuteScalar();
|
|
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12)";
|
|
}
|
|
|
|
command.Parameters.AddWithValue("p0", this.KundeArtikelID.Value);
|
|
command.Parameters.AddWithValue("p1", this.KundeID);
|
|
command.Parameters.AddWithValue("p2", this.ArtikelNR);
|
|
command.Parameters.AddWithValue("p3", string.IsNullOrWhiteSpace(this.ArtikelName) ? (object)DBNull.Value : this.ArtikelName);
|
|
command.Parameters.AddWithValue("p4", this.Stand);
|
|
command.Parameters.AddWithValue("p5", this.Fehlmenge);
|
|
command.Parameters.AddWithValue("p6", string.IsNullOrWhiteSpace(this.StandBearbeitet) ? (object)DBNull.Value : this.StandBearbeitet);
|
|
command.Parameters.AddWithValue("p7", string.IsNullOrWhiteSpace(this.FehlmengeBearbeitet) ? (object)DBNull.Value : this.FehlmengeBearbeitet);
|
|
command.Parameters.AddWithValue("p8", this.Korrektur);
|
|
command.Parameters.AddWithValue("p9", string.IsNullOrWhiteSpace(this.KorrekturBearbeitet) ? (object)DBNull.Value : this.KorrekturBearbeitet);
|
|
command.Parameters.AddWithValue("p10", this.Reklamation);
|
|
command.Parameters.AddWithValue("p11", this.ArtikelID.HasValue ?this.ArtikelID.Value : (object)DBNull.Value);
|
|
command.Parameters.AddWithValue("p12", this.Reihung);
|
|
|
|
int result = command.ExecuteNonQuery();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result;
|
|
|
|
}
|
|
public int ZahlenUpdate()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
if (this.KundeArtikelID.HasValue & this.KundeArtikelID != 0)
|
|
{
|
|
command.CommandText = $"update {TABLE} set stand = :p1, stand_bearbeitet = :p2 fehlmenge = :p3, fehlmenge_bearbeitet = :p4, korrektur = :p5, korrektur_bearbeitet = :p6 WHERE kunde_artikel_id = :p0";
|
|
}
|
|
else
|
|
{
|
|
command.CommandText = "select nextval('kundenverwaltung.kunde_artikel_seq')";
|
|
this.KundeArtikelID = (int)(long)command.ExecuteScalar();
|
|
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6)";
|
|
}
|
|
|
|
command.Parameters.AddWithValue("p0", this.KundeArtikelID.Value);
|
|
command.Parameters.AddWithValue("p1", this.Stand);
|
|
command.Parameters.AddWithValue("p2", string.IsNullOrWhiteSpace(this.StandBearbeitet) ? (object)DBNull.Value : this.StandBearbeitet);
|
|
command.Parameters.AddWithValue("p3", this.Fehlmenge);
|
|
command.Parameters.AddWithValue("p4", string.IsNullOrWhiteSpace(this.FehlmengeBearbeitet) ? (object)DBNull.Value : this.FehlmengeBearbeitet);
|
|
command.Parameters.AddWithValue("p5", this.Korrektur);
|
|
command.Parameters.AddWithValue("p6", string.IsNullOrWhiteSpace(this.KorrekturBearbeitet) ? (object)DBNull.Value : this.KorrekturBearbeitet);
|
|
|
|
int result = command.ExecuteNonQuery();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result;
|
|
|
|
|
|
}
|
|
internal static async Task<List<KundeArtikel>> LoadArtikelDatenAsync(Kunde kunde)
|
|
{
|
|
var artikelDaten = new List<KundeArtikel>();
|
|
|
|
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
|
|
|
|
await Task.Run(() =>
|
|
{
|
|
using (var conn = new NpgsqlConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
using (var cmd = new NpgsqlCommand($"SELECT a.artikel_name, a.stand, a.fehlmenge, a.kunde_id, a.reklamation, a.artikel_id, a.reihung, b.kategorie FROM {TABLE} a inner join kundenverwaltung.artikel b on a.artikel_id = b.artikel_id WHERE a.kunde_id = {kunde.KundeID} and b.kategorie > 0 and (a.fehlmenge + a.reklamation) > 0", conn))
|
|
{
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
artikelDaten.Add(new KundeArtikel
|
|
{
|
|
ArtikelName = reader.GetString(0),
|
|
Stand = reader.GetInt32(1),
|
|
Fehlmenge = reader.GetInt32(2),
|
|
KundeID = reader.GetInt32(3),
|
|
Reklamation = reader.IsDBNull(4) ? 0 : reader.GetInt32(4),
|
|
ArtikelID = reader.IsDBNull(5) ? (int?)null : reader.GetInt32(5),
|
|
Reihung = reader.IsDBNull(6) ? 0 : reader.GetInt32(6)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return artikelDaten;
|
|
}
|
|
|
|
|
|
public KundeArtikel(NpgsqlDataReader reader)
|
|
{
|
|
this.KundeArtikelID = reader.GetInt32(0);
|
|
this.KundeID = reader.GetInt32(1);
|
|
this.ArtikelNR = reader.GetInt32(2);
|
|
this.ArtikelName = reader.IsDBNull(3) ? string.Empty : reader.GetString(3);
|
|
this.Stand = reader.GetInt32(4);
|
|
this.Fehlmenge = reader.GetInt32(5);
|
|
this.StandBearbeitet = reader.IsDBNull(6) ? string.Empty : reader.GetString(6);
|
|
this.FehlmengeBearbeitet = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);
|
|
this.Korrektur = reader.IsDBNull(8) ? 0 : reader.GetInt32(8);
|
|
this.KorrekturBearbeitet = reader.IsDBNull(9) ? string.Empty : reader.GetString(9);
|
|
this.Reklamation = reader.IsDBNull(10) ? 0 : reader.GetInt32(10);
|
|
this.ArtikelID = reader.IsDBNull(11) ? (int?)null : reader.GetInt32(11);
|
|
this.Reihung = reader.IsDBNull(12) ? 0 : reader.GetInt32(12);
|
|
}
|
|
|
|
public int? KundeArtikelID { get; set; }
|
|
public int? KundeID { get; set; }
|
|
public int ArtikelNR { get; set; }
|
|
public string ArtikelName { get; set; }
|
|
public int Stand { get; set; }
|
|
public int Fehlmenge { get; set; }
|
|
public string StandBearbeitet { get; set; }
|
|
public string FehlmengeBearbeitet { get; set; }
|
|
public int Korrektur { get; set; }
|
|
public string KorrekturBearbeitet { get; set; }
|
|
public int Reklamation { get; set; }
|
|
public int? ArtikelID { get; set; }
|
|
public int Reihung { get; set; }
|
|
|
|
}
|
|
}
|