mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-17 16:45:02 +00:00
fix: saperated validations for each purpose of validation
(cherry picked from commit 07d1a0ed9c)
# Conflicts:
# erpnext/assets/doctype/asset_movement/asset_movement.py
This commit is contained in:
@@ -62,8 +62,8 @@ frappe.ui.form.on("Asset Movement", {
|
||||
fieldnames_to_be_altered = {
|
||||
target_location: { read_only: 0, reqd: 1 },
|
||||
source_location: { read_only: 1, reqd: 0 },
|
||||
from_employee: { read_only: 0, reqd: 0 },
|
||||
to_employee: { read_only: 1, reqd: 0 },
|
||||
from_employee: { read_only: 1, reqd: 0 },
|
||||
to_employee: { read_only: 0, reqd: 0 },
|
||||
};
|
||||
} else if (frm.doc.purpose === "Issue") {
|
||||
fieldnames_to_be_altered = {
|
||||
@@ -72,6 +72,13 @@ frappe.ui.form.on("Asset Movement", {
|
||||
from_employee: { read_only: 1, reqd: 0 },
|
||||
to_employee: { read_only: 0, reqd: 1 },
|
||||
};
|
||||
} else if (frm.doc.purpose === "Transfer and Issue") {
|
||||
fieldnames_to_be_altered = {
|
||||
target_location: { read_only: 0, reqd: 1 },
|
||||
source_location: { read_only: 0, reqd: 1 },
|
||||
from_employee: { read_only: 0, reqd: 1 },
|
||||
to_employee: { read_only: 0, reqd: 1 },
|
||||
};
|
||||
}
|
||||
if (fieldnames_to_be_altered) {
|
||||
Object.keys(fieldnames_to_be_altered).forEach((fieldname) => {
|
||||
|
||||
@@ -31,47 +31,42 @@ class AssetMovement(Document):
|
||||
# end: auto-generated types
|
||||
|
||||
def validate(self):
|
||||
self.validate_asset()
|
||||
self.validate_location()
|
||||
self.validate_employee()
|
||||
|
||||
def validate_asset(self):
|
||||
for d in self.assets:
|
||||
status, company = frappe.db.get_value("Asset", d.asset, ["status", "company"])
|
||||
if self.purpose == "Transfer" and status in ("Draft", "Scrapped", "Sold"):
|
||||
frappe.throw(_("{0} asset cannot be transferred").format(status))
|
||||
self.validate_asset(d)
|
||||
self.validate_movement(d)
|
||||
|
||||
if company != self.company:
|
||||
frappe.throw(_("Asset {0} does not belong to company {1}").format(d.asset, self.company))
|
||||
def validate_asset(self, d):
|
||||
status, company = frappe.db.get_value("Asset", d.asset, ["status", "company"])
|
||||
if self.purpose == "Transfer" and status in ("Draft", "Scrapped", "Sold"):
|
||||
frappe.throw(_("{0} asset cannot be transferred").format(status))
|
||||
|
||||
if not (d.source_location or d.target_location or d.from_employee or d.to_employee):
|
||||
frappe.throw(_("Either location or employee must be required"))
|
||||
if company != self.company:
|
||||
frappe.throw(_("Asset {0} does not belong to company {1}").format(d.asset, self.company))
|
||||
|
||||
def validate_location(self):
|
||||
for d in self.assets:
|
||||
if self.purpose in ["Transfer", "Issue"]:
|
||||
current_location = frappe.db.get_value("Asset", d.asset, "location")
|
||||
if d.source_location:
|
||||
if current_location != d.source_location:
|
||||
frappe.throw(
|
||||
_("Asset {0} does not belongs to the location {1}").format(
|
||||
d.asset, d.source_location
|
||||
)
|
||||
)
|
||||
else:
|
||||
d.source_location = current_location
|
||||
def validate_movement(self, d):
|
||||
if self.purpose == "Transfer and Issue":
|
||||
self.validate_location_and_employee(d)
|
||||
elif self.purpose in ["Receipt", "Transfer"]:
|
||||
self.validate_location(d)
|
||||
else:
|
||||
self.validate_employee(d)
|
||||
|
||||
if self.purpose == "Issue":
|
||||
if d.target_location:
|
||||
def validate_location_and_employee(self, d):
|
||||
self.validate_location(d)
|
||||
self.validate_employee(d)
|
||||
|
||||
def validate_location(self, d):
|
||||
if self.purpose in ["Transfer", "Transfer and Issue"]:
|
||||
current_location = frappe.db.get_value("Asset", d.asset, "location")
|
||||
if d.source_location:
|
||||
if current_location != d.source_location:
|
||||
frappe.throw(
|
||||
_(
|
||||
"Issuing cannot be done to a location. Please enter employee to issue the Asset {0} to"
|
||||
).format(d.asset),
|
||||
title=_("Incorrect Movement Purpose"),
|
||||
_("Asset {0} does not belongs to the location {1}").format(d.asset, d.source_location)
|
||||
)
|
||||
if not d.to_employee:
|
||||
frappe.throw(_("Employee is required while issuing Asset {0}").format(d.asset))
|
||||
else:
|
||||
d.source_location = current_location
|
||||
|
||||
<<<<<<< HEAD
|
||||
if self.purpose == "Transfer":
|
||||
if d.to_employee:
|
||||
frappe.throw(
|
||||
@@ -117,12 +112,42 @@ class AssetMovement(Document):
|
||||
frappe.throw(
|
||||
_("Asset {0} does not belongs to the custodian {1}").format(d.asset, d.from_employee)
|
||||
)
|
||||
=======
|
||||
if not d.target_location:
|
||||
frappe.throw(_("Target Location is required for transferring Asset {0}").format(d.asset))
|
||||
if d.source_location == d.target_location:
|
||||
frappe.throw(_("Source and Target Location cannot be same"))
|
||||
>>>>>>> 07d1a0ed9c (fix: saperated validations for each purpose of validation)
|
||||
|
||||
if self.purpose == "Receipt":
|
||||
if not d.target_location:
|
||||
frappe.throw(_("Target Location is required while receiving Asset {0}").format(d.asset))
|
||||
if d.to_employee and frappe.db.get_value("Employee", d.to_employee, "company") != self.company:
|
||||
frappe.throw(
|
||||
_("Employee {0} does not belongs to the company {1}").format(d.to_employee, self.company)
|
||||
)
|
||||
|
||||
def validate_employee(self, d):
|
||||
if self.purpose == "Tranfer and Issue":
|
||||
if not d.from_employee:
|
||||
frappe.throw(_("From Employee is required while issuing Asset {0}").format(d.asset))
|
||||
|
||||
if d.from_employee:
|
||||
current_custodian = frappe.db.get_value("Asset", d.asset, "custodian")
|
||||
|
||||
if current_custodian != d.from_employee:
|
||||
frappe.throw(
|
||||
_("Asset {0} does not belongs to the custodian {1}").format(d.asset, d.from_employee)
|
||||
)
|
||||
|
||||
if not d.to_employee:
|
||||
frappe.throw(_("Employee is required while issuing Asset {0}").format(d.asset))
|
||||
|
||||
if d.to_employee and frappe.db.get_value("Employee", d.to_employee, "company") != self.company:
|
||||
frappe.throw(
|
||||
_("Employee {0} does not belongs to the company {1}").format(d.to_employee, self.company)
|
||||
)
|
||||
|
||||
def on_submit(self):
|
||||
self.set_latest_location_and_custodian_in_asset()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user