
CREATE VIEW v_plz AS
SELECT plz, p.dest AS bestimmungsort, g.name AS gemeinde, g.gkz, o.name AS ort, o.okz
FROM AT_gem g
    JOIN AT_ort o ON o.gkz = g.gkz
    JOIN AT_plz_dest p ON p.okz = o.okz;

CREATE VIEW v_member AS
SELECT m.mgnr, m.name,
       (COALESCE(m.prefix || ' ', '') || m.given_name || COALESCE(' ' || m.middle_names, '') || COALESCE(' ' || m.suffix, '')) AS other_names,
       m.address, p.plz, o.name AS locality,
       a.name AS billing_name, a.address AS billing_address, p2.plz AS billing_plz, o2.name AS billing_locality,
       k.name AS default_kg,
       GROUP_CONCAT(e.address, ', ') AS email_addresses
FROM member m
    LEFT JOIN AT_plz_dest p ON p.id = m.postal_dest
    LEFT JOIN AT_ort o ON o.okz = p.okz
    LEFT JOIN member_billing_address a ON a.mgnr = m.mgnr
    LEFT JOIN AT_plz_dest p2 ON p2.id = a.postal_dest
    LEFT JOIN AT_ort o2 ON o2.okz = p2.okz
    LEFT JOIN AT_kg k ON k.kgnr = m.default_kgnr
    LEFT JOIN member_email_address e ON e.mgnr = m.mgnr
GROUP BY m.mgnr
ORDER BY m.mgnr;

CREATE VIEW v_virtual_season AS
SELECT year, max_kg_per_ha
FROM season
UNION
SELECT strftime('%Y', date()) + 0, (SELECT max_kg_per_ha FROM season ORDER BY year DESC LIMIT 1);

CREATE VIEW v_wine_attribute AS
SELECT a.attrid, name, active, max_kg_per_ha, strict, fill_lower,
       COUNT(t.attrid) > 0 AS area_com
FROM wine_attribute a
    LEFT JOIN area_commitment_type t ON t.attrid = a.attrid
GROUP BY a.attrid;

CREATE VIEW v_delivery AS
SELECT p.year, p.did, p.dpnr,
       d.date, d.time, d.zwstid, d.lnr, d.lsnr,
       m.mgnr, m.name, m.given_name,
       v.sortid, v.type, a.attrid, p.cultid,
       p.weight, p.kmw, ROUND(p.kmw * (4.54 + 0.022 * p.kmw), 0) AS oe, p.qualid,
       p.hkid, p.kgnr, p.rdnr,
       p.net_weight, p.gebunden,
       p.qualid IN (SELECT l.qualid FROM wine_quality_level l WHERE NOT l.predicate AND (p.kmw >= l.min_kmw OR l.min_kmw IS NULL) ORDER BY l.min_kmw DESC LIMIT 1,100) AS abgewertet,
       p.qualid NOT IN ('WEI', 'RSW', 'LDW') AS min_quw,
       IIF(a.strict, COALESCE(a.fill_lower, 0), 0) AS attribute_prio,
       GROUP_CONCAT(o.modid) AS modifiers,
       d.comment, p.comment AS part_comment
FROM delivery_part p
    JOIN delivery d ON (d.year, d.did) = (p.year, p.did)
    JOIN member m ON m.mgnr = d.mgnr
    LEFT JOIN wine_variety v ON v.sortid = p.sortid
    LEFT JOIN wine_attribute a ON a.attrid = p.attrid
    LEFT JOIN delivery_part_modifier o ON (o.year, o.did, o.dpnr) = (p.year, p.did, p.dpnr)
GROUP BY p.year, p.did, p.dpnr
ORDER BY p.year, p.did, p.dpnr, o.modid;

CREATE VIEW v_stat_season AS
SELECT year,
       SUM(weight) AS weight_total,
       SUM(IIF(type = 'R', weight, 0)) AS weight_red,
       SUM(IIF(type = 'W', weight, 0)) AS weight_white,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year
ORDER BY year;

CREATE VIEW v_stat_variety AS
SELECT year, sortid,
       SUM(weight) AS weight_total,
       SUM(IIF(type = 'R', weight, 0)) AS weight_red,
       SUM(IIF(type = 'W', weight, 0)) AS weight_white,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year, sortid
ORDER BY year, sortid;

CREATE VIEW v_stat_attr AS
SELECT year, attrid,
       SUM(weight) AS weight_total,
       SUM(IIF(type = 'R', weight, 0)) AS weight_red,
       SUM(IIF(type = 'W', weight, 0)) AS weight_white,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year, attrid
ORDER BY year, attrid;

CREATE VIEW v_stat_variety_attr AS
SELECT year, sortid, attrid,
       SUM(weight) AS weight_total,
       SUM(IIF(type = 'R', weight, 0)) AS weight_red,
       SUM(IIF(type = 'W', weight, 0)) AS weight_white,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year, sortid, attrid
ORDER BY year, sortid, attrid;

CREATE VIEW v_stat_member AS
SELECT year, mgnr,
       SUM(weight) AS weight_total,
       SUM(IIF(type = 'R', weight, 0)) AS weight_red,
       SUM(IIF(type = 'W', weight, 0)) AS weight_white,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen
FROM v_delivery
GROUP BY year, mgnr
ORDER BY year, mgnr;

CREATE VIEW v_bki_member AS
SELECT s.year, m.mgnr, m.lfbis_nr, m.name,
       (COALESCE(m.prefix || ' ', '') || m.given_name || COALESCE(' ' || m.middle_names, '') || COALESCE(' ' || m.suffix, '')) AS other_names,
       a.name AS billing_name, COALESCE(a.address, m.address) AS address,
       COALESCE(a.country, m.country) AS country, COALESCE(a.postal_dest, m.postal_dest) AS postal_dest,
       m.volllieferant AS vollablieferer,
       SUM(COALESCE(IIF((c.year_from IS NULL OR c.year_from <= s.year) AND (c.year_to IS NULL OR c.year_to >= s.year), c.area, NULL), 0)) AS area
FROM season s, member m
    LEFT JOIN member_billing_address a ON a.mgnr = m.mgnr
    LEFT JOIN area_commitment c ON c.mgnr = m.mgnr
GROUP BY s.year, m.mgnr;

CREATE VIEW v_bki_delivery AS
SELECT m.lfbis_nr, m.name, m.other_names, m.billing_name,
       m.address, plz.plz, IIF(INSTR(o.name, ',') = 0, o.name, SUBSTR(o.name, 1, INSTR(o.name, ',') - 1)) AS ort,
       d.date, d.weight, v.type, v.sortid, d.qualid, d.year, d.hkid, d.kmw, d.oe,
       m.vollablieferer, m.area
FROM v_delivery d
    JOIN v_bki_member m ON (m.year, m.mgnr) = (d.year, d.mgnr)
    JOIN postal_dest pd ON (pd.country, pd.id) = (m.country, m.postal_dest)
    LEFT JOIN AT_plz_dest ap ON (ap.country, ap.id) = (pd.country, pd.id)
    LEFT JOIN AT_plz plz ON plz.plz = ap.plz
    LEFT JOIN AT_ort o ON o.okz = ap.okz
    JOIN wine_variety v ON v.sortid = d.sortid
ORDER BY d.date, d.time;

CREATE VIEW v_member_history AS
SELECT m.mgnr, s.year,
       m.shares + SUM(IIF(h1.from_type = 1, h1.shares, 0)) - SUM(IIF(h2.to_type = 1, h2.shares, 0)) AS shares,
       m.shares_red + SUM(IIF(h1.from_type = 2, h1.shares, 0)) - SUM(IIF(h2.to_type = 2, h2.shares, 0)) AS shares_red,
       m.shares_white + SUM(IIF(h1.from_type = 3, h1.shares, 0)) - SUM(IIF(h2.to_type = 3, h2.shares, 0)) AS shares_white,
       m.shares_dormant + SUM(IIF(h1.from_type = 9, h1.shares, 0)) - SUM(IIF(h2.to_type = 9, h2.shares, 0)) AS shares_dormant
FROM member m, v_virtual_season s
    LEFT JOIN member_history h1 ON (SUBSTR(h1.date_notice, 1, 4) + 0) > s.year AND h1.from_mgnr = m.mgnr
    LEFT JOIN member_history h2 ON (SUBSTR(h2.date_notice, 1, 4) + 0) > s.year AND h2.to_mgnr = m.mgnr
GROUP BY m.mgnr, s.year
ORDER BY m.mgnr, s.year;

CREATE VIEW v_total_under_delivery AS
SELECT s.year, m.mgnr,
       h.shares,
       h.shares * COALESCE(s.min_kg_per_share, 0) AS min_kg,
       h.shares * COALESCE(s.max_kg_per_share, 0) AS max_kg,
       COALESCE(d.weight_total, 0) AS weight_total,
       IIF(COALESCE(d.weight_total, 0) < h.shares * COALESCE(s.min_kg_per_share, 0),
           COALESCE(d.weight_total, 0) - h.shares * COALESCE(s.min_kg_per_share, 0),
           IIF(COALESCE(d.weight_total, 0) > h.shares * COALESCE(s.max_kg_per_share, 0),
               COALESCE(d.weight_total, 0) - h.shares * COALESCE(s.max_kg_per_share, 0),
               0)) AS diff,
       h.shares_red,
       h.shares_red * COALESCE(s.min_kg_per_share_red, s.min_kg_per_share, 0) AS min_kg_red,
       h.shares_red * COALESCE(s.max_kg_per_share_red, s.max_kg_per_share, 0) AS max_kg_red,
       COALESCE(d.weight_red, 0) AS weight_red,
       IIF(COALESCE(d.weight_red, 0) < h.shares_red * COALESCE(s.min_kg_per_share_red, s.min_kg_per_share, 0),
           COALESCE(d.weight_red, 0) - h.shares_red * COALESCE(s.min_kg_per_share_red, s.min_kg_per_share, 0),
           IIF(COALESCE(d.weight_red, 0) > h.shares_red * COALESCE(s.max_kg_per_share_red, s.max_kg_per_share, 0),
               COALESCE(d.weight_red, 0) - h.shares_red * COALESCE(s.max_kg_per_share_red, s.max_kg_per_share, 0),
               0)) AS diff_red,
       h.shares_white,
       h.shares_white * COALESCE(s.min_kg_per_share_white, s.min_kg_per_share, 0) AS min_kg_white,
       h.shares_white * COALESCE(s.max_kg_per_share_white, s.max_kg_per_share, 0) AS max_kg_white,
       COALESCE(d.weight_white, 0) AS weight_white,
       IIF(COALESCE(d.weight_white, 0) < h.shares_white * COALESCE(s.min_kg_per_share_white, s.min_kg_per_share, 0),
           COALESCE(d.weight_white, 0) - h.shares_white * COALESCE(s.min_kg_per_share_white, s.min_kg_per_share, 0),
           IIF(COALESCE(d.weight_white, 0) > h.shares_white * COALESCE(s.max_kg_per_share_white, s.max_kg_per_share, 0),
               COALESCE(d.weight_white, 0) - h.shares_white * COALESCE(s.max_kg_per_share_white, s.max_kg_per_share, 0),
               0)) AS diff_white
FROM member m, season s
    LEFT JOIN v_member_history h ON (h.year, h.mgnr) = (s.year, m.mgnr)
    LEFT JOIN v_stat_member d ON (d.year, d.mgnr) = (s.year, m.mgnr)
ORDER BY s.year, m.mgnr;

CREATE VIEW v_stat_total AS
SELECT d.year, d.zwstid, v.type, v.sortid,
       IIF(b.discr = a.attrid OR NOT a.area_com, a.attrid, NULL) AS attrid,
       p.cultid, q.qualid,
       IIF(b.discr = '_', 'ungeb', 'geb') AS geb,
       SUM(value) AS weight
FROM delivery_part p
    LEFT JOIN delivery d ON (d.year, d.did) = (p.year, p.did)
    LEFT JOIN wine_variety v ON v.sortid = p.sortid
    LEFT JOIN wine_quality_level q ON q.qualid = p.qualid
    LEFT JOIN delivery_part_bucket b ON (b.year, b.did, b.dpnr) = (p.year, p.did, p.dpnr)
    LEFT JOIN v_wine_attribute a ON a.attrid = p.attrid
GROUP BY d.year, d.zwstid, v.type, v.sortid, IIF(b.discr = a.attrid OR NOT a.area_com, a.attrid, NULL), p.cultid, q.qualid, geb
HAVING SUM(value) > 0
ORDER BY d.year, d.zwstid, v.type DESC, v.sortid, IIF(b.discr = a.attrid OR NOT a.area_com, a.attrid, NULL), p.cultid, q.min_kmw, geb;

CREATE VIEW v_stat_modifier AS
SELECT v.year, v.avnr, m.modid, m.name, m.abs, m.rel,
       COUNT(*) AS count,
       MIN(IIF(p.net_amount = 0 AND m.abs IS NULL, NULL, ROUND(COALESCE(d.weight * m.abs, 0) + COALESCE(p.net_amount * m.rel, 0)))) AS min,
       MAX(IIF(p.net_amount = 0 AND m.abs IS NULL, NULL, ROUND(COALESCE(d.weight * m.abs, 0) + COALESCE(p.net_amount * m.rel, 0)))) AS max,
       SUM(ROUND(COALESCE(d.weight * m.abs, 0) + COALESCE(p.net_amount * m.rel, 0))) AS sum
FROM payment_variant v
    JOIN modifier m ON m.year = v.year
    JOIN delivery_part d ON d.year = v.year
    JOIN delivery_part_modifier x ON (x.year, x.did, x.dpnr, x.modid) = (d.year, d.did, d.dpnr, m.modid)
    LEFT JOIN payment_delivery_part p ON (p.year, p.did, p.dpnr, p.avnr) = (d.year, d.did, d.dpnr, v.avnr)
GROUP BY v.year, v.avnr, m.modid
ORDER BY v.year, v.avnr, m.ordering;
