diff --git a/svc/routes/product_images.go b/svc/routes/product_images.go index a45b8dd..1348ec0 100644 --- a/svc/routes/product_images.go +++ b/svc/routes/product_images.go @@ -150,9 +150,16 @@ WHERE b.typ = 'img' AND ( -- exact code match (spaces ignored) UPPER(REPLACE(COALESCE(i.code,''), ' ', '')) = UPPER(REPLACE(COALESCE($1,''), ' ', '')) - -- or exact core-code match after last '-' (e.g. S901-DMY18252 -> DMY18252) - OR UPPER(REPLACE(REGEXP_REPLACE(COALESCE(i.code,''), '^.*-', ''), ' ', '')) = - UPPER(REPLACE(REGEXP_REPLACE(COALESCE($1,''), '^.*-', ''), ' ', '')) + -- core-code fallback only when there is no exact item code for the input + OR ( + NOT EXISTS ( + SELECT 1 + FROM mmitem ix + WHERE UPPER(REPLACE(COALESCE(ix.code,''), ' ', '')) = UPPER(REPLACE(COALESCE($1,''), ' ', '')) + ) + AND UPPER(REPLACE(REGEXP_REPLACE(COALESCE(i.code,''), '^.*-', ''), ' ', '')) = + UPPER(REPLACE(REGEXP_REPLACE(COALESCE($1,''), '^.*-', ''), ' ', '')) + ) ) ORDER BY COALESCE(b.sort_order,999999),