bugfix(reports): crash on detailed sales report when sale has multiple payments with reference codes#4599
bugfix(reports): crash on detailed sales report when sale has multiple payments with reference codes#4599objecttothis wants to merge 3 commits into
Conversation
Use GROUP_CONCAT to combine multiple payment reference codes into a single comma-separated value, and group only by sale_id to correctly handle sales with multiple payment records. Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughChangesSales payment aggregation
Repository guidance
Estimated code review effort: 2 (Simple) | ~5 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@jekkos this one is a quick fix. The bug only crashed the reports when a sale had multiple payments on it and reference_codes were involved. The sales_payments table allows multiple rows per sale (one for each payment). The temp table for reporting doesn't so we just need to aggregate the reference_codes for the reporting. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/Models/Sale.php`:
- Line 1098: Update the reference_code aggregation in the Sale query to wrap
payments.reference_code with NULLIF(..., '') before GROUP_CONCAT, so empty
strings are excluded while non-empty references remain unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
- Clarify PSR-12 enforcement via PHP-CS-Fixer config reference - Bump minimum PHP version requirement from 8.1 to 8.2 - Add JavaScript const/let/var convention rule - Reorganize coding standards section for clarity Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@AGENTS.md`:
- Line 44: Update the translation guidance in AGENTS.md to prohibit using an
empty string as the untranslated placeholder. Instruct contributors to omit
untranslated keys so CodeIgniter can fall back to English, unless custom
fallback logic is explicitly implemented.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
…tion Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
Problem
Running the detailed sales report fails with:
Duplicate entry '<sale_id>' for key 'ospos_sales_payments_temp.PRIMARY'
This occurs on any sale that has multiple payment rows in ospos_sales_payments with different reference_code values (e.g. split tender with a reference-code payment type, or two separate card swipes).
create_temp_table() in Sale.php builds ospos_sales_payments_temp with PRIMARY KEY(sale_id), but the subquery grouped by (payments.sale_id, payments.reference_code). When a sale has N payments with distinct reference codes, the subquery returns N rows for the same sale_id, violating the primary key constraint on insert.
Note: ospos_sales_payments legitimately stores multiple rows per sale (its PK is auto-increment payment_id). The temp table is reporting-only and is intentionally one row per sale — the bug was in the GROUP BY not matching that intent.
Fix
In app/Models/Sale.php, create_temp_table():
Changed payments.reference_code AS reference_code → GROUP_CONCAT(payments.reference_code SEPARATOR ", ") AS reference_code
Changed GROUP BY payments.sale_id, payments.reference_code → GROUP BY payments.sale_id
This produces one row per sale in the temp table (satisfying the PRIMARY KEY), aggregates all reference codes into a comma-separated string alongside the already-aggregated payment_type column, and has no impact on write operations — the temp table is read-only and used solely for report display.
Testing
Run the detailed sales report for a date range containing a sale with multiple payment types or a reference-code payment. Report should complete without error and display all payment types and reference codes concatenated in the respective columns.
Summary by CodeRabbit