InvoiceMYOBPoster.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Poster.MYOB;
  4. using MYOB.AccountRight.SDK.Contracts.Version2.Sale;
  5. using MYOB.AccountRight.SDK.Services.Contact;
  6. using MYOB.AccountRight.SDK.Services.GeneralLedger;
  7. using MYOB.AccountRight.SDK.Services.Sale;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Invoice = Comal.Classes.Invoice;
  14. using InvoiceLine = Comal.Classes.InvoiceLine;
  15. using MYOBAccount = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.Account;
  16. using MYOBInvoice = MYOB.AccountRight.SDK.Contracts.Version2.Sale.ServiceInvoice;
  17. using MYOBInvoiceLine = MYOB.AccountRight.SDK.Contracts.Version2.Sale.ServiceInvoiceLine;
  18. using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
  19. namespace PRS.Shared.Posters.MYOB;
  20. public class InvoiceMYOBPosterSettings : MYOBPosterSettings
  21. {
  22. }
  23. public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
  24. {
  25. public MYOBConnectionData ConnectionData { get; set; }
  26. public InvoiceMYOBPosterSettings Settings { get; set; }
  27. public MYOBGlobalPosterSettings GlobalSettings { get; set; }
  28. public bool BeforePost(IDataModel<Invoice> model)
  29. {
  30. foreach (var (_, table) in model.ModelTables)
  31. {
  32. table.IsDefault = false;
  33. }
  34. model.SetIsDefault<Invoice>(true);
  35. model.SetColumns<Invoice>(RequiredInvoiceColumns());
  36. model.SetIsDefault<InvoiceLine>(true, alias: "Invoice_InvoiceLine");
  37. model.SetColumns<InvoiceLine>(RequiredInvoiceLineColumns(), alias: "Invoice_InvoiceLine");
  38. model.SetIsDefault<Customer>(true, alias: "Invoice_Customer");
  39. model.SetColumns<Customer>(RequiredCustomerColumns(), alias: "Invoice_Customer");
  40. return true;
  41. }
  42. private static Columns<Invoice> RequiredInvoiceColumns()
  43. {
  44. return Columns.None<Invoice>()
  45. .Add(x => x.ID)
  46. .Add(x => x.PostedReference)
  47. .Add(x => x.Number)
  48. .Add(x => x.Date)
  49. .Add(x => x.CustomerLink.ID)
  50. .Add(x => x.Description);
  51. }
  52. private static Columns<InvoiceLine> RequiredInvoiceLineColumns()
  53. {
  54. return Columns.None<InvoiceLine>()
  55. .Add(x => x.ID)
  56. .Add(x => x.InvoiceLink.ID)
  57. .Add(x => x.Description)
  58. .Add(x => x.IncTax)
  59. .Add(x => x.SellGL.ID)
  60. .Add(x => x.SellGL.Code)
  61. .Add(x => x.SellGL.PostedReference)
  62. .Add(x => x.TaxCode.ID)
  63. .Add(x => x.TaxCode.Code)
  64. .Add(x => x.TaxCode.PostedReference);
  65. }
  66. private static Columns<Customer> RequiredCustomerColumns()
  67. {
  68. return CustomerMYOBPoster.RequiredColumns();
  69. }
  70. public IPostResult<Invoice> Process(IDataModel<Invoice> model)
  71. {
  72. // Documentation: https://developer.myob.com/api/myob-business-api/v2/sale/invoice/invoice_service/
  73. var results = new PostResult<Invoice>();
  74. var service = new ServiceInvoiceService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
  75. var invoices = model.GetTable<Invoice>().ToArray<Invoice>();
  76. var customers = model.GetTable<Customer>("Invoice_Customer")
  77. .ToObjects<Customer>().ToDictionary(x => x.ID);
  78. var invoiceLines = model.GetTable<InvoiceLine>("Invoice_InvoiceLine")
  79. .ToObjects<InvoiceLine>()
  80. .GroupBy(x => x.InvoiceLink.ID)
  81. .ToDictionary(x => x.Key, x => x.ToArray());
  82. foreach(var invoice in invoices)
  83. {
  84. MYOBInvoice myobInvoice;
  85. Exception? error;
  86. if(Guid.TryParse(invoice.PostedReference, out var myobID))
  87. {
  88. if(!service.Get(ConnectionData, myobID).Get(out var newInvoice, out error))
  89. {
  90. CoreUtils.LogException("", error, $"Failed to find Invoice in MYOB with id {myobID}");
  91. results.AddFailed(invoice, $"Failed to find Invoice in MYOB with id {myobID}: {error.Message}");
  92. continue;
  93. }
  94. myobInvoice = newInvoice;
  95. }
  96. else
  97. {
  98. myobInvoice = new MYOBInvoice();
  99. }
  100. myobInvoice.Number = invoice.Number.ToString().Truncate(13);
  101. myobInvoice.Date = invoice.Date;
  102. // myobInvoice.CustomerPurchaseOrderNumber =
  103. if(customers.TryGetValue(invoice.CustomerLink.ID, out var customer))
  104. {
  105. if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer).Get(out var customerID, out error))
  106. {
  107. CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
  108. results.AddFailed(invoice, error.Message);
  109. continue;
  110. }
  111. myobInvoice.Customer ??= new();
  112. myobInvoice.Customer.UID = customerID;
  113. }
  114. // myobInvoice.PromisedDate =
  115. if(invoiceLines.TryGetValue(invoice.ID, out var lines))
  116. {
  117. var newLines = new MYOBInvoiceLine[lines.Length];
  118. string? failed = null;
  119. for(int i = 0; i < lines.Length; ++i)
  120. {
  121. var item = lines[i];
  122. var line = new MYOBInvoiceLine();
  123. line.Type = InvoiceLineType.Transaction;
  124. line.Description = item.Description.Truncate(1000);
  125. // line.UnitOfMeasure =
  126. // line.UnitCount =
  127. // line.UnitPrice =
  128. // line.UnitPriceForeign =
  129. // line.DiscountPercent =
  130. line.Total = (decimal)item.IncTax;
  131. line.TotalForeign = 0;
  132. if(item.SellGL.ID == Guid.Empty)
  133. {
  134. failed = "Not all lines have a SellGL code set.";
  135. break;
  136. }
  137. if(!Guid.TryParse(item.SellGL.PostedReference, out var accountID))
  138. {
  139. if(AccountMYOBUtils.GetAccount(ConnectionData, item.SellGL.Code).Get(out accountID, out error))
  140. {
  141. results.AddFragment(new GLCode { ID = item.SellGL.ID, PostedReference = accountID.ToString() });
  142. }
  143. else
  144. {
  145. CoreUtils.LogException("", error);
  146. failed = error.Message;
  147. break;
  148. }
  149. }
  150. line.Account ??= new();
  151. line.Account.UID = accountID;
  152. if(item.TaxCode.ID == Guid.Empty)
  153. {
  154. failed = "Not all lines have a TaxCode set.";
  155. break;
  156. }
  157. if(!Guid.TryParse(item.TaxCode.PostedReference, out var taxCodeID))
  158. {
  159. if (!ConnectionData.GetUID<TaxCodeService, MYOBTaxCode>(
  160. new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(item.TaxCode.Code))
  161. .Get(out taxCodeID, out error))
  162. {
  163. CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}");
  164. failed = $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}: {error.Message}";
  165. break;
  166. }
  167. else if (taxCodeID == Guid.Empty)
  168. {
  169. failed = $"Failed to find TaxCode in MYOB with code {item.TaxCode.Code}";
  170. break;
  171. }
  172. results.AddFragment(new TaxCode { ID = taxCodeID, PostedReference = taxCodeID.ToString() });
  173. }
  174. line.TaxCode ??= new();
  175. line.TaxCode.UID = taxCodeID;
  176. newLines[i] = line;
  177. }
  178. if(failed is not null)
  179. {
  180. results.AddFailed(invoice, failed);
  181. continue;
  182. }
  183. myobInvoice.Lines = newLines;
  184. }
  185. else
  186. {
  187. myobInvoice.Lines = [];
  188. }
  189. // ShipToAddress
  190. // Terms
  191. myobInvoice.IsTaxInclusive = true;
  192. // Freight
  193. // FreightTaxCode
  194. // Category
  195. // Salesperson
  196. myobInvoice.Comment = invoice.Description.Truncate(2000);
  197. // ShippingMethod
  198. // JournalMemo
  199. // ReferralSource
  200. // InvoiceDeliveryStatus
  201. // CanApplySurcharge
  202. myobInvoice.InvoiceType = InvoiceLayoutType.Service;
  203. // Order
  204. // OnlinePaymentMethod
  205. if(service.Save(ConnectionData, myobInvoice).Get(out var result, out error))
  206. {
  207. invoice.PostedReference = result.UID.ToString();
  208. results.AddSuccess(invoice);
  209. }
  210. else
  211. {
  212. CoreUtils.LogException("", error, $"Error while posting invoice {invoice.ID}");
  213. results.AddFailed(invoice, error.Message);
  214. }
  215. }
  216. return results;
  217. }
  218. }
  219. public class InvoiceMYOBPosterEngine<T> : MYOBPosterEngine<Invoice, InvoiceMYOBPosterSettings> { }