aboutsummaryrefslogtreecommitdiff
path: root/src/models/invoice.ts
blob: 7905e4d4541dff767dbf4b30249f3cb984f2ede0 (plain)
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
import z from 'zod';

export const CurrencyEnum = z.enum(['USD', 'EUR', 'RSD']);

export const LineItemSchema = z.object({
  description: z.string(),
  quantity: z.number().min(0, "Quantity can't be less than 0"),
  rate: z.number().min(0, "Rate can't be less than 0"),
});

const optionalDate = z.preprocess((v) => {
  if (v === '' || v === null) return undefined;
  if (v instanceof Date && isNaN(v.getTime())) return undefined;
  return v;
}, z.coerce.date().optional());

export const InvoiceSchema = z.object({
  invoice_number: z.number(),
  from: z.string(),
  bill_to: z.string().optional(),
  ship_to: z.string().optional(),
  date: optionalDate,
  payment_terms: z.string().optional(),
  due_date: optionalDate,
  po_number: z.string().optional(),
  currency: CurrencyEnum,
  line_items: z.array(LineItemSchema),
  notes: z.string().optional(),
  terms: z.string().optional(),
});

export type Currency = z.infer<typeof CurrencyEnum>;
export type Invoice = z.infer<typeof InvoiceSchema>;
export type LineItem = z.infer<typeof LineItemSchema>;