This commit is contained in:
seb
2026-10-19 02:28:15 +02:00
parent cce966d021
commit 5259e3bef9
6 changed files with 105 additions and 6 deletions

View File

@@ -574,7 +574,30 @@ SELECT
asup.sort,
asup.net AS [price.net],
asup.tax_id AS [price.taxId],
t.rate AS [price.taxRate]
t.rate AS [price.taxRate],
(
SELECT
m.[date] AS [date],
SUM(ma.amount - COALESCE(ma_ledger.booked_amount, 0)) AS amount
FROM manifest m
INNER JOIN manifestarticle ma ON ma.manifest_id = m.id
LEFT JOIN (
SELECT manifestarticle_id, SUM(amount) AS booked_amount
FROM ledger
WHERE warehouse_id IS NULL
GROUP BY manifestarticle_id
) ma_ledger ON ma_ledger.manifestarticle_id = ma.id
WHERE m.company_id = a.company_id
AND m.[type] = 'delivery'
AND m.entity_id = asup.entity_id
AND asup.sku IS NOT NULL
AND asup.sku <> ''
AND ma.sku = asup.sku
AND (ma.amount - COALESCE(ma_ledger.booked_amount, 0)) > 0
GROUP BY m.id, m.[date]
ORDER BY m.[date] ASC, m.id ASC
FOR JSON PATH
) AS incomingJson
FROM articlesupplier asup
INNER JOIN entity e ON e.id = asup.entity_id
LEFT JOIN tax t ON t.id = asup.tax_id

View File

@@ -207,11 +207,40 @@ SELECT auth.*,
'stock', stock, 'sort', sort,
'price', CASE WHEN net IS NULL THEN NULL ELSE json_object(
'net', net, 'taxId', tax_id, 'taxRate', tax_rate
) END
) END,
'incoming', json(incoming_json)
)), '[]') FROM (
SELECT s.id, s.article_id, s.entity_id, e.name AS supplier_name,
s.sku, s.pu, s.stock, s.sort,
s.net, s.tax_id, t.rate AS tax_rate
s.net, s.tax_id, t.rate AS tax_rate,
COALESCE((
SELECT json_group_array(json_object(
'date', inc.date,
'amount', inc.amount
))
FROM (
SELECT
m.date AS date,
SUM(ma.amount - COALESCE(ma_ledger.booked_amount, 0)) AS amount
FROM manifest m
INNER JOIN manifestarticle ma ON ma.manifest_id = m.id
LEFT JOIN (
SELECT manifestarticle_id, SUM(amount) AS booked_amount
FROM ledger
WHERE warehouse_id IS NULL
GROUP BY manifestarticle_id
) ma_ledger ON ma_ledger.manifestarticle_id = ma.id
WHERE m.company_id = a.company_id
AND m.type = 'delivery'
AND m.entity_id = s.entity_id
AND s.sku IS NOT NULL
AND s.sku <> ''
AND ma.sku = s.sku
AND (ma.amount - COALESCE(ma_ledger.booked_amount, 0)) > 0
GROUP BY m.id, m.date
ORDER BY m.date ASC, m.id ASC
) inc
), '[]') AS incoming_json
FROM articlesupplier s INNER JOIN entity e ON e.id = s.entity_id
LEFT JOIN tax t ON t.id = s.tax_id
WHERE s.article_id = a.id ORDER BY s.sort, s.id