|
@@ -1,6 +1,7 @@
|
|
|
using Comal.Classes;
|
|
|
using FastReport.Data;
|
|
|
using InABox.Core;
|
|
|
+using InABox.Core.Postable;
|
|
|
using InABox.Poster.MYOB;
|
|
|
using MYOB.AccountRight.SDK.Services;
|
|
|
using MYOB.AccountRight.SDK.Services.Contact;
|
|
@@ -13,12 +14,19 @@ using System.Threading.Tasks;
|
|
|
using Customer = Comal.Classes.Customer;
|
|
|
using MYOBCustomer = MYOB.AccountRight.SDK.Contracts.Version2.Contact.Customer;
|
|
|
using MYOBAddress = MYOB.AccountRight.SDK.Contracts.Version2.Contact.Address;
|
|
|
+using MYOB.AccountRight.SDK.Contracts.Version2.Sale;
|
|
|
|
|
|
namespace PRS.Shared.Posters.MYOB;
|
|
|
|
|
|
-public class CustomerMYOBPoster : IMYOBPoster<Customer>
|
|
|
+public class CustomerMYOBPosterSettings : MYOBPosterSettings
|
|
|
{
|
|
|
- public MYOBPosterSettings Settings { get; set; }
|
|
|
+ [TextBoxEditor(ToolTip = "The MYOB tax code which should be used when posting customers")]
|
|
|
+ public string DefaultTaxCode { get; set; }
|
|
|
+}
|
|
|
+
|
|
|
+public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettings>
|
|
|
+{
|
|
|
+ public CustomerMYOBPosterSettings Settings { get; set; }
|
|
|
public MYOBGlobalPosterSettings GlobalSettings { get; set; }
|
|
|
|
|
|
public MYOBConnectionData ConnectionData { get; set; }
|
|
@@ -29,36 +37,38 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer>
|
|
|
firstName = names.Length > 0 ? names[0] : "";
|
|
|
lastName = names.Length > 1 ? names[1] : "";
|
|
|
}
|
|
|
- private static string TruncateString(string value, int maxLength)
|
|
|
- {
|
|
|
- if(value.Length > maxLength)
|
|
|
- {
|
|
|
- return value[..maxLength];
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return value;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
private MYOBAddress ConvertAddress(Address address, int location, IContact contact)
|
|
|
{
|
|
|
return new MYOBAddress
|
|
|
{
|
|
|
Location = location,
|
|
|
- Street = address.Street,
|
|
|
- City = address.City,
|
|
|
- State = address.State,
|
|
|
- PostCode = address.PostCode,
|
|
|
- Phone1 = contact.Mobile,
|
|
|
- Phone2 = contact.Telephone,
|
|
|
- Email = contact.Email,
|
|
|
- ContactName = contact.Name
|
|
|
+ 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)
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ public bool BeforePost(IDataModel<Customer> model)
|
|
|
+ {
|
|
|
+ foreach (var (_, table) in model.ModelTables)
|
|
|
+ {
|
|
|
+ table.IsDefault = false;
|
|
|
+ }
|
|
|
+ model.SetIsDefault<Customer>(true);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
public IPostResult<Customer> Process(IDataModel<Customer> model)
|
|
|
{
|
|
|
+ // Documentation: https://developer.myob.com/api/myob-business-api/v2/contact/customer/
|
|
|
+
|
|
|
var results = new PostResult<Customer>();
|
|
|
|
|
|
var service = new CustomerService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
|
|
@@ -69,17 +79,37 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer>
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- Guid.TryParse(customer.PostedReference, out var myobID);
|
|
|
+ bool isNew;
|
|
|
+ MYOBCustomer myobCustomer;
|
|
|
+ if(Guid.TryParse(customer.PostedReference, out var myobID))
|
|
|
+ {
|
|
|
+ if(!service.Get(ConnectionData, myobID).Get(out var newCustomer, out var error))
|
|
|
+ {
|
|
|
+ CoreUtils.LogException("", error, $"Failed to find Customer in MYOB with id {myobID}");
|
|
|
+ results.AddFailed(customer, $"Failed to find Customer in MYOB with id {myobID}: {error.Message}");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ myobCustomer = newCustomer;
|
|
|
+ isNew = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ myobCustomer = new MYOBCustomer();
|
|
|
+ isNew = true;
|
|
|
+ }
|
|
|
+
|
|
|
SplitName(customer.DefaultContact.Name, out var firstName, out var lastName);
|
|
|
|
|
|
- var myobCustomer = new MYOBCustomer
|
|
|
+ myobCustomer = new MYOBCustomer
|
|
|
{
|
|
|
- UID = myobID,
|
|
|
- CompanyName = customer.Name,
|
|
|
- FirstName = firstName,
|
|
|
- LastName = lastName,
|
|
|
+ CompanyName = customer.Name.Truncate(50),
|
|
|
+ FirstName = firstName.Truncate(30),
|
|
|
+ LastName = lastName.Truncate(20),
|
|
|
IsIndividual = false,
|
|
|
- DisplayID = TruncateString(customer.Code, 15),
|
|
|
+ DisplayID = customer.Code.Truncate(15),
|
|
|
IsActive = customer.CustomerStatus.Active,
|
|
|
Addresses =
|
|
|
[
|
|
@@ -87,15 +117,12 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer>
|
|
|
ConvertAddress(customer.Postal, 2, customer.DefaultContact)
|
|
|
],
|
|
|
// Notes =
|
|
|
- CurrentBalance = (decimal)customer.Balance,
|
|
|
- // PaymentDetails =
|
|
|
// PhotoURI =
|
|
|
// RowVersion =
|
|
|
};
|
|
|
- // myobCustomer.SellingDetails.SaleLayout =
|
|
|
+ myobCustomer.SellingDetails.SaleLayout = InvoiceLayoutType.NoDefault;
|
|
|
// myobCustomer.SellingDetails.PrintedFOrm =
|
|
|
- // myobCustomer.SellingDetails.InvoiceDelivery =
|
|
|
- // myobCustomer.SellingDetails.ItemPriceLevel =
|
|
|
+ myobCustomer.SellingDetails.InvoiceDelivery = DocumentAction.PrintAndEmail;
|
|
|
// myobCustomer.SellingDetails.IncomeAccount =
|
|
|
// myobCustomer.SellingDetails.ReceiptMemo =
|
|
|
// myobCustomer.SellingDetails.SalesPerson =
|
|
@@ -103,14 +130,47 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer>
|
|
|
// myobCustomer.SellingDetails.ShippingMethod =
|
|
|
// myobCustomer.SellingDetails.HourlyBillRate =
|
|
|
// myobCustomer.SellingDetails.ABNBranch =
|
|
|
- myobCustomer.SellingDetails.ABN = customer.ABN;
|
|
|
- // myobCustomer.SellingDetails.TaxCode =
|
|
|
- // myobCustomer.SellingDetails.FreightTaxCode =
|
|
|
+ myobCustomer.SellingDetails.ABN = customer.ABN.Truncate(14);
|
|
|
+
|
|
|
+ if (isNew)
|
|
|
+ {
|
|
|
+ if (Settings.DefaultTaxCode.IsNullOrWhiteSpace())
|
|
|
+ {
|
|
|
+ throw new PostFailedMessageException("Default tax code has not been set up.");
|
|
|
+ }
|
|
|
+ else if(ConnectionData.GetMYOBTaxCodeUID(Settings.DefaultTaxCode).Get(out var taxID, out var error))
|
|
|
+ {
|
|
|
+ if (taxID.HasValue)
|
|
|
+ {
|
|
|
+ myobCustomer.SellingDetails.TaxCode.UID = taxID.Value;
|
|
|
+ myobCustomer.SellingDetails.FreightTaxCode.UID = taxID.Value;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ results.AddFailed(customer, $"Failed to find TaxCode in MYOB with code {Settings.DefaultTaxCode}");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {Settings.DefaultTaxCode}");
|
|
|
+ results.AddFailed(customer, $"Failed to find TaxCode in MYOB with code {Settings.DefaultTaxCode}: {error.Message}");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// myobCustomer.SellingDetails.UseCustomerTaxCode =
|
|
|
// myobCustomer.SellingDetails.Terms =
|
|
|
// myobCustomer.SellingDetails.Credit =
|
|
|
// myobCustomer.SellingDetails.TaxIdNumber =
|
|
|
// myobCustomer.SellingDetails.Memo =
|
|
|
+ // myboCustomer.PaymentDetails.Method =
|
|
|
+ // myboCustomer.PaymentDetails.CardNumber =
|
|
|
+ // myboCustomer.PaymentDetails.NameOnCard =
|
|
|
+ // myboCustomer.PaymentDetails.BSBNumber =
|
|
|
+ // myboCustomer.PaymentDetails.BankAccountNumber =
|
|
|
+ // myboCustomer.PaymentDetails.BankAccountName =
|
|
|
+ // myboCustomer.PaymentDetails.Notes =
|
|
|
|
|
|
var result = service.Update(ConnectionData.CompanyFile, myobCustomer, ConnectionData.CompanyFileCredentials);
|
|
|
results.AddSuccess(customer);
|
|
@@ -125,3 +185,5 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer>
|
|
|
return results;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+public class CustomerMYOBPosterEngine : MYOBPosterEngine<Customer, CustomerMYOBPosterSettings> { }
|