#7709 -supplierPackaging_ReportSource_2 #3107

Open
robert wants to merge 19 commits from 7709-supplierPackaging_ReportSource_2 into dev
1 changed files with 138 additions and 127 deletions

View File

@ -1,7 +1,7 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`supplierPackaging_ReportSource`(
vFromDated DATE,
vSupplierFk INT
vNif VARCHAR(50)
)
BEGIN
/**
@ -10,79 +10,42 @@ BEGIN
* @param vFromDated Starting date
* @param vSupplierFk Supplier ID
*/
SET @vBalance = 0;
SET @vItemFk = NULL;
DECLARE vEntityId INT;
SELECT id INTO vEntityId
FROM supplier
WHERE nif = vNif;
CREATE OR REPLACE TEMPORARY TABLE tSupplierPackaging
SELECT supplierFk,
itemFk,
SELECT itemFk,
longName,
supplier,
`name`,
entryFk,
landed,
`in`,
`out`,
sref,
buyingValue,
IF (
NOT (@vItemFk <=> sub.itemFk),
@vBalance := (`in` - `out`),
@vBalance := (`in` - `out` + @vBalance)
) balance,
@vItemFk := sub.itemFk previousItemFk
CAST(sref AS CHAR) sref,
buyingValue
FROM (
SELECT sp.supplierFk,
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,
SELECT s.itemFk,
i.longName,
c.name,
CONCAT('T',s.ticketFk),
DATE(t.shipped),
CONCAT('T',s.ticketFk) entryFk,
DATE(t.shipped) landed,
-LEAST(s.quantity,0) `in`,
GREATEST(s.quantity,0) `out`,
t.cmrFk,
s.price * (100 - s.discount) / 100
t.cmrFk sref,
s.price * (100 - s.discount) / 100 buyingValue
carlosap marked this conversation as resolved
Review

hay una columna virtual para este valor, es sale

hay una columna virtual para este valor, es sale
Review

la columna total de sale el calculo que hace es el siguiente :
(price * quantity * (100 - discount) / 100)

la columna total de sale el calculo que hace es el siguiente : (`price` * `quantity` * (100 - `discount`) / 100)
FROM sale s
JOIN item i ON i.id = s.itemFk
JOIN packaging p ON p.itemFk = i.id
JOIN ticket t ON t.id = s.ticketFk
JOIN client c ON c.id = t.clientFk
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
WHERE c.fi = vNif
AND t.shipped >= vFromDated
AND p.isPackageReturnable
UNION ALL
SELECT vSupplierFk,
s.itemFk,
SELECT s.itemFk,
i.longName,
c.name,
'T previous',
@ -96,18 +59,16 @@ BEGIN
JOIN packaging p ON p.itemFk = i.id
JOIN ticket t ON t.id = s.ticketFk
JOIN client c ON c.id = t.clientFk
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
WHERE c.fi = vNif
AND t.shipped < vFromDated
AND p.isPackageReturnable
AND NOT t.isDeleted
GROUP BY s.itemFk
UNION ALL
SELECT vSupplierFk,
p.itemFk,
SELECT p.itemFk,
i.longName,
c.name,
CONCAT('TP',tp.ticketFk),
CONCAT('TP',tp.ticketFk) entryFk,
DATE(t.shipped),
-LEAST(tp.quantity,0) `in`,
GREATEST(tp.quantity,0) `out`,
@ -118,13 +79,11 @@ BEGIN
JOIN item i ON i.id = p.itemFk
JOIN ticket t ON t.id = tp.ticketFk
JOIN client c ON c.id = t.clientFk
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
WHERE c.fi = vNif
AND t.shipped >= vFromDated
AND p.isPackageReturnable
UNION ALL
SELECT vSupplierFk,
p.itemFk,
SELECT p.itemFk,
i.longName,
c.name,
'TP previous',
@ -138,8 +97,7 @@ BEGIN
JOIN item i ON i.id = p.itemFk
JOIN ticket t ON t.id = tp.ticketFk
JOIN client c ON c.id = t.clientFk
JOIN supplier su ON su.nif = c.fi
WHERE su.id = vSupplierFk
WHERE c.fi = vNif
AND t.shipped < vFromDated
AND p.isPackageReturnable
AND NOT t.isDeleted
@ -148,19 +106,72 @@ BEGIN
)sub
WHERE `out` OR `in`;
SELECT supplierFk,
itemFk,
IF vEntityId IS NULL THEN
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
WHERE `out` OR `in`;
END IF;
SELECT vEntityId,
itemFk,
longName,
name,
entryFk,
landed,
CAST(`in` AS DECIMAL(10,0)) `in`,
CAST(`out` AS DECIMAL(10,0)) `out`,
sref,
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
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;
END$$