inventory.prisma 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. model LaborPreset {
  2. id String @id @default(cuid())
  3. name String
  4. description String?
  5. isArchived Boolean @default(false)
  6. createdAt DateTime @default(now())
  7. updatedAt DateTime @updatedAt
  8. userId String
  9. organizationId String
  10. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  11. items LaborPresetItem[]
  12. parts LaborPresetPart[]
  13. @@index([organizationId])
  14. @@map("labor_presets")
  15. }
  16. model LaborPresetItem {
  17. id String @id @default(cuid())
  18. description String
  19. hours Float @default(0)
  20. rate Float @default(0)
  21. pricingType String @default("hourly")
  22. sortOrder Int @default(0)
  23. presetId String
  24. preset LaborPreset @relation(fields: [presetId], references: [id], onDelete: Cascade)
  25. @@index([presetId])
  26. @@map("labor_preset_items")
  27. }
  28. model LaborPresetPart {
  29. id String @id @default(cuid())
  30. name String
  31. partNumber String?
  32. quantity Float @default(1)
  33. /// Unit of measure snapshotted from InventoryPart at pick time.
  34. unit String?
  35. /// Category snapshotted from InventoryPart at pick time; copied onto the
  36. /// part line when the preset is applied.
  37. category String?
  38. unitPrice Float @default(0)
  39. inventoryPartId String?
  40. sortOrder Int @default(0)
  41. presetId String
  42. preset LaborPreset @relation(fields: [presetId], references: [id], onDelete: Cascade)
  43. @@index([presetId])
  44. @@map("labor_preset_parts")
  45. }
  46. model InventoryPart {
  47. id String @id @default(cuid())
  48. partNumber String?
  49. barcode String?
  50. name String
  51. description String?
  52. category String?
  53. quantity Float @default(0)
  54. minQuantity Float @default(0)
  55. /// False for a charge kept as a part but never counted, such as a
  56. /// consumables or environmental fee. Using one on a job moves no stock and
  57. /// writes no ledger row, its `quantity` stays 0, and it is left out of
  58. /// low-stock alerts and stock value.
  59. trackStock Boolean @default(true)
  60. /// Unit of measure for `quantity` (e.g. "pcs", "l", "kg"). Free text,
  61. /// snapshotted onto part lines at pick time so invoices keep the unit the
  62. /// part had when it was billed.
  63. unit String?
  64. unitCost Float @default(0)
  65. sellPrice Float @default(0)
  66. supplier String?
  67. supplierPhone String?
  68. supplierEmail String?
  69. supplierUrl String?
  70. imageUrl String?
  71. location String?
  72. isArchived Boolean @default(false)
  73. /// Free-text labels the workshop gives a part, such as the makes and models
  74. /// it fits ("Toyota", "Hilux 2015-2020"). Searched wherever parts are, and
  75. /// matched against the job's vehicle to list fitting parts first.
  76. tags String[] @default([])
  77. /// Set when a low-stock alert has been sent for the current dip below
  78. /// `minQuantity`, and cleared once stock climbs back above it. This
  79. /// hysteresis is what stops a part that sits at zero from re-alerting on
  80. /// every check — it alerts once per descent, not once per run.
  81. lowStockAlertedAt DateTime?
  82. createdAt DateTime @default(now())
  83. updatedAt DateTime @updatedAt
  84. userId String
  85. user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  86. organizationId String?
  87. organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  88. gallery StoredImage[] @relation("InventoryPartImages")
  89. movements StockMovement[]
  90. @@unique([organizationId, barcode])
  91. @@index([organizationId])
  92. @@map("inventory_parts")
  93. }
  94. /// Append-only audit trail of every change to InventoryPart.quantity.
  95. ///
  96. /// Written inside the same transaction as the stock mutation itself, so the
  97. /// ledger can never drift from the balance it describes. `quantityAfter` is
  98. /// captured from the atomic `UPDATE ... RETURNING`, giving an exact running
  99. /// balance even under concurrent writes.
  100. model StockMovement {
  101. id String @id @default(cuid())
  102. /// Signed change in stock units (fractional allowed, e.g. 0.5 l).
  103. /// Negative = consumed, positive = restocked.
  104. delta Float
  105. /// Part balance immediately after this movement was applied.
  106. quantityAfter Float
  107. /// Why the stock moved — see StockMovementReason in src/features/inventory/Lib/stockMovement.ts
  108. reason String
  109. note String?
  110. inventoryPartId String
  111. inventoryPart InventoryPart @relation(fields: [inventoryPartId], references: [id], onDelete: Cascade)
  112. /// The job the part was used on. Kept on SetNull so history survives
  113. /// deletion of the service record — "where was this used" outlives the job.
  114. serviceRecordId String?
  115. serviceRecord ServiceRecord? @relation(fields: [serviceRecordId], references: [id], onDelete: SetNull)
  116. /// Denormalised so history still reads correctly after a service record is
  117. /// deleted and serviceRecordId goes null.
  118. serviceRecordLabel String?
  119. userId String?
  120. user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
  121. organizationId String
  122. organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  123. createdAt DateTime @default(now())
  124. @@index([inventoryPartId, createdAt(sort: Desc)])
  125. @@index([organizationId, createdAt(sort: Desc)])
  126. @@index([serviceRecordId])
  127. @@map("stock_movements")
  128. }
  129. model StoredImage {
  130. id String @id @default(cuid())
  131. url String
  132. fileName String?
  133. description String?
  134. sortOrder Int @default(0)
  135. createdAt DateTime @default(now())
  136. inventoryPartId String?
  137. inventoryPart InventoryPart? @relation("InventoryPartImages", fields: [inventoryPartId], references: [id], onDelete: Cascade)
  138. /// Condition photos taken when a tire is measured. Second parent on this
  139. /// model — an image belongs to exactly one of them.
  140. tireMeasurementId String?
  141. tireMeasurement TireMeasurement? @relation("TireMeasurementImages", fields: [tireMeasurementId], references: [id], onDelete: Cascade)
  142. @@index([inventoryPartId])
  143. @@index([tireMeasurementId])
  144. @@map("stored_images")
  145. }