123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.Poster.MYOB;
- using MYOB.AccountRight.SDK.Contracts.Version2;
- using MYOB.AccountRight.SDK.Services;
- using MYOB.AccountRight.SDK.Services.GeneralLedger;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MYOBBaseEntity = MYOB.AccountRight.SDK.Contracts.Version2.BaseEntity;
- using MYOBTaxCode = MYOB.AccountRight.SDK.Contracts.Version2.GeneralLedger.TaxCode;
- namespace PRS.Shared.Posters.MYOB;
- public static class PRSMYOBPosterUtils
- {
- public class QueryResult<T> : Result<PagedCollection<T>, Exception>
- {
- public QueryResult(PagedCollection<T> value) : base(value)
- {
- }
- public QueryResult(Exception error) : base(error)
- {
- }
- public static QueryResult<T> Ok(PagedCollection<T> collection) => new QueryResult<T>(collection);
- public static QueryResult<T> Error(Exception e) => new QueryResult<T>(e);
- }
- public class GetResult<T> : Result<T, Exception>
- {
- public GetResult(T value) : base(value)
- {
- }
- public GetResult(Exception error) : base(error)
- {
- }
- public static GetResult<T> Ok(T value) => new GetResult<T>(value);
- public static GetResult<T> Error(Exception e) => new GetResult<T>(e);
- }
- public static Result<PagedCollection<T>, Exception> Query<T>(
- this MutableService<T> service, MYOBConnectionData data,
- Filter<T>? filter,
- int? top = null, int? skip = null
- )
- where T : MYOBBaseEntity
- {
- var queries = new List<string>();
- if(filter is not null)
- {
- queries.Add($"$filter={Uri.EscapeDataString(filter.AsOData())}");
- }
- if (top.HasValue)
- {
- queries.Add($"$top={top.Value}");
- }
- if (skip.HasValue)
- {
- queries.Add($"$skip={skip.Value}");
- }
- try
- {
- var values = service.GetRange(data.CompanyFile, string.Join('&', queries), data.CompanyFileCredentials);
- return QueryResult<T>.Ok(values);
- }
- catch(Exception e)
- {
- return QueryResult<T>.Error(e);
- }
- }
- public static GetResult<T> Get<T>(this MutableService<T> service, MYOBConnectionData data, Guid id)
- where T : MYOBBaseEntity
- {
- try
- {
- var value = service.Get(data.CompanyFile, id, data.CompanyFileCredentials);
- return GetResult<T>.Ok(value);
- }
- catch(Exception e)
- {
- return GetResult<T>.Error(e);
- }
- }
- public static Result<Guid?, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, string code)
- {
- var service = new TaxCodeService(data.Configuration, null, data.AuthKey);
- var result = service.Query(data, new Filter<MYOBTaxCode>(x => x.Code).IsEqualTo(code), top: 1);
- if(!result.Get(out var myobCodes, out var error))
- {
- return Result.Error<Guid?, Exception>(error);
- }
- if(myobCodes.Count == 0)
- {
- return Result.Ok<Guid?, Exception>(Guid.Empty);
- }
- return Result.Ok<Guid?, Exception>(myobCodes.Items[0].UID);
- }
- public static Result<Guid?, Exception> GetMYOBTaxCodeUID(this MYOBConnectionData data, ITaxCode code)
- {
- if(Guid.TryParse(code.PostedReference, out var id))
- {
- return Result.Ok<Guid?, Exception>(id);
- }
- return GetMYOBTaxCodeUID(data, code.Code);
- }
- }
|