Get expense by contacts report

The expense by contact report provides a year to date profit and loss for customers and suppliers for a given organisation, including detailed contact information.

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
 * Get expense by contacts report
7
 * The expense by contact report provides a year to date profit and loss for customers and suppliers for a given organisation, including detailed contact information.
8
 */
9
export async function main(
10
	auth: Xero,
11
	contactIds: string | undefined,
12
	includeManualJournals: string | undefined,
13
	startDate: string | undefined,
14
	endDate: string | undefined,
15
	xero_tenant_id: string
16
) {
17
	const url = new URL(`https://api.xero.com/finance.xro/1.0/FinancialStatements/contacts/expense`)
18
	for (const [k, v] of [
19
		['contactIds', contactIds],
20
		['includeManualJournals', includeManualJournals],
21
		['startDate', startDate],
22
		['endDate', endDate]
23
	]) {
24
		if (v !== undefined && v !== '' && k !== undefined) {
25
			url.searchParams.append(k, v)
26
		}
27
	}
28
	const response = await fetch(url, {
29
		method: 'GET',
30
		headers: {
31
			Accept: 'application/json',
32
			'xero-tenant-id': xero_tenant_id,
33
			Authorization: 'Bearer ' + auth.token
34
		},
35
		body: undefined
36
	})
37
	if (!response.ok) {
38
		const text = await response.text()
39
		throw new Error(`${response.status} ${text}`)
40
	}
41
	return await response.json()
42
}
43