MYOBPosterUtils.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using InABox.Core;
  2. using InABox.Core.Postable;
  3. using InABox.Poster.Shared;
  4. using MYOB.AccountRight.SDK;
  5. using MYOB.AccountRight.SDK.Contracts.Version2;
  6. using MYOB.AccountRight.SDK.Services;
  7. using MYOB.AccountRight.SDK.Services.GeneralLedger;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using MYOBBaseEntity = MYOB.AccountRight.SDK.Contracts.Version2.BaseEntity;
  14. using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
  15. namespace InABox.Poster.MYOB;
  16. public static class MYOBPosterUtils
  17. {
  18. public class QueryResult<T> : Result<PagedCollection<T>, Exception>
  19. {
  20. public QueryResult(PagedCollection<T> value) : base(value)
  21. {
  22. }
  23. public QueryResult(Exception error) : base(error)
  24. {
  25. }
  26. public static QueryResult<T> Ok(PagedCollection<T> collection) => new QueryResult<T>(collection);
  27. public static QueryResult<T> Error(Exception e) => new QueryResult<T>(e);
  28. }
  29. public class GetResult<T> : Result<T, Exception>
  30. {
  31. public GetResult(T value) : base(value)
  32. {
  33. }
  34. public GetResult(Exception error) : base(error)
  35. {
  36. }
  37. public static GetResult<T> Ok(T value) => new GetResult<T>(value);
  38. public static GetResult<T> Error(Exception e) => new GetResult<T>(e);
  39. }
  40. public static Result<PagedCollection<T>, Exception> Query<T>(
  41. this MutableService<T> service, MYOBConnectionData data,
  42. Filter<T>? filter,
  43. int? top = null, int? skip = null
  44. )
  45. where T : MYOBBaseEntity
  46. {
  47. var queries = new List<string>();
  48. if(filter is not null)
  49. {
  50. queries.Add($"$filter={Uri.EscapeDataString(filter.AsOData())}");
  51. }
  52. if (top.HasValue)
  53. {
  54. queries.Add($"$top={top.Value}");
  55. }
  56. if (skip.HasValue)
  57. {
  58. queries.Add($"$skip={skip.Value}");
  59. }
  60. try
  61. {
  62. var values = service.GetRange(data.CompanyFile, string.Join('&', queries), data.CompanyFileCredentials);
  63. return QueryResult<T>.Ok(values);
  64. }
  65. catch(ApiCommunicationException e)
  66. {
  67. return QueryResult<T>.Error(new Exception(FormatApiException(e), e));
  68. }
  69. catch(Exception e)
  70. {
  71. return QueryResult<T>.Error(e);
  72. }
  73. }
  74. public static Result<T, Exception> Save<T>(
  75. this MutableService<T> service, MYOBConnectionData data,
  76. T entity)
  77. where T : MYOBBaseEntity
  78. {
  79. try
  80. {
  81. if(entity.UID == Guid.Empty)
  82. {
  83. return Result.Ok(service.InsertEx(data.CompanyFile, entity, data.CompanyFileCredentials));
  84. }
  85. else
  86. {
  87. return Result.Ok(service.UpdateEx(data.CompanyFile, entity, data.CompanyFileCredentials));
  88. }
  89. }
  90. catch(ApiCommunicationException e)
  91. {
  92. return Result.Error(new Exception(FormatApiException(e), e));
  93. }
  94. catch(Exception e)
  95. {
  96. return Result.Error(e);
  97. }
  98. }
  99. public static GetResult<T> Get<T>(this MutableService<T> service, MYOBConnectionData data, Guid id)
  100. where T : MYOBBaseEntity
  101. {
  102. try
  103. {
  104. var value = service.Get(data.CompanyFile, id, data.CompanyFileCredentials);
  105. return GetResult<T>.Ok(value);
  106. }
  107. catch(ApiCommunicationException e)
  108. {
  109. return GetResult<T>.Error(new Exception(FormatApiException(e), e));
  110. }
  111. catch(Exception e)
  112. {
  113. return GetResult<T>.Error(e);
  114. }
  115. }
  116. public static TService CreateService<TService, TEntity>(this MYOBConnectionData data)
  117. where TService : MutableService<TEntity>
  118. where TEntity : MYOBBaseEntity
  119. {
  120. return (Activator.CreateInstance(typeof(TService), data.Configuration, null, data.AuthKey) as TService)!;
  121. }
  122. public static Result<Guid, Exception> GetUID<TService, TEntity>(this MYOBConnectionData data, Filter<TEntity> filter)
  123. where TService : MutableService<TEntity>
  124. where TEntity : MYOBBaseEntity
  125. {
  126. var service = data.CreateService<TService, TEntity>();
  127. var result = service.Query(data, filter, top: 1);
  128. if(!result.Get(out var items, out var error))
  129. {
  130. return Result.Error(error);
  131. }
  132. if(items.Items.Length == 0)
  133. {
  134. return Result.Ok(Guid.Empty);
  135. }
  136. return Result.Ok(items.Items[0].UID);
  137. }
  138. public static string FormatApiException(ApiCommunicationException e)
  139. {
  140. if(e.Errors.Count > 0)
  141. {
  142. var message = string.Join('\n', e.Errors.Select(x =>
  143. {
  144. return $"{x.Name}: {x.Message} ({x.AdditionalDetails})";
  145. }));
  146. return message;
  147. }
  148. else
  149. {
  150. return e.Message;
  151. }
  152. }
  153. public static Result<Guid, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, string code)
  154. => data.GetUID<TaxCodeService, MYOBTaxCode>(new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(code));
  155. public static Result<Guid, Exception> GetDefaultTaxCode(MYOBConnectionData data, MYOBGlobalPosterSettings settings)
  156. {
  157. if (settings.DefaultTaxCode.IsNullOrWhiteSpace())
  158. {
  159. throw new MissingSettingException<MYOBGlobalPosterSettings>(x => x.DefaultTaxCode);
  160. }
  161. else if(data.GetMYOBTaxCodeUID(settings.DefaultTaxCode).Get(out var taxID, out var error))
  162. {
  163. if (taxID == Guid.Empty)
  164. {
  165. return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code '{settings.DefaultTaxCode}'"));
  166. }
  167. return Result.Ok(taxID);
  168. }
  169. else
  170. {
  171. CoreUtils.LogException("", error, $"Failed to find TaxCode in MYOB with code '{settings.DefaultTaxCode}'");
  172. return Result.Error(new Exception($"Failed to find TaxCode in MYOB with code '{settings.DefaultTaxCode}': {error.Message}", error));
  173. }
  174. }
  175. public static Result<Guid, Exception> GetDefaultTaxCode<TPostable, TSettings>(this IMYOBPoster<TPostable, TSettings> poster)
  176. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  177. where TSettings : MYOBPosterSettings
  178. {
  179. return GetDefaultTaxCode(poster.ConnectionData, poster.GlobalSettings);
  180. }
  181. public static Result<Exception> WrapScript<TPostable, TSettings>(
  182. this IMYOBPoster<TPostable, TSettings> poster,
  183. string methodname,
  184. params object?[] parameters
  185. )
  186. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  187. where TSettings : MYOBPosterSettings
  188. {
  189. if (poster.Script is null) return Result.Ok();
  190. try
  191. {
  192. if(poster.Script.Execute(methodname: methodname, parameters: parameters))
  193. {
  194. return Result.Ok();
  195. }
  196. else
  197. {
  198. throw new PostCancelledException();
  199. }
  200. }
  201. catch(Exception e)
  202. {
  203. return Result.Error(e);
  204. }
  205. }
  206. }