mirror of
https://github.com/frappe/erpnext.git
synced 2026-10-29 22:57:42 +00:00
fix: validate installation note rows against the delivery note
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.utils import cstr, getdate
|
||||
from frappe.utils import cstr, escape_html, getdate
|
||||
|
||||
from erpnext.stock.utils import get_valid_serial_nos
|
||||
from erpnext.utilities.transaction_base import TransactionBase
|
||||
@@ -65,11 +65,53 @@ class InstallationNote(TransactionBase):
|
||||
def validate(self):
|
||||
self.validate_installation_date()
|
||||
self.check_item_table()
|
||||
self.validate_with_previous_doc()
|
||||
|
||||
from erpnext.controllers.selling_controller import set_default_income_account_for_item
|
||||
|
||||
set_default_income_account_for_item(self)
|
||||
|
||||
def validate_with_previous_doc(self):
|
||||
self.validate_prevdoc_references()
|
||||
super().validate_with_previous_doc(
|
||||
{
|
||||
"Delivery Note": {
|
||||
"ref_dn_field": "prevdoc_docname",
|
||||
"compare_fields": [["customer", "="], ["company", "="]],
|
||||
},
|
||||
"Delivery Note Item": {
|
||||
"ref_dn_field": "prevdoc_detail_docname",
|
||||
"compare_fields": [["item_code", "="]],
|
||||
"is_child_table": True,
|
||||
"allow_duplicate_prev_row_id": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
def validate_prevdoc_references(self):
|
||||
for d in self.get("items"):
|
||||
if not d.prevdoc_detail_docname:
|
||||
continue
|
||||
|
||||
dn_item = frappe.db.get_value(
|
||||
"Delivery Note Item", d.prevdoc_detail_docname, ["parent", "docstatus"], as_dict=True
|
||||
)
|
||||
if not dn_item or dn_item.docstatus != 1:
|
||||
frappe.throw(
|
||||
_("Row #{0}: Installation can only be recorded against a submitted Delivery Note").format(
|
||||
d.idx
|
||||
)
|
||||
)
|
||||
|
||||
# the referenced row must belong to the named Delivery Note, else a submit could
|
||||
# update an unrelated customer's Delivery Note row
|
||||
if dn_item.parent != d.prevdoc_docname:
|
||||
frappe.throw(
|
||||
_("Row #{0}: Delivery Note Item does not belong to Delivery Note {1}").format(
|
||||
d.idx, escape_html(cstr(d.prevdoc_docname))
|
||||
)
|
||||
)
|
||||
|
||||
def is_serial_no_added(self, item_code, serial_no):
|
||||
has_serial_no = frappe.db.get_value("Item", item_code, "has_serial_no")
|
||||
if has_serial_no == 1 and not serial_no:
|
||||
|
||||
@@ -1,10 +1,43 @@
|
||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# See license.txt
|
||||
|
||||
import unittest
|
||||
import frappe
|
||||
from frappe.tests.utils import FrappeTestCase
|
||||
from frappe.utils import nowdate
|
||||
|
||||
# test_records = frappe.get_test_records('Installation Note')
|
||||
from erpnext.stock.doctype.delivery_note.delivery_note import make_installation_note
|
||||
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
|
||||
from erpnext.stock.doctype.item.test_item import make_item
|
||||
|
||||
|
||||
class TestInstallationNote(unittest.TestCase):
|
||||
pass
|
||||
class TestInstallationNote(FrappeTestCase):
|
||||
def setUp(self):
|
||||
frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
|
||||
|
||||
def test_item_must_match_delivery_note_row(self):
|
||||
dn = create_delivery_note(qty=5)
|
||||
note = make_installation_note(dn.name)
|
||||
note.inst_date = nowdate()
|
||||
note.items[0].item_code = make_item(properties={"is_stock_item": 0}).name
|
||||
|
||||
self.assertRaises(frappe.ValidationError, note.insert)
|
||||
|
||||
def test_cannot_install_against_cancelled_delivery_note(self):
|
||||
dn = create_delivery_note(qty=5)
|
||||
note = make_installation_note(dn.name)
|
||||
note.inst_date = nowdate()
|
||||
note.insert()
|
||||
|
||||
dn.cancel()
|
||||
|
||||
self.assertRaises(frappe.ValidationError, note.submit)
|
||||
|
||||
def test_cannot_install_against_foreign_delivery_row(self):
|
||||
own = create_delivery_note(qty=5)
|
||||
foreign = create_delivery_note(qty=5, customer="_Test Customer 1")
|
||||
|
||||
note = make_installation_note(own.name)
|
||||
note.inst_date = nowdate()
|
||||
note.items[0].prevdoc_detail_docname = foreign.items[0].name
|
||||
|
||||
self.assertRaises(frappe.ValidationError, note.insert)
|
||||
|
||||
Reference in New Issue
Block a user