import {z} from 'zod' export type CustomerReference = {id?: (string | null) | undefined, accounting_code?: (string | null) | undefined}; export type TaxRateReference = {code: string}; export type PriceListItemReference = {code: string}; export type ChargeInput = {name?: (string | null) | undefined, code?: (string | null) | undefined, description?: (string | null) | undefined, amount_excl_tax: number | string, tax_rate?: (TaxRateReference | null) | undefined, price_list_item: PriceListItemReference}; export type BillingEntityReference = {code: string}; export type SalesCreditNoteInput = {date: string, booking_date?: (string | null) | undefined, currency: string, custom_fields?: Record | undefined, customer_reference_numbers?: Array | undefined, customer: CustomerReference, charges: Array, billing_entity?: (BillingEntityReference | null) | undefined}; export type CreateApiV1AccountingSalesCreditNoteArgs = {body: SalesCreditNoteInput}; export type CreateApiV1AccountingSalesCreditNoteOptions = {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 salesCreditNoteOutput = z.object({date: z.string(), booking_date: z.string().nullable().optional(), currency: z.string(), custom_fields: z.object({}).optional(), customer_reference_numbers: z.array(z.string()).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 }; /** * Create A Sales Credit Note * * POST /v1/accounting/sales-credit-note/ */ export const createApiV1AccountingSalesCreditNote = async (args: CreateApiV1AccountingSalesCreditNoteArgs, options?: CreateApiV1AccountingSalesCreditNoteOptions) => { const {baseUrl = "https://api.qargo.com", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounting/sales-credit-note/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(salesCreditNoteOutput, await res.json()) };