|  | @@ -0,0 +1,127 @@
 | 
	
		
			
				|  |  | +using Comal.Classes;
 | 
	
		
			
				|  |  | +using FastReport.Data;
 | 
	
		
			
				|  |  | +using InABox.Core;
 | 
	
		
			
				|  |  | +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;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +namespace PRS.Shared.Posters.MYOB;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class CustomerMYOBPoster : IMYOBPoster<Customer>
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    public MYOBPosterSettings 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 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
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    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)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            try
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                Guid.TryParse(customer.PostedReference, out var myobID);
 | 
	
		
			
				|  |  | +                SplitName(customer.DefaultContact.Name, out var firstName, out var lastName);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                var myobCustomer = new MYOBCustomer
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    UID = myobID,
 | 
	
		
			
				|  |  | +                    CompanyName = customer.Name,
 | 
	
		
			
				|  |  | +                    FirstName = firstName,
 | 
	
		
			
				|  |  | +                    LastName = lastName,
 | 
	
		
			
				|  |  | +                    IsIndividual = false,
 | 
	
		
			
				|  |  | +                    DisplayID = TruncateString(customer.Code, 15),
 | 
	
		
			
				|  |  | +                    IsActive = customer.CustomerStatus.Active,
 | 
	
		
			
				|  |  | +                    Addresses =
 | 
	
		
			
				|  |  | +                    [
 | 
	
		
			
				|  |  | +                        ConvertAddress(customer.Delivery, 1, customer.DefaultContact),
 | 
	
		
			
				|  |  | +                        ConvertAddress(customer.Postal, 2, customer.DefaultContact)
 | 
	
		
			
				|  |  | +                    ],
 | 
	
		
			
				|  |  | +                    // Notes = 
 | 
	
		
			
				|  |  | +                    CurrentBalance = (decimal)customer.Balance,
 | 
	
		
			
				|  |  | +                    // PaymentDetails = 
 | 
	
		
			
				|  |  | +                    // PhotoURI =
 | 
	
		
			
				|  |  | +                    // RowVersion = 
 | 
	
		
			
				|  |  | +                };
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.SaleLayout = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.PrintedFOrm = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.InvoiceDelivery = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.ItemPriceLevel = 
 | 
	
		
			
				|  |  | +                // 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;
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.TaxCode = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.FreightTaxCode = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.UseCustomerTaxCode = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.Terms = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.Credit = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.TaxIdNumber = 
 | 
	
		
			
				|  |  | +                // myobCustomer.SellingDetails.Memo = 
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                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;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |