Browse Source

Improvements to Customer poster

Kenric Nugteren 1 year ago
parent
commit
a40a372cc3

+ 32 - 7
prs.shared/Posters/MYOB/CustomerMYOBPoster.cs

@@ -156,8 +156,8 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
         myobCustomer.IsActive = customer.CustomerStatus.ID == Guid.Empty || customer.CustomerStatus.Active;
         myobCustomer.Addresses =
         [
-            ContactMYOBUtils.ConvertAddress(customer.Delivery, 1, customer.DefaultContact),
-            ContactMYOBUtils.ConvertAddress(customer.Postal, 2, customer.DefaultContact)
+            ContactMYOBUtils.ConvertAddress(customer.Postal, 2, customer.DefaultContact),
+            ContactMYOBUtils.ConvertAddress(customer.Delivery, 1, customer.DefaultContact)
         ];
         // Notes = 
         // PhotoURI =
@@ -185,7 +185,7 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
             {
                 if (taxID == Guid.Empty)
                 {
-                    return Result.Error(new Exception($"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}'"));
                 }
                 myobCustomer.SellingDetails.TaxCode ??= new();
                 myobCustomer.SellingDetails.TaxCode.UID = taxID;
@@ -194,8 +194,8 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
             }
             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));
+                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.Ok();
@@ -239,6 +239,10 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
                             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}");
@@ -286,8 +290,25 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
             }
             else
             {
-                myobCustomer = new MYOBCustomer();
-                isNew = true;
+                if(service.Query(ConnectionData, new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(customer.Code)).Get(out var myobCustomers, out error))
+                {
+                    if(myobCustomers.Items.Length > 0)
+                    {
+                        myobCustomer = myobCustomers.Items[0];
+                        isNew = false;
+                    }
+                    else
+                    {
+                        myobCustomer = new MYOBCustomer();
+                        isNew = true;
+                    }
+                }
+                else
+                {
+                    CoreUtils.LogException("", error);
+                    results.AddFailed(customer, error.Message);
+                    continue;
+                }
             }
 
             if(UpdateCustomer(ConnectionData, Settings, customer, myobCustomer, isNew).Get(out error))
@@ -298,6 +319,10 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
                     customer.PostedReference = result.UID.ToString();
                     results.AddSuccess(customer);
                 }
+                catch(ApiCommunicationException e)
+                {
+                    results.AddFailed(customer, PRSMYOBPosterUtils.FormatApiException(e));
+                }
                 catch(Exception e)
                 {
                     CoreUtils.LogException("", e, $"Error while posting customer {customer.ID}");

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

@@ -1,6 +1,7 @@
 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;
@@ -72,6 +73,10 @@ public static class PRSMYOBPosterUtils
             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);
@@ -101,6 +106,10 @@ public static class PRSMYOBPosterUtils
             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);
@@ -124,7 +133,7 @@ public static class PRSMYOBPosterUtils
         {
             return Result.Error(error);
         }
-        if(items.Count == 0)
+        if(items.Items.Length == 0)
         {
             return Result.Ok(Guid.Empty);
         }
@@ -142,4 +151,20 @@ public static class PRSMYOBPosterUtils
         }
         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;
+        }
+    }
 }