Skip to content

bugfix(reports): crash on detailed sales report when sale has multiple payments with reference codes#4599

Open
objecttothis wants to merge 3 commits into
masterfrom
bugfix-multiple-payments-report-error
Open

bugfix(reports): crash on detailed sales report when sale has multiple payments with reference codes#4599
objecttothis wants to merge 3 commits into
masterfrom
bugfix-multiple-payments-report-error

Conversation

@objecttothis

@objecttothis objecttothis commented Jul 22, 2026

Copy link
Copy Markdown
Member

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

  • Bug Fixes
    • Improved sales payment data handling by consolidating multiple non-empty payment reference codes into a single sale record.
    • Reduced the likelihood of duplicate sale entries caused by splitting records across different reference codes.
  • Documentation
    • Updated contributor guidance with clearer PHP/JS style rules (including PHP-CS-Fixer and PHP 8.2+), refined view exclusion constraints, and strengthened localization and security checklists (including alphabetical insertion of new language keys and input validation/sanitization).

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>
@objecttothis objecttothis self-assigned this Jul 22, 2026
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7f3070e4-799a-46ee-8af9-431d4dca16ac

📥 Commits

Reviewing files that changed from the base of the PR and between d83536d and 17d67e9.

📒 Files selected for processing (1)
  • app/Models/Sale.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/Models/Sale.php

📝 Walkthrough

Walkthrough

Changes

Sales payment aggregation

Layer / File(s) Summary
Temporary payment table query
app/Models/Sale.php
sales_payments_temp now groups by payments.sale_id and aggregates payments.reference_code with GROUP_CONCAT, producing one row per sale.

Repository guidance

Layer / File(s) Summary
Coding, localization, and security guidance
AGENTS.md
Updates PHP-CS-Fixer, PHP 8.2+, JavaScript declaration, localization ordering and fallback, and user-input validation guidance.

Estimated code review effort: 2 (Simple) | ~5 minutes

Suggested reviewers: jekkos

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: preventing detailed sales report crashes for sales with multiple payment reference codes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix-multiple-payments-report-error

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@objecttothis
objecttothis requested a review from jekkos July 22, 2026 11:55
@objecttothis

Copy link
Copy Markdown
Member Author

@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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5c4cedb8-c27e-4216-8ce3-c27a82d0ec31

📥 Commits

Reviewing files that changed from the base of the PR and between 9c542ef and fbddbf2.

📒 Files selected for processing (1)
  • app/Models/Sale.php

Comment thread app/Models/Sale.php Outdated
- 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>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 548d0232-6917-4827-bae3-f27901cb00b4

📥 Commits

Reviewing files that changed from the base of the PR and between fbddbf2 and d83536d.

📒 Files selected for processing (1)
  • AGENTS.md

Comment thread AGENTS.md
…tion

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
@objecttothis objecttothis added this to the 3.4.2 milestone Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant