fix(sales_and_purchase_return): consider net_rate instead of rate for return validation#54628
Conversation
… for return validation
📝 WalkthroughWalkthroughThe changes modify item-level reference-rate validation in return handling by replacing Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
erpnext/controllers/sales_and_purchase_return.py (1)
89-89: Add a safe fallback toratewhennet_rateis missing/zero.For resilience with legacy/edge data, consider falling back to
ratein both reference extraction and comparison so validation is not silently skipped whennet_rateis unavailable.💡 Proposed hardening diff
- select_fields = "item_code, qty, stock_qty, net_rate, parenttype, conversion_factor, name" + select_fields = "item_code, qty, stock_qty, net_rate, rate, parenttype, conversion_factor, name" @@ - if ( - ref.net_rate - and flt(d.net_rate) > ref.net_rate + ref_net_rate = flt(ref.get("net_rate") or ref.get("rate")) + row_net_rate = flt(d.get("net_rate") or d.get("rate")) + if ( + ref_net_rate + and row_net_rate > ref_net_rate and doc.doctype in ("Delivery Note", "Sales Invoice", "POS Invoice") and get_valuation_method(ref.item_code, doc.company) != "Moving Average" ): @@ - if ref_item_row.get("net_rate", 0) > item_dict["net_rate"]: - item_dict["net_rate"] = ref_item_row.get("net_rate", 0) + ref_net_rate = flt(ref_item_row.get("net_rate") or ref_item_row.get("rate")) + if ref_net_rate > item_dict["net_rate"]: + item_dict["net_rate"] = ref_net_rateAlso applies to: 144-147, 257-258
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@erpnext/controllers/sales_and_purchase_return.py` at line 89, The code currently selects net_rate and uses it directly; update the logic to include a safe fallback to rate: add "rate" to the select_fields string (so both net_rate and rate are fetched) and, wherever the code reads net_rate (reference extraction and comparison logic in the functions that handle return item rate lookup and validation), replace direct net_rate usage with a safe value expression like: use net_rate if truthy/non-zero else use rate (and coerce to numeric safely). Apply this change at the three sites mentioned (the select_fields definition and the two places that perform reference extraction/comparison around lines 144-147 and 257-258), ensuring comparisons and any zero-checks use the fallback value so validation isn’t skipped when net_rate is missing or zero.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@erpnext/controllers/sales_and_purchase_return.py`:
- Line 89: The code currently selects net_rate and uses it directly; update the
logic to include a safe fallback to rate: add "rate" to the select_fields string
(so both net_rate and rate are fetched) and, wherever the code reads net_rate
(reference extraction and comparison logic in the functions that handle return
item rate lookup and validation), replace direct net_rate usage with a safe
value expression like: use net_rate if truthy/non-zero else use rate (and coerce
to numeric safely). Apply this change at the three sites mentioned (the
select_fields definition and the two places that perform reference
extraction/comparison around lines 144-147 and 257-258), ensuring comparisons
and any zero-checks use the fallback value so validation isn’t skipped when
net_rate is missing or zero.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 226d5083-03a7-423f-97c2-c935d79ac653
📒 Files selected for processing (1)
erpnext/controllers/sales_and_purchase_return.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #54628 +/- ##
===========================================
+ Coverage 79.44% 79.74% +0.30%
===========================================
Files 1160 1160
Lines 126161 126372 +211
===========================================
+ Hits 100224 100776 +552
+ Misses 25937 25596 -341
🚀 New features to boost your workflow:
|
Considering
net_rateinstead ofrateto validate item return.Problem with the existing flow:
rate, which allows a larger amount in the Credit Note than what is received during the Sales transaction.Todo: