@flowselections/floriday-voorraad 1.0.131 → 1.0.135

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.
Files changed (36) hide show
  1. package/dist-lib/components/voorraad/ArtikelWizard.d.ts.map +1 -1
  2. package/dist-lib/components/voorraad/ArtikelWizard.js +36 -6
  3. package/dist-lib/hooks/useLocalTradeItems.d.ts.map +1 -1
  4. package/dist-lib/hooks/useLocalTradeItems.js +4 -0
  5. package/dist-lib/hooks/useVoorraadData.d.ts.map +1 -1
  6. package/dist-lib/hooks/useVoorraadData.js +2 -1
  7. package/dist-lib/integrations/supabase/types.d.ts +72 -0
  8. package/dist-lib/integrations/supabase/types.d.ts.map +1 -1
  9. package/dist-lib/lib/__tests__/mirror-products.test.d.ts +2 -0
  10. package/dist-lib/lib/__tests__/mirror-products.test.d.ts.map +1 -0
  11. package/dist-lib/lib/__tests__/mirror-products.test.js +13 -0
  12. package/dist-lib/lib/__tests__/release-134.test.d.ts +2 -0
  13. package/dist-lib/lib/__tests__/release-134.test.d.ts.map +1 -0
  14. package/dist-lib/lib/__tests__/release-134.test.js +50 -0
  15. package/dist-lib/lib/floriday-warehouse.functions.d.ts.map +1 -1
  16. package/dist-lib/lib/floriday-warehouse.functions.js +4 -2
  17. package/dist-lib/lib/floriday-writes.functions.d.ts.map +1 -1
  18. package/dist-lib/lib/floriday-writes.functions.js +11 -10
  19. package/dist-lib/lib/idempotency-key.d.ts +11 -0
  20. package/dist-lib/lib/idempotency-key.d.ts.map +1 -0
  21. package/dist-lib/lib/idempotency-key.js +41 -0
  22. package/dist-lib/lib/mirror-products.d.ts +12 -0
  23. package/dist-lib/lib/mirror-products.d.ts.map +1 -0
  24. package/dist-lib/lib/mirror-products.js +10 -0
  25. package/dist-lib/lib/publish-errors.d.ts +1 -1
  26. package/dist-lib/lib/publish-errors.d.ts.map +1 -1
  27. package/dist-lib/lib/publish-errors.js +10 -0
  28. package/dist-lib/lib/publish-pipeline.js +1 -1
  29. package/dist-lib/lib/publish-status.d.ts +3 -1
  30. package/dist-lib/lib/publish-status.d.ts.map +1 -1
  31. package/dist-lib/lib/publish-status.js +4 -1
  32. package/dist-lib/lib/publish-verify.d.ts +25 -0
  33. package/dist-lib/lib/publish-verify.d.ts.map +1 -0
  34. package/dist-lib/lib/publish-verify.js +44 -0
  35. package/package.json +1 -1
  36. package/sql/floriday-voorraad-schema.sql +249 -70
@@ -1,4 +1,4 @@
1
- -- @flowselections/floriday-voorraad 1.0.130 – volledig schema (idempotent, geen gegevens).
1
+ -- @flowselections/floriday-voorraad 1.0.134 – volledig schema (idempotent, geen gegevens).
2
2
  -- Gegenereerd uit de database met scripts/generate-voorraad-schema.py. Niet met de hand wijzigen.
3
3
  -- Volgorde 20. Ontbrekende tabellen van andere modules worden overgeslagen met een NOTICE; draai opnieuw na die module.
4
4
  -- De statuscontrole van floriday_continuous_stock_queue is van Voorraad en wordt hier opnieuw gemaakt.
@@ -458,6 +458,9 @@ ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS notes text;
458
458
  ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS updated_at timestamp with time zone DEFAULT now() NOT NULL;
459
459
  ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS created_at timestamp with time zone DEFAULT now() NOT NULL;
460
460
  ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS change_source text DEFAULT 'MANUAL_STOCK'::text NOT NULL;
461
+ ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS archived_at timestamp with time zone;
462
+ ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS archived_reason text;
463
+ ALTER TABLE public.trade_item_stock ADD COLUMN IF NOT EXISTS archived_into text;
461
464
 
462
465
  -- trade_item_supply_lines
463
466
  CREATE TABLE IF NOT EXISTS public.trade_item_supply_lines ();
@@ -2440,7 +2443,6 @@ CREATE OR REPLACE FUNCTION public.ensure_trade_item_stock_row_key(p_key text)
2440
2443
  AS $function$
2441
2444
  DECLARE
2442
2445
  v_ctx RECORD;
2443
- v_product_id uuid;
2444
2446
  v_canonical_key text;
2445
2447
  BEGIN
2446
2448
  PERFORM public.voorraad_vereis('ingelogd', 'ensure_trade_item_stock_row_key');
@@ -2448,53 +2450,17 @@ BEGIN
2448
2450
  RETURN NULL;
2449
2451
  END IF;
2450
2452
 
2451
- v_canonical_key := btrim(p_key);
2452
-
2453
- IF v_canonical_key LIKE 'product:%' THEN
2454
- BEGIN
2455
- v_product_id := substring(v_canonical_key from 9)::uuid;
2456
- EXCEPTION WHEN others THEN
2457
- v_product_id := NULL;
2458
- END;
2459
- ELSE
2460
- BEGIN
2461
- v_product_id := v_canonical_key::uuid;
2462
- EXCEPTION WHEN others THEN
2463
- v_product_id := NULL;
2464
- END;
2465
- END IF;
2466
-
2467
- IF v_product_id IS NOT NULL THEN
2468
- SELECT COALESCE(
2469
- NULLIF(btrim(COALESCE(p.floriday_trade_item_id, '')), ''),
2470
- NULLIF(btrim(COALESCE(p.trade_item_key, '')), ''),
2471
- p.id::text
2472
- )
2473
- INTO v_canonical_key
2474
- FROM public.products p
2475
- WHERE p.id = v_product_id
2476
- LIMIT 1;
2477
- v_canonical_key := COALESCE(v_canonical_key, btrim(p_key));
2478
- ELSE
2479
- SELECT COALESCE(
2480
- NULLIF(btrim(COALESCE(p.floriday_trade_item_id, '')), ''),
2481
- NULLIF(btrim(COALESCE(p.trade_item_key, '')), ''),
2482
- p.id::text
2483
- )
2484
- INTO v_canonical_key
2485
- FROM public.products p
2486
- WHERE p.floriday_trade_item_id = p_key
2487
- OR p.trade_item_key = p_key
2488
- LIMIT 1;
2489
- v_canonical_key := COALESCE(v_canonical_key, btrim(p_key));
2490
- END IF;
2453
+ BEGIN
2454
+ SELECT c.trade_item_key INTO v_canonical_key FROM public.voorraad_canoniek_artikel(p_key) c;
2455
+ EXCEPTION
2456
+ WHEN SQLSTATE 'VR011' THEN v_canonical_key := btrim(p_key);
2457
+ WHEN SQLSTATE 'VR010' THEN
2458
+ RAISE WARNING 'Voorraad: % — geen voorraadregel gekozen', SQLERRM;
2459
+ RETURN NULL;
2460
+ END;
2461
+ v_canonical_key := COALESCE(v_canonical_key, btrim(p_key));
2491
2462
 
2492
- -- Eerst opzoeken: bestaat de canonieke rij al, dan de sleutel teruggeven,
2493
- -- ongeacht of er een actieve Floriday-koppeling is.
2494
- IF EXISTS (
2495
- SELECT 1 FROM public.trade_item_stock s
2496
- WHERE s.trade_item_key = v_canonical_key
2497
- ) THEN
2463
+ IF EXISTS (SELECT 1 FROM public.trade_item_stock s WHERE s.trade_item_key = v_canonical_key) THEN
2498
2464
  RETURN v_canonical_key;
2499
2465
  END IF;
2500
2466
 
@@ -4599,11 +4565,11 @@ DECLARE v_key text; v_p numeric; v_r numeric; v_hit boolean := false;
4599
4565
  BEGIN
4600
4566
  PERFORM public.voorraad_vereis('ingelogd', 'product_own_available');
4601
4567
  has_row := false; physical := 0; reserved := 0; available := 0;
4602
- SELECT COALESCE(NULLIF(btrim(COALESCE(p.floriday_trade_item_id,'')),''), NULLIF(btrim(COALESCE(p.trade_item_key,'')),''))
4603
- INTO v_key FROM public.products p WHERE p.id = p_product_id;
4568
+ v_key := public.voorraad_canoniek_sleutel(p_product_id);
4604
4569
  IF v_key IS NULL THEN RETURN; END IF;
4605
4570
  SELECT true, COALESCE(ts.physical_stock,0), COALESCE(ts.reserved_stock,0) INTO v_hit, v_p, v_r
4606
- FROM public.trade_item_stock ts WHERE ts.connection_id = p_connection_id AND ts.trade_item_key = v_key LIMIT 1;
4571
+ FROM public.trade_item_stock ts
4572
+ WHERE ts.connection_id = p_connection_id AND ts.trade_item_key = v_key AND ts.archived_at IS NULL LIMIT 1;
4607
4573
  IF COALESCE(v_hit,false) THEN
4608
4574
  has_row := true; physical := v_p; reserved := v_r;
4609
4575
  available := GREATEST(0, floor(v_p - v_r))::int;
@@ -5215,12 +5181,11 @@ DECLARE k text;
5215
5181
  BEGIN
5216
5182
  PERFORM public.voorraad_vereis('ingelogd', 'recompute_reserved_for_order');
5217
5183
  FOR k IN
5218
- SELECT DISTINCT p.trade_item_key
5184
+ SELECT DISTINCT public.voorraad_orderregel_sleutel(oi.product_id::text)
5219
5185
  FROM logistics_active.order_items oi
5220
- JOIN public.products p ON p.id = oi.product_id
5221
- WHERE oi.order_id = p_order_id AND p.trade_item_key IS NOT NULL
5186
+ WHERE oi.order_id = p_order_id AND oi.product_id IS NOT NULL
5222
5187
  LOOP
5223
- PERFORM public.recompute_reserved_stock(k);
5188
+ IF k IS NOT NULL THEN PERFORM public.recompute_reserved_stock(k); END IF;
5224
5189
  END LOOP;
5225
5190
  END $function$
5226
5191
  ;
@@ -5244,16 +5209,15 @@ DECLARE
5244
5209
  v_products uuid[];
5245
5210
  v_pid uuid;
5246
5211
  v_leads boolean;
5212
+ v_refs text[];
5247
5213
  BEGIN
5248
5214
  PERFORM public.voorraad_vereis('ingelogd', 'recompute_reserved_stock');
5249
5215
  IF p_trade_item_key IS NULL OR btrim(p_trade_item_key) = '' THEN RETURN; END IF;
5250
5216
  IF current_setting('app.recomputing_reserved', true) = 'on' THEN RETURN; END IF;
5251
5217
 
5252
- -- B1: nieuwe of losse sleutel zonder orderregels, toewijzing of set-/groepsrelatie en reservering 0: niets te doen.
5253
5218
  v_key := public.logistics_reserved_key(p_trade_item_key);
5254
5219
  IF public.logistics_reserved_fast_skip(p_trade_item_key, v_key) THEN RETURN; END IF;
5255
5220
 
5256
- -- Set of groepshoofdproduct: gericht herberekenen op de onderdelen en leden (M7).
5257
5221
  SELECT array_agg(p.id) INTO v_products
5258
5222
  FROM public.products p
5259
5223
  WHERE (p.trade_item_key = btrim(p_trade_item_key)
@@ -5281,8 +5245,6 @@ BEGIN
5281
5245
  RETURN;
5282
5246
  END IF;
5283
5247
 
5284
- -- Sleutel hoort bij een groep of samenstelling: de toewijzing hangt van andere
5285
- -- regels af, dus volledig herberekenen.
5286
5248
  IF v_key LIKE 'group:%'
5287
5249
  OR EXISTS (SELECT 1 FROM public.logistics_reservation_allocations a
5288
5250
  WHERE a.trade_item_key = v_key AND a.source = 'group')
@@ -5299,18 +5261,28 @@ BEGIN
5299
5261
  RETURN;
5300
5262
  END IF;
5301
5263
 
5264
+ SELECT array_agg(DISTINCT r) INTO v_refs FROM (
5265
+ SELECT p.id::text AS r FROM public.products p
5266
+ WHERE p.trade_item_key = v_key OR p.floriday_trade_item_id = v_key OR p.id::text = v_key
5267
+ OR 'product:' || p.id::text = v_key
5268
+ UNION
5269
+ SELECT btrim(p.floriday_trade_item_id) FROM public.products p
5270
+ WHERE (p.trade_item_key = v_key OR p.floriday_trade_item_id = v_key OR 'product:' || p.id::text = v_key)
5271
+ AND NULLIF(btrim(COALESCE(p.floriday_trade_item_id,'')),'') IS NOT NULL
5272
+ UNION SELECT v_key
5273
+ ) x WHERE r IS NOT NULL;
5274
+
5302
5275
  v_leads := public.voorraad_floriday_leads_key(v_key);
5303
5276
  SELECT CEIL(COALESCE(SUM(COALESCE(oi.quantity, 0)), 0))::integer INTO v_sum
5304
5277
  FROM logistics_active.order_items oi
5305
5278
  JOIN public.orders o ON o.id = oi.order_id
5306
- JOIN public.products p ON p.id = oi.product_id
5307
- WHERE COALESCE(NULLIF(btrim(COALESCE(p.floriday_trade_item_id, '')), ''),
5308
- NULLIF(btrim(COALESCE(p.trade_item_key, '')), ''), p.id::text) = v_key
5279
+ WHERE oi.product_id::text = ANY(v_refs)
5280
+ AND public.voorraad_orderregel_sleutel(oi.product_id::text) = v_key
5309
5281
  AND o.status = 'Nog te picken' AND NOT public.logistics_reserved_order_excluded(o.id)
5310
5282
  AND NOT (public.normalize_order_source(o.order_source) = 'FO' AND v_leads)
5311
- AND NOT EXISTS (SELECT 1 FROM public.product_bom_items b WHERE b.parent_product_id = p.id)
5283
+ AND NOT EXISTS (SELECT 1 FROM public.product_bom_items b WHERE b.parent_product_id = oi.product_id)
5312
5284
  AND NOT EXISTS (SELECT 1 FROM public.product_groups pg
5313
- WHERE pg.is_active = true AND (pg.id = p.id OR pg.head_product_id = p.id))
5285
+ WHERE pg.is_active = true AND (pg.id = oi.product_id OR pg.head_product_id = oi.product_id))
5314
5286
  AND NOT EXISTS (
5315
5287
  SELECT 1 FROM public.stock_mutation_log sml
5316
5288
  WHERE sml.order_item_id = oi.id
@@ -6786,7 +6758,7 @@ AS $function$
6786
6758
  DECLARE v_key text; v_old_key text;
6787
6759
  BEGIN
6788
6760
  IF (TG_OP = 'INSERT') THEN
6789
- SELECT p.trade_item_key INTO v_key FROM public.products p WHERE p.id = NEW.product_id;
6761
+ v_key := public.voorraad_orderregel_sleutel(NEW.product_id::text);
6790
6762
  PERFORM public.recompute_reserved_stock(v_key);
6791
6763
  RETURN NEW;
6792
6764
  ELSIF (TG_OP = 'UPDATE') THEN
@@ -6797,15 +6769,15 @@ BEGIN
6797
6769
  AND NEW.source_line_key IS NOT DISTINCT FROM OLD.source_line_key THEN
6798
6770
  RETURN NEW;
6799
6771
  END IF;
6800
- SELECT p.trade_item_key INTO v_key FROM public.products p WHERE p.id = NEW.product_id;
6801
- SELECT p.trade_item_key INTO v_old_key FROM public.products p WHERE p.id = OLD.product_id;
6772
+ v_key := public.voorraad_orderregel_sleutel(NEW.product_id::text);
6773
+ v_old_key := public.voorraad_orderregel_sleutel(OLD.product_id::text);
6802
6774
  PERFORM public.recompute_reserved_stock(v_key);
6803
6775
  IF v_old_key IS DISTINCT FROM v_key THEN
6804
6776
  PERFORM public.recompute_reserved_stock(v_old_key);
6805
6777
  END IF;
6806
6778
  RETURN NEW;
6807
6779
  ELSIF (TG_OP = 'DELETE') THEN
6808
- SELECT p.trade_item_key INTO v_old_key FROM public.products p WHERE p.id = OLD.product_id;
6780
+ v_old_key := public.voorraad_orderregel_sleutel(OLD.product_id::text);
6809
6781
  PERFORM public.recompute_reserved_stock(v_old_key);
6810
6782
  RETURN OLD;
6811
6783
  END IF;
@@ -7887,6 +7859,24 @@ REVOKE ALL ON FUNCTION voorraad_article_vbn(uuid) FROM PUBLIC;
7887
7859
  REVOKE ALL ON FUNCTION voorraad_article_vbn(uuid) FROM anon;
7888
7860
  GRANT EXECUTE ON FUNCTION voorraad_article_vbn(uuid) TO authenticated;
7889
7861
  GRANT EXECUTE ON FUNCTION voorraad_article_vbn(uuid) TO service_role;
7862
+ CREATE OR REPLACE FUNCTION public.voorraad_artikel_prijs(p_id text)
7863
+ RETURNS TABLE(product_id uuid, sale_price numeric, unit text)
7864
+ LANGUAGE plpgsql
7865
+ STABLE SECURITY DEFINER
7866
+ SET search_path TO 'public'
7867
+ AS $function$
7868
+ #variable_conflict use_column
7869
+ DECLARE v_pid uuid;
7870
+ BEGIN
7871
+ PERFORM public.voorraad_vereis('ingelogd', 'voorraad_artikel_prijs');
7872
+ SELECT c.product_id INTO v_pid FROM public.voorraad_canoniek_artikel(p_id) c;
7873
+ RETURN QUERY SELECT p.id, p.sale_price::numeric, p.unit::text FROM public.products p WHERE p.id = v_pid;
7874
+ END $function$
7875
+ ;
7876
+ REVOKE ALL ON FUNCTION voorraad_artikel_prijs(text) FROM PUBLIC;
7877
+ REVOKE ALL ON FUNCTION voorraad_artikel_prijs(text) FROM anon;
7878
+ GRANT EXECUTE ON FUNCTION voorraad_artikel_prijs(text) TO authenticated;
7879
+ GRANT EXECUTE ON FUNCTION voorraad_artikel_prijs(text) TO service_role;
7890
7880
  CREATE OR REPLACE FUNCTION public.voorraad_can_write()
7891
7881
  RETURNS boolean
7892
7882
  LANGUAGE sql
@@ -7906,6 +7896,81 @@ REVOKE ALL ON FUNCTION voorraad_can_write() FROM PUBLIC;
7906
7896
  REVOKE ALL ON FUNCTION voorraad_can_write() FROM anon;
7907
7897
  GRANT EXECUTE ON FUNCTION voorraad_can_write() TO authenticated;
7908
7898
  GRANT EXECUTE ON FUNCTION voorraad_can_write() TO service_role;
7899
+ CREATE OR REPLACE FUNCTION public.voorraad_canoniek_artikel(p_id text)
7900
+ RETURNS TABLE(product_id uuid, trade_item_key text)
7901
+ LANGUAGE plpgsql
7902
+ STABLE SECURITY DEFINER
7903
+ SET search_path TO 'public'
7904
+ AS $function$
7905
+ #variable_conflict use_column
7906
+ DECLARE v_raw text; v_ids uuid[]; v_uuid uuid; v_pid uuid;
7907
+ BEGIN
7908
+ PERFORM public.voorraad_vereis('ingelogd', 'voorraad_canoniek_artikel');
7909
+ v_raw := btrim(COALESCE(p_id, ''));
7910
+ IF v_raw = '' THEN
7911
+ RAISE EXCEPTION 'Geen artikel opgegeven' USING ERRCODE = 'VR011';
7912
+ END IF;
7913
+ IF v_raw LIKE 'product:%' THEN v_raw := substring(v_raw from 9); END IF;
7914
+
7915
+ -- a. Eigen voorraadartikel met dit Floriday-id wint, ook als products.id = p_id een ander (spiegel)product is.
7916
+ SELECT array_agg(p.id ORDER BY p.id) INTO v_ids FROM public.products p WHERE p.floriday_trade_item_id = v_raw;
7917
+ IF COALESCE(array_length(v_ids, 1), 0) > 1 THEN
7918
+ RAISE EXCEPTION 'Meerdere artikelen hebben Floriday-id %: %. Er wordt niets gekozen; koppel er één los.',
7919
+ v_raw, (SELECT string_agg(COALESCE(p.article_number, '?') || ' (' || p.id || ')', ', ' ORDER BY p.id) FROM public.products p WHERE p.id = ANY(v_ids))
7920
+ USING ERRCODE = 'VR010';
7921
+ END IF;
7922
+ v_pid := v_ids[1];
7923
+
7924
+ -- b. Anders products.id = p_id.
7925
+ IF v_pid IS NULL THEN
7926
+ BEGIN v_uuid := v_raw::uuid; EXCEPTION WHEN others THEN v_uuid := NULL; END;
7927
+ IF v_uuid IS NOT NULL THEN SELECT p.id INTO v_pid FROM public.products p WHERE p.id = v_uuid; END IF;
7928
+ END IF;
7929
+
7930
+ -- Terugval voor vrije sleutels (bijv. 'draft:…'): products.trade_item_key.
7931
+ IF v_pid IS NULL THEN
7932
+ SELECT array_agg(p.id ORDER BY p.id) INTO v_ids FROM public.products p WHERE p.trade_item_key = btrim(p_id);
7933
+ IF COALESCE(array_length(v_ids, 1), 0) > 1 THEN
7934
+ RAISE EXCEPTION 'Meerdere artikelen hebben voorraadsleutel %. Er wordt niets gekozen.', btrim(p_id) USING ERRCODE = 'VR010';
7935
+ END IF;
7936
+ v_pid := v_ids[1];
7937
+ END IF;
7938
+
7939
+ IF v_pid IS NULL THEN
7940
+ RAISE EXCEPTION 'Geen artikel gevonden voor %', btrim(p_id) USING ERRCODE = 'VR011';
7941
+ END IF;
7942
+ RETURN QUERY SELECT v_pid, public.voorraad_canoniek_sleutel(v_pid);
7943
+ END $function$
7944
+ ;
7945
+ REVOKE ALL ON FUNCTION voorraad_canoniek_artikel(text) FROM PUBLIC;
7946
+ REVOKE ALL ON FUNCTION voorraad_canoniek_artikel(text) FROM anon;
7947
+ GRANT EXECUTE ON FUNCTION voorraad_canoniek_artikel(text) TO authenticated;
7948
+ GRANT EXECUTE ON FUNCTION voorraad_canoniek_artikel(text) TO service_role;
7949
+ CREATE OR REPLACE FUNCTION public.voorraad_canoniek_sleutel(p_product_id uuid)
7950
+ RETURNS text
7951
+ LANGUAGE plpgsql
7952
+ STABLE SECURITY DEFINER
7953
+ SET search_path TO 'public'
7954
+ AS $function$
7955
+ DECLARE v_tk text; v_fl text;
7956
+ BEGIN
7957
+ PERFORM public.voorraad_vereis('ingelogd', 'voorraad_canoniek_sleutel');
7958
+ SELECT NULLIF(btrim(COALESCE(p.trade_item_key,'')),''), NULLIF(btrim(COALESCE(p.floriday_trade_item_id,'')),'')
7959
+ INTO v_tk, v_fl FROM public.products p WHERE p.id = p_product_id;
7960
+ IF NOT FOUND THEN RETURN NULL; END IF;
7961
+ IF v_tk IS NOT NULL AND EXISTS (SELECT 1 FROM public.trade_item_stock s WHERE s.trade_item_key = v_tk AND s.archived_at IS NULL) THEN
7962
+ RETURN v_tk;
7963
+ END IF;
7964
+ IF v_fl IS NOT NULL AND EXISTS (SELECT 1 FROM public.trade_item_stock s WHERE s.trade_item_key = v_fl AND s.archived_at IS NULL) THEN
7965
+ RETURN v_fl;
7966
+ END IF;
7967
+ RETURN COALESCE(v_fl, v_tk, p_product_id::text);
7968
+ END $function$
7969
+ ;
7970
+ REVOKE ALL ON FUNCTION voorraad_canoniek_sleutel(uuid) FROM PUBLIC;
7971
+ REVOKE ALL ON FUNCTION voorraad_canoniek_sleutel(uuid) FROM anon;
7972
+ GRANT EXECUTE ON FUNCTION voorraad_canoniek_sleutel(uuid) TO authenticated;
7973
+ GRANT EXECUTE ON FUNCTION voorraad_canoniek_sleutel(uuid) TO service_role;
7909
7974
  CREATE OR REPLACE FUNCTION public.voorraad_cleanup_old_rows()
7910
7975
  RETURNS jsonb
7911
7976
  LANGUAGE plpgsql
@@ -8639,6 +8704,71 @@ END $function$
8639
8704
  REVOKE ALL ON FUNCTION voorraad_correctie_1127(boolean) FROM PUBLIC;
8640
8705
  REVOKE ALL ON FUNCTION voorraad_correctie_1127(boolean) FROM anon;
8641
8706
  GRANT EXECUTE ON FUNCTION voorraad_correctie_1127(boolean) TO service_role;
8707
+ CREATE OR REPLACE FUNCTION public.voorraad_correctie_1131(p_uitvoeren boolean DEFAULT false)
8708
+ RETURNS jsonb
8709
+ LANGUAGE plpgsql
8710
+ SECURITY DEFINER
8711
+ SET search_path TO 'public'
8712
+ AS $function$
8713
+ DECLARE v_rows jsonb; v_result jsonb;
8714
+ BEGIN
8715
+ PERFORM public.voorraad_vereis('server', 'voorraad_correctie_1131');
8716
+ IF p_uitvoeren THEN
8717
+ RAISE EXCEPTION 'Correctie 1131 is alleen als overzicht vrijgegeven; uitvoeren wacht op akkoord' USING ERRCODE = 'VR012';
8718
+ END IF;
8719
+ SELECT COALESCE(jsonb_agg(jsonb_build_object(
8720
+ 'artikelnummer', c.article_number,
8721
+ 'voorraadartikel', c.id, 'canonieke_sleutel', public.voorraad_canoniek_sleutel(c.id),
8722
+ 'spiegelproduct', m.id, 'spiegel_bron', m.custom_fields->>'source',
8723
+ 'spiegel_herkend_als_verkoop', (m.custom_fields->>'source') = 'sales-module-floriday-mirror',
8724
+ 'canonieke_regel', (SELECT to_jsonb(s) - 'notes' FROM public.trade_item_stock s
8725
+ WHERE s.trade_item_key = public.voorraad_canoniek_sleutel(c.id) AND s.archived_at IS NULL LIMIT 1),
8726
+ 'spiegelregels', (SELECT jsonb_agg(to_jsonb(s) - 'notes' ORDER BY s.id) FROM public.trade_item_stock s
8727
+ WHERE s.trade_item_key IN (m.id::text, m.trade_item_key, 'product:' || m.id::text)
8728
+ AND s.trade_item_key IS DISTINCT FROM public.voorraad_canoniek_sleutel(c.id)
8729
+ AND s.archived_at IS NULL),
8730
+ 'voorstel', 'reserveringen opnieuw berekenen zodat ze op de canonieke regel landen; spiegelregel archiveren (archived_at, archived_into = canonieke sleutel); fysieke voorraad van de spiegel niet optellen, alleen loggen; spiegelproduct verdwijnt uit voorraadlijst en tellingen; niets verwijderen'
8731
+ ) ORDER BY c.article_number), '[]'::jsonb)
8732
+ INTO v_rows
8733
+ FROM public.products m
8734
+ JOIN public.products c ON c.floriday_trade_item_id = m.id::text AND c.id <> m.id;
8735
+ v_result := jsonb_build_object('ok', true, 'status', 'voorstel', 'artikelen', v_rows);
8736
+ INSERT INTO public.voorraad_correcties(correctie, uitgevoerd_door, proef, aantallen, hash_ok)
8737
+ VALUES ('voorraad_correctie_1131', COALESCE(auth.uid()::text, session_user::text), true, v_result, true);
8738
+ RETURN v_result;
8739
+ END $function$
8740
+ ;
8741
+ REVOKE ALL ON FUNCTION voorraad_correctie_1131(boolean) FROM PUBLIC;
8742
+ REVOKE ALL ON FUNCTION voorraad_correctie_1131(boolean) FROM anon;
8743
+ GRANT EXECUTE ON FUNCTION voorraad_correctie_1131(boolean) TO service_role;
8744
+ CREATE OR REPLACE FUNCTION public.voorraad_correctie_1132(p_uitvoeren boolean DEFAULT false)
8745
+ RETURNS jsonb
8746
+ LANGUAGE plpgsql
8747
+ SECURITY DEFINER
8748
+ SET search_path TO 'public'
8749
+ AS $function$
8750
+ DECLARE pid constant uuid := '27612767-2b14-468c-b169-a8f9a4abe053'; p record; v_status text; v_result jsonb; v_after numeric;
8751
+ BEGIN
8752
+ PERFORM public.voorraad_vereis('server', 'voorraad_correctie_1132');
8753
+ PERFORM pg_advisory_xact_lock(hashtextextended('voorraad_correctie_1132', 0));
8754
+ SELECT * INTO p FROM public.products WHERE id = pid FOR UPDATE;
8755
+ IF p.id IS NULL OR p.article_number IS DISTINCT FROM 'TQA2810A' THEN v_status := 'overgeslagen: artikel wijkt af';
8756
+ ELSIF p.sale_price = 1.40 THEN v_status := 'al uitgevoerd';
8757
+ ELSIF p.sale_price IS DISTINCT FROM 1.25 THEN v_status := 'overgeslagen: prijs gewijzigd';
8758
+ ELSE v_status := CASE WHEN p_uitvoeren THEN 'uitgevoerd' ELSE 'voorstel' END; END IF;
8759
+ IF v_status = 'uitgevoerd' THEN
8760
+ UPDATE public.products SET sale_price = 1.40, updated_at = now() WHERE id = pid;
8761
+ END IF;
8762
+ SELECT sale_price INTO v_after FROM public.products WHERE id = pid;
8763
+ v_result := jsonb_build_object('ok', v_status NOT LIKE 'overgeslagen%', 'status', v_status, 'product', pid, 'voor', p.sale_price, 'na', v_after);
8764
+ INSERT INTO public.voorraad_correcties(correctie, uitgevoerd_door, proef, aantallen, hash_ok)
8765
+ VALUES ('voorraad_correctie_1132', COALESCE(auth.uid()::text, session_user::text), NOT p_uitvoeren, v_result, true);
8766
+ RETURN v_result;
8767
+ END $function$
8768
+ ;
8769
+ REVOKE ALL ON FUNCTION voorraad_correctie_1132(boolean) FROM PUBLIC;
8770
+ REVOKE ALL ON FUNCTION voorraad_correctie_1132(boolean) FROM anon;
8771
+ GRANT EXECUTE ON FUNCTION voorraad_correctie_1132(boolean) TO service_role;
8642
8772
  CREATE OR REPLACE FUNCTION public.voorraad_correctie_status(p_correctie text DEFAULT '1098'::text)
8643
8773
  RETURNS jsonb
8644
8774
  LANGUAGE plpgsql
@@ -8662,6 +8792,33 @@ REVOKE ALL ON FUNCTION voorraad_correctie_status(text) FROM PUBLIC;
8662
8792
  REVOKE ALL ON FUNCTION voorraad_correctie_status(text) FROM anon;
8663
8793
  GRANT EXECUTE ON FUNCTION voorraad_correctie_status(text) TO authenticated;
8664
8794
  GRANT EXECUTE ON FUNCTION voorraad_correctie_status(text) TO service_role;
8795
+ CREATE OR REPLACE FUNCTION public.voorraad_dubbele_voorraadregels()
8796
+ RETURNS jsonb
8797
+ LANGUAGE plpgsql
8798
+ STABLE SECURITY DEFINER
8799
+ SET search_path TO 'public'
8800
+ AS $function$
8801
+ DECLARE v jsonb;
8802
+ BEGIN
8803
+ PERFORM public.voorraad_vereis('ingelogd', 'voorraad_dubbele_voorraadregels');
8804
+ SELECT COALESCE(jsonb_agg(x ORDER BY x->>'floriday_id'), '[]'::jsonb) INTO v FROM (
8805
+ SELECT jsonb_build_object('floriday_id', p.floriday_trade_item_id, 'product_id', p.id, 'artikelnummer', p.article_number,
8806
+ 'waarschuwing', 'meer dan één voorraadregel voor dit Floriday-id',
8807
+ 'regels', jsonb_agg(jsonb_build_object('id', s.id, 'sleutel', s.trade_item_key, 'fysiek', s.physical_stock,
8808
+ 'gereserveerd', s.reserved_stock, 'inkomend', s.incoming_stock) ORDER BY s.id)) x
8809
+ FROM public.products p
8810
+ JOIN public.trade_item_stock s ON s.archived_at IS NULL
8811
+ AND s.trade_item_key IN (p.floriday_trade_item_id, p.trade_item_key, 'product:' || p.id::text, p.id::text)
8812
+ WHERE NULLIF(btrim(COALESCE(p.floriday_trade_item_id,'')),'') IS NOT NULL
8813
+ GROUP BY p.id, p.floriday_trade_item_id, p.article_number, s.connection_id
8814
+ HAVING count(DISTINCT s.trade_item_key) > 1) q;
8815
+ RETURN v;
8816
+ END $function$
8817
+ ;
8818
+ REVOKE ALL ON FUNCTION voorraad_dubbele_voorraadregels() FROM PUBLIC;
8819
+ REVOKE ALL ON FUNCTION voorraad_dubbele_voorraadregels() FROM anon;
8820
+ GRANT EXECUTE ON FUNCTION voorraad_dubbele_voorraadregels() TO authenticated;
8821
+ GRANT EXECUTE ON FUNCTION voorraad_dubbele_voorraadregels() TO service_role;
8665
8822
  CREATE OR REPLACE FUNCTION public.voorraad_external_fulfilled_ids(p_cf jsonb)
8666
8823
  RETURNS text[]
8667
8824
  LANGUAGE sql
@@ -8975,7 +9132,7 @@ CREATE OR REPLACE FUNCTION public.voorraad_install_check()
8975
9132
  STABLE SECURITY DEFINER
8976
9133
  SET search_path TO 'public'
8977
9134
  AS $function$
8978
- DECLARE v_f text[] := ARRAY['voorraad_gateway_routes_check','voorraad_placeholder_code_reason','trg_products_placeholder_code','voorraad_fust_juist','voorraad_correctie_1121','voorraad_correctie_1121_regels','voorraad_correctie_1123','voorraad_correctie_1124','voorraad_load_carriers','voorraad_packing_from_floriday','voorraad_packing_missing','voorraad_correctie_1126','voorraad_correctie_1127','trg_voorraad_link_primary_packing','voorraad_order_geannuleerd','voorraad_article_vbn','voorraad_correctie_1112','voorraad_reverse_order_mutations','voorraad_withdraw_stock_row','ensure_voorraad_local_context','get_stock_context','voorraad_stock_context_status','voorraad_fcsq_local_guard','_floriday_queue_cleanup_batch','_migrate_rewrite_stock_key','_stock_import_caller','apply_fust_stock_mutation','apply_stock_consumption','apply_stock_reservation','autolink_product_to_floriday','backfill_open_order_reservations','calculate_floriday_desired_stock','calculate_structure_available','canonicalize_trade_item_stock_key','check_product_structure','claim_floriday_base_supply','claim_floriday_stock_queue','cleanup_floriday_stock_queue','cleanup_voorraad_module_history','complete_floriday_base_supply','complete_floriday_stock_queue','complete_floriday_stock_queue_outcome','consume_bom_for_production','consume_order_item','drain_inventory_recompute_queue','enqueue_floriday_continuous_stock','enqueue_floriday_continuous_stock_after_warehouse_preference','enqueue_structure_recompute','ensure_inventory_recompute_drain','ensure_trade_item_stock_row','ensure_trade_item_stock_row_key','fcsq_block_disabled','fcsq_fallback_supersede','fcsq_wake_base_supply','find_trade_item_key','floriday_adopt_connection','floriday_autoheal_connections','floriday_capture_stock_snapshot','floriday_cleanup_stock_queue_v2','floriday_compact_stock_snapshots','floriday_default_owner','floriday_desired_stock_regression_check','floriday_ensure_inventory_settings','floriday_erp_leading_preview','floriday_fill_publish_horizon','floriday_follow_apply','floriday_ingest_sales_orders','floriday_leading_summary','floriday_make_erp_leading','floriday_norm_offer_forms','floriday_orphan_settings_count','floriday_publish_horizon_weeks','floriday_queue_erp_guard','floriday_queue_first_positive','floriday_recompute_inventory','floriday_relink_products','floriday_requeue_dead_letters','floriday_settings_change_recompute','floriday_settings_change_recompute_stmt','floriday_stock_change_recompute','floriday_stock_queue_supersede','floriday_takeover_apply','floriday_takeover_fetch_state','floriday_takeover_store_forms','floriday_takeover_undo','fust_negative_stock_report','fust_types_duplicate_guard','get_active_floriday_context','get_group_allocation_keys','get_group_head_key','get_product_stock','get_product_structure_totals','get_product_structure_totals_batch','inventory_inline_budget_ok','merge_products','normalize_order_source','process_inventory_recompute_queue','produce_designed_product','product_active_group_id','product_id_from_ref','product_is_set','product_label','product_member_group_ids','product_own_available','product_packings_derive','product_packings_replace','product_packings_sync_from_floriday','product_primary_pieces','product_set_depth_above','product_set_depth_below','product_structure_ancestors','product_structure_max_depth','product_structure_path','product_tray_stickers_touch','product_type','promote_trade_item_key','recompute_all_reserved_stock','recompute_order_status','recompute_reserved_for_order','recompute_reserved_stock','requeue_skipped_stock_rows','reset_stalled_floriday_stock_queue','resolve_group_allocation','resolve_group_member_stock_key','resolve_product_id_for_item','resolve_stock_trade_item_key','reverse_production','run_floriday_base_supply_process','run_floriday_continuous_stock_sync','run_floriday_follow','run_floriday_stock_queue_process','run_floriday_stock_reconcile','run_logistics_tasks_dispatch','self_heal_floriday_links','set_module_runtime_config_updated_at','set_product_bom_tenant','set_product_stock','set_updated_at_order_fust_bookings','stock_import_cleanup','stock_import_create','stock_import_process_chunk','stock_import_stop','stock_import_undo_chunk','sync_order_item_fusten','sync_products_quantity_from_stock','touch_fust_types_updated_at','trade_item_drafts_readonly','trade_item_prices_touch_updated_at','trade_item_stock_duplicates','trg_cart_numbers_sync_order_status_fn','trg_cart_session_consume_fn','trg_check_product_structure','trg_floriday_cache_relink_fn','trg_floriday_trade_items_autolink_fn','trg_order_items_autolink_fn','trg_order_items_fusten','trg_order_items_reserve_fn','trg_orders_cancel_release_fn','trg_orders_consume_on_complete_fn','trg_orders_default_phase_fn','trg_orders_external_fulfilled_fn','trg_orders_recompute_reserved_fn','trg_pgi_assign_sort_order','trg_pgi_order_taken_check','trg_process_recompute_on_commit','trg_product_packings_derive_fn','trg_products_autolink_fn','trg_products_floriday_link_enqueue','trg_products_seed_stock_fn','trg_requeue_no_warehouse_cache','trg_requeue_no_warehouse_connection','trg_requeue_no_warehouse_item_pref','trg_requeue_skipped_no_price','trg_stock_structure_recompute_ins','trg_stock_structure_recompute_upd','trg_structure_change_recompute','trg_trade_item_drafts_autolink_fn','trg_trade_item_stock_auto_bom','trg_trade_item_stock_auto_bom_reverse','trg_trade_item_stock_log_change','trg_trade_item_stock_reserve_fn','trg_trade_item_stock_zero_watch','trg_trade_items_cache_autolink_fn','update_updated_at_column','voorraad_alert_floriday_not_updated','voorraad_can_write','voorraad_cleanup_old_rows','voorraad_consume_external_fulfilled','voorraad_correctie_1098','voorraad_correctie_status','voorraad_external_fulfilled_ids','voorraad_floriday_leads_key','voorraad_fust_dimensions','voorraad_hook_base_url','voorraad_hook_on','voorraad_hook_post','voorraad_hook_switch_jobs','voorraad_install_check','voorraad_is_server','voorraad_mark_floriday_sold_before_switch','voorraad_production_enabled','voorraad_schema_ddl','voorraad_touch_updated_at','voorraad_vereis','voorraad_week_is_past'];
9135
+ DECLARE v_f text[] := ARRAY['voorraad_canoniek_artikel','voorraad_canoniek_sleutel','voorraad_artikel_prijs','voorraad_dubbele_voorraadregels','voorraad_correctie_1131','voorraad_correctie_1132','voorraad_gateway_routes_check','voorraad_placeholder_code_reason','trg_products_placeholder_code','voorraad_fust_juist','voorraad_correctie_1121','voorraad_correctie_1121_regels','voorraad_correctie_1123','voorraad_correctie_1124','voorraad_load_carriers','voorraad_packing_from_floriday','voorraad_packing_missing','voorraad_correctie_1126','voorraad_correctie_1127','trg_voorraad_link_primary_packing','voorraad_order_geannuleerd','voorraad_article_vbn','voorraad_correctie_1112','voorraad_reverse_order_mutations','voorraad_withdraw_stock_row','ensure_voorraad_local_context','get_stock_context','voorraad_stock_context_status','voorraad_fcsq_local_guard','_floriday_queue_cleanup_batch','_migrate_rewrite_stock_key','_stock_import_caller','apply_fust_stock_mutation','apply_stock_consumption','apply_stock_reservation','autolink_product_to_floriday','backfill_open_order_reservations','calculate_floriday_desired_stock','calculate_structure_available','canonicalize_trade_item_stock_key','check_product_structure','claim_floriday_base_supply','claim_floriday_stock_queue','cleanup_floriday_stock_queue','cleanup_voorraad_module_history','complete_floriday_base_supply','complete_floriday_stock_queue','complete_floriday_stock_queue_outcome','consume_bom_for_production','consume_order_item','drain_inventory_recompute_queue','enqueue_floriday_continuous_stock','enqueue_floriday_continuous_stock_after_warehouse_preference','enqueue_structure_recompute','ensure_inventory_recompute_drain','ensure_trade_item_stock_row','ensure_trade_item_stock_row_key','fcsq_block_disabled','fcsq_fallback_supersede','fcsq_wake_base_supply','find_trade_item_key','floriday_adopt_connection','floriday_autoheal_connections','floriday_capture_stock_snapshot','floriday_cleanup_stock_queue_v2','floriday_compact_stock_snapshots','floriday_default_owner','floriday_desired_stock_regression_check','floriday_ensure_inventory_settings','floriday_erp_leading_preview','floriday_fill_publish_horizon','floriday_follow_apply','floriday_ingest_sales_orders','floriday_leading_summary','floriday_make_erp_leading','floriday_norm_offer_forms','floriday_orphan_settings_count','floriday_publish_horizon_weeks','floriday_queue_erp_guard','floriday_queue_first_positive','floriday_recompute_inventory','floriday_relink_products','floriday_requeue_dead_letters','floriday_settings_change_recompute','floriday_settings_change_recompute_stmt','floriday_stock_change_recompute','floriday_stock_queue_supersede','floriday_takeover_apply','floriday_takeover_fetch_state','floriday_takeover_store_forms','floriday_takeover_undo','fust_negative_stock_report','fust_types_duplicate_guard','get_active_floriday_context','get_group_allocation_keys','get_group_head_key','get_product_stock','get_product_structure_totals','get_product_structure_totals_batch','inventory_inline_budget_ok','merge_products','normalize_order_source','process_inventory_recompute_queue','produce_designed_product','product_active_group_id','product_id_from_ref','product_is_set','product_label','product_member_group_ids','product_own_available','product_packings_derive','product_packings_replace','product_packings_sync_from_floriday','product_primary_pieces','product_set_depth_above','product_set_depth_below','product_structure_ancestors','product_structure_max_depth','product_structure_path','product_tray_stickers_touch','product_type','promote_trade_item_key','recompute_all_reserved_stock','recompute_order_status','recompute_reserved_for_order','recompute_reserved_stock','requeue_skipped_stock_rows','reset_stalled_floriday_stock_queue','resolve_group_allocation','resolve_group_member_stock_key','resolve_product_id_for_item','resolve_stock_trade_item_key','reverse_production','run_floriday_base_supply_process','run_floriday_continuous_stock_sync','run_floriday_follow','run_floriday_stock_queue_process','run_floriday_stock_reconcile','run_logistics_tasks_dispatch','self_heal_floriday_links','set_module_runtime_config_updated_at','set_product_bom_tenant','set_product_stock','set_updated_at_order_fust_bookings','stock_import_cleanup','stock_import_create','stock_import_process_chunk','stock_import_stop','stock_import_undo_chunk','sync_order_item_fusten','sync_products_quantity_from_stock','touch_fust_types_updated_at','trade_item_drafts_readonly','trade_item_prices_touch_updated_at','trade_item_stock_duplicates','trg_cart_numbers_sync_order_status_fn','trg_cart_session_consume_fn','trg_check_product_structure','trg_floriday_cache_relink_fn','trg_floriday_trade_items_autolink_fn','trg_order_items_autolink_fn','trg_order_items_fusten','trg_order_items_reserve_fn','trg_orders_cancel_release_fn','trg_orders_consume_on_complete_fn','trg_orders_default_phase_fn','trg_orders_external_fulfilled_fn','trg_orders_recompute_reserved_fn','trg_pgi_assign_sort_order','trg_pgi_order_taken_check','trg_process_recompute_on_commit','trg_product_packings_derive_fn','trg_products_autolink_fn','trg_products_floriday_link_enqueue','trg_products_seed_stock_fn','trg_requeue_no_warehouse_cache','trg_requeue_no_warehouse_connection','trg_requeue_no_warehouse_item_pref','trg_requeue_skipped_no_price','trg_stock_structure_recompute_ins','trg_stock_structure_recompute_upd','trg_structure_change_recompute','trg_trade_item_drafts_autolink_fn','trg_trade_item_stock_auto_bom','trg_trade_item_stock_auto_bom_reverse','trg_trade_item_stock_log_change','trg_trade_item_stock_reserve_fn','trg_trade_item_stock_zero_watch','trg_trade_items_cache_autolink_fn','update_updated_at_column','voorraad_alert_floriday_not_updated','voorraad_can_write','voorraad_cleanup_old_rows','voorraad_consume_external_fulfilled','voorraad_correctie_1098','voorraad_correctie_status','voorraad_external_fulfilled_ids','voorraad_floriday_leads_key','voorraad_fust_dimensions','voorraad_hook_base_url','voorraad_hook_on','voorraad_hook_post','voorraad_hook_switch_jobs','voorraad_install_check','voorraad_is_server','voorraad_mark_floriday_sold_before_switch','voorraad_production_enabled','voorraad_schema_ddl','voorraad_touch_updated_at','voorraad_vereis','voorraad_week_is_past'];
8979
9136
  v_t text[] := ARRAY['trg_voorraad_link_primary_packing@products','aa0_fcsq_local_context_guard@floriday_continuous_stock_queue','aa_fcsq_erp_leading_guard@floriday_continuous_stock_queue','aa_fis_norm_offer_forms@floriday_inventory_settings','aa_pgi_assign_sort_order@product_group_items','fcsq_block_disabled@floriday_continuous_stock_queue','fcsq_wake_base_supply_ins@floriday_continuous_stock_queue','fcsq_wake_base_supply_upd@floriday_continuous_stock_queue','fust_stock_mutations_apply@fust_stock_mutations','fust_types_duplicate_guard@fust_types','fust_types_touch@fust_types','product_packings_derive_trg@product_packings','product_packings_updated_at@product_packings','set_product_bom_tenant_trigger@product_bom_items','trade_item_prices_set_updated_at@trade_item_prices','trg_cart_numbers_sync_order_status@cart_numbers','trg_cart_session_consume@cart_control_sessions','trg_check_product_structure@product_bom_items','trg_check_product_structure@product_group_items','trg_check_product_structure@product_groups','trg_fcs_queue_updated_at@floriday_continuous_stock_queue','trg_fcsq_fallback_supersede@floriday_continuous_stock_queue','trg_fcsq_supersede@floriday_continuous_stock_queue','trg_floriday_cache_relink@floriday_trade_items_cache','trg_floriday_ensure_inventory_settings@products','trg_floriday_gate_stock_queue@floriday_continuous_stock_queue','trg_floriday_settings_change_recompute_ins@floriday_inventory_settings','trg_floriday_settings_change_recompute_upd@floriday_inventory_settings','trg_floriday_stock_change_recompute@trade_item_stock','trg_floriday_takeover_runs_updated_at@floriday_takeover_runs','trg_floriday_trade_items_autolink@floriday_trade_items_cache','trg_fust_types_requeue_blocked@fust_types','trg_module_runtime_config_updated_at@module_runtime_config','trg_order_fust_bookings_updated_at@order_fust_bookings','trg_order_items_autolink@order_items','trg_order_items_fusten_aiud@order_items','trg_order_items_reserve@order_items','trg_orders_cancel_release@orders','trg_orders_consume_on_complete@orders','trg_orders_default_phase@orders','trg_orders_external_fulfilled@orders','trg_orders_recompute_reserved@orders','trg_prevent_duplicate_product_draft@trade_item_drafts','trg_product_tray_stickers_touch@product_tray_stickers','trg_products_autolink@products','trg_products_floriday_link_enqueue@products','trg_products_seed_stock@products','trg_requeue_no_warehouse_cache@floriday_trade_items_cache','trg_requeue_no_warehouse_connection@floriday_connections','trg_requeue_no_warehouse_item_pref@trade_item_warehouse_preferences','trg_requeue_skipped_no_price@products','trg_stock_mutation_alert@stock_mutation_log','trg_stock_structure_recompute_ins@trade_item_stock','trg_stock_structure_recompute_upd@trade_item_stock','trg_structure_change_recompute@product_bom_items','trg_structure_change_recompute@product_group_items','trg_structure_change_recompute@product_groups','trg_sync_layer_stock_from_trade_stock@trade_item_stock','trg_sync_products_quantity_from_stock_del@trade_item_stock','trg_sync_products_quantity_from_stock_iu@trade_item_stock','trg_trade_item_drafts_autolink@trade_item_drafts','trg_trade_item_drafts_readonly@trade_item_drafts','trg_trade_item_stock_canonical_key@trade_item_stock','trg_trade_item_stock_publish@trade_item_stock','trg_trade_item_stock_reserve@trade_item_stock','trg_trade_item_stock_updated_at@trade_item_stock','trg_trade_item_stock_zero_watch@trade_item_stock','trg_trade_item_supply_lines_touch@trade_item_supply_lines','trg_trade_item_warehouse_preference_enqueue_stock@trade_item_warehouse_preferences','trg_trade_items_cache_autolink@floriday_trade_items_cache','trg_voorraad_buffer_state_updated_at@voorraad_buffer_state','trg_voorraad_event_log_updated_at@voorraad_logistics_event_log','update_trade_item_drafts_updated_at@trade_item_drafts','update_trade_item_warehouse_preferences_updated_at@trade_item_warehouse_preferences','update_voorraad_custom_fields_updated_at@voorraad_custom_fields','update_voorraad_publish_jobs_updated_at@voorraad_publish_jobs','update_voorraad_publish_learnings_updated_at@voorraad_publish_learnings','update_voorraad_publish_steps_updated_at@voorraad_publish_steps','voorraad_hook_switch_jobs@module_runtime_config','zz_fcsq_first_positive@floriday_continuous_stock_queue'];
8980
9137
  v_p text[] := ARRAY['Authenticated can add learnings@voorraad_publish_learnings','Authenticated can delete voorraad custom fields@voorraad_custom_fields','Authenticated can insert vbn required characteristics@floriday_vbn_required_characteristics','Authenticated can insert voorraad custom fields@voorraad_custom_fields','Authenticated can read continuous stock cache@floriday_continuous_stock_cache','Authenticated can read learnings@voorraad_publish_learnings','Authenticated can read vbn required characteristics@floriday_vbn_required_characteristics','Authenticated can read voorraad custom fields@voorraad_custom_fields','Authenticated can update learnings@voorraad_publish_learnings','Authenticated can update vbn required characteristics@floriday_vbn_required_characteristics','Authenticated can update voorraad custom fields@voorraad_custom_fields','Authenticated delete trade_item_warehouse_preferences@trade_item_warehouse_preferences','Authenticated insert trade_item_warehouse_preferences@trade_item_warehouse_preferences','Authenticated select trade_item_warehouse_preferences@trade_item_warehouse_preferences','Authenticated update trade_item_warehouse_preferences@trade_item_warehouse_preferences','Eigen momentopnamen lezen@inventory_stock_snapshots','Eigen voorraadinstellingen beheren@floriday_inventory_settings','Eigenaar leest importporties@stock_import_chunks','Eigenaar leest importregels@stock_import_rows','Fust mutations delete@fust_stock_mutations','Fust mutations insert@fust_stock_mutations','Fust mutations read@fust_stock_mutations','Fust mutations update@fust_stock_mutations','Fust types delete@fust_types','Fust types insert@fust_types','Fust types read@fust_types','Fust types update@fust_types','Ingelogde gebruikers kunnen bufferstatus lezen@voorraad_buffer_state','Ingelogde gebruikers kunnen de eventlog lezen@voorraad_logistics_event_log','Ingelogde gebruikers kunnen fustboekingen lezen@order_fust_bookings','Ingelogde gebruikers lezen drempels@inventory_recompute_limits','Ingelogde gebruikers lezen opruimlogboek@structure_cleanup_log','Ingelogde gebruikers lezen opruimruns@floriday_queue_cleanup_runs','Ingelogden beheren traystickers@product_tray_stickers','Ingelogden lezen herberekenwachtrij@inventory_recompute_queue','Ingelogden lezen voorraadmeldingen@stock_integrity_alerts','Ingelogden markeren voorraadmeldingen als gezien@stock_integrity_alerts','Own queue rows select@floriday_continuous_stock_queue','Owner delete trade_item_drafts@trade_item_drafts','Owner insert trade_item_drafts@trade_item_drafts','Owner select trade_item_drafts@trade_item_drafts','Owner update trade_item_drafts@trade_item_drafts','Signed-in users read reconcile runs@floriday_reconcile_runs','Stock delete@trade_item_stock','Stock insert@trade_item_stock','Stock read@trade_item_stock','Stock update@trade_item_stock','Synchronisatieposities lezen@floriday_sync_cursors','Users can insert own publish attempts@voorraad_publish_attempts','Users can read own publish attempts@voorraad_publish_attempts','Users can read own stock mutations@stock_mutation_log','Users manage own import runs@stock_import_runs','Users manage own supply lines@trade_item_supply_lines','Users manage own trade item prices@trade_item_prices','Users manage their own publish jobs@voorraad_publish_jobs','Users manage their own publish steps@voorraad_publish_steps','Verwerkte orders lezen@floriday_sales_order_ingest','Voorraadschrijvers lezen overnameregels@floriday_takeover_items','Voorraadschrijvers lezen overnames@floriday_takeover_runs','authenticated can insert merge log@product_merge_log','authenticated can read merge log@product_merge_log','service role only@voorraad_media_upload_failures','product_packings_read@product_packings','product_packings_write@product_packings'];
8981
9138
  v_open text[] := ARRAY['voorraad_is_server','voorraad_vereis'];
@@ -9003,7 +9160,7 @@ BEGIN
9003
9160
  SELECT jsonb_agg(jsonb_build_object('policy', split_part(t, '@', 1), 'tabel', split_part(t, '@', 2),
9004
9161
  'aanwezig', EXISTS (SELECT 1 FROM pg_policies x WHERE x.schemaname = 'public' AND x.policyname = split_part(t, '@', 1)
9005
9162
  AND x.tablename = split_part(t, '@', 2))) ORDER BY t) INTO v_pol FROM unnest(v_p) t;
9006
- RETURN jsonb_build_object('gateway_routes', public.voorraad_gateway_routes_check(),
9163
+ RETURN jsonb_build_object('dubbele_voorraadregels', public.voorraad_dubbele_voorraadregels(), 'gateway_routes', public.voorraad_gateway_routes_check(),
9007
9164
  'samenvatting', jsonb_build_object(
9008
9165
  'functies', jsonb_array_length(v_func),
9009
9166
  'functies_ontbrekend', (SELECT count(*) FROM jsonb_array_elements(v_func) e WHERE NOT (e->>'aanwezig')::boolean),
@@ -9101,6 +9258,28 @@ GRANT EXECUTE ON FUNCTION voorraad_order_geannuleerd(text,timestamp with time zo
9101
9258
  REVOKE EXECUTE ON FUNCTION voorraad_order_geannuleerd(text,timestamp with time zone,timestamp with time zone) FROM anon;
9102
9259
  GRANT EXECUTE ON FUNCTION voorraad_order_geannuleerd(text,timestamp with time zone,timestamp with time zone) TO authenticated;
9103
9260
  GRANT EXECUTE ON FUNCTION voorraad_order_geannuleerd(text,timestamp with time zone,timestamp with time zone) TO service_role;
9261
+ CREATE OR REPLACE FUNCTION public.voorraad_orderregel_sleutel(p_ref text)
9262
+ RETURNS text
9263
+ LANGUAGE plpgsql
9264
+ STABLE SECURITY DEFINER
9265
+ SET search_path TO 'public'
9266
+ AS $function$
9267
+ DECLARE v_key text;
9268
+ BEGIN
9269
+ PERFORM public.voorraad_vereis('ingelogd', 'voorraad_orderregel_sleutel');
9270
+ IF p_ref IS NULL OR btrim(p_ref) = '' THEN RETURN NULL; END IF;
9271
+ BEGIN
9272
+ SELECT c.trade_item_key INTO v_key FROM public.voorraad_canoniek_artikel(p_ref) c;
9273
+ EXCEPTION WHEN others THEN
9274
+ RETURN NULL;
9275
+ END;
9276
+ RETURN v_key;
9277
+ END $function$
9278
+ ;
9279
+ REVOKE ALL ON FUNCTION voorraad_orderregel_sleutel(text) FROM PUBLIC;
9280
+ REVOKE ALL ON FUNCTION voorraad_orderregel_sleutel(text) FROM anon;
9281
+ GRANT EXECUTE ON FUNCTION voorraad_orderregel_sleutel(text) TO authenticated;
9282
+ GRANT EXECUTE ON FUNCTION voorraad_orderregel_sleutel(text) TO service_role;
9104
9283
  CREATE OR REPLACE FUNCTION public.voorraad_packing_from_floriday(p_product_id uuid, p_uitvoeren boolean)
9105
9284
  RETURNS jsonb
9106
9285
  LANGUAGE plpgsql