salix/db/routines/vn/procedures/recipe_Plaster.sql

36 lines
927 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`recipe_Plaster`(vItemFk INT, vTicketFk INT, vQuantity INT)
BEGIN
DECLARE vLastCost DECIMAL(10,2);
DECLARE vWarehouseFk INT;
DECLARE vShipped DATE;
DECLARE vEntryFk INT;
DECLARE vTravelFk INT;
CALL cache.last_buy_refresh(FALSE);
SELECT warehouseFk, shipped
INTO vWarehouseFk, vShipped
FROM vn.ticket
WHERE id = vTicketFk;
UPDATE vn.ticket t
JOIN vn.item i ON i.id = vItemFk
SET t.nickname = CONCAT(vQuantity,' ',i.longName)
WHERE t.id = vTicketFk;
SELECT b.buyingValue INTO vLastCost
FROM vn.buy b
JOIN cache.last_buy lb ON lb.buy_id = b.id
WHERE lb.item_id = vItemFk
ORDER BY (lb.warehouse_id = vWarehouseFk) DESC
LIMIT 1;
INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price)
SELECT vTicketFk, 98, - vQuantity, i.longName, vLastCost
FROM vn.item i
WHERE i.id = vItemFk;
END$$
DELIMITER ;