Skip to content

Commit

Permalink
Merge pull request #19 from navariltd/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
muruthigitau authored Oct 28, 2024
2 parents 7e42a1f + 3ae3746 commit 3330f07
Show file tree
Hide file tree
Showing 12 changed files with 846 additions and 732 deletions.
1,199 changes: 628 additions & 571 deletions utility_billing/fixtures/custom_field.json

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions utility_billing/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"BOM",
"Customer",
"Issue",
"Item",
"Item Price",
"Sales Order",
"Sales Invoice",
Expand Down Expand Up @@ -165,13 +166,20 @@
# ---------------
# Hook on document methods and events

# doc_events = {
# "*": {
# "on_update": "method",
# "on_cancel": "method",
# "on_trash": "method"
# }
# }
doc_events = {
# "*": {
# "on_update": "method",
# "on_cancel": "method",
# "on_trash": "method"
# }

"Sales Invoice": {
"before_validate": [
"utility_billing.utility_billing.overrides.server.sales_invoice.before_validate"
],
},

}

# Scheduled Tasks
# ---------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ frappe.ui.form.on("Meter Reading", {
return {
filters: {
is_sales_item: 1,
is_utility_item: 1,
has_variants: 0,
},
};
Expand Down Expand Up @@ -43,37 +44,45 @@ frappe.ui.form.on("Meter Reading", {
});
frm.refresh_field("items");
},
});
validate: function (frm) {
frm.doc.items.forEach((item) => {
if (item.current_reading >= 0 && item.previous_reading >= 0) {
const consumption = item.current_reading - item.previous_reading;

frappe.ui.form.on("Meter Reading Item", {
meter_number: function (frm, cdt, cdn) {
const row = locals[cdt][cdn];
if (row.meter_number) {
frappe.call({
method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_previous_reading",
args: {
meter_number: row.meter_number,
},
callback: function (r) {
row.previous_reading = r.message || 0;
frm.refresh_field("items");
},
});
}
if (consumption < 0) {
frappe.msgprint(
__("Current reading cannot be lower than previous reading.for item: {0}", [
item.item_code,
])
);
frappe.validated = false;
}
} else {
frappe.msgprint(
__(
"Please ensure that both current and previous readings are provided for item: {0}",
[item.item_code]
)
);
frappe.validated = false;
}
});
},
});

frappe.ui.form.on("Meter Reading Item", {
current_reading: function (frm, cdt, cdn) {
const row = locals[cdt][cdn];
if (row.previous_reading !== undefined || row.previous_reading !== 0) {
row.consumption = row.current_reading - row.previous_reading;
frm.refresh_field("items");

if (row.previous_reading >= 0 && row.current_reading >= 0) {
calculate_consumption(frm, row);
}
},
previous_reading: function (frm, cdt, cdn) {
const row = locals[cdt][cdn];
if (row.current_reading !== undefined || row.current_reading !== 0) {
row.consumption = row.current_reading - row.previous_reading;
frm.refresh_field("items");

if (row.current_reading >= 0 && row.previous_reading >= 0) {
calculate_consumption(frm, row);
}
},
item_code: function (frm, cdt, cdn) {
Expand All @@ -92,10 +101,29 @@ frappe.ui.form.on("Meter Reading Item", {
frappe.model.set_value(cdt, cdn, "stock_uom", r.message.stock_uom);
frappe.model.set_value(cdt, cdn, "description", r.message.description);

frm.refresh_field("items");
frappe.call({
method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_previous_invoice_reading",
args: {
item_code: row.item_code,
},
callback: function (r) {
row.previous_reading = r.message[1] || 0;
frm.refresh_field("items");
},
});
}
},
});
}
},
});

function calculate_consumption(frm, row) {
row.consumption = row.current_reading - row.previous_reading;
if (row.consumption < 0) {
frappe.msgprint(__("Current reading cannot be lower than previous reading."));
row.consumption = 0;
row.current_reading = 0;
}
frm.refresh_field("items");
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from frappe.query_builder.functions import Sum
from frappe.utils import nowdate

from ..utility_service_request.utility_service_request import get_item_details
from ...utils.create_meter_reading_rates import create_meter_reading_rates


Expand Down Expand Up @@ -44,21 +45,14 @@ def create_sales_order(meter_reading):
)

for rate in meter_reading.rates:
sales_order.append(
"items",
{
"item_code": rate.item_code,
"qty": round(rate.qty),
"rate": rate.rate,
"amount": rate.amount,
"block": rate.block,
"delivery_date": nowdate(),
},
)
rate_dict = rate.as_dict()
rate_dict["delivery_date"] = nowdate()
sales_order.append("items", rate_dict)


for i in meter_reading.items:
_, prev_reading = get_previous_invoice_reading(i.meter_number)
prev_consumption = get_previous_consumption(i.meter_number)
_, prev_reading = get_previous_invoice_reading(i.item_code)
prev_consumption = get_previous_consumption(i.item_code)
sales_order.append(
"meter_readings",
{
Expand All @@ -78,9 +72,9 @@ def create_sales_order(meter_reading):


@frappe.whitelist()
def get_previous_consumption(meter_number):
def get_previous_consumption(item_code):
"""Fetch the sum of previous readings for the specified meter number sharing the same parent."""
parent, _ = get_previous_invoice_reading(meter_number)
parent, _ = get_previous_invoice_reading(item_code)
meter_reading = DocType("Sales Invoice Meter Reading")
previous_consumption = (
frappe.qb.from_(meter_reading)
Expand All @@ -91,36 +85,21 @@ def get_previous_consumption(meter_number):


@frappe.whitelist()
def get_previous_invoice_reading(meter_number):
def get_previous_invoice_reading(item_code):
"""Fetch the previous reading and its parent for the specified meter number."""
previous_reading = frappe.get_all(
"Sales Invoice Meter Reading",
filters={"meter_number": meter_number},
filters={"item_code": item_code},
fields=["current_reading", "creation", "parent"],
order_by="creation desc",
limit_page_length=1,
)

if previous_reading:
return previous_reading[0].parent, previous_reading[0].current_reading
else:
return "None", 0


@frappe.whitelist()
def get_previous_reading(meter_number):
"""Fetch the previous reading for the specified meter number."""
previous_reading = frappe.get_all(
"Meter Reading Item",
filters={"meter_number": meter_number},
fields=["current_reading", "creation"],
order_by="creation desc",
limit_page_length=1,
)

return previous_reading[0].current_reading if previous_reading else 0


@frappe.whitelist()
def get_customer_details(customer):
"""Fetch all customer details, including the default price list and other fields."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"label": "Description"
},
{
"default": "0",
"fieldname": "current_reading",
"fieldtype": "Float",
"in_filter": 1,
Expand All @@ -82,10 +83,10 @@
"in_preview": 1,
"in_standard_filter": 1,
"label": "Meter Number",
"options": "Serial No",
"reqd": 1
"options": "Serial No"
},
{
"default": "0",
"fieldname": "previous_reading",
"fieldtype": "Float",
"in_filter": 1,
Expand All @@ -97,6 +98,7 @@
"reqd": 1
},
{
"default": "0",
"fieldname": "consumption",
"fieldtype": "Float",
"in_filter": 1,
Expand All @@ -121,7 +123,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-10-24 13:41:29.433226",
"modified": "2024-10-28 07:49:08.291537",
"modified_by": "Administrator",
"module": "Utility Billing",
"name": "Meter Reading Item",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"item_code",
"meter_reading",
"stock_uom",
"stock_qty",
"column_break_fkvl",
"meter_number",
"uom",
"qty",
"column_break_smgb",
"previous_reading",
"previous_consumption",
Expand Down Expand Up @@ -95,12 +97,24 @@
"in_preview": 1,
"in_standard_filter": 1,
"label": "Previous Consumption"
},
{
"default": "1",
"fieldname": "stock_qty",
"fieldtype": "Float",
"label": "Stock Qty"
},
{
"default": "1",
"fieldname": "qty",
"fieldtype": "Float",
"label": "Qty"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-10-22 09:37:40.803921",
"modified": "2024-10-28 07:51:46.444996",
"modified_by": "Administrator",
"module": "Utility Billing",
"name": "Sales Order Meter Reading",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ frappe.ui.form.on("Utility Service Request", {
return {
filters: {
is_sales_item: 1,
is_utility_item: 1,
has_variants: 0,
},
};
Expand Down Expand Up @@ -64,6 +65,28 @@ frappe.ui.form.on("Utility Service Request", {
}
},

tc_name: function (frm) {
if (!frm.doc.tc_name) {
frm.set_value("terms", "");
return;
}
frappe.call({
method: "frappe.client.get_value",
args: {
doctype: "Terms and Conditions",
fieldname: "terms",
filters: {
name: frm.doc.tc_name,
},
},
callback: function (r) {
if (r.message && r.message.terms) {
frm.set_value("terms", r.message.terms);
}
},
});
},

onload: function (frm) {
frm.ignore_doctypes_on_cancel_all = ["BOM"];
},
Expand Down Expand Up @@ -107,6 +130,8 @@ frappe.ui.form.on("Utility Service Request Item", {
bom_no: item.bom_no,
weight_per_unit: item.weight_per_unit,
weight_uom: item.weight_uom,
item_tax_template: item.item_tax_template,
warehouse: item.default_warehouse,
});

let amount = flt(item.rate) * flt(row.qty || 1);
Expand Down Expand Up @@ -176,8 +201,8 @@ function open_bom_creation_modal(frm) {
if (response.message) {
handle_response(response, "BOM", frm);
modal.hide();

frappe.set_route("Form", "BOM", response.message.bom);
const bomUrl = frappe.utils.get_form_link("BOM", response.message.bom);
window.location.href = bomUrl;
}
},
});
Expand Down Expand Up @@ -226,7 +251,7 @@ function addActionButtons(frm) {

if (currentStatus === "") {
frm.add_custom_button(
__("Issue Site Survey"),
__("Site Survey"),
function () {
frappe.call({
method: "utility_billing.utility_billing.doctype.utility_service_request.utility_service_request.create_site_survey",
Expand Down
Loading

0 comments on commit 3330f07

Please sign in to comment.