git_bsmd/ENI2/EditControls/EditWasteReceiptDialog.xaml.cs

154 lines
6.9 KiB
C#

// Copyright (c) 2017- schick Informatik
// Description: Waste receipts bearbeiten..
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using bsmd.database;
using ENI2.Controls;
namespace ENI2.EditControls
{
/// <summary>
/// Interaction logic for EditWasteReceiptDialog.xaml
/// </summary>
public partial class EditWasteReceiptDialog : EditWindowBase
{
// TODO: "unstatic" the templates and take care they are synchronized between controls
private List<WasteDisposalServiceProvider_Template> _wsdpTemplates = null;
private WasteDisposalServiceProvider_Template _currentTemplate;
private string _undoTemplate;
public EditWasteReceiptDialog()
{
InitializeComponent();
Loaded += EditWasteReceiptDialog_Loaded;
AddClicked += () => { /* this.comboBoxWasteCode.Focus(); */ };
}
public WAS_RCPT WAS_RCPT { get; set; }
private async void EditWasteReceiptDialog_Loaded(object sender, RoutedEventArgs e)
{
this.textIdentificationNumber.Text = this.WAS_RCPT.IdentificationNumber;
this.textBoxPortReceptionFacilityName.Text = this.WAS_RCPT.PortReceptionFacilityName;
this.textBoxPortReceptionFacilityProviderName.Text = this.WAS_RCPT.PortReceptionFacilityProviderName;
this.textBoxTreatmentFacilityProvider.Text = this.WAS_RCPT.TreatmentFacilityProviderText;
this.dateTimePickerWasteDeliveryDateFrom.Value = this.WAS_RCPT.WasteDeliveryDateFrom?.ToLocalTime();
this.dateTimePickerWasteDeliveryDateTo.Value = this.WAS_RCPT.WasteDeliveryDateTo?.ToLocalTime();
OKClicked += EditWasteReceiptDialog_OKClicked;
this.AddVisible = true;
_wsdpTemplates = await DBManagerAsync.GetWastDisposalServiceProviderTemplatesAsync();
_wsdpTemplates.Sort();
Trace.WriteLine($"{_wsdpTemplates.Count} WSDP templates loaded");
this.comboBox_WSDPTemplate.ItemsSource = _wsdpTemplates;
}
public void CopyValuesToEntity()
{
// copy back
this.WAS_RCPT.IdentificationNumber = this.textIdentificationNumber.Text?.Trim();
this.WAS_RCPT.PortReceptionFacilityName = this.textBoxPortReceptionFacilityName.Text?.Trim();
this.WAS_RCPT.PortReceptionFacilityProviderName = this.textBoxPortReceptionFacilityProviderName.Text?.Trim();
this.WAS_RCPT.TreatmentFacilityProviderText = this.textBoxTreatmentFacilityProvider.Text?.Trim();
this.WAS_RCPT.WasteDeliveryDateFrom = this.dateTimePickerWasteDeliveryDateFrom.Value?.ToUniversalTime();
this.WAS_RCPT.WasteDeliveryDateTo = this.dateTimePickerWasteDeliveryDateTo.Value?.ToUniversalTime();
}
private void EditWasteReceiptDialog_OKClicked()
{
this.CopyValuesToEntity();
}
private void comboBox_WSDPTemplate_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
Trace.WriteLine("WSDP combo selection changed");
if (this.comboBox_WSDPTemplate.SelectedItem is WasteDisposalServiceProvider_Template wdsp_t)
{
this.textBoxTemplateTitle.Text = wdsp_t.Remark;
this.buttonDeleteTemplate.IsEnabled = true;
this._currentTemplate = wdsp_t;
this._undoTemplate = this.textBoxPortReceptionFacilityProviderName.Text.Trim();
this.buttonUndoTemplate.IsEnabled = this._undoTemplate.Length > 0;
this.textBoxPortReceptionFacilityProviderName.Text = wdsp_t.WasteDisposalServiceProviderName;
}
}
private async void buttonSaveTemplate_Click(object sender, RoutedEventArgs e)
{
string currentWSDPProviderName = this.textBoxPortReceptionFacilityProviderName.Text.Trim();
string currentRemark = this.textBoxTemplateTitle.Text.Trim();
if ((currentWSDPProviderName.Length == 0) || (currentRemark.Length == 0)) return;
WasteDisposalServiceProvider_Template existingTemplate = null;
foreach (WasteDisposalServiceProvider_Template wdsp_template in _wsdpTemplates)
{
// bei gefundenem Match wird ggf. der Remark überschrieben
if (wdsp_template.Remark.Equals(currentRemark))
{
existingTemplate = wdsp_template;
break;
}
}
if (existingTemplate != null)
{
if (MessageBox.Show("A template with this name already exists, overwrite?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
return;
existingTemplate.WasteDisposalServiceProviderName = currentWSDPProviderName;
await DBManagerAsync.SaveAsync(existingTemplate);
return;
}
WasteDisposalServiceProvider_Template newTemplate = new WasteDisposalServiceProvider_Template();
newTemplate.WasteDisposalServiceProviderName = currentWSDPProviderName;
newTemplate.Remark = currentRemark;
await DBManagerAsync.SaveAsync(newTemplate);
comboBox_WSDPTemplate.ItemsSource = null;
_wsdpTemplates.Add(newTemplate);
_wsdpTemplates.Sort();
comboBox_WSDPTemplate.ItemsSource = _wsdpTemplates;
MessageBox.Show("Template saved", "OK", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void buttonDeleteTemplate_Click(object sender, RoutedEventArgs e)
{
if (_currentTemplate != null)
{
if (MessageBox.Show("Delete this template?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
this.comboBox_WSDPTemplate.SelectedItem = null;
this.comboBox_WSDPTemplate.ItemsSource = null;
DBManager.Instance.Delete(_currentTemplate);
_wsdpTemplates.Remove(_currentTemplate);
this.textBoxTemplateTitle.Text = null;
this.buttonDeleteTemplate.IsEnabled = false;
this.comboBox_WSDPTemplate.ItemsSource = _wsdpTemplates;
}
}
}
private void buttonUndoTemplate_Click(object sender, RoutedEventArgs e)
{
if (this._undoTemplate != null)
{
this.textBoxPortReceptionFacilityProviderName.Text = this._undoTemplate;
this.buttonUndoTemplate.IsEnabled = false;
this._undoTemplate = null;
this.comboBox_WSDPTemplate.SelectedItem = null;
}
}
}
}