From 0991dd408fb884c48eb7398c65b9e0eb2992c592 Mon Sep 17 00:00:00 2001 From: muruthigitau Date: Fri, 8 Nov 2024 08:41:02 +0300 Subject: [PATCH] fix: meter number filter on prev reading --- .../doctype/meter_reading/meter_reading.js | 49 ++++++++++--------- .../doctype/meter_reading/meter_reading.py | 36 +++++++++----- .../sales_invoice_meter_reading.json | 6 ++- .../sales_order_meter_reading.json | 5 +- 4 files changed, 58 insertions(+), 38 deletions(-) diff --git a/utility_billing/utility_billing/doctype/meter_reading/meter_reading.js b/utility_billing/utility_billing/doctype/meter_reading/meter_reading.js index d1b131e..794b4e5 100644 --- a/utility_billing/utility_billing/doctype/meter_reading/meter_reading.js +++ b/utility_billing/utility_billing/doctype/meter_reading/meter_reading.js @@ -31,19 +31,7 @@ frappe.ui.form.on("Meter Reading", { frm.set_value("territory", r.message.territory); frm.set_value("price_list", r.message.default_price_list); frm.doc.items.forEach((item) => { - if (item.item_code) { - frappe.call({ - method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_previous_invoice_reading", - args: { - item_code: item.item_code, - customer: frm.doc.customer, - }, - callback: function (r) { - item.previous_reading = r.message || 0; - frm.refresh_field("items"); - }, - }); - } + fetch_previous_reading(frm, item); }); } }, @@ -116,24 +104,37 @@ 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); - frappe.call({ - method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_previous_invoice_reading", - args: { - item_code: row.item_code, - customer: frm.doc.customer, - }, - callback: function (r) { - row.previous_reading = r.message || 0; - frm.refresh_field("items"); - }, - }); + fetch_previous_reading(frm, row); } }, }); } }, + meter_number: function (frm, cdt, cdn) { + const row = locals[cdt][cdn]; + if (row.item_code && frm.doc.customer) { + fetch_previous_reading(frm, row); + } + }, }); +function fetch_previous_reading(frm, row) { + if (row.item_code && frm.doc.customer) { + frappe.call({ + method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_previous_invoice_reading", + args: { + item_code: row.item_code, + customer: frm.doc.customer, + meter_number: row.meter_number, + }, + callback: function (r) { + row.previous_reading = r.message || 0; + frm.refresh_field("items"); + }, + }); + } +} + function calculate_consumption(frm, row) { row.consumption = row.current_reading - row.previous_reading; if (row.consumption < 0) { 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 93e90e0..f4c083e 100644 --- a/utility_billing/utility_billing/doctype/meter_reading/meter_reading.py +++ b/utility_billing/utility_billing/doctype/meter_reading/meter_reading.py @@ -3,11 +3,11 @@ import frappe from frappe.model.document import Document from frappe.query_builder import DocType -from frappe.query_builder.functions import Sum from frappe.utils import nowdate from erpnext.controllers.accounts_controller import AccountsController +from frappe.query_builder import DocType +from pypika import Order -from ..utility_service_request.utility_service_request import get_item_details from ...utils.create_meter_reading_rates import create_meter_reading_rates @@ -55,7 +55,7 @@ def create_sales_order(meter_reading): for i in meter_reading.items: - prev_reading = get_previous_invoice_reading(i.item_code, meter_reading.customer) + prev_reading = get_previous_invoice_reading(i.item_code, meter_reading.customer, i.meter_number) sales_order.append( "meter_readings", { @@ -76,20 +76,32 @@ def create_sales_order(meter_reading): return sales_order + @frappe.whitelist() -def get_previous_invoice_reading(item_code, customer): - """Fetch the last submitted invoice's current reading for the specified item and customer.""" +def get_previous_invoice_reading(item_code, customer, meter_number=None): + """Fetch the latest reading for the specified customer, item, and optional meter number.""" - latest_invoice = frappe.get_value("Sales Invoice", filters={"customer": customer, "docstatus": 1}, - fieldname="name", order_by="creation desc") + SalesInvoiceMeterReading = DocType("Sales Invoice Meter Reading") + SalesInvoice = DocType("Sales Invoice") - if not latest_invoice: - return 0 + query = ( + frappe.qb.from_(SalesInvoiceMeterReading) + .join(SalesInvoice).on(SalesInvoice.name == SalesInvoiceMeterReading.parent) + .select(SalesInvoiceMeterReading.current_reading) + .where(SalesInvoice.customer == customer) + .where(SalesInvoiceMeterReading.item_code == item_code) + .where(SalesInvoice.docstatus == 1) + ) - previous_reading = frappe.get_value("Sales Invoice Meter Reading", filters={"parent": latest_invoice, "item_code": item_code}, - fieldname="current_reading", order_by="creation desc") + if meter_number: + query = query.where(SalesInvoiceMeterReading.meter_number == meter_number) + else: + query = query.where(SalesInvoiceMeterReading.meter_number.isnull()) - return previous_reading or 0 + query = query.orderby(SalesInvoiceMeterReading.creation, order=Order.desc) + result = query.limit(1).run() + + return result[0][0] if result else 0 @frappe.whitelist() diff --git a/utility_billing/utility_billing/doctype/sales_invoice_meter_reading/sales_invoice_meter_reading.json b/utility_billing/utility_billing/doctype/sales_invoice_meter_reading/sales_invoice_meter_reading.json index 04df749..3e99068 100644 --- a/utility_billing/utility_billing/doctype/sales_invoice_meter_reading/sales_invoice_meter_reading.json +++ b/utility_billing/utility_billing/doctype/sales_invoice_meter_reading/sales_invoice_meter_reading.json @@ -38,6 +38,10 @@ { "fieldname": "meter_number", "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "in_preview": 1, + "in_standard_filter": 1, "label": "Meter Number", "options": "Serial No" }, @@ -106,7 +110,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2024-11-07 07:25:13.812814", + "modified": "2024-11-08 07:35:30.185347", "modified_by": "Administrator", "module": "Utility Billing", "name": "Sales Invoice Meter Reading", diff --git a/utility_billing/utility_billing/doctype/sales_order_meter_reading/sales_order_meter_reading.json b/utility_billing/utility_billing/doctype/sales_order_meter_reading/sales_order_meter_reading.json index 90b1a84..88a6aa6 100644 --- a/utility_billing/utility_billing/doctype/sales_order_meter_reading/sales_order_meter_reading.json +++ b/utility_billing/utility_billing/doctype/sales_order_meter_reading/sales_order_meter_reading.json @@ -38,7 +38,10 @@ { "fieldname": "meter_number", "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, "in_preview": 1, + "in_standard_filter": 1, "label": "Meter Number", "options": "Serial No" }, @@ -111,7 +114,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2024-11-07 00:03:24.415579", + "modified": "2024-11-08 07:35:51.102934", "modified_by": "Administrator", "module": "Utility Billing", "name": "Sales Order Meter Reading",