Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doctype links for sales order #2

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions utility_billing/fixtures/doctype_link.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
8 changes: 8 additions & 0 deletions utility_billing/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
["module", "=", "Utility Billing"],
],
},
{
"doctype": "DocType Link",
"filters": [
["parent", "in", ["Sales Order"]],
["group", "=", "Utility Billing"],
],
},
]


Expand Down Expand Up @@ -73,6 +80,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 @@ -15,6 +15,7 @@
"date",
"request_type",
"territory",
"sales_order",
"column_break_wxld",
"company",
"tax_id",
Expand Down Expand Up @@ -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",
Expand Down
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,41 @@ 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" :
doc.sales_order = sales_order_doc.name
doc.save()

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")
);
},
});
Loading