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

fix: meter number filter on prev reading #29

Merged
merged 1 commit into from
Nov 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
},
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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",
{
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
Loading