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.
This commit is contained in:
Ankush Menat 2022-05-12 18:17:09 +05:30
parent cb8cd7f5bc
commit ee0a277540
4 changed files with 65 additions and 14 deletions

View File

@ -307,14 +307,15 @@ class BuyingController(StockController, Subcontracting):
if self.is_internal_transfer(): if self.is_internal_transfer():
if rate != d.rate: if rate != d.rate:
d.rate = rate d.rate = rate
d.discount_percentage = 0
d.discount_amount = 0
frappe.msgprint( frappe.msgprint(
_( _(
"Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer" "Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer"
).format(d.idx), ).format(d.idx),
alert=1, 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): def get_supplied_items_cost(self, item_row_id, reset_outgoing_rate=True):
supplied_items_cost = 0.0 supplied_items_cost = 0.0

View File

@ -447,15 +447,16 @@ class SellingController(StockController):
rate = flt(d.incoming_rate * d.conversion_factor, d.precision("rate")) rate = flt(d.incoming_rate * d.conversion_factor, d.precision("rate"))
if d.rate != rate: if d.rate != rate:
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_percentage = 0.0
d.discount_amount = 0 d.discount_amount = 0.0
frappe.msgprint( d.margin_rate_or_amount = 0.0
_(
"Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer"
).format(d.idx),
alert=1,
)
elif self.get("return_against"): elif self.get("return_against"):
# Get incoming rate of return entry from reference document # Get incoming rate of return entry from reference document

View File

@ -367,7 +367,14 @@ def set_credit_limit(customer, company, credit_limit):
customer.credit_limits[-1].db_insert() 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): if not frappe.db.exists("Customer", customer_name):
customer = frappe.get_doc( customer = frappe.get_doc(
{ {

View File

@ -570,15 +570,12 @@ class TestDeliveryNote(FrappeTestCase):
customer=customer_name, customer=customer_name,
cost_center="Main - TCP1", cost_center="Main - TCP1",
expense_account="Cost of Goods Sold - TCP1", expense_account="Cost of Goods Sold - TCP1",
do_not_submit=True,
qty=5, qty=5,
rate=500, rate=500,
warehouse="Stores - TCP1", warehouse="Stores - TCP1",
target_warehouse=target_warehouse, target_warehouse=target_warehouse,
) )
dn.submit()
# qty after delivery # qty after delivery
actual_qty_at_source = get_qty_after_transaction(warehouse="Stores - TCP1") actual_qty_at_source = get_qty_after_transaction(warehouse="Stores - TCP1")
self.assertEqual(actual_qty_at_source, 475) self.assertEqual(actual_qty_at_source, 475)
@ -1000,6 +997,51 @@ class TestDeliveryNote(FrappeTestCase):
self.assertEqual(dn2.items[0].returned_qty, 0) self.assertEqual(dn2.items[0].returned_qty, 0)
self.assertEqual(dn2.per_billed, 100) 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): def create_delivery_note(**args):
dn = frappe.new_doc("Delivery Note") dn = frappe.new_doc("Delivery Note")