-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.prisma
More file actions
603 lines (550 loc) · 17.5 KB
/
schema.prisma
File metadata and controls
603 lines (550 loc) · 17.5 KB
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
generator client {
provider = "prisma-client-js"
output = "./node_modules/.prisma/client"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
name String
password String
role Role @default(USER)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
employeeId String? @unique
// 🏢 COMPANY FIELDS - ADDED FOR PROFILE FUNCTIONALITY:
companyName String?
companyAddress String?
companyPhone String?
companyEmail String?
companyWebsite String?
companyVatNumber String?
companyLogo String? @db.Text // For large base64 images
useCompanyDetailsOnQuotes Boolean? @default(false) // ← CRITICAL: The missing checkbox field!
uploadedDocs Document[] @relation("DocumentUser")
jobHistory JobHistory[] @relation("JobHistoryUser")
createdOrders Order[] @relation("CreatedBy")
managedOrders Order[] @relation("ProjectOwner")
orderHistory OrderHistory[] @relation("OrderHistoryUser")
createdQuotes Quote[]
quoteHistory QuoteHistory[] @relation("QuoteHistoryUser")
timeEntries TimeEntry[]
employee Employee? @relation(fields: [employeeId], references: [id])
}
model Employee {
id String @id @default(cuid())
name String
email String? @unique
phone String?
jobTitle String
technicalQualifications String[]
isActive Boolean @default(true)
hireDate DateTime?
terminationDate DateTime?
notes String?
userId String? @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User?
@@index([jobTitle])
@@index([isActive])
}
model Job {
id String @id @default(cuid())
title String
customerReference String? // ADD THIS - Inherited from quote/order
description String?
status JobStatus @default(ACTIVE)
customerId String
startDate DateTime?
expectedEndDate DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
currentVersion Int @default(1)
documents Document[]
customer Customer @relation(fields: [customerId], references: [id])
costs JobCost[]
history JobHistory[]
materials JobMaterial[]
orders Order[]
timeEntries TimeEntry[]
@@index([customerId])
}
model JobCost {
id String @id @default(cuid())
jobId String
description String
amount Float
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
costDate DateTime @default(now())
category String?
job Job @relation(fields: [jobId], references: [id], onDelete: Cascade)
@@index([jobId])
}
model JobMaterial {
id String @id @default(cuid())
jobId String
materialId String
quantityNeeded Float @default(1)
quantityUsed Float @default(0)
quantityAllocated Float @default(0)
unitCost Float?
totalCost Float?
allocatedAt DateTime?
usedAt DateTime?
notes String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
job Job @relation(fields: [jobId], references: [id], onDelete: Cascade)
material Material @relation(fields: [materialId], references: [id], onDelete: Cascade)
@@unique([jobId, materialId])
@@index([jobId])
@@index([materialId])
}
model Customer {
id String @id @default(cuid())
name String
email String? @unique
phone String?
address String?
importSource String?
status String?
totalOrders Int?
totalSpent Float?
lastOrderDate DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
billingAddress String?
creditLimit Float?
discountPercentage Float?
paymentTerms PaymentTerms?
shippingAddress String?
specialTermsNotes String?
contactPersons ContactPerson[]
jobs Job[]
materials Material[]
orders Order[]
quotes Quote[]
@@index([email])
}
model ContactPerson {
id String @id @default(cuid())
name String
email String?
phone String?
role ContactRole?
notes String?
isPrimary Boolean @default(false)
customerId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
@@index([customerId])
@@index([email])
}
model Order {
id String @id @default(cuid())
projectTitle String
quoteRef String
customerReference String? // ADD THIS - Customer's own reference number
orderType OrderType @default(CUSTOMER_LINKED)
status OrderStatus @default(IN_PRODUCTION)
customerName String
contactPerson String?
contactPhone String?
contactEmail String?
projectValue Float
marginPercent Float?
leadTimeWeeks Int?
items Json
costBreakdown Json?
paymentTerms PaymentTerms @default(THIRTY_DAYS)
customPaymentTerms String?
currency String @default("GBP")
exchangeRate Float?
vatRate Float?
subTotal Float @default(0)
totalTax Float @default(0)
totalAmount Float @default(0)
profitMargin Float @default(0)
discounts Json?
paymentSchedule Json?
budgetAllocations Json?
notes String?
customerId String?
jobId String?
projectOwnerId String
createdById String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sourceQuoteId String?
currentVersion Int @default(1)
documents Document[]
createdBy User @relation("CreatedBy", fields: [createdById], references: [id])
customer Customer? @relation(fields: [customerId], references: [id])
job Job? @relation(fields: [jobId], references: [id])
projectOwner User @relation("ProjectOwner", fields: [projectOwnerId], references: [id])
sourceQuote Quote? @relation(fields: [sourceQuoteId], references: [id])
history OrderHistory[]
paymentMilestones PaymentMilestone[]
@@index([customerId])
@@index([jobId])
@@index([projectOwnerId])
@@index([createdById])
@@index([status])
@@index([quoteRef])
@@index([sourceQuoteId])
}
model ImportLog {
id String @id @default(cuid())
filename String
status ImportStatus
records Int
errors Json?
createdAt DateTime @default(now())
}
model RegionalTaxSetting {
id String @id @default(cuid())
country String
region String?
standardVatRate Float
reducedVatRate Float?
taxCode String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model CurrencyRate {
id String @id @default(cuid())
fromCurrency String
toCurrency String
rate Float
validFrom DateTime
validTo DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model PaymentMilestone {
id String @id @default(cuid())
orderId String
description String
amount Float
dueDate DateTime
status MilestoneStatus @default(PENDING)
paidDate DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
@@index([orderId])
}
model Supplier {
id String @id @default(cuid())
name String
email String? @unique
phone String?
address String?
rating Float?
status SupplierStatus @default(ACTIVE)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
materials Material[]
@@index([email])
}
model Material {
id String @id @default(cuid())
code String @unique
name String
description String?
unitPrice Float
unit String
supplierId String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
currentStock Int @default(0)
minStock Int @default(0)
category String?
customerMarkupPercent Float?
inventoryPurpose String? @default("INTERNAL")
isOrderable Boolean? @default(true)
isQuotable Boolean? @default(false)
leadTimeInDays Int? @default(0)
reorderPoint Int? @default(0)
visibleToCustomers Boolean? @default(false)
customerId String?
manufacturer String?
productSpecifications String?
jobMaterials JobMaterial[]
customer Customer? @relation(fields: [customerId], references: [id])
supplier Supplier? @relation(fields: [supplierId], references: [id])
quoteLineItems QuoteLineItem[]
@@index([supplierId])
@@index([customerId])
@@index([code])
}
model CompanySettings {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lastQuoteReferenceSeq Int @default(0)
quoteReferencePrefix String @default("QR")
quoteNumberPadding Int @default(4) // ADD THIS LINE
companyAddress String?
companyEmail String?
companyLogo String?
companyName String?
companyPhone String?
companyVatNumber String?
companyWebsite String?
defaultVatRate Float? @default(20.0)
lastQuoteNumber Int @default(0)
quoteNumberFormat String @default("{PREFIX}-{NUMBER}")
quoteNumberPrefix String @default("Q")
// ADD THESE NEW FIELDS:
standardWarranty String?
standardDeliveryTerms String?
defaultLeadTimeWeeks Int?
standardExclusions String?
useCompanyDetailsOnQuotes Boolean? @default(false)
}
model Quote {
id String @id @default(cuid())
customerId String
title String
description String?
notes String? // Add this line
termsAndConditions String?
paymentTerms String? // ADD THIS
deliveryTerms String? // ADD THIS
warranty String? // ADD THIS
exclusions String? // ADD THIS
status QuoteStatus @default(DRAFT)
totalAmount Float @default(0)
validUntil DateTime?
createdById String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
customerReference String?
quoteNumber String @unique
contactEmail String?
contactPerson String?
contactPhone String?
changeReason String?
isLatestVersion Boolean @default(true)
parentQuoteId String?
quoteReference String
versionNumber Int @default(1)
currentVersion Int @default(1)
documents Document[]
orders Order[]
createdBy User @relation(fields: [createdById], references: [id])
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
parentQuote Quote? @relation("QuoteRevisions", fields: [parentQuoteId], references: [id], onDelete: NoAction, onUpdate: NoAction)
childQuotes Quote[] @relation("QuoteRevisions")
history QuoteHistory[]
lineItems QuoteLineItem[]
@@index([quoteReference])
@@index([customerId])
@@index([createdById])
@@index([status])
@@index([parentQuoteId])
}
model QuoteLineItem {
id String @id @default(cuid())
quoteId String
description String
quantity Float @default(1)
unitPrice Float @default(0)
materialId String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
material Material? @relation(fields: [materialId], references: [id])
quote Quote @relation(fields: [quoteId], references: [id], onDelete: Cascade)
@@index([quoteId])
@@index([materialId])
}
model QuoteHistory {
id String @id @default(cuid())
quoteId String
version Int
status String
data Json
changeType String
changedBy String
changeReason String?
customerApproved Boolean?
customerSignature String?
approvalTimestamp DateTime?
createdAt DateTime @default(now())
ipAddress String?
userAgent String?
changedByUser User @relation("QuoteHistoryUser", fields: [changedBy], references: [id])
quote Quote @relation(fields: [quoteId], references: [id], onDelete: Cascade)
@@index([quoteId])
@@index([changedBy])
@@index([createdAt])
}
model OrderHistory {
id String @id @default(cuid())
orderId String
version Int
status String
data Json
changeType String
changedBy String
changeReason String?
customerApproved Boolean?
customerSignature String?
approvalTimestamp DateTime?
createdAt DateTime @default(now())
ipAddress String?
userAgent String?
changedByUser User @relation("OrderHistoryUser", fields: [changedBy], references: [id])
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
@@index([orderId])
@@index([changedBy])
@@index([createdAt])
}
model JobHistory {
id String @id @default(cuid())
jobId String
version Int
status String
data Json
changeType String
changedBy String
changeReason String?
materialChanges Json?
progressNotes String?
attachments Json?
createdAt DateTime @default(now())
ipAddress String?
userAgent String?
changedByUser User @relation("JobHistoryUser", fields: [changedBy], references: [id])
job Job @relation(fields: [jobId], references: [id], onDelete: Cascade)
@@index([jobId])
@@index([changedBy])
@@index([createdAt])
}
model Document {
id String @id @default(cuid())
name String
originalName String
mimeType String
fileSize Int
fileHash String
storagePath String
storageType String @default("LOCAL")
category String
quoteId String?
orderId String?
jobId String?
isLegalDocument Boolean @default(false)
retentionPeriod Int?
uploadedBy String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
job Job? @relation(fields: [jobId], references: [id])
order Order? @relation(fields: [orderId], references: [id])
quote Quote? @relation(fields: [quoteId], references: [id])
uploadedByUser User @relation("DocumentUser", fields: [uploadedBy], references: [id])
@@index([quoteId])
@@index([orderId])
@@index([jobId])
@@index([uploadedBy])
@@index([category])
}
model TimeEntry {
id String @id @default(cuid())
employeeId String @map("employee_id")
jobId String @map("job_id")
date DateTime @db.Date
hours Float
isRdActivity Boolean @default(false) @map("is_rd_activity")
rdDescription String? @map("rd_description")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
employee User @relation(fields: [employeeId], references: [id])
job Job @relation(fields: [jobId], references: [id])
@@index([employeeId])
@@index([jobId])
@@index([date])
@@index([isRdActivity])
}
enum Role {
USER
ADMIN
}
enum OrderType {
JOB_LINKED
CUSTOMER_LINKED
INTERNAL
}
enum OrderStatus {
IN_PRODUCTION
ON_HOLD
READY_FOR_DELIVERY
DELIVERED
COMPLETED
}
enum JobStatus {
ACTIVE
DRAFT
PENDING
IN_PROGRESS
CANCELED
}
enum ImportStatus {
SUCCESS
PARTIAL
FAILED
}
enum PaymentTerms {
WITH_ORDER
PRIOR_TO_DISPATCH
THIRTY_DAYS
SIXTY_DAYS
NINETY_DAYS
CUSTOM
SPLIT_50_50
SPLIT_50_40_10
}
enum SupplierStatus {
ACTIVE
INACTIVE
SUSPENDED
UNDER_REVIEW
BLACKLISTED
}
enum MilestoneStatus {
PENDING
OVERDUE
PAID
CANCELLED
}
enum QuoteStatus {
DRAFT
SENT
APPROVED
DECLINED
EXPIRED
CONVERTED
PENDING
}
enum ContactRole {
ACCOUNTS
DELIVERIES
PRIMARY_BUYER
TECHNICAL_CONTACT
SITE_CONTACT
PROJECT_MANAGER
GENERAL_INQUIRY
OTHER
PRIMARY
BILLING
TECHNICAL
SALES
SUPPORT
}