Skip to content

Commit

Permalink
fix: Negative rates should be allowed via Update Items in SO/PO
Browse files Browse the repository at this point in the history
  • Loading branch information
marination committed Nov 22, 2023
1 parent 680ced4 commit 619db8a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 deletions erpnext/controllers/accounts_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3040,16 +3040,19 @@ def should_update_supplied_items(doc) -> bool:
conv_fac_precision = child_item.precision("conversion_factor") or 2
qty_precision = child_item.precision("qty") or 2

if flt(child_item.billed_amt, rate_precision) > flt(
flt(d.get("rate"), rate_precision) * flt(d.get("qty"), qty_precision), rate_precision
):
# Amount cannot be lesser than billed amount, except for negative amounts
row_rate = flt(d.get("rate"), rate_precision)
amount_below_billed_amt = flt(child_item.billed_amt, rate_precision) > flt(
row_rate * flt(d.get("qty"), qty_precision), rate_precision
)
if amount_below_billed_amt and row_rate > 0.0:
frappe.throw(
_("Row #{0}: Cannot set Rate if amount is greater than billed amount for Item {1}.").format(
child_item.idx, child_item.item_code
)
)
else:
child_item.rate = flt(d.get("rate"), rate_precision)
child_item.rate = row_rate

if d.get("conversion_factor"):
if child_item.stock_uom == child_item.uom:
Expand Down
29 changes: 29 additions & 0 deletions erpnext/selling/doctype/sales_order/test_sales_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ def tearDownClass(cls) -> None:
def tearDown(self):
frappe.set_user("Administrator")

def test_sales_order_with_negative_rate(self):
"""
Test if negative rate is allowed in Sales Order via doc submission and update items
"""
so = make_sales_order(qty=1, rate=100, do_not_save=True)
so.append("items", {"item_code": "_Test Item", "qty": 1, "rate": -10})
so.save()
so.submit()

first_item = so.get("items")[0]
second_item = so.get("items")[1]
trans_item = json.dumps(
[
{
"item_code": first_item.item_code,
"rate": first_item.rate,
"qty": first_item.qty,
"docname": first_item.name,
},
{
"item_code": second_item.item_code,
"rate": -20,
"qty": second_item.qty,
"docname": second_item.name,
},
]
)
update_child_qty_rate("Sales Order", trans_item, so.name)

def test_make_material_request(self):
so = make_sales_order(do_not_submit=True)

Expand Down

0 comments on commit 619db8a

Please sign in to comment.