@flowselections/floriday-verkoop-module 1.0.104 → 1.0.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ // Verpakking-snapshot per orderregel — pure regels, voor alle ordersoorten
2
+ // (FO, HO, MO, AO). Een regel krijgt bij het aanmaken één vaste verpakking;
3
+ // daarna verandert die nooit vanzelf. Ontbrekend = leeg (null), nooit 1.
4
+ export const EMPTY_PACKING = {
5
+ plants_per_crate: null,
6
+ crates_per_layer: null,
7
+ layers_per_cart: null,
8
+ vbn_package_code: null,
9
+ load_carrier_type: null,
10
+ packing_source: null,
11
+ };
12
+ /** Positief geheel getal of null. 0, leeg of onzin wordt null — nooit 1. */
13
+ export function posOrNull(v) {
14
+ if (v == null || v === "")
15
+ return null;
16
+ const n = Number(v);
17
+ return Number.isFinite(n) && n > 0 ? Math.round(n) : null;
18
+ }
19
+ function strOrNull(v) {
20
+ if (v == null)
21
+ return null;
22
+ const s = String(v).trim();
23
+ return s ? s : null;
24
+ }
25
+ /**
26
+ * FO: alleen de packingConfiguration van de Floriday-sales order zelf.
27
+ * Bij klokorders zijn packagesPerLayer/layersPerLoadCarrier 0 → leeg; de
28
+ * Logistieke Engine vult die aan. Geen terugval op artikel of product.
29
+ */
30
+ export function snapshotFromFloridayOrder(cfg) {
31
+ if (!cfg || typeof cfg !== "object")
32
+ return { ...EMPTY_PACKING };
33
+ const snap = {
34
+ plants_per_crate: posOrNull(cfg.piecesPerPackage ?? cfg.unitsPerPackage),
35
+ crates_per_layer: posOrNull(cfg.packagesPerLayer),
36
+ layers_per_cart: posOrNull(cfg.layersPerLoadCarrier),
37
+ vbn_package_code: strOrNull(cfg.package?.vbnPackageCode ?? cfg.vbnPackageCode),
38
+ load_carrier_type: strOrNull(cfg.loadCarrier ?? cfg.loadCarrierType),
39
+ packing_source: null,
40
+ };
41
+ const any = snap.plants_per_crate != null ||
42
+ snap.crates_per_layer != null ||
43
+ snap.layers_per_cart != null ||
44
+ snap.vbn_package_code != null ||
45
+ snap.load_carrier_type != null;
46
+ return { ...snap, packing_source: any ? "floriday_order" : null };
47
+ }
48
+ /**
49
+ * Bestaande regel bijwerken: een gevuld veld wordt nooit overschreven; een
50
+ * leeg veld mag alleen worden aangevuld met een echte waarde (> 0 of tekst).
51
+ */
52
+ export function fillOnlyEmpty(existing, incoming) {
53
+ const num = (a, b) => posOrNull(a) ?? b;
54
+ const txt = (a, b) => strOrNull(a) ?? b;
55
+ return {
56
+ plants_per_crate: num(existing.plants_per_crate, incoming.plants_per_crate),
57
+ crates_per_layer: num(existing.crates_per_layer, incoming.crates_per_layer),
58
+ layers_per_cart: num(existing.layers_per_cart, incoming.layers_per_cart),
59
+ vbn_package_code: txt(existing.vbn_package_code, incoming.vbn_package_code),
60
+ load_carrier_type: txt(existing.load_carrier_type, incoming.load_carrier_type),
61
+ packing_source: strOrNull(existing.packing_source) ?? incoming.packing_source,
62
+ };
63
+ }
64
+ function toSnap(c, source) {
65
+ return {
66
+ plants_per_crate: posOrNull(c.plants_per_crate),
67
+ crates_per_layer: posOrNull(c.crates_per_layer),
68
+ layers_per_cart: posOrNull(c.layers_per_cart),
69
+ vbn_package_code: strOrNull(c.vbn_package_code),
70
+ load_carrier_type: strOrNull(c.load_carrier_type),
71
+ packing_source: source,
72
+ };
73
+ }
74
+ const hasAny = (c) => !!c &&
75
+ (posOrNull(c.plants_per_crate) != null ||
76
+ posOrNull(c.crates_per_layer) != null ||
77
+ posOrNull(c.layers_per_cart) != null);
78
+ /**
79
+ * HO/MO/AO: eigen keuze → klantspecifiek → primaire verpakking van het
80
+ * product. Niets bekend → leeg snapshot (de aanroeper meldt dat).
81
+ */
82
+ export function choosePacking(opts) {
83
+ if (hasAny(opts.chosen))
84
+ return toSnap(opts.chosen, "chosen");
85
+ if (hasAny(opts.customer))
86
+ return toSnap(opts.customer, "customer");
87
+ if (hasAny(opts.primary))
88
+ return toSnap(opts.primary, "product_primary");
89
+ return { ...EMPTY_PACKING };
90
+ }
91
+ /**
92
+ * Hoeveelheid in fust → planten. Zonder stuks per fust: fout, nooit raden.
93
+ */
94
+ export function plantsFromCrates(crates, plantsPerCrate) {
95
+ const c = posOrNull(crates);
96
+ if (c == null)
97
+ return { ok: false, reason: "Het aantal fust moet groter dan 0 zijn." };
98
+ if (plantsPerCrate == null || !(plantsPerCrate > 0)) {
99
+ return {
100
+ ok: false,
101
+ reason: "Het aantal planten per fust is niet bekend voor deze verpakking. Kies een verpakking met planten per fust.",
102
+ };
103
+ }
104
+ return { ok: true, plants: c * plantsPerCrate };
105
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"salesorders-list.server.d.ts","sourceRoot":"","sources":["../../src/lib/salesorders-list.server.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAML,KAAK,aAAa,EAClB,KAAK,iBAAiB,EAEvB,MAAM,uBAAuB,CAAC;AAQ/B,OAAO,EAAkC,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAIjF,4EAA4E;AAC5E,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,UAAU,GAAG,SAAS,GAAG,cAAc,CAAC;AAExE,MAAM,WAAW,UAAU;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,SAAS,GAAG,UAAU,CAAC;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,SAAS,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,+DAA+D;IAC/D,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,yEAAyE;IACzE,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,aAAa,EAAE,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACxC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,WAAW,EAAE,SAAS,GAAG,MAAM,CAAC;IAChC,2DAA2D;IAC3D,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;CACpB;AA8ID,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,cAAc,EACxB,MAAM,EAAE,UAAU,EAClB,kBAAkB,GAAE,cAAyB,GAC5C,OAAO,CAAC,UAAU,CAAC,CA8lCrB"}
1
+ {"version":3,"file":"salesorders-list.server.d.ts","sourceRoot":"","sources":["../../src/lib/salesorders-list.server.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAML,KAAK,aAAa,EAClB,KAAK,iBAAiB,EAEvB,MAAM,uBAAuB,CAAC;AAQ/B,OAAO,EAAkC,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAIjF,4EAA4E;AAC5E,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,UAAU,GAAG,SAAS,GAAG,cAAc,CAAC;AAExE,MAAM,WAAW,UAAU;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,SAAS,GAAG,UAAU,CAAC;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,SAAS,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,+DAA+D;IAC/D,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,yEAAyE;IACzE,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,aAAa,EAAE,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACxC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,WAAW,EAAE,SAAS,GAAG,MAAM,CAAC;IAChC,2DAA2D;IAC3D,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;CACpB;AA8ID,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,cAAc,EACxB,MAAM,EAAE,UAAU,EAClB,kBAAkB,GAAE,cAAyB,GAC5C,OAAO,CAAC,UAAU,CAAC,CA8lCrB"}
@@ -1,4 +1,5 @@
1
1
  import { loadReviewDismissals, isReviewDismissed } from "./review-dismissals.server.js";
2
+ import { posOrNull } from "./packing-snapshot.js";
2
3
  import { customerDisplayName } from "./customer-name.js";
3
4
  import { isNotReportedLocally } from "./floriday-status.js";
4
5
  import { unlinkedFloridayProducts } from "./floriday-unlinked-products.js";
@@ -1174,13 +1175,17 @@ async function loadGroupPacking(supabase) {
1174
1175
  for (const p of (data ?? []))
1175
1176
  headById.set(p.id, p);
1176
1177
  }
1177
- const packFromRow = (row) => row
1178
- ? {
1179
- plants_per_crate: Math.max(1, Number(row.plants_per_crate ?? 1)),
1180
- crates_per_layer: Math.max(1, Number(row.crates_per_layer ?? 1)),
1181
- layers_per_cart: Math.max(1, Number(row.layers_per_cart ?? 1)),
1182
- }
1183
- : null;
1178
+ // Alleen een volledige verpakking telt; ontbrekend wordt nooit 1.
1179
+ const packFromRow = (row) => {
1180
+ if (!row)
1181
+ return null;
1182
+ const v = {
1183
+ plants_per_crate: posOrNull(row.plants_per_crate),
1184
+ crates_per_layer: posOrNull(row.crates_per_layer),
1185
+ layers_per_cart: posOrNull(row.layers_per_cart),
1186
+ };
1187
+ return v.plants_per_crate && v.crates_per_layer && v.layers_per_cart ? v : null;
1188
+ };
1184
1189
  const byName = new Map();
1185
1190
  for (const g of groups) {
1186
1191
  const packing = packFromRow(headById.get(g.head_product_id)) ?? packFromRow(mirrorByGroupId.get(g.id));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/floriday-verkoop-module",
3
- "version": "1.0.104",
3
+ "version": "1.0.107",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -349,6 +349,14 @@ DO $b$ BEGIN IF to_regclass('public.order_items') IS NOT NULL THEN
349
349
  ALTER TABLE public.order_items ADD COLUMN IF NOT EXISTS needs_sticker boolean NOT NULL DEFAULT false;
350
350
  END IF; END $b$;
351
351
 
352
+ DO $c$ BEGIN IF to_regclass('public.order_items') IS NOT NULL THEN
353
+ -- Verpakking-snapshot per orderregel (vast bij aanmaken).
354
+ ALTER TABLE public.order_items ADD COLUMN IF NOT EXISTS quantity_crates integer;
355
+ ALTER TABLE public.order_items ADD COLUMN IF NOT EXISTS vbn_package_code text;
356
+ ALTER TABLE public.order_items ADD COLUMN IF NOT EXISTS load_carrier_type text;
357
+ ALTER TABLE public.order_items ADD COLUMN IF NOT EXISTS packing_source text;
358
+ END IF; END $c$;
359
+
352
360
  -- 3. Constraints en indexen
353
361
  DO $c$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname='invoice_timing_settings_pkey' AND conrelid='invoice_timing_settings'::regclass) THEN ALTER TABLE invoice_timing_settings ADD CONSTRAINT invoice_timing_settings_pkey PRIMARY KEY (id); END IF; EXCEPTION WHEN undefined_table OR undefined_column OR duplicate_object OR duplicate_table OR invalid_table_definition THEN RAISE NOTICE 'verkoop-schema: constraint overgeslagen'; END $c$;
354
362
  DO $c$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname='order_cart_layer_positions_pkey' AND conrelid='order_cart_layer_positions'::regclass) THEN ALTER TABLE order_cart_layer_positions ADD CONSTRAINT order_cart_layer_positions_pkey PRIMARY KEY (id); END IF; EXCEPTION WHEN undefined_table OR undefined_column OR duplicate_object OR duplicate_table OR invalid_table_definition THEN RAISE NOTICE 'verkoop-schema: constraint overgeslagen'; END $c$;
@@ -1637,6 +1645,11 @@ DECLARE
1637
1645
  v_stickers jsonb := '{}'::jsonb;
1638
1646
  v_items jsonb := '[]'::jsonb;
1639
1647
  v_cf jsonb;
1648
+ v_pj jsonb;
1649
+ v_pack jsonb;
1650
+ v_ppc int; v_cpl int; v_lpc int;
1651
+ v_vbn text; v_lc text; v_psrc text;
1652
+ v_qty_crates int;
1640
1653
  BEGIN
1641
1654
  -- Wie mag: serverrol / databasetaak (geen ingelogde gebruiker, geen anon)
1642
1655
  -- of een ingelogde gebruiker met een verkooprol.
@@ -1681,28 +1694,68 @@ BEGIN
1681
1694
  -- Regels: product op id, prijs > 0, belading uit het product.
1682
1695
  FOR v_line IN SELECT * FROM jsonb_array_elements(p_lines) LOOP
1683
1696
  v_n := v_n + 1;
1697
+ v_pack := CASE WHEN jsonb_typeof(v_line->'packing') = 'object' THEN v_line->'packing' ELSE NULL END;
1684
1698
  BEGIN
1685
- v_qty := (v_line->>'quantity_plants')::int;
1699
+ v_qty := nullif(v_line->>'quantity_plants', '')::int;
1700
+ v_qty_crates := nullif(v_line->>'quantity_crates', '')::int;
1686
1701
  v_price := (v_line->>'unit_price')::numeric;
1702
+ v_ppc := nullif(greatest(coalesce(nullif(v_pack->>'plants_per_crate', '')::numeric, 0), 0), 0)::int;
1703
+ v_cpl := nullif(greatest(coalesce(nullif(v_pack->>'crates_per_layer', '')::numeric, 0), 0), 0)::int;
1704
+ v_lpc := nullif(greatest(coalesce(nullif(v_pack->>'layers_per_cart', '')::numeric, 0), 0), 0)::int;
1687
1705
  EXCEPTION WHEN others THEN
1688
- RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: aantal of prijs ongeldig.', v_n));
1706
+ RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: aantal, prijs of verpakking ongeldig.', v_n));
1689
1707
  END;
1690
- IF v_qty IS NULL OR v_qty <= 0 THEN
1691
- RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: aantal moet groter dan 0 zijn.', v_n));
1692
- END IF;
1693
1708
  IF v_price IS NULL OR v_price <= 0 THEN
1694
1709
  RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: prijs moet groter dan 0 zijn.', v_n));
1695
1710
  END IF;
1696
- SELECT id, product, unit, plants_per_crate, crates_per_layer, layers_per_cart INTO v_prod
1697
- FROM public.products WHERE id = nullif(v_line->>'product_id', '')::uuid;
1698
- IF NOT FOUND THEN
1711
+ -- Product op id; als jsonb gelezen zodat een ontbrekende kolom nooit stukloopt.
1712
+ v_pj := NULL;
1713
+ SELECT to_jsonb(p.*) INTO v_pj FROM public.products p WHERE p.id = nullif(v_line->>'product_id', '')::uuid;
1714
+ IF v_pj IS NULL THEN
1699
1715
  RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: product niet gevonden.', v_n));
1700
1716
  END IF;
1717
+ -- Verpakking-snapshot: eigen keuze per regel, anders de primaire van het product.
1718
+ -- Ontbrekend blijft leeg; nooit stil 1.
1719
+ IF v_ppc IS NOT NULL OR v_cpl IS NOT NULL OR v_lpc IS NOT NULL THEN
1720
+ v_vbn := nullif(trim(coalesce(v_pack->>'vbn_package_code', '')), '');
1721
+ v_lc := nullif(trim(coalesce(v_pack->>'load_carrier_type', '')), '');
1722
+ v_psrc := 'chosen';
1723
+ ELSE
1724
+ v_ppc := CASE WHEN (v_pj->>'plants_per_crate') ~ '^[0-9]+$' AND (v_pj->>'plants_per_crate')::int > 0 THEN (v_pj->>'plants_per_crate')::int END;
1725
+ v_cpl := CASE WHEN (v_pj->>'crates_per_layer') ~ '^[0-9]+$' AND (v_pj->>'crates_per_layer')::int > 0 THEN (v_pj->>'crates_per_layer')::int END;
1726
+ v_lpc := CASE WHEN (v_pj->>'layers_per_cart') ~ '^[0-9]+$' AND (v_pj->>'layers_per_cart')::int > 0 THEN (v_pj->>'layers_per_cart')::int END;
1727
+ v_vbn := NULL;
1728
+ IF nullif(v_pj->>'fust_type_id', '') IS NOT NULL THEN
1729
+ BEGIN
1730
+ EXECUTE 'SELECT vbn_code::text FROM public.fust_types WHERE id = $1' INTO v_vbn USING (v_pj->>'fust_type_id')::uuid;
1731
+ EXCEPTION WHEN others THEN v_vbn := NULL;
1732
+ END;
1733
+ END IF;
1734
+ v_lc := NULL;
1735
+ v_psrc := CASE WHEN v_ppc IS NOT NULL OR v_cpl IS NOT NULL OR v_lpc IS NOT NULL THEN 'product_primary' END;
1736
+ END IF;
1737
+ -- Hoeveelheid: altijd ook in planten. Fust -> planten met de stuks per fust.
1738
+ IF v_qty IS NULL AND v_qty_crates IS NOT NULL THEN
1739
+ IF v_qty_crates <= 0 THEN
1740
+ RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: aantal fust moet groter dan 0 zijn.', v_n));
1741
+ END IF;
1742
+ IF v_ppc IS NULL THEN
1743
+ RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: het aantal planten per fust is niet bekend; de regel kan niet in fust worden aangemaakt.', v_n));
1744
+ END IF;
1745
+ v_qty := v_qty_crates * v_ppc;
1746
+ ELSIF v_qty IS NOT NULL AND v_qty_crates IS NOT NULL AND v_ppc IS NOT NULL AND v_qty <> v_qty_crates * v_ppc THEN
1747
+ RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: aantal planten past niet bij het aantal fust.', v_n));
1748
+ END IF;
1749
+ IF v_qty IS NULL OR v_qty <= 0 THEN
1750
+ RETURN jsonb_build_object('ok', false, 'reason', format('Regel %s: aantal moet groter dan 0 zijn.', v_n));
1751
+ END IF;
1701
1752
  v_total := v_total + v_qty * v_price;
1702
1753
  v_items := v_items || jsonb_build_object(
1703
- 'product_id', v_prod.id, 'product_name', v_prod.product, 'quantity', v_qty,
1704
- 'unit', coalesce(v_prod.unit, 'stuks'), 'unit_price', v_price,
1705
- 'ppc', v_prod.plants_per_crate, 'cpl', v_prod.crates_per_layer, 'lpc', v_prod.layers_per_cart,
1754
+ 'product_id', v_pj->>'id', 'product_name', v_pj->>'product', 'quantity', v_qty,
1755
+ 'quantity_crates', v_qty_crates,
1756
+ 'unit', coalesce(v_pj->>'unit', 'stuks'), 'unit_price', v_price,
1757
+ 'ppc', v_ppc, 'cpl', v_cpl, 'lpc', v_lpc,
1758
+ 'vbn', v_vbn, 'lc', v_lc, 'psrc', v_psrc,
1706
1759
  'key', v_src || ':' || p_source_id || ':' || v_n,
1707
1760
  'needs_sticker', CASE WHEN jsonb_typeof(v_line->'needs_sticker') = 'boolean' THEN (v_line->>'needs_sticker')::boolean ELSE false END);
1708
1761
  IF nullif(v_line->>'sticker', '') IS NOT NULL THEN
@@ -1771,14 +1824,17 @@ BEGIN
1771
1824
  INSERT INTO public.orders (customer_id, customer_name, order_date, delivery_date, is_reservation,
1772
1825
  status, total, notes, order_source, custom_fields)
1773
1826
  VALUES (p_customer_id, v_customer.name, current_date, p_delivery_date, false,
1774
- 'Nog te picken', to_char(round(v_total, 2), 'FM9999999990.00'), p_notes, v_cf_source, v_cf)
1827
+ 'Nog te picken', to_char(round(v_total, 2), 'FM9999999990.00'), p_notes,
1828
+ CASE WHEN v_src = 'actiemail' THEN 'offer' ELSE 'mailbox' END, v_cf)
1775
1829
  RETURNING id, order_number INTO v_order_id, v_order_number;
1776
1830
 
1777
1831
  INSERT INTO public.order_items (order_id, product_id, product_name, quantity, unit, unit_price,
1778
- plants_per_crate, crates_per_layer, layers_per_cart, source_line_key, needs_sticker)
1832
+ plants_per_crate, crates_per_layer, layers_per_cart, source_line_key, needs_sticker,
1833
+ quantity_crates, vbn_package_code, load_carrier_type, packing_source)
1779
1834
  SELECT v_order_id, (i->>'product_id')::uuid, i->>'product_name', (i->>'quantity')::int, i->>'unit',
1780
1835
  (i->>'unit_price')::numeric, nullif(i->>'ppc', '')::int, nullif(i->>'cpl', '')::int,
1781
- nullif(i->>'lpc', '')::int, i->>'key', coalesce((i->>'needs_sticker')::boolean, false)
1836
+ nullif(i->>'lpc', '')::int, i->>'key', coalesce((i->>'needs_sticker')::boolean, false),
1837
+ nullif(i->>'quantity_crates', '')::int, i->>'vbn', i->>'lc', i->>'psrc'
1782
1838
  FROM jsonb_array_elements(v_items) i;
1783
1839
 
1784
1840
  -- Planning melden; mislukt dat, dan pakt de planning de order bij zijn eigen ronde op.
@@ -1794,6 +1850,22 @@ $function$
1794
1850
  ;
1795
1851
 
1796
1852
 
1853
+ -- Opruimen: oude kopie van productverpakking naar Floriday-gegevens en
1854
+ -- sales orders (overschreef de verpakking met productwaarden, met stille 1).
1855
+ DO $d$ BEGIN
1856
+ IF to_regclass('public.products') IS NOT NULL THEN
1857
+ EXECUTE 'DROP TRIGGER IF EXISTS products_sync_cached_packing_trg ON public.products';
1858
+ END IF;
1859
+ IF to_regclass('public.floriday_trade_items_cache') IS NOT NULL THEN
1860
+ EXECUTE 'DROP TRIGGER IF EXISTS floriday_cache_sync_packing_trg ON public.floriday_trade_items_cache';
1861
+ END IF;
1862
+ END $d$;
1863
+ DROP FUNCTION IF EXISTS public.trg_products_sync_cached_packing();
1864
+ DROP FUNCTION IF EXISTS public.trg_floriday_cache_sync_packing();
1865
+ DROP FUNCTION IF EXISTS public.sync_cached_packing_from_product(uuid);
1866
+ DROP FUNCTION IF EXISTS public.sync_order_cart_item_packing_from_product(uuid);
1867
+ DROP FUNCTION IF EXISTS public.sync_product_packing_from_floriday_cache(text);
1868
+
1797
1869
  -- 5. RLS, rechten, policies, triggers
1798
1870
  ALTER TABLE public.cart_replan_queue ENABLE ROW LEVEL SECURITY;
1799
1871
  ALTER TABLE public.delivery_notes ENABLE ROW LEVEL SECURITY;