123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- 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<Bill, BillMYOBPosterSettings>
- {
- public MYOBConnectionData ConnectionData { get; set; }
- public BillMYOBPosterSettings Settings { get; set; }
- public MYOBGlobalPosterSettings GlobalSettings { get; set; }
- public bool BeforePost(IDataModel<Bill> model)
- {
- foreach(var (_, table) in model.ModelTables)
- {
- table.ShouldLoad = false;
- }
- model.SetShouldLoad<Bill>(true);
- model.SetColumns<Bill>(RequiredBillColumns());
- model.SetShouldLoad<BillLine>(true, alias: "Bill_BillLine");
- model.SetColumns<BillLine>(RequiredBillLineColumns());
- model.SetIsDefault<Supplier>(true, alias: "Bill_Supplier");
- model.SetColumns<Supplier>(RequiredSupplierColumns());
- return true;
- }
- private static Columns<Bill> RequiredBillColumns()
- {
- return Columns.None<Bill>()
- .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<BillLine> RequiredBillLineColumns()
- {
- return Columns.None<BillLine>()
- .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<Supplier> RequiredSupplierColumns()
- {
- return SupplierMYOBPoster.RequiredColumns();
- }
- public IPostResult<Bill> Process(IDataModel<Bill> model)
- {
- // https://developer.myob.com/api/myob-business-api/v2/purchase/bill/bill_service/
- var results = new PostResult<Bill>();
- var service = new ServiceBillService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
- var bills = model.GetTable<Bill>().ToArray<Bill>();
- var suppliers = model.GetTable<Supplier>("Bill_Supplier")
- .ToObjects<Supplier>().ToDictionary(x => x.ID);
- var billLines = model.GetTable<BillLine>("Bill_BillLine")
- .ToObjects<BillLine>().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<TaxCodeService, MYOBTaxCode>(
- new Filter<MYOBTaxCode>(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<T> : MYOBPosterEngine<Bill, BillMYOBPosterSettings> { }
|