Преглед изворни кода

Fixes to various MYOB posters

Kenric Nugteren пре 1 година
родитељ
комит
381865662a

+ 12 - 9
prs.shared/Posters/MYOB/BillMYOBPoster.cs

@@ -94,9 +94,10 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
         foreach(var bill in bills)
         {
             MYOBBill myobBill;
+            Exception? error;
             if(Guid.TryParse(bill.PostedReference, out var myobID))
             {
-                if(!service.Get(ConnectionData, myobID).Get(out var newBill, out var error))
+                if(!service.Get(ConnectionData, myobID).Get(out var newBill, out error))
                 {
                     CoreUtils.LogException("", error, $"Failed to find Bill in MYOB with id {myobID}");
                     results.AddFailed(bill, $"Failed to find Bill in MYOB with id {myobID}: {error.Message}");
@@ -117,12 +118,13 @@ 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 var error))
+                if(!SupplierMYOBPoster.MapSupplier(ConnectionData, supplier).Get(out var supplierID, out error))
                 {
                     CoreUtils.LogException("", error, $"Error while posting bill {bill.ID}");
                     results.AddFailed(bill, error.Message);
                     continue;
                 }
+                myobBill.Supplier ??= new();
                 myobBill.Supplier.UID = supplierID;
             }
 
@@ -161,7 +163,7 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                     }
                     if(!Guid.TryParse(billLine.PurchaseGL.PostedReference, out var accountID))
                     {
-                        if(AccountMYOBUtils.GetAccount(ConnectionData, billLine.PurchaseGL.Code).Get(out accountID, out var error))
+                        if(AccountMYOBUtils.GetAccount(ConnectionData, billLine.PurchaseGL.Code).Get(out accountID, out error))
                         {
                             results.AddFragment(new GLCode { ID = billLine.PurchaseGL.ID, PostedReference = accountID.ToString() });
                         }
@@ -172,6 +174,7 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                             break;
                         }
                     }
+                    line.Account ??= new();
                     line.Account.UID = accountID;
                     line.Total = (decimal)billLine.IncTax;
                     line.TotalForeign = 0;
@@ -191,7 +194,7 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                     {
                         if (!ConnectionData.GetUID<TaxCodeService, MYOBTaxCode>(
                                 new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(billLine.TaxCode.Code))
-                            .Get(out taxCodeID, out var error))
+                            .Get(out taxCodeID, out error))
                         {
                             CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}");
                             failed = $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}: {error.Message}";
@@ -204,6 +207,7 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                         }
                         results.AddFragment(new TaxCode { ID = taxCodeID, PostedReference = taxCodeID.ToString() });
                     }
+                    line.TaxCode ??= new();
                     line.TaxCode.UID = taxCodeID;
 
                     newLines[i] = line;
@@ -221,16 +225,15 @@ public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
                 myobBill.Lines = [];
             }
 
-            try
+            if(service.Save(ConnectionData, myobBill).Get(out var result, out error))
             {
-                var result = service.UpdateEx(ConnectionData.CompanyFile, myobBill, ConnectionData.CompanyFileCredentials);
                 bill.PostedReference = result.UID.ToString();
                 results.AddSuccess(bill);
             }
-            catch(Exception e)
+            else
             {
-                CoreUtils.LogException("", e, $"Error while posting receipt {bill.ID}");
-                results.AddFailed(bill, e.Message);
+                CoreUtils.LogException("", error, $"Error while posting receipt {bill.ID}");
+                results.AddFailed(bill, error.Message);
             }
         }
 

+ 5 - 16
prs.shared/Posters/MYOB/CustomerMYOBPoster.cs

@@ -311,23 +311,12 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
                 }
             }
 
-            if(UpdateCustomer(ConnectionData, Settings, customer, myobCustomer, isNew).Get(out error))
+            if(UpdateCustomer(ConnectionData, Settings, customer, myobCustomer, isNew)
+                .MapOk(() => service.Save(ConnectionData, myobCustomer)).Flatten()
+                .Get(out var result, out error))
             {
-                try
-                {
-                    var result = service.Save(ConnectionData, myobCustomer);
-                    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}");
-                    results.AddFailed(customer, e.Message);
-                }
+                customer.PostedReference = result.UID.ToString();
+                results.AddSuccess(customer);
             }
             else
             {

+ 12 - 9
prs.shared/Posters/MYOB/InvoiceMYOBPoster.cs

@@ -94,9 +94,10 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
         foreach(var invoice in invoices)
         {
             MYOBInvoice myobInvoice;
+            Exception? error;
             if(Guid.TryParse(invoice.PostedReference, out var myobID))
             {
-                if(!service.Get(ConnectionData, myobID).Get(out var newInvoice, out var error))
+                if(!service.Get(ConnectionData, myobID).Get(out var newInvoice, out error))
                 {
                     CoreUtils.LogException("", error, $"Failed to find Invoice in MYOB with id {myobID}");
                     results.AddFailed(invoice, $"Failed to find Invoice in MYOB with id {myobID}: {error.Message}");
@@ -115,12 +116,13 @@ 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 var error))
+                if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer).Get(out var customerID, out error))
                 {
                     CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
                     results.AddFailed(invoice, error.Message);
                     continue;
                 }
+                myobInvoice.Customer ??= new();
                 myobInvoice.Customer.UID = customerID;
             }
             // myobInvoice.PromisedDate = 
@@ -151,7 +153,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
                     }
                     if(!Guid.TryParse(item.SellGL.PostedReference, out var accountID))
                     {
-                        if(AccountMYOBUtils.GetAccount(ConnectionData, item.SellGL.Code).Get(out accountID, out var error))
+                        if(AccountMYOBUtils.GetAccount(ConnectionData, item.SellGL.Code).Get(out accountID, out error))
                         {
                             results.AddFragment(new GLCode { ID = item.SellGL.ID, PostedReference = accountID.ToString() });
                         }
@@ -162,6 +164,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
                             break;
                         }
                     }
+                    line.Account ??= new();
                     line.Account.UID = accountID;
 
                     if(item.TaxCode.ID == Guid.Empty)
@@ -173,7 +176,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
                     {
                         if (!ConnectionData.GetUID<TaxCodeService, MYOBTaxCode>(
                                 new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(item.TaxCode.Code))
-                            .Get(out taxCodeID, out var error))
+                            .Get(out taxCodeID, out error))
                         {
                             CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}");
                             failed = $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}: {error.Message}";
@@ -186,6 +189,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
                         }
                         results.AddFragment(new TaxCode { ID = taxCodeID, PostedReference = taxCodeID.ToString() });
                     }
+                    line.TaxCode ??= new();
                     line.TaxCode.UID = taxCodeID;
 
                     newLines[i] = line;
@@ -219,16 +223,15 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
             // Order
             // OnlinePaymentMethod
 
-            try
+            if(service.Save(ConnectionData, myobInvoice).Get(out var result, out error))
             {
-                var result = service.UpdateEx(ConnectionData.CompanyFile, myobInvoice, ConnectionData.CompanyFileCredentials);
                 invoice.PostedReference = result.UID.ToString();
                 results.AddSuccess(invoice);
             }
-            catch (Exception e)
+            else
             {
-                CoreUtils.LogException("", e, $"Error while posting invoice {invoice.ID}");
-                results.AddFailed(invoice, e.Message);
+                CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
+                results.AddFailed(invoice, error.Message);
             }
         }
 

+ 16 - 5
prs.shared/Posters/MYOB/PRSMYOBPosterUtils.cs

@@ -83,18 +83,29 @@ public static class PRSMYOBPosterUtils
         }
     }
 
-    public static T Save<T>(
+    public static Result<T, Exception> Save<T>(
         this MutableService<T> service, MYOBConnectionData data,
         T entity)
         where T : MYOBBaseEntity
     {
-        if(entity.UID == Guid.Empty)
+        try
         {
-            return service.InsertEx(data.CompanyFile, entity, data.CompanyFileCredentials);
+            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));
+            }
         }
-        else
+        catch(ApiCommunicationException e)
+        {
+            return Result.Error(new Exception(FormatApiException(e), e));
+        }
+        catch(Exception e)
         {
-            return service.UpdateEx(data.CompanyFile, entity, data.CompanyFileCredentials);
+            return Result.Error(e);
         }
     }
 

+ 4 - 3
prs.shared/Posters/MYOB/ReceiptMYOBPoster.cs

@@ -98,6 +98,7 @@ public class ReceiptMYOBPoster : IMYOBPoster<Receipt, ReceiptMYOBPosterSettings>
             myobReceipt.DepositTo = DepositTo.UndepositedFunds;
 
             // Setting this to null right now.
+            myobReceipt.Account ??= new();
             myobReceipt.Account.UID = Guid.Empty;
             // Account =
 
@@ -109,6 +110,7 @@ public class ReceiptMYOBPoster : IMYOBPoster<Receipt, ReceiptMYOBPosterSettings>
                     results.AddFailed(receipt, error.Message);
                     continue;
                 }
+                myobReceipt.Customer ??= new();
                 myobReceipt.Customer.UID = customerID;
             }
 
@@ -158,13 +160,12 @@ public class ReceiptMYOBPoster : IMYOBPoster<Receipt, ReceiptMYOBPosterSettings>
                 myobReceipt.Invoices = [];
             }
 
-            try
+            if(service.Save(ConnectionData, myobReceipt).Get(out var result, out var e))
             {
-                var result = service.UpdateEx(ConnectionData.CompanyFile, myobReceipt, ConnectionData.CompanyFileCredentials);
                 receipt.PostedReference = result.UID.ToString();
                 results.AddSuccess(receipt);
             }
-            catch(Exception e)
+            else
             {
                 CoreUtils.LogException("", e, $"Error while posting receipt {receipt.ID}");
                 results.AddFailed(receipt, e.Message);

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

@@ -81,6 +81,7 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
         // Notes = 
         // PhotoURI =
         // RowVersion = 
+        myobSupplier.BuyingDetails ??= new();
         // myobSupplier.BuyingDetails.PurchaseLayout = 
         // myobCustomer.BuyingDetails.PrintedForm = 
         // myobSupplier.BuyingDetails.PurchaseOrderDelivery = 
@@ -108,7 +109,9 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
                 {
                     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
@@ -156,20 +159,7 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
                 }
                 var myobSupplier = new MYOBSupplier();
                 return UpdateSupplier(data, PosterUtils.LoadPosterSettings<Supplier, SupplierMYOBPosterSettings>(), supplier, myobSupplier, true)
-                    .MapOk<Result<Guid, Exception>>(() =>
-                    {
-                        try
-                        {
-                            var result = service.UpdateEx(data.CompanyFile, myobSupplier, data.CompanyFileCredentials);
-                            supplier.PostedReference = result.UID.ToString();
-                            return Result.Ok(result.UID);
-                        }
-                        catch (Exception e)
-                        {
-                            CoreUtils.LogException("", e, $"Error while posting supplier {supplier.ID}");
-                            return Result.Error(e);
-                        }
-                    }).Flatten();
+                    .MapOk(() => service.Save(data, myobSupplier).MapOk(x => x.UID)).Flatten();
             }
             else
             {
@@ -211,26 +201,39 @@ 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(myobSuppliers.Items.Length > 0)
+                    {
+                        myobSupplier = myobSuppliers.Items[0];
+                        isNew = false;
+                    }
+                    else
+                    {
+                        myobSupplier = new MYOBSupplier();
+                        isNew = true;
+                    }
+                }
+                else
+                {
+                    CoreUtils.LogException("", error);
+                    results.AddFailed(supplier, error.Message);
+                    continue;
+                }
                 myobSupplier = new MYOBSupplier();
                 isNew = true;
             }
 
-            if(UpdateSupplier(ConnectionData, Settings, supplier, myobSupplier, isNew).Get(out error))
+            if(UpdateSupplier(ConnectionData, Settings, supplier, myobSupplier, isNew)
+                .MapOk(() => service.Save(ConnectionData, myobSupplier)).Flatten()
+                .Get(out var result, out error))
             {
-                try
-                {
-                    var result = service.UpdateEx(ConnectionData.CompanyFile, myobSupplier, ConnectionData.CompanyFileCredentials);
-                    supplier.PostedReference = result.UID.ToString();
-                    results.AddSuccess(supplier);
-                }
-                catch(Exception e)
-                {
-                    CoreUtils.LogException("", e, $"Error while posting supplier {supplier.ID}");
-                    results.AddFailed(supplier, e.Message);
-                }
+                supplier.PostedReference = result.UID.ToString();
+                results.AddSuccess(supplier);
             }
             else
             {
+                CoreUtils.LogException("", error, $"Error while posting supplier {supplier.ID}");
                 results.AddFailed(supplier, error.Message);
             }
         }