panels-origin/db/core/changesets/019-schema-warehouse.sql
Alberto Martinez 41273dfb04 Gastos, almacén y control presupuestal
<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
## Validación de endpoints y SPs

Se probaron todos los endpoints nuevos (gastos, almacén, control presupuestal, IAM) y sus RPCs asociados.

### Bugs corregidos en migraciones/SQL

1. **IAM grants en schema core** — `iam-004e` intentaba `GRANT` sobre `core` con rol `iam_owner` (sin permiso). Los grants cruzados se movieron a `core-027-iam-rpc-cross-grants.sql`.
2. **`_cost_settings` ambiguo** — columnas `budget_warn_pct` etc. colisionaban con `RETURNS TABLE` en PL/pgSQL, rompiendo `fn_expense_create` y control presupuestal. Corregido en `core-028-fix-cost-settings-ambiguous.sql`.

### Tests añadidos

- `api/cost_modules_test.ts` — CRUD RPC gastos, flujo almacén completo, control presupuestal, IAM permisos
- `scripts/crud-smoke-test.sh` — smoke HTTP de todos los endpoints nuevos

### Resultados

- `deno test cost_modules_test.ts` — 5/5 OK
- `./scripts/crud-smoke-test.sh` — todos los checks OK (gastos CRUD, IVA, cost-settings, almacén, transferencias, cost-control, IAM)
<!-- CURSOR_AGENT_PR_BODY_END -->

<div><a href="https://cursor.com/agents/bc-eeec3b5c-f789-43e8-a353-0755e4706377?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>&nbsp;<a href="https://cursor.com/background-agent?bcId=bc-eeec3b5c-f789-43e8-a353-0755e4706377&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>&nbsp;</div>
2026-09-04 04:29:02 +00:00

94 lines
5.1 KiB
SQL

--liquibase formatted sql
-- PANELS · core · almacén central y por obra
--changeset panel:core-019a-warehouse-materials endDelimiter:; splitStatements:true
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='core' AND table_name='warehouse_materials'
CREATE TABLE core.warehouse_materials (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
tenant_id INTEGER NOT NULL,
code TEXT NOT NULL,
name TEXT NOT NULL,
unit TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (tenant_id, code)
);
CREATE INDEX idx_core_warehouse_materials_tenant ON core.warehouse_materials(tenant_id);
--changeset panel:core-019b-warehouses endDelimiter:; splitStatements:true
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='core' AND table_name='warehouses'
CREATE TABLE core.warehouses (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
tenant_id INTEGER NOT NULL,
kind TEXT NOT NULL CHECK (kind IN ('central', 'project')),
project_id BIGINT REFERENCES core.projects(id) ON DELETE CASCADE,
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'closing', 'closed')),
opened_at TIMESTAMPTZ NOT NULL DEFAULT now(),
closed_at TIMESTAMPTZ,
closed_by_name TEXT NOT NULL DEFAULT '',
close_notes TEXT NOT NULL DEFAULT ''
);
CREATE UNIQUE INDEX idx_core_warehouses_central ON core.warehouses(tenant_id) WHERE kind = 'central';
CREATE UNIQUE INDEX idx_core_warehouses_project ON core.warehouses(project_id) WHERE kind = 'project' AND project_id IS NOT NULL;
CREATE INDEX idx_core_warehouses_tenant ON core.warehouses(tenant_id);
--changeset panel:core-019c-warehouse-stock endDelimiter:; splitStatements:true
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='core' AND table_name='warehouse_stock'
CREATE TABLE core.warehouse_stock (
warehouse_id BIGINT NOT NULL REFERENCES core.warehouses(id) ON DELETE CASCADE,
material_id BIGINT NOT NULL REFERENCES core.warehouse_materials(id) ON DELETE CASCADE,
qty_on_hand NUMERIC(14,4) NOT NULL DEFAULT 0,
avg_unit_cost NUMERIC(14,4) NOT NULL DEFAULT 0,
PRIMARY KEY (warehouse_id, material_id)
);
--changeset panel:core-019d-warehouse-transfers endDelimiter:; splitStatements:true
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='core' AND table_name='warehouse_transfers'
CREATE TABLE core.warehouse_transfers (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
tenant_id INTEGER NOT NULL,
from_warehouse_id BIGINT NOT NULL REFERENCES core.warehouses(id),
to_warehouse_id BIGINT NOT NULL REFERENCES core.warehouses(id),
material_id BIGINT NOT NULL REFERENCES core.warehouse_materials(id),
quantity NUMERIC(14,4) NOT NULL,
unit_cost NUMERIC(14,4) NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'completed' CHECK (status IN ('pending', 'completed', 'cancelled')),
transfer_date DATE NOT NULL DEFAULT CURRENT_DATE,
notes TEXT NOT NULL DEFAULT '',
created_by_name TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_core_warehouse_transfers_tenant ON core.warehouse_transfers(tenant_id);
--changeset panel:core-019e-warehouse-movements endDelimiter:; splitStatements:true
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='core' AND table_name='warehouse_movements'
CREATE TABLE core.warehouse_movements (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
tenant_id INTEGER NOT NULL,
warehouse_id BIGINT NOT NULL REFERENCES core.warehouses(id),
material_id BIGINT NOT NULL REFERENCES core.warehouse_materials(id),
movement_type TEXT NOT NULL CHECK (movement_type IN ('entry', 'exit', 'transfer_out', 'transfer_in', 'adjustment')),
transfer_id BIGINT REFERENCES core.warehouse_transfers(id) ON DELETE SET NULL,
movement_date DATE NOT NULL DEFAULT CURRENT_DATE,
quantity NUMERIC(14,4) NOT NULL,
unit_cost NUMERIC(14,4) NOT NULL DEFAULT 0,
total_cost NUMERIC(14,2) NOT NULL DEFAULT 0,
budget_item_id BIGINT REFERENCES core.budget_items(id) ON DELETE SET NULL,
destination_note TEXT NOT NULL DEFAULT '',
expense_id BIGINT REFERENCES core.expense_entries(id) ON DELETE SET NULL,
adjustment_reason TEXT NOT NULL DEFAULT '',
created_by_id INTEGER,
created_by_name TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_core_warehouse_movements_wh ON core.warehouse_movements(warehouse_id, movement_date);
CREATE INDEX idx_core_warehouse_movements_budget ON core.warehouse_movements(budget_item_id);
--changeset panel:core-019f-warehouse-material-links-fk endDelimiter:; splitStatements:true
ALTER TABLE core.warehouse_material_budget_links
ADD CONSTRAINT warehouse_material_budget_links_material_fk
FOREIGN KEY (material_id) REFERENCES core.warehouse_materials(id) ON DELETE CASCADE;