Creates a pay run

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 pay run
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	Xero_Tenant_Id: string,
12
	Idempotency_Key: string,
13
	body: {
14
		payRunID?: string
15
		payrollCalendarID?: string
16
		periodStartDate?: string
17
		periodEndDate?: string
18
		paymentDate?: string
19
		totalCost?: number
20
		totalPay?: number
21
		payRunStatus?: 'Draft' | 'Posted'
22
		payRunType?: 'Scheduled' | 'Unscheduled' | 'EarlierYearUpdate'
23
		calendarType?:
24
			| 'Weekly'
25
			| 'Fortnightly'
26
			| 'FourWeekly'
27
			| 'Monthly'
28
			| 'Annual'
29
			| 'Quarterly'
30
			| 'TwiceMonthly'
31
		postedDateTime?: string
32
		paySlips?: {
33
			paySlipID?: string
34
			employeeID?: string
35
			payRunID?: string
36
			lastEdited?: string
37
			firstName?: string
38
			lastName?: string
39
			totalEarnings?: number
40
			grossEarnings?: number
41
			totalPay?: number
42
			totalEmployerTaxes?: number
43
			totalEmployeeTaxes?: number
44
			totalDeductions?: number
45
			totalReimbursements?: number
46
			totalStatutoryDeductions?: number
47
			totalSuperannuation?: number
48
			bacsHash?: string
49
			paymentMethod?: 'Cheque' | 'Electronically' | 'Manual'
50
			earningsLines?: {
51
				earningsLineID?: string
52
				earningsRateID?: string
53
				displayName?: string
54
				ratePerUnit?: number
55
				numberOfUnits?: number
56
				fixedAmount?: number
57
				amount?: number
58
				isLinkedToTimesheet?: false | true
59
				isAverageDailyPayRate?: false | true
60
				isSystemGenerated?: false | true
61
			}[]
62
			leaveEarningsLines?: {
63
				earningsLineID?: string
64
				earningsRateID?: string
65
				displayName?: string
66
				ratePerUnit?: number
67
				numberOfUnits?: number
68
				fixedAmount?: number
69
				amount?: number
70
				isLinkedToTimesheet?: false | true
71
				isAverageDailyPayRate?: false | true
72
				isSystemGenerated?: false | true
73
			}[]
74
			timesheetEarningsLines?: {
75
				earningsLineID?: string
76
				earningsRateID?: string
77
				displayName?: string
78
				ratePerUnit?: number
79
				numberOfUnits?: number
80
				fixedAmount?: number
81
				amount?: number
82
				isLinkedToTimesheet?: false | true
83
				isAverageDailyPayRate?: false | true
84
				isSystemGenerated?: false | true
85
			}[]
86
			deductionLines?: {
87
				deductionTypeID?: string
88
				displayName?: string
89
				amount?: number
90
				subjectToTax?: false | true
91
				percentage?: number
92
			}[]
93
			reimbursementLines?: {
94
				reimbursementTypeID?: string
95
				description?: string
96
				amount?: number
97
				ratePerUnit?: number
98
				numberOfUnits?: number
99
			}[]
100
			leaveAccrualLines?: { leaveTypeID?: string; numberOfUnits?: number }[]
101
			superannuationLines?: {
102
				superannuationTypeID?: string
103
				displayName?: string
104
				amount?: number
105
				fixedAmount?: number
106
				percentage?: number
107
				manualAdjustment?: false | true
108
			}[]
109
			paymentLines?: {
110
				paymentLineID?: string
111
				amount?: number
112
				accountNumber?: string
113
				sortCode?: string
114
				accountName?: string
115
			}[]
116
			employeeTaxLines?: {
117
				taxLineID?: string
118
				description?: string
119
				amount?: number
120
				globalTaxTypeID?: string
121
				manualAdjustment?: false | true
122
			}[]
123
			employerTaxLines?: {
124
				taxLineID?: string
125
				description?: string
126
				amount?: number
127
				globalTaxTypeID?: string
128
				manualAdjustment?: false | true
129
			}[]
130
			statutoryDeductionLines?: {
131
				statutoryDeductionTypeID?: string
132
				amount?: number
133
				fixedAmount?: number
134
				manualAdjustment?: false | true
135
			}[]
136
			taxSettings?: {
137
				periodUnits?: number
138
				periodType?: 'weeks' | 'months'
139
				taxCode?:
140
					| 'ND'
141
					| 'M'
142
					| 'ME'
143
					| 'MSL'
144
					| 'MESL'
145
					| 'SB'
146
					| 'S'
147
					| 'SH'
148
					| 'ST'
149
					| 'SBSL'
150
					| 'SSL'
151
					| 'SHSL'
152
					| 'STSL'
153
					| 'WT'
154
					| 'CAE'
155
					| 'EDW'
156
					| 'NSW'
157
					| 'STC'
158
					| 'STCSL'
159
				specialTaxRate?: string
160
				lumpSumTaxCode?: string
161
				lumpSumAmount?: string
162
			}
163
			grossEarningsHistory?: { daysPaid?: number; unpaidWeeks?: number }
164
		}[]
165
	}
166
) {
167
	const url = new URL(`https://api.xero.com/payroll.xro/2.0/PayRuns`)
168

169
	const response = await fetch(url, {
170
		method: 'POST',
171
		headers: {
172
			Accept: 'application/json',
173
			'Xero-Tenant-Id': Xero_Tenant_Id,
174
			'Idempotency-Key': Idempotency_Key,
175
			'Content-Type': 'application/json',
176
			Authorization: 'Bearer ' + auth.token
177
		},
178
		body: JSON.stringify(body)
179
	})
180
	if (!response.ok) {
181
		const text = await response.text()
182
		throw new Error(`${response.status} ${text}`)
183
	}
184
	return await response.json()
185
}
186