mirror of
https://origin.cursor.com/mrdevmx/panels.git
synced 2026-10-09 17:33:16 +00:00
<!-- CURSOR_AGENT_PR_BODY_BEGIN --> ## Summary Implementa el módulo **Programa de obra** por proyecto, vinculado al presupuesto ya existente. ### Flujo 1. Cargar presupuesto (requisito) 2. Definir fechas de inicio/término en el proyecto 3. Crear programa: **importar Excel Neodata/Opus**, **generar automático** o **crear vacío** 4. Editar en cuadrícula (conceptos con `Barra=` y partidas con erogaciones semanales) 5. Visualizar Gantt resumido y comparativo **programado vs ejecutado** ### Backend - Tablas: `work_programs`, `work_program_periods`, `work_program_concept_slots`, `work_program_partida_amounts`, `work_program_progress` - RPC: list/get, replace (import), generate (wizard), update celdas, recalculate partidas, vs-cost - Parser Excel para formatos **por concepto** (`Barra=`) y **por partida** (montos semanales) - Tests con fixtures reales del usuario ### Frontend - Nueva página `/programa-obra` con pestañas: Conceptos, Partidas, Gantt, Programado vs costos - Diálogos de importación y generación automática - Menú principal, enlace en ficha de proyecto, banner en presupuesto ### Permisos Reutiliza `manage_budget` en MVP. ## Test plan - [x] `deno test api/work_program_excel_test.ts` (5 tests) - [x] `deno check` API modules - [x] `npm run build` web-panel - [ ] Migrar DB (`029`, `030`) en entorno con Postgres - [ ] Importar Excel por concepto y por partida en obra con presupuesto - [ ] Generar automático con estrategia keyword - [ ] Recalcular partidas desde conceptos - [ ] Verificar tab Programado vs costos con gastos registrados <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-3985fe74-d26b-4506-bd4d-f9ad57374141?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-3985fe74-d26b-4506-bd4d-f9ad57374141&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div>
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());
|
|
});
|
|
}
|