// Copyright (c) 2025 - schick Informatik // Description: Display control of formsheet Tab 8. Pas data arrival // // using bsmd.database; using ClosedXML.Excel; using ENI2.EditControls; using ENI2.Locode; using ENI2.Util; using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace ENI2.SheetDisplayControls { /// /// Interaction logic for PassengerPreArrivalControl.xaml /// public partial class PassengerPreArrivalControl : DetailBaseControl { #region Fields private Message _pasMessage; private Message _pasdMessage; #endregion #region Construction public PassengerPreArrivalControl() { InitializeComponent(); } #endregion #region public overrides public override void Initialize() { base.Initialize(); foreach (Message aMessage in this.Messages) { if (aMessage.MessageNotificationClass == Message.NotificationClass.PASA) { this._pasMessage = aMessage; this.ControlMessages.Add(aMessage); } if (aMessage.MessageNotificationClass == Message.NotificationClass.PASD) { this._pasdMessage = aMessage; this.ControlMessages.Add(aMessage); } } #region init PASA if (this._pasMessage == null) { this._pasMessage = this.Core.CreateMessage(Message.NotificationClass.PASA); this.Messages.Add(this._pasMessage); } this.textBlockNumPasEntries.DataContext = this._pasMessage; this.dataGridPassengerList.Initialize(); this.dataGridPassengerList.ItemsSource = this._pasMessage.Elements; this.dataGridPassengerList.AddingNewItem += DataGridPassengerList_AddingNewItem; this.dataGridPassengerList.EditRequested += DataGridPassengerList_EditRequested; this.dataGridPassengerList.DeleteRequested += DataGridPassengerList_DeleteRequested; this.dataGridPassengerList.CreateRequested += DataGridPassengerList_CreateRequested; this.dataGridPassengerList.RefreshGrid += DataGridPassengerList_RefreshGrid; this.dataGridPassengerList.MultiEditRequested += DataGridPassengerList_MultiEditRequested; if (this._pasMessage.Elements.Count > 0) { this.checkBoxPasNotificationSchengen.IsChecked = ((PAS)this._pasMessage.Elements[0]).NotificationSchengen; this.checkBoxPasNotificationPAX.IsChecked = ((PAS)this._pasMessage.Elements[0]).NotificationPAX; } // extra menu copy to PASD { this.dataGridPassengerList.ContextMenu.Items.Add(new Separator()); MenuItem copyPASAItem = new MenuItem(); copyPASAItem.Header = Properties.Resources.textCopyToPASD; copyPASAItem.Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/documents.png")) }; copyPASAItem.Click += CopyPASAItem_Click; ; this.dataGridPassengerList.ContextMenu.Items.Add(copyPASAItem); } #endregion } public override void SetEnabled(bool enabled) { base.SetEnabled(enabled); this.passengerArrivalGroupBox.IsEnabled = enabled; } #endregion #region event handler private void CopyPASAItem_Click(object sender, RoutedEventArgs e) { if (this.dataGridPassengerList.SelectedItems != null) { foreach (PAS pasa in this.dataGridPassengerList.SelectedItems) { PASD pasd = new PASD(); pasd.MessageHeader = this._pasdMessage; pasd.CopyFromPAS(pasa); pasd.IsDeparture = true; pasd.Identifier = DatabaseEntity.GetNewIdentifier(this._pasdMessage.Elements); this._pasdMessage.Elements.Add(pasd); this.SublistElementChanged(Message.NotificationClass.PASD); } } } private void buttonImportExcelPassenger_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog { Filter = "Excel Files|*.xls;*.xlsx" }; if (ofd.ShowDialog() ?? false) { try { using (var workbook = new XLWorkbook(ofd.FileName)) { var worksheet = workbook.Worksheet(1); // Get first worksheet var rows = worksheet.RangeUsed().RowsUsed().Skip(1); // Skip header row if present List importPassenger = new List(); foreach (var row in rows) { if (row.RowNumber() > worksheet.RangeUsed().RowCount()) break; if (worksheet.RangeUsed().ColumnCount() < 17) { throw new InvalidDataException("Sheet must have 17 columns of data"); } PAS pas = new PAS(); if (row.Cell(1).IsEmpty() && row.Cell(2).IsEmpty()) continue; if (!row.Cell(1).IsEmpty()) pas.PassengerLastName = row.Cell(1).GetString().Clean(); if (pas.PassengerLastName?.Equals("Family Name") == true || string.IsNullOrWhiteSpace(pas.PassengerLastName)) continue; if (!row.Cell(2).IsEmpty()) pas.PassengerFirstName = row.Cell(2).GetString().Clean(); if (!row.Cell(3).IsEmpty()) pas.PassengerGender = GlobalStructures.ParseGender(row.Cell(3).GetString()); if (!row.Cell(4).IsEmpty()) pas.PassengerPortOfEmbarkation = row.Cell(4).GetString().Clean(); if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfEmbarkation) == null) pas.PassengerPortOfEmbarkation = null; if (!row.Cell(5).IsEmpty()) pas.PassengerPortOfDisembarkation = row.Cell(5).GetString().Clean(); if (LocodeDB.PortNameFromLocode(pas.PassengerPortOfDisembarkation) == null) pas.PassengerPortOfDisembarkation = null; if (!row.Cell(6).IsEmpty()) pas.PassengerInTransit = GlobalStructures.ReadBoolean(row.Cell(6).GetString()); if (!row.Cell(7).IsEmpty()) pas.PassengerNationality = row.Cell(7).GetString().Substring(0, 2).ToUpper(); if (!row.Cell(8).IsEmpty()) pas.PassengerPlaceOfBirth = row.Cell(8).GetString().Clean(); if (!row.Cell(9).IsEmpty()) pas.PassengerCountryOfBirth = row.Cell(9).GetString().Substring(0, 2).ToUpper().Clean(); if (!row.Cell(10).IsEmpty()) pas.PassengerDateOfBirth = row.Cell(10).GetDateTime(); if (!row.Cell(11).IsEmpty()) pas.PassengerIdentityDocumentType = GlobalStructures.ReadIdentityDocumentType(row.Cell(11).GetString()); if (!row.Cell(12).IsEmpty()) pas.PassengerIdentityDocumentId = row.Cell(12).GetString().Clean(); if (!row.Cell(13).IsEmpty()) pas.PassengerIdentityDocumentIssuingState = row.Cell(13).GetString().Substring(0, 2).ToUpper(); if (!row.Cell(14).IsEmpty()) pas.PassengerIdentityDocumentExpiryDate = row.Cell(14).GetDateTime(); if (!row.Cell(15).IsEmpty()) pas.PassengerVisaNumber = row.Cell(15).GetString().Clean(); if (!row.Cell(16).IsEmpty()) pas.EmergencyCare = row.Cell(16).GetString().Clean(); if (!row.Cell(17).IsEmpty()) pas.EmergencyContactNumber = row.Cell(17).GetString().Clean(); pas.MessageHeader = this._pasMessage; pas.IsDirty = true; pas.Identifier = PAS.GetNewIdentifier(this._pasMessage.Elements); this._pasMessage.Elements.Add(pas); importPassenger.Add(pas); } if (importPassenger.Count > 0) { this.dataGridPassengerList.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.PASA); MessageBox.Show(String.Format(Properties.Resources.textPassengerImported, importPassenger.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information); } } } catch (Exception ex) { MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error); } } } private void checkBoxPasNotificationSchengen_Click(object sender, RoutedEventArgs e) { foreach (PAS pas in _pasMessage.Elements.Cast()) { pas.NotificationSchengen = checkBoxPasNotificationSchengen.IsChecked; } this.SublistElementChanged(Message.NotificationClass.PASA); } private void checkBoxPasNotificationPAX_Click(object sender, RoutedEventArgs e) { foreach (PAS pas in _pasMessage.Elements.Cast()) { pas.NotificationPAX = checkBoxPasNotificationPAX.IsChecked; } this.SublistElementChanged(Message.NotificationClass.PASA); } private async void buttonDeleteAllPasA_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show(Properties.Resources.textConfimDeleteAllEntries, Properties.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { foreach (PAS pasa in this._pasMessage.Elements.Cast()) { await DBManagerAsync.DeleteAsync(pasa); } this._pasMessage.Elements.Clear(); this.dataGridPassengerList.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.PASA); } } #endregion #region passenger grid arrival private void DataGridPassengerList_CreateRequested() { EditPASDialog epd = new EditPASDialog { PAS = new PAS() }; epd.PAS.Identifier = PAS.GetNewIdentifier(_pasMessage.Elements); epd.PAS.MessageHeader = this._pasMessage; epd.AddClicked += () => { epd.CopyValuesToEntity(); if (!this._pasMessage.Elements.Contains(epd.PAS)) { this._pasMessage.Elements.Add(epd.PAS); this.CheckPASA(); } this.dataGridPassengerList.Items.Refresh(); epd.PAS = new PAS { MessageHeader = this._pasMessage, Identifier = PAS.GetNewIdentifier(_pasMessage.Elements) }; this.SublistElementChanged(Message.NotificationClass.PASA); }; if (epd.ShowDialog() ?? false) { if (!this._pasMessage.Elements.Contains(epd.PAS)) { _pasMessage.Elements.Add(epd.PAS); this.CheckPASA(); } this.dataGridPassengerList.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.PASA); } } private void DataGridPassengerList_DeleteRequested(DatabaseEntity obj) { if (obj is PAS pas) { // are you sure dialog is in base class _pasMessage.Elements.Remove(pas); DBManager.Instance.Delete(pas); } } private void DataGridPassengerList_RefreshGrid() { DatabaseEntity.ResetIdentifiers(_pasMessage.Elements); this.SublistElementChanged(Message.NotificationClass.PASA); this.dataGridPassengerList.Items.Refresh(); } private void DataGridPassengerList_EditRequested(DatabaseEntity obj) { EditPASDialog epd = new EditPASDialog { PAS = obj as PAS }; epd.AddClicked += () => { epd.CopyValuesToEntity(); if (!_pasMessage.Elements.Contains(epd.PAS)) { _pasMessage.Elements.Add(epd.PAS); this.CheckPASA(); } this.dataGridPassengerList.Items.Refresh(); epd.PAS = new PAS { Identifier = PAS.GetNewIdentifier(_pasMessage.Elements), MessageHeader = _pasMessage }; this.SublistElementChanged(Message.NotificationClass.PASA); }; if (epd.ShowDialog() ?? false) { if (!_pasMessage.Elements.Contains(epd.PAS)) { _pasMessage.Elements.Add(epd.PAS); this.CheckPASA(); } epd.PAS.IsDirty = true; this.dataGridPassengerList.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.PASA); } } private void DataGridPassengerList_AddingNewItem(object sender, AddingNewItemEventArgs e) { this.DataGridPassengerList_CreateRequested(); } private void DataGridPassengerList_MultiEditRequested(List databaseEntities) { List pasList = new List(); foreach (PAS apas in databaseEntities.Cast()) pasList.Add(apas); // write common values of all PAS entities to template entity PAS pas = PAS.CreateCommon(pasList); EditPASDialog dialog = new EditPASDialog(); dialog.PAS = pas; dialog.AddVisible = false; if (dialog.ShowDialog() ?? false) { // write back changed values from pas to all entities and mark them as changed PAS.WriteTemplateToList(pas, pasList); this.SublistElementChanged(Message.NotificationClass.PASA); this.dataGridPassengerList.Items.Refresh(); } } #endregion #region private methods private void CheckPASA() { if (this._pasMessage.Elements.Count == 0) return; PAS firstPAS = this._pasMessage.Elements[0] as PAS; if (this._pasMessage.Elements.Count == 1) { firstPAS.NotificationSchengen = true; this.checkBoxPasNotificationSchengen.IsChecked = true; } else { for (int i = 1; i < this._pasMessage.Elements.Count; i++) { ((PAS)this._pasMessage.Elements[i]).NotificationSchengen = firstPAS.NotificationSchengen; } } } #endregion } }