123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.Poster.MYOB;
- using InABox.Scripting;
- using MYOB.AccountRight.SDK.Contracts.Version2.Sale;
- using MYOB.AccountRight.SDK.Services.Contact;
- using MYOB.AccountRight.SDK.Services.GeneralLedger;
- using MYOB.AccountRight.SDK.Services.Sale;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MYOB.AccountRight.SDK.Contracts.Version2;
- using Invoice = Comal.Classes.Invoice;
- using InvoiceLine = Comal.Classes.InvoiceLine;
- using MYOBAccount = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.Account;
- using MYOBInvoice = MYOB.AccountRight.SDK.Contracts.Version2.Sale.ServiceInvoice;
- using MYOBInvoiceLine = MYOB.AccountRight.SDK.Contracts.Version2.Sale.ServiceInvoiceLine;
- using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
- namespace PRS.Shared.Posters.MYOB;
- public class InvoiceMYOBPosterSettings : MYOBPosterSettings
- {
- public override string DefaultScript(Type TPostable)
- {
- return @"using MYOBInvoice = MYOB.AccountRight.SDK.Contracts.Version2.Sale.ServiceInvoice;
- using MYOBInvoiceLine = MYOB.AccountRight.SDK.Contracts.Version2.Sale.ServiceInvoiceLine;
- public class Module
- {
- public void BeforePost(IDataModel<Invoice> model)
- {
- // Perform pre-processing
- }
- public void ProcessInvoice(IDataModel<Invoice> model, Invoice invoice, MYOBInvoice myobInvoice)
- {
- // Do extra processing for an invoice; throw an exception to fail this invoice.
- }
- public void ProcessInvoiceLine(IDataModel<Invoice> model, InvoiceLine invoiceLine, MYOBInvoiceLine myobInvoiceLine)
- {
- // Do extra processing for an invoice line; throw an exception to fail this invoice line (and thus fail the entire invoice)
- }
- }";
- }
- }
- public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
- {
- public ScriptDocument? Script { get; set; }
- public MYOBConnectionData ConnectionData { get; set; }
- public InvoiceMYOBPosterSettings Settings { get; set; }
- public MYOBGlobalPosterSettings GlobalSettings { get; set; }
- public bool BeforePost(IDataModel<Invoice> model)
- {
- foreach (var (_, table) in model.ModelTables)
- {
- table.IsDefault = false;
- }
- model.SetIsDefault<Invoice>(true);
- model.SetColumns<Invoice>(RequiredInvoiceColumns());
- model.SetIsDefault<InvoiceLine>(true, alias: "Invoice_InvoiceLine");
- model.SetColumns<InvoiceLine>(RequiredInvoiceLineColumns(), alias: "Invoice_InvoiceLine");
- model.SetIsDefault<Customer>(true, alias: "Invoice_Customer");
- model.SetColumns<Customer>(RequiredCustomerColumns(), alias: "Invoice_Customer");
- Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
- return true;
- }
- #region Script Functions
- private Result<Exception> ProcessInvoice(IDataModel<Invoice> model, Invoice invoice, MYOBInvoice myobInvoice)
- {
- return this.WrapScript("ProcessInvoice", model, invoice, myobInvoice);
- }
- private Result<Exception> ProcessInvoiceLine(IDataModel<Invoice> model, InvoiceLine invoiceLine, MYOBInvoiceLine myobInvoiceLine)
- {
- return this.WrapScript("ProcessInvoiceLine", model, invoiceLine, myobInvoiceLine);
- }
- #endregion
- private static Columns<Invoice> RequiredInvoiceColumns()
- {
- return Columns.None<Invoice>()
- .Add(x => x.ID)
- .Add(x => x.PostedReference)
- .Add(x => x.Number)
- .Add(x => x.Date)
- .Add(x => x.DueDate)
- .Add(x => x.CustomerLink.ID)
- .Add(x => x.Description);
- }
- private static Columns<InvoiceLine> RequiredInvoiceLineColumns()
- {
- return Columns.None<InvoiceLine>()
- .Add(x => x.ID)
- .Add(x => x.InvoiceLink.ID)
- .Add(x => x.Description)
- .Add(x => x.IncTax)
- .Add(x => x.SellGL.ID)
- .Add(x => x.SellGL.Code)
- .Add(x => x.SellGL.PostedReference)
- .Add(x => x.TaxCode.ID)
- .Add(x => x.TaxCode.Code)
- .Add(x => x.TaxCode.PostedReference);
- }
- private static Columns<Customer> RequiredCustomerColumns()
- {
- return CustomerMYOBPoster.RequiredColumns();
- }
- public IPostResult<Invoice> Process(IDataModel<Invoice> model)
- {
- // Documentation: https://developer.myob.com/api/myob-business-api/v2/sale/invoice/invoice_service/
- var results = new PostResult<Invoice>();
- var service = new ServiceInvoiceService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
- var invoices = model.GetTable<Invoice>().ToArray<Invoice>();
- var customers = model.GetTable<Customer>("Invoice_Customer")
- .ToObjects<Customer>().ToDictionary(x => x.ID);
- var invoiceLines = model.GetTable<InvoiceLine>("Invoice_InvoiceLine")
- .ToObjects<InvoiceLine>()
- .GroupBy(x => x.InvoiceLink.ID)
- .ToDictionary(x => x.Key, x => x.ToArray());
- foreach(var invoice in invoices)
- {
- MYOBInvoice myobInvoice;
- Exception? error;
- if(Guid.TryParse(invoice.PostedReference, out var myobID))
- {
- if(!service.Get(ConnectionData, myobID).Get(out var newInvoice, out error))
- {
- CoreUtils.LogException("", error, $"Failed to find Invoice in MYOB with id {myobID}");
- results.AddFailed(invoice, $"Failed to find Invoice in MYOB with id {myobID}: {error.Message}");
- continue;
- }
- myobInvoice = newInvoice;
- }
- else
- {
- myobInvoice = new MYOBInvoice();
- }
- myobInvoice.Number = invoice.Number.ToString().Truncate(13);
- myobInvoice.Date = invoice.Date;
-
- myobInvoice.Terms ??= new();
- myobInvoice.Terms.PaymentIsDue = TermsPaymentType.InAGivenNumberOfDays;
- myobInvoice.Terms.BalanceDueDate = (invoice.DueDate.Date - invoice.Date.Date).Days + 1;
-
- // myobInvoice.CustomerPurchaseOrderNumber =
- if(customers.TryGetValue(invoice.CustomerLink.ID, out var customer))
- {
- if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer, GlobalSettings).Get(out var customerID, out error))
- {
- CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
- results.AddFailed(invoice, error.Message);
- continue;
- }
- myobInvoice.Customer ??= new();
- myobInvoice.Customer.UID = customerID;
- results.AddFragment(customer, customer.PostedStatus);
- }
- // myobInvoice.PromisedDate =
- if(invoiceLines.TryGetValue(invoice.ID, out var lines))
- {
- var newLines = new MYOBInvoiceLine[lines.Length];
- string? failed = null;
- for(int i = 0; i < lines.Length; ++i)
- {
- var item = lines[i];
- var line = new MYOBInvoiceLine();
- line.Type = InvoiceLineType.Transaction;
- line.Description = item.Description.Truncate(1000);
- // line.UnitOfMeasure =
- // line.UnitCount =
- // line.UnitPrice =
- // line.UnitPriceForeign =
- // line.DiscountPercent =
- line.Total = Math.Round(Convert.ToDecimal(item.IncTax),2);
- line.TotalForeign = 0;
- if(item.SellGL.ID == Guid.Empty)
- {
- failed = "Not all lines have a SellGL code set.";
- break;
- }
- if(!Guid.TryParse(item.SellGL.PostedReference, out var accountID))
- {
- if(AccountMYOBUtils.GetAccount(ConnectionData, item.SellGL.Code).Get(out accountID, out error))
- {
- results.AddFragment(new GLCode { PostedReference = accountID.ToString() }.SetID(item.SellGL.ID));
- }
- else
- {
- CoreUtils.LogException("", error);
- failed = error.Message;
- break;
- }
- }
- line.Account ??= new();
- line.Account.UID = accountID;
- if(item.TaxCode.ID == Guid.Empty)
- {
- failed = "Not all lines have a TaxCode set.";
- break;
- }
- if(!Guid.TryParse(item.TaxCode.PostedReference, out var taxCodeID))
- {
- if (!ConnectionData.GetUID<TaxCodeService, MYOBTaxCode>(
- new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(item.TaxCode.Code))
- .Get(out taxCodeID, out error))
- {
- CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}");
- failed = $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}: {error.Message}";
- break;
- }
- else if (taxCodeID == Guid.Empty)
- {
- failed = $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}";
- break;
- }
- results.AddFragment(new TaxCode { PostedReference = taxCodeID.ToString() }.SetID(taxCodeID));
- }
- line.TaxCode ??= new();
- line.TaxCode.UID = taxCodeID;
- if(!ProcessInvoiceLine(model, item, line).Get(out error))
- {
- failed = error.Message;
- break;
- }
- newLines[i] = line;
- }
- if(failed is not null)
- {
- results.AddFailed(invoice, failed);
- continue;
- }
- myobInvoice.Lines = newLines;
- }
- else
- {
- myobInvoice.Lines = [];
- }
- // ShipToAddress
- // Terms
- myobInvoice.IsTaxInclusive = true;
- // Freight
- // FreightTaxCode
- // Category
- // Salesperson
- myobInvoice.Comment = invoice.Description.Truncate(2000);
- // ShippingMethod
- // JournalMemo
- // ReferralSource
- // InvoiceDeliveryStatus
- // CanApplySurcharge
- myobInvoice.InvoiceType = InvoiceLayoutType.Service;
- // Order
- // OnlinePaymentMethod
- if(!ProcessInvoice(model, invoice, myobInvoice).Get(out error))
- {
- results.AddFailed(invoice, error.Message);
- continue;
- }
- if(service.Save(ConnectionData, myobInvoice).Get(out var result, out error))
- {
- invoice.PostedReference = result.UID.ToString();
- results.AddSuccess(invoice);
- }
- else
- {
- CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
- results.AddFailed(invoice, error.Message);
- }
- }
- return results;
- }
- }
- public class InvoiceMYOBPosterEngine<T> : MYOBPosterEngine<Invoice, InvoiceMYOBPoster, InvoiceMYOBPosterSettings> { }
|