| 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 | unique, |
| 12 | } from 'drizzle-orm/pg-core' |
| 13 | import { members } |
| 14 | import { media } |
| 15 | import { projects } |
| 16 | import { requirements } |
| 17 | |
| 18 | export const invoiceStatusEnum = |
| 19 | 'draft', |
| 20 | 'sent', |
| 21 | 'disputed', |
| 22 | 'paid', |
| 23 | 'cancelled', |
| 24 | ]) |
| 25 | |
| 26 | export const invoiceRecipientEnum = |
| 27 | 'member', |
| 28 | 'client', |
| 29 | ]) |
| 30 | |
| 31 | export const invoices = |
| 32 | 'invoices', |
| 33 | { |
| 34 | id: text('id |
| 35 | .primaryKey() |
| 36 | .$defaultFn(() => |
| 37 | projectId: text('project_id |
| 38 | .references(() => |
| 39 | .notNull(), |
| 40 | invoiceNumber: text('invoice_number |
| 41 | status: invoiceStatusEnum('status |
| 42 | issueDate: timestamp('issue_date |
| 43 | dueDate: timestamp('due_date |
| 44 | notes: text('notes |
| 45 | totalAmount: numeric('total_amount |
| 46 | .default('0') |
| 47 | .notNull(), |
| 48 | currency: text('currency |
| 49 | senderLogo: text('sender_logo |
| 50 | onDelete: 'set null', |
| 51 | }), |
| 52 | senderSignature: text('sender_signature |
| 53 | onDelete: 'set null', |
| 54 | }), |
| 55 | senderName: text('sender_name |
| 56 | senderAddress: text('sender_address |
| 57 | senderCustomFields: jsonb('sender_custom_fields |
| 58 | { label: string; |
| 59 | >(), |
| 60 | // Client (To) details |
| 61 | clientName: text('client_name |
| 62 | clientAddress: text('client_address |
| 63 | clientCustomFields: jsonb('client_custom_fields |
| 64 | { label: string; |
| 65 | >(), |
| 66 | // Additional details |
| 67 | paymentTerms: text('payment_terms |
| 68 | terms: text('terms |
| 69 | // Discount |
| 70 | discountLabel: text('discount_label |
| 71 | discountAmount: numeric('discount_amount |
| 72 | |
| 73 | recipient: invoiceRecipientEnum().default |
| 74 | createdAt: timestamp('created_at |
| 75 | updatedAt: timestamp('updated_at |
| 76 | .defaultNow() |
| 77 | .$onUpdate(() => |
| 78 | .notNull(), |
| 79 | }, |
| 80 | (table) => |
| 81 | index('invoices_project_id_status_idx'). |
| 82 | unique().on(table |
| 83 | ] |
| 84 | ) |
| 85 | |
| 86 | export const invoiceRecipients = |
| 87 | 'invoice_recipients', |
| 88 | { |
| 89 | id: text('id |
| 90 | .primaryKey() |
| 91 | .$defaultFn(() => |
| 92 | invoiceId: text('invoice_id |
| 93 | .references(() => |
| 94 | .notNull(), |
| 95 | memberId: text('member_id |
| 96 | .references(() => |
| 97 | .notNull(), |
| 98 | createdAt: timestamp('created_at |
| 99 | updatedAt: timestamp('updated_at |
| 100 | .defaultNow() |
| 101 | .$onUpdate(() => |
| 102 | .notNull(), |
| 103 | }, |
| 104 | (t) => |
| 105 | ) |
| 106 | |
| 107 | export const invoiceItems = |
| 108 | id: text('id |
| 109 | .primaryKey() |
| 110 | .$defaultFn(() => |
| 111 | invoiceId: text('invoice_id |
| 112 | .references(() => |
| 113 | .notNull(), |
| 114 | description: text('description |
| 115 | quantity: numeric('quantity |
| 116 | unitPrice: numeric('unit_price |
| 117 | amount: numeric('amount |
| 118 | sortOrder: integer('sort_order |
| 119 | createdAt: timestamp('created_at |
| 120 | updatedAt: timestamp('updated_at |
| 121 | .defaultNow() |
| 122 | .$onUpdate(() => |
| 123 | .notNull(), |
| 124 | }) |
| 125 | |
| 126 | export const invoiceRequirements = |
| 127 | id: text('id |
| 128 | .primaryKey() |
| 129 | .$defaultFn(() => |
| 130 | invoiceId: text('invoice_id |
| 131 | .references(() => |
| 132 | .notNull(), |
| 133 | requirementId: text('requirement_id |
| 134 | .references(() => |
| 135 | .notNull(), |
| 136 | createdAt: timestamp('created_at |
| 137 | updatedAt: timestamp('updated_at |
| 138 | .defaultNow() |
| 139 | .$onUpdate(() => |
| 140 | .notNull(), |
| 141 | }) |