Creates a new earnings rate

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
 * Creates a new earnings rate
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	Xero_Tenant_Id: string,
12
	Idempotency_Key: string,
13
	body: {
14
		earningsRateID?: string
15
		name: string
16
		earningsType:
17
			| 'Allowance'
18
			| 'BackPay'
19
			| 'Bonus'
20
			| 'Commission'
21
			| 'LumpSum'
22
			| 'OtherEarnings'
23
			| 'OvertimeEarnings'
24
			| 'RegularEarnings'
25
			| 'StatutoryAdoptionPay'
26
			| 'StatutoryAdoptionPayNonPensionable'
27
			| 'StatutoryBereavementPay'
28
			| 'StatutoryMaternityPay'
29
			| 'StatutoryMaternityPayNonPensionable'
30
			| 'StatutoryPaternityPay'
31
			| 'StatutoryPaternityPayNonPensionable'
32
			| 'StatutoryParentalBereavementPayNonPensionable'
33
			| 'StatutorySharedParentalPay'
34
			| 'StatutorySharedParentalPayNonPensionable'
35
			| 'StatutorySickPay'
36
			| 'StatutorySickPayNonPensionable'
37
			| 'TipsNonDirect'
38
			| 'TipsDirect'
39
			| 'TerminationPay'
40
		rateType: 'RatePerUnit' | 'MultipleOfOrdinaryEarningsRate' | 'FixedAmount'
41
		typeOfUnits: string
42
		currentRecord?: false | true
43
		expenseAccountID: string
44
		ratePerUnit?: number
45
		multipleOfOrdinaryEarningsRate?: number
46
		fixedAmount?: number
47
	}
48
) {
49
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/EarningsRates`)
50

51
	const response = await fetch(url, {
52
		method: 'POST',
53
		headers: {
54
			Accept: 'application/json',
55
			'Xero-Tenant-Id': Xero_Tenant_Id,
56
			'Idempotency-Key': Idempotency_Key,
57
			'Content-Type': 'application/json',
58
			Authorization: 'Bearer ' + auth.token
59
		},
60
		body: JSON.stringify(body)
61
	})
62
	if (!response.ok) {
63
		const text = await response.text()
64
		throw new Error(`${response.status} ${text}`)
65
	}
66
	return await response.json()
67
}
68