Jelajahi Sumber

Added company file selector

Kenric Nugteren 1 tahun lalu
induk
melakukan
44faa3791b

+ 1 - 0
InABox.Poster.MYOB/InABox.Poster.MYOB.csproj

@@ -14,6 +14,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
+    <ProjectReference Include="..\inabox.wpf\InABox.Wpf.csproj" />
   </ItemGroup>
 
 </Project>

+ 23 - 0
InABox.Poster.MYOB/MYOBCompanyFileGrid.cs

@@ -0,0 +1,23 @@
+using InABox.Core;
+using InABox.DynamicGrid;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Poster.MYOB;
+
+public class MYOBCompanyFile : BaseObject
+{
+    [NullEditor]
+    public Guid ID { get; set; }
+
+    [EditorSequence(1)]
+    [TextBoxEditor]
+    public string Name { get; set; }
+}
+
+public class MYOBCompanyFileGrid : DynamicItemsListGrid<MYOBCompanyFile>
+{
+}

+ 29 - 0
InABox.Poster.MYOB/MYOBCompanyFileSelectionDialog.xaml

@@ -0,0 +1,29 @@
+<Window x:Class="InABox.Poster.MYOB.MYOBCompanyFileSelectionDialog"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:InABox.Poster.MYOB"
+        mc:Ignorable="d"
+        Title="Select Company File:" Height="450" Width="800"
+        x:Name="Window">
+    <Grid DataContext="{Binding ElementName=Window}">
+        <Grid.RowDefinitions>
+            <RowDefinition Height="*"/>
+            <RowDefinition Height="Auto"/>
+        </Grid.RowDefinitions>
+        <local:MYOBCompanyFileGrid x:Name="Grid" Grid.Row="0" Margin="5,5,5,0"
+                                   OnSelectItem="Grid_OnSelectItem"/>
+        <DockPanel Grid.Row="1" LastChildFill="False" x:Name="Buttons">
+            <Button x:Name="CancelButton" Click="CancelButton_Click"
+                    Content="Cancel"
+                    Margin="5" Padding="5" MinWidth="60"
+                    DockPanel.Dock="Right"/>
+            <Button x:Name="OKButton" Click="OKButton_Click"
+                    Content="OK"
+                    Margin="5,5,0,5" Padding="5" MinWidth="60"
+                    DockPanel.Dock="Right"
+                    IsEnabled="{Binding CanSave}"/>
+        </DockPanel>
+    </Grid>
+</Window>

+ 108 - 0
InABox.Poster.MYOB/MYOBCompanyFileSelectionDialog.xaml.cs

@@ -0,0 +1,108 @@
+using MYOB.AccountRight.SDK.Contracts;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Runtime.CompilerServices;
+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;
+
+namespace InABox.Poster.MYOB;
+/// <summary>
+/// Interaction logic for MYOBCompanyFileSelectionDialog.xaml
+/// </summary>
+public partial class MYOBCompanyFileSelectionDialog : Window, INotifyPropertyChanged
+{
+    private bool _canSave;
+    public bool CanSave
+    {
+        get => _canSave;
+        set
+        {
+            _canSave = true;
+            OnPropertyChanged();
+        }
+    }
+
+    public List<MYOBCompanyFile> Items
+    {
+        get => Grid.Items;
+        set => Grid.Items = value;
+    }
+
+    public MYOBCompanyFile? Result
+    {
+        get
+        {
+            var row = Grid.SelectedRows.FirstOrDefault();
+            if(row is not null)
+            {
+                return Grid.LoadItem(row);
+            }
+            return null;
+        }
+    }
+
+    public MYOBCompanyFileSelectionDialog()
+    {
+        InitializeComponent();
+    }
+
+    public event PropertyChangedEventHandler? PropertyChanged;
+
+    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
+    {
+        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+    }
+
+    private void Grid_OnSelectItem(object sender, DynamicGrid.DynamicGridSelectionEventArgs e)
+    {
+        CanSave = e.Rows is not null && e.Rows.Length > 0;
+    }
+
+    public static CompanyFile? SelectCompanyFile()
+    {
+        var data = MYOBPosterEngine.GetConnectionData();
+        var window = new MYOBCompanyFileSelectionDialog();
+
+        var files = data.CompanyFileService.GetRange();
+
+        window.Items = files.Select(x =>
+        {
+            return new MYOBCompanyFile
+            {
+                ID = x.Id,
+                Name = x.Name
+            };
+        }).ToList();
+        if(window.ShowDialog() == true)
+        {
+            var result = window.Result;
+            if(result is not null)
+            {
+                return files.FirstOrDefault(x => x.Id == result.ID);
+            }
+            else
+            {
+                return null;
+            }
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    private void Window_Loaded(object sender, RoutedEventArgs e)
+    {
+        Grid.Refresh(true, true);
+    }
+}

+ 8 - 2
InABox.Poster.MYOB/MYOBPosterEngine.cs

@@ -154,11 +154,17 @@ public abstract class MYOBPosterEngine<TPostable, TSettings> :
         var companyFileID = globalSettings.GetCompanyFileID();
         if(data.CompanyFile is null || data.CompanyFile.Id != companyFileID)
         {
+            CompanyFile? file;
             if(companyFileID == Guid.Empty)
             {
-                throw new PostFailedMessageException("No CompanyFileID has been set.");
+                file = MYOBCompanyFileSelectionDialog.SelectCompanyFile();
+                if(file is null)
+                {
+                    throw new PostCancelledException();
+                }
             }
-            else if(globalSettings.CompanyFileUserID.IsNullOrWhiteSpace())
+
+            if(globalSettings.CompanyFileUserID.IsNullOrWhiteSpace())
             {
                 throw new PostFailedMessageException("CompanyFile credentials have not been set.");
             }