Chilkat Online Tools

SQL Server / New FreshBooks / Sales Tax Summary

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @queryParams int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'start_date', '2019-01-01'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'end_date', '2019-05-01'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'currency_code', 'USD'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'cash_based', 'true'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/taxsummary', @queryParams
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @queryParams
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @queryParams
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "response": {
    --     "result": {
    --       "taxsummary": {
    --         "cash_based": true,
    --         "currency_code": "USD",
    --         "download_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50aWQiOiJ3a01kMmciLCJ1c2VyaWQiOjEsImNvbnRhY3RpZCI6LTEsImxldmVsIjozLCJ0eXBlIjoicmVwb3J0Iiwib2JqZWN0aWQiOiJ0YXhzdW1tYXJ5IiwicGFyYW1zIjp7InN0YXJ0X2RhdGUiOiIyMDE5LTAxLTAxIiwiZW5kX2RhdGUiOiIyMDE5LTA1LTAxIiwiY3VycmVuY3lfY29kZSI6IlVTRCIsImNhc2hfYmFzZWQiOnRydWV9LCJpYXQiOjE1NTYxMzA2MTksImV4cCI6MTU1ODcyMjYxOX0.7Ewyvilpqg_TzcPjGm70jfkLZhg-jAm6N0i5QJICrl8",
    --         "end_date": "2019-05-01",
    --         "start_date": "2019-01-01",
    --         "taxes": [
    --           {
    --             "net_tax": {
    --               "amount": "-659.42",
    --               "code": "USD"
    --             },
    --             "net_taxable_amount": {
    --               "amount": "-4815.57",
    --               "code": "USD"
    --             },
    --             "tax_collected": {
    --               "amount": "146.00",
    --               "code": "USD"
    --             },
    --             "tax_name": "HST",
    --             "tax_paid": {
    --               "amount": "805.42",
    --               "code": "USD"
    --             },
    --             "taxable_amount_collected": {
    --               "amount": "1380.00",
    --               "code": "USD"
    --             },
    --             "taxable_amount_paid": {
    --               "amount": "6195.57",
    --               "code": "USD"
    --             }
    --           },
    --           {
    --             "net_tax": {
    --               "amount": "-18.34",
    --               "code": "USD"
    --             },
    --             "net_taxable_amount": {
    --               "amount": "-141.12",
    --               "code": "USD"
    --             },
    --             "tax_collected": {
    --               "amount": "0.00",
    --               "code": "USD"
    --             },
    --             "tax_name": "other tax",
    --             "tax_paid": {
    --               "amount": "18.34",
    --               "code": "USD"
    --             },
    --             "taxable_amount_collected": {
    --               "amount": "0.00",
    --               "code": "USD"
    --             },
    --             "taxable_amount_paid": {
    --               "amount": "141.12",
    --               "code": "USD"
    --             }
    --           }
    --         ],
    --         "total_invoiced": {
    --           "amount": "9891.00",
    --           "code": "USD"
    --         }
    --       }
    --     }
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @net_taxAmount nvarchar(4000)

    DECLARE @net_taxCode nvarchar(4000)

    DECLARE @net_taxable_amountAmount nvarchar(4000)

    DECLARE @net_taxable_amountCode nvarchar(4000)

    DECLARE @tax_collectedAmount nvarchar(4000)

    DECLARE @tax_collectedCode nvarchar(4000)

    DECLARE @tax_name nvarchar(4000)

    DECLARE @tax_paidAmount nvarchar(4000)

    DECLARE @tax_paidCode nvarchar(4000)

    DECLARE @taxable_amount_collectedAmount nvarchar(4000)

    DECLARE @taxable_amount_collectedCode nvarchar(4000)

    DECLARE @taxable_amount_paidAmount nvarchar(4000)

    DECLARE @taxable_amount_paidCode nvarchar(4000)

    DECLARE @Cash_based int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Cash_based OUT, 'response.result.taxsummary.cash_based'
    DECLARE @Currency_code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Currency_code OUT, 'response.result.taxsummary.currency_code'
    DECLARE @Download_token nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Download_token OUT, 'response.result.taxsummary.download_token'
    DECLARE @End_date nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @End_date OUT, 'response.result.taxsummary.end_date'
    DECLARE @Start_date nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Start_date OUT, 'response.result.taxsummary.start_date'
    DECLARE @Amount nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Amount OUT, 'response.result.taxsummary.total_invoiced.amount'
    DECLARE @Code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'response.result.taxsummary.total_invoiced.code'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.taxsummary.taxes'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @net_taxAmount OUT, 'response.result.taxsummary.taxes[i].net_tax.amount'
        EXEC sp_OAMethod @jResp, 'StringOf', @net_taxCode OUT, 'response.result.taxsummary.taxes[i].net_tax.code'
        EXEC sp_OAMethod @jResp, 'StringOf', @net_taxable_amountAmount OUT, 'response.result.taxsummary.taxes[i].net_taxable_amount.amount'
        EXEC sp_OAMethod @jResp, 'StringOf', @net_taxable_amountCode OUT, 'response.result.taxsummary.taxes[i].net_taxable_amount.code'
        EXEC sp_OAMethod @jResp, 'StringOf', @tax_collectedAmount OUT, 'response.result.taxsummary.taxes[i].tax_collected.amount'
        EXEC sp_OAMethod @jResp, 'StringOf', @tax_collectedCode OUT, 'response.result.taxsummary.taxes[i].tax_collected.code'
        EXEC sp_OAMethod @jResp, 'StringOf', @tax_name OUT, 'response.result.taxsummary.taxes[i].tax_name'
        EXEC sp_OAMethod @jResp, 'StringOf', @tax_paidAmount OUT, 'response.result.taxsummary.taxes[i].tax_paid.amount'
        EXEC sp_OAMethod @jResp, 'StringOf', @tax_paidCode OUT, 'response.result.taxsummary.taxes[i].tax_paid.code'
        EXEC sp_OAMethod @jResp, 'StringOf', @taxable_amount_collectedAmount OUT, 'response.result.taxsummary.taxes[i].taxable_amount_collected.amount'
        EXEC sp_OAMethod @jResp, 'StringOf', @taxable_amount_collectedCode OUT, 'response.result.taxsummary.taxes[i].taxable_amount_collected.code'
        EXEC sp_OAMethod @jResp, 'StringOf', @taxable_amount_paidAmount OUT, 'response.result.taxsummary.taxes[i].taxable_amount_paid.amount'
        EXEC sp_OAMethod @jResp, 'StringOf', @taxable_amount_paidCode OUT, 'response.result.taxsummary.taxes[i].taxable_amount_paid.code'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @queryParams
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -G -d "start_date=2019-01-01"
	-d "end_date=2019-05-01"
	-d "currency_code=USD"
	-d "cash_based=true"
	-H "Authorization: Bearer <access_token>"
https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/taxsummary

Postman Collection Item JSON

{
  "name": "Sales Tax Summary",
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/taxsummary?start_date=2019-01-01&end_date=2019-05-01&currency_code=USD&cash_based=true",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "reports",
        "accounting",
        "taxsummary"
      ],
      "query": [
        {
          "key": "start_date",
          "value": "2019-01-01",
          "description": "created during or after the given date i.e. `2019-01-01`"
        },
        {
          "key": "end_date",
          "value": "2019-05-01",
          "description": "created during or before the given date i.e. `2019-05-01`"
        },
        {
          "key": "currency_code",
          "value": "USD",
          "description": "reports using the given currency as primary currency i.e. `\"USD\"`"
        },
        {
          "key": "cash_based",
          "value": "true",
          "description": "True ignores billed payments and uses just collected payments i.e. `true`, `false`"
        }
      ]
    }
  },
  "response": [
    {
      "name": "Sales Tax Summary",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/taxsummary?start_date=2019-01-01&end_date=2019-05-01&currency_code=USD&cash_based=true",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "reports",
            "accounting",
            "taxsummary"
          ],
          "query": [
            {
              "key": "start_date",
              "value": "2019-01-01",
              "description": "created during or after the given date i.e. `2019-01-01`"
            },
            {
              "key": "end_date",
              "value": "2019-05-01",
              "description": "created during or before the given date i.e. `2019-05-01`"
            },
            {
              "key": "currency_code",
              "value": "USD",
              "description": "reports using the given currency as primary currency i.e. `\"USD\"`"
            },
            {
              "key": "cash_based",
              "value": "true",
              "description": "True ignores billed payments and uses just collected payments i.e. `true`, `false`"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gWVEZcEBIQWFEBWw1NVkBbXQJLBEEOQRYXFBtRSlMWAwldUVMPAgRTVwUEB1JRUVEcTwlOGghQUAJQAVQIUwsHAFAHUwRBFFVRCBIHag=="
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:30:19 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Age",
          "value": "0"
        },
        {
          "key": "Age",
          "value": "0"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:30:19 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"taxsummary\": {\n                \"cash_based\": true,\n                \"currency_code\": \"USD\",\n                \"download_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50aWQiOiJ3a01kMmciLCJ1c2VyaWQiOjEsImNvbnRhY3RpZCI6LTEsImxldmVsIjozLCJ0eXBlIjoicmVwb3J0Iiwib2JqZWN0aWQiOiJ0YXhzdW1tYXJ5IiwicGFyYW1zIjp7InN0YXJ0X2RhdGUiOiIyMDE5LTAxLTAxIiwiZW5kX2RhdGUiOiIyMDE5LTA1LTAxIiwiY3VycmVuY3lfY29kZSI6IlVTRCIsImNhc2hfYmFzZWQiOnRydWV9LCJpYXQiOjE1NTYxMzA2MTksImV4cCI6MTU1ODcyMjYxOX0.7Ewyvilpqg_TzcPjGm70jfkLZhg-jAm6N0i5QJICrl8\",\n                \"end_date\": \"2019-05-01\",\n                \"start_date\": \"2019-01-01\",\n                \"taxes\": [\n                    {\n                        \"net_tax\": {\n                            \"amount\": \"-659.42\",\n                            \"code\": \"USD\"\n                        },\n                        \"net_taxable_amount\": {\n                            \"amount\": \"-4815.57\",\n                            \"code\": \"USD\"\n                        },\n                        \"tax_collected\": {\n                            \"amount\": \"146.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"tax_name\": \"HST\",\n                        \"tax_paid\": {\n                            \"amount\": \"805.42\",\n                            \"code\": \"USD\"\n                        },\n                        \"taxable_amount_collected\": {\n                            \"amount\": \"1380.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"taxable_amount_paid\": {\n                            \"amount\": \"6195.57\",\n                            \"code\": \"USD\"\n                        }\n                    },\n                    {\n                        \"net_tax\": {\n                            \"amount\": \"-18.34\",\n                            \"code\": \"USD\"\n                        },\n                        \"net_taxable_amount\": {\n                            \"amount\": \"-141.12\",\n                            \"code\": \"USD\"\n                        },\n                        \"tax_collected\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"tax_name\": \"other tax\",\n                        \"tax_paid\": {\n                            \"amount\": \"18.34\",\n                            \"code\": \"USD\"\n                        },\n                        \"taxable_amount_collected\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"taxable_amount_paid\": {\n                            \"amount\": \"141.12\",\n                            \"code\": \"USD\"\n                        }\n                    }\n                ],\n                \"total_invoiced\": {\n                    \"amount\": \"9891.00\",\n                    \"code\": \"USD\"\n                }\n            }\n        }\n    }\n}"
    }
  ]
}