#7709 -supplierPackaging_ReportSource_2 #3107
|
@ -1,166 +1,177 @@
|
||||||
DELIMITER $$
|
DELIMITER $$
|
||||||
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`supplierPackaging_ReportSource`(
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`supplierPackaging_ReportSource`(
|
||||||
vFromDated DATE,
|
vFromDated DATE,
|
||||||
vSupplierFk INT
|
vNif VARCHAR(50)
|
||||||
)
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
* Create a report with packaging balance
|
* Create a report with packaging balance
|
||||||
*
|
*
|
||||||
* @param vFromDated Starting date
|
* @param vFromDated Starting date
|
||||||
* @param vSupplierFk Supplier ID
|
* @param vSupplierFk Supplier ID
|
||||||
*/
|
*/
|
||||||
SET @vBalance = 0;
|
DECLARE vEntityId INT;
|
||||||
SET @vItemFk = NULL;
|
|
||||||
|
SELECT id INTO vEntityId
|
||||||
|
FROM supplier
|
||||||
|
WHERE nif = vNif;
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging
|
CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging
|
||||||
SELECT supplierFk,
|
SELECT itemFk,
|
||||||
itemFk,
|
|
||||||
longName,
|
longName,
|
||||||
supplier,
|
`name`,
|
||||||
entryFk,
|
entryFk,
|
||||||
landed,
|
landed,
|
||||||
`in`,
|
`in`,
|
||||||
`out`,
|
`out`,
|
||||||
sref,
|
CAST(sref AS CHAR) sref,
|
||||||
buyingValue,
|
buyingValue
|
||||||
IF (
|
|
||||||
NOT (@vItemFk <=> sub.itemFk),
|
|
||||||
@vBalance := (`in` - `out`),
|
|
||||||
@vBalance := (`in` - `out` + @vBalance)
|
|
||||||
) balance,
|
|
||||||
@vItemFk := sub.itemFk previousItemFk
|
|
||||||
FROM (
|
FROM (
|
||||||
SELECT sp.supplierFk,
|
SELECT s.itemFk,
|
||||||
sp.itemFk,
|
|
||||||
sp.longName,
|
|
||||||
sp.supplier,
|
|
||||||
CONCAT('E',sp.entryFk) entryFk,
|
|
||||||
sp.landed,
|
|
||||||
sp.`in`,
|
|
||||||
sp.`out`,
|
|
||||||
e.invoiceNumber sref,
|
|
||||||
sp.buyingValue
|
|
||||||
FROM supplierPackaging sp
|
|
||||||
JOIN entry e ON e.id = sp.entryFk
|
|
||||||
WHERE sp.supplierFk = vSupplierFk
|
|
||||||
AND sp.landed >= vFromDated
|
|
||||||
UNION ALL
|
|
||||||
SELECT vSupplierFk,
|
|
||||||
itemFk,
|
|
||||||
longName,
|
|
||||||
supplier,
|
|
||||||
'E previous',
|
|
||||||
vFromDated,
|
|
||||||
SUM(`in`),
|
|
||||||
SUM(`out`),
|
|
||||||
NULL,
|
|
||||||
AVG(buyingValue)
|
|
||||||
FROM supplierPackaging
|
|
||||||
WHERE supplierFk = vSupplierFk
|
|
||||||
AND landed < vFromDated
|
|
||||||
GROUP BY itemFk
|
|
||||||
UNION ALL
|
|
||||||
SELECT vSupplierFk,
|
|
||||||
s.itemFk,
|
|
||||||
i.longName,
|
i.longName,
|
||||||
c.name,
|
c.name,
|
||||||
CONCAT('T',s.ticketFk),
|
CONCAT('T',s.ticketFk) entryFk,
|
||||||
DATE(t.shipped),
|
DATE(t.shipped) landed,
|
||||||
-LEAST(s.quantity,0) `in`,
|
-LEAST(s.quantity,0) `in`,
|
||||||
GREATEST(s.quantity,0) `out`,
|
GREATEST(s.quantity,0) `out`,
|
||||||
t.cmrFk,
|
t.cmrFk sref,
|
||||||
s.price * (100 - s.discount) / 100
|
s.price * (100 - s.discount) / 100 buyingValue
|
||||||
carlosap marked this conversation as resolved
|
|||||||
FROM sale s
|
FROM sale s
|
||||||
JOIN item i ON i.id = s.itemFk
|
JOIN item i ON i.id = s.itemFk
|
||||||
JOIN packaging p ON p.itemFk = i.id
|
JOIN packaging p ON p.itemFk = i.id
|
||||||
JOIN ticket t ON t.id = s.ticketFk
|
JOIN ticket t ON t.id = s.ticketFk
|
||||||
JOIN client c ON c.id = t.clientFk
|
JOIN client c ON c.id = t.clientFk
|
||||||
JOIN supplier su ON su.nif = c.fi
|
WHERE c.fi = vNif
|
||||||
WHERE su.id = vSupplierFk
|
|
||||||
AND t.shipped >= vFromDated
|
AND t.shipped >= vFromDated
|
||||||
AND p.isPackageReturnable
|
AND p.isPackageReturnable
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT vSupplierFk,
|
SELECT s.itemFk,
|
||||||
s.itemFk,
|
i.longName,
|
||||||
i.longName,
|
c.name,
|
||||||
c.name,
|
'T previous',
|
||||||
'T previous',
|
vFromDated,
|
||||||
vFromDated,
|
SUM(-LEAST(s.quantity,0)) `in`,
|
||||||
SUM(-LEAST(s.quantity,0)) `in`,
|
SUM(GREATEST(s.quantity,0)) `out`,
|
||||||
SUM(GREATEST(s.quantity,0)) `out`,
|
NULL,
|
||||||
NULL,
|
AVG(s.price * (100 - s.discount) / 100)
|
||||||
AVG(s.price * (100 - s.discount) / 100)
|
FROM sale s
|
||||||
FROM sale s
|
JOIN item i ON i.id = s.itemFk
|
||||||
JOIN item i ON i.id = s.itemFk
|
JOIN packaging p ON p.itemFk = i.id
|
||||||
JOIN packaging p ON p.itemFk = i.id
|
JOIN ticket t ON t.id = s.ticketFk
|
||||||
JOIN ticket t ON t.id = s.ticketFk
|
JOIN client c ON c.id = t.clientFk
|
||||||
JOIN client c ON c.id = t.clientFk
|
WHERE c.fi = vNif
|
||||||
JOIN supplier su ON su.nif = c.fi
|
AND t.shipped < vFromDated
|
||||||
WHERE su.id = vSupplierFk
|
AND p.isPackageReturnable
|
||||||
AND t.shipped < vFromDated
|
AND NOT t.isDeleted
|
||||||
AND p.isPackageReturnable
|
GROUP BY s.itemFk
|
||||||
AND NOT t.isDeleted
|
UNION ALL
|
||||||
GROUP BY s.itemFk
|
SELECT p.itemFk,
|
||||||
UNION ALL
|
i.longName,
|
||||||
SELECT vSupplierFk,
|
c.name,
|
||||||
p.itemFk,
|
CONCAT('TP',tp.ticketFk) entryFk,
|
||||||
i.longName,
|
DATE(t.shipped),
|
||||||
c.name,
|
-LEAST(tp.quantity,0) `in`,
|
||||||
CONCAT('TP',tp.ticketFk),
|
GREATEST(tp.quantity,0) `out`,
|
||||||
DATE(t.shipped),
|
t.cmrFk,
|
||||||
-LEAST(tp.quantity,0) `in`,
|
0
|
||||||
GREATEST(tp.quantity,0) `out`,
|
FROM ticketPackaging tp
|
||||||
t.cmrFk,
|
JOIN packaging p ON p.id = tp.packagingFk
|
||||||
0
|
JOIN item i ON i.id = p.itemFk
|
||||||
FROM ticketPackaging tp
|
JOIN ticket t ON t.id = tp.ticketFk
|
||||||
JOIN packaging p ON p.id = tp.packagingFk
|
JOIN client c ON c.id = t.clientFk
|
||||||
JOIN item i ON i.id = p.itemFk
|
WHERE c.fi = vNif
|
||||||
JOIN ticket t ON t.id = tp.ticketFk
|
AND t.shipped >= vFromDated
|
||||||
JOIN client c ON c.id = t.clientFk
|
AND p.isPackageReturnable
|
||||||
JOIN supplier su ON su.nif = c.fi
|
UNION ALL
|
||||||
WHERE su.id = vSupplierFk
|
SELECT p.itemFk,
|
||||||
AND t.shipped >= vFromDated
|
i.longName,
|
||||||
AND p.isPackageReturnable
|
c.name,
|
||||||
UNION ALL
|
'TP previous',
|
||||||
SELECT vSupplierFk,
|
vFromDated,
|
||||||
p.itemFk,
|
SUM(-LEAST(tp.quantity,0)) `in`,
|
||||||
i.longName,
|
SUM(GREATEST(tp.quantity,0)) `out`,
|
||||||
c.name,
|
NULL,
|
||||||
'TP previous',
|
0
|
||||||
vFromDated,
|
FROM ticketPackaging tp
|
||||||
SUM(-LEAST(tp.quantity,0)) `in`,
|
JOIN packaging p ON p.id = tp.packagingFk
|
||||||
SUM(GREATEST(tp.quantity,0)) `out`,
|
JOIN item i ON i.id = p.itemFk
|
||||||
NULL,
|
JOIN ticket t ON t.id = tp.ticketFk
|
||||||
0
|
JOIN client c ON c.id = t.clientFk
|
||||||
FROM ticketPackaging tp
|
WHERE c.fi = vNif
|
||||||
JOIN packaging p ON p.id = tp.packagingFk
|
AND t.shipped < vFromDated
|
||||||
JOIN item i ON i.id = p.itemFk
|
AND p.isPackageReturnable
|
||||||
JOIN ticket t ON t.id = tp.ticketFk
|
AND NOT t.isDeleted
|
||||||
JOIN client c ON c.id = t.clientFk
|
GROUP BY p.itemFk
|
||||||
JOIN supplier su ON su.nif = c.fi
|
ORDER BY itemFk , landed, entryFk
|
||||||
WHERE su.id = vSupplierFk
|
)sub
|
||||||
AND t.shipped < vFromDated
|
WHERE `out` OR `in`;
|
||||||
AND p.isPackageReturnable
|
|
||||||
AND NOT t.isDeleted
|
IF vEntityId IS NULL THEN
|
||||||
GROUP BY p.itemFk
|
|
||||||
ORDER BY itemFk, landed, entryFk
|
SELECT id INTO vEntityId
|
||||||
|
FROM client
|
||||||
|
WHERE fi = vNif;
|
||||||
|
|
||||||
|
INSERT INTO tSupplierPackaging
|
||||||
|
SELECT itemFk,
|
||||||
|
longName,
|
||||||
|
`name`,
|
||||||
|
entryFk,
|
||||||
|
landed,
|
||||||
|
`in`,
|
||||||
|
`out`,
|
||||||
|
sref,
|
||||||
|
buyingValue
|
||||||
|
FROM (
|
||||||
|
SELECT sp.itemFk,
|
||||||
|
sp.longName,
|
||||||
|
sp.supplier name,
|
||||||
|
CONCAT('E',sp.entryFk) entryFk,
|
||||||
|
sp.landed,
|
||||||
|
sp.`in`,
|
||||||
|
sp.`out`,
|
||||||
|
e.invoiceNumber sref,
|
||||||
|
sp.buyingValue
|
||||||
|
FROM supplierPackaging sp
|
||||||
|
JOIN entry e ON e.id = sp.entryFk
|
||||||
|
JOIN supplier s ON s.id = sp.supplierFk
|
||||||
|
WHERE s.nif = vNif
|
||||||
|
AND sp.landed >= vFromDated
|
||||||
|
UNION ALL
|
||||||
|
SELECT itemFk,
|
||||||
|
longName,
|
||||||
|
supplier,
|
||||||
|
'E previous',
|
||||||
|
vFromDated,
|
||||||
|
IF(SUM(`in`) + SUM(-`out`)>0, SUM(`in`) + SUM(-`out`), 0) `in`,
|
||||||
|
-IF(SUM(`in`) + SUM(-`out`)<0, SUM(`in`) + SUM(-`out`), 0) `out`,
|
||||||
|
NULL,
|
||||||
|
AVG(buyingValue)
|
||||||
|
FROM supplierPackaging sp
|
||||||
|
JOIN supplier s ON s.id = sp.supplierFk
|
||||||
|
WHERE s.nif = vNif
|
||||||
|
AND landed < vFromDated
|
||||||
|
GROUP BY itemFk
|
||||||
|
ORDER BY itemFk , landed, entryFk
|
||||||
) sub
|
) sub
|
||||||
WHERE `out` OR `in`;
|
WHERE `out` OR `in`;
|
||||||
|
|
||||||
SELECT supplierFk,
|
END IF;
|
||||||
|
|
||||||
|
SELECT vEntityId,
|
||||||
itemFk,
|
itemFk,
|
||||||
longName,
|
longName,
|
||||||
supplier,
|
name,
|
||||||
entryFk,
|
entryFk,
|
||||||
landed,
|
landed,
|
||||||
CAST(`in` AS DECIMAL(10,0)) `in`,
|
CAST(`in` AS DECIMAL(10,0)) `in`,
|
||||||
CAST(`out` AS DECIMAL(10,0)) `out`,
|
CAST(`out` AS DECIMAL(10,0)) `out`,
|
||||||
sref,
|
sref,
|
||||||
buyingValue,
|
buyingValue,
|
||||||
balance
|
CAST(SUM(IF(`in`=0, -`out`, `in`)) OVER (PARTITION BY itemFk ORDER BY itemFk , landed, entryFk) AS DECIMAL(10,2)) balance
|
||||||
FROM tSupplierPackaging
|
FROM tSupplierPackaging
|
||||||
WHERE NOT (NOT balance AND landed < CURDATE() - INTERVAL 1 YEAR);
|
WHERE NOT (landed < util.VN_CURDATE() - INTERVAL 1 YEAR)
|
||||||
|
ORDER BY itemFk, landed, entryFk;
|
||||||
|
|
||||||
DROP TEMPORARY TABLE tSupplierPackaging;
|
DROP TEMPORARY TABLE tSupplierPackaging;
|
||||||
END$$
|
END$$
|
||||||
|
|
Loading…
Reference in New Issue
hay una columna virtual para este valor, es sale
la columna total de sale el calculo que hace es el siguiente :
(
price
*quantity
* (100 -discount
) / 100)