Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve VAT obligations
        /// </summary>
        /// <returns></returns>
        public obligationResponse Obligations(obligationsRequest request, string testScenario = null)
        {
            RefreshAccessToken();
            string             url         = urlSite + _urlObligations.Replace("{vrn}", vrn) + "?from=" + request.from?.ToString("yyyy-MM-dd") + "&to=" + request.to?.ToString("yyyy-MM-dd");
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);

            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(_acceptHeader));
            httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);

            foreach (var item in fraudHeaders)
            {
                httpRequest.Headers.Add(item.Key, item.Value);
            }

            if (!String.IsNullOrEmpty(testScenario))
            {
                httpRequest.Headers.Add("Gov-Test-Scenario", testScenario);
            }
            obligationResponse obligationResponse = null;

            using (var httpClient = new HttpClient())
            {
                HttpResponseMessage response = httpClient.SendAsync(httpRequest).Result;
                checkApiResponse(response, VATApiType.RetrieveVATobligations);

                var content = response.Content.ReadAsStringAsync();
                obligationResponse = JsonConvert.DeserializeObject <obligationResponse>(content.Result);
            }

            return(obligationResponse);
        }
Ejemplo n.º 2
0
        public static void GetVATObligationsProc(VATMaint graph, DateTime from, DateTime to, string status = null)
        {
            obligationsRequest req = new obligationsRequest()
            {
                from = from, to = to, status = status
            };
            obligationResponse obligationResponse = null;

            try
            {
                obligationResponse = graph.VATProvider.Obligations(req);
            }
            catch (Exceptions.VATAPIInvalidToken eToken)
            {
                PXTrace.WriteError(eToken);
                graph.signInHMRC.SetEnabled(true);
                throw new PXException(Messages.PleaseAuthorize);
            }
            catch (Exceptions.VATAPIException eApi)
            {
                PXTrace.WriteError(eApi);
                if (eApi.Data.Contains("json"))
                {
                    PXTrace.WriteError(eApi.Data["json"].ToString());
                }
                if (eApi.Code != error.MATCHING_RESOURCE_NOT_FOUND)
                {
                    throw eApi;
                }
            }
            catch (Exception e)
            {
                PXTrace.WriteError(e);
                throw e;
            }

            graph.Obligations.Cache.Clear();
            if (obligationResponse != null)
            {
                foreach (var o in obligationResponse.obligations)
                {
                    graph.Obligations.Insert(new Obligation()
                    {
                        Start     = o.start,
                        End       = o.end,
                        Due       = o.due,
                        Status    = o.status,
                        PeriodKey = o.periodKey,
                        Received  = o.received
                    });
                }
            }
            return;
        }