|
@@ -0,0 +1,240 @@
|
|
|
+using Comal.Classes;
|
|
|
+using InABox.Clients;
|
|
|
+using InABox.Core;
|
|
|
+using InABox.Core.Postable;
|
|
|
+using InABox.Poster.MYOB;
|
|
|
+using MYOB.AccountRight.SDK.Services.Contact;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using MYOBSupplier = MYOB.AccountRight.SDK.Contracts.Version2.Contact.Supplier;
|
|
|
+
|
|
|
+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 MYOBConnectionData ConnectionData { get; set; }
|
|
|
+ public SupplierMYOBPosterSettings Settings { get; set; }
|
|
|
+ public MYOBGlobalPosterSettings GlobalSettings { get; set; }
|
|
|
+
|
|
|
+ public bool BeforePost(IDataModel<Supplier> model)
|
|
|
+ {
|
|
|
+ foreach (var (_, table) in model.ModelTables)
|
|
|
+ {
|
|
|
+ table.IsDefault = false;
|
|
|
+ }
|
|
|
+ model.SetIsDefault<Supplier>(true);
|
|
|
+ model.SetColumns<Supplier>(RequiredColumns());
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ public static Columns<Supplier> RequiredColumns()
|
|
|
+ {
|
|
|
+ return Columns.None<Supplier>()
|
|
|
+ .Add(x => x.ID)
|
|
|
+ .Add(x => x.PostedReference)
|
|
|
+ .Add(x => x.Name)
|
|
|
+ .Add(x => x.Code)
|
|
|
+ .Add(x => x.SupplierStatus.ID)
|
|
|
+ .Add(x => x.SupplierStatus.Active)
|
|
|
+ .Add(x => x.Postal.Street)
|
|
|
+ .Add(x => x.Postal.City)
|
|
|
+ .Add(x => x.Postal.State)
|
|
|
+ .Add(x => x.Postal.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)
|
|
|
+ {
|
|
|
+ // Documentation: https://developer.myob.com/api/myob-business-api/v2/contact/supplier/
|
|
|
+
|
|
|
+ // Since this might be called from some other poster, we need to ensure we have the right columns.
|
|
|
+ Client.EnsureColumns(supplier, RequiredColumns());
|
|
|
+
|
|
|
+ // ContactMYOBUtils.SplitName(supplier.DefaultContact.Name, out var firstName, out var lastName);
|
|
|
+
|
|
|
+ myobSupplier.CompanyName = supplier.Name.Truncate(50);
|
|
|
+ // myobSupplier.FirstName =
|
|
|
+ // myobSupplier.LastName =
|
|
|
+ myobSupplier.IsIndividual = false;
|
|
|
+ myobSupplier.DisplayID = supplier.Code.Truncate(15);
|
|
|
+ // If there is not customer status, we will use default to Active = true.
|
|
|
+ myobSupplier.IsActive = supplier.SupplierStatus.ID == Guid.Empty || supplier.SupplierStatus.Active;
|
|
|
+ myobSupplier.Addresses =
|
|
|
+ [
|
|
|
+ ContactMYOBUtils.ConvertAddress(supplier.Postal, 1, new Contact
|
|
|
+ {
|
|
|
+ Email = supplier.Email,
|
|
|
+ Telephone = supplier.Telephone
|
|
|
+ })
|
|
|
+ ];
|
|
|
+ // Notes =
|
|
|
+ // PhotoURI =
|
|
|
+ // RowVersion =
|
|
|
+ // myobSupplier.BuyingDetails.PurchaseLayout =
|
|
|
+ // myobCustomer.BuyingDetails.PrintedForm =
|
|
|
+ // myobSupplier.BuyingDetails.PurchaseOrderDelivery =
|
|
|
+ // myobCustomer.BuyingDetails.ExpenseAccount.UID =
|
|
|
+ // myobCustomer.BuyingDetails.PaymentMemo =
|
|
|
+ // myobCustomer.BuyingDetails.PurchaseComment =
|
|
|
+ // myobCustomer.BuyingDetails.SupplierBillingRate =
|
|
|
+ // myobCustomer.BuyingDetails.ShippingMethod =
|
|
|
+ // myobCustomer.BuyingDetails.IsReportable =
|
|
|
+ // myobCustomer.BuyingDetails.CostPerHour =
|
|
|
+ // myobCustomer.BuyingDetails.Credit.Limit =
|
|
|
+ myobSupplier.BuyingDetails.ABN = supplier.ABN.Truncate(14);
|
|
|
+ // myobCustomer.BuyingDetails.ABNBranch
|
|
|
+ // myobCustomer.BuyingDetails.TaxIdNumber
|
|
|
+
|
|
|
+ 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.UID = taxID;
|
|
|
+ myobSupplier.BuyingDetails.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));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // myobCustomer.BuyingDetails.UseSupplierTaxCode
|
|
|
+ // myobCustomer.BuyingDetails.Terms
|
|
|
+
|
|
|
+ // myobCustomer.PaymentDetails
|
|
|
+ // myobCustomer.PhotoURI
|
|
|
+
|
|
|
+ return Result.Ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Try to find a supplier in MYOB which matches <paramref name="supplier"/>, and if this fails, create a new one.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// After this has finished, <paramref name="supplier"/> will be updated with <see cref="Supplier.PostedReference"/> set to the correct ID.
|
|
|
+ /// <br/>
|
|
|
+ /// <paramref name="supplier"/> needs to have at least <see cref="Supplier.Code"/> and <see cref="Supplier.PostedReference"/> as loaded columns.
|
|
|
+ /// </remarks>
|
|
|
+ /// <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)
|
|
|
+ {
|
|
|
+ if(Guid.TryParse(supplier.PostedReference, out var myobID))
|
|
|
+ {
|
|
|
+ return Result.Ok(myobID);
|
|
|
+ }
|
|
|
+
|
|
|
+ var service = new SupplierService(data.Configuration, null, data.AuthKey);
|
|
|
+ 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(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)
|
|
|
+ .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();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ supplier.PostedReference = suppliers.Items[0].UID.ToString();
|
|
|
+ return Result.Ok(suppliers.Items[0].UID);
|
|
|
+ }
|
|
|
+ }).Flatten();
|
|
|
+ }
|
|
|
+
|
|
|
+ public IPostResult<Supplier> Process(IDataModel<Supplier> model)
|
|
|
+ {
|
|
|
+ var results = new PostResult<Supplier>();
|
|
|
+
|
|
|
+ var service = new SupplierService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
|
|
|
+
|
|
|
+ var suppliers = model.GetTable<Supplier>().ToArray<Supplier>();
|
|
|
+
|
|
|
+ foreach(var supplier in suppliers)
|
|
|
+ {
|
|
|
+ if(supplier.Code.Length > 15)
|
|
|
+ {
|
|
|
+ results.AddFailed(supplier, "Code is longer than 15 characters.");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool isNew;
|
|
|
+ MYOBSupplier myobSupplier;
|
|
|
+ Exception? error;
|
|
|
+ if(Guid.TryParse(supplier.PostedReference, out var myobID))
|
|
|
+ {
|
|
|
+ if(!service.Get(ConnectionData, myobID).Get(out var newSupplier, out error))
|
|
|
+ {
|
|
|
+ CoreUtils.LogException("", error, $"Failed to find Supplier in MYOB with id {myobID}");
|
|
|
+ results.AddFailed(supplier, $"Failed to find Supplier in MYOB with id {myobID}: {error.Message}");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ myobSupplier = newSupplier;
|
|
|
+ isNew = false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ myobSupplier = new MYOBSupplier();
|
|
|
+ isNew = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(UpdateSupplier(ConnectionData, Settings, supplier, myobSupplier, isNew).Get(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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ results.AddFailed(supplier, error.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+}
|