Creates a new deduction

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 deduction
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	Xero_Tenant_Id: string,
12
	Idempotency_Key: string,
13
	body: {
14
		deductionId?: string
15
		deductionName: string
16
		deductionCategory?:
17
			| 'CapitalContributions'
18
			| 'ChildCareVoucher'
19
			| 'MakingGood'
20
			| 'PostgraduateLoanDeductions'
21
			| 'PrivateUsePayments'
22
			| 'SalarySacrifice'
23
			| 'StakeholderPension'
24
			| 'StakeholderPensionPostTax'
25
			| 'StudentLoanDeductions'
26
			| 'UkOther'
27
		liabilityAccountId: string
28
		currentRecord?: false | true
29
		standardAmount?: number
30
		reducesSuperLiability?: false | true
31
		reducesTaxLiability?: false | true
32
		calculationType?: 'FixedAmount' | 'PercentageOfGross'
33
		percentage?: number
34
		subjectToNIC?: false | true
35
		subjectToTax?: false | true
36
		isReducedByBasicRate?: false | true
37
		applyToPensionCalculations?: false | true
38
		isCalculatingOnQualifyingEarnings?: false | true
39
		isPension?: false | true
40
	}
41
) {
42
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/Deductions`)
43

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