mirror of
https://origin.cursor.com/mrdevmx/panels.git
synced 2026-10-09 14:03:16 +00:00
Implementa el módulo completo de programa de obra vinculado al presupuesto: - Schema PostgreSQL: work_programs, periods, concept_slots, partida_amounts, progress - RPC: CRUD, import replace, generate wizard, recalculate partidas, vs-cost - API: parser Excel Neodata (concepto Barra= + partida erogaciones), endpoints REST - UI: página /programa-obra con cuadrículas, Gantt, comparativo programado/ejecutado - Integración: menú, ficha proyecto, banner en presupuesto, permiso manage_budget Co-authored-by: alberto.martinez <alberto.martinez@mrdev.mx>
145 lines
5.4 KiB
TypeScript
145 lines
5.4 KiB
TypeScript
import type { Hono } from "hono";
|
|
import type { AuthUser } from "./auth.ts";
|
|
import { requireCoreAuth } from "./scope.ts";
|
|
import type { Db } from "./db.ts";
|
|
import { projectById } from "./db.ts";
|
|
import { requirePermission } from "./permissions.ts";
|
|
import {
|
|
createEmptyWorkProgram,
|
|
exportWorkProgram,
|
|
generateWorkProgram,
|
|
getVsCost,
|
|
getWorkProgram,
|
|
importWorkProgramExcel,
|
|
listConcepts,
|
|
listPartidas,
|
|
previewWorkProgramExcel,
|
|
recalculatePartidas,
|
|
updateConceptSlot,
|
|
updatePartidaAmount,
|
|
updateProgress,
|
|
} from "./work_program.ts";
|
|
|
|
type App = Hono<{ Variables: { user: AuthUser; db: Db } }>;
|
|
|
|
const auth = [...requireCoreAuth, requirePermission("manage_budget")] as const;
|
|
|
|
async function readUpload(c: { req: { formData: () => Promise<FormData> } }): Promise<Uint8Array> {
|
|
const form = await c.req.formData();
|
|
const file = form.get("file");
|
|
if (!(file instanceof File)) throw new Error("Archivo Excel requerido (campo file)");
|
|
return new Uint8Array(await file.arrayBuffer());
|
|
}
|
|
|
|
export function registerWorkProgramRoutes(app: App) {
|
|
app.get("/v1/projects/:id/work-program", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const data = await getWorkProgram(db, Number(c.req.param("id")));
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.get("/v1/projects/:id/work-program/concepts", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const data = await listConcepts(db, Number(c.req.param("id")));
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.get("/v1/projects/:id/work-program/partidas", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const data = await listPartidas(db, Number(c.req.param("id")));
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.get("/v1/projects/:id/work-program/vs-cost", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const data = await getVsCost(db, Number(c.req.param("id")));
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.post("/v1/projects/:id/work-program/preview", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const bytes = await readUpload(c);
|
|
const preview = await previewWorkProgramExcel(bytes, Number(c.req.param("id")), db);
|
|
return c.json({ ok: true, data: preview });
|
|
});
|
|
|
|
app.post("/v1/projects/:id/work-program/import", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const bytes = await readUpload(c);
|
|
const createChapters = c.req.query("create_chapters") !== "false";
|
|
const data = await importWorkProgramExcel(db, Number(c.req.param("id")), bytes, { createChapters });
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.post("/v1/projects/:id/work-program", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const data = await createEmptyWorkProgram(db, Number(c.req.param("id")));
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.post("/v1/projects/:id/work-program/generate", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const body = await c.req.json().catch(() => ({})) as { strategy?: string };
|
|
const data = await generateWorkProgram(db, Number(c.req.param("id")), body.strategy || "keyword");
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.post("/v1/projects/:id/work-program/recalculate-partidas", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const env = await recalculatePartidas(db, Number(c.req.param("id")));
|
|
return c.json({ ok: true, data: env });
|
|
});
|
|
|
|
app.patch("/v1/projects/:id/work-program/concepts/:itemId/periods/:periodId", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const body = await c.req.json() as { start_pct?: number; end_pct?: number };
|
|
const data = await updateConceptSlot(
|
|
db,
|
|
Number(c.req.param("id")),
|
|
Number(c.req.param("itemId")),
|
|
Number(c.req.param("periodId")),
|
|
Number(body.start_pct ?? 0),
|
|
Number(body.end_pct ?? 0),
|
|
);
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.patch("/v1/projects/:id/work-program/partidas/:chapterId/periods/:periodId", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const body = await c.req.json() as { amount?: number };
|
|
const data = await updatePartidaAmount(
|
|
db,
|
|
Number(c.req.param("id")),
|
|
Number(c.req.param("chapterId")),
|
|
Number(c.req.param("periodId")),
|
|
Number(body.amount ?? 0),
|
|
);
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.patch("/v1/projects/:id/work-program/progress/:itemId/periods/:periodId", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const body = await c.req.json() as { pct_complete?: number };
|
|
const data = await updateProgress(
|
|
db,
|
|
Number(c.req.param("id")),
|
|
Number(c.req.param("itemId")),
|
|
Number(c.req.param("periodId")),
|
|
Number(body.pct_complete ?? 0),
|
|
);
|
|
return c.json({ ok: true, data });
|
|
});
|
|
|
|
app.get("/v1/projects/:id/work-program/export", ...auth, async (c) => {
|
|
const db = c.get("db");
|
|
const id = Number(c.req.param("id"));
|
|
const kind = (c.req.query("kind") === "partida" ? "partida" : "concepto") as "concepto" | "partida";
|
|
const project = await projectById(db, id);
|
|
if (!project) return c.json({ error: "Proyecto no encontrado" }, 404);
|
|
const bytes = await exportWorkProgram(db, id, kind, String(project.name || "Obra"));
|
|
const filename = kind === "partida" ? "programa-partidas.xlsx" : "programa-conceptos.xlsx";
|
|
c.header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
c.header("Content-Disposition", `attachment; filename="${filename}"`);
|
|
return c.body(bytes.slice());
|
|
});
|
|
}
|