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

174 lines
4.0 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`entry_moveNotPrinted`(vSelf INT,
vDays INT,
vChangeEntry BOOL,
OUT vNewEntryFk INT)
BEGIN
/**
* Crea una entrada a futuro y divide las lineas de buy de
* la entrada original en función de las etiquetas impresas
*
* @param vSelf Identificador de entry
* @param vDays Número de dias a futuro que se quiere la nueva entrada
* @param vChangeEntry Si se modifica la entrada original
*
* @return vNewEntryFk Identificador de la nueva entrada
*/
DECLARE vNewTravelFk INT;
DECLARE vTravelFk INT;
DECLARE vInvoiceAmountOldEntryFk DECIMAL(10,2);
DECLARE vInvoiceAmountNewEntry DECIMAL(10,2);
DECLARE vIsOrdered BOOL;
DECLARE vIsConfirmed BOOL;
DECLARE vInvoiceNumber VARCHAR(50);
DECLARE vTypeFk VARCHAR(100);
SELECT travelFk, isOrdered, isConfirmed, invoiceNumber, typeFk INTO vTravelFk, vIsOrdered, vIsConfirmed, vInvoiceNumber, vTypeFk
FROM entry
WHERE id = vSelf;
CALL travel_clone(vTravelFk, vDays, vNewTravelFk);
CALL entry_cloneWithoutBuy(vSelf, vNewEntryFk);
UPDATE entry e
SET e.travelFk = vNewTravelFk,
e.evaNotes = CONCAT('No impresas de: ', vSelf, ' ', IFNULL(e.evaNotes,'')),
e.isOrdered = vIsOrdered,
e.isConfirmed = vIsConfirmed,
e.invoiceNumber = vInvoiceNumber,
e.typeFk = vTypeFk
WHERE e.id = vNewEntryFk;
IF vChangeEntry THEN
UPDATE buy b
SET b.entryFk = vNewEntryFk
WHERE b.printedStickers = 0
AND b.entryFk = vSelf;
END IF;
INSERT INTO buy(entryFk,
itemFk,
quantity,
buyingValue,
freightValue,
isIgnored,
stickers,
packing,
`grouping`,
groupingMode,
comissionValue,
packageValue,
packagingFk,
price1,
price2,
price3,
minPrice,
workerFk,
weight,
itemOriginalFk)
SELECT vNewEntryFk,
itemFk,
((stickers - CAST(printedStickers AS INT)) * packing) quantity,
buyingValue,
freightValue,
isIgnored,
(stickers - CAST(printedStickers AS INT)) stickers,
packing,
`grouping`,
groupingMode,
comissionValue,
packageValue,
packagingFk,
price1,
price2,
price3,
minPrice,
workerFk,
weight,
itemOriginalFk
FROM buy b
WHERE b.entryFk = vSelf
AND b.printedStickers != b.stickers;
IF vChangeEntry THEN
UPDATE buy
SET stickers = printedStickers,
quantity = printedStickers * packing
WHERE entryFk = vSelf;
ELSE
INSERT INTO buy(entryFk, itemFk)
SELECT vSelf, i.id
FROM item i
WHERE i.description = 'MISFIT'
LIMIT 1;
INSERT INTO buy(entryFk,
itemFk,
quantity,
buyingValue,
freightValue,
isIgnored,
stickers,
packing,
`grouping`,
groupingMode,
comissionValue,
packageValue,
location,
packagingFk,
price1,
price2,
price3,
minPrice)
SELECT vSelf,
itemFk,
(CAST(printedStickers AS INT) - stickers) * packing quantity,
buyingValue,
freightValue,
TRUE isIgnored,
CAST(printedStickers AS INT) - stickers,
packing,
`grouping`,
groupingMode,
comissionValue,
packageValue,
location,
packagingFk,
price1,
price2,
price3,
minPrice
FROM buy
WHERE entryFk = vSelf
AND printedStickers != stickers;
END IF;
SELECT SUM(IFNULL(b.buyingValue * b.quantity, 0)) INTO vInvoiceAmountOldEntryFk
FROM buy b
WHERE b.entryFk = vSelf;
UPDATE entry e
JOIN buy b ON b.entryFk = e.id
SET e.reference = CONCAT(e.reference,'(1)'),
e.invoiceAmount = vInvoiceAmountOldEntryFk
WHERE e.id = vSelf;
SELECT SUM(IFNULL(b.buyingValue * b.quantity, 0)) INTO vInvoiceAmountNewEntry
FROM buy b
WHERE b.entryFk = vNewEntryFk;
UPDATE entry e
JOIN buy b ON b.entryFk = e.id
SET e.reference = CONCAT(e.reference,'(2)'),
e.invoiceAmount = vInvoiceAmountNewEntry
WHERE e.id = vNewEntryFk;
UPDATE entry
SET gestDocFk = (SELECT gestDocFk FROM entry WHERE id = vSelf LIMIT 1)
WHERE id = vNewEntryFk;
INSERT INTO duaEntry (duaFk, entryFk)
SELECT duaFk, vNewEntryFk FROM duaEntry WHERE entryFk = vSelf LIMIT 1;
END$$
DELIMITER ;