// Copyright (c) 2017 schick Informatik // Description: PO Nummer Übersicht. Ergänzung Nummern. Excel Export // using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Windows; using System.Windows.Controls; using Microsoft.Win32; using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; using bsmd.database; namespace ENI2.Controls { /// /// Interaction logic for POListControl.xaml /// public partial class POListControl : UserControl { #region Fields private readonly string[] _comboBoxEntries = { "All", "Maersk BRV/WHV", "SeaGo BHV", "SeaGo WHV" }; private List searchResult = new List(); private readonly List filteredResult = new List(); #endregion #region Construction public POListControl() { InitializeComponent(); Loaded += POList_Loaded; } #endregion #region control event handler private void POList_Loaded(object sender, RoutedEventArgs e) { this.comboBoxFilterType.ItemsSource = this._comboBoxEntries; this.doubleUpDownCalendarWeek.Value = bsmd.database.Util.GetIso8601WeekOfYear(DateTime.Now); this.dataGridPOCores.ItemsSource = this.filteredResult; } private void buttonExcelExport_Click(object sender, RoutedEventArgs e) { if (this.filteredResult.Count == 0) return; OpenFileDialog ofd = new OpenFileDialog { Filter = "Excel Files|*.xls;*.xlsx" }; if (ofd.ShowDialog() ?? false) { try { Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); excelApp.DisplayAlerts = false; Workbook workBook = excelApp.Workbooks.Open(ofd.FileName); Worksheet workSheet = workBook.Worksheets[1]; int rowIndex = 3; foreach(MessageCore core in this.filteredResult) { workSheet.Cells[rowIndex, 7].Value = core.Shipname; workSheet.Cells[rowIndex, 10].Value = core.PoC.Substring(2); if(core.POATA.HasValue) workSheet.Cells[rowIndex, 11].Value = core.POATA.Value.ToShortDateString(); workSheet.Cells[rowIndex, 13].Value = core.PONumber; rowIndex++; } workBook.Save(); workBook.Close(); Marshal.ReleaseComObject(workBook); excelApp.Quit(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } private void buttonSaveChanges_Click(object sender, RoutedEventArgs e) { foreach(MessageCore messageCore in this.filteredResult) { if (messageCore.IsDirty) { DBManager.Instance.Save(messageCore); messageCore.IsDirty = false; // load ATA for this Core /* DAS ATA WIRD JETZT NICHT MEHR IN DIE MELDEKLASSE GESPEICHERT Message ataMessage = DBManager.Instance.GetMessage(messageCore, Message.NotificationClass.ATA); if(ataMessage?.Elements.Count == 1) { if(messageCore.ATA != ((ATA)ataMessage.Elements[0]).ATAPortOfCall) { ((ATA)ataMessage.Elements[0]).ATAPortOfCall = messageCore.ATA; DBManager.Instance.Save(ataMessage.Elements[0]); } } */ } } this.buttonSaveChanges.IsEnabled = false; } private void doubleUpDownCalendarWeek_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (!this.IsLoaded) return; this.comboBoxFilterType.SelectedIndex = -1; this.PerformSearch(); } private void comboBoxFilterType_SelectionChanged(object sender, SelectionChangedEventArgs e) { this.dataGridPOCores.ItemsSource = null; if (this.comboBoxFilterType.SelectedIndex == -1) return; this.filteredResult.Clear(); foreach (MessageCore core in this.searchResult) { switch (this.comboBoxFilterType.SelectedIndex) { case 0: this.filteredResult.Add(core); break; case 1: if (core.IsFlagSet(MessageCore.CoreFlags.MAERSK_BHV)) this.filteredResult.Add(core); break; case 2: if (core.IsFlagSet(MessageCore.CoreFlags.SEAGO_BHV)) this.filteredResult.Add(core); break; case 3: if (core.IsFlagSet(MessageCore.CoreFlags.SEAGO_WHV)) this.filteredResult.Add(core); break; case 4: if (core.IsFlagSet(MessageCore.CoreFlags.HOEGH)) this.filteredResult.Add(core); break; } } this.dataGridPOCores.ItemsSource = this.filteredResult; } private void dataGridPOCores_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { if (e.Column == gridColumnPONumber) { // validate for "true" number var el = e.EditingElement as System.Windows.Controls.TextBox; if (!el.Text.IsDigitsOnly()) { el.Text = string.Empty; e.Cancel = true; } else { buttonSaveChanges.IsEnabled = true; MessageCore editedCore = this.filteredResult[e.Row.GetIndex()]; editedCore.IsDirty = true; } } if(e.Column == gridColumnGroup) { var el = e.EditingElement as ComboBox; DictionaryEntry selectedItem = (DictionaryEntry) el.SelectedItem; MessageCore.CoreFlags coreFlag = (MessageCore.CoreFlags) Enum.Parse(typeof(MessageCore.CoreFlags), selectedItem.Value.ToString()); MessageCore editedCore = this.filteredResult[e.Row.GetIndex()]; // clear all first editedCore.SetFlag(false, MessageCore.CoreFlags.MAERSK_BHV); editedCore.SetFlag(false, MessageCore.CoreFlags.SEAGO_BHV); editedCore.SetFlag(false, MessageCore.CoreFlags.SEAGO_WHV); editedCore.SetFlag(false, MessageCore.CoreFlags.HOEGH); if (coreFlag != MessageCore.CoreFlags.NONE) { editedCore.SetFlag(true, coreFlag); } buttonSaveChanges.IsEnabled = true; editedCore.IsDirty = true; } if(e.Column == gridColumnATA) { var el = e.EditingElement as System.Windows.Controls.TextBox; if(DateTime.TryParse(el.Text, out DateTime localATA)) { MessageCore editedCore = this.filteredResult[e.Row.GetIndex()]; editedCore.ATA = DateTime.SpecifyKind(localATA, DateTimeKind.Local).ToUniversalTime(); buttonSaveChanges.IsEnabled = true; editedCore.IsDirty = true; } else { el.Text = string.Empty; e.Cancel = true; } } } } #endregion #region private methods private void PerformSearch() { this.dataGridPOCores.ItemsSource = null; this.filteredResult.Clear(); if (!this.doubleUpDownCalendarWeek.Value.HasValue) return; Dictionary filterDict = new Dictionary(); DateTime start = bsmd.database.Util.FirstDateOfWeekISO8601(DateTime.Now.Year, (int)this.doubleUpDownCalendarWeek.Value); DateTime end = start.Add(new TimeSpan(6, 23, 59, 59)); // Die Suche findet in einem erweiterten Intervall statt, da später wenn möglich nach ATA gefiltert wird uint from = start.Subtract(new TimeSpan(10, 0, 0, 0)).ToUniversalTime().ToUnixTimeStamp(); uint to = end.Add(new TimeSpan(5, 0, 0, 0)).ToUniversalTime().ToUnixTimeStamp(); filterDict.Add(MessageCore.SearchFilterType.FILTER_ETA, string.Format("{0}:{1}", from.ToString() ?? "", to.ToString() ?? "")); Util.UIHelper.SetBusyState(); // suche auslösen this.searchResult = DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).GetMessageCoresWithFilters(filterDict); // alle anderen Häfen weg this.searchResult.RemoveAll(item => !item.PoC.Equals("DEBRV") && !item.PoC.Equals("DEWHV") && !item.PoC.Equals("DEWVN")); // rückwärts iterieren um nach ETA und ATA zu filtern if (this.searchResult.Count > 0) { for (int i = this.searchResult.Count - 1; i >= 0; i--) { MessageCore messageCore = this.searchResult[i]; if (messageCore.ATA.HasValue) { if ((messageCore.ATA.Value < start) || (messageCore.ATA.Value > end)) { this.searchResult.RemoveAt(i); } else { if(!messageCore.POATA.HasValue) { messageCore.POATA = messageCore.ATA; messageCore.IsDirty = true; this.buttonSaveChanges.IsEnabled = true; } } } else { if ((messageCore.ETA.Value < start) || (messageCore.ETA.Value > end)) this.searchResult.RemoveAt(i); } } } searchResult.Sort((x, y) => DateTime.Compare(x.ATA ?? DateTime.MaxValue, y.ATA ?? DateTime.MaxValue)); this.dataGridPOCores.SelectedItem = null; this.filteredResult.AddRange(searchResult); this.dataGridPOCores.ItemsSource = this.filteredResult; } #endregion } }