|
@@ -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))
|
|
|
{
|