mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-12 17:51:20 +00:00
refactor: split set_latest_location_and_custodian_in_asset into smaller functions
This commit is contained in:
@@ -107,53 +107,63 @@ class AssetMovement(Document):
|
|||||||
self.set_latest_location_and_custodian_in_asset()
|
self.set_latest_location_and_custodian_in_asset()
|
||||||
|
|
||||||
def set_latest_location_and_custodian_in_asset(self):
|
def set_latest_location_and_custodian_in_asset(self):
|
||||||
|
for d in self.assets:
|
||||||
|
current_location, current_employee = self.get_latest_location_and_custodian(d.asset)
|
||||||
|
self.update_asset_location_and_custodian(d.asset, current_location, current_employee)
|
||||||
|
self.log_asset_activity(d.asset, current_location, current_employee)
|
||||||
|
|
||||||
|
def get_latest_location_and_custodian(self, asset):
|
||||||
current_location, current_employee = "", ""
|
current_location, current_employee = "", ""
|
||||||
cond = "1=1"
|
cond = "1=1"
|
||||||
|
|
||||||
for d in self.assets:
|
# latest entry corresponds to current document's location, employee when transaction date > previous dates
|
||||||
args = {"asset": d.asset, "company": self.company}
|
# In case of cancellation it corresponds to previous latest document's location, employee
|
||||||
|
args = {"asset": asset, "company": self.company}
|
||||||
|
latest_movement_entry = frappe.db.sql(
|
||||||
|
f"""
|
||||||
|
SELECT asm_item.target_location, asm_item.to_employee
|
||||||
|
FROM `tabAsset Movement Item` asm_item
|
||||||
|
JOIN `tabAsset Movement` asm ON asm_item.parent = asm.name
|
||||||
|
WHERE
|
||||||
|
asm_item.asset = %(asset)s AND
|
||||||
|
asm.company = %(company)s AND
|
||||||
|
asm.docstatus = 1 AND {cond}
|
||||||
|
ORDER BY asm.transaction_date DESC
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
args,
|
||||||
|
)
|
||||||
|
|
||||||
# latest entry corresponds to current document's location, employee when transaction date > previous dates
|
if latest_movement_entry:
|
||||||
# In case of cancellation it corresponds to previous latest document's location, employee
|
current_location = latest_movement_entry[0][0]
|
||||||
latest_movement_entry = frappe.db.sql(
|
current_employee = latest_movement_entry[0][1]
|
||||||
f"""
|
|
||||||
SELECT asm_item.target_location, asm_item.to_employee
|
return current_location, current_employee
|
||||||
FROM `tabAsset Movement Item` asm_item, `tabAsset Movement` asm
|
|
||||||
WHERE
|
def update_asset_location_and_custodian(self, asset_id, location, employee):
|
||||||
asm_item.parent=asm.name and
|
asset = frappe.get_doc("Asset", asset_id)
|
||||||
asm_item.asset=%(asset)s and
|
|
||||||
asm.company=%(company)s and
|
if employee and employee != asset.custodian:
|
||||||
asm.docstatus=1 and {cond}
|
frappe.db.set_value("Asset", asset_id, "custodian", employee)
|
||||||
ORDER BY
|
if location and location != asset.location:
|
||||||
asm.transaction_date desc limit 1
|
frappe.db.set_value("Asset", asset_id, "location", location)
|
||||||
""",
|
|
||||||
args,
|
def log_asset_activity(self, asset_id, location, employee):
|
||||||
|
if location and employee:
|
||||||
|
add_asset_activity(
|
||||||
|
asset_id,
|
||||||
|
_("Asset received at Location {0} and issued to Employee {1}").format(
|
||||||
|
get_link_to_form("Location", location),
|
||||||
|
get_link_to_form("Employee", employee),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
elif location:
|
||||||
|
add_asset_activity(
|
||||||
|
asset_id,
|
||||||
|
_("Asset transferred to Location {0}").format(get_link_to_form("Location", location)),
|
||||||
|
)
|
||||||
|
elif employee:
|
||||||
|
add_asset_activity(
|
||||||
|
asset_id,
|
||||||
|
_("Asset issued to Employee {0}").format(get_link_to_form("Employee", employee)),
|
||||||
)
|
)
|
||||||
|
|
||||||
if latest_movement_entry:
|
|
||||||
current_location = latest_movement_entry[0][0]
|
|
||||||
current_employee = latest_movement_entry[0][1]
|
|
||||||
|
|
||||||
frappe.db.set_value("Asset", d.asset, "location", current_location, update_modified=False)
|
|
||||||
frappe.db.set_value("Asset", d.asset, "custodian", current_employee, update_modified=False)
|
|
||||||
|
|
||||||
if current_location and current_employee:
|
|
||||||
add_asset_activity(
|
|
||||||
d.asset,
|
|
||||||
_("Asset received at Location {0} and issued to Employee {1}").format(
|
|
||||||
get_link_to_form("Location", current_location),
|
|
||||||
get_link_to_form("Employee", current_employee),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
elif current_location:
|
|
||||||
add_asset_activity(
|
|
||||||
d.asset,
|
|
||||||
_("Asset transferred to Location {0}").format(
|
|
||||||
get_link_to_form("Location", current_location)
|
|
||||||
),
|
|
||||||
)
|
|
||||||
elif current_employee:
|
|
||||||
add_asset_activity(
|
|
||||||
d.asset,
|
|
||||||
_("Asset issued to Employee {0}").format(get_link_to_form("Employee", current_employee)),
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user