Commit Graph

2 Commits

Author SHA1 Message Date
Ollama
423c06142c Prevent negative reward points and address CodeRabbit review comments
CodeRabbit issues addressed:

1. Negative points prevention in Reward_lib:
   - adjustRewardPoints(): Validate sufficient balance before deduct/adjust
   - handleCustomerChange(): Cap charge at available points, add 'insufficient' flag
   - adjustRewardDelta(): Validate sufficient points for positive adjustments

2. Sale.php fixes:
   - Add null coalescing for reward points in processPaymentType()
   - Validate giftcard payment format before accessing array index
   - Remove unused loop variables $paymentId and $line
   - Add null check for deleted customer in delete() method
   - Log warnings when insufficient points detected

3. Test coverage:
   - Add test for exact points match (hasSufficientPoints)
   - Add tests for insufficient points scenarios
   - Add tests for negative adjustment (refund)
   - Add tests for handleCustomerChange caps

All changes prevent customers from having negative reward point balances.
2026-03-22 19:56:04 +00:00
Ollama
66b6c99384 Extract reward logic to Reward_lib library and add unit tests
- Create app/Libraries/Reward_lib.php with reward point business logic:
  - calculatePointsEarned(): calculate rewards from purchase amount
  - adjustRewardPoints(): deduct/restore/adjust customer points
  - handleCustomerChange(): handle points when customer changes on sale
  - adjustRewardDelta(): adjust delta for same customer payment changes
  - hasSufficientPoints(): validate customer has enough points
  - getPointsBalance(): get customer points balance
  - calculateRewardPaymentAmount(): sum reward payments from array

- Add tests/Libraries/Reward_libTest.php with 20+ test cases covering:
  - Points calculation edge cases
  - Customer change scenarios
  - Deletion/restore operations
  - Sufficient points validation
  - Payment amount calculation

- Update tests/phpunit.xml to include Libraries testsuite

This extraction centralizes reward logic for better testability and
follows the existing library pattern (Tax_lib, Sale_lib, etc.).
2026-03-17 15:00:57 +00:00