2024-03-15 13:46:43 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_devalueA2`(
|
2024-05-09 12:24:20 +00:00
|
|
|
vSelf INT,
|
2024-03-15 13:46:43 +00:00
|
|
|
vShelvingFK VARCHAR(10),
|
|
|
|
vBuyingValue DECIMAL(10,4),
|
|
|
|
vQuantity INT
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Devalua un item modificando su calidad de A1 a A2.
|
|
|
|
* Si no existe el item A2 lo crea y genera los movimientos de las entradas
|
|
|
|
* de almacén y shelvings correspondientes
|
|
|
|
*
|
2024-05-09 12:24:20 +00:00
|
|
|
* @param vSelf Id de artículo a devaluar
|
2024-03-15 13:46:43 +00:00
|
|
|
* @param vShelvingFK Ubicación actual del artículo
|
|
|
|
* @param vBuyingValue Nuevo precio de coste
|
2024-05-15 15:03:33 +00:00
|
|
|
* @param vQuantity Cantidad del ítem a pasar a A2
|
2024-03-15 13:46:43 +00:00
|
|
|
*/
|
|
|
|
DECLARE vItemA2Fk INT;
|
|
|
|
DECLARE vLastBuyFk BIGINT;
|
|
|
|
DECLARE vA1BuyFk INT;
|
|
|
|
DECLARE vA2BuyFk INT;
|
|
|
|
DECLARE vTargetEntryFk INT;
|
|
|
|
DECLARE vTargetEntryDate DATE;
|
|
|
|
DECLARE vTargetItemShelvingFk BIGINT;
|
|
|
|
DECLARE vWarehouseFk INT;
|
|
|
|
DECLARE vCacheFk INT;
|
|
|
|
DECLARE vLastEntryFk INT;
|
|
|
|
DECLARE vCurrentVisible INT;
|
2024-05-09 12:24:20 +00:00
|
|
|
DECLARE vDevalueTravelFk INT;
|
2024-03-15 13:46:43 +00:00
|
|
|
DECLARE vCurdate DATE;
|
2024-05-15 15:03:33 +00:00
|
|
|
DECLARE vBuyingValueOriginal DECIMAL(10,4);
|
2024-03-15 13:46:43 +00:00
|
|
|
|
|
|
|
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
IF (SELECT TRUE FROM item WHERE id = vSelf AND (category <> 'A1' OR category IS NULL)) THEN
|
2024-03-15 13:46:43 +00:00
|
|
|
CALL util.throw('Item has not category A1');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT warehouseFk INTO vWarehouseFk
|
2024-07-10 05:46:18 +00:00
|
|
|
FROM userMultiConfig
|
2024-03-15 13:46:43 +00:00
|
|
|
WHERE userFk = account.myUser_getId();
|
|
|
|
|
|
|
|
IF NOT vWarehouseFk OR vWarehouseFk IS NULL THEN
|
|
|
|
CALL util.throw ('Operator has not a valid warehouse');
|
|
|
|
END IF;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
IF vQuantity <= 0 OR vQuantity IS NULL THEN
|
|
|
|
CALL util.throw ('The quantity is incorrect');
|
|
|
|
END IF;
|
|
|
|
|
2024-03-15 13:46:43 +00:00
|
|
|
SELECT util.VN_CURDATE() INTO vCurdate;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
SELECT t.id INTO vDevalueTravelFk
|
2024-03-15 13:46:43 +00:00
|
|
|
FROM travel t
|
2024-05-09 12:24:20 +00:00
|
|
|
JOIN travelConfig tc
|
2024-03-15 13:46:43 +00:00
|
|
|
WHERE t.shipped = vCurdate
|
|
|
|
AND t.landed = vCurdate
|
|
|
|
AND t.warehouseInFk = vWarehouseFk
|
|
|
|
AND t.warehouseOutFk = tc.devalueWarehouseOutFk
|
|
|
|
AND t.agencyModeFk = tc.devalueAgencyModeFk
|
|
|
|
LIMIT 1;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
IF NOT vDevalueTravelFk OR vDevalueTravelFk IS NULL THEN
|
2024-03-15 13:46:43 +00:00
|
|
|
INSERT INTO travel (
|
|
|
|
shipped,
|
|
|
|
landed,
|
|
|
|
warehouseInFk,
|
|
|
|
warehouseOutFk,
|
|
|
|
`ref`,
|
|
|
|
isReceived,
|
|
|
|
agencyModeFk)
|
|
|
|
SELECT vCurdate,
|
|
|
|
vCurdate,
|
|
|
|
vWarehouseFk,
|
|
|
|
tc.devalueWarehouseOutFk,
|
|
|
|
tc.devalueRef,
|
|
|
|
TRUE,
|
|
|
|
tc.devalueAgencyModeFk
|
|
|
|
FROM travelConfig tc;
|
2024-05-09 12:24:20 +00:00
|
|
|
SET vDevalueTravelFk = LAST_INSERT_ID();
|
2024-03-15 13:46:43 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
SELECT id, DATE(dated) INTO vTargetEntryFk, vTargetEntryDate
|
2024-03-15 13:46:43 +00:00
|
|
|
FROM `entry` e
|
2024-05-09 12:24:20 +00:00
|
|
|
WHERE DATE(dated) = vCurdate
|
2024-05-15 15:03:33 +00:00
|
|
|
AND typeFk = 'devaluation'
|
2024-05-09 12:24:20 +00:00
|
|
|
AND travelFk = vDevalueTravelFk
|
2024-03-15 13:46:43 +00:00
|
|
|
ORDER BY created DESC
|
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
CALL buyUltimate(vWarehouseFk, vCurdate);
|
|
|
|
|
2024-05-15 15:03:33 +00:00
|
|
|
SELECT b.entryFk, bu.buyFk,IFNULL(b.buyingValue, 0) INTO vLastEntryFk, vLastBuyFk, vBuyingValueOriginal
|
2024-03-15 13:46:43 +00:00
|
|
|
FROM tmp.buyUltimate bu
|
2024-05-15 15:03:33 +00:00
|
|
|
JOIN vn.buy b ON b.id = bu.buyFk
|
2024-05-09 12:24:20 +00:00
|
|
|
WHERE bu.itemFk = vSelf
|
2024-03-15 13:46:43 +00:00
|
|
|
AND bu.warehouseFk = vWarehouseFk;
|
2024-05-15 15:03:33 +00:00
|
|
|
|
|
|
|
IF vBuyingValue > vBuyingValueOriginal THEN
|
|
|
|
CALL util.throw ('Price not valid');
|
|
|
|
END IF;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
IF vLastEntryFk IS NULL OR vLastBuyFk IS NULL THEN
|
|
|
|
CALL util.throw ('The item has not a buy');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT id,visible INTO vTargetItemShelvingFk, vCurrentVisible
|
2024-03-15 13:46:43 +00:00
|
|
|
FROM itemShelving
|
|
|
|
WHERE shelvingFk = vShelvingFK COLLATE utf8mb3_general_ci
|
2024-05-09 12:24:20 +00:00
|
|
|
AND itemFk = vSelf
|
2024-03-15 13:46:43 +00:00
|
|
|
LIMIT 1;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
IF vCurrentVisible IS NULL THEN
|
|
|
|
CALL util.throw ('The shelving has not a visible for this item');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vQuantity > vCurrentVisible THEN
|
2024-03-15 13:46:43 +00:00
|
|
|
CALL util.throw('Quantity is greater than visible');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
IF NOT vTargetEntryFk OR vTargetEntryFk IS NULL
|
2024-05-09 12:24:20 +00:00
|
|
|
OR NOT vTargetEntryDate <=> vCurdate THEN
|
2024-03-15 13:46:43 +00:00
|
|
|
INSERT INTO entry(
|
|
|
|
travelFk,
|
|
|
|
supplierFk,
|
|
|
|
dated,
|
|
|
|
commission,
|
|
|
|
currencyFk,
|
|
|
|
companyFk,
|
|
|
|
clonedFrom,
|
|
|
|
typeFk
|
|
|
|
)
|
2024-05-09 12:24:20 +00:00
|
|
|
SELECT vDevalueTravelFk,
|
2024-03-15 13:46:43 +00:00
|
|
|
supplierFk,
|
|
|
|
vCurdate,
|
|
|
|
commission,
|
|
|
|
currencyFk,
|
|
|
|
companyFk,
|
|
|
|
vLastEntryFk,
|
|
|
|
'devaluation'
|
|
|
|
FROM entry
|
|
|
|
WHERE id = vLastEntryFk;
|
|
|
|
|
|
|
|
SET vTargetEntryFk = LAST_INSERT_ID();
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT i.id INTO vItemA2Fk
|
|
|
|
FROM item i
|
|
|
|
JOIN (
|
|
|
|
SELECT i.id,
|
|
|
|
i.name,
|
|
|
|
i.subname,
|
|
|
|
i.value5,
|
|
|
|
i.value6,
|
|
|
|
i.value7,
|
|
|
|
i.value8,
|
|
|
|
i.value9,
|
|
|
|
i.value10,
|
|
|
|
i.NumberOfItemsPerCask,
|
|
|
|
i.EmbalageCode,
|
|
|
|
i.quality
|
|
|
|
FROM item i
|
2024-05-09 12:24:20 +00:00
|
|
|
WHERE i.id = vSelf
|
2024-03-15 13:46:43 +00:00
|
|
|
)i2 ON i2.name <=> i.name
|
|
|
|
AND i2.subname <=> i.subname
|
|
|
|
AND i2.value5 <=> i.value5
|
|
|
|
AND i2.value6 <=> i.value6
|
|
|
|
AND i2.value8 <=> i.value8
|
|
|
|
AND i2.value9 <=> i.value9
|
|
|
|
AND i2.value10 <=> i.value10
|
|
|
|
AND i2.NumberOfItemsPerCask <=> i.NumberOfItemsPerCask
|
|
|
|
AND i2.EmbalageCode <=> i.EmbalageCode
|
|
|
|
AND i2.quality <=> i.quality
|
|
|
|
WHERE i.id <> i2.id
|
|
|
|
AND i.category = 'A2'
|
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
IF vItemA2Fk IS NULL THEN
|
|
|
|
INSERT INTO item (
|
|
|
|
equivalent,
|
|
|
|
name,
|
|
|
|
`size`,
|
|
|
|
stems,
|
|
|
|
minPrice,
|
|
|
|
isToPrint,
|
|
|
|
family,
|
|
|
|
box,
|
|
|
|
category,
|
|
|
|
originFk,
|
|
|
|
doPhoto,
|
|
|
|
image,
|
|
|
|
inkFk,
|
|
|
|
intrastatFk,
|
|
|
|
hasMinPrice,
|
|
|
|
created,
|
|
|
|
comment,
|
|
|
|
typeFk,
|
|
|
|
generic,
|
|
|
|
producerFk,
|
|
|
|
description,
|
|
|
|
density,
|
|
|
|
relevancy,
|
|
|
|
expenseFk,
|
|
|
|
isActive,
|
|
|
|
longName,
|
|
|
|
subName,
|
|
|
|
minimum,
|
|
|
|
upToDown,
|
|
|
|
supplyResponseFk,
|
|
|
|
hasKgPrice,
|
|
|
|
isFloramondo,
|
|
|
|
isFragile,
|
|
|
|
numberOfItemsPerCask,
|
|
|
|
embalageCode,
|
|
|
|
quality,
|
|
|
|
stemMultiplier,
|
|
|
|
itemPackingTypeFk,
|
|
|
|
packingOut,
|
|
|
|
genericFk,
|
|
|
|
isLaid,
|
|
|
|
lastUsed,
|
|
|
|
weightByPiece,
|
|
|
|
editorFk,
|
|
|
|
recycledPlastic,
|
|
|
|
nonRecycledPlastic)
|
|
|
|
SELECT equivalent,
|
|
|
|
name,
|
|
|
|
`size`,
|
|
|
|
stems,
|
|
|
|
minPrice,
|
|
|
|
isToPrint,
|
|
|
|
family,
|
|
|
|
box,
|
|
|
|
'A2',
|
|
|
|
originFk,
|
|
|
|
doPhoto,
|
|
|
|
image,
|
|
|
|
inkFk,
|
|
|
|
intrastatFk,
|
|
|
|
hasMinPrice,
|
|
|
|
created,
|
|
|
|
comment,
|
|
|
|
typeFk,
|
|
|
|
generic,
|
|
|
|
producerFk,
|
|
|
|
description,
|
|
|
|
density,
|
|
|
|
relevancy,
|
|
|
|
expenseFk,
|
|
|
|
isActive,
|
|
|
|
longName,
|
2024-05-09 12:24:20 +00:00
|
|
|
subName,
|
2024-03-15 13:46:43 +00:00
|
|
|
minimum,
|
|
|
|
upToDown,
|
|
|
|
supplyResponseFk,
|
|
|
|
hasKgPrice,
|
|
|
|
isFloramondo,
|
|
|
|
isFragile,
|
|
|
|
numberOfItemsPerCask,
|
|
|
|
embalageCode,
|
|
|
|
quality,
|
|
|
|
stemMultiplier,
|
|
|
|
itemPackingTypeFk,
|
|
|
|
packingOut,
|
|
|
|
genericFk,
|
|
|
|
isLaid,
|
|
|
|
lastUsed,
|
|
|
|
weightByPiece,
|
|
|
|
editorFk,
|
|
|
|
recycledPlastic,
|
|
|
|
nonRecycledPlastic
|
|
|
|
FROM item
|
2024-05-09 12:24:20 +00:00
|
|
|
WHERE id = vSelf;
|
2024-03-15 13:46:43 +00:00
|
|
|
|
|
|
|
SET vItemA2Fk = LAST_INSERT_ID();
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
INSERT INTO itemTag (itemFk, tagFk, `value`, intValue, priority, editorFk)
|
|
|
|
SELECT vItemA2Fk, tagFk, `value`, intValue, priority, editorFk
|
|
|
|
FROM itemTag
|
|
|
|
WHERE id = vSelf;
|
|
|
|
|
2024-03-15 13:46:43 +00:00
|
|
|
UPDATE itemTaxCountry itc
|
2024-05-09 12:24:20 +00:00
|
|
|
JOIN itemTaxCountry itc2 ON itc2.itemFk = vSelf
|
2024-03-15 13:46:43 +00:00
|
|
|
AND itc2.countryFk = itc.countryFk
|
|
|
|
SET itc2.taxClassFk = itc.taxClassFk
|
|
|
|
WHERE itc.id = vItemA2Fk;
|
2024-05-09 12:24:20 +00:00
|
|
|
|
2024-03-15 13:46:43 +00:00
|
|
|
INSERT INTO itemBotanical (itemFk, genusFk, specieFk)
|
|
|
|
SELECT vItemA2Fk, genusFk, specieFk
|
|
|
|
FROM itemBotanical
|
2024-05-09 12:24:20 +00:00
|
|
|
WHERE itemFk = vSelf;
|
2024-03-15 13:46:43 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vQuantity = vCurrentVisible THEN
|
|
|
|
DELETE FROM itemShelving
|
|
|
|
WHERE id = vTargetItemShelvingFk;
|
|
|
|
ELSE
|
|
|
|
UPDATE itemShelving
|
|
|
|
SET visible = vCurrentVisible - vQuantity
|
|
|
|
WHERE id = vTargetItemShelvingFk;
|
|
|
|
END IF;
|
|
|
|
|
2024-05-08 15:38:52 +00:00
|
|
|
INSERT INTO buy(
|
|
|
|
entryFk,
|
|
|
|
itemFk,
|
|
|
|
quantity,
|
|
|
|
buyingValue,
|
|
|
|
freightValue,
|
|
|
|
isIgnored,
|
|
|
|
stickers,
|
|
|
|
packagingFk,
|
|
|
|
packing,
|
|
|
|
`grouping`,
|
|
|
|
groupingMode,
|
|
|
|
comissionValue,
|
|
|
|
packageValue,
|
|
|
|
price1,
|
|
|
|
price2,
|
|
|
|
price3,
|
|
|
|
minPrice,
|
|
|
|
isChecked,
|
|
|
|
location,
|
|
|
|
weight,
|
|
|
|
itemOriginalFk)
|
|
|
|
SELECT vTargetEntryFk,
|
|
|
|
itemFk,
|
|
|
|
- LEAST(vQuantity, vCurrentVisible),
|
2024-05-09 12:24:20 +00:00
|
|
|
buyingValue,
|
2024-05-08 15:38:52 +00:00
|
|
|
freightValue,
|
|
|
|
TRUE,
|
|
|
|
stickers,
|
|
|
|
packagingFk,
|
|
|
|
packing,
|
|
|
|
`grouping`,
|
|
|
|
groupingMode,
|
|
|
|
comissionValue,
|
|
|
|
packageValue,
|
|
|
|
price1,
|
|
|
|
price2,
|
|
|
|
price3,
|
|
|
|
minPrice,
|
|
|
|
isChecked,
|
|
|
|
location,
|
|
|
|
weight,
|
|
|
|
itemOriginalFk
|
|
|
|
FROM vn.buy
|
|
|
|
WHERE id = vLastBuyFk
|
|
|
|
UNION
|
|
|
|
SELECT vTargetEntryFk,
|
|
|
|
vItemA2Fk,
|
|
|
|
vQuantity,
|
|
|
|
vBuyingValue,
|
|
|
|
freightValue,
|
|
|
|
TRUE,
|
|
|
|
stickers,
|
|
|
|
packagingFk,
|
|
|
|
packing,
|
|
|
|
`grouping`,
|
|
|
|
groupingMode,
|
|
|
|
comissionValue,
|
|
|
|
packageValue,
|
|
|
|
price1,
|
|
|
|
price2,
|
|
|
|
price3,
|
|
|
|
minPrice,
|
|
|
|
isChecked,
|
|
|
|
location,
|
|
|
|
weight,
|
|
|
|
itemOriginalFk
|
|
|
|
FROM vn.buy
|
|
|
|
WHERE id = vLastBuyFk;
|
|
|
|
|
2024-05-09 12:24:20 +00:00
|
|
|
INSERT IGNORE INTO itemShelving (
|
2024-03-15 13:46:43 +00:00
|
|
|
itemFk,
|
|
|
|
shelvingFk,
|
|
|
|
visible,
|
|
|
|
`grouping`,
|
|
|
|
packing,
|
|
|
|
packagingFk,
|
|
|
|
userFk,
|
|
|
|
isChecked)
|
|
|
|
SELECT vItemA2Fk,
|
|
|
|
shelvingFk,
|
2024-05-09 12:24:20 +00:00
|
|
|
vQuantity ,
|
2024-03-15 13:46:43 +00:00
|
|
|
`grouping`,
|
|
|
|
packing,
|
|
|
|
packagingFk,
|
|
|
|
account.myUser_getId(),
|
|
|
|
isChecked
|
|
|
|
FROM itemShelving
|
2024-05-09 12:24:20 +00:00
|
|
|
WHERE itemFK = vSelf
|
|
|
|
AND shelvingFk = vShelvingFK COLLATE utf8mb3_general_ci
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
visible = vQuantity + VALUES(visible);
|
2024-03-15 13:46:43 +00:00
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
CALL cache.visible_refresh(vCacheFk, TRUE, vWarehouseFk);
|
|
|
|
CALL cache.available_refresh(vCacheFk, TRUE, vWarehouseFk, vCurdate);
|
|
|
|
CALL buy_recalcPricesByBuy(vA2BuyFk);
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|