123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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;
- namespace PRS.Shared.Posters.MYOB;
- public class CustomerMYOBPosterSettings : MYOBPosterSettings
- {
- [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; }
- private 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] : "";
- }
- private MYOBAddress ConvertAddress(Address address, int location, IContact contact)
- {
- return new MYOBAddress
- {
- Location = location,
- 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);
- var customers = model.GetTable<Customer>().ToArray<Customer>();
-
- foreach(var customer in customers)
- {
- try
- {
- 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);
- myobCustomer = new MYOBCustomer
- {
- CompanyName = customer.Name.Truncate(50),
- FirstName = firstName.Truncate(30),
- LastName = lastName.Truncate(20),
- IsIndividual = false,
- DisplayID = customer.Code.Truncate(15),
- IsActive = customer.CustomerStatus.Active,
- Addresses =
- [
- ConvertAddress(customer.Delivery, 1, customer.DefaultContact),
- ConvertAddress(customer.Postal, 2, customer.DefaultContact)
- ],
- // Notes =
- // PhotoURI =
- // RowVersion =
- };
- 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 (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);
- }
- catch (Exception e)
- {
- CoreUtils.LogException("", e, $"Error while posting customer {customer.ID}");
- results.AddFailed(customer, e.Message);
- }
- }
- return results;
- }
- }
- public class CustomerMYOBPosterEngine : MYOBPosterEngine<Customer, CustomerMYOBPosterSettings> { }
|