Agregando el backend de lumen
This commit is contained in:
148
database/inserts.sql
Normal file
148
database/inserts.sql
Normal file
@@ -0,0 +1,148 @@
|
||||
-----------------------------------
|
||||
--- Inserts basicos
|
||||
-----------------------------------
|
||||
insert into estados_produccion (nombre)
|
||||
values ('Enviada'),
|
||||
('Preparada'),
|
||||
('Vendida'),
|
||||
('Mermada'),
|
||||
('Devuelta');
|
||||
|
||||
insert into tipos_canal (nombre)
|
||||
values ('Mesa'),
|
||||
('Delivery');
|
||||
|
||||
-----------------------------------
|
||||
--- Creando restaurant de prueba
|
||||
-----------------------------------
|
||||
|
||||
insert into restaurantes (nombre)
|
||||
values ('Todo Rico Restaurant');
|
||||
|
||||
insert into categorias (nombre, restaurante_id)
|
||||
values ('Platos de fondo', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Bebidas', (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into sectores (nombre, restaurante_id)
|
||||
values ('Salon Principal', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Salon Secundario', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Virtual', (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into canales_venta (nombre, sector_id, tipo_canal_id, restaurante_id)
|
||||
values ('1', (select id from sectores where nombre = 'Salon Principal'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('2', (select id from sectores where nombre = 'Salon Principal'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('3', (select id from sectores where nombre = 'Salon Principal'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('4', (select id from sectores where nombre = 'Salon Principal'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('5', (select id from sectores where nombre = 'Salon Principal'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('6', (select id from sectores where nombre = 'Salon Secundario'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('7', (select id from sectores where nombre = 'Salon Secundario'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('8', (select id from sectores where nombre = 'Salon Secundario'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('9', (select id from sectores where nombre = 'Salon Secundario'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('10', (select id from sectores where nombre = 'Salon Secundario'), (select id from tipos_canal where nombre = 'Mesa'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Pedidos Ya', (select id from sectores where nombre = 'Virtual'), (select id from tipos_canal where nombre = 'Delivery'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Uber Eats', (select id from sectores where nombre = 'Virtual'), (select id from tipos_canal where nombre = 'Delivery'), (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into zonas_produccion (nombre, restaurante_id)
|
||||
values ('Cocina', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Barra', (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into usuarios (auth0_id, nombre)
|
||||
values ('123456', 'Mesero');
|
||||
|
||||
insert into usuarios_restaurantes (usuario_id, restaurante_id)
|
||||
values ((select id from usuarios where nombre = 'Mesero'), (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into productores (usuario_id, zona_produccion_id)
|
||||
values ((select id from usuarios where nombre = 'Mesero'), (select id from zonas_produccion where nombre = 'Cocina'));
|
||||
|
||||
-----------------------------------
|
||||
--- Inventando ingredientes y productos de venta
|
||||
-----------------------------------
|
||||
|
||||
insert into ingredientes (nombre, medida, restaurante_id)
|
||||
values ('Papas', 'Kg', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Papas Pre-Fritas', 'Kg', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Sal', 'Kg', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Mantequilla', 'Kg', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Leche', 'L', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Chuletas', 'Kg', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Aceite', 'L', (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Coca-Cola', 'Unidad', (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into productos (nombre, precio_venta, categoria_id, zona_produccion_id, restaurante_id)
|
||||
values ('Chuleta con Pure', 4390, (select id from categorias where nombre = 'Platos de fondo'),
|
||||
(select id from zonas_produccion where nombre = 'Cocina'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Chuleta con Papas Fritas', 4690, (select id from categorias where nombre = 'Platos de fondo'),
|
||||
(select id from zonas_produccion where nombre = 'Cocina'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('Coca-Cola', 1200, (select id from categorias where nombre = 'Bebidas'),
|
||||
(select id from zonas_produccion where nombre = 'Barra'), (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into recetas (producto_id, ingrediente_id, unidades)
|
||||
values ((select id from productos where nombre = 'Chuleta con Pure'), (select id from ingredientes where nombre = 'Papas'), 0.25),
|
||||
((select id from productos where nombre = 'Chuleta con Pure'), (select id from ingredientes where nombre = 'Sal'), 0.002),
|
||||
((select id from productos where nombre = 'Chuleta con Pure'), (select id from ingredientes where nombre = 'Mantequilla'), 0.03),
|
||||
((select id from productos where nombre = 'Chuleta con Pure'), (select id from ingredientes where nombre = 'Leche'), 0.2),
|
||||
((select id from productos where nombre = 'Chuleta con Pure'), (select id from ingredientes where nombre = 'Chuletas'), 0.25);
|
||||
|
||||
insert into recetas (producto_id, ingrediente_id, unidades)
|
||||
values ((select id from productos where nombre = 'Chuleta con Papas Fritas'), (select id from ingredientes where nombre = 'Papas Pre-Fritas'), 0.25),
|
||||
((select id from productos where nombre = 'Chuleta con Papas Fritas'), (select id from ingredientes where nombre = 'Aceite'), 0.01),
|
||||
((select id from productos where nombre = 'Chuleta con Papas Fritas'), (select id from ingredientes where nombre = 'Sal'), 0.002),
|
||||
((select id from productos where nombre = 'Chuleta con Papas Fritas'), (select id from ingredientes where nombre = 'Chuletas'), 0.25);
|
||||
|
||||
insert into recetas (producto_id, ingrediente_id, unidades)
|
||||
values ((select id from productos where nombre = 'Coca-Cola'), (select id from ingredientes where nombre = 'Coca-Cola'), 1);
|
||||
|
||||
-----------------------------------
|
||||
--- Creando compras para crear un inventario
|
||||
-----------------------------------
|
||||
|
||||
insert into proveedores (rut, nombre, descripcion, direccion, telefono, restaurante_id)
|
||||
values ('123-1', 'Coca-Cola', 'Vende Coca Cola', null, null, (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('123-1', 'Mr. Carnes', 'Vende Carnes', null, null, (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('123-1', 'Santa Isabel', 'Vende Productos Generales', null, null, (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into compras (fecha_compra, proveedor_id, restaurante_id)
|
||||
values ('2021-01-01'::date, (select id from proveedores where nombre = 'Coca-Cola'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('2021-02-01'::date, (select id from proveedores where nombre = 'Mr. Carnes'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('2021-02-01'::date, (select id from proveedores where nombre = 'Santa Isabel'), (select id from restaurantes where nombre = 'Todo Rico Restaurant')),
|
||||
('2021-01-01'::date, (select id from proveedores where nombre = 'Coca-Cola'), (select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into compra_ingredientes (unidades, monto_unitario_bruto, iva, ila, monto_unitario_neto, compra_id, ingrediente_id)
|
||||
values (20, 500, 500 * .19, 0, 500 * 1.19, (select id from compras limit 1 offset 0), (select id from ingredientes where nombre = 'Coca-Cola')),
|
||||
(50, 1000, 1000 * .19, 0, 1000 * 1.19, (select id from compras limit 1 offset 1), (select id from ingredientes where nombre = 'Chuletas')),
|
||||
(25, 1000, 1000 * .19, 0, 1000 * 1.19, (select id from compras limit 1 offset 2), (select id from ingredientes where nombre = 'Papas')),
|
||||
(10, 500, 500 * .19, 0, 500 * 1.19, (select id from compras limit 1 offset 2), (select id from ingredientes where nombre = 'Papas Pre-Fritas')),
|
||||
(5, 300, 300 * .19, 0, 300 * 1.19, (select id from compras limit 1 offset 2), (select id from ingredientes where nombre = 'Sal')),
|
||||
(10, 2000, 2000 * .19, 0, 2000 * 1.19, (select id from compras limit 1 offset 2), (select id from ingredientes where nombre = 'Mantequilla')),
|
||||
(10, 700, 700 * .19, 0, 700 * 1.19, (select id from compras limit 1 offset 2), (select id from ingredientes where nombre = 'Leche')),
|
||||
(50, 1000, 1000 * .19, 0, 1000 * 1.19, (select id from compras limit 1 offset 2), (select id from ingredientes where nombre = 'Aceite')),
|
||||
(20, 500, 500 * .19, 0, 500 * 1.19, (select id from compras limit 1 offset 3), (select id from ingredientes where nombre = 'Coca-Cola'));
|
||||
|
||||
insert into facturas (numero, monto_bruto, iva, ila, monto_neto, fecha_emision, fecha_vencimiento, compra_id)
|
||||
values ('1', 500 * 20, 500 * 20 * .19, 0, 500 * 20 * 1.19, '2021-01-01'::date, '2021-01-30'::date, (select id from compras limit 1 offset 0)),
|
||||
('1', 1000 * 50, 1000 * 50 * .19, 0, 1000 * 50 * 1.19, '2021-02-01'::date, '2021-02-28'::date, (select id from compras limit 1 offset 1)),
|
||||
('1', 108500, 20615, 0, 129115, '2021-02-10'::date, '2021-02-28'::date, (select id from compras limit 1 offset 2)),
|
||||
('1', 500 * 20, 500 * 20 * .19, 0, 500 * 20 * 1.19, '2021-01-01'::date, '2021-01-30'::date, (select id from compras limit 1 offset 0));
|
||||
|
||||
-----------------------------------
|
||||
--- Creando ventas para descontar del inventario
|
||||
-----------------------------------
|
||||
|
||||
insert into ventas (tiempo_venta, mesero_id, canal_id, restaurante_id)
|
||||
values (current_timestamp, (select id from meseros limit 1), (select id from canales_venta where nombre = '1'),
|
||||
(select id from restaurantes where nombre = 'Todo Rico Restaurant'));
|
||||
|
||||
insert into venta_productos (tiempo_entrada, tiempo_salida, venta_id, producto_id, estado_id)
|
||||
values (current_timestamp, null, (select id from ventas limit 1), (select id from productos where nombre = 'Coca-Cola'),
|
||||
(select id from estados_produccion where nombre = 'Enviada')),
|
||||
(current_timestamp, null, (select id from ventas limit 1), (select id from productos where nombre = 'Coca-Cola'),
|
||||
(select id from estados_produccion where nombre = 'Enviada')),
|
||||
(current_timestamp, null, (select id from ventas limit 1), (select id from productos where nombre = 'Coca-Cola'),
|
||||
(select id from estados_produccion where nombre = 'Enviada')),
|
||||
(current_timestamp, null, (select id from ventas limit 1), (select id from productos where nombre = 'Chuleta con Papas Fritas'),
|
||||
(select id from estados_produccion where nombre = 'Enviada')),
|
||||
(current_timestamp, null, (select id from ventas limit 1), (select id from productos where nombre = 'Chuleta con Papas Fritas'),
|
||||
(select id from estados_produccion where nombre = 'Enviada')),
|
||||
(current_timestamp, null, (select id from ventas limit 1), (select id from productos where nombre = 'Chuleta con Pure'),
|
||||
(select id from estados_produccion where nombre = 'Enviada'));
|
||||
BIN
database/modelo.vpp
Normal file
BIN
database/modelo.vpp
Normal file
Binary file not shown.
439
database/schema.sql
Normal file
439
database/schema.sql
Normal file
@@ -0,0 +1,439 @@
|
||||
create table restaurantes (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
|
||||
create table categorias (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index categorias_restaurante_id on categorias (restaurante_id);
|
||||
|
||||
create table zonas_produccion (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
|
||||
);
|
||||
create index zonas_produccion_restaurante_id on zonas_produccion (restaurante_id);
|
||||
|
||||
create table usuarios (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
auth0_id text not null,
|
||||
nombre text not null
|
||||
);
|
||||
|
||||
create table usuarios_restaurantes (
|
||||
usuario_id uuid references usuarios,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp,
|
||||
primary key (usuario_id, restaurante_id)
|
||||
);
|
||||
|
||||
create table productores (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
zona_produccion_id uuid references zonas_produccion,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index productores_usuario_id on productores (usuario_id);
|
||||
create index productores_zona_produccion_id on productores (zona_produccion_id);
|
||||
|
||||
create table recaudadores (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index recaudadores_usuario_id on recaudadores (usuario_id);
|
||||
|
||||
create table meseros (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index meseros_usuario_id on meseros (usuario_id);
|
||||
|
||||
create table administradores (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
usuario_id uuid references usuarios,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index administradores_usuario_id on administradores (usuario_id);
|
||||
|
||||
create table ingredientes (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
medida text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index ingredientes_restaurante_id on ingredientes (restaurante_id);
|
||||
|
||||
create table productos (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
precio_venta bigint not null,
|
||||
categoria_id uuid references categorias,
|
||||
zona_produccion_id uuid references zonas_produccion,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index productos_categoria_id on productos (categoria_id);
|
||||
create index productos_zona_produccion_id on productos (zona_produccion_id);
|
||||
create index productos_restaurante_id on productos (restaurante_id);
|
||||
|
||||
create table recetas (
|
||||
producto_id uuid references productos,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
unidades numeric not null,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp,
|
||||
primary key (producto_id, ingrediente_id)
|
||||
);
|
||||
|
||||
create table proveedores (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
rut text not null,
|
||||
nombre text not null,
|
||||
descripcion text not null,
|
||||
direccion text null,
|
||||
telefono text null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index proveedores_restaurante_id on proveedores (restaurante_id);
|
||||
|
||||
create table compras (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
fecha_compra date not null,
|
||||
proveedor_id uuid references proveedores,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index compras_proveedor_id on compras (proveedor_id);
|
||||
create index compras_restaurante_id on compras (restaurante_id);
|
||||
|
||||
create table facturas (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
numero text not null,
|
||||
monto_bruto bigint not null,
|
||||
iva bigint not null default 0,
|
||||
ila bigint not null default 0,
|
||||
monto_neto bigint not null,
|
||||
fecha_emision date not null,
|
||||
fecha_vencimiento date not null,
|
||||
compra_id uuid references compras,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index facturas_compra_id on facturas (compra_id);
|
||||
|
||||
create table compra_ingredientes (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
unidades numeric not null,
|
||||
monto_unitario_bruto bigint not null,
|
||||
iva bigint not null default 0,
|
||||
ila bigint not null default 0,
|
||||
monto_unitario_neto bigint not null,
|
||||
compra_id uuid references compras,
|
||||
ingrediente_id uuid references ingredientes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index compra_ingredientes_compra_id on compra_ingredientes (compra_id);
|
||||
create index compra_ingredientes_ingrediente_id on compra_ingredientes (ingrediente_id);
|
||||
|
||||
create table sectores (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index sectores_restaurante_id on sectores (restaurante_id);
|
||||
|
||||
create table tipos_canal (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
|
||||
create table canales_venta (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
sector_id uuid references sectores,
|
||||
tipo_canal_id uuid references tipos_canal,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index canales_venta_sector_id on canales_venta (sector_id);
|
||||
create index canales_venta_tipo_canal_id on canales_venta (tipo_canal_id);
|
||||
create index canales_venta_restaurante_id on canales_venta (restaurante_id);
|
||||
|
||||
create table ventas (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
tiempo_venta timestamptz,
|
||||
mesero_id uuid references meseros,
|
||||
canal_id uuid references canales_venta,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index ventas_mesero_id on ventas (mesero_id);
|
||||
create index ventas_canal_id on ventas (canal_id);
|
||||
create index ventas_restaurante_id on ventas (restaurante_id);
|
||||
|
||||
create table boletas_electronicas (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
numero_boleta text not null,
|
||||
venta_id uuid references ventas,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index boletas_electronicas_venta_id on boletas_electronicas (venta_id);
|
||||
create index boletas_electronicas_restaurante_id on boletas_electronicas (restaurante_id);
|
||||
|
||||
create table boletas_exentas (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
venta_id uuid references ventas,
|
||||
restaurante_id uuid references restaurantes,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
create index boletas_exentas_venta_id on boletas_exentas (venta_id);
|
||||
create index boletas_exentas_restaurante_id on boletas_exentas (restaurante_id);
|
||||
|
||||
create table estados_produccion (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
nombre text not null,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
|
||||
create table venta_productos (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
tiempo_entrada timestamptz not null,
|
||||
tiempo_salida timestamptz null,
|
||||
venta_id uuid references ventas,
|
||||
producto_id uuid references productos,
|
||||
estado_id uuid references estados_produccion,
|
||||
inserted_at timestamptz not null default current_timestamp,
|
||||
updated_at timestamptz not null default current_timestamp
|
||||
);
|
||||
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);
|
||||
|
||||
------------------------------------
|
||||
--- Triggers para update
|
||||
------------------------------------
|
||||
|
||||
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
|
||||
RETURNS TRIGGER AS
|
||||
$$
|
||||
BEGIN
|
||||
NEW.updated_at = current_timestamp;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
create trigger trigger_update_restaurantes
|
||||
before update
|
||||
on restaurantes
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_categorias
|
||||
before update
|
||||
on categorias
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_zonas_produccion
|
||||
before update
|
||||
on zonas_produccion
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_usuarios
|
||||
before update
|
||||
on usuarios
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_usuarios_restaurantes
|
||||
before update
|
||||
on usuarios_restaurantes
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_productores
|
||||
before update
|
||||
on productores
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_recaudadores
|
||||
before update
|
||||
on recaudadores
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_meseros
|
||||
before update
|
||||
on meseros
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_administradores
|
||||
before update
|
||||
on administradores
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_ingredientes
|
||||
before update
|
||||
on ingredientes
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_productos
|
||||
before update
|
||||
on productos
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_recetas
|
||||
before update
|
||||
on recetas
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_proveedores
|
||||
before update
|
||||
on proveedores
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_compras
|
||||
before update
|
||||
on compras
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_facturas
|
||||
before update
|
||||
on facturas
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_compra_ingredientes
|
||||
before update
|
||||
on compra_ingredientes
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_sectores
|
||||
before update
|
||||
on sectores
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_tipos_canal
|
||||
before update
|
||||
on tipos_canal
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_canales_venta
|
||||
before update
|
||||
on canales_venta
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_ventas
|
||||
before update
|
||||
on ventas
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_boletas_electronicas
|
||||
before update
|
||||
on boletas_electronicas
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_boletas_exentas
|
||||
before update
|
||||
on boletas_exentas
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_estados_produccion
|
||||
before update
|
||||
on estados_produccion
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
create trigger trigger_update_venta_productos
|
||||
before update
|
||||
on venta_productos
|
||||
for each row
|
||||
execute procedure trigger_set_timestamp();
|
||||
|
||||
------------------------------------
|
||||
--- 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
|
||||
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;
|
||||
8
database/selects.sql
Normal file
8
database/selects.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
select *
|
||||
from bodega_ingresos;
|
||||
select *
|
||||
from bodega_egresos;
|
||||
select *
|
||||
from bodega_movimientos;
|
||||
select *
|
||||
from bodega_actual;
|
||||
Reference in New Issue
Block a user