BillMYOBPoster.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Core.Postable;
  4. using InABox.Poster.MYOB;
  5. using MYOB.AccountRight.SDK.Contracts.Version2.Sale;
  6. using MYOB.AccountRight.SDK.Services.GeneralLedger;
  7. using MYOB.AccountRight.SDK.Services.Purchase;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using MYOBAccount = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.Account;
  14. using MYOBBill = MYOB.AccountRight.SDK.Contracts.Version2.Purchase.ServiceBill;
  15. using MYOBBillLine = MYOB.AccountRight.SDK.Contracts.Version2.Purchase.ServiceBillLine;
  16. using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
  17. namespace PRS.Shared.Posters.MYOB;
  18. public class BillMYOBPosterSettings : MYOBPosterSettings
  19. {
  20. }
  21. public class BillMYOBPoster : IMYOBPoster<Bill, BillMYOBPosterSettings>
  22. {
  23. public MYOBConnectionData ConnectionData { get; set; }
  24. public BillMYOBPosterSettings Settings { get; set; }
  25. public MYOBGlobalPosterSettings GlobalSettings { get; set; }
  26. public bool BeforePost(IDataModel<Bill> model)
  27. {
  28. foreach(var (_, table) in model.ModelTables)
  29. {
  30. table.IsDefault = false;
  31. }
  32. model.SetIsDefault<Bill>(true);
  33. model.SetColumns<Bill>(RequiredBillColumns());
  34. model.SetIsDefault<BillLine>(true, alias: "Bill_BillLine");
  35. model.SetColumns<BillLine>(RequiredBillLineColumns(), alias: "Bill_BillLine");
  36. model.SetIsDefault<Supplier>(true, alias: "Bill_Supplier");
  37. model.SetColumns<Supplier>(RequiredSupplierColumns(), alias: "Bill_Supplier");
  38. return true;
  39. }
  40. private static Columns<Bill> RequiredBillColumns()
  41. {
  42. return Columns.None<Bill>()
  43. .Add(x => x.ID)
  44. .Add(x => x.PostedReference)
  45. .Add(x => x.Number)
  46. .Add(x => x.AccountingDate)
  47. .Add(x => x.SupplierLink.ID)
  48. .Add(x => x.Description);
  49. }
  50. private static Columns<BillLine> RequiredBillLineColumns()
  51. {
  52. return Columns.None<BillLine>()
  53. .Add(x => x.ID)
  54. .Add(x => x.BillLink.ID)
  55. .Add(x => x.Description)
  56. .Add(x => x.IncTax)
  57. .Add(x => x.PurchaseGL.ID)
  58. .Add(x => x.PurchaseGL.Code)
  59. .Add(x => x.PurchaseGL.PostedReference)
  60. .Add(x => x.TaxCode.ID)
  61. .Add(x => x.TaxCode.Code)
  62. .Add(x => x.TaxCode.PostedReference);
  63. }
  64. private static Columns<Supplier> RequiredSupplierColumns()
  65. {
  66. return SupplierMYOBPoster.RequiredColumns();
  67. }
  68. public IPostResult<Bill> Process(IDataModel<Bill> model)
  69. {
  70. // https://developer.myob.com/api/myob-business-api/v2/purchase/bill/bill_service/
  71. var results = new PostResult<Bill>();
  72. var service = new ServiceBillService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
  73. var bills = model.GetTable<Bill>().ToArray<Bill>();
  74. var suppliers = model.GetTable<Supplier>("Bill_Supplier")
  75. .ToObjects<Supplier>().ToDictionary(x => x.ID);
  76. var billLines = model.GetTable<BillLine>("Bill_BillLine")
  77. .ToObjects<BillLine>().GroupBy(x => x.BillLink.ID).ToDictionary(x => x.Key, x => x.ToArray());
  78. foreach(var bill in bills)
  79. {
  80. MYOBBill myobBill;
  81. Exception? error;
  82. bool isNew;
  83. if(Guid.TryParse(bill.PostedReference, out var myobID))
  84. {
  85. if(!service.Get(ConnectionData, myobID).Get(out var newBill, out error))
  86. {
  87. CoreUtils.LogException("", error, $"Failed to find Bill in MYOB with id {myobID}");
  88. results.AddFailed(bill, $"Failed to find Bill in MYOB with id {myobID}: {error.Message}");
  89. continue;
  90. }
  91. myobBill = newBill;
  92. isNew = false;
  93. }
  94. else
  95. {
  96. myobBill = new MYOBBill();
  97. isNew = true;
  98. }
  99. myobBill.Number = bill.Number.Truncate(13);
  100. // Probably configure which date.
  101. myobBill.Date = bill.AccountingDate;
  102. myobBill.SupplierInvoiceNumber = bill.Number.Truncate(255);
  103. if(suppliers.TryGetValue(bill.SupplierLink.ID, out var supplier))
  104. {
  105. if(!SupplierMYOBPoster.MapSupplier(ConnectionData, supplier, GlobalSettings).Get(out var supplierID, out error))
  106. {
  107. CoreUtils.LogException("", error, $"Error while posting bill {bill.ID}");
  108. results.AddFailed(bill, error.Message);
  109. continue;
  110. }
  111. myobBill.Supplier ??= new();
  112. myobBill.Supplier.UID = supplierID;
  113. results.AddFragment(supplier);
  114. }
  115. // myobBill.ShipToAddress =
  116. // myobBill.Terms =
  117. myobBill.IsTaxInclusive = true;
  118. myobBill.IsReportable = true;
  119. // myobBill.Freight =
  120. // myobBill.FreightForeign =
  121. if (isNew)
  122. {
  123. if(!this.GetDefaultTaxCode().Get(out var taxCodeID, out error))
  124. {
  125. results.AddFailed(bill, error.Message);
  126. continue;
  127. }
  128. myobBill.FreightTaxCode ??= new();
  129. myobBill.FreightTaxCode.UID = taxCodeID;
  130. }
  131. // myobBill.Category =
  132. myobBill.Comment = bill.Description.Truncate(2000);
  133. // myobBill.ShippingMethod =
  134. // myobBill.PromisedDate =
  135. // myobBill.JournalMemo =
  136. // myobBill.BillDeliveryStatus =
  137. // myobBill.Order =
  138. if(billLines.TryGetValue(bill.ID, out var lines))
  139. {
  140. var newLines = new MYOBBillLine[lines.Length];
  141. string? failed = null;
  142. for(int i = 0; i < lines.Length; ++i)
  143. {
  144. var billLine = lines[i];
  145. var line = new MYOBBillLine();
  146. line.Type = InvoiceLineType.Transaction;
  147. line.Description = billLine.Description;
  148. if(billLine.PurchaseGL.ID == Guid.Empty)
  149. {
  150. failed = "Not all lines have a PurchaseGL code set.";
  151. break;
  152. }
  153. if(!Guid.TryParse(billLine.PurchaseGL.PostedReference, out var accountID))
  154. {
  155. if(AccountMYOBUtils.GetAccount(ConnectionData, billLine.PurchaseGL.Code).Get(out accountID, out error))
  156. {
  157. results.AddFragment(new GLCode { ID = billLine.PurchaseGL.ID, PostedReference = accountID.ToString() });
  158. }
  159. else
  160. {
  161. CoreUtils.LogException("", error);
  162. failed = error.Message;
  163. break;
  164. }
  165. }
  166. line.Account ??= new();
  167. line.Account.UID = accountID;
  168. line.Total = (decimal)billLine.IncTax;
  169. line.TotalForeign = 0;
  170. // line.UnitsOfMeasure =
  171. // line.UnitCount =
  172. // line.UnitPrice =
  173. // line.UnitPriceForeign =
  174. // line.DiscountPercent =
  175. // line.Job =
  176. if(billLine.TaxCode.ID == Guid.Empty)
  177. {
  178. failed = "Not all lines have a TaxCode set.";
  179. break;
  180. }
  181. if(!Guid.TryParse(billLine.TaxCode.PostedReference, out var taxCodeID))
  182. {
  183. if (!ConnectionData.GetUID<TaxCodeService, MYOBTaxCode>(
  184. new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(billLine.TaxCode.Code))
  185. .Get(out taxCodeID, out error))
  186. {
  187. CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}");
  188. failed = $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}: {error.Message}";
  189. break;
  190. }
  191. else if (taxCodeID == Guid.Empty)
  192. {
  193. failed = $"Failed to find TaxCode in MYOB with code {billLine.TaxCode.Code}";
  194. break;
  195. }
  196. results.AddFragment(new TaxCode { ID = taxCodeID, PostedReference = taxCodeID.ToString() });
  197. }
  198. line.TaxCode ??= new();
  199. line.TaxCode.UID = taxCodeID;
  200. newLines[i] = line;
  201. }
  202. if(failed is not null)
  203. {
  204. results.AddFailed(bill, failed);
  205. continue;
  206. }
  207. myobBill.Lines = newLines;
  208. }
  209. else
  210. {
  211. myobBill.Lines = [];
  212. }
  213. if(service.Save(ConnectionData, myobBill).Get(out var result, out error))
  214. {
  215. bill.PostedReference = result.UID.ToString();
  216. results.AddSuccess(bill);
  217. }
  218. else
  219. {
  220. CoreUtils.LogException("", error, $"Error while posting receipt {bill.ID}");
  221. results.AddFailed(bill, error.Message);
  222. }
  223. }
  224. return results;
  225. }
  226. }
  227. public class BillMYOBPosterEngine<T> : MYOBPosterEngine<Bill, BillMYOBPosterSettings> { }