Add dailyreport.sql

This commit is contained in:
gitea 2025-12-27 06:29:45 +00:00
commit 69e506ddaf
1 changed files with 21 additions and 0 deletions

21
dailyreport.sql Normal file
View File

@ -0,0 +1,21 @@
SELECT
date_trunc('day', po.create_date) AS giorno,
COUNT(po.id) AS scontrini,
ROUND(AVG(po.amount_total), 2) AS scontrino_medio,
ROUND(SUM(po.amount_total), 2) AS fatturato,
ROUND(SUM(COALESCE(po.amount_tax, 0)), 2) AS tasse,
ROUND(SUM(pol_tot.costo_reale), 2) AS costi,
ROUND(SUM(po.amount_total) - SUM(pol_tot.costo_reale) - SUM(COALESCE(po.amount_tax, 0)), 2) AS margine,
ROUND(SUM(CASE WHEN po.state = 'cancel' THEN po.amount_total ELSE 0 END), 2) AS rimborsi
FROM pos_order po
LEFT JOIN (
SELECT
order_id,
SUM(pol.qty * COALESCE((pp.standard_price->'1')::numeric, 0)) AS costo_reale
FROM pos_order_line pol
LEFT JOIN product_product pp ON pol.product_id = pp.id
GROUP BY order_id
) pol_tot ON po.id = pol_tot.order_id
WHERE po.state IN ('done')
GROUP BY 1
ORDER BY 1 DESC;