BillMYOBPoster.cs 9.1 KB

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