Está en la página 1de 2

----------------------ENVIAR A PRODUCCION ULTIMA ORDEN

create procedure Enviar_Produccion


as
DECLARE @id_mozo bigint,
@id_mesa bigint,
@id_cliente bigint,
@id_OP bigint,
@estado bigint,
@id bigint

set @id_OP = (select top 1 id_orden_pedido from Orden_Pedido order by


id_orden_pedido desc)
set @estado = (select id_estado from Orden_Pedido where id_orden_pedido = @id_OP)
set @id=(select id_orden_pedido from Orden_Produccion where id_orden_pedido=@id_OP)
if(@id_OP=@id)
begin
print ('error , no se puede duplicar id_orden_pedido')
end
else
begin
if(@estado = 1)

begin
declare C_OP cursor local
for select id_mozo, id_mesa, id_cliente
from Orden_Pedido where id_orden_pedido = @id_OP
open C_OP
fetch C_OP into @id_mozo,@id_mesa,@id_cliente
insert into Orden_Produccion
values(@id_OP,GETDATE(),2,@id_mozo,@id_mesa,@id_cliente)
close C_OP
deallocate C_OP

declare @cantidad int,


@precio decimal(10,2),
@id_orden bigint,
@producto bigint
set @id_orden = (select top 1 id_orden_produccion from Orden_Produccion order by
id_orden_produccion desc)
declare C_OP_D cursor local
for select cantidad_orden_pedido, precio_unitario,id_producto
from Orden_Pedido_Detalle where id_orden_pedido = @id_OP
open C_OP_D
fetch C_OP_D into @cantidad,@precio,@producto
while(@@fetch_status = 0)
begin
insert into Orden_Produccion_Detalle
values(@producto,@id_orden,@id_OP,@cantidad,@precio)
fetch C_OP_D into @cantidad,@precio,@producto
end
close C_OP_D
deallocate C_OP_D
end
else
begin
print 'Error, no se puede enviar porque la orden esta anulada,cancelada o no
existe'
end
end

También podría gustarte