From f91e28d6ba95495166b26c11bb4b175eabef16c0 Mon Sep 17 00:00:00 2001 From: anhilmy Date: Wed, 24 Jul 2024 13:55:35 +0700 Subject: [PATCH] feat: add purchase order history in purcahse order --- inn/fixtures/custom_field.json | 282 ++++++++++++++++++ inn/hooks.py | 17 +- .../accounts/purchase_order/__init__.py | 0 .../accounts/purchase_order/purchase_order.py | 33 ++ .../js/purchase-order/purchase-order.js | 19 ++ 5 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 inn/fixtures/custom_field.json create mode 100644 inn/overrides/erpnext/accounts/purchase_order/__init__.py create mode 100644 inn/overrides/erpnext/accounts/purchase_order/purchase_order.py create mode 100644 inn/public/js/purchase-order/purchase-order.js diff --git a/inn/fixtures/custom_field.json b/inn/fixtures/custom_field.json new file mode 100644 index 0000000..aaca3e9 --- /dev/null +++ b/inn/fixtures/custom_field.json @@ -0,0 +1,282 @@ +[ + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 1, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "Purchase Order Item", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_recent_order", + "fieldtype": "Section Break", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "stock_qty", + "is_system_generated": 0, + "is_virtual": 0, + "label": "Recent Order", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2024-07-24 13:06:15.955365", + "module": "Inn Hotels", + "name": "Purchase Order Item-custom_recent_order", + "no_copy": 0, + "non_negative": 0, + "options": null, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 0, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "show_dashboard": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + }, + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 1, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "Purchase Order Item", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_last_purchased_quantity", + "fieldtype": "Int", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "custom_recent_order", + "is_system_generated": 0, + "is_virtual": 0, + "label": "Last Purchased Quantity", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2024-07-24 13:08:10.741623", + "module": "Inn Hotels", + "name": "Purchase Order Item-custom_last_purchased_quantity", + "no_copy": 0, + "non_negative": 0, + "options": null, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 1, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "show_dashboard": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + }, + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 1, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "Purchase Order Item", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_col_br1", + "fieldtype": "Column Break", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "custom_last_purchased_quantity", + "is_system_generated": 0, + "is_virtual": 0, + "label": "", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2024-07-24 13:11:53.292843", + "module": "Inn Hotels", + "name": "Purchase Order Item-custom_col_br1", + "no_copy": 0, + "non_negative": 0, + "options": "", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 0, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "show_dashboard": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + }, + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 1, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "Purchase Order Item", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_last_purchase_order", + "fieldtype": "Link", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "custom_col_br1", + "is_system_generated": 0, + "is_virtual": 0, + "label": "Last Purchase Order", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2024-07-24 13:14:00.775785", + "module": "Inn Hotels", + "name": "Purchase Order Item-custom_last_purchase_order", + "no_copy": 0, + "non_negative": 0, + "options": "Purchase Order", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 1, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "show_dashboard": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + }, + { + "allow_in_quick_entry": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 1, + "collapsible_depends_on": null, + "columns": 0, + "default": null, + "depends_on": null, + "description": null, + "docstatus": 0, + "doctype": "Custom Field", + "dt": "Purchase Order Item", + "fetch_from": null, + "fetch_if_empty": 0, + "fieldname": "custom_last_purchase_date", + "fieldtype": "Date", + "hidden": 0, + "hide_border": 0, + "hide_days": 0, + "hide_seconds": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_preview": 0, + "in_standard_filter": 0, + "insert_after": "custom_last_purchase_order", + "is_system_generated": 0, + "is_virtual": 0, + "label": "Last Purchase Date", + "length": 0, + "link_filters": null, + "mandatory_depends_on": null, + "modified": "2024-07-24 13:46:45.136947", + "module": "Inn Hotels", + "name": "Purchase Order Item-custom_last_purchase_date", + "no_copy": 0, + "non_negative": 0, + "options": "", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "print_width": null, + "read_only": 1, + "read_only_depends_on": null, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "show_dashboard": 0, + "sort_options": 0, + "translatable": 0, + "unique": 0, + "width": null + } +] \ No newline at end of file diff --git a/inn/hooks.py b/inn/hooks.py index 4202655..4a2d167 100644 --- a/inn/hooks.py +++ b/inn/hooks.py @@ -22,7 +22,9 @@ # page_js = {"point-of-sale" : "public/js/inn-pos.bundle.js"} # include js in doctype views -# doctype_js = {"doctype" : "public/js/doctype.js"} +doctype_js = { + "Purchase Order" : "public/js/purchase-order/purchase-order.js" +} # doctype_list_js = {"doctype" : "public/js/doctype_list.js"} # doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"} # doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"} @@ -119,6 +121,19 @@ # ] } +# Fixture +# --------- + +fixtures = [ + { + "doctype": "Custom Field", + "filters": [ + ["module", "=", "Inn Hotels"] + ] + } +] + + # Testing # ------- diff --git a/inn/overrides/erpnext/accounts/purchase_order/__init__.py b/inn/overrides/erpnext/accounts/purchase_order/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/inn/overrides/erpnext/accounts/purchase_order/purchase_order.py b/inn/overrides/erpnext/accounts/purchase_order/purchase_order.py new file mode 100644 index 0000000..93213d8 --- /dev/null +++ b/inn/overrides/erpnext/accounts/purchase_order/purchase_order.py @@ -0,0 +1,33 @@ +import frappe + + +@frappe.whitelist() +def get_last_purchase_request(item_code): + result = { + "last_purchased_quantity": 0, + "last_purchase_order": "", + "transaction_date": "" + } + if not frappe.db.exists("Item", item_code, cache=True): + return result + + query = f''' + select tpoi.parent as parent, tpoi.qty as qty, tpo.transaction_date as transaction_date + from `tabPurchase Order Item` tpoi + left join `tabPurchase Order` tpo + on tpo.name = tpoi.parent + where tpoi.item_code = '{item_code}' + and tpo.status != 'Draft' + order by tpo.transaction_date desc + limit 1 + ''' + + last = frappe.db.sql(query, as_dict=1) + if len(last) == 0: + return result + last = last[0] + + result["last_purchased_quantity"] = last.qty + result["last_purchase_order"] = last.parent + result["transaction_date"] = last.transaction_date + return result diff --git a/inn/public/js/purchase-order/purchase-order.js b/inn/public/js/purchase-order/purchase-order.js new file mode 100644 index 0000000..2f1dfc0 --- /dev/null +++ b/inn/public/js/purchase-order/purchase-order.js @@ -0,0 +1,19 @@ +frappe.ui.form.on("Purchase Order Item", { + + item_code: async function (frm, cdt, cdn) { + var row = locals[cdt][cdn] + console.log("called") + + let result = await frappe.call({ + method: "inn.overrides.erpnext.accounts.purchase_order.purchase_order.get_last_purchase_request", + args: { + item_code: row.item_code + } + }) + result = result.message + row.custom_last_purchased_quantity = result.last_purchased_quantity + row.custom_last_purchase_order = result.last_purchase_order + row.custom_last_purchase_date = result.transaction_date + refresh_field("items") + } +}) \ No newline at end of file