In LPH 2/3 there are no circuits yet, but the office needs a power balance by DIN 276 cost group, as in the Excel template "Leistungsbilanz - KG400": each trade enters consumers by piece or by area (W/m²) with GLF and cos phi; the balance sums AV and SV per cost group, adds a reserve and derives the maximum current. The module is separate from the circuit structure: own tables (migration 0010), own folder src/estimate-balance and an import-free shared calculation used by API and UI. Writes go through project commands (estimate-balance.insert/.update/.delete with complete documents), so revision checks and Undo/Redo apply; it is not part of project snapshots or transfer yet. A comparison endpoint sums the device rows of all circuit lists by cost group (longest prefix) against the estimate. The UI adds a sidebar entry, a list of Stände (template KG 400, empty or copy) and a spreadsheet-like editor with autosave, AMEV picker for the GLF, totals, reserve, maximum current, comparison table and print layout. The tests reproduce the template example exactly (143.24 kW, 148.48 kVA, 157.56 kW / 163.33 kVA with 10 % reserve, 235.75 A). See docs/estimate-balance.md and issue #7. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
2.4 KiB
TypeScript
60 lines
2.4 KiB
TypeScript
import { asc, eq } from "drizzle-orm";
|
|
import type {
|
|
DetailedDeviceRowPower,
|
|
EstimateBalanceSnapshot,
|
|
} from "../../shared/estimate-balance/estimate-balance.js";
|
|
import { calculateRowTotalPower } from "../../domain/calculations/circuit-power-calculation.js";
|
|
import type { AppDatabase } from "../database-context.js";
|
|
import { circuitDeviceRows } from "../schema/circuit-device-rows.js";
|
|
import { circuitLists } from "../schema/circuit-lists.js";
|
|
import { circuits } from "../schema/circuits.js";
|
|
import { estimateBalances } from "../schema/estimate-balances.js";
|
|
import { readEstimateBalances } from "./estimate-balance.persistence.js";
|
|
|
|
/** Read side of the estimate-balance module; writes go through commands. */
|
|
export class EstimateBalanceRepository {
|
|
constructor(private readonly database: AppDatabase) {}
|
|
|
|
listByProject(projectId: string): EstimateBalanceSnapshot[] {
|
|
return readEstimateBalances(this.database, projectId);
|
|
}
|
|
|
|
findById(projectId: string, balanceId: string): EstimateBalanceSnapshot | null {
|
|
return readEstimateBalances(this.database, projectId, balanceId)[0] ?? null;
|
|
}
|
|
|
|
nextSortOrder(projectId: string): number {
|
|
const rows = this.database
|
|
.select({ sortOrder: estimateBalances.sortOrder })
|
|
.from(estimateBalances)
|
|
.where(eq(estimateBalances.projectId, projectId))
|
|
.orderBy(asc(estimateBalances.sortOrder))
|
|
.all();
|
|
return rows.length ? Math.max(...rows.map((row) => row.sortOrder)) + 1 : 0;
|
|
}
|
|
|
|
/**
|
|
* Device rows of all circuit lists of the project with their total power
|
|
* (quantity · power per unit · device GZF, like the circuit tree).
|
|
*/
|
|
listDetailedDeviceRowPower(projectId: string): DetailedDeviceRowPower[] {
|
|
return this.database
|
|
.select({
|
|
costGroup: circuitDeviceRows.costGroup,
|
|
quantity: circuitDeviceRows.quantity,
|
|
powerPerUnit: circuitDeviceRows.powerPerUnit,
|
|
simultaneityFactor: circuitDeviceRows.simultaneityFactor,
|
|
cosPhi: circuitDeviceRows.cosPhi,
|
|
})
|
|
.from(circuitDeviceRows)
|
|
.innerJoin(circuits, eq(circuits.id, circuitDeviceRows.circuitId))
|
|
.innerJoin(circuitLists, eq(circuitLists.id, circuits.circuitListId))
|
|
.where(eq(circuitLists.projectId, projectId))
|
|
.all()
|
|
.map((row) => ({
|
|
costGroup: row.costGroup,
|
|
kw: calculateRowTotalPower(row.quantity, row.powerPerUnit, row.simultaneityFactor),
|
|
cosPhi: row.cosPhi,
|
|
}));
|
|
}
|
|
}
|