import {z} from 'zod' export type PropertyAccountingExtendedNoteUser = {id?: string | undefined, name?: string | undefined}; export type AccountingExtendedNote = {note?: string | undefined, user?: PropertyAccountingExtendedNoteUser | undefined, date?: string | undefined, visibility?: ('PUBLIC' | 'PRIVATE') | undefined}; export type PropertyAccountingInvoiceExtendedNotes = Array; export type PropertyAccountingLineitemCategoryIds = Array; export type AccountingReference = {id?: string | undefined, name?: string | undefined}; export type PropertyAccountingLineitemLocations = Array; export type PropertyAccountingLineitemItemVariants = Array; export type AccountingFee = {type: 'TAX' | 'DISCOUNT' | 'PROMOTION' | 'SHIPPING' | 'GIFT_WRAP' | 'COD' | 'SURCHARGE' | 'TIP' | 'SERVICE_CHARGE' | 'OTHER', original_type?: string | undefined, amount: number, currency?: string | undefined}; export type PropertyAccountingLineitemFees = Array; export type AccountingLineitem = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, refunded_at?: string | undefined, total_amount?: number | undefined, refund_amount?: number | undefined, discount_amount?: number | undefined, tax_amount?: number | undefined, item_id?: string | undefined, unit_amount?: number | undefined, unit_quantity?: number | undefined, item_sku?: string | undefined, item_name?: string | undefined, item_description?: string | undefined, notes?: string | undefined, taxrate_id?: string | undefined, account_id?: string | undefined, category_ids?: PropertyAccountingLineitemCategoryIds | undefined, locations?: PropertyAccountingLineitemLocations | undefined, item_variants?: PropertyAccountingLineitemItemVariants | undefined, fees?: PropertyAccountingLineitemFees | undefined, contact_id?: string | undefined, project_id?: string | undefined, exchange_rate?: number | undefined, is_billable?: boolean | undefined}; export type PropertyAccountingInvoiceLineitems = Array; export type AccountingAttachment = {id?: string | undefined, download_url?: string | undefined, name?: string | undefined, mime_type?: string | undefined}; export type PropertyAccountingInvoiceAttachments = Array; export type AccountingPaymentReference = {payment_id?: string | undefined, amount?: number | undefined, allocated_at?: string | undefined}; export type PropertyAccountingInvoicePayments = Array; export type PropertyAccountingInvoiceCategoryIds = Array; export type AccountingMetadata = {id?: string | undefined, slug?: string | undefined, value?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, namespace?: string | undefined, format?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'MEASUREMENT' | 'PRICE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, extra_data?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined}; export type PropertyAccountingInvoiceMetadata = Array; export type PropertyAccountingInvoiceSalesorderIds = Array; export type AccountingInvoice = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, due_at?: string | undefined, paid_at?: string | undefined, refunded_at?: string | undefined, cancelled_at?: string | undefined, posted_at?: string | undefined, total_amount?: number | undefined, paid_amount?: number | undefined, refund_amount?: number | undefined, tax_amount?: number | undefined, discount_amount?: number | undefined, balance_amount?: number | undefined, invoice_number?: string | undefined, reference?: string | undefined, contact_id?: string | undefined, currency?: string | undefined, notes?: string | undefined, extended_notes?: PropertyAccountingInvoiceExtendedNotes | undefined, refund_reason?: string | undefined, term?: ('ON_RECEIPT' | 'NET_7' | 'NET_10' | 'NET_15' | 'NET_20' | 'NET_25' | 'NET_30' | 'NET_45' | 'NET_60' | 'NET_90' | 'OTHER') | undefined, payment_terms?: ('ON_RECEIPT' | 'NET_7' | 'NET_10' | 'NET_15' | 'NET_20' | 'NET_25' | 'NET_30' | 'NET_45' | 'NET_60' | 'NET_90' | 'OTHER') | undefined, paymentterm_id?: string | undefined, lineitems?: PropertyAccountingInvoiceLineitems | undefined, status?: ('DRAFT' | 'VOIDED' | 'AUTHORIZED' | 'PAID' | 'PARTIALLY_PAID' | 'PARTIALLY_REFUNDED' | 'REFUNDED' | 'SUBMITTED' | 'DELETED' | 'OVERDUE') | undefined, url?: string | undefined, payment_collection_method?: ('send_invoice' | 'charge_automatically') | undefined, type?: ('INVOICE' | 'CREDITMEMO') | undefined, attachments?: PropertyAccountingInvoiceAttachments | undefined, send?: boolean | undefined, organization_id?: string | undefined, payments?: PropertyAccountingInvoicePayments | undefined, category_ids?: PropertyAccountingInvoiceCategoryIds | undefined, project_id?: string | undefined, metadata?: PropertyAccountingInvoiceMetadata | undefined, sub_total_amount?: number | undefined, exchange_rate?: number | undefined, is_inclusive_of_tax?: boolean | undefined, salesorder_ids?: PropertyAccountingInvoiceSalesorderIds | undefined, raw?: {[key: string]: unknown} | undefined}; export type CreateApiAccountingConnectionIdInvoiceArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'due_at' | 'paid_at' | 'refunded_at' | 'cancelled_at' | 'posted_at' | 'total_amount' | 'paid_amount' | 'refund_amount' | 'tax_amount' | 'discount_amount' | 'balance_amount' | 'invoice_number' | 'reference' | 'contact_id' | 'currency' | 'notes' | 'extended_notes' | 'refund_reason' | 'term' | 'payment_terms' | 'paymentterm_id' | 'lineitems' | 'status' | 'url' | 'payment_collection_method' | 'type' | 'attachments' | 'send' | 'organization_id' | 'payments' | 'category_ids' | 'project_id' | 'metadata' | 'sub_total_amount' | 'exchange_rate' | 'is_inclusive_of_tax' | 'salesorder_ids' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, body: AccountingInvoice}; export type CreateApiAccountingConnectionIdInvoiceOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyAccountingExtendedNoteUser = z.object({id: z.string().optional(), name: z.string().optional()}); export const accountingExtendedNote = z.object({note: z.string().optional(), user: propertyAccountingExtendedNoteUser.optional(), date: z.string().optional(), visibility: z.enum(["PUBLIC", "PRIVATE"]).optional()}); export const propertyAccountingInvoiceExtendedNotes = z.array(accountingExtendedNote); export const propertyAccountingLineitemCategoryIds = z.array(z.string()); export const accountingReference = z.object({id: z.string().optional(), name: z.string().optional()}); export const propertyAccountingLineitemLocations = z.array(accountingReference); export const propertyAccountingLineitemItemVariants = z.array(accountingReference); export const accountingFee = z.object({type: z.enum(["TAX", "DISCOUNT", "PROMOTION", "SHIPPING", "GIFT_WRAP", "COD", "SURCHARGE", "TIP", "SERVICE_CHARGE", "OTHER"]), original_type: z.string().optional(), amount: z.number(), currency: z.string().optional()}); export const propertyAccountingLineitemFees = z.array(accountingFee); export const accountingLineitem = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), refunded_at: z.string().optional(), total_amount: z.number().optional(), refund_amount: z.number().optional(), discount_amount: z.number().optional(), tax_amount: z.number().optional(), item_id: z.string().optional(), unit_amount: z.number().optional(), unit_quantity: z.number().optional(), item_sku: z.string().optional(), item_name: z.string().optional(), item_description: z.string().optional(), notes: z.string().optional(), taxrate_id: z.string().optional(), account_id: z.string().optional(), category_ids: propertyAccountingLineitemCategoryIds.optional(), locations: propertyAccountingLineitemLocations.optional(), item_variants: propertyAccountingLineitemItemVariants.optional(), fees: propertyAccountingLineitemFees.optional(), contact_id: z.string().optional(), project_id: z.string().optional(), exchange_rate: z.number().optional(), is_billable: z.boolean().optional()}); export const propertyAccountingInvoiceLineitems = z.array(accountingLineitem); export const accountingAttachment = z.object({id: z.string().optional(), download_url: z.string().optional(), name: z.string().optional(), mime_type: z.string().optional()}); export const propertyAccountingInvoiceAttachments = z.array(accountingAttachment); export const accountingPaymentReference = z.object({payment_id: z.string().optional(), amount: z.number().optional(), allocated_at: z.string().optional()}); export const propertyAccountingInvoicePayments = z.array(accountingPaymentReference); export const propertyAccountingInvoiceCategoryIds = z.array(z.string()); export const accountingMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertyAccountingInvoiceMetadata = z.array(accountingMetadata); export const propertyAccountingInvoiceSalesorderIds = z.array(z.string()); export const accountingInvoice = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), due_at: z.string().optional(), paid_at: z.string().optional(), refunded_at: z.string().optional(), cancelled_at: z.string().optional(), posted_at: z.string().optional(), total_amount: z.number().optional(), paid_amount: z.number().optional(), refund_amount: z.number().optional(), tax_amount: z.number().optional(), discount_amount: z.number().optional(), balance_amount: z.number().optional(), invoice_number: z.string().optional(), reference: z.string().optional(), contact_id: z.string().optional(), currency: z.string().optional(), notes: z.string().optional(), extended_notes: propertyAccountingInvoiceExtendedNotes.optional(), refund_reason: z.string().optional(), term: z.enum(["ON_RECEIPT", "NET_7", "NET_10", "NET_15", "NET_20", "NET_25", "NET_30", "NET_45", "NET_60", "NET_90", "OTHER"]).optional(), payment_terms: z.enum(["ON_RECEIPT", "NET_7", "NET_10", "NET_15", "NET_20", "NET_25", "NET_30", "NET_45", "NET_60", "NET_90", "OTHER"]).optional(), paymentterm_id: z.string().optional(), lineitems: propertyAccountingInvoiceLineitems.optional(), status: z.enum(["DRAFT", "VOIDED", "AUTHORIZED", "PAID", "PARTIALLY_PAID", "PARTIALLY_REFUNDED", "REFUNDED", "SUBMITTED", "DELETED", "OVERDUE"]).optional(), url: z.string().optional(), payment_collection_method: z.enum(["send_invoice", "charge_automatically"]).optional(), type: z.enum(["INVOICE", "CREDITMEMO"]).optional(), attachments: propertyAccountingInvoiceAttachments.optional(), send: z.boolean().optional(), organization_id: z.string().optional(), payments: propertyAccountingInvoicePayments.optional(), category_ids: propertyAccountingInvoiceCategoryIds.optional(), project_id: z.string().optional(), metadata: propertyAccountingInvoiceMetadata.optional(), sub_total_amount: z.number().optional(), exchange_rate: z.number().optional(), is_inclusive_of_tax: z.boolean().optional(), salesorder_ids: propertyAccountingInvoiceSalesorderIds.optional(), raw: z.record(z.string(), z.unknown()).optional()}); 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 an invoice * * POST /accounting/{connection_id}/invoice */ export const createApiAccountingConnectionIdInvoice = async (args: CreateApiAccountingConnectionIdInvoiceArgs, options?: CreateApiAccountingConnectionIdInvoiceOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/accounting/${encodeURIComponent(String(args.connection_id))}/invoice${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(accountingInvoice, await res.json()) };