diff --git a/utility_billing/utility_billing/apis/delete_doc.py b/utility_billing/utility_billing/apis/delete_doc.py new file mode 100644 index 0000000..c401f84 --- /dev/null +++ b/utility_billing/utility_billing/apis/delete_doc.py @@ -0,0 +1,27 @@ +import frappe + +@frappe.whitelist(allow_guest=True) +def drop_meter_reading(): + try: + meter_readings = frappe.get_all("Meter Reading", pluck="name") + for reading in meter_readings: + frappe.delete_doc("Meter Reading", reading) + + meter_reading_items = frappe.get_all("Meter Reading Item", pluck="name") + for item in meter_reading_items: + frappe.delete_doc("Meter Reading Item", item) + + meter_reading_rates = frappe.get_all("Meter Reading Rate", pluck="name") + for rate in meter_reading_rates: + frappe.delete_doc("Meter Reading Rate", rate) + + frappe.db.commit() + + return { + "status": "success", + "message": "All entries in Meter Reading, Meter Reading Item, and Meter Reading Rate have been successfully deleted." + } + + except Exception as e: + frappe.log_error(f"An error occurred while deleting records: {str(e)}", "Drop Reading Error") + return {"status": "error", "message": str(e)} diff --git a/utility_billing/utility_billing/doctype/meter_reading_rate/meter_reading_rate.json b/utility_billing/utility_billing/doctype/meter_reading_rate/meter_reading_rate.json index 4071e82..8d871f2 100644 --- a/utility_billing/utility_billing/doctype/meter_reading_rate/meter_reading_rate.json +++ b/utility_billing/utility_billing/doctype/meter_reading_rate/meter_reading_rate.json @@ -84,26 +84,14 @@ } ], "index_web_pages_for_search": 1, + "istable": 1, "links": [], - "modified": "2024-10-17 07:30:33.471183", + "modified": "2024-10-17 08:27:35.854008", "modified_by": "Administrator", "module": "Utility Billing", "name": "Meter Reading Rate", "owner": "Administrator", - "permissions": [ - { - "create": 1, - "delete": 1, - "email": 1, - "export": 1, - "print": 1, - "read": 1, - "report": 1, - "role": "System Manager", - "share": 1, - "write": 1 - } - ], + "permissions": [], "sort_field": "creation", "sort_order": "DESC", "states": [] diff --git a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js index 1bfdddb..da22529 100644 --- a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js +++ b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.js @@ -85,58 +85,50 @@ frappe.ui.form.on("Utility Service Request Item", { let row = locals[cdt][cdn]; if (row.item_code) { frappe.call({ - method: "frappe.client.get", - args: { doctype: "Item", name: row.item_code }, + method: "utility_billing.utility_billing.doctype.utility_service_request.utility_service_request.get_item_details", + args: { + item_code: row.item_code, + price_list: frm.doc.price_list, + }, callback: function (r) { if (r.message) { let item = r.message; frappe.model.set_value(cdt, cdn, { item_name: item.item_name, - uom: item.stock_uom, - rate: item.standard_rate, - warehouse: item.default_warehouse, + uom: item.uom, + rate: item.rate, + warehouse: item.warehouse, description: item.description, qty: 1, - conversion_factor: (item.uoms[0] || {}).conversion_factor || 1, - brand: item.brand || null, + conversion_factor: item.conversion_factor, + brand: item.brand, item_group: item.item_group, stock_uom: item.stock_uom, - bom_no: item.default_bom, + bom_no: item.bom_no, weight_per_unit: item.weight_per_unit, - weight_uom: item.weight_uom || null, + weight_uom: item.weight_uom, }); - // Fetch price list rate if available - if (frm.doc.price_list) { - frappe.call({ - method: "frappe.client.get_list", - args: { - doctype: "Item Price", - filters: { - price_list: frm.doc.price_list, - item_code: row.item_code, - }, - fields: ["price_list_rate"], - }, - callback: function (res) { - let rate = - res.message[0]?.price_list_rate || item.standard_rate; - let amount = rate * row.qty; - frappe.model.set_value(cdt, cdn, { - rate: rate, - price_list_rate: rate, - amount: amount, - base_price_list_rate: rate, - }); - }, - }); - } + let amount = flt(item.rate) * flt(row.qty || 1); + frappe.model.set_value(cdt, cdn, { + rate: item.rate, + amount: amount, + base_price_list_rate: item.rate, + }); } }, }); } }, + rate: function (frm, cdt, cdn) { + calculate_amount(frm, cdt, cdn); + }, + + qty: function (frm, cdt, cdn) { + calculate_amount(frm, cdt, cdn); + }, + delivery_date: function (frm) { if (!frm.doc.delivery_date) { erpnext.utils.copy_value_in_all_rows(frm.doc, null, null, "items", "delivery_date"); @@ -144,6 +136,12 @@ frappe.ui.form.on("Utility Service Request Item", { }, }); +function calculate_amount(frm, cdt, cdn) { + let row = locals[cdt][cdn]; + let amount = flt(row.rate) * flt(row.qty); + frappe.model.set_value(cdt, cdn, "amount", amount); +} + function fetch_customer_details(frm) { frappe.call({ method: "utility_billing.utility_billing.doctype.meter_reading.meter_reading.get_customer_details", diff --git a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py index e99e1cb..9a9c0b0 100644 --- a/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py +++ b/utility_billing/utility_billing/doctype/utility_service_request/utility_service_request.py @@ -6,7 +6,7 @@ import frappe from frappe.contacts.address_and_contact import load_address_and_contact from frappe.model.document import Document - +from frappe import _ class UtilityServiceRequest(Document): def onload(self): @@ -147,3 +147,40 @@ def check_request_status(request_name): status = "" return status + + +@frappe.whitelist() +def get_item_details(item_code, price_list=None): + item = frappe.get_doc("Item", item_code) + + if not item: + frappe.throw(_("Item not found")) + + default_warehouse = getattr(item, 'default_warehouse', None) + + item_details = { + "item_name": item.item_name, + "uom": item.stock_uom, + "rate": item.standard_rate, + "warehouse": default_warehouse, + "description": item.description, + "qty": 1, + "conversion_factor": (item.uoms[0] or {}).get('conversion_factor', 1) if item.uoms else 1, + "brand": item.brand, + "item_group": item.item_group, + "stock_uom": item.stock_uom, + "bom_no": item.default_bom, + "weight_per_unit": item.weight_per_unit, + "weight_uom": item.weight_uom + } + + if price_list: + item_price = frappe.db.get_value( + "Item Price", + filters={"price_list": price_list, "item_code": item_code}, + fieldname=["price_list_rate"] + ) + if item_price: + item_details["rate"] = item_price + + return item_details