Просмотр исходного кода

Moved Default Tax code setting into the global settings

Kenric Nugteren 1 год назад
Родитель
Сommit
b03de5b8ff

+ 3 - 0
InABox.Poster.MYOB/MYOBGlobalPosterSettings.cs

@@ -20,4 +20,7 @@ public class MYOBGlobalPosterSettings : GlobalPosterSettings
     [EditorSequence(3)]
     [PasswordEditor(ViewButtonVisible = true)]
     public string CompanyFilePassword { get; set; }
+
+    [TextBoxEditor(ToolTip = "The MYOB tax code which should be used for global supplier tax codes, customer tax codes, freight tax codes, etc.")]
+    public string DefaultTaxCode { get; set; }
 }

+ 199 - 0
InABox.Poster.MYOB/MYOBPosterUtils.cs

@@ -0,0 +1,199 @@
+using InABox.Core;
+using InABox.Core.Postable;
+using MYOB.AccountRight.SDK;
+using MYOB.AccountRight.SDK.Contracts.Version2;
+using MYOB.AccountRight.SDK.Services;
+using MYOB.AccountRight.SDK.Services.GeneralLedger;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MYOBBaseEntity = MYOB.AccountRight.SDK.Contracts.Version2.BaseEntity;
+using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
+
+namespace InABox.Poster.MYOB;
+
+public static class MYOBPosterUtils
+{
+    public class QueryResult<T> : Result<PagedCollection<T>, Exception>
+    {
+        public QueryResult(PagedCollection<T> value) : base(value)
+        {
+        }
+
+        public QueryResult(Exception error) : base(error)
+        {
+        }
+
+        public static QueryResult<T> Ok(PagedCollection<T> collection) => new QueryResult<T>(collection);
+
+        public static QueryResult<T> Error(Exception e) => new QueryResult<T>(e);
+    }
+
+    public class GetResult<T> : Result<T, Exception>
+    {
+        public GetResult(T value) : base(value)
+        {
+        }
+
+        public GetResult(Exception error) : base(error)
+        {
+        }
+
+        public static GetResult<T> Ok(T value) => new GetResult<T>(value);
+
+        public static GetResult<T> Error(Exception e) => new GetResult<T>(e);
+    }
+
+    public static Result<PagedCollection<T>, Exception> Query<T>(
+        this MutableService<T> service, MYOBConnectionData data,
+        Filter<T>? filter,
+        int? top = null, int? skip = null
+    )
+        where T : MYOBBaseEntity
+    {
+        var queries = new List<string>();
+        if(filter is not null)
+        {
+            queries.Add($"$filter={Uri.EscapeDataString(filter.AsOData())}");
+        }
+        if (top.HasValue)
+        {
+            queries.Add($"$top={top.Value}");
+        }
+        if (skip.HasValue)
+        {
+            queries.Add($"$skip={skip.Value}");
+        }
+
+        try
+        {
+            var values = service.GetRange(data.CompanyFile, string.Join('&', queries), data.CompanyFileCredentials);
+            return QueryResult<T>.Ok(values);
+        }
+        catch(ApiCommunicationException e)
+        {
+            return QueryResult<T>.Error(new Exception(FormatApiException(e), e));
+        }
+        catch(Exception e)
+        {
+            return QueryResult<T>.Error(e);
+        }
+    }
+
+    public static Result<T, Exception> Save<T>(
+        this MutableService<T> service, MYOBConnectionData data,
+        T entity)
+        where T : MYOBBaseEntity
+    {
+        try
+        {
+            if(entity.UID == Guid.Empty)
+            {
+                return Result.Ok(service.InsertEx(data.CompanyFile, entity, data.CompanyFileCredentials));
+            }
+            else
+            {
+                return Result.Ok(service.UpdateEx(data.CompanyFile, entity, data.CompanyFileCredentials));
+            }
+        }
+        catch(ApiCommunicationException e)
+        {
+            return Result.Error(new Exception(FormatApiException(e), e));
+        }
+        catch(Exception e)
+        {
+            return Result.Error(e);
+        }
+    }
+
+    public static GetResult<T> Get<T>(this MutableService<T> service, MYOBConnectionData data, Guid id)
+        where T : MYOBBaseEntity
+    {
+        try
+        {
+            var value = service.Get(data.CompanyFile, id, data.CompanyFileCredentials);
+            return GetResult<T>.Ok(value);
+        }
+        catch(ApiCommunicationException e)
+        {
+            return GetResult<T>.Error(new Exception(FormatApiException(e), e));
+        }
+        catch(Exception e)
+        {
+            return GetResult<T>.Error(e);
+        }
+    }
+
+    public static TService CreateService<TService, TEntity>(this MYOBConnectionData data)
+        where TService : MutableService<TEntity>
+        where TEntity : MYOBBaseEntity
+    {
+        return (Activator.CreateInstance(typeof(TService), data.Configuration, null, data.AuthKey) as TService)!;
+    }
+
+    public static Result<Guid, Exception> GetUID<TService, TEntity>(this MYOBConnectionData data, Filter<TEntity> filter)
+        where TService : MutableService<TEntity>
+        where TEntity : MYOBBaseEntity
+    {
+        var service = data.CreateService<TService, TEntity>();
+        var result = service.Query(data, filter, top: 1);
+        if(!result.Get(out var items, out var error))
+        {
+            return Result.Error(error);
+        }
+        if(items.Items.Length == 0)
+        {
+            return Result.Ok(Guid.Empty);
+        }
+        return Result.Ok(items.Items[0].UID);
+    }
+
+    public static string FormatApiException(ApiCommunicationException e)
+    {
+        if(e.Errors.Count > 0)
+        {
+            var message = string.Join('\n', e.Errors.Select(x =>
+            {
+                return $"{x.Name}: {x.Message} ({x.AdditionalDetails})";
+            }));
+            return message;
+        }
+        else
+        {
+            return e.Message;
+        }
+    }
+
+    public static Result<Guid, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, string code)
+        => data.GetUID<TaxCodeService, MYOBTaxCode>(new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(code));
+
+    public static Result<Guid, Exception> GetDefaultTaxCode(MYOBConnectionData data, MYOBGlobalPosterSettings settings)
+    {
+        if (settings.DefaultTaxCode.IsNullOrWhiteSpace())
+        {
+            throw new MissingSettingException<MYOBGlobalPosterSettings>(x => x.DefaultTaxCode);
+        }
+        else if(data.GetMYOBTaxCodeUID(settings.DefaultTaxCode).Get(out var taxID, out var error))
+        {
+            if (taxID == Guid.Empty)
+            {
+                return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code '{settings.DefaultTaxCode}'"));
+            }
+            return Result.Ok(taxID);
+        }
+        else
+        {
+            CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code '{settings.DefaultTaxCode}'");
+            return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code '{settings.DefaultTaxCode}': {error.Message}", error));
+        }
+    }
+
+    public static Result<Guid, Exception> GetDefaultTaxCode<TPostable, TSettings>(this IMYOBPoster<TPostable, TSettings> poster)
+        where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+        where TSettings : MYOBPosterSettings
+    {
+        return GetDefaultTaxCode(poster.ConnectionData, poster.GlobalSettings);
+    }
+}

+ 7 - 3
inabox.wpf/Grids/PostableSettingsGrid.cs

@@ -71,26 +71,30 @@ public class PostableSettingsGrid : DynamicItemsListGrid<PostableSettings>
         ConfigurePosterSettings(entityType, settingsType);
     }
 
-    public static void ConfigureGlobalPosterSettings(Type globalSettingsType)
+    public static bool ConfigureGlobalPosterSettings(Type globalSettingsType)
     {
         var globalPosterSettings = PosterUtils.LoadGlobalPosterSettings(globalSettingsType);
         var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), globalSettingsType);
         if(grid.EditItems(new object[] { globalPosterSettings }))
         {
             PosterUtils.SaveGlobalPosterSettings(globalSettingsType, globalPosterSettings);
+            return true;
         }
+        return false;
     }
 
-    public static void ConfigurePosterSettings(Type entityType, Type settingsType)
+    public static bool ConfigurePosterSettings(Type entityType, Type settingsType)
     {
         var posterSettings = PosterUtils.LoadPosterSettings(entityType, settingsType);
         var grid = DynamicGridUtils.CreateDynamicGrid(typeof(PosterSettingsGrid<>), settingsType);
         if(grid.EditItems(new object[] { posterSettings }))
         {
             PosterUtils.SavePosterSettings(entityType, settingsType, posterSettings);
+            return true;
         }
+        return false;
     }
 
-    public static void ConfigurePosterSettings<T>(Type settingsType)
+    public static bool ConfigurePosterSettings<T>(Type settingsType)
         where T : Entity, IPostable, IRemotable, IPersistent, new() => ConfigurePosterSettings(typeof(T), settingsType);
 }