PRSMYOBPosterUtils.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Poster.MYOB;
  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 PRS.Shared.Posters.MYOB;
  16. public static class PRSMYOBPosterUtils
  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 T Save<T>(
  75. this MutableService<T> service, MYOBConnectionData data,
  76. T entity)
  77. where T : MYOBBaseEntity
  78. {
  79. if(entity.UID == Guid.Empty)
  80. {
  81. return service.InsertEx(data.CompanyFile, entity, data.CompanyFileCredentials);
  82. }
  83. else
  84. {
  85. return service.UpdateEx(data.CompanyFile, entity, data.CompanyFileCredentials);
  86. }
  87. }
  88. public static GetResult<T> Get<T>(this MutableService<T> service, MYOBConnectionData data, Guid id)
  89. where T : MYOBBaseEntity
  90. {
  91. try
  92. {
  93. var value = service.Get(data.CompanyFile, id, data.CompanyFileCredentials);
  94. return GetResult<T>.Ok(value);
  95. }
  96. catch(ApiCommunicationException e)
  97. {
  98. return GetResult<T>.Error(new Exception(FormatApiException(e), e));
  99. }
  100. catch(Exception e)
  101. {
  102. return GetResult<T>.Error(e);
  103. }
  104. }
  105. public static TService CreateService<TService, TEntity>(this MYOBConnectionData data)
  106. where TService : MutableService<TEntity>
  107. where TEntity : MYOBBaseEntity
  108. {
  109. return (Activator.CreateInstance(typeof(TService), data.Configuration, null, data.AuthKey) as TService)!;
  110. }
  111. public static Result<Guid, Exception> GetUID<TService, TEntity>(this MYOBConnectionData data, Filter<TEntity> filter)
  112. where TService : MutableService<TEntity>
  113. where TEntity : MYOBBaseEntity
  114. {
  115. var service = data.CreateService<TService, TEntity>();
  116. var result = service.Query(data, filter, top: 1);
  117. if(!result.Get(out var items, out var error))
  118. {
  119. return Result.Error(error);
  120. }
  121. if(items.Items.Length == 0)
  122. {
  123. return Result.Ok(Guid.Empty);
  124. }
  125. return Result.Ok(items.Items[0].UID);
  126. }
  127. public static Result<Guid, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, string code)
  128. => data.GetUID<TaxCodeService, MYOBTaxCode>(new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(code));
  129. public static Result<Guid, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, ITaxCode code)
  130. {
  131. if(Guid.TryParse(code.PostedReference, out var id))
  132. {
  133. return Result.Ok(id);
  134. }
  135. return GetMYOBTaxCodeUID(data, code.Code);
  136. }
  137. public static string FormatApiException(ApiCommunicationException e)
  138. {
  139. if(e.Errors.Count > 0)
  140. {
  141. var message = string.Join('\n', e.Errors.Select(x =>
  142. {
  143. return $"{x.Name}: {x.Message} ({x.AdditionalDetails})";
  144. }));
  145. return message;
  146. }
  147. else
  148. {
  149. return e.Message;
  150. }
  151. }
  152. }