Updates or creates one or more quotes
One script reply has been approved by the moderators Verified
Created by hugo697 448 days ago
Submitted by hugo697 Bun
Verified 448 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Updates or creates one or more quotes
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	summarizeErrors: string | undefined,
12
	xero_tenant_id: string,
13
	Idempotency_Key: string,
14
	body: {
15
		Quotes?: {
16
			QuoteID?: string
17
			QuoteNumber?: string
18
			Reference?: string
19
			Terms?: string
20
			Contact?: string
21
			LineItems?: {
22
				LineItemID?: string
23
				Description?: string
24
				Quantity?: number
25
				UnitAmount?: number
26
				ItemCode?: string
27
				AccountCode?: string
28
				AccountID?: string
29
				TaxType?: string
30
				TaxAmount?: number
31
				Item?: { Code?: string; Name?: string; ItemID?: string }
32
				LineAmount?: number
33
				Tracking?: {
34
					TrackingCategoryID?: string
35
					TrackingOptionID?: string
36
					Name?: string
37
					Option?: string
38
				}[]
39
				DiscountRate?: number
40
				DiscountAmount?: number
41
				RepeatingInvoiceID?: string
42
				Taxability?: 'TAXABLE' | 'NON_TAXABLE' | 'EXEMPT' | 'PART_TAXABLE' | 'NOT_APPLICABLE'
43
				SalesTaxCodeId?: number
44
				TaxBreakdown?: {
45
					TaxComponentId?: string
46
					Type?:
47
						| 'SYSGST/USCOUNTRY'
48
						| 'SYSGST/USSTATE'
49
						| 'SYSGST/USCOUNTY'
50
						| 'SYSGST/USCITY'
51
						| 'SYSGST/USSPECIAL'
52
					Name?: string
53
					TaxPercentage?: number
54
					TaxAmount?: number
55
					TaxableAmount?: number
56
					NonTaxableAmount?: number
57
					ExemptAmount?: number
58
					StateAssignedNo?: string
59
					JurisdictionRegion?: string
60
				}[]
61
			}[]
62
			Date?: string
63
			DateString?: string
64
			ExpiryDate?: string
65
			ExpiryDateString?: string
66
			Status?: 'DRAFT' | 'SENT' | 'DECLINED' | 'ACCEPTED' | 'INVOICED' | 'DELETED'
67
			CurrencyCode?:
68
				| 'AED'
69
				| 'AFN'
70
				| 'ALL'
71
				| 'AMD'
72
				| 'ANG'
73
				| 'AOA'
74
				| 'ARS'
75
				| 'AUD'
76
				| 'AWG'
77
				| 'AZN'
78
				| 'BAM'
79
				| 'BBD'
80
				| 'BDT'
81
				| 'BGN'
82
				| 'BHD'
83
				| 'BIF'
84
				| 'BMD'
85
				| 'BND'
86
				| 'BOB'
87
				| 'BRL'
88
				| 'BSD'
89
				| 'BTN'
90
				| 'BWP'
91
				| 'BYN'
92
				| 'BYR'
93
				| 'BZD'
94
				| 'CAD'
95
				| 'CDF'
96
				| 'CHF'
97
				| 'CLF'
98
				| 'CLP'
99
				| 'CNY'
100
				| 'COP'
101
				| 'CRC'
102
				| 'CUC'
103
				| 'CUP'
104
				| 'CVE'
105
				| 'CZK'
106
				| 'DJF'
107
				| 'DKK'
108
				| 'DOP'
109
				| 'DZD'
110
				| 'EEK'
111
				| 'EGP'
112
				| 'ERN'
113
				| 'ETB'
114
				| 'EUR'
115
				| 'FJD'
116
				| 'FKP'
117
				| 'GBP'
118
				| 'GEL'
119
				| 'GHS'
120
				| 'GIP'
121
				| 'GMD'
122
				| 'GNF'
123
				| 'GTQ'
124
				| 'GYD'
125
				| 'HKD'
126
				| 'HNL'
127
				| 'HRK'
128
				| 'HTG'
129
				| 'HUF'
130
				| 'IDR'
131
				| 'ILS'
132
				| 'INR'
133
				| 'IQD'
134
				| 'IRR'
135
				| 'ISK'
136
				| 'JMD'
137
				| 'JOD'
138
				| 'JPY'
139
				| 'KES'
140
				| 'KGS'
141
				| 'KHR'
142
				| 'KMF'
143
				| 'KPW'
144
				| 'KRW'
145
				| 'KWD'
146
				| 'KYD'
147
				| 'KZT'
148
				| 'LAK'
149
				| 'LBP'
150
				| 'LKR'
151
				| 'LRD'
152
				| 'LSL'
153
				| 'LTL'
154
				| 'LVL'
155
				| 'LYD'
156
				| 'MAD'
157
				| 'MDL'
158
				| 'MGA'
159
				| 'MKD'
160
				| 'MMK'
161
				| 'MNT'
162
				| 'MOP'
163
				| 'MRO'
164
				| 'MRU'
165
				| 'MUR'
166
				| 'MVR'
167
				| 'MWK'
168
				| 'MXN'
169
				| 'MXV'
170
				| 'MYR'
171
				| 'MZN'
172
				| 'NAD'
173
				| 'NGN'
174
				| 'NIO'
175
				| 'NOK'
176
				| 'NPR'
177
				| 'NZD'
178
				| 'OMR'
179
				| 'PAB'
180
				| 'PEN'
181
				| 'PGK'
182
				| 'PHP'
183
				| 'PKR'
184
				| 'PLN'
185
				| 'PYG'
186
				| 'QAR'
187
				| 'RON'
188
				| 'RSD'
189
				| 'RUB'
190
				| 'RWF'
191
				| 'SAR'
192
				| 'SBD'
193
				| 'SCR'
194
				| 'SDG'
195
				| 'SEK'
196
				| 'SGD'
197
				| 'SHP'
198
				| 'SKK'
199
				| 'SLE'
200
				| 'SLL'
201
				| 'SOS'
202
				| 'SRD'
203
				| 'STD'
204
				| 'STN'
205
				| 'SVC'
206
				| 'SYP'
207
				| 'SZL'
208
				| 'THB'
209
				| 'TJS'
210
				| 'TMT'
211
				| 'TND'
212
				| 'TOP'
213
				| 'TRY'
214
				| 'TTD'
215
				| 'TWD'
216
				| 'TZS'
217
				| 'UAH'
218
				| 'UGX'
219
				| 'USD'
220
				| 'UYU'
221
				| 'UZS'
222
				| 'VEF'
223
				| 'VES'
224
				| 'VND'
225
				| 'VUV'
226
				| 'WST'
227
				| 'XAF'
228
				| 'XCD'
229
				| 'XOF'
230
				| 'XPF'
231
				| 'YER'
232
				| 'ZAR'
233
				| 'ZMW'
234
				| 'ZMK'
235
				| 'ZWD'
236
			CurrencyRate?: number
237
			SubTotal?: number
238
			TotalTax?: number
239
			Total?: number
240
			TotalDiscount?: number
241
			Title?: string
242
			Summary?: string
243
			BrandingThemeID?: string
244
			UpdatedDateUTC?: string
245
			LineAmountTypes?: 'EXCLUSIVE' | 'INCLUSIVE' | 'NOTAX'
246
			StatusAttributeString?: string
247
			ValidationErrors?: { Message?: string }[]
248
		}[]
249
	}
250
) {
251
	const url = new URL(`https://api.xero.com/api.xro/2.0/Quotes`)
252
	for (const [k, v] of [['summarizeErrors', summarizeErrors]]) {
253
		if (v !== undefined && v !== '' && k !== undefined) {
254
			url.searchParams.append(k, v)
255
		}
256
	}
257
	const response = await fetch(url, {
258
		method: 'POST',
259
		headers: {
260
			Accept: 'application/json',
261
			'xero-tenant-id': xero_tenant_id,
262
			'Idempotency-Key': Idempotency_Key,
263
			'Content-Type': 'application/json',
264
			Authorization: 'Bearer ' + auth.token
265
		},
266
		body: JSON.stringify(body)
267
	})
268
	if (!response.ok) {
269
		const text = await response.text()
270
		throw new Error(`${response.status} ${text}`)
271
	}
272
	return await response.json()
273
}
274