@flowselections/floriday-klanten-module 1.0.75 → 1.0.79

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 (33) hide show
  1. package/dist-lib/components/klanten/DeliveryLocationsTab.d.ts.map +1 -1
  2. package/dist-lib/components/klanten/DeliveryLocationsTab.js +34 -5
  3. package/dist-lib/components/klanten/KlantenOverzicht.d.ts.map +1 -1
  4. package/dist-lib/components/klanten/KlantenOverzicht.js +51 -32
  5. package/dist-lib/components/klanten/LocationGlns.d.ts +20 -0
  6. package/dist-lib/components/klanten/LocationGlns.d.ts.map +1 -0
  7. package/dist-lib/components/klanten/LocationGlns.js +84 -0
  8. package/dist-lib/components/klanten/ui/styleguide.d.ts +58 -0
  9. package/dist-lib/components/klanten/ui/styleguide.d.ts.map +1 -0
  10. package/dist-lib/components/klanten/ui/styleguide.js +88 -0
  11. package/dist-lib/components/settings/ChannelsCard.d.ts +2 -0
  12. package/dist-lib/components/settings/ChannelsCard.d.ts.map +1 -0
  13. package/dist-lib/components/settings/ChannelsCard.js +35 -0
  14. package/dist-lib/index.d.ts.map +1 -1
  15. package/dist-lib/index.js +2 -0
  16. package/dist-lib/integrations/supabase/types.d.ts +2840 -122
  17. package/dist-lib/integrations/supabase/types.d.ts.map +1 -1
  18. package/dist-lib/integrations/supabase/types.js +1 -0
  19. package/dist-lib/lib/channel-providers.d.ts +8 -0
  20. package/dist-lib/lib/channel-providers.d.ts.map +1 -0
  21. package/dist-lib/lib/channel-providers.js +23 -0
  22. package/dist-lib/lib/customer-schemas.d.ts +1 -1
  23. package/dist-lib/lib/customers.functions.d.ts +1 -1
  24. package/dist-lib/lib/delivery-gln.d.ts +58 -0
  25. package/dist-lib/lib/delivery-gln.d.ts.map +1 -0
  26. package/dist-lib/lib/delivery-gln.js +81 -0
  27. package/dist-lib/lib/delivery-locations.functions.d.ts +115 -1
  28. package/dist-lib/lib/delivery-locations.functions.d.ts.map +1 -1
  29. package/dist-lib/lib/delivery-locations.functions.js +124 -4
  30. package/dist-lib/lib/external-customers.functions.d.ts.map +1 -1
  31. package/dist-lib/lib/external-customers.functions.js +18 -15
  32. package/package.json +1 -1
  33. package/schema/klanten.sql +467 -1
@@ -1078,7 +1078,7 @@ BEGIN
1078
1078
  'import_floriday_delivery_locations','refresh_external_customers_grouped','enqueue_twinfield_push'] LOOP
1079
1079
  IF NOT EXISTS (SELECT 1 FROM pg_proc WHERE proname=_o AND pronamespace='public'::regnamespace) THEN _missing := _missing || (_o||'()'); END IF;
1080
1080
  END LOOP;
1081
- FOR _r IN SELECT v.naam, v.niveau, p.oid AS oid, p.prosrc FROM (VALUES ('import_floriday_delivery_locations','ingelogd'),('next_twinfield_code','ingelogd'),('refresh_floriday_delivery_locations','ingelogd'),('reorder_customer_price_groups','ingelogd'),('retry_twinfield_push','ingelogd'),('set_primary_delivery_location','ingelogd'),('sync_floriday_prices_into_price_groups','ingelogd'),('klanten_auto_promote_status','ingelogd'),('klanten_refresh_grouped_if_stale','ingelogd'),('floriday_effective_price','ingelogd'),('has_twinfield_link','ingelogd'),('local_effective_price','ingelogd'),('refresh_external_customers_grouped','ingelogd'),('floriday_customer_candidates','ingelogd'),('floriday_customer_promotion_preview','ingelogd'),('klanten_install_check','ingelogd'),('schedule_floriday_network_sync','beheerder'),('klanten_set_auto_promote','beheerder'),('klanten_promote_round','beheerder'),('promote_floriday_customer','beheerder'),('klanten_ensure_cron','beheerder'),('run_twinfield_queue_tick','server'),('twinfield_sync_apply','server'),('twinfield_sync_insert','server'),('twinfield_sync_mark_missing','server'),('promote_floriday_order_customers','server'),('klanten_correctie_landcodes','server'),('customer_soft_key','open'),('server_now','open'),('klanten_is_server','open'),('klanten_toegang','open'),('klanten_landnaam_kandidaten','open'),('enqueue_twinfield_push','trigger'),('log_customer_changes','trigger'),('tg_customer_price_groups_touch','trigger'),('trg_floriday_delivery_location_import','trigger'),('trg_refresh_external_customers_grouped','trigger'),('validate_twinfield_required_fields','trigger')) v(naam, niveau)
1081
+ FOR _r IN SELECT v.naam, v.niveau, p.oid AS oid, p.prosrc FROM (VALUES ('import_floriday_delivery_locations','ingelogd'),('next_twinfield_code','ingelogd'),('refresh_floriday_delivery_locations','ingelogd'),('reorder_customer_price_groups','ingelogd'),('retry_twinfield_push','ingelogd'),('set_primary_delivery_location','ingelogd'),('sync_floriday_prices_into_price_groups','ingelogd'),('klanten_auto_promote_status','ingelogd'),('klanten_refresh_grouped_if_stale','ingelogd'),('floriday_effective_price','ingelogd'),('has_twinfield_link','ingelogd'),('local_effective_price','ingelogd'),('refresh_external_customers_grouped','ingelogd'),('floriday_customer_candidates','ingelogd'),('floriday_customer_promotion_preview','ingelogd'),('klanten_install_check','ingelogd'),('klanten_gln_backfill_report','ingelogd'),('resolve_delivery_location_by_gln','ingelogd'),('add_delivery_location_gln','ingelogd'),('remove_delivery_location_gln','ingelogd'),('set_primary_delivery_location_gln','ingelogd'),('klanten_locaties_samenvoegen_aan','ingelogd'),('klanten_locatie_zendingen','ingelogd'),('merge_preview','ingelogd'),('merge_customer_delivery_locations','ingelogd'),('delete_customer_delivery_location','ingelogd'),('klanten_set_locaties_samenvoegen','beheerder'),('klanten_gln_norm','open'),('klanten_gln_geldig','open'),('klanten_gln_naar_locatie','trigger'),('klanten_locatie_naar_gln','trigger'),('schedule_floriday_network_sync','beheerder'),('klanten_set_auto_promote','beheerder'),('klanten_promote_round','beheerder'),('promote_floriday_customer','beheerder'),('klanten_ensure_cron','beheerder'),('klanten_set_provider_enabled','beheerder'),('run_twinfield_queue_tick','server'),('twinfield_sync_apply','server'),('twinfield_sync_insert','server'),('twinfield_sync_mark_missing','server'),('promote_floriday_order_customers','server'),('klanten_correctie_landcodes','server'),('customer_soft_key','open'),('server_now','open'),('klanten_is_server','open'),('klanten_toegang','open'),('klanten_landnaam_kandidaten','open'),('enqueue_twinfield_push','trigger'),('log_customer_changes','trigger'),('tg_customer_price_groups_touch','trigger'),('trg_floriday_delivery_location_import','trigger'),('trg_refresh_external_customers_grouped','trigger'),('validate_twinfield_required_fields','trigger')) v(naam, niveau)
1082
1082
  LEFT JOIN pg_proc p ON p.proname = v.naam AND p.pronamespace = 'public'::regnamespace ORDER BY v.niveau, v.naam LOOP
1083
1083
  _f := _f || jsonb_build_object('functie', _r.naam, 'niveau',
1084
1084
  CASE _r.niveau WHEN 'open' THEN 'bewust open (hulpfunctie zonder gegevens)'
@@ -2409,3 +2409,469 @@ SELECT pg_temp.klanten_try($klanten_stmt$DO $cron$ BEGIN
2409
2409
  PERFORM cron.schedule('promote-floriday-order-customers','*/5 * * * *','SELECT public.promote_floriday_order_customers();');
2410
2410
  END IF;
2411
2411
  END $cron$$klanten_stmt$, 'DO $cron$ BEGIN IF EXISTS (SELECT 1 FROM pg_extension WHERE extname=''pg_cron'') AND NOT EXISTS (SELEC');
2412
+ SELECT pg_temp.klanten_try($klanten_stmt$INSERT INTO public.external_customer_providers (provider, label, icon, color, sort_order, is_enabled) VALUES
2413
+ ('floriday','Floriday','/flowselections-assets/Klanten/floriday-logo.png','#00A651',20,true),
2414
+ ('twinfield','Twinfield','/flowselections-assets/Klanten/twinfield-logo.svg','#0057B7',10,true)
2415
+ ON CONFLICT (provider) DO NOTHING$klanten_stmt$, 'INSERT INTO public.external_customer_providers (provider, label, icon, color, sort_order, is_enabled');
2416
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_set_provider_enabled(p_provider text, p_enabled boolean)
2417
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2418
+ BEGIN
2419
+ PERFORM public.klanten_toegang('beheerder');
2420
+ IF p_provider IS NULL OR btrim(p_provider) = '' OR p_enabled IS NULL THEN
2421
+ RAISE EXCEPTION 'Kanaal en stand zijn verplicht';
2422
+ END IF;
2423
+ INSERT INTO public.external_customer_providers (provider, label, is_enabled)
2424
+ VALUES (p_provider, initcap(p_provider), p_enabled)
2425
+ ON CONFLICT (provider) DO UPDATE SET is_enabled = EXCLUDED.is_enabled;
2426
+ RETURN jsonb_build_object('provider', p_provider, 'is_enabled', p_enabled);
2427
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_set_provider_enabled(p_provider text, p_enabled boolean) R');
2428
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.klanten_set_provider_enabled(text, boolean) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.klanten_set_provider_enabled(text, boolean) FROM PUBLIC, anon');
2429
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.klanten_set_provider_enabled(text, boolean) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.klanten_set_provider_enabled(text, boolean) TO authenticated, servi');
2430
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_gln_norm(p text)
2431
+ RETURNS text LANGUAGE sql IMMUTABLE SET search_path = public AS $f$
2432
+ SELECT NULLIF(regexp_replace(COALESCE(p, ''), '[^0-9]', '', 'g'), '')
2433
+ $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_gln_norm(p text) RETURNS text LANGUAGE sql IMMUTABLE SET s');
2434
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_gln_geldig(p text)
2435
+ RETURNS boolean LANGUAGE plpgsql IMMUTABLE SET search_path = public AS $f$
2436
+ DECLARE _s int := 0; _i int;
2437
+ BEGIN
2438
+ IF p IS NULL OR p !~ '^[0-9]{13}$' THEN RETURN false; END IF;
2439
+ FOR _i IN 1..12 LOOP
2440
+ _s := _s + substr(p, _i, 1)::int * CASE WHEN _i % 2 = 0 THEN 3 ELSE 1 END;
2441
+ END LOOP;
2442
+ RETURN (10 - _s % 10) % 10 = substr(p, 13, 1)::int;
2443
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_gln_geldig(p text) RETURNS boolean LANGUAGE plpgsql IMMUTA');
2444
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE TABLE IF NOT EXISTS public.customer_delivery_location_glns (
2445
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
2446
+ customer_id uuid NOT NULL REFERENCES public.customers(id) ON DELETE CASCADE,
2447
+ location_id uuid NOT NULL REFERENCES public.customer_delivery_locations(id) ON DELETE CASCADE,
2448
+ gln text NOT NULL CHECK (gln ~ '^[0-9]{13}$'),
2449
+ is_primary boolean NOT NULL DEFAULT false,
2450
+ floriday_location_id text,
2451
+ source text NOT NULL DEFAULT 'manual' CHECK (source IN ('manual','floriday','merge')),
2452
+ removed_at timestamptz,
2453
+ created_at timestamptz NOT NULL DEFAULT now()
2454
+ )$klanten_stmt$, 'CREATE TABLE IF NOT EXISTS public.customer_delivery_location_glns ( id uuid PRIMARY KEY DEFAULT gen_');
2455
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE UNIQUE INDEX IF NOT EXISTS cdlg_customer_gln_uq ON public.customer_delivery_location_glns (customer_id, gln)$klanten_stmt$, 'CREATE UNIQUE INDEX IF NOT EXISTS cdlg_customer_gln_uq ON public.customer_delivery_location_glns (cu');
2456
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE UNIQUE INDEX IF NOT EXISTS cdlg_one_primary_per_location ON public.customer_delivery_location_glns (location_id) WHERE is_primary AND removed_at IS NULL$klanten_stmt$, 'CREATE UNIQUE INDEX IF NOT EXISTS cdlg_one_primary_per_location ON public.customer_delivery_location');
2457
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE INDEX IF NOT EXISTS cdlg_location_idx ON public.customer_delivery_location_glns (location_id)$klanten_stmt$, 'CREATE INDEX IF NOT EXISTS cdlg_location_idx ON public.customer_delivery_location_glns (location_id)');
2458
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE INDEX IF NOT EXISTS cdlg_floriday_idx ON public.customer_delivery_location_glns (customer_id, floriday_location_id)$klanten_stmt$, 'CREATE INDEX IF NOT EXISTS cdlg_floriday_idx ON public.customer_delivery_location_glns (customer_id,');
2459
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT SELECT, INSERT, UPDATE, DELETE ON public.customer_delivery_location_glns TO authenticated$klanten_stmt$, 'GRANT SELECT, INSERT, UPDATE, DELETE ON public.customer_delivery_location_glns TO authenticated');
2460
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT ALL ON public.customer_delivery_location_glns TO service_role$klanten_stmt$, 'GRANT ALL ON public.customer_delivery_location_glns TO service_role');
2461
+ SELECT pg_temp.klanten_try($klanten_stmt$ALTER TABLE public.customer_delivery_location_glns ENABLE ROW LEVEL SECURITY$klanten_stmt$, 'ALTER TABLE public.customer_delivery_location_glns ENABLE ROW LEVEL SECURITY');
2462
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can read customer_delivery_location_glns" ON public.customer_delivery_location_glns$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can read customer_delivery_location_glns" ON public.customer_de');
2463
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can read customer_delivery_location_glns" ON public.customer_delivery_location_glns FOR SELECT TO authenticated USING (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can read customer_delivery_location_glns" ON public.customer_delivery_l');
2464
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can insert customer_delivery_location_glns" ON public.customer_delivery_location_glns$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can insert customer_delivery_location_glns" ON public.customer_');
2465
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can insert customer_delivery_location_glns" ON public.customer_delivery_location_glns FOR INSERT TO authenticated WITH CHECK (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can insert customer_delivery_location_glns" ON public.customer_delivery');
2466
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can update customer_delivery_location_glns" ON public.customer_delivery_location_glns$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can update customer_delivery_location_glns" ON public.customer_');
2467
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can update customer_delivery_location_glns" ON public.customer_delivery_location_glns FOR UPDATE TO authenticated USING (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can update customer_delivery_location_glns" ON public.customer_delivery');
2468
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can delete customer_delivery_location_glns" ON public.customer_delivery_location_glns$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can delete customer_delivery_location_glns" ON public.customer_');
2469
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can delete customer_delivery_location_glns" ON public.customer_delivery_location_glns FOR DELETE TO authenticated USING (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can delete customer_delivery_location_glns" ON public.customer_delivery');
2470
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE TABLE IF NOT EXISTS public.customer_delivery_location_ignored (
2471
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
2472
+ customer_id uuid NOT NULL REFERENCES public.customers(id) ON DELETE CASCADE,
2473
+ gln text,
2474
+ floriday_location_id text,
2475
+ reason text,
2476
+ created_by uuid DEFAULT auth.uid(),
2477
+ created_at timestamptz NOT NULL DEFAULT now()
2478
+ )$klanten_stmt$, 'CREATE TABLE IF NOT EXISTS public.customer_delivery_location_ignored ( id uuid PRIMARY KEY DEFAULT g');
2479
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE INDEX IF NOT EXISTS cdli_customer_idx ON public.customer_delivery_location_ignored (customer_id)$klanten_stmt$, 'CREATE INDEX IF NOT EXISTS cdli_customer_idx ON public.customer_delivery_location_ignored (customer_');
2480
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT SELECT, INSERT, UPDATE, DELETE ON public.customer_delivery_location_ignored TO authenticated$klanten_stmt$, 'GRANT SELECT, INSERT, UPDATE, DELETE ON public.customer_delivery_location_ignored TO authenticated');
2481
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT ALL ON public.customer_delivery_location_ignored TO service_role$klanten_stmt$, 'GRANT ALL ON public.customer_delivery_location_ignored TO service_role');
2482
+ SELECT pg_temp.klanten_try($klanten_stmt$ALTER TABLE public.customer_delivery_location_ignored ENABLE ROW LEVEL SECURITY$klanten_stmt$, 'ALTER TABLE public.customer_delivery_location_ignored ENABLE ROW LEVEL SECURITY');
2483
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can read customer_delivery_location_ignored" ON public.customer_delivery_location_ignored$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can read customer_delivery_location_ignored" ON public.customer');
2484
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can read customer_delivery_location_ignored" ON public.customer_delivery_location_ignored FOR SELECT TO authenticated USING (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can read customer_delivery_location_ignored" ON public.customer_deliver');
2485
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can insert customer_delivery_location_ignored" ON public.customer_delivery_location_ignored$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can insert customer_delivery_location_ignored" ON public.custom');
2486
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can insert customer_delivery_location_ignored" ON public.customer_delivery_location_ignored FOR INSERT TO authenticated WITH CHECK (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can insert customer_delivery_location_ignored" ON public.customer_deliv');
2487
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can update customer_delivery_location_ignored" ON public.customer_delivery_location_ignored$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can update customer_delivery_location_ignored" ON public.custom');
2488
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can update customer_delivery_location_ignored" ON public.customer_delivery_location_ignored FOR UPDATE TO authenticated USING (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can update customer_delivery_location_ignored" ON public.customer_deliv');
2489
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP POLICY IF EXISTS "Authenticated can delete customer_delivery_location_ignored" ON public.customer_delivery_location_ignored$klanten_stmt$, 'DROP POLICY IF EXISTS "Authenticated can delete customer_delivery_location_ignored" ON public.custom');
2490
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE POLICY "Authenticated can delete customer_delivery_location_ignored" ON public.customer_delivery_location_ignored FOR DELETE TO authenticated USING (true)$klanten_stmt$, 'CREATE POLICY "Authenticated can delete customer_delivery_location_ignored" ON public.customer_deliv');
2491
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_gln_naar_locatie()
2492
+ RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2493
+ DECLARE _ids uuid[]; _id uuid;
2494
+ BEGIN
2495
+ IF current_setting('klanten.gln_sync', true) = 'on' THEN RETURN NULL; END IF;
2496
+ _ids := ARRAY[]::uuid[];
2497
+ IF TG_OP <> 'DELETE' THEN _ids := _ids || NEW.location_id; END IF;
2498
+ IF TG_OP <> 'INSERT' THEN _ids := _ids || OLD.location_id; END IF;
2499
+ PERFORM set_config('klanten.gln_sync', 'on', true);
2500
+ FOREACH _id IN ARRAY _ids LOOP
2501
+ UPDATE customer_delivery_locations l
2502
+ SET gln = (SELECT g.gln FROM customer_delivery_location_glns g
2503
+ WHERE g.location_id = _id AND g.is_primary AND g.removed_at IS NULL LIMIT 1)
2504
+ WHERE l.id = _id
2505
+ AND l.gln IS DISTINCT FROM (SELECT g.gln FROM customer_delivery_location_glns g
2506
+ WHERE g.location_id = _id AND g.is_primary AND g.removed_at IS NULL LIMIT 1);
2507
+ END LOOP;
2508
+ PERFORM set_config('klanten.gln_sync', '', true);
2509
+ RETURN NULL;
2510
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_gln_naar_locatie() RETURNS trigger LANGUAGE plpgsql SECURI');
2511
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP TRIGGER IF EXISTS trg_cdlg_naar_locatie ON public.customer_delivery_location_glns$klanten_stmt$, 'DROP TRIGGER IF EXISTS trg_cdlg_naar_locatie ON public.customer_delivery_location_glns');
2512
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE TRIGGER trg_cdlg_naar_locatie AFTER INSERT OR UPDATE OR DELETE ON public.customer_delivery_location_glns
2513
+ FOR EACH ROW EXECUTE FUNCTION public.klanten_gln_naar_locatie()$klanten_stmt$, 'CREATE TRIGGER trg_cdlg_naar_locatie AFTER INSERT OR UPDATE OR DELETE ON public.customer_delivery_lo');
2514
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_locatie_naar_gln()
2515
+ RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2516
+ DECLARE _new text; _r record; _p record; _naam text;
2517
+ BEGIN
2518
+ IF current_setting('klanten.gln_sync', true) = 'on' THEN RETURN NULL; END IF;
2519
+ IF NEW.customer_id IS NULL THEN RETURN NULL; END IF;
2520
+ IF TG_OP = 'UPDATE' AND klanten_gln_norm(NEW.gln) IS NOT DISTINCT FROM klanten_gln_norm(OLD.gln) THEN RETURN NULL; END IF;
2521
+ _new := klanten_gln_norm(NEW.gln);
2522
+ PERFORM set_config('klanten.gln_sync', 'on', true);
2523
+ IF _new IS NULL THEN
2524
+ UPDATE customer_delivery_location_glns SET is_primary = false
2525
+ WHERE location_id = NEW.id AND is_primary;
2526
+ PERFORM set_config('klanten.gln_sync', '', true);
2527
+ RETURN NULL;
2528
+ END IF;
2529
+ IF _new !~ '^[0-9]{13}$' THEN
2530
+ RAISE NOTICE 'Klanten: GLN % van locatie % heeft geen 13 cijfers en wordt niet in de GLN-lijst gezet', NEW.gln, NEW.id;
2531
+ PERFORM set_config('klanten.gln_sync', '', true);
2532
+ RETURN NULL;
2533
+ END IF;
2534
+ SELECT * INTO _r FROM customer_delivery_location_glns WHERE customer_id = NEW.customer_id AND gln = _new;
2535
+ IF FOUND AND _r.location_id <> NEW.id AND _r.removed_at IS NULL THEN
2536
+ SELECT COALESCE(NULLIF(name,''), NULLIF(address_line,''), id::text) INTO _naam FROM customer_delivery_locations WHERE id = _r.location_id;
2537
+ RAISE EXCEPTION 'GLN % hoort al bij locatie %', _new, _naam USING ERRCODE = '23505', HINT = _r.location_id::text;
2538
+ END IF;
2539
+ SELECT * INTO _p FROM customer_delivery_location_glns
2540
+ WHERE location_id = NEW.id AND is_primary AND removed_at IS NULL;
2541
+ IF FOUND AND _p.gln <> _new THEN
2542
+ IF _p.source = 'floriday' THEN
2543
+ UPDATE customer_delivery_location_glns SET is_primary = false, removed_at = now() WHERE id = _p.id;
2544
+ ELSE
2545
+ DELETE FROM customer_delivery_location_glns WHERE id = _p.id;
2546
+ END IF;
2547
+ END IF;
2548
+ IF _r.id IS NOT NULL THEN
2549
+ UPDATE customer_delivery_location_glns SET location_id = NEW.id, removed_at = NULL, is_primary = true WHERE id = _r.id;
2550
+ ELSE
2551
+ INSERT INTO customer_delivery_location_glns (customer_id, location_id, gln, is_primary, floriday_location_id, source)
2552
+ VALUES (NEW.customer_id, NEW.id, _new, true, NEW.floriday_location_id,
2553
+ CASE WHEN NEW.external_provider = 'floriday' OR NEW.floriday_location_id IS NOT NULL THEN 'floriday' ELSE 'manual' END);
2554
+ END IF;
2555
+ PERFORM set_config('klanten.gln_sync', '', true);
2556
+ RETURN NULL;
2557
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_locatie_naar_gln() RETURNS trigger LANGUAGE plpgsql SECURI');
2558
+ SELECT pg_temp.klanten_try($klanten_stmt$DROP TRIGGER IF EXISTS trg_cdl_naar_gln ON public.customer_delivery_locations$klanten_stmt$, 'DROP TRIGGER IF EXISTS trg_cdl_naar_gln ON public.customer_delivery_locations');
2559
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE TRIGGER trg_cdl_naar_gln AFTER INSERT OR UPDATE OF gln ON public.customer_delivery_locations
2560
+ FOR EACH ROW EXECUTE FUNCTION public.klanten_locatie_naar_gln()$klanten_stmt$, 'CREATE TRIGGER trg_cdl_naar_gln AFTER INSERT OR UPDATE OF gln ON public.customer_delivery_locations ');
2561
+ SELECT pg_temp.klanten_try($klanten_stmt$DO $b$
2562
+ DECLARE _l record; _v text;
2563
+ BEGIN
2564
+ PERFORM set_config('klanten.gln_sync', 'on', true);
2565
+ FOR _l IN SELECT id, customer_id, gln, floriday_location_id, external_provider
2566
+ FROM public.customer_delivery_locations
2567
+ WHERE customer_id IS NOT NULL AND NULLIF(btrim(gln), '') IS NOT NULL
2568
+ ORDER BY created_at, id LOOP
2569
+ _v := public.klanten_gln_norm(_l.gln);
2570
+ CONTINUE WHEN NOT public.klanten_gln_geldig(_v);
2571
+ CONTINUE WHEN EXISTS (SELECT 1 FROM public.customer_delivery_location_glns WHERE customer_id = _l.customer_id AND gln = _v);
2572
+ INSERT INTO public.customer_delivery_location_glns (customer_id, location_id, gln, is_primary, floriday_location_id, source)
2573
+ VALUES (_l.customer_id, _l.id, _v,
2574
+ NOT EXISTS (SELECT 1 FROM public.customer_delivery_location_glns WHERE location_id = _l.id AND is_primary AND removed_at IS NULL),
2575
+ _l.floriday_location_id,
2576
+ CASE WHEN _l.external_provider = 'floriday' OR _l.floriday_location_id IS NOT NULL THEN 'floriday' ELSE 'manual' END);
2577
+ END LOOP;
2578
+ UPDATE public.customer_delivery_locations l SET gln = g.gln
2579
+ FROM public.customer_delivery_location_glns g
2580
+ WHERE g.location_id = l.id AND g.is_primary AND g.removed_at IS NULL AND l.gln IS DISTINCT FROM g.gln;
2581
+ PERFORM set_config('klanten.gln_sync', '', true);
2582
+ END $b$$klanten_stmt$, 'DO $b$ DECLARE _l record; _v text; BEGIN PERFORM set_config(''klanten.gln_sync'', ''on'', true); FOR _l ');
2583
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_gln_backfill_report()
2584
+ RETURNS jsonb LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public AS $f$
2585
+ BEGIN
2586
+ PERFORM public.klanten_toegang('ingelogd');
2587
+ RETURN COALESCE((
2588
+ SELECT jsonb_agg(jsonb_build_object(
2589
+ 'location_id', l.id, 'customer_id', l.customer_id, 'naam', l.name, 'gln', l.gln,
2590
+ 'reden', CASE
2591
+ WHEN NOT klanten_gln_geldig(klanten_gln_norm(l.gln)) THEN 'ongeldig'
2592
+ WHEN g.location_id IS NOT NULL AND g.location_id <> l.id THEN 'dubbel'
2593
+ WHEN klanten_gln_norm(l.gln) <> l.gln THEN 'niet genormaliseerd'
2594
+ ELSE 'onbekend' END,
2595
+ 'hoort_bij', CASE WHEN g.location_id <> l.id THEN g.location_id END) ORDER BY l.customer_id, l.created_at)
2596
+ FROM customer_delivery_locations l
2597
+ LEFT JOIN customer_delivery_location_glns g ON g.customer_id = l.customer_id AND g.gln = klanten_gln_norm(l.gln)
2598
+ WHERE l.customer_id IS NOT NULL AND NULLIF(btrim(l.gln), '') IS NOT NULL
2599
+ AND (g.id IS NULL OR g.location_id <> l.id OR klanten_gln_norm(l.gln) <> l.gln)), '[]'::jsonb);
2600
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_gln_backfill_report() RETURNS jsonb LANGUAGE plpgsql STABL');
2601
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.klanten_gln_backfill_report() FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.klanten_gln_backfill_report() FROM PUBLIC, anon');
2602
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.klanten_gln_backfill_report() TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.klanten_gln_backfill_report() TO authenticated, service_role');
2603
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.resolve_delivery_location_by_gln(p_customer_id uuid, p_gln text)
2604
+ RETURNS uuid LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public AS $f$
2605
+ DECLARE _v text := klanten_gln_norm(p_gln); _id uuid;
2606
+ BEGIN
2607
+ PERFORM public.klanten_toegang('ingelogd');
2608
+ IF p_customer_id IS NULL OR _v IS NULL THEN RETURN NULL; END IF;
2609
+ SELECT location_id INTO _id FROM customer_delivery_location_glns
2610
+ WHERE customer_id = p_customer_id AND gln = _v AND removed_at IS NULL;
2611
+ IF _id IS NOT NULL THEN RETURN _id; END IF;
2612
+ SELECT CASE WHEN count(*) = 1 THEN min(id::text)::uuid END INTO _id
2613
+ FROM customer_delivery_locations WHERE customer_id = p_customer_id AND klanten_gln_norm(gln) = _v;
2614
+ RETURN _id;
2615
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.resolve_delivery_location_by_gln(p_customer_id uuid, p_gln text) R');
2616
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.resolve_delivery_location_by_gln(uuid, text) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.resolve_delivery_location_by_gln(uuid, text) FROM PUBLIC, anon');
2617
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.resolve_delivery_location_by_gln(uuid, text) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.resolve_delivery_location_by_gln(uuid, text) TO authenticated, serv');
2618
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.add_delivery_location_gln(p_location_id uuid, p_gln text)
2619
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2620
+ DECLARE _l record; _v text := klanten_gln_norm(p_gln); _r record; _naam text; _heeft_primair boolean; _id uuid;
2621
+ BEGIN
2622
+ PERFORM public.klanten_toegang('ingelogd');
2623
+ SELECT * INTO _l FROM customer_delivery_locations WHERE id = p_location_id;
2624
+ IF NOT FOUND OR _l.customer_id IS NULL THEN RAISE EXCEPTION 'Locatie niet gevonden'; END IF;
2625
+ IF _v IS NULL OR _v !~ '^[0-9]{13}$' THEN RAISE EXCEPTION 'Een GLN bestaat uit 13 cijfers'; END IF;
2626
+ IF NOT klanten_gln_geldig(_v) THEN RAISE EXCEPTION 'Het controlecijfer van deze GLN klopt niet'; END IF;
2627
+ SELECT * INTO _r FROM customer_delivery_location_glns WHERE customer_id = _l.customer_id AND gln = _v FOR UPDATE;
2628
+ IF FOUND AND _r.removed_at IS NULL THEN
2629
+ IF _r.location_id = p_location_id THEN RAISE EXCEPTION 'Deze GLN staat al bij deze locatie'; END IF;
2630
+ SELECT COALESCE(NULLIF(name,''), NULLIF(address_line,''), id::text) INTO _naam FROM customer_delivery_locations WHERE id = _r.location_id;
2631
+ RAISE EXCEPTION 'Deze GLN hoort bij %', _naam USING ERRCODE = '23505', HINT = _r.location_id::text;
2632
+ END IF;
2633
+ _heeft_primair := EXISTS (SELECT 1 FROM customer_delivery_location_glns WHERE location_id = p_location_id AND is_primary AND removed_at IS NULL);
2634
+ IF _r.id IS NOT NULL THEN
2635
+ UPDATE customer_delivery_location_glns SET location_id = p_location_id, removed_at = NULL, is_primary = NOT _heeft_primair
2636
+ WHERE id = _r.id RETURNING id INTO _id;
2637
+ ELSE
2638
+ INSERT INTO customer_delivery_location_glns (customer_id, location_id, gln, is_primary, source)
2639
+ VALUES (_l.customer_id, p_location_id, _v, NOT _heeft_primair, 'manual') RETURNING id INTO _id;
2640
+ END IF;
2641
+ RETURN (SELECT to_jsonb(g) FROM customer_delivery_location_glns g WHERE id = _id);
2642
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.add_delivery_location_gln(p_location_id uuid, p_gln text) RETURNS ');
2643
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.add_delivery_location_gln(uuid, text) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.add_delivery_location_gln(uuid, text) FROM PUBLIC, anon');
2644
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.add_delivery_location_gln(uuid, text) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.add_delivery_location_gln(uuid, text) TO authenticated, service_rol');
2645
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.remove_delivery_location_gln(p_gln_id uuid)
2646
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2647
+ DECLARE _g record; _n int; _next uuid;
2648
+ BEGIN
2649
+ PERFORM public.klanten_toegang('ingelogd');
2650
+ SELECT * INTO _g FROM customer_delivery_location_glns WHERE id = p_gln_id AND removed_at IS NULL FOR UPDATE;
2651
+ IF NOT FOUND THEN RAISE EXCEPTION 'GLN niet gevonden'; END IF;
2652
+ SELECT count(*) INTO _n FROM customer_delivery_location_glns WHERE location_id = _g.location_id AND removed_at IS NULL;
2653
+ IF _n <= 1 THEN RAISE EXCEPTION 'Een locatie heeft minimaal één GLN'; END IF;
2654
+ IF _g.source = 'floriday' OR _g.floriday_location_id IS NOT NULL THEN
2655
+ UPDATE customer_delivery_location_glns SET removed_at = now(), is_primary = false WHERE id = _g.id;
2656
+ ELSE
2657
+ DELETE FROM customer_delivery_location_glns WHERE id = _g.id;
2658
+ END IF;
2659
+ IF _g.is_primary THEN
2660
+ SELECT id INTO _next FROM customer_delivery_location_glns
2661
+ WHERE location_id = _g.location_id AND removed_at IS NULL ORDER BY created_at, id LIMIT 1;
2662
+ UPDATE customer_delivery_location_glns SET is_primary = true WHERE id = _next;
2663
+ END IF;
2664
+ RETURN jsonb_build_object('ok', true, 'soft', _g.source = 'floriday' OR _g.floriday_location_id IS NOT NULL);
2665
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.remove_delivery_location_gln(p_gln_id uuid) RETURNS jsonb LANGUAGE');
2666
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.remove_delivery_location_gln(uuid) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.remove_delivery_location_gln(uuid) FROM PUBLIC, anon');
2667
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.remove_delivery_location_gln(uuid) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.remove_delivery_location_gln(uuid) TO authenticated, service_role');
2668
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.set_primary_delivery_location_gln(p_gln_id uuid)
2669
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2670
+ DECLARE _g record;
2671
+ BEGIN
2672
+ PERFORM public.klanten_toegang('ingelogd');
2673
+ SELECT * INTO _g FROM customer_delivery_location_glns WHERE id = p_gln_id AND removed_at IS NULL;
2674
+ IF NOT FOUND THEN RAISE EXCEPTION 'GLN niet gevonden'; END IF;
2675
+ UPDATE customer_delivery_location_glns SET is_primary = false WHERE location_id = _g.location_id AND is_primary AND id <> _g.id;
2676
+ UPDATE customer_delivery_location_glns SET is_primary = true WHERE id = _g.id;
2677
+ RETURN jsonb_build_object('ok', true);
2678
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.set_primary_delivery_location_gln(p_gln_id uuid) RETURNS jsonb LAN');
2679
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.set_primary_delivery_location_gln(uuid) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.set_primary_delivery_location_gln(uuid) FROM PUBLIC, anon');
2680
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.set_primary_delivery_location_gln(uuid) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.set_primary_delivery_location_gln(uuid) TO authenticated, service_r');
2681
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_locaties_samenvoegen_aan()
2682
+ RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public AS $f$
2683
+ DECLARE _aan boolean;
2684
+ BEGIN
2685
+ PERFORM public.klanten_toegang('ingelogd');
2686
+ IF to_regclass('public.app_settings') IS NULL THEN RETURN false; END IF;
2687
+ EXECUTE $q$SELECT value = 'true' FROM public.app_settings WHERE key = 'klanten_locaties_samenvoegen' LIMIT 1$q$ INTO _aan;
2688
+ RETURN COALESCE(_aan, false);
2689
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_locaties_samenvoegen_aan() RETURNS boolean LANGUAGE plpgsq');
2690
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.klanten_locaties_samenvoegen_aan() FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.klanten_locaties_samenvoegen_aan() FROM PUBLIC, anon');
2691
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.klanten_locaties_samenvoegen_aan() TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.klanten_locaties_samenvoegen_aan() TO authenticated, service_role');
2692
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_set_locaties_samenvoegen(p_aan boolean)
2693
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2694
+ DECLARE _n integer;
2695
+ BEGIN
2696
+ PERFORM public.klanten_toegang('beheerder');
2697
+ IF p_aan IS NULL THEN RAISE EXCEPTION 'Stand is verplicht'; END IF;
2698
+ IF to_regclass('public.app_settings') IS NULL THEN RAISE EXCEPTION 'Instellingentabel app_settings ontbreekt in deze Shell'; END IF;
2699
+ EXECUTE $q$UPDATE public.app_settings SET value = $1, updated_at = now() WHERE key = 'klanten_locaties_samenvoegen'$q$
2700
+ USING CASE WHEN p_aan THEN 'true' ELSE 'false' END;
2701
+ GET DIAGNOSTICS _n = ROW_COUNT;
2702
+ IF _n = 0 THEN
2703
+ EXECUTE $q$INSERT INTO public.app_settings (key, value) VALUES ('klanten_locaties_samenvoegen', $1)$q$
2704
+ USING CASE WHEN p_aan THEN 'true' ELSE 'false' END;
2705
+ END IF;
2706
+ RETURN jsonb_build_object('aan', p_aan);
2707
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_set_locaties_samenvoegen(p_aan boolean) RETURNS jsonb LANG');
2708
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.klanten_set_locaties_samenvoegen(boolean) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.klanten_set_locaties_samenvoegen(boolean) FROM PUBLIC, anon');
2709
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.klanten_set_locaties_samenvoegen(boolean) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.klanten_set_locaties_samenvoegen(boolean) TO authenticated, service');
2710
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.klanten_locatie_zendingen(p_location_id uuid)
2711
+ RETURNS integer LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public AS $f$
2712
+ DECLARE _n integer := 0;
2713
+ BEGIN
2714
+ PERFORM public.klanten_toegang('ingelogd');
2715
+ IF to_regclass('public.order_shipments') IS NOT NULL THEN
2716
+ EXECUTE 'SELECT count(*) FROM public.order_shipments WHERE delivery_location_id = $1' INTO _n USING p_location_id;
2717
+ END IF;
2718
+ RETURN COALESCE(_n, 0);
2719
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.klanten_locatie_zendingen(p_location_id uuid) RETURNS integer LANG');
2720
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.klanten_locatie_zendingen(uuid) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.klanten_locatie_zendingen(uuid) FROM PUBLIC, anon');
2721
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.klanten_locatie_zendingen(uuid) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.klanten_locatie_zendingen(uuid) TO authenticated, service_role');
2722
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.merge_preview(p_source_id uuid, p_target_id uuid)
2723
+ RETURNS jsonb LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public AS $f$
2724
+ DECLARE _s record; _t record; _glns jsonb; _vv jsonb := '[]'::jsonb;
2725
+ BEGIN
2726
+ PERFORM public.klanten_toegang('ingelogd');
2727
+ SELECT * INTO _s FROM customer_delivery_locations WHERE id = p_source_id;
2728
+ SELECT * INTO _t FROM customer_delivery_locations WHERE id = p_target_id;
2729
+ IF _s.id IS NULL OR _t.id IS NULL THEN RAISE EXCEPTION 'Locatie niet gevonden'; END IF;
2730
+ IF _s.customer_id IS DISTINCT FROM _t.customer_id OR _s.customer_id IS NULL THEN RAISE EXCEPTION 'Beide locaties moeten bij dezelfde klant horen'; END IF;
2731
+ IF _s.id = _t.id THEN RAISE EXCEPTION 'Kies twee verschillende locaties'; END IF;
2732
+ SELECT COALESCE(jsonb_agg(jsonb_build_object('id', id, 'gln', gln, 'removed', removed_at IS NOT NULL) ORDER BY created_at), '[]'::jsonb)
2733
+ INTO _glns FROM customer_delivery_location_glns WHERE location_id = p_source_id;
2734
+ IF to_regclass('public.carriers') IS NOT NULL THEN
2735
+ EXECUTE $q$
2736
+ SELECT COALESCE(jsonb_agg(jsonb_build_object('gln', g.gln, 'carrier_id', c.id, 'carrier', c.name)), '[]'::jsonb)
2737
+ FROM customer_delivery_location_glns g
2738
+ JOIN carriers c ON g.gln = ANY (c.delivery_location_glns)
2739
+ WHERE g.location_id = $1 AND g.removed_at IS NULL
2740
+ AND NOT EXISTS (SELECT 1 FROM customer_delivery_location_glns t
2741
+ WHERE t.location_id = $2 AND t.removed_at IS NULL AND t.gln = ANY (c.delivery_location_glns))$q$
2742
+ INTO _vv USING p_source_id, p_target_id;
2743
+ END IF;
2744
+ RETURN jsonb_build_object(
2745
+ 'source', jsonb_build_object('id', _s.id, 'name', _s.name, 'address', _s.address_line, 'postal_code', _s.postal_code, 'city', _s.city, 'is_primary', _s.is_primary),
2746
+ 'target', jsonb_build_object('id', _t.id, 'name', _t.name, 'address', _t.address_line, 'postal_code', _t.postal_code, 'city', _t.city, 'is_primary', _t.is_primary),
2747
+ 'glns', _glns,
2748
+ 'shipments', public.klanten_locatie_zendingen(p_source_id),
2749
+ 'carrier_differences', COALESCE(_vv, '[]'::jsonb),
2750
+ 'enabled', public.klanten_locaties_samenvoegen_aan());
2751
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.merge_preview(p_source_id uuid, p_target_id uuid) RETURNS jsonb LA');
2752
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.merge_preview(uuid, uuid) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.merge_preview(uuid, uuid) FROM PUBLIC, anon');
2753
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.merge_preview(uuid, uuid) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.merge_preview(uuid, uuid) TO authenticated, service_role');
2754
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.merge_customer_delivery_locations(p_source_id uuid, p_target_id uuid)
2755
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2756
+ DECLARE _s record; _t record; _n integer := 0; _glns jsonb;
2757
+ BEGIN
2758
+ PERFORM public.klanten_toegang('ingelogd');
2759
+ IF NOT public.klanten_locaties_samenvoegen_aan() THEN
2760
+ RAISE EXCEPTION 'Locaties samenvoegen staat uit in de instellingen' USING ERRCODE = '42501';
2761
+ END IF;
2762
+ IF p_source_id = p_target_id THEN RAISE EXCEPTION 'Kies twee verschillende locaties'; END IF;
2763
+ PERFORM 1 FROM customer_delivery_locations WHERE id IN (p_source_id, p_target_id) ORDER BY id FOR UPDATE;
2764
+ SELECT * INTO _s FROM customer_delivery_locations WHERE id = p_source_id;
2765
+ SELECT * INTO _t FROM customer_delivery_locations WHERE id = p_target_id;
2766
+ IF _s.id IS NULL OR _t.id IS NULL THEN RAISE EXCEPTION 'Locatie niet gevonden'; END IF;
2767
+ IF _s.customer_id IS NULL OR _s.customer_id IS DISTINCT FROM _t.customer_id THEN
2768
+ RAISE EXCEPTION 'Beide locaties moeten bij dezelfde klant horen';
2769
+ END IF;
2770
+ SELECT COALESCE(jsonb_agg(jsonb_build_object('gln', gln, 'removed', removed_at IS NOT NULL)), '[]'::jsonb)
2771
+ INTO _glns FROM customer_delivery_location_glns WHERE location_id = p_source_id;
2772
+ UPDATE customer_delivery_location_glns SET
2773
+ location_id = p_target_id,
2774
+ is_primary = false,
2775
+ source = CASE WHEN removed_at IS NULL AND source = 'manual' THEN 'merge' ELSE source END,
2776
+ floriday_location_id = COALESCE(floriday_location_id, _s.floriday_location_id)
2777
+ WHERE location_id = p_source_id;
2778
+ IF _s.floriday_location_id IS NOT NULL AND NOT EXISTS (
2779
+ SELECT 1 FROM customer_delivery_location_glns WHERE customer_id = _s.customer_id AND floriday_location_id = _s.floriday_location_id) THEN
2780
+ INSERT INTO customer_delivery_location_ignored (customer_id, floriday_location_id, reason)
2781
+ VALUES (_s.customer_id, _s.floriday_location_id, 'samengevoegd met ' || p_target_id::text);
2782
+ END IF;
2783
+ IF to_regclass('public.order_shipments') IS NOT NULL THEN
2784
+ EXECUTE 'UPDATE public.order_shipments SET delivery_location_id = $1 WHERE delivery_location_id = $2' USING p_target_id, p_source_id;
2785
+ GET DIAGNOSTICS _n = ROW_COUNT;
2786
+ END IF;
2787
+ IF _s.is_primary AND NOT _t.is_primary THEN
2788
+ UPDATE customer_delivery_locations SET is_primary = false WHERE id = p_source_id;
2789
+ PERFORM public.set_primary_delivery_location(p_target_id);
2790
+ END IF;
2791
+ INSERT INTO customer_audit_log (customer_id, user_id, action, changes)
2792
+ VALUES (_s.customer_id, auth.uid(), 'update', jsonb_build_object(
2793
+ 'type', 'locatie_samengevoegd',
2794
+ 'bron', jsonb_build_object('id', _s.id, 'naam', _s.name, 'adres', concat_ws(', ', _s.address_line, _s.postal_code, _s.city), 'floriday_location_id', _s.floriday_location_id),
2795
+ 'doel', jsonb_build_object('id', _t.id, 'naam', _t.name),
2796
+ 'glns', _glns,
2797
+ 'zendingen', _n));
2798
+ DELETE FROM customer_delivery_locations WHERE id = p_source_id;
2799
+ RETURN (SELECT to_jsonb(l) FROM customer_delivery_locations l WHERE id = p_target_id) || jsonb_build_object('shipments_moved', _n);
2800
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.merge_customer_delivery_locations(p_source_id uuid, p_target_id uu');
2801
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.merge_customer_delivery_locations(uuid, uuid) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.merge_customer_delivery_locations(uuid, uuid) FROM PUBLIC, anon');
2802
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.merge_customer_delivery_locations(uuid, uuid) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.merge_customer_delivery_locations(uuid, uuid) TO authenticated, ser');
2803
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.delete_customer_delivery_location(p_location_id uuid)
2804
+ RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $f$
2805
+ DECLARE _l record;
2806
+ BEGIN
2807
+ PERFORM public.klanten_toegang('ingelogd');
2808
+ SELECT * INTO _l FROM customer_delivery_locations WHERE id = p_location_id FOR UPDATE;
2809
+ IF NOT FOUND THEN RAISE EXCEPTION 'Locatie niet gevonden'; END IF;
2810
+ IF public.klanten_locatie_zendingen(p_location_id) > 0 THEN
2811
+ RAISE EXCEPTION 'Aan deze locatie hangen zendingen; voeg hem samen met een andere locatie';
2812
+ END IF;
2813
+ IF _l.customer_id IS NOT NULL AND (_l.floriday_location_id IS NOT NULL OR _l.external_provider = 'floriday'
2814
+ OR EXISTS (SELECT 1 FROM customer_delivery_location_glns WHERE location_id = p_location_id AND source = 'floriday')) THEN
2815
+ INSERT INTO customer_delivery_location_ignored (customer_id, gln, floriday_location_id, reason)
2816
+ SELECT _l.customer_id, g.gln, COALESCE(g.floriday_location_id, _l.floriday_location_id), 'verwijderd'
2817
+ FROM customer_delivery_location_glns g WHERE g.location_id = p_location_id;
2818
+ IF _l.floriday_location_id IS NOT NULL THEN
2819
+ INSERT INTO customer_delivery_location_ignored (customer_id, floriday_location_id, reason)
2820
+ VALUES (_l.customer_id, _l.floriday_location_id, 'verwijderd');
2821
+ END IF;
2822
+ END IF;
2823
+ DELETE FROM customer_delivery_locations WHERE id = p_location_id;
2824
+ RETURN jsonb_build_object('ok', true);
2825
+ END $f$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.delete_customer_delivery_location(p_location_id uuid) RETURNS json');
2826
+ SELECT pg_temp.klanten_try($klanten_stmt$REVOKE ALL ON FUNCTION public.delete_customer_delivery_location(uuid) FROM PUBLIC, anon$klanten_stmt$, 'REVOKE ALL ON FUNCTION public.delete_customer_delivery_location(uuid) FROM PUBLIC, anon');
2827
+ SELECT pg_temp.klanten_try($klanten_stmt$GRANT EXECUTE ON FUNCTION public.delete_customer_delivery_location(uuid) TO authenticated, service_role$klanten_stmt$, 'GRANT EXECUTE ON FUNCTION public.delete_customer_delivery_location(uuid) TO authenticated, service_r');
2828
+ SELECT pg_temp.klanten_try($klanten_stmt$CREATE OR REPLACE FUNCTION public.import_floriday_delivery_locations(_org_id text)
2829
+ RETURNS integer
2830
+ LANGUAGE plpgsql
2831
+ SECURITY DEFINER
2832
+ SET search_path TO 'public'
2833
+ AS $function$
2834
+ DECLARE _cid uuid; _n integer := 0; _l record; _a jsonb; _lid text; _g text; _known record;
2835
+ BEGIN
2836
+ PERFORM public.klanten_toegang('ingelogd');
2837
+ SELECT id INTO _cid FROM customers WHERE floriday_organization_id = _org_id;
2838
+ IF _cid IS NULL THEN RETURN 0; END IF;
2839
+ FOR _l IN SELECT * FROM floriday_delivery_locations_cache
2840
+ WHERE organization_id = _org_id AND COALESCE(is_deleted,false) = false LOOP
2841
+ _a := COALESCE(_l.data->'address', _l.data->'physicalAddress', _l.data);
2842
+ _lid := COALESCE(NULLIF(_l.floriday_id,''), _l.data->>'locationId', _l.id::text);
2843
+ _g := klanten_gln_norm(_l.gln_code);
2844
+ CONTINUE WHEN EXISTS (SELECT 1 FROM customer_delivery_location_ignored
2845
+ WHERE customer_id = _cid AND (gln = _g OR floriday_location_id = _lid));
2846
+ SELECT * INTO _known FROM customer_delivery_location_glns
2847
+ WHERE customer_id = _cid AND (gln = _g OR floriday_location_id = _lid)
2848
+ ORDER BY (gln = _g) DESC NULLS LAST, removed_at NULLS FIRST LIMIT 1;
2849
+ IF FOUND THEN
2850
+ CONTINUE WHEN _known.removed_at IS NOT NULL;
2851
+ UPDATE customer_delivery_locations SET
2852
+ name = COALESCE(NULLIF(name,''), NULLIF(_l.name,''), _l.data->>'name'),
2853
+ address_line = COALESCE(NULLIF(address_line,''), NULLIF(_a->>'addressLine','')),
2854
+ postal_code = COALESCE(NULLIF(postal_code,''), NULLIF(_a->>'postalCode','')),
2855
+ city = COALESCE(NULLIF(city,''), NULLIF(_a->>'city','')),
2856
+ country = COALESCE(NULLIF(country,''), NULLIF(_a->>'countryCode','')),
2857
+ updated_at = now()
2858
+ WHERE id = _known.location_id;
2859
+ CONTINUE;
2860
+ END IF;
2861
+ IF EXISTS (SELECT 1 FROM customer_delivery_locations WHERE customer_id=_cid AND floriday_location_id=_lid) THEN
2862
+ UPDATE customer_delivery_locations SET
2863
+ gln = CASE WHEN klanten_gln_geldig(_g) THEN _g ELSE gln END,
2864
+ raw = _l.data, updated_at = now()
2865
+ WHERE customer_id=_cid AND floriday_location_id=_lid;
2866
+ ELSE
2867
+ INSERT INTO customer_delivery_locations (customer_id, floriday_location_id, name, address_line, postal_code, city, country, gln, raw, external_provider, external_key)
2868
+ VALUES (_cid, _lid,
2869
+ COALESCE(NULLIF(_l.name,''), _l.data->>'name'),
2870
+ NULLIF(_a->>'addressLine',''), NULLIF(_a->>'postalCode',''), NULLIF(_a->>'city',''),
2871
+ NULLIF(_a->>'countryCode',''), CASE WHEN klanten_gln_geldig(_g) THEN _g ELSE NULLIF(_l.gln_code,'') END,
2872
+ _l.data, 'floriday', _lid);
2873
+ _n := _n + 1;
2874
+ END IF;
2875
+ END LOOP;
2876
+ RETURN _n;
2877
+ END; $function$$klanten_stmt$, 'CREATE OR REPLACE FUNCTION public.import_floriday_delivery_locations(_org_id text) RETURNS integer L');