Do not use bio as attribute
This commit is contained in:
		@@ -61,7 +61,7 @@ CREATE TABLE area_commitment_type (
 | 
			
		||||
) STRICT;
 | 
			
		||||
 | 
			
		||||
CREATE TABLE wine_cultivation (
 | 
			
		||||
    cultid      TEXT NOT NULL CHECK (cultid REGEXP '^[A-Z]+$'),
 | 
			
		||||
    cultid      TEXT NOT NULL CHECK (cultid REGEXP '^[A-Z][A-Z0-9]*$'),
 | 
			
		||||
    name        TEXT NOT NULL,
 | 
			
		||||
    description TEXT DEFAULT NULL,
 | 
			
		||||
 | 
			
		||||
@@ -175,7 +175,7 @@ CREATE TABLE area_commitment (
 | 
			
		||||
    mgnr      INTEGER NOT NULL,
 | 
			
		||||
 | 
			
		||||
    vtrgid    TEXT    NOT NULL,
 | 
			
		||||
    cultid    TEXT    NOT NULL,
 | 
			
		||||
    cultid    TEXT             DEFAULT NULL,
 | 
			
		||||
    area      INTEGER NOT NULL,
 | 
			
		||||
 | 
			
		||||
    kgnr      INTEGER NOT NULL,
 | 
			
		||||
 
 | 
			
		||||
@@ -89,7 +89,8 @@ CREATE TABLE delivery_part (
 | 
			
		||||
    dpnr            INTEGER NOT NULL,
 | 
			
		||||
 | 
			
		||||
    sortid          TEXT    NOT NULL,
 | 
			
		||||
    attrid          TEXT,
 | 
			
		||||
    attrid          TEXT    DEFAULT NULL,
 | 
			
		||||
    cultid          TEXT    DEFAULT NULL,
 | 
			
		||||
    weight          INTEGER NOT NULL,
 | 
			
		||||
    kmw             REAL    NOT NULL,
 | 
			
		||||
    qualid          TEXT    NOT NULL,
 | 
			
		||||
@@ -98,7 +99,7 @@ CREATE TABLE delivery_part (
 | 
			
		||||
    kgnr            INTEGER DEFAULT NULL,
 | 
			
		||||
    rdnr            INTEGER DEFAULT NULL,
 | 
			
		||||
 | 
			
		||||
    gerebelt        INTEGER NOT NULL CHECK (gerebelt        IN (TRUE, FALSE)),
 | 
			
		||||
    net_weight      INTEGER NOT NULL CHECK (net_weight      IN (TRUE, FALSE)),
 | 
			
		||||
    manual_weighing INTEGER NOT NULL CHECK (manual_weighing IN (TRUE, FALSE)),
 | 
			
		||||
    spl_check       INTEGER NOT NULL CHECK (spl_check       IN (TRUE, FALSE)) DEFAULT FALSE,
 | 
			
		||||
 | 
			
		||||
@@ -127,6 +128,9 @@ CREATE TABLE delivery_part (
 | 
			
		||||
    CONSTRAINT fk_delivery_part_wine_attribute FOREIGN KEY (attrid) REFERENCES wine_attribute (attrid)
 | 
			
		||||
        ON UPDATE CASCADE
 | 
			
		||||
        ON DELETE RESTRICT,
 | 
			
		||||
    CONSTRAINT fk_delivery_part_wine_cultivation FOREIGN KEY (cultid) REFERENCES wine_cultivation (cultid)
 | 
			
		||||
        ON UPDATE CASCADE
 | 
			
		||||
        ON DELETE RESTRICT,
 | 
			
		||||
    CONSTRAINT fk_delivery_part_wine_quality_level FOREIGN KEY (qualid) REFERENCES wine_quality_level (qualid)
 | 
			
		||||
        ON UPDATE CASCADE
 | 
			
		||||
        ON DELETE RESTRICT,
 | 
			
		||||
 
 | 
			
		||||
@@ -11,14 +11,21 @@ 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.family_name, m.given_name,
 | 
			
		||||
       p.sortid, a.attrid,
 | 
			
		||||
       p.sortid, 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.gerebelt, p.gebunden,
 | 
			
		||||
       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,
 | 
			
		||||
 
 | 
			
		||||
@@ -33,24 +33,25 @@ ORDER BY year, mgnr, bucket;
 | 
			
		||||
 | 
			
		||||
CREATE VIEW v_delivery_bucket_strict AS
 | 
			
		||||
SELECT year, mgnr,
 | 
			
		||||
       sortid || IIF(min_quw, COALESCE(attrid, ''), '_') AS bucket,
 | 
			
		||||
       sortid, IIF(min_quw, attrid, NULL) AS attrid,
 | 
			
		||||
       sortid || IIF(min_quw OR NOT COALESCE(area_com, TRUE), COALESCE(a.attrid, ''), '_') AS bucket,
 | 
			
		||||
       sortid, IIF(min_quw OR NOT COALESCE(area_com, TRUE), a.attrid, NULL) AS attrid,
 | 
			
		||||
       SUM(weight) AS weight,
 | 
			
		||||
       min_quw
 | 
			
		||||
FROM v_delivery
 | 
			
		||||
       min_quw OR NOT COALESCE(area_com, TRUE) AS valid
 | 
			
		||||
FROM v_delivery d
 | 
			
		||||
    LEFT JOIN v_wine_attribute a ON a.attrid = d.attrid
 | 
			
		||||
GROUP BY year, mgnr, bucket
 | 
			
		||||
ORDER BY year, mgnr, bucket;
 | 
			
		||||
 | 
			
		||||
CREATE VIEW v_delivery_bucket AS
 | 
			
		||||
SELECT year, mgnr, bucket, weight
 | 
			
		||||
FROM v_delivery_bucket_strict
 | 
			
		||||
WHERE attrid IS NOT NULL OR NOT min_quw
 | 
			
		||||
WHERE attrid IS NOT NULL OR NOT valid
 | 
			
		||||
UNION ALL
 | 
			
		||||
SELECT b.year, b.mgnr, b.sortid,
 | 
			
		||||
       SUM(b.weight) AS weight
 | 
			
		||||
FROM v_delivery_bucket_strict b
 | 
			
		||||
    LEFT JOIN wine_attribute a ON a.attrid = b.attrid
 | 
			
		||||
WHERE min_quw AND (a.strict IS NULL OR a.strict = FALSE)
 | 
			
		||||
WHERE valid AND (a.strict IS NULL OR a.strict = FALSE)
 | 
			
		||||
GROUP BY b.year, b.mgnr, b.sortid
 | 
			
		||||
ORDER BY year, mgnr, bucket;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,3 @@
 | 
			
		||||
 | 
			
		||||
-- This value MUST NOT be changed while other connections are open!
 | 
			
		||||
PRAGMA schema_version = 1700;
 | 
			
		||||
PRAGMA schema_version = 1800;
 | 
			
		||||
 
 | 
			
		||||
@@ -756,6 +756,8 @@ def migrate_attributes(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
            if attrid == 'BIO':
 | 
			
		||||
                attrid = 'B'
 | 
			
		||||
            ATTRIBUTE_MAP[a['SANR']] = attrid
 | 
			
		||||
            if attrid == 'B':
 | 
			
		||||
                continue
 | 
			
		||||
            f.row(attrid, a['Attribut'], True, max_kg, False, 0)
 | 
			
		||||
        if CLIENT == WG.MATZEN:
 | 
			
		||||
            f.row('M', 'Matzen', False, None, False, 0)
 | 
			
		||||
@@ -783,9 +785,14 @@ def migrate_cultivations(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
            cultid = name[0].upper()
 | 
			
		||||
            if name.isupper():
 | 
			
		||||
                cultid = name
 | 
			
		||||
            elif name == 'Normal':
 | 
			
		||||
                cultid = None
 | 
			
		||||
            elif 'biolog' in name.lower():
 | 
			
		||||
                cultid = 'BIO'
 | 
			
		||||
                cultid = 'B'
 | 
			
		||||
                name = 'Bio'
 | 
			
		||||
            CULTIVATION_MAP[c['BANR']] = cultid
 | 
			
		||||
            if cultid is None:
 | 
			
		||||
                continue
 | 
			
		||||
            f.row(cultid, name, None)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -803,14 +810,8 @@ def migrate_area_commitment_types(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
            attrid = ATTRIBUTE_MAP[t['SANR']] if t['SANR'] else None
 | 
			
		||||
            f.row(sortid + (attrid or ''), sortid[:2], attrid or sortid[2:] or None, None, menge,
 | 
			
		||||
                  None, None, None)
 | 
			
		||||
        bio = []
 | 
			
		||||
        if CLIENT == WG.MATZEN:
 | 
			
		||||
            bio = ['GV', 'ZW', 'MT']
 | 
			
		||||
            f.row('BM', 'BM', None, None, None, None, None, None)
 | 
			
		||||
        elif CLIENT == WG.WINZERKELLER:
 | 
			
		||||
            bio = ['GV', 'ZW', 'WR', 'MT', 'RR', 'WB', 'CH', 'MU']
 | 
			
		||||
        for sortid in bio:
 | 
			
		||||
            f.row(f'{sortid}B', sortid, 'B', None, None, None, None, None)
 | 
			
		||||
            f.row('BM', 'BM', None, None, None, None, None, None, None)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def normalize_name(family_name: str, given_name: str) -> Tuple[Optional[str], Optional[str], Optional[str], Optional[str], Optional[str], Optional[str]]:
 | 
			
		||||
@@ -1449,6 +1450,8 @@ def migrate_area_commitments(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
 | 
			
		||||
            to = fb['Bis'] if fb['Bis'] and fb['Bis'] < 3000 else None
 | 
			
		||||
            attrid = ATTRIBUTE_MAP[fb['SANR']] if fb['SANR'] else None
 | 
			
		||||
            if attrid == 'B':
 | 
			
		||||
                attrid = None
 | 
			
		||||
            f_fb.row(fbnr, mgnr, fb['SNR'] + (attrid or ''), CULTIVATION_MAP[fb['BANR'] or 1], area,
 | 
			
		||||
                     kgnr, gstnr, rdnr, fb['Von'], to, comment)
 | 
			
		||||
 | 
			
		||||
@@ -1560,9 +1563,9 @@ def migrate_deliveries(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
    with utils.csv_open(f'{out_dir}/delivery.csv') as f_delivery, \
 | 
			
		||||
            utils.csv_open(f'{out_dir}/delivery_part.csv') as f_part:
 | 
			
		||||
        f_delivery.header('year', 'did', 'date', 'time', 'zwstid', 'lnr', 'lsnr', 'mgnr', 'comment')
 | 
			
		||||
        f_part.header('year', 'did', 'dpnr', 'sortid', 'attrid', 'weight', 'kmw', 'qualid', 'hkid', 'kgnr', 'rdnr',
 | 
			
		||||
                      'gerebelt', 'manual_weighing', 'spl_check', 'hand_picked', 'lesewagen', 'gebunden',
 | 
			
		||||
                      'temperature', 'acid', 'scale_id', 'weighing_id', 'weighing_reason', 'comment')
 | 
			
		||||
        f_part.header('year', 'did', 'dpnr', 'sortid', 'attrid', 'cultid', 'weight', 'kmw', 'qualid',
 | 
			
		||||
                      'hkid', 'kgnr', 'rdnr', 'gerebelt', 'manual_weighing', 'spl_check', 'hand_picked', 'lesewagen',
 | 
			
		||||
                      'gebunden', 'temperature', 'acid', 'scale_id', 'weighing_id', 'weighing_reason', 'comment')
 | 
			
		||||
 | 
			
		||||
        for lsnr, linrs, date in fixed:
 | 
			
		||||
            if date.year not in seasons:
 | 
			
		||||
@@ -1607,7 +1610,10 @@ def migrate_deliveries(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
                kmw = GRADATION_MAP[oe]
 | 
			
		||||
                sortid = d['SNR'].upper()
 | 
			
		||||
                attrid = ATTRIBUTE_MAP[d['SANR']] if d['SANR'] else None
 | 
			
		||||
                if attrid:
 | 
			
		||||
                cultid = None
 | 
			
		||||
                if attrid == 'B':
 | 
			
		||||
                    cultid = 'B'
 | 
			
		||||
                elif attrid:
 | 
			
		||||
                    attributes.add(attrid)
 | 
			
		||||
                if len(sortid) != 2:
 | 
			
		||||
                    attributes.add(sortid[2:])
 | 
			
		||||
@@ -1716,7 +1722,7 @@ def migrate_deliveries(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
                    print("ERROR: ", attributes)
 | 
			
		||||
                attrid = attributes.pop() if len(attributes) == 1 else None
 | 
			
		||||
                f_part.row(
 | 
			
		||||
                    date.year, snr, dpnr, sortid, attrid, int(d['Gewicht']), kmw, qualid, hkid, kgnr, rdnr,
 | 
			
		||||
                    date.year, snr, dpnr, sortid, attrid, cultid, int(d['Gewicht']), kmw, qualid, hkid, kgnr, rdnr,
 | 
			
		||||
                    gerebelt, handwiegung, d['Spaetlese-Ueberpruefung'] or False,
 | 
			
		||||
                    hand, lesewagen, gebunden, d['Temperatur'], acid, scale_id, weighing_id, None, comment
 | 
			
		||||
                )
 | 
			
		||||
@@ -1928,7 +1934,7 @@ def migrate_payments(in_dir: str, out_dir: str) -> None:
 | 
			
		||||
                    variant_year_map[year] = []
 | 
			
		||||
                variant_year_map[year].append((p['AZNR'], year_map[year], p['TeilzahlungNr']))
 | 
			
		||||
            f_payment.row(year, year_map[year], p['Titel'], p['Datum'], test, None,
 | 
			
		||||
                          p['Beschreibung'], json.dumps(data))
 | 
			
		||||
                          p['Beschreibung'], json.dumps(data).replace('/B', '/-B').replace('"/-B"', '"-B"'))
 | 
			
		||||
 | 
			
		||||
    with utils.csv_open(f'{out_dir}/payment_delivery_part.csv') as f_del_pay, \
 | 
			
		||||
            utils.csv_open(f'{out_dir}/delivery_part_bucket.csv') as f_bucket:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user