salix/db/changes/10120-december/02-buyAfterUpsert.sql

61 lines
1.5 KiB
SQL

DROP procedure IF EXISTS `vn`.`buy_afterUpsert`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`buy_afterUpsert`(vSelf INT)
BEGIN
/**
* Triggered actions when a buy is updated or inserted.
*
* @param vSelf The buy reference
*/
DECLARE vEntryFk INT;
DECLARE vItemFk INT;
DECLARE vStickers INT;
DECLARE vPacking INT;
DECLARE vWarehouse INT;
DECLARE vWarehouseOut INT;
DECLARE vIsMerchandise BOOL;
DECLARE vIsFeedStock BOOL;
SELECT entryFk, itemFk, stickers, packing
INTO vEntryFk, vItemFk, vStickers, vPacking
FROM buy
WHERE id = vSelf;
SELECT t.warehouseInFk, t.warehouseOutFk
INTO vWarehouse, vWarehouseOut
FROM entry e
JOIN travel t ON t.id = e.travelFk
WHERE e.id = vEntryFk;
SELECT k.merchandise INTO vIsMerchandise
FROM itemCategory k
JOIN itemType it ON it.categoryFk = k.id
JOIN item i ON i.typeFk = it.id
WHERE i.id = vItemFk;
IF vIsMerchandise THEN
REPLACE bi.rotacion SET
Id_Article = vItemFk,
warehouse_id = vWarehouse,
cm3 = buy_getUnitVolume(vSelf);
END IF;
SELECT isFeedStock INTO vIsFeedStock
FROM warehouse WHERE id = vWarehouseOut AND id <> 13;
IF vIsFeedStock AND buy_hasNotifyPassport(vSelf, vItemFk) THEN
CALL buy_notifyPassport(vSelf, vItemFk, vStickers, vPacking);
INSERT IGNORE INTO producer(`name`)
SELECT es.company_name
FROM buy b
JOIN edi.ekt be ON be.id = b.ektFk
JOIN edi.supplier es ON es.supplier_id = be.pro
WHERE b.id = vSelf;
END IF;
END$$
DELIMITER ;