Sitelet https://github.com/opensourcepos/opensourcepos/pull/4496
Skip to content

Update Sale.php - #4496

Open
vjcp64 wants to merge 1 commit into
opensourcepos:masterfrom
vjcp64:master
Open

Update Sale.php#4496
vjcp64 wants to merge 1 commit into
opensourcepos:masterfrom
vjcp64:master

Conversation

@vjcp64

@vjcp64 vjcp64 commented Apr 13, 2026

Copy link
Copy Markdown

comiting changes for the issue relating to the reward system
updating rewards and
deleting reward in case of rewfunds

Summary by CodeRabbit

  • New Features

    • Added the ability to permanently delete sales records from the system, giving users enhanced control over sales data management.
  • Documentation

    • Clarified documentation on reward point handling during sales modifications, noting that certain updates may not automatically recalculate reward points or may result in inconsistent customer balances.

comiting changes for the issue relating to the reward system
@coderabbitai

coderabbitai Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A new public method delete_sale() was added to the Sale model that performs hard deletion of sales records. Existing update() method documentation was augmented with inline comments noting potential reward-point recalculation inconsistencies when payment-related fields change.

Changes

Cohort / File(s) Summary
Sale Model Deletion & Documentation
app/Models/Sale.php
Added new public method delete_sale(int $sale_id): bool that hard-deletes sales records; added inline comments to update() documenting that reward-point recalculation may not occur when payment_type changes and sales_reward_points may become inconsistent if payment_amount changes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A sale now bids farewell with one swift swing,
Though points and balances might miss the thing—
Delete it goes, no transaction to bind,
A careful review of data left behind! 🗑️

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Update Sale.php' is vague and generic, using non-descriptive language that fails to convey the specific changes made. Replace with a more specific title that describes the actual changes, such as 'Add delete_sale method and document reward-point inconsistencies' or similar.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@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: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/Models/Sale.php`:
- Around line 441-452: In update($sale_id = null, $sale_data = null) add reward
reconciliation: within the same DB transaction that updates the sales row and
payments, load current sales_reward_points entries for the sale
(sales_reward_points), compute the existing reward amount total and compare to
the new payments payload (detect payment_type === 'Reward' and sum
payment_amount), calculate the delta, update or insert the sales_reward_points
row accordingly, and increment/decrement the customer's points balance
(customers.points) by that delta; ensure you also handle payment_type changes
to/from 'Reward' and rollback the transaction on any error so sale, payments,
reward ledger, and customer balance stay consistent.
🪄 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: 1e6c0847-f4c0-41dc-842d-2059d03a3e97

📥 Commits

Reviewing files that changed from the base of the PR and between 332d8c8 and e90fae7.

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

Comment thread app/Models/Sale.php
Comment on lines +441 to +452
public function update($sale_id = null, $sale_data = null): bool //Bug Identifyed: When a sale is edited, the reward points are not recalculated.
// This function needs a hook to the Rewards model to check if the payment_type
// has changed to or from 'Reward' to ensure the customer's balance is updated.
{
$builder = $this->db->table('sales');
$builder->where('sale_id', $sale_id);
$update_data = $sale_data;
unset($update_data['payments']);
$success = $builder->update($update_data);

// DATA INTEGRITY ISSUE: If the payment_amount is edited here, the
// 'sales_reward_points' table does not receive an update, leading
// to a mismatch between the sale total and the customer's point balance.

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.

⚠️ Potential issue | 🟠 Major

The sale-edit path still does not reconcile rewards.

These comments describe the bug, but update() still only persists sales and sales_payments. Changing a payment to/from rewards, or changing the reward payment amount, leaves both customers.points and the sales_reward_points entry stale. Please load the existing reward ledger for the sale, compute the delta against the edited payments, and apply that inside the same transaction as the sale/payment update.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/Models/Sale.php` around lines 441 - 452, In update($sale_id = null,
$sale_data = null) add reward reconciliation: within the same DB transaction
that updates the sales row and payments, load current sales_reward_points
entries for the sale (sales_reward_points), compute the existing reward amount
total and compare to the new payments payload (detect payment_type === 'Reward'
and sum payment_amount), calculate the delta, update or insert the
sales_reward_points row accordingly, and increment/decrement the customer's
points balance (customers.points) by that delta; ensure you also handle
payment_type changes to/from 'Reward' and rollback the transaction on any error
so sale, payments, reward ledger, and customer balance stay consistent.

Comment thread app/Models/Sale.php
Comment on lines +1517 to +1523
public function delete_sale(int $sale_id): bool
{
// CRITICAL BUG: Deleting a sale fails to restore 'used' points to the customer.
// We must query 'sales_reward_points' for this $sale_id and increment
// the 'points' column in the 'customers' table before completing the deletion.
return $this->db->table('sales')->where('sale_id', $sale_id)->delete();
}

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.

⚠️ Potential issue | 🔴 Critical

Avoid introducing a header-only hard delete for sales.

delete_sale() contradicts the existing delete() contract and removes only the sales row. That either leaves dependent sale rows / sales_reward_points behind or fails on FK constraints, and it never reverses the customer's net reward delta (used back, earned removed). The current refund path in app/Controllers/Sales.php:1531-1545 still calls delete(), so this helper also does not fix the live workflow. Put the reward reversal into the shared transactional delete path instead of adding a raw hard delete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant