You are on page 1of 2

create database parcial2; use parcial2; create table productos (id_producto int not null auto_increment primary key,

descripcion char(50), precio int, saldo double, estado char(1), porcentaje_descuento int); create table movimientos_productos (numero_mvto int not null auto_increment primary key, tipomov char(1), cantidad int, fecha_mvto date); insert into productos (descripcion, precio, saldo, estado, porcentaje_descuento) values ('Producto1', 100, 10, 'P', 10), ('Producto2', 200, 20, 'P', 10), ('Producto3', 300, 30, 'D', 10), ('Producto4', 400, 40, 'D', 10), ('Producto5', 500, 50, 'P', 10); delimiter // create function descuento (valor int, descu int) RETURNS FLOAT deterministic begin set @des=(descu/100); set @vlt=valor*@des; return valor-@vlt; end; //

delimiter // create procedure realizamvto (in t_mvto char(1), prod int, cantidad int, s int) begin set @saldo := (select saldo from productos where id_producto=prod); if (s=1) then if (@saldo=0) then if (t_mvto='1') then update productos set saldo=saldo+cantidad where id_producto=prod; insert into movimientos_productos (tipomov, cantidad, fecha_mvto) value ('1', cantidad, now()); else Select 'No se puede realizar el movimiento saldo insuficiente'; end if; else if (@saldo>=cantidad) then if (t_mvto='1') then update productos set saldo=saldo+cantidad where id_producto=prod;

insert into movimientos_productos (tipomov, cantidad, fecha_mvto) value ('1', cantidad, now()); select 'Compra realizada'; end if; if (t_mvto='2') then set @desc := (select porcentaje_descuento from productos where id_producto=prod and estado='D'); set @val := (select precio from productos where id_producto=prod); update productos set saldo=saldo-cantidad where id_producto=prod; insert into movimientos_productos (tipomov, cantidad, fecha_mvto) value ('2', cantidad, now()); select descuento(@val, @desc); end if; if (t_mvto='3') then set @desc := (select porcentaje_descuento from productos where id_producto=prod and estado='D'); set @val := (select precio from productos where id_producto=prod); update productos set saldo=saldo-cantidad where id_producto=prod; insert into movimientos_productos (tipomov, cantidad, fecha_mvto) value ('3', cantidad, now()); select descuento(@val, @desc); end if; else if (t_mvto='1') then update productos set saldo=cantidad where id_producto=prod; insert into movimientos_productos (tipomov, cantidad, fecha_mvto) value ('1', cantidad, now()); else Select 'No se puede realizar el movimiento saldo insuficiente'; end if; end if; end if; else Select 'No se puede realizar el movimiento fallo del sistema'; end if; end; //

You might also like