7430_devToTest #2490
|
@ -20,57 +20,62 @@ BEGIN
|
||||||
(KEY (ticketFk))
|
(KEY (ticketFk))
|
||||||
ENGINE = MEMORY
|
ENGINE = MEMORY
|
||||||
WITH ticket AS(
|
WITH ticket AS(
|
||||||
SELECT id ticketFk, DATE(shipped) dated
|
SELECT id ticketFk, companyFk, DATE(shipped) dated
|
||||||
FROM vn.ticket t
|
FROM vn.ticket t
|
||||||
WHERE clientFk = vSelf
|
WHERE clientFk = vSelf
|
||||||
AND refFk IS NULL
|
AND refFk IS NULL
|
||||||
AND NOT isDeleted
|
AND NOT isDeleted
|
||||||
AND totalWithoutVat <> 0
|
AND totalWithoutVat <> 0
|
||||||
), dated AS(
|
), dated AS(
|
||||||
SELECT MIN(DATE(t.dated) - INTERVAL cc.riskScope MONTH) started,
|
SELECT t.companyFk, MIN(DATE(t.dated) - INTERVAL cc.riskScope MONTH) started,
|
||||||
MAX(DATE(t.dated)) ended
|
MAX(DATE(t.dated)) ended
|
||||||
FROM ticket t
|
FROM ticket t
|
||||||
JOIN vn.clientConfig cc
|
JOIN vn.clientConfig cc
|
||||||
|
GROUP BY t.companyFk
|
||||||
), balance AS(
|
), balance AS(
|
||||||
SELECT SUM(amount)amount
|
SELECT SUM(amount)amount, companyFk
|
||||||
FROM (
|
FROM (
|
||||||
SELECT SUM(amount) amount
|
SELECT amount, companyFk
|
||||||
FROM vn.clientRisk
|
FROM vn.clientRisk
|
||||||
WHERE clientFk = vSelf
|
WHERE clientFk = vSelf
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT -(SUM(amount) / 100) amount
|
SELECT -(SUM(amount) / 100) amount, tm.companyFk
|
||||||
FROM hedera.tpvTransaction t
|
FROM hedera.tpvTransaction t
|
||||||
|
JOIN hedera.tpvMerchant tm ON t.id = t.merchantFk
|
||||||
WHERE clientFk = vSelf
|
WHERE clientFk = vSelf
|
||||||
AND receiptFk IS NULL
|
AND receiptFk IS NULL
|
||||||
AND status = 'ok'
|
AND status = 'ok'
|
||||||
) sub
|
) sub
|
||||||
|
WHERE companyFk
|
||||||
|
GROUP BY companyFk
|
||||||
), uninvoiced AS(
|
), uninvoiced AS(
|
||||||
SELECT DATE(t.shipped) dated, SUM(t.totalWithVat) amount
|
SELECT t.companyFk, DATE(t.shipped) dated, SUM(IFNULL(t.totalWithVat, 0)) amount
|
||||||
FROM vn.ticket t
|
FROM vn.ticket t
|
||||||
JOIN dated d
|
JOIN dated d
|
||||||
WHERE t.clientFk = vSelf
|
WHERE t.clientFk = vSelf
|
||||||
AND t.refFk IS NULL
|
AND t.refFk IS NULL
|
||||||
AND t.shipped BETWEEN d.started AND d.ended
|
AND t.shipped BETWEEN d.started AND d.ended
|
||||||
GROUP BY DATE(t.shipped)
|
GROUP BY t.companyFk, DATE(t.shipped)
|
||||||
), receipt AS(
|
), receipt AS(
|
||||||
SELECT DATE(payed) dated, SUM(amountPaid) amount
|
SELECT companyFk,DATE(payed) dated, SUM(amountPaid) amount
|
||||||
FROM vn.receipt
|
FROM vn.receipt
|
||||||
WHERE clientFk = vSelf
|
WHERE clientFk = vSelf
|
||||||
AND payed > util.VN_CURDATE()
|
AND payed > util.VN_CURDATE()
|
||||||
GROUP BY DATE(payed)
|
GROUP BY companyFk, DATE(payed)
|
||||||
), risk AS(
|
), risk AS(
|
||||||
SELECT ui.dated,
|
SELECT b.companyFk,
|
||||||
SUM(ui.amount) OVER (ORDER BY ui.dated) +
|
ui.dated,
|
||||||
|
SUM(ui.amount) OVER (PARTITION BY b.companyFk ORDER BY ui.dated ) +
|
||||||
b.amount +
|
b.amount +
|
||||||
SUM(IFNULL(r.amount, 0)) amount
|
SUM(IFNULL(r.amount, 0)) amount
|
||||||
FROM balance b
|
FROM balance b
|
||||||
JOIN uninvoiced ui
|
JOIN uninvoiced ui ON ui.companyFk = b.companyFk
|
||||||
LEFT JOIN receipt r ON r.dated > ui.dated
|
LEFT JOIN receipt r ON r.dated > ui.dated AND r.companyFk = ui.companyFk
|
||||||
GROUP BY ui.dated
|
GROUP BY b.companyFk, ui.dated
|
||||||
)
|
)
|
||||||
SELECT ti.ticketFk, r.amount
|
SELECT ti.ticketFk, r.amount
|
||||||
FROM ticket ti
|
FROM ticket ti
|
||||||
JOIN risk r ON r.dated = ti.dated;
|
JOIN risk r ON r.dated = ti.dated AND r.companyFk = ti.companyFk;
|
||||||
|
|
||||||
UPDATE ticket t
|
UPDATE ticket t
|
||||||
JOIN tTicketRisk tr ON tr.ticketFk = t.id
|
JOIN tTicketRisk tr ON tr.ticketFk = t.id
|
||||||
|
|
Loading…
Reference in New Issue