56 lines
2.2 KiB
SQL
56 lines
2.2 KiB
SQL
-- ============================================================================
|
|
-- Summe je Abgangsland
|
|
-- Odoo 17 / PostgreSQL
|
|
--
|
|
-- Modell : brandize.datacenter.amazon.vat.transaction
|
|
-- Tabelle: brandize_datacenter_amazon_vat_transaction
|
|
--
|
|
-- Summiert den Bruttoumsatz (inkl. USt) je Bereich und Abgangsland
|
|
-- (SALE_DEPART_COUNTRY) und haengt eine Gesamt-Summe an.
|
|
--
|
|
-- Parameter:
|
|
-- %(month)s Abrechnungszeitraum als Text, z. B. '2026-SEP'
|
|
--
|
|
-- Aufruf (Odoo / psycopg2):
|
|
-- cr.execute(open('Summe_je_Abgangsland.sql').read(), {'month': '2026-SEP'})
|
|
--
|
|
-- In psql stattdessen:
|
|
-- \set month '2026-SEP'
|
|
-- ... AND activity_period = :'month'
|
|
--
|
|
-- Hinweis: Die Spaltennamen entsprechen den Odoo-Feldnamen (Kleinschreibung
|
|
-- der CSV-Header). Falls ein Feld in der DB anders heisst, hier anpassen.
|
|
-- ============================================================================
|
|
|
|
WITH base AS (
|
|
SELECT
|
|
-- Bereich: EU-Marktplaetze werden zusammengefasst, alle uebrigen
|
|
-- Marktplaetze (z. B. amazon.co.uk) erscheinen unter ihrem eigenen Namen.
|
|
CASE
|
|
WHEN marketplace IN (
|
|
'amazon.de', 'amazon.fr', 'amazon.it', 'amazon.es',
|
|
'amazon.nl', 'amazon.pl', 'amazon.se', 'amazon.cz',
|
|
'amazon.com.be', 'amazon.be'
|
|
) THEN 'EU-Marktplätze'
|
|
ELSE marketplace
|
|
END AS bereich,
|
|
sale_depart_country AS abgangsland,
|
|
total_activity_value_amt_vat_incl AS brutto
|
|
FROM brandize_datacenter_amazon_vat_transaction
|
|
WHERE sale_depart_country IS NOT NULL
|
|
AND sale_depart_country <> ''
|
|
AND activity_period = %(month)s
|
|
)
|
|
SELECT
|
|
CASE WHEN GROUPING(bereich) = 1 THEN 'Gesamt' ELSE bereich END AS "Bereich",
|
|
CASE WHEN GROUPING(bereich) = 1 THEN '' ELSE abgangsland END AS "Abgangsland",
|
|
-- Deutsche Zahlformatierung: 90717.93 -> "90.717,93 €"
|
|
translate(to_char(SUM(brutto), 'FM999,999,999,999.99'), ',.', '.,') || ' €'
|
|
AS "Brutto (inkl. USt)"
|
|
FROM base
|
|
GROUP BY GROUPING SETS ((bereich, abgangsland), ())
|
|
ORDER BY
|
|
GROUPING(bereich), -- Gesamt-Zeile zuletzt
|
|
CASE bereich WHEN 'EU-Marktplätze' THEN 0 ELSE 1 END, -- EU zuerst
|
|
abgangsland;
|