Commit 14e892ff authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(accounting): connection centre 500'd under only_full_group_by

The stream list used LEFT JOIN revenue_posting_rules + GROUP BY s.id and selected
r.id / r.stage. MySQL rejects that under only_full_group_by — r.id is not
functionally dependent on s.id — so the page threw PDOException on every load.

Caught by running the query against the live database rather than trusting that
it looked reasonable.

The representative rule is now picked in its own aggregate and joined back by id,
preferring the collection stage since that is the one people mean by "where does
this money go", then the newest effective date and version.
Co-Authored-By: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
parent 5ed15737
...@@ -722,17 +722,37 @@ class RevenueMappingController extends Controller ...@@ -722,17 +722,37 @@ class RevenueMappingController extends Controller
$this->authorize('accounting.revenue_mapping.view'); $this->authorize('accounting.revenue_mapping.view');
$db = App::getInstance()->db(); $db = App::getInstance()->db();
// One representative rule per stream, chosen deterministically.
//
// A plain LEFT JOIN + GROUP BY s.id is rejected under only_full_group_by
// (r.id is not functionally dependent on s.id), so the pick happens in its
// own aggregate and the rule row is then joined by id. Stage preference is
// collection first, since that is the one people mean by "where does this
// money go".
$streams = $db->select( $streams = $db->select(
"SELECT s.*, "SELECT s.*, r.id AS rule_id, r.stage AS rule_stage
r.id AS rule_id, r.stage AS rule_stage
FROM revenue_streams s FROM revenue_streams s
LEFT JOIN revenue_posting_rules r LEFT JOIN (
ON r.stream_id = s.id SELECT stream_id,
AND r.status = 'active' CAST(
AND r.effective_from <= CURDATE() SUBSTRING_INDEX(
AND (r.effective_to IS NULL OR r.effective_to >= CURDATE()) GROUP_CONCAT(
id
ORDER BY FIELD(stage,'collection','accrual','payment','refund','writeoff','transfer'),
effective_from DESC,
version DESC
),
',', 1
) AS UNSIGNED
) AS rule_id
FROM revenue_posting_rules
WHERE status = 'active'
AND effective_from <= CURDATE()
AND (effective_to IS NULL OR effective_to >= CURDATE())
GROUP BY stream_id
) pick ON pick.stream_id = s.id
LEFT JOIN revenue_posting_rules r ON r.id = pick.rule_id
WHERE s.is_active = 1 WHERE s.is_active = 1
GROUP BY s.id
ORDER BY s.category, s.name_ar" ORDER BY s.category, s.name_ar"
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment