Retrieves report for profit and loss

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Retrieves report for profit and loss
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	fromDate: string | undefined,
12
	toDate: string | undefined,
13
	periods: string | undefined,
14
	timeframe: 'MONTH' | 'QUARTER' | 'YEAR' | undefined,
15
	trackingCategoryID: string | undefined,
16
	trackingCategoryID2: string | undefined,
17
	trackingOptionID: string | undefined,
18
	trackingOptionID2: string | undefined,
19
	standardLayout: string | undefined,
20
	paymentsOnly: string | undefined,
21
	xero_tenant_id: string
22
) {
23
	const url = new URL(`https://api.xero.com/api.xro/2.0/Reports/ProfitAndLoss`)
24
	for (const [k, v] of [
25
		['fromDate', fromDate],
26
		['toDate', toDate],
27
		['periods', periods],
28
		['timeframe', timeframe],
29
		['trackingCategoryID', trackingCategoryID],
30
		['trackingCategoryID2', trackingCategoryID2],
31
		['trackingOptionID', trackingOptionID],
32
		['trackingOptionID2', trackingOptionID2],
33
		['standardLayout', standardLayout],
34
		['paymentsOnly', paymentsOnly]
35
	]) {
36
		if (v !== undefined && v !== '' && k !== undefined) {
37
			url.searchParams.append(k, v)
38
		}
39
	}
40
	const response = await fetch(url, {
41
		method: 'GET',
42
		headers: {
43
			Accept: 'application/json',
44
			'xero-tenant-id': xero_tenant_id,
45
			Authorization: 'Bearer ' + auth.token
46
		},
47
		body: undefined
48
	})
49
	if (!response.ok) {
50
		const text = await response.text()
51
		throw new Error(`${response.status} ${text}`)
52
	}
53
	return await response.json()
54
}
55