import {z} from 'zod' export type ApiChannelEmailRecipients = {bcc?: Array | undefined, cc?: Array | undefined, to: Array}; export type ApiChannelEmail = {message: string, recipients: ApiChannelEmailRecipients, subject: string}; export type MediumEnum = 'email'; export type ApiChannel = {data: ApiChannelEmail, medium: MediumEnum}; export type ChargeReasonCodeEnum = 'AA' | 'AAA' | 'AAC' | 'AAD' | 'AAE' | 'AAF' | 'AAH' | 'AAI' | 'AAS' | 'AAT' | 'AAV' | 'AAY' | 'AAZ' | 'ABA' | 'ABB' | 'ABC' | 'ABD' | 'ABF' | 'ABK' | 'ABL' | 'ABN' | 'ABR' | 'ABS' | 'ABT' | 'ABU' | 'ACF' | 'ACG' | 'ACH' | 'ACI' | 'ACJ' | 'ACK' | 'ACL' | 'ACM' | 'ACS' | 'ADC' | 'ADE' | 'ADJ' | 'ADK' | 'ADL' | 'ADM' | 'ADN' | 'ADO' | 'ADP' | 'ADQ' | 'ADR' | 'ADT' | 'ADW' | 'ADY' | 'ADZ' | 'AEA' | 'AEB' | 'AEC' | 'AED' | 'AEF' | 'AEH' | 'AEI' | 'AEJ' | 'AEK' | 'AEL' | 'AEM' | 'AEN' | 'AEO' | 'AEP' | 'AES' | 'AET' | 'AEU' | 'AEV' | 'AEW' | 'AEX' | 'AEY' | 'AEZ' | 'AJ' | 'AU' | 'CA' | 'CAB' | 'CAD' | 'CAE' | 'CAF' | 'CAI' | 'CAJ' | 'CAK' | 'CAL' | 'CAM' | 'CAN' | 'CAO' | 'CAP' | 'CAQ' | 'CAR' | 'CAS' | 'CAT' | 'CAU' | 'CAV' | 'CAW' | 'CD' | 'CG' | 'CS' | 'CT' | 'DAB' | 'DAD' | 'DL' | 'EG' | 'EP' | 'ER' | 'FAA' | 'FAB' | 'FAC' | 'FC' | 'FH' | 'FI' | 'GAA' | 'HAA' | 'HD' | 'HH' | 'IAA' | 'IAB' | 'ID' | 'IF' | 'IR' | 'IS' | 'KO' | 'L1' | 'LA' | 'LAA' | 'LAB' | 'LF' | 'MAE' | 'MI' | 'ML' | 'NAA' | 'OA' | 'PA' | 'PAA' | 'PC' | 'PL' | 'RAB' | 'RAC' | 'RAD' | 'RAF' | 'RE' | 'RF' | 'RH' | 'RV' | 'SA' | 'SAA' | 'SAD' | 'SAE' | 'SAI' | 'SG' | 'SH' | 'SM' | 'SU' | 'TAB' | 'TAC' | 'TT' | 'TV' | 'V1' | 'V2' | 'WH' | 'XAA' | 'YY'; export type ExemptionReasonEnum = 'VATEX-SA-29' | 'VATEX-SA-29-7' | 'VATEX-SA-30' | 'VATEX-SA-32' | 'VATEX-SA-32(bis)' | 'VATEX-SA-33' | 'VATEX-SA-34-1' | 'VATEX-SA-34-2' | 'VATEX-SA-34-3' | 'VATEX-SA-34-4' | 'VATEX-SA-34-5' | 'VATEX-SA-35' | 'VATEX-SA-36' | 'VATEX-SA-DIPLOMAT' | 'VATEX-SA-DUTYFREE' | 'VATEX-SA-EDU' | 'VATEX-SA-HEA' | 'VATEX-SA-MLTRY' | 'VATEX-SA-OOS' | 'VATEX-SA-ROYALDECREE'; export type TaxCategoryCodeEnum = 'E' | 'O' | 'S' | 'Z'; export type ApiV1ZatcaDocumentReportTaxWrite = {exemption_reason?: ExemptionReasonEnum | undefined, exemption_reason_text?: string | undefined, percent: number, type: TaxCategoryCodeEnum}; export type ApiV1ZatcaDocumentReportChargeWrite = {amount?: number | undefined, base_amount?: number | undefined, percent?: number | undefined, reason: ChargeReasonCodeEnum, reason_text?: string | undefined, tax: ApiV1ZatcaDocumentReportTaxWrite}; export type CurrencyEnum = 'AED' | 'SAR' | 'USD' | 'EUR' | 'CAD' | 'AFN' | 'ALL' | 'AMD' | 'ARS' | 'AUD' | 'AZN' | 'BAM' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BND' | 'BOB' | 'BRL' | 'BWP' | 'BYN' | 'BZD' | 'CDF' | 'CHF' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'GBP' | 'GEL' | 'GHS' | 'GNF' | 'GTQ' | 'HKD' | 'HNL' | 'HRK' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KHR' | 'KMF' | 'KRW' | 'KWD' | 'KZT' | 'LBP' | 'LKR' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MOP' | 'MUR' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SDG' | 'SEK' | 'SGD' | 'SOS' | 'SYP' | 'THB' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'UYU' | 'UZS' | 'VES' | 'VND' | 'XAF' | 'XOF' | 'YER' | 'ZAR' | 'ZMW'; export type CountryCodeEnum = 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AQ' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BV' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GS' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HM' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PN' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TF' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'UM' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW' | '1A' | 'XI'; export type ApiV1ZatcaDocumentReportCustomerAddressWrite = {additional_street?: string | undefined, building?: string | undefined, city: string, country: CountryCodeEnum, district?: string | undefined, postal_code?: string | undefined, province?: string | undefined, street: string}; export type CustomerPartyIdentificationEnum = 'CRN' | 'GCC' | 'IQA' | 'MLS' | 'SAG' | 'MOM' | 'NAT' | '700' | 'OTH' | 'PAS' | 'TIN'; export type ApiV1ZatcaDocumentReportCustomerIdentificationWrite = {type: CustomerPartyIdentificationEnum, value: string}; export type ApiV1ZatcaDocumentReportCustomerWrite = {address: ApiV1ZatcaDocumentReportCustomerAddressWrite, identification?: ApiV1ZatcaDocumentReportCustomerIdentificationWrite | undefined, name: string, tax_registration_number?: string | undefined}; export type DiscountReasonCodeEnum = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13' | '14' | '15' | '16' | '17' | '18' | '19' | '20' | '21' | '22' | '23' | '24' | '25' | '26' | '27' | '28' | '29' | '30' | '31' | '32' | '33' | '34' | '35' | '36' | '37' | '38' | '39' | '40' | '41' | '42' | '44' | '45' | '46' | '47' | '48' | '49' | '50' | '51' | '52' | '53' | '54' | '55' | '56' | '57' | '58' | '59' | '60' | '61' | '62' | '63' | '64' | '65' | '66' | '67' | '68' | '69' | '70' | '71' | '72' | '73' | '74' | '75' | '76' | '77' | '78' | '79' | '80' | '81' | '82' | '83' | '84' | '85' | '86' | '87' | '88' | '89' | '90' | '91' | '92' | '93' | '94' | '95' | '96' | '97' | '98' | '99' | '100' | '101' | '102' | '103' | '104' | '105'; export type ApiV1ZatcaDocumentReportDiscountWrite = {amount?: number | undefined, base_amount?: number | undefined, percent?: number | undefined, reason: DiscountReasonCodeEnum, tax: ApiV1ZatcaDocumentReportTaxWrite}; export type IndicatorsEnum = 'SUMMARY' | 'EXPORT' | 'NOMINAL' | 'THIRD_PARTY' | 'SELF_BILLING'; export type ApiV1ZatcaDocumentReportLineChargeWrite = {amount?: number | undefined, base_amount?: number | undefined, percent?: number | undefined, reason: ChargeReasonCodeEnum, reason_text?: string | undefined}; export type ApiV1ZatcaDocumentReportLineDiscountWrite = {amount?: number | undefined, base_amount?: number | undefined, percent?: number | undefined, reason: DiscountReasonCodeEnum}; export type ApiV1ZatcaDocumentLineStandardItemIdentifierWrite = {scheme: string, value: string}; export type ApiV1ZatcaDocumentLineIdentifiersWrite = {customer_id?: string | undefined, standard_id?: (ApiV1ZatcaDocumentLineStandardItemIdentifierWrite | null) | undefined, supplier_id?: string | undefined}; export type ApiV1ZatcaDocumentLineWrite = {charges?: Array | undefined, description: string, discounts?: Array | undefined, identifiers?: ApiV1ZatcaDocumentLineIdentifiersWrite | undefined, item: string, quantity: number, tax: ApiV1ZatcaDocumentReportTaxWrite, unit_of_measure?: string | undefined, unit_price: number}; export type PaymentMeansCodeEnum = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13' | '14' | '15' | '16' | '17' | '18' | '19' | '20' | '21' | '22' | '23' | '24' | '25' | '26' | '27' | '28' | '29' | '30' | '31' | '32' | '33' | '34' | '35' | '36' | '37' | '38' | '39' | '40' | '41' | '42' | '43' | '44' | '45' | '46' | '47' | '48' | '49' | '50' | '51' | '52' | '53' | '54' | '55' | '56' | '57' | '58' | '59' | '60' | '61' | '62' | '63' | '64' | '65' | '66' | '67' | '68' | '70' | '74' | '75' | '76' | '77' | '78' | '91' | '92' | '93' | '94' | '95' | '96' | '97'; export type ApiV1ZatcaDocumentReportPaymentMeansRequiredWrite = {account_id?: string | undefined, code: PaymentMeansCodeEnum, issue_reason: string, payment_terms?: string | undefined}; export type ApiV1ZatcaDocumentPrepaymentLineWrite = {amount: number, issue_datetime: string, reference: string, tax: ApiV1ZatcaDocumentReportTaxWrite}; export type ApiV1ZatcaDocumentReportSupplierAddressWrite = {additional_street?: string | undefined, building: string, city: string, country: CountryCodeEnum, district: string, postal_code: string, province?: string | undefined, street: string}; export type SupplierPartyIdentificationEnum = 'CRN' | 'MLS' | 'SAG' | 'MOM' | '700' | 'OTH'; export type ApiV1ZatcaDocumentReportSupplierIdentificationWrite = {type: SupplierPartyIdentificationEnum, value: string}; export type ApiV1ZatcaDocumentReportSupplierWrite = {address: ApiV1ZatcaDocumentReportSupplierAddressWrite, identification: ApiV1ZatcaDocumentReportSupplierIdentificationWrite, name: string}; export type TaxAmountTypeEnum = 'E' | 'I'; export type ApiV1ZatcaCreditNoteReportDocumentWrite = {charges?: Array | undefined, created_ts?: string | undefined, currency: CurrencyEnum, customer: ApiV1ZatcaDocumentReportCustomerWrite, discounts?: Array | undefined, document_number: string, indicators?: Array | undefined, line_items: Array, note?: string | undefined, payment_means: ApiV1ZatcaDocumentReportPaymentMeansRequiredWrite, prepayments?: Array | undefined, reference_contract_id?: string | undefined, reference_document_number: string, reference_purchase_order_id?: string | undefined, supplier: ApiV1ZatcaDocumentReportSupplierWrite, supply_date: string, supply_end_date?: string | undefined, tax_amount_type?: TaxAmountTypeEnum | undefined, tax_total?: number | undefined}; export type LanguageEnum = 'ar' | 'en'; export type ApiV1ZatcaDocumentSendTemplateConfigAllowancesLabelsWrite = {ar: string, en: string}; export type ApiV1ZatcaDocumentSendTemplateConfigAllowancesWrite = {labels: ApiV1ZatcaDocumentSendTemplateConfigAllowancesLabelsWrite, reasons: Array}; export type ApiV1ZatcaDocumentSendTemplateConfigWrite = {charges?: Array | undefined, discounts?: Array | undefined}; export type ApiV1ZatcaDocumentSendTemplateWrite = {config?: ApiV1ZatcaDocumentSendTemplateConfigWrite | undefined, context: unknown, id?: string | undefined}; export type ApiV1ZatcaCreditNoteReportWrite = {channels?: Array | undefined, document: ApiV1ZatcaCreditNoteReportDocumentWrite, language?: LanguageEnum | undefined, metadata?: {[key: string]: string} | undefined, reference?: string | undefined, template?: ApiV1ZatcaDocumentSendTemplateWrite | undefined}; export type CreateApiZatcaCreditNotesReportArgs = {'X-ZATCA-Branch'?: string | undefined, 'X-ZATCA-Connected-Account-Id'?: string | undefined, 'X-ZATCA-Environment'?: string | undefined, body: ApiV1ZatcaCreditNoteReportWrite}; export type CreateApiZatcaCreditNotesReportOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiV1ZatcaDocumentDetailRead = z.object({document_number: z.string(), id: z.string(), issue_datetime: z.string(), supply_date: z.string()}); export const apiV1ZatcaDetailRead = z.object({created_ts: z.string(), document: apiV1ZatcaDocumentDetailRead, metadata: z.record(z.string(), z.string()), qr_code_data: z.string(), reference: z.string(), reported_ts: z.string().nullable(), response: z.unknown(), sent_ts: z.string().nullable(), status: z.string()}); 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 }; /** * Report a credit note * * * POST /zatca/credit-notes/report/ */ export const createApiZatcaCreditNotesReport = async (args: CreateApiZatcaCreditNotesReportArgs, options?: CreateApiZatcaCreditNotesReportOptions) => { const {baseUrl = "https://api.wafeq.com/v1", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-ZATCA-Branch'] != null) headers.set("X-ZATCA-Branch", String(args['X-ZATCA-Branch'])) if (args['X-ZATCA-Connected-Account-Id'] != null) headers.set("X-ZATCA-Connected-Account-Id", String(args['X-ZATCA-Connected-Account-Id'])) if (args['X-ZATCA-Environment'] != null) headers.set("X-ZATCA-Environment", String(args['X-ZATCA-Environment'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/zatca/credit-notes/report/${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(apiV1ZatcaDetailRead, await res.json()) };