Skip to content

Commit

Permalink
feat: customer create service request button
Browse files Browse the repository at this point in the history
  • Loading branch information
muruthigitau committed Oct 15, 2024
1 parent 7792904 commit 4376119
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 45 deletions.
1 change: 1 addition & 0 deletions utility_billing/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
});

Expand Down Expand Up @@ -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);
}
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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
16 changes: 16 additions & 0 deletions utility_billing/utility_billing/overrides/client/customer.js
Original file line number Diff line number Diff line change
@@ -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")
);
},
});

0 comments on commit 4376119

Please sign in to comment.