SQL Server / New FreshBooks / Profit/Loss Report
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
-- 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
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
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 @http, 'QuickGetSb', @success OUT, 'https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/profitloss_entity', @sbResponseBody
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
RETURN
END
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 @http, 'LastStatus', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "response": {
-- "result": {
-- "profitloss": {
-- "cash_based": false,
-- "company_name": "Postman Sandbox",
-- "currency_code": "USD",
-- "download_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50aWQiOiJ3a01kMmciLCJ1c2VyaWQiOjEsImNvbnRhY3RpZCI6LTEsImxldmVsIjozLCJ0eXBlIjoicmVwb3J0Iiwib2JqZWN0aWQiOiJwcm9maXRsb3NzIiwicGFyYW1zIjp7InN0YXJ0X2RhdGUiOiIyMDE5LTA0LTI0IiwiZW5kX2RhdGUiOiIyMDE5LTA0LTI0IiwicmVzb2x1dGlvbiI6bnVsbCwiY3VycmVuY3lfY29kZSI6IlVTRCIsImNhc2hfYmFzZWQiOmZhbHNlLCJyZXBvcnRfbW9kZSI6Im1vZGVybl9lbnRpdHkifSwiaWF0IjoxNTU2MTMwNTkzLCJleHAiOjE1NTg3MjI1OTN9.YbSTjVO5hTVP_l2bo0w2m5i_11IlLBD_KePsJp5FPIg",
-- "end_date": "2019-04-24",
-- "expenses": [
-- {
-- "children": [
-- {
-- "children": [
-- ],
-- "data": [
-- ],
-- "description": "Gas",
-- "entry_type": "debit",
-- "total": {
-- "amount": "141.12",
-- "code": "USD"
-- }
-- }
-- ],
-- "data": [
-- ],
-- "description": "Car & Truck Expenses",
-- "entry_type": "debit",
-- "total": {
-- "amount": "141.12",
-- "code": "USD"
-- }
-- }
-- ],
-- "income": [
-- {
-- "children": [
-- ],
-- "data": [
-- ],
-- "description": "Sales",
-- "entry_type": "credit",
-- "total": {
-- "amount": "0.00",
-- "code": "USD"
-- }
-- },
-- {
-- "children": [
-- ],
-- "data": [
-- ],
-- "description": "Cost of Goods Sold",
-- "entry_type": "credit",
-- "total": {
-- "amount": "0.00",
-- "code": "USD"
-- }
-- }
-- ],
-- "labels": [
-- ],
-- "net_profit": {
-- "children": [
-- ],
-- "data": [
-- ],
-- "description": "Net Profit (USD)",
-- "entry_type": "credit",
-- "total": {
-- "amount": "-141.12",
-- "code": "USD"
-- }
-- },
-- "resolution": null,
-- "start_date": "2019-04-24",
-- "total_expenses": {
-- "children": [
-- ],
-- "data": [
-- ],
-- "description": "Total Expenses",
-- "entry_type": "debit",
-- "total": {
-- "amount": "141.12",
-- "code": "USD"
-- }
-- },
-- "total_income": {
-- "children": [
-- ],
-- "data": [
-- ],
-- "description": "Gross Profit",
-- "entry_type": "credit",
-- "total": {
-- "amount": "0.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 @description nvarchar(4000)
DECLARE @entry_type nvarchar(4000)
DECLARE @totalAmount nvarchar(4000)
DECLARE @totalCode nvarchar(4000)
DECLARE @j int
DECLARE @count_j int
DECLARE @k int
DECLARE @count_k int
DECLARE @Cash_based int
EXEC sp_OAMethod @jResp, 'BoolOf', @Cash_based OUT, 'response.result.profitloss.cash_based'
DECLARE @Company_name nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Company_name OUT, 'response.result.profitloss.company_name'
DECLARE @Currency_code nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Currency_code OUT, 'response.result.profitloss.currency_code'
DECLARE @Download_token nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Download_token OUT, 'response.result.profitloss.download_token'
DECLARE @End_date nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @End_date OUT, 'response.result.profitloss.end_date'
DECLARE @Description nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'response.result.profitloss.net_profit.description'
DECLARE @Entry_type nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Entry_type OUT, 'response.result.profitloss.net_profit.entry_type'
DECLARE @Amount nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Amount OUT, 'response.result.profitloss.net_profit.total.amount'
DECLARE @Code nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'response.result.profitloss.net_profit.total.code'
DECLARE @Resolution nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Resolution OUT, 'response.result.profitloss.resolution'
DECLARE @Start_date nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Start_date OUT, 'response.result.profitloss.start_date'
DECLARE @Total_expensesDescription nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Total_expensesDescription OUT, 'response.result.profitloss.total_expenses.description'
DECLARE @Total_expensesEntry_type nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Total_expensesEntry_type OUT, 'response.result.profitloss.total_expenses.entry_type'
DECLARE @TotalAmount nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @TotalAmount OUT, 'response.result.profitloss.total_expenses.total.amount'
DECLARE @TotalCode nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @TotalCode OUT, 'response.result.profitloss.total_expenses.total.code'
DECLARE @Total_incomeDescription nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Total_incomeDescription OUT, 'response.result.profitloss.total_income.description'
DECLARE @Total_incomeEntry_type nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Total_incomeEntry_type OUT, 'response.result.profitloss.total_income.entry_type'
EXEC sp_OAMethod @jResp, 'StringOf', @TotalAmount OUT, 'response.result.profitloss.total_income.total.amount'
EXEC sp_OAMethod @jResp, 'StringOf', @TotalCode OUT, 'response.result.profitloss.total_income.total.code'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.expenses'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'response.result.profitloss.expenses[i].description'
EXEC sp_OAMethod @jResp, 'StringOf', @entry_type OUT, 'response.result.profitloss.expenses[i].entry_type'
EXEC sp_OAMethod @jResp, 'StringOf', @totalAmount OUT, 'response.result.profitloss.expenses[i].total.amount'
EXEC sp_OAMethod @jResp, 'StringOf', @totalCode OUT, 'response.result.profitloss.expenses[i].total.code'
SELECT @j = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.profitloss.expenses[i].children'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jResp, 'J', @j
EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'response.result.profitloss.expenses[i].children[j].description'
EXEC sp_OAMethod @jResp, 'StringOf', @entry_type OUT, 'response.result.profitloss.expenses[i].children[j].entry_type'
EXEC sp_OAMethod @jResp, 'StringOf', @totalAmount OUT, 'response.result.profitloss.expenses[i].children[j].total.amount'
EXEC sp_OAMethod @jResp, 'StringOf', @totalCode OUT, 'response.result.profitloss.expenses[i].children[j].total.code'
SELECT @k = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_k OUT, 'response.result.profitloss.expenses[i].children[j].children'
WHILE @k < @count_k
BEGIN
EXEC sp_OASetProperty @jResp, 'K', @k
SELECT @k = @k + 1
END
SELECT @k = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_k OUT, 'response.result.profitloss.expenses[i].children[j].data'
WHILE @k < @count_k
BEGIN
EXEC sp_OASetProperty @jResp, 'K', @k
SELECT @k = @k + 1
END
SELECT @j = @j + 1
END
SELECT @j = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.profitloss.expenses[i].data'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jResp, 'J', @j
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.income'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'response.result.profitloss.income[i].description'
EXEC sp_OAMethod @jResp, 'StringOf', @entry_type OUT, 'response.result.profitloss.income[i].entry_type'
EXEC sp_OAMethod @jResp, 'StringOf', @totalAmount OUT, 'response.result.profitloss.income[i].total.amount'
EXEC sp_OAMethod @jResp, 'StringOf', @totalCode OUT, 'response.result.profitloss.income[i].total.code'
SELECT @j = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.profitloss.income[i].children'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jResp, 'J', @j
SELECT @j = @j + 1
END
SELECT @j = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.profitloss.income[i].data'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jResp, 'J', @j
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.labels'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.net_profit.children'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.net_profit.data'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.total_expenses.children'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.total_expenses.data'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.total_income.children'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.profitloss.total_income.data'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO
Curl Command
curl -X GET
-H "Authorization: Bearer <access_token>"
-H "Content-Type: application/json"
https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/profitloss_entity
Postman Collection Item JSON
{
"name": "Profit/Loss Report",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"url": {
"raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/profitloss_entity",
"protocol": "https",
"host": [
"api",
"freshbooks",
"com"
],
"path": [
"accounting",
"account",
"{{accountId}}",
"reports",
"accounting",
"profitloss_entity"
],
"query": [
{
"key": "start_date",
"value": "2019-01-01",
"description": "created during or after the given date i.e. `2019-01-01`",
"disabled": true
},
{
"key": "end_date",
"value": "2019-05-01",
"description": "created during or before the given date i.e. `2019-05-01`",
"disabled": true
},
{
"key": "currency_code",
"value": "USD",
"description": "reports using the given currency as primary currency i.e. `\"USD\"`",
"disabled": true
},
{
"key": "cash_based",
"value": "false",
"description": "True ignores billed payments and uses just collected payments i.e. `true`, `false`",
"disabled": true
},
{
"key": "resolution",
"value": "3m",
"description": "specifies how often data is calculated and stored. For example, \"3m\" would specify to return data grouped in three month intervals. i.e. `\"3m\"`",
"disabled": true
}
]
},
"description": "The Profit/Loss Report shows all the information involving both your profits and losses"
},
"response": [
{
"name": "Profit/Loss Report",
"originalRequest": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"url": {
"raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/profitloss_entity",
"protocol": "https",
"host": [
"api",
"freshbooks",
"com"
],
"path": [
"accounting",
"account",
"{{accountId}}",
"reports",
"accounting",
"profitloss_entity"
],
"query": [
{
"key": "start_date",
"value": "2019-01-01",
"description": "created during or after the given date i.e. `2019-01-01`",
"disabled": true
},
{
"key": "end_date",
"value": "2019-05-01",
"description": "created during or before the given date i.e. `2019-05-01`",
"disabled": true
},
{
"key": "currency_code",
"value": "USD",
"description": "reports using the given currency as primary currency i.e. `\"USD\"`",
"disabled": true
},
{
"key": "cash_based",
"value": "false",
"description": "True ignores billed payments and uses just collected payments i.e. `true`, `false`",
"disabled": true
},
{
"key": "resolution",
"value": "3m",
"description": "specifies how often data is calculated and stored. For example, \"3m\" would specify to return data grouped in three month intervals. i.e. `\"3m\"`",
"disabled": true
}
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "nginx"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "X-NewRelic-App-Data",
"value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gWVEZcEBIQWFEBWw1NVkBbXQJLBEEOQRYXFBtRSlMWAgVRWFAHBAdTUQMFBVpXVVYcTwlOGg8GUwIDC1JSVwpQVQ8BVwBBFFVRCBIHag=="
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Expires",
"value": "Tue, 24 Apr 2018 18:29:53 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:29:53 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 \"profitloss\": {\n \"cash_based\": false,\n \"company_name\": \"Postman Sandbox\",\n \"currency_code\": \"USD\",\n \"download_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50aWQiOiJ3a01kMmciLCJ1c2VyaWQiOjEsImNvbnRhY3RpZCI6LTEsImxldmVsIjozLCJ0eXBlIjoicmVwb3J0Iiwib2JqZWN0aWQiOiJwcm9maXRsb3NzIiwicGFyYW1zIjp7InN0YXJ0X2RhdGUiOiIyMDE5LTA0LTI0IiwiZW5kX2RhdGUiOiIyMDE5LTA0LTI0IiwicmVzb2x1dGlvbiI6bnVsbCwiY3VycmVuY3lfY29kZSI6IlVTRCIsImNhc2hfYmFzZWQiOmZhbHNlLCJyZXBvcnRfbW9kZSI6Im1vZGVybl9lbnRpdHkifSwiaWF0IjoxNTU2MTMwNTkzLCJleHAiOjE1NTg3MjI1OTN9.YbSTjVO5hTVP_l2bo0w2m5i_11IlLBD_KePsJp5FPIg\",\n \"end_date\": \"2019-04-24\",\n \"expenses\": [\n {\n \"children\": [\n {\n \"children\": [],\n \"data\": [],\n \"description\": \"Gas\",\n \"entry_type\": \"debit\",\n \"total\": {\n \"amount\": \"141.12\",\n \"code\": \"USD\"\n }\n }\n ],\n \"data\": [],\n \"description\": \"Car & Truck Expenses\",\n \"entry_type\": \"debit\",\n \"total\": {\n \"amount\": \"141.12\",\n \"code\": \"USD\"\n }\n }\n ],\n \"income\": [\n {\n \"children\": [],\n \"data\": [],\n \"description\": \"Sales\",\n \"entry_type\": \"credit\",\n \"total\": {\n \"amount\": \"0.00\",\n \"code\": \"USD\"\n }\n },\n {\n \"children\": [],\n \"data\": [],\n \"description\": \"Cost of Goods Sold\",\n \"entry_type\": \"credit\",\n \"total\": {\n \"amount\": \"0.00\",\n \"code\": \"USD\"\n }\n }\n ],\n \"labels\": [],\n \"net_profit\": {\n \"children\": [],\n \"data\": [],\n \"description\": \"Net Profit (USD)\",\n \"entry_type\": \"credit\",\n \"total\": {\n \"amount\": \"-141.12\",\n \"code\": \"USD\"\n }\n },\n \"resolution\": null,\n \"start_date\": \"2019-04-24\",\n \"total_expenses\": {\n \"children\": [],\n \"data\": [],\n \"description\": \"Total Expenses\",\n \"entry_type\": \"debit\",\n \"total\": {\n \"amount\": \"141.12\",\n \"code\": \"USD\"\n }\n },\n \"total_income\": {\n \"children\": [],\n \"data\": [],\n \"description\": \"Gross Profit\",\n \"entry_type\": \"credit\",\n \"total\": {\n \"amount\": \"0.00\",\n \"code\": \"USD\"\n }\n }\n }\n }\n }\n}"
}
]
}