Browse Source

Fixing problems with Bill poster; made supplier auto-refresh

Kenric Nugteren 1 year ago
parent
commit
27c5f2e991

+ 22 - 8
prs.shared/Posters/MYOB/BillMYOBPoster.cs

@@ -1,5 +1,6 @@
 using Comal.Classes;
 using InABox.Core;
+using InABox.Core.Postable;
 using InABox.Poster.MYOB;
 using MYOB.AccountRight.SDK.Contracts.Version2.Sale;
 using MYOB.AccountRight.SDK.Services.GeneralLedger;
@@ -32,16 +33,16 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
     {
         foreach(var (_, table) in model.ModelTables)
         {
-            table.ShouldLoad = false;
+            table.IsDefault = false;
         }
-        model.SetShouldLoad<Bill>(true);
+        model.SetIsDefault<Bill>(true);
         model.SetColumns<Bill>(RequiredBillColumns());
 
-        model.SetShouldLoad<BillLine>(true, alias: "Bill_BillLine");
-        model.SetColumns<BillLine>(RequiredBillLineColumns());
+        model.SetIsDefault<BillLine>(true, alias: "Bill_BillLine");
+        model.SetColumns<BillLine>(RequiredBillLineColumns(), alias: "Bill_BillLine");
 
         model.SetIsDefault<Supplier>(true, alias: "Bill_Supplier");
-        model.SetColumns<Supplier>(RequiredSupplierColumns());
+        model.SetColumns<Supplier>(RequiredSupplierColumns(), alias: "Bill_Supplier");
 
         return true;
     }
@@ -95,6 +96,7 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
         {
             MYOBBill myobBill;
             Exception? error;
+            bool isNew;
             if(Guid.TryParse(bill.PostedReference, out var myobID))
             {
                 if(!service.Get(ConnectionData, myobID).Get(out var newBill, out error))
@@ -104,10 +106,12 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                     continue;
                 }
                 myobBill = newBill;
+                isNew = false;
             }
             else
             {
                 myobBill = new MYOBBill();
+                isNew = true;
             }
 
             myobBill.Number = bill.Number.Truncate(13);
@@ -118,7 +122,7 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
 
             if(suppliers.TryGetValue(bill.SupplierLink.ID, out var supplier))
             {
-                if(!SupplierMYOBPoster.MapSupplier(ConnectionData, supplier).Get(out var supplierID, out error))
+                if(!SupplierMYOBPoster.MapSupplier(ConnectionData, supplier, GlobalSettings).Get(out var supplierID, out error))
                 {
                     CoreUtils.LogException("", error, $"Error while posting bill {bill.ID}");
                     results.AddFailed(bill, error.Message);
@@ -134,7 +138,18 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
             myobBill.IsReportable = true;
             // myobBill.Freight = 
             // myobBill.FreightForeign = 
-            // myobBill.FreightTaxCode = 
+
+            if (isNew)
+            {
+                if(!this.GetDefaultTaxCode().Get(out var taxCodeID, out error))
+                {
+                    results.AddFailed(bill, error.Message);
+                    continue;
+                }
+                myobBill.FreightTaxCode ??= new();
+                myobBill.FreightTaxCode.UID = taxCodeID;
+            }
+
             // myobBill.Category = 
             myobBill.Comment = bill.Description.Truncate(2000);
             // myobBill.ShippingMethod = 
@@ -152,7 +167,6 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                     var billLine = lines[i];
 
                     var line = new MYOBBillLine();
-                    line.RowID = i + 1;
                     line.Type = InvoiceLineType.Transaction;
                     line.Description = billLine.Description;
 

+ 30 - 47
prs.shared/Posters/MYOB/CustomerMYOBPoster.cs

@@ -23,8 +23,6 @@ namespace PRS.Shared.Posters.MYOB;
 
 public class CustomerMYOBPosterSettings : MYOBPosterSettings
 {
-    [TextBoxEditor(ToolTip = "The MYOB tax code which should be used when posting customers")]
-    public string DefaultTaxCode { get; set; }
 }
 
 public static class ContactMYOBUtils
@@ -38,18 +36,28 @@ public static class ContactMYOBUtils
 
     public static MYOBAddress ConvertAddress(Address address, int location, IContact contact)
     {
-        return new MYOBAddress
+        var mobile = contact.Mobile.Truncate(21);
+        var phone = contact.Telephone.Truncate(21);
+        var newAddress = new MYOBAddress
         {
             Location = location,
             Street = address.Street.Truncate(255),
             City = address.City.Truncate(255),
             State = address.State.Truncate(255),
             PostCode = address.PostCode.Truncate(11),
-            Phone1 = contact.Mobile.Truncate(21),
-            Phone2 = contact.Telephone.Truncate(21),
             Email = contact.Email.Truncate(255),
             ContactName = contact.Name.Truncate(25)
         };
+        if (mobile.IsNullOrWhiteSpace())
+        {
+            newAddress.Phone1 = phone;
+        }
+        else
+        {
+            newAddress.Phone1 = mobile;
+            newAddress.Phone2 = phone;
+        }
+        return newAddress;
     }
 }
 
@@ -138,7 +146,7 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
             .Add(x => x.ABN);
     }
 
-    public static Result<Exception> UpdateCustomer(MYOBConnectionData data, CustomerMYOBPosterSettings settings, Customer customer, MYOBCustomer myobCustomer, bool isNew)
+    public static Result<Exception> UpdateCustomer(MYOBConnectionData data, MYOBGlobalPosterSettings settings, Customer customer, MYOBCustomer myobCustomer, bool isNew)
     {
         // Documentation: https://developer.myob.com/api/myob-business-api/v2/contact/customer/
 
@@ -177,26 +185,14 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
 
         if (isNew)
         {
-            if (settings.DefaultTaxCode.IsNullOrWhiteSpace())
-            {
-                throw new PostFailedMessageException("Default tax code has not been set up.");
-            }
-            else if(data.GetMYOBTaxCodeUID(settings.DefaultTaxCode).Get(out var taxID, out var error))
+            if(!MYOBPosterUtils.GetDefaultTaxCode(data, settings).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}'"));
-                }
-                myobCustomer.SellingDetails.TaxCode ??= new();
-                myobCustomer.SellingDetails.TaxCode.UID = taxID;
-                myobCustomer.SellingDetails.FreightTaxCode ??= new();
-                myobCustomer.SellingDetails.FreightTaxCode.UID = 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));
+                return Result.Error(error);
             }
+            myobCustomer.SellingDetails.TaxCode ??= new();
+            myobCustomer.SellingDetails.TaxCode.UID = taxID;
+            myobCustomer.SellingDetails.FreightTaxCode ??= new();
+            myobCustomer.SellingDetails.FreightTaxCode.UID = taxID;
         }
         return Result.Ok();
     }
@@ -212,7 +208,7 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
     /// <param name="data"></param>
     /// <param name="customer">The customer to map to.</param>
     /// <returns>The UID of the MYOB customer.</returns>
-    public static Result<Guid, Exception> MapCustomer(MYOBConnectionData data, Customer customer)
+    public static Result<Guid, Exception> MapCustomer(MYOBConnectionData data, Customer customer, MYOBGlobalPosterSettings settings)
     {
         if(Guid.TryParse(customer.PostedReference, out var myobID))
         {
@@ -223,32 +219,16 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
         var result = service.Query(data, new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(customer.Code), top: 1);
         return result.MapOk(customers =>
         {
-            if(customers.Count == 0)
+            if(customers.Items.Length == 0)
             {
                 if(customer.Code.Length > 15)
                 {
                     return Result.Error(new Exception("Customer code is longer than 15 characters"));
                 }
                 var myobCustomer = new MYOBCustomer();
-                return UpdateCustomer(data, PosterUtils.LoadPosterSettings<Customer, CustomerMYOBPosterSettings>(), customer, myobCustomer, true)
-                    .MapOk<Result<Guid, Exception>>(() =>
-                    {
-                        try
-                        {
-                            var result = service.UpdateEx(data.CompanyFile, myobCustomer, data.CompanyFileCredentials);
-                            customer.PostedReference = result.UID.ToString();
-                            return Result.Ok(result.UID);
-                        }
-                        catch(ApiCommunicationException e)
-                        {
-                            return Result.Error(new Exception(PRSMYOBPosterUtils.FormatApiException(e), e));
-                        }
-                        catch (Exception e)
-                        {
-                            CoreUtils.LogException("", e, $"Error while posting customer {customer.ID}");
-                            return Result.Error(e);
-                        }
-                    }).Flatten();
+                return UpdateCustomer(data, settings, customer, myobCustomer, true)
+                    .MapOk(() => service.Save(data, myobCustomer).MapOk(x => x.UID))
+                    .Flatten();
             }
             else
             {
@@ -290,7 +270,10 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
             }
             else
             {
-                if(service.Query(ConnectionData, new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(customer.Code)).Get(out var myobCustomers, out error))
+                if(service.Query(
+                    ConnectionData,
+                    new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(customer.Code),
+                    top: 1).Get(out var myobCustomers, out error))
                 {
                     if(myobCustomers.Items.Length > 0)
                     {
@@ -311,7 +294,7 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
                 }
             }
 
-            if(UpdateCustomer(ConnectionData, Settings, customer, myobCustomer, isNew)
+            if(UpdateCustomer(ConnectionData, GlobalSettings, customer, myobCustomer, isNew)
                 .MapOk(() => service.Save(ConnectionData, myobCustomer)).Flatten()
                 .Get(out var result, out error))
             {

+ 1 - 1
prs.shared/Posters/MYOB/InvoiceMYOBPoster.cs

@@ -116,7 +116,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
 
             if(customers.TryGetValue(invoice.CustomerLink.ID, out var customer))
             {
-                if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer).Get(out var customerID, out error))
+                if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer, GlobalSettings).Get(out var customerID, out error))
                 {
                     CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
                     results.AddFailed(invoice, error.Message);

+ 1 - 165
prs.shared/Posters/MYOB/PRSMYOBPosterUtils.cs

@@ -1,181 +1,17 @@
 using Comal.Classes;
 using InABox.Core;
 using InABox.Poster.MYOB;
-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 PRS.Shared.Posters.MYOB;
 
 public static class PRSMYOBPosterUtils
 {
-    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 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> GetMYOBTaxCodeUID(this MYOBConnectionData data, ITaxCode code)
     {
         if(Guid.TryParse(code.PostedReference, out var id))
         {
             return Result.Ok(id);
         }
-        return GetMYOBTaxCodeUID(data, code.Code);
-    }
-
-    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;
-        }
+        return MYOBPosterUtils.GetMYOBTaxCodeUID(data, code.Code);
     }
 }

+ 2 - 2
prs.shared/Posters/MYOB/ReceiptMYOBPoster.cs

@@ -28,7 +28,7 @@ public class ReceiptMYOBPoster : IMYOBPoster<Receipt, ReceiptMYOBPosterSettings>
     {
         foreach(var (_, table) in model.ModelTables)
         {
-            table.ShouldLoad = false;
+            table.IsDefault = false;
         }
         model.SetShouldLoad<Receipt>(true);
         model.SetColumns<Receipt>(RequiredReceiptColumns());
@@ -104,7 +104,7 @@ public class ReceiptMYOBPoster : IMYOBPoster<Receipt, ReceiptMYOBPosterSettings>
 
             if(customers.TryGetValue(receipt.CustomerLink.ID, out var customer))
             {
-                if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer).Get(out var customerID, out var error))
+                if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer, GlobalSettings).Get(out var customerID, out var error))
                 {
                     CoreUtils.LogException("", error, $"Error while posting receipt {receipt.ID}");
                     results.AddFailed(receipt, error.Message);

+ 66 - 29
prs.shared/Posters/MYOB/SupplierMYOBPoster.cs

@@ -2,6 +2,7 @@
 using InABox.Clients;
 using InABox.Core;
 using InABox.Core.Postable;
+using InABox.Database;
 using InABox.Poster.MYOB;
 using MYOB.AccountRight.SDK.Services.Contact;
 using System;
@@ -15,11 +16,49 @@ namespace PRS.Shared.Posters.MYOB;
 
 public class SupplierMYOBPosterSettings : MYOBPosterSettings
 {
-    [TextBoxEditor(ToolTip = "The MYOB tax code which should be used when posting suppliers")]
-    public string DefaultTaxCode { get; set; }
 }
 
-public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettings>
+public class SupplierMYOBAutoRefresher : IAutoRefresher<Supplier>
+{
+    public bool ShouldRepost(Supplier supplier)
+    {
+        var shouldRepost = supplier.HasOriginalValue(x => x.Name)
+            || supplier.HasOriginalValue(x => x.Code)
+            || supplier.HasOriginalValue(x => x.ABN)
+            || supplier.HasOriginalValue(x => x.Email)
+            || supplier.HasOriginalValue(x => x.Telephone)
+            || supplier.Delivery.HasOriginalValue(x => x.Street)
+            || supplier.Delivery.HasOriginalValue(x => x.City)
+            || supplier.Delivery.HasOriginalValue(x => x.State)
+            || supplier.Delivery.HasOriginalValue(x => x.PostCode)
+            || supplier.Postal.HasOriginalValue(x => x.Street)
+            || supplier.Postal.HasOriginalValue(x => x.City)
+            || supplier.Postal.HasOriginalValue(x => x.State)
+            || supplier.Postal.HasOriginalValue(x => x.PostCode);
+        if (shouldRepost)
+        {
+            return true;
+        }
+
+        if(supplier.SupplierStatus.HasOriginalValue(x => x.ID))
+        {
+            var originalID = supplier.SupplierStatus.GetOriginalValue(x => x.ID);
+            var currentID = supplier.SupplierStatus.ID;
+
+            var statuses = DbFactory.Provider.Query(
+                new Filter<SupplierStatus>(x => x.ID).IsEqualTo(originalID).Or(x => x.ID).IsEqualTo(currentID),
+                Columns.None<SupplierStatus>().Add(x => x.ID).Add(x => x.Active))
+                .ToArray<SupplierStatus>();
+            if (statuses.Length == 2 && statuses[0].Active != statuses[1].Active)
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+}
+public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettings>, IAutoRefreshPoster<Supplier, SupplierMYOBAutoRefresher>
 {
     public MYOBConnectionData ConnectionData { get; set; }
     public SupplierMYOBPosterSettings Settings { get; set; }
@@ -49,12 +88,16 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
             .Add(x => x.Postal.City)
             .Add(x => x.Postal.State)
             .Add(x => x.Postal.PostCode)
+            .Add(x => x.Delivery.Street)
+            .Add(x => x.Delivery.City)
+            .Add(x => x.Delivery.State)
+            .Add(x => x.Delivery.PostCode)
             .Add(x => x.Email)
             .Add(x => x.Telephone)
             .Add(x => x.ABN);
     }
 
-    public static Result<Exception> UpdateSupplier(MYOBConnectionData data, SupplierMYOBPosterSettings settings, Supplier supplier, MYOBSupplier myobSupplier, bool isNew)
+    public static Result<Exception> UpdateSupplier(MYOBConnectionData data, MYOBGlobalPosterSettings settings, Supplier supplier, MYOBSupplier myobSupplier, bool isNew)
     {
         // Documentation: https://developer.myob.com/api/myob-business-api/v2/contact/supplier/
 
@@ -73,6 +116,11 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
         myobSupplier.Addresses =
         [
             ContactMYOBUtils.ConvertAddress(supplier.Postal, 1, new Contact
+            {
+                Email = supplier.Email,
+                Telephone = supplier.Telephone
+            }),
+            ContactMYOBUtils.ConvertAddress(supplier.Delivery, 2, new Contact
             {
                 Email = supplier.Email,
                 Telephone = supplier.Telephone
@@ -99,26 +147,14 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
 
         if (isNew)
         {
-            if (settings.DefaultTaxCode.IsNullOrWhiteSpace())
-            {
-                throw new PostFailedMessageException("Default tax code has not been set up.");
-            }
-            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}"));
-                }
-                myobSupplier.BuyingDetails.TaxCode ??= new();
-                myobSupplier.BuyingDetails.TaxCode.UID = taxID;
-                myobSupplier.BuyingDetails.FreightTaxCode ??= new();
-                myobSupplier.BuyingDetails.FreightTaxCode.UID = taxID;
-            }
-            else
+            if(!MYOBPosterUtils.GetDefaultTaxCode(data, settings).Get(out var taxID, out var error))
             {
-                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));
+                return Result.Error(error);
             }
+            myobSupplier.BuyingDetails.TaxCode ??= new();
+            myobSupplier.BuyingDetails.TaxCode.UID = taxID;
+            myobSupplier.BuyingDetails.FreightTaxCode ??= new();
+            myobSupplier.BuyingDetails.FreightTaxCode.UID = taxID;
         }
         // myobCustomer.BuyingDetails.UseSupplierTaxCode
         // myobCustomer.BuyingDetails.Terms
@@ -140,7 +176,7 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
     /// <param name="data"></param>
     /// <param name="supplier">The supplier to map to.</param>
     /// <returns>The UID of the MYOB supplier.</returns>
-    public static Result<Guid, Exception> MapSupplier(MYOBConnectionData data, Supplier supplier)
+    public static Result<Guid, Exception> MapSupplier(MYOBConnectionData data, Supplier supplier, MYOBGlobalPosterSettings settings)
     {
         if(Guid.TryParse(supplier.PostedReference, out var myobID))
         {
@@ -151,14 +187,14 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
         var result = service.Query(data, new Filter<MYOBSupplier>(x => x.DisplayID).IsEqualTo(supplier.Code), top: 1);
         return result.MapOk(suppliers =>
         {
-            if(suppliers.Count == 0)
+            if(suppliers.Items.Length == 0)
             {
                 if(supplier.Code.Length > 15)
                 {
                     return Result.Error(new Exception("Customer code is longer than 15 characters"));
                 }
                 var myobSupplier = new MYOBSupplier();
-                return UpdateSupplier(data, PosterUtils.LoadPosterSettings<Supplier, SupplierMYOBPosterSettings>(), supplier, myobSupplier, true)
+                return UpdateSupplier(data, settings, supplier, myobSupplier, true)
                     .MapOk(() => service.Save(data, myobSupplier).MapOk(x => x.UID)).Flatten();
             }
             else
@@ -201,7 +237,10 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
             }
             else
             {
-                if(service.Query(ConnectionData, new Filter<MYOBSupplier>(x => x.DisplayID).IsEqualTo(supplier.Code)).Get(out var myobSuppliers, out error))
+                if(service.Query(
+                    ConnectionData,
+                    new Filter<MYOBSupplier>(x => x.DisplayID).IsEqualTo(supplier.Code),
+                    top: 1).Get(out var myobSuppliers, out error))
                 {
                     if(myobSuppliers.Items.Length > 0)
                     {
@@ -220,11 +259,9 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
                     results.AddFailed(supplier, error.Message);
                     continue;
                 }
-                myobSupplier = new MYOBSupplier();
-                isNew = true;
             }
 
-            if(UpdateSupplier(ConnectionData, Settings, supplier, myobSupplier, isNew)
+            if(UpdateSupplier(ConnectionData, GlobalSettings, supplier, myobSupplier, isNew)
                 .MapOk(() => service.Save(ConnectionData, myobSupplier)).Flatten()
                 .Get(out var result, out error))
             {