// Copyright (c) 2017 schick Informatik // Description: Detailansicht Müllmeldung // using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using ENI2.EditControls; using ENI2.Util; using bsmd.database; using ExcelDataReader; using System.IO; using System; using Microsoft.Win32; namespace ENI2.DetailViewControls { /// /// Interaction logic for WasteDetailControl.xaml /// public partial class WasteDetailControl : DetailBaseControl { private Message _wasMessage; private WAS _was; private static readonly string[] _wasteDeliveryList = { "ALL", "SOME", "NONE" }; public WasteDetailControl() { InitializeComponent(); Loaded += WasteDetailControl_Loaded; } private void WasteDetailControl_Loaded(object sender, RoutedEventArgs e) { this.RegisterLocodeChange(this.locodeCtrlLastWastePort, Message.NotificationClass.WAS); this.RegisterCheckboxChange(this.checkBoxAccurateCorrectDetails, Message.NotificationClass.WAS); this.RegisterCheckboxChange(this.checkBoxValidExemption, Message.NotificationClass.WAS); this.RegisterDatePickerChange(this.datePickerDateLastDisposal, Message.NotificationClass.WAS); this.RegisterComboboxIndexChange(this.comboBoxWasteDisposal, Message.NotificationClass.WAS); this.RegisterTextboxChange(this.textBoxWasteDisposalServiceProviders, Message.NotificationClass.WAS); } private void CheckBoxValidExemption_Checked(object sender, RoutedEventArgs e) { bool on = !(this.checkBoxValidExemption.IsChecked ?? false); this.checkBoxAccurateCorrectDetails.IsEnabled = on; this.comboBoxWasteDisposal.IsEnabled = on; this.textBoxWasteDisposalServiceProviders.IsEnabled = on; this.dataGridWaste.IsEnabled = on; this.locodeCtrlLastWastePort.IsEnabled = on; this.datePickerDateLastDisposal.IsEnabled = on; } private void buttonAddMissingEntries_Click(object sender, RoutedEventArgs e) { this._was.AddMissingWaste(); if(this._was.Waste.Count < 15) { Waste newWaste = new Waste { Identifier = DatabaseEntity.GetNewIdentifier(this._was.Waste), WAS = this._was, WasteAmountGeneratedTillNextPort_MTQ = 0, WasteAmountRetained_MTQ = 0, WasteCapacity_MTQ = 0, WasteDescription = "", WasteDisposalAmount_MTQ = 0, WasteDisposalPort = "ZZUKN", WasteDisposedAtLastPort_MTQ = 0 }; this._was.Waste.Add(newWaste); } this.SublistElementChanged(Message.NotificationClass.WAS); this.dataGridWaste.Items.Refresh(); } public override void Initialize() { base.Initialize(); foreach (Message aMessage in this.Messages) { if (aMessage.MessageNotificationClass == Message.NotificationClass.WAS) { this._wasMessage = aMessage; this.ControlMessages.Add(aMessage); } } #region init WAS if (this._wasMessage == null) { this._wasMessage = this.Core.CreateMessage(Message.NotificationClass.WAS); this.Messages.Add(this._wasMessage); } WAS was = null; if (this._wasMessage.Elements.Count > 0) was = this._wasMessage.Elements[0] as WAS; if (was == null) { was = new WAS(); was.MessageCore = this.Core; was.MessageHeader = this._wasMessage; _wasMessage.Elements.Add(was); } this.wasGroupBox.DataContext = was; this._was = was; this.dataGridWaste.Initialize(); this.dataGridWaste.ItemsSource = was.Waste; this.comboBoxWasteDisposal.ItemsSource = _wasteDeliveryList; this.dataGridWaste.AddingNewItem += DataGridWaste_AddingNewItem; this.dataGridWaste.EditRequested += DataGridWaste_EditRequested; this.dataGridWaste.DeleteRequested += DataGridWaste_DeleteRequested; this.dataGridWaste.CreateRequested += DataGridWaste_CreateRequested; this.checkBoxValidExemption.Checked += CheckBoxValidExemption_Checked; this.checkBoxValidExemption.Unchecked += CheckBoxValidExemption_Checked; #endregion } #region Waste grid event handler private void DataGridWaste_CreateRequested() { EditWasteDialog epd = new EditWasteDialog(); epd.Waste = new Waste(); epd.Waste.Identifier = Waste.GetNewIdentifier(_was.Waste); epd.Waste.WAS = this._was; epd.AddClicked += () => { epd.CopyValuesToEntity(); if(!this._was.Waste.Contains(epd.Waste)) this._was.Waste.Add(epd.Waste); this.dataGridWaste.Items.Refresh(); epd.Waste = new Waste(); epd.Waste.WAS = this._was; epd.Waste.Identifier = Waste.GetNewIdentifier(_was.Waste); this.SublistElementChanged(Message.NotificationClass.WAS); }; if (epd.ShowDialog() ?? false) { if(!this._was.Waste.Contains(epd.Waste)) _was.Waste.Add(epd.Waste); this.dataGridWaste.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.WAS); } } private void DataGridWaste_DeleteRequested(DatabaseEntity obj) { if (obj is Waste waste) { // are you sure dialog is in base class _was.Waste.Remove(waste); DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Delete(waste); DatabaseEntity.ResetIdentifiers(new List(_was.Waste)); this.SublistElementChanged(Message.NotificationClass.WAS); this.dataGridWaste.Items.Refresh(); } } private void DataGridWaste_EditRequested(DatabaseEntity obj) { EditWasteDialog epd = new EditWasteDialog(); epd.Waste = obj as Waste; epd.AddClicked += () => { epd.CopyValuesToEntity(); if(!_was.Waste.Contains(epd.Waste)) _was.Waste.Add(epd.Waste); this.dataGridWaste.Items.Refresh(); epd.Waste = new Waste(); epd.Waste.Identifier = Waste.GetNewIdentifier(_was.Waste); epd.Waste.WAS = _was; this.SublistElementChanged(Message.NotificationClass.WAS); }; if (epd.ShowDialog() ?? false) { if (!_was.Waste.Contains(epd.Waste)) _was.Waste.Add(epd.Waste); this.dataGridWaste.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.WAS); } } private void DataGridWaste_AddingNewItem(object sender, AddingNewItemEventArgs e) { this.DataGridWaste_CreateRequested(); } #endregion #region Highlighting public override void HighlightErrorMessageContainer() { if (this._wasMessage.HasErrors) { HighlightService.HighlightControl(this.wasGroupBox, HighlightService.HighlightStyle.ERROR, this._wasMessage); } } public override void HighlightViolationMessageContainer() { if (this._wasMessage.HasViolations) HighlightService.HighlightControl(this.wasGroupBox, HighlightService.HighlightStyle.VIOLATION, this._wasMessage); } #endregion #region mouse wheel private void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) { ScrollViewer scv = (ScrollViewer)sender; scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta); e.Handled = true; } #endregion #region Excel file import private void buttonImportFromExcel_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Excel Files|*.xls;*.xlsx"; if (ofd.ShowDialog() ?? false) { FileStream stream; try { stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } using (var reader = ExcelReaderFactory.CreateReader(stream)) { List importWasteList = new List(); try { do { // skip first two rows reader.Read(); reader.Read(); int cnt = 0; object o = null; while (reader.Read() && (cnt < 15)) { if (((IExcelDataReader)reader).FieldCount < 10) { throw new InvalidDataException("Sheet must have 10 Columns of data"); } Waste waste = new Waste(); if (!reader.IsDBNull(1)) o = reader.GetValue(1); else o = null; if (o != null) { waste.WasteType = Convert.ToInt32(o); if (waste.WasteType == 2313) waste.WasteType = 2600; } if (!reader.IsDBNull(3)) waste.WasteDescription = reader.GetString(3); if (!reader.IsDBNull(4)) o = reader.GetValue(4); else o = null; if (o != null) waste.WasteDisposalAmount_MTQ = Convert.ToDouble(o); if (!reader.IsDBNull(5)) o = reader.GetValue(5); else o = null; if (o != null) waste.WasteCapacity_MTQ = Convert.ToDouble(o); if (!reader.IsDBNull(6)) o = reader.GetValue(6); else o = null; if (o != null) waste.WasteAmountRetained_MTQ = Convert.ToDouble(o); if (!reader.IsDBNull(7)) waste.WasteDisposalPort = reader.GetString(7); if (!reader.IsDBNull(8)) o = reader.GetValue(8); else o = null; if (o != null) waste.WasteAmountGeneratedTillNextPort_MTQ = Convert.ToDouble(o); if (!reader.IsDBNull(9)) o = reader.GetValue(9); else o = null; if (o != null) waste.WasteDisposedAtLastPort_MTQ = Convert.ToDouble(o); waste.WAS = this._was; waste.IsDirty = true; waste.Identifier = Waste.GetNewIdentifier(this._was.Waste); this._was.Waste.Add(waste); importWasteList.Add(waste); cnt++; } } while (reader.NextResult()); } catch (Exception ex) { MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); } if (importWasteList.Count > 0) { this.dataGridWaste.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.SEC); MessageBox.Show(String.Format(Properties.Resources.textWasteImported, importWasteList.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); } } stream.Close(); } } #endregion } }