Skip to content

fix(items): prevent corrupt average cost when receiving against negative stock - #4838

Open
VighneshNilajakar wants to merge 1 commit into
opensourcepos:masterfrom
VighneshNilajakar:fix/negative-stock-average-cost
Open

VighneshNilajakar wants to merge 1 commit into
opensourcepos:masterfrom
VighneshNilajakar:fix/negative-stock-average-cost

Conversation

@VighneshNilajakar

@VighneshNilajakar VighneshNilajakar commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Guard Item::change_cost_price() against missing or negative old stock, non-positive received quantity, and non-positive total quantity.
  • Use the newly received price instead of calculating a weighted average when the stock state is invalid.
  • Preserve the existing weighted-average behavior for valid non-negative stock and prevent division by zero.

Root cause

change_cost_price() included negative stock in the weighted-average denominator. Receiving stock while inventory was negative could therefore produce nonsensical cost_price values, which were later snapshotted into ospos_sales_items.item_cost_price.

Tests

  • vendor/bin/phpunit tests/Models/ItemTest.php tests/Models/ReceivingTest.php - 11 tests, 34 assertions
  • vendor/bin/phpunit tests/Models - 119 tests, 286 assertions
  • PHP lint on both changed files

Notes

  • No migrations or business data changes are included.
  • Historical ospos_sales_items.item_cost_price values may already be corrupted and require a separate, reviewed data-repair process.

Summary by CodeRabbit

  • Bug Fixes
    • Cost price calculations now handle missing, zero, or negative stock totals and nonpositive received quantities consistently, using the incoming price when a weighted average cannot be applied.
    • For positive stock totals and received quantities, cost price continues to reflect a weighted average.

Skip weighted-average cost calculation when stock is missing or negative, or when received/total quantities are non-positive. Use the newly received price instead, preventing invalid cost prices and division by zero during receiving. Add regression coverage for positive, zero, and negative stock.
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

change_cost_price() now uses the incoming price for missing or nonpositive stock conditions. Tests cover these cases and confirm that positive stock still uses a weighted average.

Changes: Cost price calculation

Layer / File(s) Summary
Cost calculation and test coverage
app/Models/Item.php, tests/Models/ItemTest.php
The cost-price calculation uses the incoming price when the prior stock total is missing or negative, the received quantity is nonpositive, or the combined quantity is nonpositive. Tests cover these cases and verify the weighted average for positive stock.

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

Change: Bug fix

Suggested labels: bug

Suggested reviewers: objecttothis, jekkos

Merge Risk: Minimal · up to 50cc4

Receiving against negative or missing stock now sets the cost price to the received price instead of a nonsensical average. Weighted-average behavior for positive stock is unchanged. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Item::change_cost_price() now guards against receiving stock against negative stock, which can corrupt the average cost. The raw summary and objectives confirm the cha…
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.
Full details: Title check

Explanation

The title names the main change: Item::change_cost_price() now guards against receiving stock against negative stock, which can corrupt the average cost. The raw summary and objectives confirm the change adds guards for negative old stock, non-positive received quantity, and non-positive total, and adds regression tests for positive, zero, and negative stock. The title uses the conventional fix(scope) form and is specific.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e98bb527-5e4d-4411-b868-11c2cb01ff1d
Commits

Reviewing files that changed from the base of the PR and between e908135 and 50cc435.

Files selected for processing: 2

  • app/Models/Item.php
  • tests/Models/ItemTest.php

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@jekkos
jekkos self-requested a review November 23, 2026 19:50

This branch has not been deployed

No deployments
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