SupplierMYOBPoster.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.PurchaseLayout =
  76. // myobCustomer.BuyingDetails.PrintedForm =
  77. // myobSupplier.BuyingDetails.PurchaseOrderDelivery =
  78. // myobCustomer.BuyingDetails.ExpenseAccount.UID =
  79. // myobCustomer.BuyingDetails.PaymentMemo =
  80. // myobCustomer.BuyingDetails.PurchaseComment =
  81. // myobCustomer.BuyingDetails.SupplierBillingRate =
  82. // myobCustomer.BuyingDetails.ShippingMethod =
  83. // myobCustomer.BuyingDetails.IsReportable =
  84. // myobCustomer.BuyingDetails.CostPerHour =
  85. // myobCustomer.BuyingDetails.Credit.Limit =
  86. myobSupplier.BuyingDetails.ABN = supplier.ABN.Truncate(14);
  87. // myobCustomer.BuyingDetails.ABNBranch
  88. // myobCustomer.BuyingDetails.TaxIdNumber
  89. if (isNew)
  90. {
  91. if (settings.DefaultTaxCode.IsNullOrWhiteSpace())
  92. {
  93. throw new PostFailedMessageException("Default tax code has not been set up.");
  94. }
  95. else if(data.GetMYOBTaxCodeUID(settings.DefaultTaxCode).Get(out var taxID, out var error))
  96. {
  97. if (taxID == Guid.Empty)
  98. {
  99. return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code {settings.DefaultTaxCode}"));
  100. }
  101. myobSupplier.BuyingDetails.TaxCode.UID = taxID;
  102. myobSupplier.BuyingDetails.FreightTaxCode.UID = taxID;
  103. }
  104. else
  105. {
  106. CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code {settings.DefaultTaxCode}");
  107. return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code {settings.DefaultTaxCode}: {error.Message}", error));
  108. }
  109. }
  110. // myobCustomer.BuyingDetails.UseSupplierTaxCode
  111. // myobCustomer.BuyingDetails.Terms
  112. // myobCustomer.PaymentDetails
  113. // myobCustomer.PhotoURI
  114. return Result.Ok();
  115. }
  116. /// <summary>
  117. /// Try to find a supplier in MYOB which matches <paramref name="supplier"/>, and if this fails, create a new one.
  118. /// </summary>
  119. /// <remarks>
  120. /// After this has finished, <paramref name="supplier"/> will be updated with <see cref="Supplier.PostedReference"/> set to the correct ID.
  121. /// <br/>
  122. /// <paramref name="supplier"/> needs to have at least <see cref="Supplier.Code"/> and <see cref="Supplier.PostedReference"/> as loaded columns.
  123. /// </remarks>
  124. /// <param name="data"></param>
  125. /// <param name="supplier">The supplier to map to.</param>
  126. /// <returns>The UID of the MYOB supplier.</returns>
  127. public static Result<Guid, Exception> MapSupplier(MYOBConnectionData data, Supplier supplier)
  128. {
  129. if(Guid.TryParse(supplier.PostedReference, out var myobID))
  130. {
  131. return Result.Ok(myobID);
  132. }
  133. var service = new SupplierService(data.Configuration, null, data.AuthKey);
  134. var result = service.Query(data, new Filter<MYOBSupplier>(x => x.DisplayID).IsEqualTo(supplier.Code), top: 1);
  135. return result.MapOk(suppliers =>
  136. {
  137. if(suppliers.Count == 0)
  138. {
  139. if(supplier.Code.Length > 15)
  140. {
  141. return Result.Error(new Exception("Customer code is longer than 15 characters"));
  142. }
  143. var myobSupplier = new MYOBSupplier();
  144. return UpdateSupplier(data, PosterUtils.LoadPosterSettings<Supplier, SupplierMYOBPosterSettings>(), supplier, myobSupplier, true)
  145. .MapOk<Result<Guid, Exception>>(() =>
  146. {
  147. try
  148. {
  149. var result = service.UpdateEx(data.CompanyFile, myobSupplier, data.CompanyFileCredentials);
  150. supplier.PostedReference = result.UID.ToString();
  151. return Result.Ok(result.UID);
  152. }
  153. catch (Exception e)
  154. {
  155. CoreUtils.LogException("", e, $"Error while posting supplier {supplier.ID}");
  156. return Result.Error(e);
  157. }
  158. }).Flatten();
  159. }
  160. else
  161. {
  162. supplier.PostedReference = suppliers.Items[0].UID.ToString();
  163. return Result.Ok(suppliers.Items[0].UID);
  164. }
  165. }).Flatten();
  166. }
  167. public IPostResult<Supplier> Process(IDataModel<Supplier> model)
  168. {
  169. var results = new PostResult<Supplier>();
  170. var service = new SupplierService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
  171. var suppliers = model.GetTable<Supplier>().ToArray<Supplier>();
  172. foreach(var supplier in suppliers)
  173. {
  174. if(supplier.Code.Length > 15)
  175. {
  176. results.AddFailed(supplier, "Code is longer than 15 characters.");
  177. continue;
  178. }
  179. bool isNew;
  180. MYOBSupplier myobSupplier;
  181. Exception? error;
  182. if(Guid.TryParse(supplier.PostedReference, out var myobID))
  183. {
  184. if(!service.Get(ConnectionData, myobID).Get(out var newSupplier, out error))
  185. {
  186. CoreUtils.LogException("", error, $"Failed to find Supplier in MYOB with id {myobID}");
  187. results.AddFailed(supplier, $"Failed to find Supplier in MYOB with id {myobID}: {error.Message}");
  188. continue;
  189. }
  190. myobSupplier = newSupplier;
  191. isNew = false;
  192. }
  193. else
  194. {
  195. myobSupplier = new MYOBSupplier();
  196. isNew = true;
  197. }
  198. if(UpdateSupplier(ConnectionData, Settings, supplier, myobSupplier, isNew).Get(out error))
  199. {
  200. try
  201. {
  202. var result = service.UpdateEx(ConnectionData.CompanyFile, myobSupplier, ConnectionData.CompanyFileCredentials);
  203. supplier.PostedReference = result.UID.ToString();
  204. results.AddSuccess(supplier);
  205. }
  206. catch(Exception e)
  207. {
  208. CoreUtils.LogException("", e, $"Error while posting supplier {supplier.ID}");
  209. results.AddFailed(supplier, e.Message);
  210. }
  211. }
  212. else
  213. {
  214. results.AddFailed(supplier, error.Message);
  215. }
  216. }
  217. return results;
  218. }
  219. }
  220. public class SupplierMYOBPosterEngine<T> : MYOBPosterEngine<Supplier, SupplierMYOBPosterSettings> { }