@flowselections/floriday-verkoop-module 1.0.106 → 1.0.109

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.
@@ -1,2 +1,2 @@
1
- export declare const MODULE_VERSION = "1.0.105";
1
+ export declare const MODULE_VERSION = "1.0.108";
2
2
  //# sourceMappingURL=module-version.d.ts.map
@@ -1,3 +1,3 @@
1
1
  // Versie van deze module, meegestuurd in antwoorden van geplande taken zodat
2
2
  // je ziet welke code echt draait. Bijwerken samen met package.json.
3
- export const MODULE_VERSION = "1.0.105";
3
+ export const MODULE_VERSION = "1.0.108";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/floriday-verkoop-module",
3
- "version": "1.0.106",
3
+ "version": "1.0.109",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -2009,3 +2009,73 @@ END IF; EXCEPTION WHEN undefined_function OR undefined_table OR undefined_column
2009
2009
  REVOKE ALL ON FUNCTION verkoop_create_order(text,text,uuid,date,uuid,uuid,jsonb,text,text,boolean,text,boolean,text) FROM PUBLIC, anon;
2010
2010
  GRANT EXECUTE ON FUNCTION verkoop_create_order(text,text,uuid,date,uuid,uuid,jsonb,text,text,boolean,text,boolean,text) TO authenticated;
2011
2011
  GRANT EXECUTE ON FUNCTION verkoop_create_order(text,text,uuid,date,uuid,uuid,jsonb,text,text,boolean,text,boolean,text) TO service_role;
2012
+
2013
+ -- 1.0.108: eenmalige verpakkingscorrectie Floriday-orderregels (lijst alleen lezen + correctie op opgegeven orders)
2014
+ CREATE OR REPLACE FUNCTION public.verkoop_packing_drift_report()
2015
+ RETURNS TABLE(order_id uuid, order_number text, order_status text, order_item_id uuid, sales_order_id text, afgesloten boolean, vastgezet boolean, cache_match text, cur_plants_per_crate integer, cur_crates_per_layer integer, cur_layers_per_cart integer, cur_vbn_package_code text, cur_load_carrier_type text, cur_packing_source text, fd_plants_per_crate integer, fd_crates_per_layer integer, fd_layers_per_cart integer, fd_vbn_package_code text, fd_load_carrier_type text)
2016
+ LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path TO 'public'
2017
+ AS $function$
2018
+ BEGIN
2019
+ IF auth.uid() IS NOT NULL AND NOT (public.has_role(auth.uid(),'admin') OR public.has_role(auth.uid(),'superadmin')) THEN
2020
+ RAISE EXCEPTION 'Alleen beheerders mogen deze lijst opvragen';
2021
+ END IF;
2022
+ RETURN QUERY
2023
+ WITH m AS (
2024
+ SELECT oi.id, oi.order_id, oi.source_line_key, oi.plants_per_crate, oi.crates_per_layer, oi.layers_per_cart,
2025
+ oi.vbn_package_code, oi.load_carrier_type, oi.packing_source, oi.packing_locked_at,
2026
+ o.order_number::text AS onum, o.status::text AS ostatus,
2027
+ (SELECT count(*) FROM floriday_salesorders_cache c WHERE c.floriday_id = oi.source_line_key AND coalesce(c.is_deleted,false)=false) AS n,
2028
+ (SELECT c.data->'packingConfiguration' FROM floriday_salesorders_cache c WHERE c.floriday_id = oi.source_line_key AND coalesce(c.is_deleted,false)=false LIMIT 1) AS pc
2029
+ FROM order_items oi JOIN orders o ON o.id = oi.order_id
2030
+ WHERE oi.source_line_key IS NOT NULL AND oi.quarantined_at IS NULL AND o.deleted_at IS NULL
2031
+ ), f AS (
2032
+ SELECT m.*,
2033
+ nullif((pc->>'piecesPerPackage')::numeric,0)::int AS f1,
2034
+ nullif((pc->>'packagesPerLayer')::numeric,0)::int AS f2,
2035
+ nullif((pc->>'layersPerLoadCarrier')::numeric,0)::int AS f3,
2036
+ nullif(pc#>>'{package,vbnPackageCode}','') AS f4,
2037
+ nullif(pc->>'loadCarrier','') AS f5
2038
+ FROM m
2039
+ )
2040
+ SELECT f.order_id, f.onum, f.ostatus, f.id, f.source_line_key,
2041
+ lower(coalesce(f.ostatus,'')) ~ '(verzonden|afgerond|geannuleerd|geleverd|cancel|shipped|delivered|vervallen|afgewezen|gereed voor facturatie)',
2042
+ f.packing_locked_at IS NOT NULL,
2043
+ CASE WHEN f.n = 1 AND f.pc IS NOT NULL THEN 'ok' WHEN f.n = 0 THEN 'geen Floriday-gegevens' WHEN f.n > 1 THEN 'meerdere Floriday-rijen' ELSE 'geen verpakking in Floriday' END,
2044
+ f.plants_per_crate, f.crates_per_layer, f.layers_per_cart, f.vbn_package_code, f.load_carrier_type, f.packing_source,
2045
+ f.f1, f.f2, f.f3, f.f4, f.f5
2046
+ FROM f
2047
+ WHERE f.n = 1 AND f.pc IS NOT NULL AND (
2048
+ f.plants_per_crate IS DISTINCT FROM f.f1 OR f.crates_per_layer IS DISTINCT FROM f.f2 OR f.layers_per_cart IS DISTINCT FROM f.f3)
2049
+ ORDER BY f.onum, f.id;
2050
+ END $function$;
2051
+
2052
+ CREATE OR REPLACE FUNCTION public.verkoop_packing_drift_apply(p_order_ids uuid[])
2053
+ RETURNS TABLE(order_id uuid, order_number text, lines_updated integer, replan_event text)
2054
+ LANGUAGE plpgsql SECURITY DEFINER SET search_path TO 'public'
2055
+ AS $function$
2056
+ DECLARE r record; v_n int; v_ev jsonb; v_at text := to_char(now() AT TIME ZONE 'UTC','YYYYMMDDHH24MISS');
2057
+ BEGIN
2058
+ IF auth.uid() IS NOT NULL AND NOT (public.has_role(auth.uid(),'admin') OR public.has_role(auth.uid(),'superadmin')) THEN
2059
+ RAISE EXCEPTION 'Alleen beheerders mogen deze correctie uitvoeren';
2060
+ END IF;
2061
+ IF p_order_ids IS NULL OR cardinality(p_order_ids) = 0 THEN RAISE EXCEPTION 'Geen orders opgegeven'; END IF;
2062
+ FOR r IN SELECT d.order_id, min(d.order_number) AS onum FROM public.verkoop_packing_drift_report() d
2063
+ WHERE d.order_id = ANY(p_order_ids) AND NOT d.afgesloten AND NOT d.vastgezet GROUP BY d.order_id LOOP
2064
+ UPDATE order_items oi SET plants_per_crate = d.fd_plants_per_crate, crates_per_layer = d.fd_crates_per_layer,
2065
+ layers_per_cart = d.fd_layers_per_cart, vbn_package_code = d.fd_vbn_package_code,
2066
+ load_carrier_type = d.fd_load_carrier_type, packing_source = 'floriday_order'
2067
+ FROM public.verkoop_packing_drift_report() d
2068
+ WHERE d.order_item_id = oi.id AND d.order_id = r.order_id AND NOT d.afgesloten AND NOT d.vastgezet AND oi.packing_locked_at IS NULL;
2069
+ GET DIAGNOSTICS v_n = ROW_COUNT;
2070
+ v_ev := to_jsonb(public.logistics_emit_event('verkoop','order.changed', r.order_id::text,
2071
+ jsonb_build_object('orderId', r.order_id, 'reason', 'Verpakking hersteld uit Floriday'),
2072
+ NULL, false, 'verpakking-hersteld:' || r.order_id || ':' || v_at, NULL));
2073
+ order_id := r.order_id; order_number := r.onum; lines_updated := v_n; replan_event := v_ev #>> '{}';
2074
+ RETURN NEXT;
2075
+ END LOOP;
2076
+ END $function$;
2077
+
2078
+ REVOKE EXECUTE ON FUNCTION public.verkoop_packing_drift_report() FROM PUBLIC, anon;
2079
+ REVOKE EXECUTE ON FUNCTION public.verkoop_packing_drift_apply(uuid[]) FROM PUBLIC, anon;
2080
+ GRANT EXECUTE ON FUNCTION public.verkoop_packing_drift_report() TO authenticated, service_role;
2081
+ GRANT EXECUTE ON FUNCTION public.verkoop_packing_drift_apply(uuid[]) TO authenticated, service_role;