PRSMYOBPosterUtils.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Poster.MYOB;
  4. using MYOB.AccountRight.SDK.Contracts.Version2;
  5. using MYOB.AccountRight.SDK.Services;
  6. using MYOB.AccountRight.SDK.Services.GeneralLedger;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using MYOBBaseEntity = MYOB.AccountRight.SDK.Contracts.Version2.BaseEntity;
  13. using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
  14. namespace PRS.Shared.Posters.MYOB;
  15. public static class PRSMYOBPosterUtils
  16. {
  17. public class QueryResult<T> : Result<PagedCollection<T>, Exception>
  18. {
  19. public QueryResult(PagedCollection<T> value) : base(value)
  20. {
  21. }
  22. public QueryResult(Exception error) : base(error)
  23. {
  24. }
  25. public static QueryResult<T> Ok(PagedCollection<T> collection) => new QueryResult<T>(collection);
  26. public static QueryResult<T> Error(Exception e) => new QueryResult<T>(e);
  27. }
  28. public class GetResult<T> : Result<T, Exception>
  29. {
  30. public GetResult(T value) : base(value)
  31. {
  32. }
  33. public GetResult(Exception error) : base(error)
  34. {
  35. }
  36. public static GetResult<T> Ok(T value) => new GetResult<T>(value);
  37. public static GetResult<T> Error(Exception e) => new GetResult<T>(e);
  38. }
  39. public static Result<PagedCollection<T>, Exception> Query<T>(
  40. this MutableService<T> service, MYOBConnectionData data,
  41. Filter<T>? filter,
  42. int? top = null, int? skip = null
  43. )
  44. where T : MYOBBaseEntity
  45. {
  46. var queries = new List<string>();
  47. if(filter is not null)
  48. {
  49. queries.Add($"$filter={Uri.EscapeDataString(filter.AsOData())}");
  50. }
  51. if (top.HasValue)
  52. {
  53. queries.Add($"$top={top.Value}");
  54. }
  55. if (skip.HasValue)
  56. {
  57. queries.Add($"$skip={skip.Value}");
  58. }
  59. try
  60. {
  61. var values = service.GetRange(data.CompanyFile, string.Join('&', queries), data.CompanyFileCredentials);
  62. return QueryResult<T>.Ok(values);
  63. }
  64. catch(Exception e)
  65. {
  66. return QueryResult<T>.Error(e);
  67. }
  68. }
  69. public static GetResult<T> Get<T>(this MutableService<T> service, MYOBConnectionData data, Guid id)
  70. where T : MYOBBaseEntity
  71. {
  72. try
  73. {
  74. var value = service.Get(data.CompanyFile, id, data.CompanyFileCredentials);
  75. return GetResult<T>.Ok(value);
  76. }
  77. catch(Exception e)
  78. {
  79. return GetResult<T>.Error(e);
  80. }
  81. }
  82. public static TService CreateService<TService, TEntity>(this MYOBConnectionData data)
  83. where TService : MutableService<TEntity>
  84. where TEntity : MYOBBaseEntity
  85. {
  86. return (Activator.CreateInstance(typeof(TService), data.Configuration, null, data.AuthKey) as TService)!;
  87. }
  88. public static Result<Guid, Exception> GetUID<TService, TEntity>(this MYOBConnectionData data, Filter<TEntity> filter)
  89. where TService : MutableService<TEntity>
  90. where TEntity : MYOBBaseEntity
  91. {
  92. var service = data.CreateService<TService, TEntity>();
  93. var result = service.Query(data, filter, top: 1);
  94. if(!result.Get(out var items, out var error))
  95. {
  96. return Result.Error(error);
  97. }
  98. if(items.Count == 0)
  99. {
  100. return Result.Ok(Guid.Empty);
  101. }
  102. return Result.Ok(items.Items[0].UID);
  103. }
  104. public static Result<Guid, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, string code)
  105. => data.GetUID<TaxCodeService, MYOBTaxCode>(new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(code));
  106. public static Result<Guid, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, ITaxCode code)
  107. {
  108. if(Guid.TryParse(code.PostedReference, out var id))
  109. {
  110. return Result.Ok(id);
  111. }
  112. return GetMYOBTaxCodeUID(data, code.Code);
  113. }
  114. }