import {z} from 'zod' export type CreateApiApiCommercialSalesDocumentsArgs = {body: {data?: ({attributes?: ({apply_retention_when_paid?: boolean | undefined, currency_conversion_rate?: number | undefined, currency_iso_code?: string | undefined, customer_id?: number | undefined, date?: string | undefined, document_type?: string | undefined, due_date?: string | undefined, external_reference?: string | undefined, notes?: string | undefined, operation_country?: string | undefined, payment_mechanism?: string | undefined, retention?: number | undefined, retention_type?: string | undefined, settlement_expression?: string | undefined, vat_included_prices?: boolean | undefined}) | undefined, type?: string | undefined}) | undefined}}; export type CreateApiApiCommercialSalesDocumentsOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiCommercialSalesDocumentsResponse = z.object({data: z.object({attributes: z.object({acts_as_shipment_document: z.boolean().optional(), apply_retention_when_paid: z.boolean().optional(), base_currency_conversion_difference: z.unknown(), base_currency_gross_total: z.unknown(), base_currency_net_total: z.unknown(), base_currency_other_retentions: z.unknown(), base_currency_other_taxes_total: z.unknown(), base_currency_pending_total: z.unknown(), base_currency_retention_aware_gross_total: z.unknown(), base_currency_retention_total: z.unknown(), base_currency_settlement_total: z.unknown(), base_currency_tax_payable: z.unknown(), base_currency_vat_incidence_int: z.unknown(), base_currency_vat_incidence_ise: z.unknown(), base_currency_vat_incidence_nor: z.unknown(), base_currency_vat_incidence_red: z.unknown(), base_currency_vat_total_int: z.unknown(), base_currency_vat_total_nor: z.unknown(), base_currency_vat_total_red: z.unknown(), cashed_vat: z.boolean().optional(), child_documents_ids: z.unknown(), communication_code: z.unknown(), communication_code_source: z.string().optional(), communication_status: z.string().optional(), created_at: z.string().optional(), currency_amount: z.unknown(), currency_conversion_rate: z.number().optional(), currency_iso_code: z.string().optional(), customer_address_detail: z.string().optional(), customer_business_name: z.string().optional(), customer_city: z.string().optional(), customer_country: z.string().optional(), customer_postcode: z.string().optional(), customer_tax_country_region: z.string().optional(), customer_tax_registration_number: z.string().optional(), date: z.string().optional(), description: z.unknown(), document_area: z.string().optional(), document_hash_sum: z.unknown(), document_no: z.unknown(), document_series_no: z.unknown(), document_series_prefix: z.string().optional(), document_type: z.string().optional(), due_date: z.string().optional(), due_days: z.unknown(), email_readed: z.boolean().optional(), emailed: z.unknown(), end_date: z.unknown(), expected_shipment_unloading_time: z.unknown(), expired: z.unknown(), external_reference: z.string().optional(), fulfilled_mask: z.number().optional(), gross_total: z.number().optional(), hash_control: z.unknown(), invoice_counter: z.unknown(), is_active: z.unknown(), is_document_to_self: z.boolean().optional(), is_invoice_receipt: z.unknown(), is_receipt: z.unknown(), is_third_party: z.boolean().optional(), issue_finalized: z.unknown(), line_settlement_with_tax: z.number().optional(), line_settlement_with_tax_unrounded: z.number().optional(), line_settlement_without_tax: z.number().optional(), line_settlement_without_tax_unrounded: z.number().optional(), made_available_to: z.boolean().optional(), manual_registration_number: z.string().optional(), manual_registration_series: z.string().optional(), manual_registration_type: z.unknown(), net_settlement_total: z.unknown(), net_total: z.number().optional(), notes: z.string().optional(), operation_country: z.string().optional(), other_retentions: z.number().optional(), other_taxes_total: z.number().optional(), parent_document_area: z.unknown(), parent_document_reference: z.string().optional(), parent_document_type: z.unknown(), parent_documents_ids: z.unknown(), payment_mechanism: z.string().optional(), pending_retention_value: z.number().optional(), pending_total: z.number().optional(), periodicity: z.unknown(), periodicity_type: z.unknown(), printed: z.unknown(), public_link: z.string().optional(), receipts_ids: z.unknown(), reference: z.unknown(), reference_document_type: z.unknown(), required_mask: z.number().optional(), retention: z.number().optional(), retention_aware_gross_total: z.number().optional(), retention_type: z.string().optional(), retention_value: z.number().optional(), retention_value_unrounded: z.number().optional(), scheduled_description: z.unknown(), scheduled_end_date: z.unknown(), scheduled_interval: z.unknown(), scheduled_invoice_id: z.unknown(), scheduled_start_date: z.unknown(), send_email: z.unknown(), settlement: z.unknown(), settlement_expression: z.string().optional(), settlement_percentage: z.number().optional(), settlement_total: z.number().optional(), settlement_with_tax: z.number().optional(), settlement_with_tax_unrounded: z.number().optional(), settlement_without_tax: z.number().optional(), settlement_without_tax_unrounded: z.number().optional(), shipment_address_detail: z.unknown(), shipment_city: z.unknown(), shipment_country: z.unknown(), shipment_from_address_detail: z.unknown(), shipment_from_city: z.unknown(), shipment_from_country: z.unknown(), shipment_from_postcode: z.unknown(), shipment_loading_time: z.unknown(), shipment_postcode: z.unknown(), sort_status: z.number().optional(), start_date: z.unknown(), status: z.number().optional(), system_entry_date: z.unknown(), tax_payable: z.number().optional(), third_party_type: z.number().optional(), total_pending_quantity: z.unknown(), updated_at: z.string().optional(), vat_incidence_int: z.number().optional(), vat_incidence_int_unrounded: z.number().optional(), vat_incidence_ise: z.number().optional(), vat_incidence_ise_unrounded: z.number().optional(), vat_incidence_nor: z.number().optional(), vat_incidence_nor_unrounded: z.number().optional(), vat_incidence_red: z.number().optional(), vat_incidence_red_unrounded: z.number().optional(), vat_included_prices: z.boolean().optional(), vat_percentage_int: z.unknown(), vat_percentage_nor: z.unknown(), vat_percentage_red: z.unknown(), vat_total_int: z.number().optional(), vat_total_int_unrounded: z.number().optional(), vat_total_nor: z.number().optional(), vat_total_nor_unrounded: z.number().optional(), vat_total_red: z.number().optional(), vat_total_red_unrounded: z.number().optional(), vehicle_registration: z.unknown(), voided_reason: z.unknown()}).optional(), id: z.string().optional(), relationships: z.object({bank_accounts: z.object({data: z.unknown()}).optional(), cash_accounts: z.object({data: z.unknown()}).optional(), commercial_document_series: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), company: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), currency: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), customer: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional(), lines: z.object({data: z.array(z.unknown()).optional()}).optional(), tax_exemption_reasons: z.object({data: z.unknown()}).optional(), user: z.object({data: z.object({id: z.string().optional(), type: z.string().optional()}).optional()}).optional()}).optional(), type: z.string().optional()}).optional(), meta: z.object({observed: z.object({scalar: z.number().optional()}).optional()}).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 }; /** * Criar CabeƧalho de Documentos de Venda * * POST /api/commercial_sales_documents */ export const createApiApiCommercialSalesDocuments = async (args: CreateApiApiCommercialSalesDocumentsArgs, options: CreateApiApiCommercialSalesDocumentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/commercial_sales_documents${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(createApiApiCommercialSalesDocumentsResponse, await res.json()) };