import {z} from 'zod' export type GetApiV1AccountingSalesInvoiceIdArgs = {id: string}; export type GetApiV1AccountingSalesInvoiceIdOptions = {baseUrl?: string | undefined, oAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const eInvoicingOutput = z.object({number: z.string().nullable().optional(), url: z.string().nullable().optional(), regulator: z.string().nullable().optional()}); export const exchangeRate = z.object({rate_date: z.string().nullable().optional(), rate: z.number(), inverse_rate: z.number()}); export const taxTypeEnum = z.enum(["VAT", "REVERSE_CHARGE", "UK_REVERSE_CHARGE", "TRANSIT", "NO_VAT", "CO_CONTRACTING", "OTHER"]); export const customerStatus = z.enum(["PROSPECT", "NOT_APPROVED", "APPROVED", "WARNING", "BLOCKED"]); export const locationOutput = z.object({code: z.string().nullable().optional(), display_name: z.string().nullable().optional(), external_id: z.string().nullable().optional(), address: z.string().nullable().optional(), address_second_line: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), name: z.string().nullable().optional(), id: z.string().nullable().optional(), latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional()}); export const paymentTermCode = z.enum(["END_OF_MONTH_2", "END_OF_MONTH_1", "END_OF_MONTH_0_NET_90", "END_OF_MONTH_0_NET_75", "END_OF_MONTH_0_NET_70", "END_OF_MONTH_0_NET_65", "END_OF_MONTH_0_NET_60", "END_OF_MONTH_0_NET_50", "END_OF_MONTH_0_NET_45", "END_OF_MONTH_0_NET_35", "END_OF_MONTH_0_NET_31", "END_OF_MONTH_0_NET_30", "END_OF_MONTH_0_NET_25", "END_OF_MONTH_0_NET_21", "END_OF_MONTH_0_NET_20", "END_OF_MONTH_0_NET_14", "END_OF_MONTH_0_NET_7", "END_OF_MONTH_0_NET_1", "END_OF_MONTH_0", "PREPAID", "CUSTOM_DUE_DATE", "UPON_RECEIPT", "NET_1", "NET_2", "NET_3", "NET_5", "NET_7", "NET_8", "NET_10", "NET_12", "NET_14", "NET_15", "NET_20", "NET_21", "NET_28", "NET_30", "NET_35", "NET_40", "NET_42", "NET_45", "NET_48", "NET_49", "NET_50", "NET_52", "NET_55", "NET_56", "NET_60", "NET_63", "NET_70", "NET_75", "NET_77", "NET_80", "NET_84", "NET_90", "NET_91", "NET_98", "NET_120"]); export const basePaymentTerm = z.object({code: paymentTermCode}); export const companySummaryOutput = z.object({id: z.string(), code: z.string().nullable().optional(), currency: z.string(), custom_fields: z.object({}).nullable().optional(), is_customer: z.boolean().optional(), is_subcontractor: z.boolean().optional(), is_buyer_or_seller: z.boolean().nullable().optional(), name: z.string(), legal_name: z.string().nullable().optional(), vat_number: z.string().nullable().optional(), note: z.string().nullable().optional(), tax_type: taxTypeEnum.nullable().optional(), status: customerStatus.optional(), accounting_code: z.string().nullable().optional(), billing_location: locationOutput.nullable().optional(), payment_term: basePaymentTerm.nullable().optional(), is_blocked: z.boolean().optional()}); export const apiOrderModelsChargeAccountOutput = z.object({code: z.string().nullable().optional()}); export const taxRateType = z.enum(["VAT", "NO_VAT", "REVERSE_CHARGE", "UK_REVERSE_CHARGE", "TRANSIT", "CO_CONTRACTING", "OTHER"]); export const apiAccountingModelsInvoiceTaxRate = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), percentage: z.number().nullable().optional(), code: z.string().nullable().optional(), tax_type: taxRateType.nullable().optional()}); export const baseTaxRate = z.object({percentage: z.number().nullable().optional()}); export const lineItem = z.object({id: z.string().nullable(), invoice_line_id: z.string().nullable(), name: z.string(), amount: z.string(), code: z.string(), account: apiOrderModelsChargeAccountOutput.nullable().optional(), description: z.string().nullable().optional(), tax_rate: apiAccountingModelsInvoiceTaxRate.nullable().optional(), base_tax_rate: baseTaxRate.nullable().optional(), order_name: z.string().nullable().optional(), order_id: z.string().nullable().optional(), trip_id: z.string().nullable().optional(), is_credit_charge: z.boolean().optional(), invoice_name: z.string().nullable().optional(), currency: z.string(), fraction: z.string().optional(), dimension_1: z.string().nullable().optional(), dimension_2: z.string().nullable().optional(), dimension_3: z.string().nullable().optional(), dimension_4: z.string().nullable().optional(), dimension_5: z.string().nullable().optional(), dimension_6: z.string().nullable().optional()}); export const billingEntity = z.object({id: z.string(), legal_name: z.string(), vat_number: z.string(), code: z.string(), company_registration_number: z.string(), is_default: z.boolean().optional()}); export const salesInvoiceOutput = z.object({date: z.string(), due_date: z.string(), booking_date: z.string().nullable().optional(), currency: z.string(), customer_reference_numbers: z.array(z.string()).optional(), custom_fields: z.object({}).optional(), id: z.string().nullable(), name: z.string(), e_invoicing: eInvoicingOutput.nullable().optional(), exchange_rate: exchangeRate.nullable().optional(), total_excl_tax: z.string(), total_incl_tax: z.string(), total_tax: z.string(), customer: companySummaryOutput, line_items: z.array(lineItem), billing_entity: billingEntity}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Retrieve Sales Invoice * * GET /v1/accounting/sales-invoice/{id} */ export const getApiV1AccountingSalesInvoiceId = async (args: GetApiV1AccountingSalesInvoiceIdArgs, options?: GetApiV1AccountingSalesInvoiceIdOptions) => { const {baseUrl = "https://api.qargo.com", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounting/sales-invoice/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(salesInvoiceOutput, await res.json()) };