import {z} from 'zod' export type WithholidingTaxItem = {incomeType: number, description?: string | undefined, taxRate: number, taxAmount: number, taxAmountNoVat: number, withheld: number, total: number, vatType: number}; export type WithholidingTaxDocument = {contactCode?: string | undefined, contactName: string, contactAddress?: string | undefined, contactTaxId?: string | undefined, contactBranch?: string | undefined, contactPerson?: string | undefined, contactEmail?: string | undefined, contactNumber?: string | undefined, contactZipCode?: string | undefined, contactGroup?: number | undefined, publishedOn?: string | undefined, entity: number, textOther?: string | undefined, withholdingTaxItems: Array, total: number, totalTaxWithheld: number, taxPayment?: number | undefined, taxPaymentOthers?: string | undefined, providentFundNumber?: string | undefined, providentFundAmount?: string | undefined, socialSecurityAmount?: string | undefined, remarks?: string | undefined, internalNotes?: string | undefined, showSignatureOrStamp?: boolean | undefined, externalDocumentId?: string | undefined}; export type CreateApiWithholdingTaxesArgs = {Authorization: string, body: WithholidingTaxDocument}; export type CreateApiWithholdingTaxesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const withholidingTaxItem = z.object({incomeType: z.number().int(), description: z.string().optional(), taxRate: z.number(), taxAmount: z.number(), taxAmountNoVat: z.number(), withheld: z.number(), total: z.number(), vatType: z.number().int()}); export const withholidingTaxDocumentResponse = z.object({status: z.boolean().optional(), message: z.string().optional(), code: z.number().int().optional(), data: z.object({recordId: z.string().optional(), documentId: z.string().optional(), documentSerial: z.string().optional(), contactCode: z.string().optional(), contactName: z.string().optional(), contactAddress: z.string().optional(), contactTaxId: z.string().optional(), contactBranch: z.string().optional(), contactPerson: z.string().optional(), contactEmail: z.string().optional(), contactNumber: z.string().optional(), contactZipCode: z.string().optional(), contactGroup: z.number().int().optional(), publishedOn: z.string().optional(), entity: z.number().int().optional(), textOther: z.string().optional(), withholdingTaxItems: z.array(withholidingTaxItem).optional(), total: z.number().optional(), totalTaxWithheld: z.number().optional(), taxPayment: z.number().int().optional(), taxPaymentOthers: z.string().optional(), providentFundNumber: z.string().optional(), providentFundAmount: z.string().optional(), socialSecurityAmount: z.string().optional(), remarks: z.string().optional(), internalNotes: z.string().optional(), showSignatureOrStamp: z.boolean().optional(), company: z.object({companyName: z.string().optional(), companyNameEn: z.string().optional(), companyAddress: z.string().optional(), companyAddressEn: z.string().optional(), companyTaxId: z.string().optional(), companyBranch: z.string().optional(), companyBranchEn: z.string().optional(), companyPhone: z.string().optional(), companyMobile: z.string().optional(), companyFax: z.string().optional(), companyWebsite: z.string().optional()}).optional(), status: z.number().int().optional(), statusString: z.number().int().optional(), documentType: z.number().int().optional(), allowDelete: z.boolean().optional(), isDelete: z.string().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 }; /** * Create withholding tax document. * * POST /withholding-taxes */ export const createApiWithholdingTaxes = async (args: CreateApiWithholdingTaxesArgs, options?: CreateApiWithholdingTaxesOptions) => { const {baseUrl = "https://openapi.flowaccount.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/withholding-taxes${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(withholidingTaxDocumentResponse, await res.json()) };