SupplierMYOBPoster.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Core.Postable;
  5. using InABox.Poster.MYOB;
  6. using MYOB.AccountRight.SDK.Services.Contact;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using MYOBSupplier = MYOB.AccountRight.SDK.Contracts.Version2.Contact.Supplier;
  13. namespace PRS.Shared.Posters.MYOB;
  14. public class SupplierMYOBPosterSettings : MYOBPosterSettings
  15. {
  16. [TextBoxEditor(ToolTip = "The MYOB tax code which should be used when posting suppliers")]
  17. public string DefaultTaxCode { get; set; }
  18. }
  19. public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettings>
  20. {
  21. public MYOBConnectionData ConnectionData { get; set; }
  22. public SupplierMYOBPosterSettings Settings { get; set; }
  23. public MYOBGlobalPosterSettings GlobalSettings { get; set; }
  24. public bool BeforePost(IDataModel<Supplier> model)
  25. {
  26. foreach (var (_, table) in model.ModelTables)
  27. {
  28. table.IsDefault = false;
  29. }
  30. model.SetIsDefault<Supplier>(true);
  31. model.SetColumns<Supplier>(RequiredColumns());
  32. return true;
  33. }
  34. public static Columns<Supplier> RequiredColumns()
  35. {
  36. return Columns.None<Supplier>()
  37. .Add(x => x.ID)
  38. .Add(x => x.PostedReference)
  39. .Add(x => x.Name)
  40. .Add(x => x.Code)
  41. .Add(x => x.SupplierStatus.ID)
  42. .Add(x => x.SupplierStatus.Active)
  43. .Add(x => x.Postal.Street)
  44. .Add(x => x.Postal.City)
  45. .Add(x => x.Postal.State)
  46. .Add(x => x.Postal.PostCode)
  47. .Add(x => x.Email)
  48. .Add(x => x.Telephone)
  49. .Add(x => x.ABN);
  50. }
  51. public static Result<Exception> UpdateSupplier(MYOBConnectionData data, SupplierMYOBPosterSettings settings, Supplier supplier, MYOBSupplier myobSupplier, bool isNew)
  52. {
  53. // Documentation: https://developer.myob.com/api/myob-business-api/v2/contact/supplier/
  54. // Since this might be called from some other poster, we need to ensure we have the right columns.
  55. Client.EnsureColumns(supplier, RequiredColumns());
  56. // ContactMYOBUtils.SplitName(supplier.DefaultContact.Name, out var firstName, out var lastName);
  57. myobSupplier.CompanyName = supplier.Name.Truncate(50);
  58. // myobSupplier.FirstName =
  59. // myobSupplier.LastName =
  60. myobSupplier.IsIndividual = false;
  61. myobSupplier.DisplayID = supplier.Code.Truncate(15);
  62. // If there is not customer status, we will use default to Active = true.
  63. myobSupplier.IsActive = supplier.SupplierStatus.ID == Guid.Empty || supplier.SupplierStatus.Active;
  64. myobSupplier.Addresses =
  65. [
  66. ContactMYOBUtils.ConvertAddress(supplier.Postal, 1, new Contact
  67. {
  68. Email = supplier.Email,
  69. Telephone = supplier.Telephone
  70. })
  71. ];
  72. // Notes =
  73. // PhotoURI =
  74. // RowVersion =
  75. myobSupplier.BuyingDetails ??= new();
  76. // myobSupplier.BuyingDetails.PurchaseLayout =
  77. // myobCustomer.BuyingDetails.PrintedForm =
  78. // myobSupplier.BuyingDetails.PurchaseOrderDelivery =
  79. // myobCustomer.BuyingDetails.ExpenseAccount.UID =
  80. // myobCustomer.BuyingDetails.PaymentMemo =
  81. // myobCustomer.BuyingDetails.PurchaseComment =
  82. // myobCustomer.BuyingDetails.SupplierBillingRate =
  83. // myobCustomer.BuyingDetails.ShippingMethod =
  84. // myobCustomer.BuyingDetails.IsReportable =
  85. // myobCustomer.BuyingDetails.CostPerHour =
  86. // myobCustomer.BuyingDetails.Credit.Limit =
  87. myobSupplier.BuyingDetails.ABN = supplier.ABN.Truncate(14);
  88. // myobCustomer.BuyingDetails.ABNBranch
  89. // myobCustomer.BuyingDetails.TaxIdNumber
  90. if (isNew)
  91. {
  92. if (settings.DefaultTaxCode.IsNullOrWhiteSpace())
  93. {
  94. throw new PostFailedMessageException("Default tax code has not been set up.");
  95. }
  96. else if(data.GetMYOBTaxCodeUID(settings.DefaultTaxCode).Get(out var taxID, out var error))
  97. {
  98. if (taxID == Guid.Empty)
  99. {
  100. return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code {settings.DefaultTaxCode}"));
  101. }
  102. myobSupplier.BuyingDetails.TaxCode ??= new();
  103. myobSupplier.BuyingDetails.TaxCode.UID = taxID;
  104. myobSupplier.BuyingDetails.FreightTaxCode ??= new();
  105. myobSupplier.BuyingDetails.FreightTaxCode.UID = taxID;
  106. }
  107. else
  108. {
  109. CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {settings.DefaultTaxCode}");
  110. return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code {settings.DefaultTaxCode}: {error.Message}", error));
  111. }
  112. }
  113. // myobCustomer.BuyingDetails.UseSupplierTaxCode
  114. // myobCustomer.BuyingDetails.Terms
  115. // myobCustomer.PaymentDetails
  116. // myobCustomer.PhotoURI
  117. return Result.Ok();
  118. }
  119. /// <summary>
  120. /// Try to find a supplier in MYOB which matches <paramref name="supplier"/>, and if this fails, create a new one.
  121. /// </summary>
  122. /// <remarks>
  123. /// After this has finished, <paramref name="supplier"/> will be updated with <see cref="Supplier.PostedReference"/> set to the correct ID.
  124. /// <br/>
  125. /// <paramref name="supplier"/> needs to have at least <see cref="Supplier.Code"/> and <see cref="Supplier.PostedReference"/> as loaded columns.
  126. /// </remarks>
  127. /// <param name="data"></param>
  128. /// <param name="supplier">The supplier to map to.</param>
  129. /// <returns>The UID of the MYOB supplier.</returns>
  130. public static Result<Guid, Exception> MapSupplier(MYOBConnectionData data, Supplier supplier)
  131. {
  132. if(Guid.TryParse(supplier.PostedReference, out var myobID))
  133. {
  134. return Result.Ok(myobID);
  135. }
  136. var service = new SupplierService(data.Configuration, null, data.AuthKey);
  137. var result = service.Query(data, new Filter<MYOBSupplier>(x => x.DisplayID).IsEqualTo(supplier.Code), top: 1);
  138. return result.MapOk(suppliers =>
  139. {
  140. if(suppliers.Count == 0)
  141. {
  142. if(supplier.Code.Length > 15)
  143. {
  144. return Result.Error(new Exception("Customer code is longer than 15 characters"));
  145. }
  146. var myobSupplier = new MYOBSupplier();
  147. return UpdateSupplier(data, PosterUtils.LoadPosterSettings<Supplier, SupplierMYOBPosterSettings>(), supplier, myobSupplier, true)
  148. .MapOk(() => service.Save(data, myobSupplier).MapOk(x => x.UID)).Flatten();
  149. }
  150. else
  151. {
  152. supplier.PostedReference = suppliers.Items[0].UID.ToString();
  153. return Result.Ok(suppliers.Items[0].UID);
  154. }
  155. }).Flatten();
  156. }
  157. public IPostResult<Supplier> Process(IDataModel<Supplier> model)
  158. {
  159. var results = new PostResult<Supplier>();
  160. var service = new SupplierService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
  161. var suppliers = model.GetTable<Supplier>().ToArray<Supplier>();
  162. foreach(var supplier in suppliers)
  163. {
  164. if(supplier.Code.Length > 15)
  165. {
  166. results.AddFailed(supplier, "Code is longer than 15 characters.");
  167. continue;
  168. }
  169. bool isNew;
  170. MYOBSupplier myobSupplier;
  171. Exception? error;
  172. if(Guid.TryParse(supplier.PostedReference, out var myobID))
  173. {
  174. if(!service.Get(ConnectionData, myobID).Get(out var newSupplier, out error))
  175. {
  176. CoreUtils.LogException("", error, $"Failed to find Supplier in MYOB with id {myobID}");
  177. results.AddFailed(supplier, $"Failed to find Supplier in MYOB with id {myobID}: {error.Message}");
  178. continue;
  179. }
  180. myobSupplier = newSupplier;
  181. isNew = false;
  182. }
  183. else
  184. {
  185. if(service.Query(ConnectionData, new Filter<MYOBSupplier>(x => x.DisplayID).IsEqualTo(supplier.Code)).Get(out var myobSuppliers, out error))
  186. {
  187. if(myobSuppliers.Items.Length > 0)
  188. {
  189. myobSupplier = myobSuppliers.Items[0];
  190. isNew = false;
  191. }
  192. else
  193. {
  194. myobSupplier = new MYOBSupplier();
  195. isNew = true;
  196. }
  197. }
  198. else
  199. {
  200. CoreUtils.LogException("", error);
  201. results.AddFailed(supplier, error.Message);
  202. continue;
  203. }
  204. myobSupplier = new MYOBSupplier();
  205. isNew = true;
  206. }
  207. if(UpdateSupplier(ConnectionData, Settings, supplier, myobSupplier, isNew)
  208. .MapOk(() => service.Save(ConnectionData, myobSupplier)).Flatten()
  209. .Get(out var result, out error))
  210. {
  211. supplier.PostedReference = result.UID.ToString();
  212. results.AddSuccess(supplier);
  213. }
  214. else
  215. {
  216. CoreUtils.LogException("", error, $"Error while posting supplier {supplier.ID}");
  217. results.AddFailed(supplier, error.Message);
  218. }
  219. }
  220. return results;
  221. }
  222. }
  223. public class SupplierMYOBPosterEngine<T> : MYOBPosterEngine<Supplier, SupplierMYOBPosterSettings> { }