Cambiando de views a tablas la bodega
This commit is contained in:
@@ -33,7 +33,8 @@ drop table if exists medios_pago cascade;
|
||||
drop table if exists cajas cascade;
|
||||
drop table if exists estados_produccion cascade;
|
||||
|
||||
create table restaurantes (
|
||||
create table restaurantes
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -41,7 +42,8 @@ create table restaurantes (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table categorias (
|
||||
create table categorias
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
@@ -50,7 +52,8 @@ create table categorias (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table zonas_produccion (
|
||||
create table zonas_produccion
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
@@ -59,7 +62,8 @@ create table zonas_produccion (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table usuarios (
|
||||
create table usuarios
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
auth0_id text not null,
|
||||
nombre text not null,
|
||||
@@ -68,7 +72,8 @@ create table usuarios (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table usuarios_restaurantes (
|
||||
create table usuarios_restaurantes
|
||||
(
|
||||
usuario_id uuid references usuarios,
|
||||
restaurante_id uuid references restaurantes,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -77,7 +82,8 @@ create table usuarios_restaurantes (
|
||||
primary key (usuario_id, restaurante_id)
|
||||
);
|
||||
|
||||
create table productores (
|
||||
create table productores
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
zona_produccion_id uuid references zonas_produccion,
|
||||
@@ -86,7 +92,8 @@ create table productores (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table recaudadores (
|
||||
create table recaudadores
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -94,7 +101,8 @@ create table recaudadores (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table meseros (
|
||||
create table meseros
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -102,7 +110,8 @@ create table meseros (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table administradores (
|
||||
create table administradores
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -110,7 +119,8 @@ create table administradores (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table ingredientes (
|
||||
create table ingredientes
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
medida text not null,
|
||||
@@ -120,7 +130,8 @@ create table ingredientes (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table productos (
|
||||
create table productos
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
precio_venta bigint not null,
|
||||
@@ -132,7 +143,8 @@ create table productos (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table recetas (
|
||||
create table recetas
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
producto_id uuid references productos,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
@@ -143,7 +155,8 @@ create table recetas (
|
||||
primary key (producto_id, ingrediente_id)
|
||||
);
|
||||
|
||||
create table proveedores (
|
||||
create table proveedores
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
rut text not null,
|
||||
nombre text not null,
|
||||
@@ -156,7 +169,8 @@ create table proveedores (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table compras (
|
||||
create table compras
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
fecha_compra date not null,
|
||||
en_arqueo bool not null default true,
|
||||
@@ -167,7 +181,8 @@ create table compras (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table facturas (
|
||||
create table facturas
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
numero text not null,
|
||||
monto_bruto bigint not null,
|
||||
@@ -182,7 +197,8 @@ create table facturas (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table compra_ingredientes (
|
||||
create table compra_ingredientes
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
unidades numeric not null,
|
||||
monto_unitario_bruto bigint not null,
|
||||
@@ -194,7 +210,8 @@ create table compra_ingredientes (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table sectores (
|
||||
create table sectores
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
@@ -203,7 +220,8 @@ create table sectores (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table tipos_canal (
|
||||
create table tipos_canal
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -211,7 +229,8 @@ create table tipos_canal (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table canales_venta (
|
||||
create table canales_venta
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
sector_id uuid references sectores,
|
||||
@@ -222,7 +241,8 @@ create table canales_venta (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table medios_pago (
|
||||
create table medios_pago
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -230,7 +250,8 @@ create table medios_pago (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table cajas (
|
||||
create table cajas
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
fondo bigint not null,
|
||||
apertura timestamptz not null,
|
||||
@@ -240,7 +261,8 @@ create table cajas (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table efectivos (
|
||||
create table efectivos
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
veinte_mil bigint not null,
|
||||
diez_mil bigint not null,
|
||||
@@ -257,7 +279,8 @@ create table efectivos (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table ventas (
|
||||
create table ventas
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
tiempo_venta timestamptz,
|
||||
mesero_id uuid references meseros,
|
||||
@@ -270,7 +293,8 @@ create table ventas (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table boletas_electronicas (
|
||||
create table boletas_electronicas
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
numero_boleta text not null,
|
||||
venta_id uuid references ventas,
|
||||
@@ -280,7 +304,8 @@ create table boletas_electronicas (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table boletas_exentas (
|
||||
create table boletas_exentas
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
venta_id uuid references ventas,
|
||||
restaurante_id uuid references restaurantes,
|
||||
@@ -289,7 +314,8 @@ create table boletas_exentas (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table estados_produccion (
|
||||
create table estados_produccion
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
@@ -297,7 +323,8 @@ create table estados_produccion (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table venta_productos (
|
||||
create table venta_productos
|
||||
(
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
tiempo_entrada timestamptz not null,
|
||||
tiempo_salida timestamptz null,
|
||||
@@ -309,6 +336,28 @@ create table venta_productos (
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table bodega_egresos
|
||||
(
|
||||
unidades numeric not null,
|
||||
fecha timestamptz not null,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
restaurante_id uuid references restaurantes,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp,
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table bodega_ingresos
|
||||
(
|
||||
unidades numeric not null,
|
||||
fecha timestamptz not null,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
restaurante_id uuid references restaurantes,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp,
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
------------------------------------
|
||||
--- Indexes para todas las referencias
|
||||
------------------------------------
|
||||
@@ -343,31 +392,114 @@ create index boletas_exentas_restaurante_id on boletas_exentas (restaurante_id);
|
||||
create index venta_productos_venta_id on venta_productos (venta_id);
|
||||
create index venta_productos_producto_id on venta_productos (producto_id);
|
||||
create index venta_productos_estado_id on venta_productos (estado_id);
|
||||
create index bodega_egresos_ingrediente_id on bodega_egresos (ingrediente_id);
|
||||
create index bodega_egresos_restaurante_id on bodega_egresos (restaurante_id);
|
||||
create index bodega_ingresos_ingrediente_id on bodega_ingresos (ingrediente_id);
|
||||
create index bodega_ingresos_restaurante_id on bodega_ingresos (restaurante_id);
|
||||
|
||||
------------------------------------
|
||||
--- Triggers para poblar la bodega
|
||||
------------------------------------
|
||||
|
||||
create or replace function bodega_egresos_trigger_insert_procedure() returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_egresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select recetas.ingrediente_id as ingrediente_id,
|
||||
recetas.unidades as unidades,
|
||||
date(venta_productos.tiempo_entrada) as fecha,
|
||||
ventas.restaurante_id as restaurante_id
|
||||
from ventas
|
||||
join venta_productos on ventas.id = venta_productos.venta_id
|
||||
join productos on venta_productos.producto_id = productos.id
|
||||
join recetas on productos.id = recetas.producto_id
|
||||
where venta_productos.id = NEW.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create or replace function bodega_egresos_trigger_delete_procedure()
|
||||
returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_egresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select recetas.ingrediente_id as ingrediente_id,
|
||||
recetas.unidades * -1 as unidades,
|
||||
date(venta_productos.tiempo_entrada) as fecha,
|
||||
ventas.restaurante_id as restaurante_id
|
||||
from ventas
|
||||
join venta_productos on ventas.id = venta_productos.venta_id
|
||||
join productos on venta_productos.producto_id = productos.id
|
||||
join recetas on productos.id = recetas.producto_id
|
||||
where venta_productos.id = OLD.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create or replace function bodega_ingresos_trigger_insert_procedure()
|
||||
returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_ingresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select compra_ingredientes.ingrediente_id as ingrediente_id,
|
||||
compra_ingredientes.unidades as unidades,
|
||||
compras.fecha_compra as fecha,
|
||||
compras.restaurante_id as restaurante_id
|
||||
from compras
|
||||
join compra_ingredientes on compras.id = compra_ingredientes.compra_id
|
||||
where compra_ingredientes.id = NEW.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create or replace function bodega_ingresos_trigger_delete_procedure()
|
||||
returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_ingresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select compra_ingredientes.ingrediente_id as ingrediente_id,
|
||||
compra_ingredientes.unidades * -1 as unidades,
|
||||
compras.fecha_compra as fecha,
|
||||
compras.restaurante_id as restaurante_id
|
||||
from compras
|
||||
join compra_ingredientes on compras.id = compra_ingredientes.compra_id
|
||||
where compra_ingredientes.id = OLD.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create trigger bodega_egresos_insert_trigger
|
||||
after insert
|
||||
on venta_productos
|
||||
for each row
|
||||
execute procedure bodega_egresos_trigger_insert_procedure();
|
||||
|
||||
create trigger bodega_egresos_delete_trigger
|
||||
after update of deleted_at
|
||||
on venta_productos
|
||||
for each row
|
||||
execute procedure bodega_egresos_trigger_delete_procedure();
|
||||
|
||||
create trigger bodega_ingresos_insert_trigger
|
||||
after insert
|
||||
on compra_ingredientes
|
||||
for each row
|
||||
execute procedure bodega_ingresos_trigger_insert_procedure();
|
||||
|
||||
create trigger bodega_ingresos_delete_trigger
|
||||
after update of deleted_at
|
||||
on compra_ingredientes
|
||||
for each row
|
||||
execute procedure bodega_ingresos_trigger_delete_procedure();
|
||||
|
||||
------------------------------------
|
||||
--- Views para crear la bodega
|
||||
------------------------------------
|
||||
|
||||
--- View para obtener la lista de egresos de bodega
|
||||
create view bodega_egresos as
|
||||
select recetas.ingrediente_id as ingrediente_id,
|
||||
recetas.unidades as unidades,
|
||||
date(venta_productos.tiempo_entrada) as fecha,
|
||||
ventas.restaurante_id as restaurante_id
|
||||
from ventas
|
||||
join venta_productos on ventas.id = venta_productos.venta_id
|
||||
join productos on venta_productos.producto_id = productos.id
|
||||
join recetas on productos.id = recetas.producto_id;
|
||||
|
||||
--- View para obtener la lista de ingresos a bodega
|
||||
create view bodega_ingresos as
|
||||
select compra_ingredientes.ingrediente_id as ingrediente_id,
|
||||
compra_ingredientes.unidades as unidades,
|
||||
compras.fecha_compra as fecha,
|
||||
compras.restaurante_id as restaurante_id
|
||||
from compras
|
||||
join compra_ingredientes on compras.id = compra_ingredientes.compra_id;
|
||||
|
||||
--- View que une el estado de bodega en una sola tabla
|
||||
create view bodega_movimientos as
|
||||
select ingrediente_id, unidades * -1 as unidades, fecha, restaurante_id
|
||||
|
||||
144
database/modifications/02-from-views-to-trigger.sql
Normal file
144
database/modifications/02-from-views-to-trigger.sql
Normal file
@@ -0,0 +1,144 @@
|
||||
start transaction;
|
||||
|
||||
drop view bodega_actual cascade;
|
||||
drop view bodega_movimientos cascade;
|
||||
drop view bodega_egresos cascade;
|
||||
drop view bodega_ingresos cascade;
|
||||
|
||||
create table bodega_egresos
|
||||
(
|
||||
unidades numeric not null,
|
||||
fecha timestamptz not null,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
restaurante_id uuid references restaurantes,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp,
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create table bodega_ingresos
|
||||
(
|
||||
unidades numeric not null,
|
||||
fecha timestamptz not null,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
restaurante_id uuid references restaurantes,
|
||||
created_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp,
|
||||
deleted_at timestamptz
|
||||
);
|
||||
|
||||
create index bodega_egresos_ingrediente_id on bodega_egresos (ingrediente_id);
|
||||
create index bodega_egresos_restaurante_id on bodega_egresos (restaurante_id);
|
||||
create index bodega_ingresos_ingrediente_id on bodega_ingresos (ingrediente_id);
|
||||
create index bodega_ingresos_restaurante_id on bodega_ingresos (restaurante_id);
|
||||
|
||||
create or replace function bodega_egresos_trigger_insert_procedure() returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_egresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select recetas.ingrediente_id as ingrediente_id,
|
||||
recetas.unidades as unidades,
|
||||
date(venta_productos.tiempo_entrada) as fecha,
|
||||
ventas.restaurante_id as restaurante_id
|
||||
from ventas
|
||||
join venta_productos on ventas.id = venta_productos.venta_id
|
||||
join productos on venta_productos.producto_id = productos.id
|
||||
join recetas on productos.id = recetas.producto_id
|
||||
where venta_productos.id = NEW.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create or replace function bodega_egresos_trigger_delete_procedure()
|
||||
returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_egresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select recetas.ingrediente_id as ingrediente_id,
|
||||
recetas.unidades * -1 as unidades,
|
||||
date(venta_productos.tiempo_entrada) as fecha,
|
||||
ventas.restaurante_id as restaurante_id
|
||||
from ventas
|
||||
join venta_productos on ventas.id = venta_productos.venta_id
|
||||
join productos on venta_productos.producto_id = productos.id
|
||||
join recetas on productos.id = recetas.producto_id
|
||||
where venta_productos.id = OLD.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create or replace function bodega_ingresos_trigger_insert_procedure()
|
||||
returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_ingresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select compra_ingredientes.ingrediente_id as ingrediente_id,
|
||||
compra_ingredientes.unidades as unidades,
|
||||
compras.fecha_compra as fecha,
|
||||
compras.restaurante_id as restaurante_id
|
||||
from compras
|
||||
join compra_ingredientes on compras.id = compra_ingredientes.compra_id
|
||||
where compra_ingredientes.id = NEW.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create or replace function bodega_ingresos_trigger_delete_procedure()
|
||||
returns trigger
|
||||
language PLPGSQL as
|
||||
$$
|
||||
BEGIN
|
||||
insert into bodega_ingresos (ingrediente_id, unidades, fecha, restaurante_id)
|
||||
select compra_ingredientes.ingrediente_id as ingrediente_id,
|
||||
compra_ingredientes.unidades * -1 as unidades,
|
||||
compras.fecha_compra as fecha,
|
||||
compras.restaurante_id as restaurante_id
|
||||
from compras
|
||||
join compra_ingredientes on compras.id = compra_ingredientes.compra_id
|
||||
where compra_ingredientes.id = OLD.id;
|
||||
RETURN null;
|
||||
END;
|
||||
$$;
|
||||
|
||||
create trigger bodega_egresos_insert_trigger
|
||||
after insert
|
||||
on venta_productos
|
||||
for each row
|
||||
execute procedure bodega_egresos_trigger_insert_procedure();
|
||||
|
||||
create trigger bodega_egresos_delete_trigger
|
||||
after update of deleted_at
|
||||
on venta_productos
|
||||
for each row
|
||||
execute procedure bodega_egresos_trigger_delete_procedure();
|
||||
|
||||
create trigger bodega_ingresos_insert_trigger
|
||||
after insert
|
||||
on compra_ingredientes
|
||||
for each row
|
||||
execute procedure bodega_ingresos_trigger_insert_procedure();
|
||||
|
||||
create trigger bodega_ingresos_delete_trigger
|
||||
after update of deleted_at
|
||||
on compra_ingredientes
|
||||
for each row
|
||||
execute procedure bodega_ingresos_trigger_delete_procedure();
|
||||
|
||||
--- View que une el estado de bodega en una sola tabla
|
||||
create view bodega_movimientos as
|
||||
select ingrediente_id, unidades * -1 as unidades, fecha, restaurante_id
|
||||
from bodega_egresos
|
||||
union all
|
||||
select ingrediente_id, unidades, fecha, restaurante_id
|
||||
from bodega_ingresos;
|
||||
|
||||
--- View que muestra el estado actual de la bodega por cada ingrediente
|
||||
create view bodega_actual as
|
||||
select ingrediente_id as ingrediente_id, sum(unidades) as stock, restaurante_id as restaurante_id
|
||||
from bodega_movimientos
|
||||
group by bodega_movimientos.ingrediente_id, bodega_movimientos.restaurante_id;
|
||||
|
||||
commit;
|
||||
Reference in New Issue
Block a user