ZKuP/ZKuP/AddCardUser.xaml.cs
Marcus 8544d5c30a Bug behoben dass Parkkartenantrag nicht für unregistrierte funktioniert hat
FailSafes für gridRowArrivals,damit es nicht außerhalb sein kann
illegalAccess Log eingebaut
Bug des Splashscreens behoben, sodass die Animation jetzt immer mittig ist
parkausweisantrag und mcfit antrag: Fenster zentriert
kartenausgabe: wenn keine telnr bei der 2.+ Person angegeben wird, wird die von der 1. Person hinterlegt
Styling für unregistrierte modernisiert
2025-09-25 14:04:37 +02:00

260 lines
9.9 KiB
C#

using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace ZKuP
{
/// <summary>
/// Interaktionslogik für AddCardUser.xaml
/// </summary>
public partial class AddCardUser : MetroWindow
{
string _user = "";
//string _ansprechpartnerFirma = "";
bool _isBesucher = false;
List<string> comboList = new List<string>() { "Grün", "Rot", "Gerätewagen" };
List<string> comboListNurRot = new List<string>() { "Rot" };
List<string> mitarbeiterList = new List<string>();
List<string> telList = new List<string>();
byte[] _signature = null;
string _ap = "";
bool _needTel = true;
string _neededTel = "";
DispatcherTimer blinkTimer = new DispatcherTimer();
public AddCardUser(string user = "", bool isBesucher = false, string count = "", string ap = "", bool needTel = true, bool hasEinweisung = false, string firma = "", string telnr = "", string idFirma = "")
{
_user = user;
_ap = ap;
_needTel = needTel;
_isBesucher = isBesucher;
InitializeComponent();
Helper.InitSerial();
Helper.DataReceived += Helper_DataReceived;
blinkTimer.Interval = TimeSpan.FromMilliseconds(50);
blinkTimer.Tick += BlinkTimer_Tick;
blinkTimer.Start();
this.Title = "Karten ausgeben - " + count;
lblFirma.Text = isBesucher ? "Firma" : "Firma*";
lblTelnr.Text = needTel ? "Telefonnummer*" : "Telefonnummer";
cbColor.ItemsSource = isBesucher ? comboListNurRot : comboList;
cbColor.ItemsSource = hasEinweisung ? comboList : comboListNurRot;
//cbColor.ItemsSource = comboListNurRot;
if (!isBesucher)
{
tbFirma.Text = firma;
mitarbeiterList = SQL.ReadSingleValue($"SELECT Personen FROM {MainWindow.table}.firmenPeople WHERE idFirmen = '{idFirma}'").Split(';').ToList();
telList = SQL.ReadSingleValue($"SELECT TelNr FROM {MainWindow.table}.firmenPeople WHERE idFirmen = '{idFirma}'").Split(';').ToList();
if (!mitarbeiterList.Contains(user)) mitarbeiterList.Add(user);
if (!telList.Contains(telnr)) telList.Add(telnr);
tbUser.ItemsSource = mitarbeiterList;
tbTelnr.ItemsSource = telList;
if (!string.IsNullOrWhiteSpace(count))
{
var selection = Convert.ToInt16(count.Split('/')[0]) - 2;
tbUser.SelectedIndex = selection;
tbTelnr.SelectedIndex = selection;
}
}
if (!user.Contains("["))
{
if(tbUser.SelectedIndex == -1) tbUser.Text = user;
try
{
if (user != "")
tbTelnr.Text = telnr;
//tbTelnr.Text = SQL.ReadSingleValue($"SELECT Tel_Nr_Verantwortlicher_Firma FROM {MainWindow.table}.firmen WHERE Name='{user}'");
}
catch (Exception)
{
}
}
else
{
tbUser.Text = user.Split(',')[1].TrimEnd(']').Trim();
tbTelnr.Text = SQL.ReadSingleValue($"SELECT TelNr FROM {MainWindow.table}.family WHERE Name='{tbUser.Text}'");
cbColor.SelectedIndex = 0;
tbNummer.Focus();
}
}
bool up = false;
private void BlinkTimer_Tick(object sender, EventArgs e)
{
Dispatcher.Invoke(() =>
{
if (up)
{
if (borderKartenleser.Opacity < 1)
borderKartenleser.Opacity += 0.05;
else
up = !up;
}
else
{
if (borderKartenleser.Opacity > 0)
borderKartenleser.Opacity -= 0.05;
else
up = !up;
}
});
}
private void Helper_DataReceived(object sender, string e)
{
var text = "";
Dispatcher.Invoke(() =>
{
text = tbNummer.Text = e.Split('=')[0];
borderKartenleser.Visibility = Visibility.Collapsed;
blinkTimer.Stop();
if (!string.IsNullOrWhiteSpace(text))
{
var farbe = SQL.ReadSingleValue($"SELECT Farbe FROM {MainWindow.table}.kartennummern WHERE Kartennummer='{text}'");
switch (farbe)
{
case "1":
cbColor.SelectedValue = "Rot";
break;
case "2":
cbColor.SelectedValue = "Grün";
break;
}
}
});
}
private void tbNummer_TextChanged(object sender, TextChangedEventArgs e)
{
Dispatcher.Invoke(() =>
{
if (tbNummer.Text.Length <= 0)
{
borderKartenleser.Visibility = Visibility.Visible;
blinkTimer.Start();
}
else
{
borderKartenleser.Visibility = Visibility.Collapsed;
blinkTimer.Stop();
}
});
}
private async void btnAdd_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() =>
{
bool cardExists = (SQL.RowExists($"{MainWindow.table}.kartennummern", "Kartennummer", tbNummer.Text).Result);
if (string.IsNullOrWhiteSpace(tbNummer.Text))
MessageBox.Show(this, "Kartennummer angeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
else if (!cardExists)
{
MessageBox.Show("Die eingegebene Kartennummer existiert nicht!\nBitte den Kartenleser verwenden oder die Nummer auf der Rückseite des Ausweises eingeben", "Fehlerhafte Kartennummer", MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (cbColor.SelectedIndex == -1)
MessageBox.Show(this, "Kartenfarbe auswählen!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
else if (string.IsNullOrWhiteSpace(tbUser.Text))
MessageBox.Show(this, "Person angeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
else if (!_isBesucher && string.IsNullOrWhiteSpace(tbFirma.Text))
MessageBox.Show(this, "Firma angeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
else if (_needTel && string.IsNullOrWhiteSpace(tbTelnr.Text))
MessageBox.Show(this, "Telefonnummer angeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
try
{
if (!string.IsNullOrWhiteSpace(_neededTel))
{
_neededTel = tbTelnr.Text;
}
var sig = new Signature(Signature.DisclaimerType.Rot, tbUser.Text, tbTelnr.Text);
if (sig.ShowDialog() == false)
{
_signature = sig.ResultByte;
if (_signature != null)
{
var tel = !string.IsNullOrWhiteSpace(tbTelnr.Text) ? tbTelnr.Text : _neededTel;
SQL.WriteSQL($"REPLACE INTO karten (kartennr,farbe,benutzer,telnr,AusgegebenDurch,AusgegebenTimestamp,Ansprechpartner,Signature) VALUES ('{tbNummer.Text}','{cbColor.SelectedValue.ToString()}','{tbUser.Text}','{tbTelnr.Text}','{Environment.UserName}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','{_ap}',@signature)", _signature);
MessageBox.Show(this, "Karte erfolgreich verknüpft", "Erfolg", MessageBoxButton.OK, MessageBoxImage.Information);
ResetValues();
this.Close();
}
else MessageBox.Show("Unterschrift nicht gefunden\nVorgang bitte wiederholen");
}
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
if (MessageBox.Show(this, "Beim verknüpfen der Karte ist ein Fehler aufgetreten\n\nMöchten Sie die interne Fehlermeldung anzeigen?", "Fehler", MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.No) == MessageBoxResult.Yes)
MessageBox.Show(this, $"Fehlermeldung:\n\n{ex.Message}", "Fehlermeldung", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}));
}
private void ResetValues()
{
Dispatcher.Invoke(() =>
{
tbNummer.Text = "";
tbTelnr.Text = "";
});
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Helper.CheckWindowIsInScreenSpace(this);
}
private void Window_LocationChanged(object sender, EventArgs e)
{
Helper.CheckWindowIsInScreenSpace(this);
}
}
}