enum BatchCostLineKind { PRODUCTION PACKAGING } model BatchCostSnapshot { id String @id @default(uuid()) @db.Uuid organizationId String @map("organization_id") @db.Uuid batchId String @unique @map("batch_id") @db.Uuid createdById String @map("created_by_id") @db.Uuid idempotencyKey String @map("idempotency_key") @db.Uuid requestHash String @map("request_hash") @db.Char(64) currency String @db.Char(3) goodQuantity Int @map("good_quantity") productionTotal Decimal @map("production_total") @db.Decimal(12,2) packagingTotal Decimal @map("packaging_total") @db.Decimal(12,2) total Decimal @db.Decimal(12,2) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) sealedAt DateTime? @map("sealed_at") @db.Timestamptz(3) organization Organization @relation(fields: [organizationId], references: [id], onDelete: Restrict) batch ProductionBatch @relation(fields: [batchId, organizationId], references: [id, organizationId], onDelete: Restrict) createdBy User @relation(fields: [createdById, organizationId], references: [id, organizationId], onDelete: Restrict) lines BatchCostLine[] @@unique([organizationId, idempotencyKey]) @@unique([batchId, organizationId]) @@unique([id, organizationId]) @@map("batch_cost_snapshots") } model BatchCostLine { id String @id @default(uuid()) @db.Uuid organizationId String @map("organization_id") @db.Uuid snapshotId String @map("snapshot_id") @db.Uuid kind BatchCostLineKind bomId String @map("bom_id") @db.Uuid bomLineId String @map("bom_line_id") @db.Uuid materialId String @map("material_id") @db.Uuid rateId String @map("rate_id") @db.Uuid usedQuantity Decimal @map("used_quantity") @db.Decimal(12,3) wastedQuantity Decimal @map("wasted_quantity") @db.Decimal(12,3) unitCost Decimal @map("unit_cost") @db.Decimal(12,4) lineCost Decimal @map("line_cost") @db.Decimal(12,2) snapshot BatchCostSnapshot @relation(fields: [snapshotId, organizationId], references: [id, organizationId], onDelete: Restrict) bomLine ProductBomLine @relation(fields: [bomLineId, bomId, materialId, organizationId], references: [id, bomId, materialId, organizationId], onDelete: Restrict, map: "batch_cost_lines_bom_scope_fkey") rate MaterialCostRate @relation(fields: [rateId, materialId, organizationId], references: [id, materialId, organizationId], onDelete: Restrict, map: "batch_cost_lines_rate_scope_fkey") @@unique([snapshotId, bomLineId]) @@map("batch_cost_lines") }