// R-P8a (P8 Projekt & Pipelines core) — typed project/pipeline entities. // // AD-20: there is ONE `Projekt` core. "Deal" and "Auftrag" are pipeline // views (`Pipeline.typHint`), not separate entities and not a won-handoff copy. // // AD-21: this module owns its schema file. Cross-module references to Tenant, // User, Kontakt, InboxLead and Adresse are intentionally bare String scalars so // this slice does not modify core/accounts/crm/leads schemas. enum PipelineTypHint { sales auftrag custom } enum PipelineStageKind { offen gewonnen verloren } enum PipelinePrincipalType { role group user } enum ProjektPrioritaet { niedrig normal hoch dringend } enum ProjektKontaktRolle { auftraggeber bauherr rechnungsempfaenger ansprechpartner } model HiveNumberSeq { id String @id @default(cuid()) tenantId String entityType String prefix String nextNumber Int @default(1) padding Int @default(5) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@unique([tenantId, entityType]) @@index([tenantId]) } model Pipeline { id String @id @default(cuid()) tenantId String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt createdByUserId String? name String typHint PipelineTypHint position Int @default(0) isDefault Boolean @default(false) isDemoData Boolean @default(false) archivedAt DateTime? /// WF-P8 M04: pipeline a project hands over to once it reaches the final /// stage here. Null = this pipeline is terminal and M04 never opens. nachfolgePipelineId String? stages PipelineStage[] accessRules PipelineAccess[] fieldConfigs PipelineFieldConfig[] fieldGroups PipelineFieldGroup[] projekte Projekt[] nachfolgePipeline Pipeline? @relation("PipelineNachfolge", fields: [nachfolgePipelineId], references: [id], onDelete: SetNull) vorgaengerPipelines Pipeline[] @relation("PipelineNachfolge") @@unique([tenantId, name]) @@index([tenantId, typHint, archivedAt]) @@index([tenantId, position]) @@index([tenantId, nachfolgePipelineId]) } model PipelineStage { id String @id @default(cuid()) tenantId String pipelineId String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt name String stageKind PipelineStageKind winProbability Int @default(0) color String @default("#6b7280") position Int @default(0) isDemoData Boolean @default(false) archivedAt DateTime? pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade) fieldConfigs PipelineFieldConfig[] projekte Projekt[] portalVisibilities PortalStageVisibility[] portalEmailTemplates PortalEmailTemplate[] @@unique([pipelineId, name]) @@unique([id, tenantId]) @@index([tenantId, pipelineId, archivedAt]) @@index([tenantId, stageKind]) } model PipelineAccess { id String @id @default(cuid()) tenantId String pipelineId String principalType PipelinePrincipalType principalId String kannSehen Boolean @default(true) kannBearbeiten Boolean @default(false) createdAt DateTime @default(now()) pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade) @@unique([pipelineId, principalType, principalId]) @@index([tenantId, principalType, principalId]) } /// WF-P8 S05: a named section of the project detail template ("Grunddaten", /// "Planung & Verantwortliche", "KWP-Integration", …). System groups ship with /// the pipeline and may be reordered but not deleted (`istSystem`). model PipelineFieldGroup { id String @id @default(cuid()) tenantId String pipelineId String name String reihenfolge Int @default(0) istSystem Boolean @default(false) isDemoData Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade) fieldConfigs PipelineFieldConfig[] @@unique([pipelineId, name]) @@index([tenantId, pipelineId, reihenfolge]) } model PipelineFieldConfig { id String @id @default(cuid()) tenantId String pipelineId String stageId String? fieldKey String sichtbar Boolean @default(true) pflicht Boolean @default(false) reihenfolge Int @default(0) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt /// WF-P8 S05 template layout: owning section. Null = field sits in the /// "Bestandsfelder" pool outside the template. groupId String? /// WF-P8 S05 "Pflicht ab Board-Spalte": the field only becomes mandatory once /// the project reaches this stage. Null = `pflicht` applies unconditionally. pflichtAbStageId String? /// WF-P8 S05 optional helper text rendered under the field. hinweis String? /// WF-P8 S05: system fields may be reordered/locked but never deleted. istSystem Boolean @default(false) /// WF-P8 S05 legend "gesperrt / nur lesbar". gesperrt Boolean @default(false) pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade) stage PipelineStage? @relation(fields: [stageId], references: [id], onDelete: Cascade) group PipelineFieldGroup? @relation(fields: [groupId], references: [id], onDelete: SetNull) visibilities PipelineFieldVisibility[] @@unique([pipelineId, stageId, fieldKey]) @@index([tenantId, pipelineId]) @@index([tenantId, fieldKey]) @@index([tenantId, groupId, reihenfolge]) } model PipelineFieldVisibility { id String @id @default(cuid()) tenantId String fieldConfigId String principalType PipelinePrincipalType principalId String kannSehen Boolean @default(true) kannBearbeiten Boolean @default(false) createdAt DateTime @default(now()) fieldConfig PipelineFieldConfig @relation(fields: [fieldConfigId], references: [id], onDelete: Cascade) @@unique([fieldConfigId, principalType, principalId]) @@index([tenantId, principalType, principalId]) } model LostReason { id String @id @default(cuid()) tenantId String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt name String /// WF-P8 S06: one-line explanation shown next to the reason in the admin table. beschreibung String? /// WF-P8 S06: free-text grouping ("Preis", "Budget", "Wettbewerb", ...). kategorie String? /// WF-P8 S06: true for the SHK reasons the tenant seed ships, false for /// reasons the tenant created itself. Distinct from isDemoData, which marks /// throwaway demo rows. istVorlage Boolean @default(false) position Int @default(0) isDemoData Boolean @default(false) archivedAt DateTime? projekte Projekt[] @@unique([tenantId, name]) @@index([tenantId, archivedAt]) } model Projekt { id String @id @default(cuid()) tenantId String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt createdByUserId String? projektNummer String projektname String beschreibung String? projekttyp String? quelle String? /// WF-P8 S02 "Grunddaten": campaign/promotion the project came in through. promotion String? /// WF-P8 S02 "Projektkontaktdaten": contact channels of the project itself, /// distinct from the linked Kontakt records (site office, site phone, ...). email String? telefon String? fax String? prioritaet ProjektPrioritaet @default(normal) pipelineId String stageId String potenziellerAuftragswert Decimal? auftragsvolumen Decimal? startDatum DateTime? deadline DateTime? zustaendigePersonId String? projektleiterId String? projektAdresseId String? rechnungsAdresseId String? bauherrAdresseId String? konvertiertAusLeadId String? lostReasonId String? wonAt DateTime? lostAt DateTime? undoStageChangeUntil DateTime? // WF-P8 M03 "Wiederholen": the stage an undo moved the project away from, so // the board can offer redo without querying the stage history per card. Set // when an undo runs, cleared by the next forward move. redoStageId String? // The reason that came with the undone move. A lost stage cannot be entered // without one, so a redo would otherwise be rejected for exactly the moves // the undo window exists for. redoLostReasonId String? isDemoData Boolean @default(false) archivedAt DateTime? pipeline Pipeline @relation(fields: [pipelineId], references: [id]) stage PipelineStage @relation(fields: [stageId], references: [id]) lostReason LostReason? @relation(fields: [lostReasonId], references: [id], onDelete: SetNull) kontakte ProjektKontakt[] stageChanges ProjektStageChange[] mitglieder ProjektMitglied[] adressDetails ProjektAdressDetail[] checklists ProjektChecklist[] portalAccesses PortalProjectAccess[] portalDocuments PortalProjectDocument[] @@unique([tenantId, projektNummer]) @@unique([id, tenantId]) @@index([tenantId, pipelineId, stageId, archivedAt]) @@index([tenantId, zustaendigePersonId]) @@index([tenantId, projektleiterId]) @@index([tenantId, konvertiertAusLeadId]) @@index([tenantId, lostReasonId]) } model ProjektStageChange { id String @id @default(cuid()) tenantId String projektId String fromStageId String toStageId String lostReasonId String? /// WF-P8 M01: free-text note the user leaves when marking a project lost. notiz String? changedByUserId String? undoUntil DateTime? undoneAt DateTime? createdAt DateTime @default(now()) projekt Projekt @relation(fields: [projektId], references: [id], onDelete: Cascade) @@index([tenantId, projektId, createdAt]) @@index([tenantId, toStageId, createdAt]) @@index([tenantId, undoUntil]) } model PipelineAggregateSnapshot { id String @id @default(cuid()) tenantId String pipelineId String stageId String? scopeKey String projectCount Int @default(0) potentialValue Decimal @default(0) orderVolume Decimal @default(0) weightedForecast Decimal @default(0) realizedRevenue Decimal @default(0) lostCount Int @default(0) stageConversionRate Decimal? lostReasonDistribution Json @default("{}") isDemoDataIncluded Boolean @default(false) calculatedAt DateTime @default(now()) @@unique([tenantId, pipelineId, scopeKey]) @@index([tenantId, pipelineId]) @@index([tenantId, calculatedAt]) } model ProjektKontakt { id String @id @default(cuid()) tenantId String createdAt DateTime @default(now()) projektId String kontaktId String rolle ProjektKontaktRolle primaer Boolean @default(false) projekt Projekt @relation(fields: [projektId], references: [id], onDelete: Cascade) @@unique([projektId, kontaktId, rolle]) @@index([tenantId, projektId]) @@index([tenantId, kontaktId]) } /// WF-P8 S02 "Adressdaten": which of the three project address slots a detail row /// belongs to. Mirrors the three address FKs on Projekt. enum ProjektAdressSlot { projekt rechnung bauherr } /// The per-slot extras the S02 address group asks for on top of the shared /// `Adresse`. Deliberately NOT columns on `Adresse`: that is a shared CRM entity /// which contacts reuse, so a project's "Zahlungsziel" or "Zufahrt" would leak /// into every contact address. One row per project and slot. model ProjektAdressDetail { id String @id @default(cuid()) tenantId String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt projektId String slot ProjektAdressSlot /// Slot "projekt": where on the site the work happens and how to get there. bauabschnitt String? zufahrt String? koordinaten String? /// Slot "rechnung": who receives the invoice, on what terms, and where the /// invoice itself is sent. Separate from `Projekt.email`, which is the /// operational contact address of the project. empfaenger String? zahlungsziel String? rechnungsEmail String? /// Slot "bauherr": whether the building owner wants postal mail, and who to /// reach on site. The owner is often not the CRM contact carrying the project. postErwuenscht Boolean @default(false) telefon String? ansprechpartnerVorOrt String? /// Free text, one per slot. The content is slot-specific — delivery /// constraints at the site, payment remarks on the invoice address — so it /// cannot live on the shared `Adresse` any more than the fields above. notizen String? projekt Projekt @relation(fields: [projektId], references: [id], onDelete: Cascade) @@unique([projektId, slot]) @@index([tenantId, projektId]) }