| 1 | import { createId } from '@paralleldrive/cuid2' |
| 2 | import { |
| 3 | index, |
| 4 | integer, |
| 5 | jsonb, |
| 6 | numeric, |
| 7 | pgEnum, |
| 8 | pgTable, |
| 9 | text, |
| 10 | timestamp, |
| 11 | } from 'drizzle-orm/pg-core' |
| 12 | import { members } |
| 13 | import { media } |
| 14 | import { projects } |
| 15 | import { requirements } |
| 16 | |
| 17 | export const invoiceStatusEnum = |
| 18 | 'draft', |
| 19 | 'sent', |
| 20 | 'disputed', |
| 21 | 'paid', |
| 22 | 'cancelled', |
| 23 | ]) |
| 24 | |
| 25 | export const invoices = |
| 26 | 'invoices', |
| 27 | { |
| 28 | id: text('id |
| 29 | .primaryKey() |
| 30 | .$defaultFn(() => |
| 31 | projectId: text('project_id |
| 32 | .references(() => |
| 33 | .notNull(), |
| 34 | invoiceNumber: text('invoice_number |
| 35 | status: invoiceStatusEnum('status |
| 36 | issueDate: timestamp('issue_date |
| 37 | dueDate: timestamp('due_date |
| 38 | notes: text('notes |
| 39 | totalAmount: numeric('total_amount |
| 40 | .default('0') |
| 41 | .notNull(), |
| 42 | currency: text('currency |
| 43 | senderLogo: text('sender_logo |
| 44 | onDelete: 'set null', |
| 45 | }), |
| 46 | senderSignature: text('sender_signature |
| 47 | onDelete: 'set null', |
| 48 | }), |
| 49 | senderName: text('sender_name |
| 50 | senderAddress: text('sender_address |
| 51 | senderCustomFields: jsonb('sender_custom_fields |
| 52 | { label: string; |
| 53 | >(), |
| 54 | // Client (To) details |
| 55 | clientName: text('client_name |
| 56 | clientAddress: text('client_address |
| 57 | clientCustomFields: jsonb('client_custom_fields |
| 58 | { label: string; |
| 59 | >(), |
| 60 | // Additional details |
| 61 | paymentTerms: text('payment_terms |
| 62 | terms: text('terms |
| 63 | // Discount |
| 64 | discountLabel: text('discount_label |
| 65 | discountAmount: numeric('discount_amount |
| 66 | createdAt: timestamp('created_at |
| 67 | updatedAt: timestamp('updated_at |
| 68 | .defaultNow() |
| 69 | .$onUpdate(() => |
| 70 | .notNull(), |
| 71 | }, |
| 72 | (table) => |
| 73 | index('invoices_project_id_status_idx'). |
| 74 | ] |
| 75 | ) |
| 76 | |
| 77 | export const invoiceRecipients = |
| 78 | id: text('id |
| 79 | .primaryKey() |
| 80 | .$defaultFn(() => |
| 81 | invoiceId: text('invoice_id |
| 82 | .references(() => |
| 83 | .notNull(), |
| 84 | clientMemberId: text('client_member_id |
| 85 | .references(() => |
| 86 | .notNull(), |
| 87 | }) |
| 88 | |
| 89 | export const invoiceItems = |
| 90 | id: text('id |
| 91 | .primaryKey() |
| 92 | .$defaultFn(() => |
| 93 | invoiceId: text('invoice_id |
| 94 | .references(() => |
| 95 | .notNull(), |
| 96 | description: text('description |
| 97 | quantity: numeric('quantity |
| 98 | unitPrice: numeric('unit_price |
| 99 | amount: numeric('amount |
| 100 | sortOrder: integer('sort_order |
| 101 | }) |
| 102 | |
| 103 | export const invoiceRequirements = |
| 104 | id: text('id |
| 105 | .primaryKey() |
| 106 | .$defaultFn(() => |
| 107 | invoiceId: text('invoice_id |
| 108 | .references(() => |
| 109 | .notNull(), |
| 110 | requirementId: text('requirement_id |
| 111 | .references(() => |
| 112 | .notNull(), |
| 113 | }) |