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

52 lines
1.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`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;
DECLARE vCurrencyFk INT;
CALL cache.last_buy_refresh(FALSE);
SELECT warehouseFk, shipped, currencyFk
INTO vWarehouseFk, vShipped, vCurrencyFk
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,
foreignPrice
)SELECT vTicketFk,
98,
- vQuantity,
i.longName,
vLastCost,
currency_getRate(vCurrencyFk, NULL) * vLastCost
FROM vn.item i
WHERE i.id = vItemFk;
END$$
DELIMITER ;