Creates draft expense claim receipts for any user

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 draft expense claim receipts for any user
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	unitdp: string | undefined,
12
	xero_tenant_id: string,
13
	Idempotency_Key: string,
14
	body: {
15
		Receipts?: {
16
			Date?: string
17
			Contact?: {
18
				ContactID?: string
19
				MergedToContactID?: string
20
				ContactNumber?: string
21
				AccountNumber?: string
22
				ContactStatus?: 'ACTIVE' | 'ARCHIVED' | 'GDPRREQUEST'
23
				Name?: string
24
				FirstName?: string
25
				LastName?: string
26
				CompanyNumber?: string
27
				EmailAddress?: string
28
				ContactPersons?: {
29
					FirstName?: string
30
					LastName?: string
31
					EmailAddress?: string
32
					IncludeInEmails?: false | true
33
				}[]
34
				BankAccountDetails?: string
35
				TaxNumber?: string
36
				AccountsReceivableTaxType?: string
37
				AccountsPayableTaxType?: string
38
				Addresses?: {
39
					AddressType?: 'POBOX' | 'STREET'
40
					AddressLine1?: string
41
					AddressLine2?: string
42
					AddressLine3?: string
43
					AddressLine4?: string
44
					City?: string
45
					Region?: string
46
					PostalCode?: string
47
					Country?: string
48
					AttentionTo?: string
49
				}[]
50
				Phones?: {
51
					PhoneType?: 'DEFAULT' | 'DDI' | 'MOBILE' | 'FAX' | 'OFFICE'
52
					PhoneNumber?: string
53
					PhoneAreaCode?: string
54
					PhoneCountryCode?: string
55
				}[]
56
				IsSupplier?: false | true
57
				IsCustomer?: false | true
58
				SalesDefaultLineAmountType?: 'INCLUSIVE' | 'EXCLUSIVE' | 'NONE'
59
				PurchasesDefaultLineAmountType?: 'INCLUSIVE' | 'EXCLUSIVE' | 'NONE'
60
				DefaultCurrency?:
61
					| 'AED'
62
					| 'AFN'
63
					| 'ALL'
64
					| 'AMD'
65
					| 'ANG'
66
					| 'AOA'
67
					| 'ARS'
68
					| 'AUD'
69
					| 'AWG'
70
					| 'AZN'
71
					| 'BAM'
72
					| 'BBD'
73
					| 'BDT'
74
					| 'BGN'
75
					| 'BHD'
76
					| 'BIF'
77
					| 'BMD'
78
					| 'BND'
79
					| 'BOB'
80
					| 'BRL'
81
					| 'BSD'
82
					| 'BTN'
83
					| 'BWP'
84
					| 'BYN'
85
					| 'BYR'
86
					| 'BZD'
87
					| 'CAD'
88
					| 'CDF'
89
					| 'CHF'
90
					| 'CLF'
91
					| 'CLP'
92
					| 'CNY'
93
					| 'COP'
94
					| 'CRC'
95
					| 'CUC'
96
					| 'CUP'
97
					| 'CVE'
98
					| 'CZK'
99
					| 'DJF'
100
					| 'DKK'
101
					| 'DOP'
102
					| 'DZD'
103
					| 'EEK'
104
					| 'EGP'
105
					| 'ERN'
106
					| 'ETB'
107
					| 'EUR'
108
					| 'FJD'
109
					| 'FKP'
110
					| 'GBP'
111
					| 'GEL'
112
					| 'GHS'
113
					| 'GIP'
114
					| 'GMD'
115
					| 'GNF'
116
					| 'GTQ'
117
					| 'GYD'
118
					| 'HKD'
119
					| 'HNL'
120
					| 'HRK'
121
					| 'HTG'
122
					| 'HUF'
123
					| 'IDR'
124
					| 'ILS'
125
					| 'INR'
126
					| 'IQD'
127
					| 'IRR'
128
					| 'ISK'
129
					| 'JMD'
130
					| 'JOD'
131
					| 'JPY'
132
					| 'KES'
133
					| 'KGS'
134
					| 'KHR'
135
					| 'KMF'
136
					| 'KPW'
137
					| 'KRW'
138
					| 'KWD'
139
					| 'KYD'
140
					| 'KZT'
141
					| 'LAK'
142
					| 'LBP'
143
					| 'LKR'
144
					| 'LRD'
145
					| 'LSL'
146
					| 'LTL'
147
					| 'LVL'
148
					| 'LYD'
149
					| 'MAD'
150
					| 'MDL'
151
					| 'MGA'
152
					| 'MKD'
153
					| 'MMK'
154
					| 'MNT'
155
					| 'MOP'
156
					| 'MRO'
157
					| 'MRU'
158
					| 'MUR'
159
					| 'MVR'
160
					| 'MWK'
161
					| 'MXN'
162
					| 'MXV'
163
					| 'MYR'
164
					| 'MZN'
165
					| 'NAD'
166
					| 'NGN'
167
					| 'NIO'
168
					| 'NOK'
169
					| 'NPR'
170
					| 'NZD'
171
					| 'OMR'
172
					| 'PAB'
173
					| 'PEN'
174
					| 'PGK'
175
					| 'PHP'
176
					| 'PKR'
177
					| 'PLN'
178
					| 'PYG'
179
					| 'QAR'
180
					| 'RON'
181
					| 'RSD'
182
					| 'RUB'
183
					| 'RWF'
184
					| 'SAR'
185
					| 'SBD'
186
					| 'SCR'
187
					| 'SDG'
188
					| 'SEK'
189
					| 'SGD'
190
					| 'SHP'
191
					| 'SKK'
192
					| 'SLE'
193
					| 'SLL'
194
					| 'SOS'
195
					| 'SRD'
196
					| 'STD'
197
					| 'STN'
198
					| 'SVC'
199
					| 'SYP'
200
					| 'SZL'
201
					| 'THB'
202
					| 'TJS'
203
					| 'TMT'
204
					| 'TND'
205
					| 'TOP'
206
					| 'TRY'
207
					| 'TTD'
208
					| 'TWD'
209
					| 'TZS'
210
					| 'UAH'
211
					| 'UGX'
212
					| 'USD'
213
					| 'UYU'
214
					| 'UZS'
215
					| 'VEF'
216
					| 'VES'
217
					| 'VND'
218
					| 'VUV'
219
					| 'WST'
220
					| 'XAF'
221
					| 'XCD'
222
					| 'XOF'
223
					| 'XPF'
224
					| 'YER'
225
					| 'ZAR'
226
					| 'ZMW'
227
					| 'ZMK'
228
					| 'ZWD'
229
				XeroNetworkKey?: string
230
				SalesDefaultAccountCode?: string
231
				PurchasesDefaultAccountCode?: string
232
				SalesTrackingCategories?: {
233
					TrackingCategoryName?: string
234
					TrackingOptionName?: string
235
				}[]
236
				PurchasesTrackingCategories?: {
237
					TrackingCategoryName?: string
238
					TrackingOptionName?: string
239
				}[]
240
				TrackingCategoryName?: string
241
				TrackingCategoryOption?: string
242
				PaymentTerms?: {
243
					Bills?: {
244
						Day?: number
245
						Type?:
246
							| 'DAYSAFTERBILLDATE'
247
							| 'DAYSAFTERBILLMONTH'
248
							| 'OFCURRENTMONTH'
249
							| 'OFFOLLOWINGMONTH'
250
					}
251
					Sales?: {
252
						Day?: number
253
						Type?:
254
							| 'DAYSAFTERBILLDATE'
255
							| 'DAYSAFTERBILLMONTH'
256
							| 'OFCURRENTMONTH'
257
							| 'OFFOLLOWINGMONTH'
258
					}
259
				}
260
				UpdatedDateUTC?: string
261
				ContactGroups?: {
262
					Name?: string
263
					Status?: 'ACTIVE' | 'DELETED'
264
					ContactGroupID?: string
265
					Contacts?: {}[]
266
				}[]
267
				Website?: string
268
				BrandingTheme?: {
269
					BrandingThemeID?: string
270
					Name?: string
271
					LogoUrl?: string
272
					Type?: 'INVOICE'
273
					SortOrder?: number
274
					CreatedDateUTC?: string
275
				}
276
				BatchPayments?: {
277
					BankAccountNumber?: string
278
					BankAccountName?: string
279
					Details?: string
280
					Code?: string
281
					Reference?: string
282
				}
283
				Discount?: number
284
				Balances?: {
285
					AccountsReceivable?: { Outstanding?: number; Overdue?: number }
286
					AccountsPayable?: { Outstanding?: number; Overdue?: number }
287
				}
288
				Attachments?: {
289
					AttachmentID?: string
290
					FileName?: string
291
					Url?: string
292
					MimeType?: string
293
					ContentLength?: number
294
					IncludeOnline?: false | true
295
				}[]
296
				HasAttachments?: never
297
				ValidationErrors?: { Message?: string }[]
298
				HasValidationErrors?: never
299
				StatusAttributeString?: string
300
			}
301
			LineItems?: {
302
				LineItemID?: string
303
				Description?: string
304
				Quantity?: number
305
				UnitAmount?: number
306
				ItemCode?: string
307
				AccountCode?: string
308
				AccountID?: string
309
				TaxType?: string
310
				TaxAmount?: number
311
				Item?: { Code?: string; Name?: string; ItemID?: string }
312
				LineAmount?: number
313
				Tracking?: {
314
					TrackingCategoryID?: string
315
					TrackingOptionID?: string
316
					Name?: string
317
					Option?: string
318
				}[]
319
				DiscountRate?: number
320
				DiscountAmount?: number
321
				RepeatingInvoiceID?: string
322
				Taxability?: 'TAXABLE' | 'NON_TAXABLE' | 'EXEMPT' | 'PART_TAXABLE' | 'NOT_APPLICABLE'
323
				SalesTaxCodeId?: number
324
				TaxBreakdown?: {
325
					TaxComponentId?: string
326
					Type?:
327
						| 'SYSGST/USCOUNTRY'
328
						| 'SYSGST/USSTATE'
329
						| 'SYSGST/USCOUNTY'
330
						| 'SYSGST/USCITY'
331
						| 'SYSGST/USSPECIAL'
332
					Name?: string
333
					TaxPercentage?: number
334
					TaxAmount?: number
335
					TaxableAmount?: number
336
					NonTaxableAmount?: number
337
					ExemptAmount?: number
338
					StateAssignedNo?: string
339
					JurisdictionRegion?: string
340
				}[]
341
			}[]
342
			User?: {
343
				UserID?: string
344
				EmailAddress?: string
345
				FirstName?: string
346
				LastName?: string
347
				UpdatedDateUTC?: string
348
				IsSubscriber?: false | true
349
				OrganisationRole?:
350
					| 'READONLY'
351
					| 'INVOICEONLY'
352
					| 'STANDARD'
353
					| 'FINANCIALADVISER'
354
					| 'MANAGEDCLIENT'
355
					| 'CASHBOOKCLIENT'
356
					| 'UNKNOWN'
357
			}
358
			Reference?: string
359
			LineAmountTypes?: 'Exclusive' | 'Inclusive' | 'NoTax'
360
			SubTotal?: number
361
			TotalTax?: number
362
			Total?: number
363
			ReceiptID?: string
364
			Status?: 'DRAFT' | 'SUBMITTED' | 'AUTHORISED' | 'DECLINED' | 'VOIDED'
365
			ReceiptNumber?: string
366
			UpdatedDateUTC?: string
367
			HasAttachments?: never
368
			Url?: string
369
			ValidationErrors?: { Message?: string }[]
370
			Warnings?: { Message?: string }[]
371
			Attachments?: {
372
				AttachmentID?: string
373
				FileName?: string
374
				Url?: string
375
				MimeType?: string
376
				ContentLength?: number
377
				IncludeOnline?: false | true
378
			}[]
379
		}[]
380
	}
381
) {
382
	const url = new URL(`https://api.xero.com/api.xro/2.0/Receipts`)
383
	for (const [k, v] of [['unitdp', unitdp]]) {
384
		if (v !== undefined && v !== '' && k !== undefined) {
385
			url.searchParams.append(k, v)
386
		}
387
	}
388
	const response = await fetch(url, {
389
		method: 'PUT',
390
		headers: {
391
			Accept: 'application/json',
392
			'xero-tenant-id': xero_tenant_id,
393
			'Idempotency-Key': Idempotency_Key,
394
			'Content-Type': 'application/json',
395
			Authorization: 'Bearer ' + auth.token
396
		},
397
		body: JSON.stringify(body)
398
	})
399
	if (!response.ok) {
400
		const text = await response.text()
401
		throw new Error(`${response.status} ${text}`)
402
	}
403
	return await response.json()
404
}
405