PRSMYOBPosterUtils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Result<Guid?, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, string code)
  83. {
  84. var service = new TaxCodeService(data.Configuration, null, data.AuthKey);
  85. var result = service.Query(data, new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(code), top: 1);
  86. if(!result.Get(out var myobCodes, out var error))
  87. {
  88. return Result.Error<Guid?, Exception>(error);
  89. }
  90. if(myobCodes.Count == 0)
  91. {
  92. return Result.Ok<Guid?, Exception>(Guid.Empty);
  93. }
  94. return Result.Ok<Guid?, Exception>(myobCodes.Items[0].UID);
  95. }
  96. public static Result<Guid?, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, ITaxCode code)
  97. {
  98. if(Guid.TryParse(code.PostedReference, out var id))
  99. {
  100. return Result.Ok<Guid?, Exception>(id);
  101. }
  102. return GetMYOBTaxCodeUID(data, code.Code);
  103. }
  104. }