123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- 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;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- 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;
- using InABox.Clients;
- using InABox.Database;
- using MYOB.AccountRight.SDK;
- using InABox.Scripting;
- using Org.BouncyCastle.Bcpg.OpenPgp;
- namespace PRS.Shared.Posters.MYOB;
- public class CustomerMYOBPosterSettings : MYOBPosterSettings
- {
- public override string DefaultScript(Type TPostable)
- {
- return @"using MYOBCustomer = MYOB.AccountRight.SDK.Contracts.Version2.Contact.Customer;
- public class Module
- {
- public void BeforePost(IDataModel<Customer> model)
- {
- // Perform pre-processing
- }
- public void ProcessCustomer(IDataModel<Customer> model, Customer customer, MYOBCustomer myobCustomer)
- {
- // Do extra processing for a customer; throw an exception to fail this customer.
- }
- }";
- }
- }
- public static class ContactMYOBUtils
- {
- public static void SplitName(string name, out string firstName, out string lastName)
- {
- var names = name.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
- firstName = names.Length > 0 ? names[0] : "";
- lastName = names.Length > 1 ? names[1] : "";
- }
- public static Address ConvertAddress(MYOBAddress address)
- {
- var newAddress = new Address
- {
- Street = address.Street,
- City = address.City,
- State = address.State,
- PostCode = address.PostCode
- };
- return newAddress;
- }
- public static MYOBAddress ConvertAddress(Address address, int location, IContact contact)
- {
- var mobile = contact.Mobile.Truncate(21);
- var phone = contact.Telephone.Truncate(21);
- var newAddress = new MYOBAddress
- {
- Location = location,
- Street = address.Street.Truncate(255),
- City = address.City.Truncate(255),
- State = address.State.Truncate(255),
- PostCode = address.PostCode.Truncate(11),
- Email = contact.Email.Truncate(255),
- ContactName = contact.Name.Truncate(25)
- };
- if (mobile.IsNullOrWhiteSpace())
- {
- newAddress.Phone1 = phone;
- }
- else
- {
- newAddress.Phone1 = mobile;
- newAddress.Phone2 = phone;
- }
- return newAddress;
- }
- }
- public class CustomerMYOBAutoRefresher : IAutoRefresher<Customer>
- {
- public bool ShouldRepost(Customer customer)
- {
- var shouldRepost = customer.HasOriginalValue(x => x.Name)
- || customer.HasOriginalValue(x => x.Code)
- || customer.HasOriginalValue(x => x.ABN)
- || customer.DefaultContact.HasOriginalValue(x => x.ID)
- || customer.Delivery.HasOriginalValue(x => x.Street)
- || customer.Delivery.HasOriginalValue(x => x.City)
- || customer.Delivery.HasOriginalValue(x => x.State)
- || customer.Delivery.HasOriginalValue(x => x.PostCode)
- || customer.Postal.HasOriginalValue(x => x.Street)
- || customer.Postal.HasOriginalValue(x => x.City)
- || customer.Postal.HasOriginalValue(x => x.State)
- || customer.Postal.HasOriginalValue(x => x.PostCode);
- if (shouldRepost)
- {
- return true;
- }
- if(customer.CustomerStatus.HasOriginalValue(x => x.ID))
- {
- var originalID = customer.CustomerStatus.GetOriginalValue(x => x.ID);
- var currentID = customer.CustomerStatus.ID;
- var statuses = DbFactory.NewProvider(Logger.Main).Query<CustomerStatus>(
- new Filter<CustomerStatus>(x => x.ID).IsEqualTo(originalID).Or(x => x.ID).IsEqualTo(currentID),
- Columns.None<CustomerStatus>().Add(x => x.ID).Add(x => x.Active))
- .ToArray<CustomerStatus>();
- if (statuses.Length == 2 && statuses[0].Active != statuses[1].Active)
- {
- return true;
- }
- }
- return false;
- }
- }
- public class CustomerMYOBPoster :
- IMYOBPoster<Customer, CustomerMYOBPosterSettings>,
- IAutoRefreshPoster<Customer, CustomerMYOBAutoRefresher>
- {
- public ScriptDocument? Script { get; set; }
- public CustomerMYOBPosterSettings Settings { get; set; }
- public MYOBGlobalPosterSettings GlobalSettings { get; set; }
- public MYOBConnectionData ConnectionData { get; set; }
- public bool BeforePost(IDataModel<Customer> model)
- {
- foreach (var (_, table) in model.ModelTables)
- {
- table.IsDefault = false;
- }
- model.SetIsDefault<Customer>(true);
- model.SetColumns<Customer>(RequiredColumns());
- Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
- return true;
- }
- public static Columns<Customer> RequiredColumns()
- {
- return Columns.None<Customer>()
- .Add(x => x.ID)
- .Add(x => x.PostedReference)
- .Add(x => x.PostedStatus)
- .Add(x => x.DefaultContact.Name)
- .Add(x => x.Name)
- .Add(x => x.Code)
- .Add(x => x.CustomerStatus.ID)
- .Add(x => x.CustomerStatus.Active)
- .Add(x => x.Delivery.Street)
- .Add(x => x.Delivery.City)
- .Add(x => x.Delivery.State)
- .Add(x => x.Delivery.PostCode)
- .Add(x => x.Postal.Street)
- .Add(x => x.Postal.City)
- .Add(x => x.Postal.State)
- .Add(x => x.Postal.PostCode)
- .Add(x => x.DefaultContact.Mobile)
- .Add(x => x.DefaultContact.Telephone)
- .Add(x => x.DefaultContact.Email)
- .Add(x => x.DefaultContact.Name)
- .Add(x => x.DefaultContact.Mobile)
- .Add(x => x.ABN);
- }
- #region Script Functions
- private Result<Exception> ProcessCustomer(IDataModel<Customer> model, Customer customer, MYOBCustomer myobCustomer)
- {
- return this.WrapScript("ProcessCustomer", model, customer, myobCustomer);
- }
- #endregion
- public static Result<Exception> UpdateCustomer(MYOBConnectionData data, MYOBGlobalPosterSettings settings, Customer customer, MYOBCustomer myobCustomer, bool isNew)
- {
- // Documentation: https://developer.myob.com/api/myob-business-api/v2/contact/customer/
- // Since this might be called from some other poster, we need to ensure we have the right columns.
- Client.EnsureColumns(customer, RequiredColumns());
- ContactMYOBUtils.SplitName(customer.DefaultContact.Name, out var firstName, out var lastName);
- myobCustomer.CompanyName = customer.Name.Truncate(50);
- myobCustomer.FirstName = firstName.Truncate(30);
- myobCustomer.LastName = lastName.Truncate(20);
- myobCustomer.IsIndividual = false;
- myobCustomer.DisplayID = customer.Code.Truncate(15);
- // If there is not customer status, we will use default to Active = true.
- myobCustomer.IsActive = customer.CustomerStatus.ID == Guid.Empty || customer.CustomerStatus.Active;
- myobCustomer.Addresses =
- [
- ContactMYOBUtils.ConvertAddress(customer.Postal, 1, customer.DefaultContact),
- ContactMYOBUtils.ConvertAddress(customer.Delivery, 2, customer.DefaultContact)
- ];
- // Notes =
- // PhotoURI =
- // RowVersion =
- myobCustomer.SellingDetails ??= new();
- myobCustomer.SellingDetails.SaleLayout = InvoiceLayoutType.NoDefault;
- // myobCustomer.SellingDetails.PrintedFOrm =
- myobCustomer.SellingDetails.InvoiceDelivery = DocumentAction.PrintAndEmail;
- // myobCustomer.SellingDetails.IncomeAccount =
- // myobCustomer.SellingDetails.ReceiptMemo =
- // myobCustomer.SellingDetails.SalesPerson =
- // myobCustomer.SellingDetails.SaleComment =
- // myobCustomer.SellingDetails.ShippingMethod =
- // myobCustomer.SellingDetails.HourlyBillRate =
- // myobCustomer.SellingDetails.ABNBranch =
- myobCustomer.SellingDetails.ABN = customer.ABN.Truncate(14);
- if (isNew)
- {
- if(!MYOBPosterUtils.GetDefaultTaxCode(data, settings).Get(out var taxID, out var error))
- {
- return Result.Error(error);
- }
- myobCustomer.SellingDetails.TaxCode ??= new();
- myobCustomer.SellingDetails.TaxCode.UID = taxID;
- myobCustomer.SellingDetails.FreightTaxCode ??= new();
- myobCustomer.SellingDetails.FreightTaxCode.UID = taxID;
- }
- return Result.Ok();
- }
- /// <summary>
- /// Try to find a customer in MYOB which matches <paramref name="customer"/>, and if this fails, create a new one.
- /// </summary>
- /// <remarks>
- /// After this has finished, <paramref name="customer"/> will be updated with <see cref="Customer.PostedReference"/> set to the correct ID.
- /// <br/>
- /// <paramref name="customer"/> needs to have at least <see cref="Customer.Code"/> and <see cref="Customer.PostedReference"/> as loaded columns.
- /// </remarks>
- /// <param name="data"></param>
- /// <param name="customer">The customer to map to.</param>
- /// <returns>The UID of the MYOB customer.</returns>
- public static Result<Guid, Exception> MapCustomer(MYOBConnectionData data, Customer customer, MYOBGlobalPosterSettings settings)
- {
- if(Guid.TryParse(customer.PostedReference, out var myobID))
- {
- return new Result<Guid, Exception>(myobID);
- }
- var service = new CustomerService(data.Configuration, null, data.AuthKey);
- var result = service.Query(data, new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(customer.Code), top: 1);
- return result.MapOk(customers =>
- {
- if(customers.Items.Length == 0)
- {
- if(customer.Code.Length > 15)
- {
- return Result.Error(new Exception("Customer code is longer than 15 characters"));
- }
- var myobCustomer = new MYOBCustomer();
- return UpdateCustomer(data, settings, customer, myobCustomer, true)
- .MapOk(() => service.Save(data, myobCustomer)
- .MapOk(x =>
- {
- // Marking as repost because a script may not have run.
- customer.PostedStatus = PostedStatus.RequiresRepost;
- customer.PostedReference = x.UID.ToString();
- return x.UID;
- }))
- .Flatten();
- }
- else
- {
- customer.PostedReference = customers.Items[0].UID.ToString();
- customer.PostedStatus = PostedStatus.RequiresRepost;
- return Result.Ok(customers.Items[0].UID);
- }
- }).Flatten();
- }
- private static bool IsBlankCode(string code)
- {
- return code.IsNullOrWhiteSpace() || code.Equals("*None");
- }
- public IPullResult<Customer> Pull()
- {
- var result = new PullResult<Customer>();
- var top = 400;
- var skip = 0;
- var customerCodes = new HashSet<string>();
- var service = new CustomerService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
- while (true)
- {
- if(!service.Query(ConnectionData, null, top: top, skip: skip).Get(out var myobCustomers, out var error))
- {
- CoreUtils.LogException("", error);
- throw new PullFailedMessageException(error.Message);
- }
- if(myobCustomers.Items.Length == 0)
- {
- break;
- }
- var myobIDs = myobCustomers.Items.ToArray(x => x.UID.ToString());
- var myobCodes = myobCustomers.Items.Select(x => x.DisplayID).Where(x => !IsBlankCode(x)).ToArray();
- var myobNames = myobCustomers.Items.Where(x => IsBlankCode(x.DisplayID) && !x.CompanyName.IsNullOrWhiteSpace())
- .Select(x => x.CompanyName).ToArray();
- var customers = Client.Query(
- new Filter<Customer>(x => x.PostedReference).InList(myobIDs)
- .Or(x => x.Code).InList(myobCodes)
- .Or(x => x.Name).InList(myobNames),
- Columns.None<Customer>().Add(x => x.ID).Add(x => x.PostedReference).Add(x => x.Code).Add(x => x.Name))
- .ToArray<Customer>();
- var customerDict = customers.Where(x => !x.PostedReference.IsNullOrWhiteSpace())
- .ToDictionary(x => x.PostedReference);
- var blankCustomers = customers.Where(x => x.PostedReference.IsNullOrWhiteSpace()).ToArray();
- var needCodes = new Dictionary<string, (string prefix, int i, Customer customer)>();
- foreach(var myobCustomer in myobCustomers.Items)
- {
- if (customerDict.TryGetValue(myobCustomer.UID.ToString(), out var customer))
- {
- // Skipping existing customers at this point.
- continue;
- }
- customer = !IsBlankCode(myobCustomer.DisplayID)
- ? blankCustomers.FirstOrDefault(x => string.Equals(x.Code, myobCustomer.DisplayID))
- : blankCustomers.FirstOrDefault(x => string.Equals(x.Name, myobCustomer.CompanyName));
- if(customer is not null)
- {
- customer.PostedReference = myobCustomer.UID.ToString();
- result.AddEntity(PullResultType.Linked, customer);
- continue;
- }
- customer = new Customer();
- string code;
- if (!IsBlankCode(myobCustomer.DisplayID))
- {
- code = myobCustomer.DisplayID.ToString();
- }
- else if (!myobCustomer.CompanyName.IsNullOrWhiteSpace())
- {
- code = myobCustomer.CompanyName[..Math.Min(3, myobCustomer.CompanyName.Length)].ToUpper();
- }
- else
- {
- code = "CUS";
- }
- int i = 1;
- customer.Code = code;
- while (customerCodes.Contains(customer.Code))
- {
- customer.Code = $"{code}{i:d3}";
- ++i;
- }
- customerCodes.Add(customer.Code);
- customer.Name = myobCustomer.CompanyName;
- customer.ABN = myobCustomer.SellingDetails.ABN;
- if(myobCustomer.Addresses is not null)
- {
- var delivery = myobCustomer.Addresses.FirstOrDefault(x => x.Location == 2);
- if(delivery is not null)
- {
- customer.Delivery.CopyFrom(ContactMYOBUtils.ConvertAddress(delivery));
- }
- var postal = myobCustomer.Addresses.FirstOrDefault(x => x.Location == 1);
- if(postal is not null)
- {
- customer.Postal.CopyFrom(ContactMYOBUtils.ConvertAddress(postal));
- }
- customer.Email = delivery?.Email ?? postal?.Email ?? "";
- }
- customer.PostedReference = myobCustomer.UID.ToString();
- result.AddEntity(PullResultType.New, customer);
- needCodes.Add(customer.Code, (code, i, customer));
- }
- // Do code clash checking
- while(needCodes.Count > 0)
- {
- var codes = Client.Query(
- new Filter<Customer>(x => x.Code).InList(needCodes.Values.Select(x => x.customer.Code).ToArray()),
- Columns.None<Customer>().Add(x => x.Code));
- var newNeedCodes = new Dictionary<string, (string prefix, int i, Customer customer)>();
- foreach(var row in codes.Rows)
- {
- var code = row.Get<Customer, string>(x => x.Code);
- if(needCodes.Remove(code, out var needed))
- {
- int i = needed.i;
- do
- {
- needed.customer.Code = $"{needed.prefix}{i:d3}";
- ++i;
- } while (customerCodes.Contains(needed.customer.Code));
- customerCodes.Add(needed.customer.Code);
- newNeedCodes.Add(needed.customer.Code, (needed.prefix, i, needed.customer));
- }
- }
- needCodes = newNeedCodes;
- }
- skip += top;
- }
- return result;
- }
- public IPostResult<Customer> Process(IDataModel<Customer> model)
- {
- var results = new PostResult<Customer>();
- var service = new CustomerService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
- var customers = model.GetTable<Customer>().ToArray<Customer>();
-
- foreach(var customer in customers)
- {
- if(customer.Code.Length > 15)
- {
- results.AddFailed(customer, "Code is longer than 15 characters.");
- continue;
- }
- bool isNew;
- MYOBCustomer myobCustomer;
- Exception? error;
- if(Guid.TryParse(customer.PostedReference, out var myobID))
- {
- if(!service.Get(ConnectionData, myobID).Get(out var newCustomer, out 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;
- }
- myobCustomer = newCustomer;
- isNew = false;
- }
- else
- {
- if(service.Query(
- ConnectionData,
- new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(customer.Code),
- top: 1).Get(out var myobCustomers, out error))
- {
- if(myobCustomers.Items.Length > 0)
- {
- myobCustomer = myobCustomers.Items[0];
- isNew = false;
- }
- else if(service.Query(
- ConnectionData,
- new Filter<MYOBCustomer>(x => x.CompanyName).IsEqualTo(customer.Name)
- .And(new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(null)
- .Or(x => x.DisplayID).IsEqualTo("")
- .Or(x => x.DisplayID).IsEqualTo("*None")),
- top: 1).Get(out myobCustomers, out error))
- {
- if(myobCustomers.Items.Length > 0)
- {
- myobCustomer = myobCustomers.Items[0];
- myobCustomer.DisplayID = customer.Code;
- isNew = false;
- }
- else
- {
- myobCustomer = new MYOBCustomer();
- isNew = true;
- }
- }
- else
- {
- CoreUtils.LogException("", error);
- results.AddFailed(customer, error.Message);
- continue;
- }
- }
- else
- {
- CoreUtils.LogException("", error);
- results.AddFailed(customer, error.Message);
- continue;
- }
- }
- if(UpdateCustomer(ConnectionData, GlobalSettings, customer, myobCustomer, isNew)
- .MapOk(() => ProcessCustomer(model, customer, myobCustomer)).Flatten()
- .MapOk(() => service.Save(ConnectionData, myobCustomer)).Flatten()
- .Get(out var result, out error))
- {
- customer.PostedReference = result.UID.ToString();
- results.AddSuccess(customer);
- }
- else
- {
- results.AddFailed(customer, error.Message);
- }
- }
- return results;
- }
- }
- public class CustomerMYOBPosterEngine<T> : MYOBPosterEngine<Customer, CustomerMYOBPoster, CustomerMYOBPosterSettings>, IPullerEngine<Customer, CustomerMYOBPoster>
- {
- public IPullResult<Customer> DoPull()
- {
- LoadConnectionData();
- return Poster.Pull();
- }
- }
|