This commit is contained in:
oliver committed 2026-10-09 15:01:40 -03:00
commit 1ea3de74f2
10 files changed
+1825

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
-- ============================================================================
-- Loeschen aller Datensaetze eines Abrechnungszeitraums
-- Odoo 17 / PostgreSQL
--
-- Modell : brandize.datacenter.amazon.vat.transaction
-- Tabelle: brandize_datacenter_amazon_vat_transaction
--
-- Parameter:
-- %(month)s Abrechnungszeitraum als Text, z. B. '2026-SEP'
--
-- Aufruf (Odoo / psycopg2):
-- cr.execute(open('Delete_Activity_Period.sql').read(), {'month': '2026-SEP'})
--
-- In psql stattdessen:
-- \set month '2026-SEP'
-- ... WHERE activity_period = :'month'
--
-- ACHTUNG: Loescht unwiderruflich. Vorher die Anzahl pruefen (siehe unten).
-- ============================================================================
-- Kontrolle vor dem Loeschen (betroffene Datensaetze zaehlen):
-- SELECT count(*) FROM brandize_datacenter_amazon_vat_transaction
-- WHERE activity_period = %(month)s;
DELETE FROM brandize_datacenter_amazon_vat_transaction
WHERE activity_period = %(month)s;
-- ----------------------------------------------------------------------------
-- In psql empfiehlt sich eine Transaktionsklammer, damit die Loeschung erst
-- nach Sichtkontrolle bestaetigt wird:
--
-- BEGIN;
-- DELETE FROM brandize_datacenter_amazon_vat_transaction
-- WHERE activity_period = :'month';
-- -- Ergebnis pruefen, dann:
-- COMMIT; -- bestaetigen
-- -- ROLLBACK; -- verwerfen
--
-- In Odoo NICHT verwenden: dort umschliesst Odoo die Transaktion selbst.
-- ----------------------------------------------------------------------------
+55
View File
@@ -0,0 +1,55 @@
-- ============================================================================
-- 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;
+105
View File
@@ -0,0 +1,105 @@
-- ============================================================================
-- VAT Summary (Kontrolle Umsatzsteuer-Voranmeldung)
-- Odoo 17 / PostgreSQL
--
-- Modell : brandize.datacenter.amazon.vat.transaction
-- Tabelle: brandize_datacenter_amazon_vat_transaction
--
-- Rechnungsdatum von / bis : 01.09.2026 / 30.09.2026
--
-- Parameter:
-- %(month)s Abrechnungszeitraum als Text, z. B. '2026-SEP'
--
-- Aufruf (Odoo / psycopg2):
-- cr.execute(open('VAT_Summary.sql').read(), {'month': '2026-SEP'})
--
-- In psql:
-- \set month '2026-SEP'
-- ... WHERE activity_period = :'month'
--
-- Excel-Namen -> SQL-Spalten:
-- QUANTITY -> qty
-- GROSS_INVOICE_AMOUNT -> total_activity_value_amt_vat_incl
-- TOTAL_VAT -> total_activity_value_vat_amt
-- IN_PERIOD -> activity_period = %(month)s
-- SALES_CHANNEL -> sales_channel (<> 'AMAZON_FEE')
-- SALE_ARRIVAL_COUNTRY -> sale_arrival_country
-- SALE_DEPART_COUNTRY -> sale_depart_country
-- BUYER_VAT_NR_COUNTRY -> buyer_vat_number_country
-- ZERO_HAS_B2B_VAT_NR -> buyer_vat_number <> ''
--
-- Hinweis: Die "bereinigten" Hilfsspalten (CU-CX) sind nur ein Excel-Workaround
-- fuer als Text/Datum importierte Zellen. In der DB sind die numerischen
-- Spalten bereits korrekt typisiert, daher werden hier die Rohspalten genutzt.
-- ============================================================================
WITH base AS (
SELECT
sale_arrival_country,
sale_depart_country,
buyer_vat_number_country,
buyer_vat_number,
qty,
total_activity_value_amt_vat_incl AS gross,
total_activity_value_vat_amt AS vat
FROM brandize_datacenter_amazon_vat_transaction
WHERE activity_period = %(month)s
AND sales_channel <> 'AMAZON_FEE'
),
arrival AS (
SELECT
sale_arrival_country AS country_code,
SUM(qty) AS quantity,
SUM(gross) AS gross_total,
-- Non-OSS: Abgangs- und Ankunftsland identisch (Inlandslieferung)
SUM(gross) FILTER (WHERE sale_depart_country = sale_arrival_country) AS gross_non_oss,
SUM(vat) FILTER (WHERE sale_depart_country = sale_arrival_country) AS vat_non_oss,
-- Excl. B2B: nur Umsaetze mit USt (TOTAL_VAT <> 0)
SUM(gross) FILTER (WHERE vat <> 0) AS gross_excl_b2b
FROM base
WHERE sale_arrival_country IS NOT NULL
AND sale_arrival_country <> ''
GROUP BY sale_arrival_country
),
intra AS (
-- Innergemeinschaftliche Lieferung: B2B mit USt-Nr. und 0 %% USt
SELECT
buyer_vat_number_country AS country_code,
SUM(gross) AS intra_net
FROM base
WHERE buyer_vat_number_country IS NOT NULL
AND buyer_vat_number_country <> ''
AND buyer_vat_number IS NOT NULL
AND buyer_vat_number <> ''
AND vat = 0
GROUP BY buyer_vat_number_country
)
SELECT
a.country_code AS "Country Code",
COALESCE(c.name->>'de_DE', c.name->>'en_US', a.country_code) AS "Land",
to_char(a.quantity, 'FM999,999,999') AS "Quantity",
translate(to_char(a.gross_total, 'FM999,999,999,999.99'), ',.', '.,') || ' €' AS "Gross Sales (Total)",
translate(to_char(a.gross_non_oss, 'FM999,999,999,999.99'), ',.', '.,') || ' €' AS "Gross Sales (Non-OSS)",
translate(to_char(a.vat_non_oss, 'FM999,999,999,999.99'), ',.', '.,') || ' €' AS "VAT to Remit (Non-OSS)",
translate(to_char(a.gross_excl_b2b, 'FM999,999,999,999.99'), ',.', '.,') || ' €' AS "Gross Sales (Excl. B2B)",
translate(to_char(COALESCE(i.intra_net, 0), 'FM999,999,999,999.99'), ',.', '.,') || ' €'
AS "Intra-community supply – Net sales"
FROM arrival a
LEFT JOIN intra i ON i.country_code = a.country_code
LEFT JOIN res_country c ON c.code = a.country_code
ORDER BY a.country_code;
-- ----------------------------------------------------------------------------
-- Import-Kontrolle (entspricht der LET-Pruefung im Sheet):
-- zaehlt Datensaetze, deren Betragsspalten nicht als Zahl vorliegen.
--
-- SELECT count(*) AS nicht_numerische_zellen
-- FROM brandize_datacenter_amazon_vat_transaction
-- WHERE activity_period = %(month)s
-- AND (total_activity_value_amt_vat_incl IS NULL
-- OR total_activity_value_vat_amt IS NULL);
--
-- Hinweis: Die Zeilenreihenfolge im Sheet ist die Erstauftretens-Reihenfolge
-- (UNIQUE(FILTER(...))). Falls gewuenscht, statt "ORDER BY a.country_code"
-- ein "MIN(id) AS first_seen" in der arrival-CTE ergaenzen und danach sortieren.
-- ----------------------------------------------------------------------------