From 4376119dc608387883dbe4184852b4a503acbf05 Mon Sep 17 00:00:00 2001 From: muruthigitau Date: Tue, 15 Oct 2024 09:27:32 +0300 Subject: [PATCH 1/2] feat: customer create service request button --- utility_billing/hooks.py | 1 + .../doctype/meter_reading/meter_reading.py | 24 ++++------ .../utility_service_request.js | 29 +++++++++++ .../utility_service_request.py | 48 +++++++------------ .../overrides/client/customer.js | 16 +++++++ 5 files changed, 73 insertions(+), 45 deletions(-) create mode 100644 utility_billing/utility_billing/overrides/client/customer.js diff --git a/utility_billing/hooks.py b/utility_billing/hooks.py index 92d1504..b661936 100644 --- a/utility_billing/hooks.py +++ b/utility_billing/hooks.py @@ -73,6 +73,7 @@ # include js in doctype views doctype_js = {"Item Price": "utility_billing/overrides/client/item_price.js"} +doctype_js = {"Customer": "utility_billing/overrides/client/customer.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"} diff --git a/utility_billing/utility_billing/doctype/meter_reading/meter_reading.py b/utility_billing/utility_billing/doctype/meter_reading/meter_reading.py index 6b9b9c5..a5bd4b9 100644 --- a/utility_billing/utility_billing/doctype/meter_reading/meter_reading.py +++ b/utility_billing/utility_billing/doctype/meter_reading/meter_reading.py @@ -65,17 +65,13 @@ def get_previous_reading(meter_number): @frappe.whitelist() def get_customer_details(customer): - """Fetch customer details such as name and default price list.""" - customer_details = frappe.db.get_value( - "Customer", - customer, - ["customer_name", "default_price_list", "territory"], - as_dict=True, - ) - if customer_details: - if not customer_details.default_price_list: - customer_group = frappe.db.get_value("Customer", customer, "customer_group") - customer_details.default_price_list = frappe.db.get_value( - "Customer Group", customer_group, "default_price_list" - ) - return customer_details or {} + """Fetch all customer details, including the default price list and other fields.""" + + customer_doc = frappe.get_doc("Customer", customer) + + if not customer_doc.default_price_list: + customer_doc.default_price_list = frappe.db.get_value( + "Customer Group", customer_doc.customer_group, "default_price_list" + ) + + return customer_doc.as_dict() diff --git a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js index 55e9e10..ba27a8a 100644 --- a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js +++ b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js @@ -43,6 +43,15 @@ frappe.ui.form.on("Utility Service Request", { ).addClass("btn-primary"); frm.page.set_inner_btn_group_as_primary(__("Create")); } + if (frm.doc.customer) { + fetch_customer_details(frm); + } + }, + + customer: function (frm) { + if (frm.doc.customer) { + fetch_customer_details(frm); + } }, }); @@ -123,3 +132,23 @@ frappe.ui.form.on("Utility Service Request Item", { } }, }); + +function fetch_customer_details(frm) { + frappe.call({ + method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_customer_details", + args: { + customer: frm.doc.customer, + }, + callback: function (r) { + if (r.message) { + frm.set_value("customer_name", r.message.customer_name); + frm.set_value("territory", r.message.territory); + frm.set_value("customer_group", r.message.customer_group); + frm.set_value("customer_type", r.message.customer_type); + frm.set_value("company", r.message.company); + frm.set_value("tax_id", r.message.tax_id); + frm.set_value("nrcpassport_no", r.message.nrc_or_passport_no); + } + }, + }); +} diff --git a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py index ff7d9a4..a808823 100644 --- a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py +++ b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py @@ -10,32 +10,14 @@ class UtilityServiceRequest(Document): def onload(self): load_address_and_contact(self) - def on_submit(self): - if not frappe.db.exists("Customer", self.customer_name): - customer = frappe.get_doc( - { - "doctype": "Customer", - "customer_name": self.customer_name, - "customer_type": self.customer_type, - "customer_group": self.customer_group, - "territory": self.territory, - "tax_id": self.tax_id, - "nrc_or_passport_no": self.nrcpassport_no, - "company": self.company, - } - ) - customer.insert() - - @frappe.whitelist() def create_customer_and_sales_order(docname): doc = frappe.get_doc("Utility Service Request", docname) customer_doc = create_customer(doc) sales_order_doc = create_sales_order(doc, customer_doc) - return { - "sales_order": sales_order_doc.name - } + return {"sales_order": sales_order_doc.name} + def create_customer(doc): """ @@ -47,36 +29,40 @@ def create_customer(doc): customer_doc.customer_type = doc.customer_type customer_doc.customer_group = doc.customer_group customer_doc.territory = doc.territory + customer_doc.tax_id = doc.tax_id + customer_doc.nrc_or_passport_no = doc.nrcpassport_no + customer_doc.company = doc.company customer_doc.insert() doc.customer = customer_doc.name doc.save() else: customer_doc = frappe.get_doc("Customer", doc.customer) - + return customer_doc + def create_sales_order(doc, customer_doc): """ Create a new Sales Order linked to the provided customer. """ - auto_submit_sales_order = frappe.db.get_single_value('Utility Billing Settings', 'sales_order_creation_state') + auto_submit_sales_order = frappe.db.get_single_value( + "Utility Billing Settings", "sales_order_creation_state" + ) sales_order_doc = frappe.new_doc("Sales Order") sales_order_doc.customer = customer_doc.name sales_order_doc.transaction_date = frappe.utils.nowdate() - for item in doc.items: - sales_order_doc.append("items", { - "item_code": item.item_code, - "qty": item.qty or 1, - "rate": item.rate or 0 - }) + for item in doc.items: + sales_order_doc.append( + "items", + {"item_code": item.item_code, "qty": item.qty or 1, "rate": item.rate or 0}, + ) sales_order_doc.insert() - - + doc.customer = customer_doc.name - if auto_submit_sales_order != "Draft" : + if auto_submit_sales_order != "Draft": sales_order_doc.submit() return sales_order_doc diff --git a/utility_billing/utility_billing/overrides/client/customer.js b/utility_billing/utility_billing/overrides/client/customer.js new file mode 100644 index 0000000..953d681 --- /dev/null +++ b/utility_billing/utility_billing/overrides/client/customer.js @@ -0,0 +1,16 @@ +// Copyright (c) 2024, Navari and contributors +// For license information, please see license.txt + +frappe.ui.form.on("Customer", { + refresh: function (frm) { + frm.add_custom_button( + __("Utility Service Request"), + function () { + frappe.new_doc("Utility Service Request", { + customer: frm.doc.name, + }); + }, + __("Create") + ); + }, +}); From dc6fbc62a9274e3b8c6e75484d9d9f3753ce11f9 Mon Sep 17 00:00:00 2001 From: muruthigitau Date: Tue, 15 Oct 2024 10:10:57 +0300 Subject: [PATCH 2/2] feat: doctype links for sales order --- utility_billing/fixtures/doctype_link.json | 36 +++++++++++++++++++ utility_billing/hooks.py | 7 ++++ .../utility_service_request.json | 9 ++++- .../utility_service_request.py | 3 +- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 utility_billing/fixtures/doctype_link.json diff --git a/utility_billing/fixtures/doctype_link.json b/utility_billing/fixtures/doctype_link.json new file mode 100644 index 0000000..ad2a7c0 --- /dev/null +++ b/utility_billing/fixtures/doctype_link.json @@ -0,0 +1,36 @@ +[ + { + "custom": 0, + "docstatus": 0, + "doctype": "DocType Link", + "group": "Utility Billing", + "hidden": 0, + "is_child_table": 0, + "link_doctype": "Utility Service Request", + "link_fieldname": "sales_order", + "modified": "2024-10-15 09:05:23.141183", + "name": "p9tpcelsh8", + "parent": "Sales Order", + "parent_doctype": null, + "parentfield": "links", + "parenttype": "DocType", + "table_fieldname": null + }, + { + "custom": 0, + "docstatus": 0, + "doctype": "DocType Link", + "group": "Utility Billing", + "hidden": 0, + "is_child_table": 0, + "link_doctype": "Meter Reading", + "link_fieldname": "sales_order", + "modified": "2024-10-15 09:05:23.141183", + "name": "pig054jhm1", + "parent": "Sales Order", + "parent_doctype": null, + "parentfield": "links", + "parenttype": "DocType", + "table_fieldname": "sales_order" + } +] \ No newline at end of file diff --git a/utility_billing/hooks.py b/utility_billing/hooks.py index b661936..5479be1 100644 --- a/utility_billing/hooks.py +++ b/utility_billing/hooks.py @@ -31,6 +31,13 @@ ["module", "=", "Utility Billing"], ], }, + { + "doctype": "DocType Link", + "filters": [ + ["parent", "in", ["Sales Order"]], + ["group", "=", "Utility Billing"], + ], + }, ] diff --git a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.json b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.json index ae84c4f..bba08a9 100644 --- a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.json +++ b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.json @@ -15,6 +15,7 @@ "date", "request_type", "territory", + "sales_order", "column_break_wxld", "company", "tax_id", @@ -265,12 +266,18 @@ "fieldtype": "Link", "label": "Customer", "options": "Customer" + }, + { + "fieldname": "sales_order", + "fieldtype": "Link", + "label": "Sales Order", + "options": "Sales Order" } ], "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2024-10-15 07:45:18.266932", + "modified": "2024-10-15 08:34:46.547441", "modified_by": "Administrator", "module": "Utility Billing", "name": "Utility Service Request", diff --git a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py index a808823..6eb3341 100644 --- a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py +++ b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py @@ -60,7 +60,8 @@ def create_sales_order(doc, customer_doc): sales_order_doc.insert() - doc.customer = customer_doc.name + doc.sales_order = sales_order_doc.name + doc.save() if auto_submit_sales_order != "Draft": sales_order_doc.submit()