using Comal.Classes; using InABox.Core; using InABox.Poster.MYOB; using MYOB.AccountRight.SDK.Contracts.Version2.Sale; using MYOB.AccountRight.SDK.Services.GeneralLedger; using MYOB.AccountRight.SDK.Services.Purchase; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MYOBAccount = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.Account; using MYOBBill = MYOB.AccountRight.SDK.Contracts.Version2.Purchase.ServiceBill; using MYOBBillLine = MYOB.AccountRight.SDK.Contracts.Version2.Purchase.ServiceBillLine; using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode; namespace PRS.Shared.Posters.MYOB; public class BillMYOBPosterSettings : MYOBPosterSettings { } public class BillMYOBPoster : IMYOBPoster { public MYOBConnectionData ConnectionData { get; set; } public BillMYOBPosterSettings Settings { get; set; } public MYOBGlobalPosterSettings GlobalSettings { get; set; } public bool BeforePost(IDataModel model) { foreach(var (_, table) in model.ModelTables) { table.ShouldLoad = false; } model.SetShouldLoad(true); model.SetColumns(RequiredBillColumns()); model.SetShouldLoad(true, alias: "Bill_BillLine"); model.SetColumns(RequiredBillLineColumns()); model.SetIsDefault(true, alias: "Bill_Supplier"); model.SetColumns(RequiredSupplierColumns()); return true; } private static Columns RequiredBillColumns() { return Columns.None() .Add(x => x.ID) .Add(x => x.PostedReference) .Add(x => x.Number) .Add(x => x.AccountingDate) .Add(x => x.SupplierLink.ID) .Add(x => x.Description); } private static Columns RequiredBillLineColumns() { return Columns.None() .Add(x => x.ID) .Add(x => x.BillLink.ID) .Add(x => x.Description) .Add(x => x.IncTax) .Add(x => x.PurchaseGL.ID) .Add(x => x.PurchaseGL.Code) .Add(x => x.PurchaseGL.PostedReference) .Add(x => x.TaxCode.ID) .Add(x => x.TaxCode.Code) .Add(x => x.TaxCode.PostedReference); } private static Columns RequiredSupplierColumns() { return SupplierMYOBPoster.RequiredColumns(); } public IPostResult Process(IDataModel model) { // https://developer.myob.com/api/myob-business-api/v2/purchase/bill/bill_service/ var results = new PostResult(); var service = new ServiceBillService(ConnectionData.Configuration, null, ConnectionData.AuthKey); var bills = model.GetTable().ToArray(); var suppliers = model.GetTable("Bill_Supplier") .ToObjects().ToDictionary(x => x.ID); var billLines = model.GetTable("Bill_BillLine") .ToObjects().GroupBy(x => x.BillLink.ID).ToDictionary(x => x.Key, x => x.ToArray()); foreach(var bill in bills) { MYOBBill myobBill; if(Guid.TryParse(bill.PostedReference, out var myobID)) { if(!service.Get(ConnectionData, myobID).Get(out var newBill, out var error)) { CoreUtils.LogException("", error, $"Failed to find Bill in MYOB with id {myobID}"); results.AddFailed(bill, $"Failed to find Bill in MYOB with id {myobID}: {error.Message}"); continue; } myobBill = newBill; } else { myobBill = new MYOBBill(); } myobBill.Number = bill.Number.Truncate(13); // Probably configure which date. myobBill.Date = bill.AccountingDate; myobBill.SupplierInvoiceNumber = bill.Number.Truncate(255); if(suppliers.TryGetValue(bill.SupplierLink.ID, out var supplier)) { if(!SupplierMYOBPoster.MapSupplier(ConnectionData, supplier).Get(out var supplierID, out var error)) { CoreUtils.LogException("", error, $"Error while posting bill {bill.ID}"); results.AddFailed(bill, error.Message); continue; } myobBill.Supplier.UID = supplierID; } // myobBill.ShipToAddress = // myobBill.Terms = myobBill.IsTaxInclusive = true; myobBill.IsReportable = true; // myobBill.Freight = // myobBill.FreightForeign = // myobBill.FreightTaxCode = // myobBill.Category = myobBill.Comment = bill.Description.Truncate(2000); // myobBill.ShippingMethod = // myobBill.PromisedDate = // myobBill.JournalMemo = // myobBill.BillDeliveryStatus = // myobBill.Order = if(billLines.TryGetValue(bill.ID, out var lines)) { var newLines = new MYOBBillLine[lines.Length]; string? failed = null; for(int i = 0; i < lines.Length; ++i) { var billLine = lines[i]; var line = new MYOBBillLine(); line.RowID = i + 1; line.Type = InvoiceLineType.Transaction; line.Description = billLine.Description; if(billLine.PurchaseGL.ID == Guid.Empty) { failed = "Not all lines have a PurchaseGL code set."; break; } if(!Guid.TryParse(billLine.PurchaseGL.PostedReference, out var accountID)) { if(AccountMYOBUtils.GetAccount(ConnectionData, billLine.PurchaseGL.Code).Get(out accountID, out var error)) { results.AddFragment(new GLCode { ID = billLine.PurchaseGL.ID, PostedReference = accountID.ToString() }); } else { CoreUtils.LogException("", error); failed = error.Message; break; } } line.Account.UID = accountID; line.Total = (decimal)billLine.IncTax; line.TotalForeign = 0; // line.UnitsOfMeasure = // line.UnitCount = // line.UnitPrice = // line.UnitPriceForeign = // line.DiscountPercent = // line.Job = if(billLine.TaxCode.ID == Guid.Empty) { failed = "Not all lines have a TaxCode set."; break; } if(!Guid.TryParse(billLine.TaxCode.PostedReference, out var taxCodeID)) { if (!ConnectionData.GetUID( new Filter(x => x.Code).IsEqualTo(billLine.TaxCode.Code)) .Get(out taxCodeID, out var error)) { CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}"); failed = $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}: {error.Message}"; break; } else if (taxCodeID == Guid.Empty) { failed = $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}"; break; } results.AddFragment(new TaxCode { ID = taxCodeID, PostedReference = taxCodeID.ToString() }); } line.TaxCode.UID = taxCodeID; newLines[i] = line; } if(failed is not null) { results.AddFailed(bill, failed); continue; } myobBill.Lines = newLines; } else { myobBill.Lines = []; } try { var result = service.UpdateEx(ConnectionData.CompanyFile, myobBill, ConnectionData.CompanyFileCredentials); bill.PostedReference = result.UID.ToString(); results.AddSuccess(bill); } catch(Exception e) { CoreUtils.LogException("", e, $"Error while posting receipt {bill.ID}"); results.AddFailed(bill, e.Message); } } return results; } } public class BillMYOBPosterEngine : MYOBPosterEngine { }