From 668d090eed8f51ecc3e62cb600f6bfc416d99644 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 5 Jul 2016 13:14:30 +0530 Subject: [PATCH] Fetch deatils from Item and Asset Category --- erpnext/accounts/doctype/asset/asset.js | 22 ++++++++- erpnext/accounts/doctype/asset/asset.json | 55 ++++++++++++----------- erpnext/accounts/doctype/asset/asset.py | 26 +++++++++-- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/erpnext/accounts/doctype/asset/asset.js b/erpnext/accounts/doctype/asset/asset.js index e6d8b3b3f1..2cd800a2b8 100644 --- a/erpnext/accounts/doctype/asset/asset.js +++ b/erpnext/accounts/doctype/asset/asset.js @@ -8,7 +8,9 @@ frappe.ui.form.on('Asset', { frm.set_query("item_code", function() { return { "filters": { - "disabled": 0 + "disabled": 0, + "is_fixed_asset": 1, + "is_stock_item": 0 } }; }); @@ -113,6 +115,24 @@ frappe.ui.form.on('Asset', { }); }, + item_code: function(frm) { + if(frm.doc.item_code) { + frappe.call({ + method: "erpnext.accounts.doctype.asset.asset.get_item_details", + args: { + item_code: frm.doc.item_code + }, + callback: function(r, rt) { + if(r.message) { + $.each(r.message, function(field, value) { + frm.set_value(field, value); + }) + } + } + }) + } + }, + is_existing_asset: function(frm) { frm.toggle_enable("supplier", frm.doc.is_existing_asset); frm.toggle_reqd("next_depreciation_date", !frm.doc.is_existing_asset); diff --git a/erpnext/accounts/doctype/asset/asset.json b/erpnext/accounts/doctype/asset/asset.json index fa53aedd9d..612574d2aa 100644 --- a/erpnext/accounts/doctype/asset/asset.json +++ b/erpnext/accounts/doctype/asset/asset.json @@ -35,32 +35,6 @@ "set_only_once": 0, "unique": 0 }, - { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "fieldname": "asset_category", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "label": "Asset Category", - "length": 0, - "no_copy": 0, - "options": "Asset Category", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "report_hide": 0, - "reqd": 1, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, { "allow_on_submit": 0, "bold": 0, @@ -87,6 +61,32 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "asset_category", + "fieldtype": "Link", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Asset Category", + "length": 0, + "no_copy": 0, + "options": "Asset Category", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 1, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_on_submit": 0, "bold": 0, @@ -733,13 +733,14 @@ "hide_toolbar": 0, "idx": 72, "image_field": "image", + "image_view": 0, "in_create": 0, "in_dialog": 0, "is_submittable": 1, "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2016-05-30 18:09:56.158782", + "modified": "2016-07-05 12:54:38.585259", "modified_by": "Administrator", "module": "Accounts", "name": "Asset", diff --git a/erpnext/accounts/doctype/asset/asset.py b/erpnext/accounts/doctype/asset/asset.py index 23bb08be59..366a02cd4e 100644 --- a/erpnext/accounts/doctype/asset/asset.py +++ b/erpnext/accounts/doctype/asset/asset.py @@ -30,9 +30,16 @@ class Asset(Document): self.set_status() def validate_item(self): - item = frappe.get_doc("Item", self.item_code) - if item.disabled: + item = frappe.db.get_value("Item", self.item_code, + ["is_fixed_asset", "is_stock_item", "disabled"], as_dict=1) + if not item: + frappe.throw(_("Item {0} does not exist").format(self.item_code)) + elif item.disabled: frappe.throw(_("Item {0} has been disabled").format(self.item_code)) + elif not item.is_fixed_asset: + frappe.throw(_("Item {0} must be a Fixed Asset Item").format(self.item_code)) + elif item.is_stock_item: + frappe.throw(_("Item {0} must be a non-stock item").format(self.item_code)) def validate_asset_values(self): self.value_after_depreciation = flt(self.gross_purchase_amount) - flt(self.opening_accumulated_depreciation) @@ -203,4 +210,17 @@ def transfer_asset(args): frappe.db.commit() - frappe.msgprint(_("Asset Movement record {0} created").format("{0}".format(movement_entry.name))) \ No newline at end of file + frappe.msgprint(_("Asset Movement record {0} created").format("{0}".format(movement_entry.name))) + +@frappe.whitelist() +def get_item_details(item_code): + asset_category = frappe.db.get_value("Item", item_code, "asset_category") + + ret = frappe.db.get_value("Asset Category", asset_category, + ["depreciation_method", "total_number_of_depreciations", "frequency_of_depreciation"], as_dict=1) + + ret.update({ + "asset_category": asset_category + }) + + return ret \ No newline at end of file