Skip to content

Commit

Permalink
fix: prevent bypassing forced valuation rate (backport #30987) (#31020)
Browse files Browse the repository at this point in the history
fix: prevent bypassing forced valuation rate

if you edit "margin_rate_or_amount" after saving DN then based on
selected margin the rate gets updated which isn't valuation rate.

(cherry picked from commit ee0a277)

Co-authored-by: Ankush Menat <me@ankush.dev>
  • Loading branch information
mergify[bot] and ankush authored May 15, 2022
1 parent c7f4a15 commit 706c19d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
5 changes: 3 additions & 2 deletions erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,15 @@ def set_incoming_rate(self):
if self.is_internal_transfer():
if rate != d.rate:
d.rate = rate
d.discount_percentage = 0
d.discount_amount = 0
frappe.msgprint(
_(
"Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer"
).format(d.idx),
alert=1,
)
d.discount_percentage = 0.0
d.discount_amount = 0.0
d.margin_rate_or_amount = 0.0

def get_supplied_items_cost(self, item_row_id, reset_outgoing_rate=True):
supplied_items_cost = 0.0
Expand Down
17 changes: 9 additions & 8 deletions erpnext/controllers/selling_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,16 @@ def set_incoming_rate(self):
rate = flt(d.incoming_rate * d.conversion_factor, d.precision("rate"))
if d.rate != rate:
d.rate = rate
frappe.msgprint(
_(
"Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer"
).format(d.idx),
alert=1,
)

d.discount_percentage = 0
d.discount_amount = 0
frappe.msgprint(
_(
"Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer"
).format(d.idx),
alert=1,
)
d.discount_percentage = 0.0
d.discount_amount = 0.0
d.margin_rate_or_amount = 0.0

elif self.get("return_against"):
# Get incoming rate of return entry from reference document
Expand Down
9 changes: 8 additions & 1 deletion erpnext/selling/doctype/customer/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,14 @@ def set_credit_limit(customer, company, credit_limit):
customer.credit_limits[-1].db_insert()


def create_internal_customer(customer_name, represents_company, allowed_to_interact_with):
def create_internal_customer(
customer_name=None, represents_company=None, allowed_to_interact_with=None
):
if not customer_name:
customer_name = represents_company
if not allowed_to_interact_with:
allowed_to_interact_with = represents_company

if not frappe.db.exists("Customer", customer_name):
customer = frappe.get_doc(
{
Expand Down
48 changes: 45 additions & 3 deletions erpnext/stock/doctype/delivery_note/test_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,12 @@ def test_delivery_of_bundled_items_to_target_warehouse(self):
customer=customer_name,
cost_center="Main - TCP1",
expense_account="Cost of Goods Sold - TCP1",
do_not_submit=True,
qty=5,
rate=500,
warehouse="Stores - TCP1",
target_warehouse=target_warehouse,
)

dn.submit()

# qty after delivery
actual_qty_at_source = get_qty_after_transaction(warehouse="Stores - TCP1")
self.assertEqual(actual_qty_at_source, 475)
Expand Down Expand Up @@ -1000,6 +997,51 @@ def test_returned_qty_in_return_dn(self):
self.assertEqual(dn2.items[0].returned_qty, 0)
self.assertEqual(dn2.per_billed, 100)

def test_internal_transfer_with_valuation_only(self):
from erpnext.selling.doctype.customer.test_customer import create_internal_customer

item = make_item().name
warehouse = "_Test Warehouse - _TC"
target = "Stores - _TC"
company = "_Test Company"
customer = create_internal_customer(represents_company=company)
rate = 42

frappe.get_doc(
{
"item_code": item,
"price_list": "Standard Selling",
"price_list_rate": 1000,
"doctype": "Item Price",
}
).insert()

make_stock_entry(target=warehouse, qty=5, basic_rate=rate, item_code=item)
dn = create_delivery_note(
item_code=item,
company=company,
customer=customer,
qty=5,
rate=500,
warehouse=warehouse,
target_warehouse=target,
do_not_save=True,
do_not_submit=True,
)

self.assertEqual(dn.items[0].rate, 500) # haven't saved yet
dn.save()

# rate should reset to incoming rate
self.assertEqual(dn.items[0].rate, rate)

# rate should reset again if discounts are fiddled with
dn.items[0].margin_type = "Amount"
dn.items[0].margin_rate_or_amount = 50
dn.save()

self.assertEqual(dn.items[0].rate, rate)


def create_delivery_note(**args):
dn = frappe.new_doc("Delivery Note")
Expand Down

0 comments on commit 706c19d

Please sign in to comment.