only use actual sales for last price info in the evaluation process

This commit is contained in:
Paul Trowbridge 2025-08-25 17:35:19 -04:00
parent f19bd138e3
commit 2a699e8f83

View File

@ -35,40 +35,31 @@ BEGIN
END IF; END IF;
-- 1. Prefer the most recent of dss/dsq if either is within the age threshold -- 1. Prefer the most recent of dss/dsq if either is within the age threshold
IF (dsq_date IS NOT NULL AND dsq_date > (CURRENT_DATE - age_threshold)) IF dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold) THEN
OR (dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold)) THEN result := dss || jsonb_build_object('source', 'dss');
IF dsq_date IS NOT NULL AND (dss_date IS NULL OR dsq_date >= dss_date) AND dsq_date > (CURRENT_DATE - age_threshold) THEN
result := dsq || jsonb_build_object('source', 'dsq');
ELSIF dss_date IS NOT NULL AND dss_date > (CURRENT_DATE - age_threshold) THEN
result := dss || jsonb_build_object('source', 'dss');
END IF;
-- 2. If both dss/dsq are older than the threshold, use the most recent of mrs/mrq if either exists -- 2. If both dss/dsq are older than the threshold, use the most recent of mrs/mrq if either exists
ELSIF (mrq_date IS NOT NULL OR mrs_date IS NOT NULL) THEN ELSIF mrs_date IS NOT NULL AND mrs_date > (CURRENT_DATE - age_threshold) THEN
IF mrq_date IS NOT NULL AND (mrs_date IS NULL OR mrq_date >= mrs_date) THEN result := mrs || jsonb_build_object('source', 'mrs');
result := mrq || jsonb_build_object('source', 'mrq');
ELSIF mrs_date IS NOT NULL THEN
result := mrs || jsonb_build_object('source', 'mrs');
END IF;
-- 3. If all are at least as old as the threshold, pick the least oldest price available -- 3. If all are at least as old as the threshold, pick the least oldest price available
ELSE ELSE
best := NULL; best := NULL;
best_date := NULL; best_date := NULL;
IF dsq_date IS NOT NULL THEN -- IF dsq_date IS NOT NULL THEN
best := dsq || jsonb_build_object('source', 'dsq'); -- best := dsq || jsonb_build_object('source', 'dsq');
best_date := dsq_date; -- best_date := dsq_date;
END IF; -- END IF;
IF dss_date IS NOT NULL AND (best_date IS NULL OR dss_date > best_date) THEN -- IF dss_date IS NOT NULL AND (best_date IS NULL OR dss_date > best_date) THEN
best := dss || jsonb_build_object('source', 'dss'); -- best := dss || jsonb_build_object('source', 'dss');
best_date := dss_date; -- best_date := dss_date;
END IF; -- END IF;
IF mrq_date IS NOT NULL AND (best_date IS NULL OR mrq_date > best_date) THEN -- IF mrq_date IS NOT NULL AND (best_date IS NULL OR mrq_date > best_date) THEN
best := mrq || jsonb_build_object('source', 'mrq'); -- best := mrq || jsonb_build_object('source', 'mrq');
best_date := mrq_date; -- best_date := mrq_date;
END IF; -- END IF;
IF mrs_date IS NOT NULL AND (best_date IS NULL OR mrs_date > best_date) THEN -- IF mrs_date IS NOT NULL AND (best_date IS NULL OR mrs_date > best_date) THEN
best := mrs || jsonb_build_object('source', 'mrs'); -- best := mrs || jsonb_build_object('source', 'mrs');
best_date := mrs_date; -- best_date := mrs_date;
END IF; -- END IF;
result := best; result := best;
END IF; END IF;
RETURN result; RETURN result;