Skip to content

Commit

Permalink
feat: create Customer & Sales Order from utility request
Browse files Browse the repository at this point in the history
  • Loading branch information
muruthigitau committed Oct 15, 2024
1 parent 4ca5b09 commit 7792904
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import frappe
from frappe.model.document import Document
from frappe.utils import nowdate

from ...utils.create_meter_reading_rates import create_meter_reading_rates


Expand All @@ -20,7 +21,6 @@ def validate(self):
self.sales_order = sales_order.name



def create_sales_order(meter_reading):
"""Create a Sales Order based on the Meter Reading."""
sales_order = frappe.get_doc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ frappe.ui.form.on("Utility Service Request", {
let currentDate = frappe.datetime.nowdate();
frm.set_value("date", currentDate);
}
if (!frm.is_new()) {
frm.add_custom_button(
__("Sales Order"),
function () {
frappe.call({
method: "utility_billing.utility_billing.doctype.utility_service_request.utility_service_request.create_customer_and_sales_order",
args: {
docname: frm.doc.name,
},
callback: function (response) {
if (response.message) {
let sales_order_link = frappe.utils.get_form_link(
"Sales Order",
response.message.sales_order
);

frappe.msgprint({
title: __("Success"),
message: __(
"Sales Order <a href='{0}'>{1}</a> created successfully.",
[sales_order_link, response.message.sales_order]
),
indicator: "green",
});
}
},
});
},
__("Create")
).addClass("btn-primary");
frm.page.set_inner_btn_group_as_primary(__("Create"));
}
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"field_order": [
"naming_series",
"customer_name",
"customer",
"customer_type",
"customer_group",
"column_break_nqzi",
Expand All @@ -22,10 +23,10 @@
"connection_size",
"pipes_typedistance",
"column_break_bfhu",
"east_cordinates",
"water_network",
"south_cordinates",
"column_break_omtp",
"east_cordinates",
"south_cordinates",
"accounting_dimensions_section",
"cost_center",
"column_break_vjsy",
Expand Down Expand Up @@ -258,12 +259,18 @@
{
"fieldname": "column_break_omtp",
"fieldtype": "Column Break"
},
{
"fieldname": "customer",
"fieldtype": "Link",
"label": "Customer",
"options": "Customer"
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2024-10-14 12:09:57.615331",
"modified": "2024-10-15 07:45:18.266932",
"modified_by": "Administrator",
"module": "Utility Billing",
"name": "Utility Service Request",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,58 @@ def on_submit(self):
}
)
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
}

def create_customer(doc):
"""
Create a new customer if not already linked to the Utility Service Request.
"""
if not doc.customer:
customer_doc = frappe.new_doc("Customer")
customer_doc.customer_name = doc.customer_name
customer_doc.customer_type = doc.customer_type
customer_doc.customer_group = doc.customer_group
customer_doc.territory = doc.territory
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')
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
})

sales_order_doc.insert()


doc.customer = customer_doc.name

if auto_submit_sales_order != "Draft" :
sales_order_doc.submit()

return sales_order_doc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import frappe


def create_meter_reading_rates(meter_reading, price_list, reading_date):
"""Create Meter Reading Rate documents based on the Meter Reading date and append to its rates child table."""
meter_reading.set("rates", [])
Expand Down Expand Up @@ -52,4 +53,3 @@ def create_meter_reading_rates(meter_reading, price_list, reading_date):
)

total_consumption -= slab_quantity

0 comments on commit 7792904

Please sign in to comment.