diff --git a/india_compliance/gst_india/doctype/e_invoice_log/e_invoice_log.json b/india_compliance/gst_india/doctype/e_invoice_log/e_invoice_log.json index 816dd88ef..1e34877f0 100644 --- a/india_compliance/gst_india/doctype/e_invoice_log/e_invoice_log.json +++ b/india_compliance/gst_india/doctype/e_invoice_log/e_invoice_log.json @@ -22,9 +22,7 @@ "cancelled_on", "column_break_3", "cancel_reason_code", - "cancel_remark", - "section_break_clwh", - "item_mapping" + "cancel_remark" ], "fields": [ { @@ -133,16 +131,6 @@ "label": "Reference Document Name", "options": "reference_doctype", "read_only": 1 - }, - { - "fieldname": "section_break_clwh", - "fieldtype": "Section Break" - }, - { - "fieldname": "item_mapping", - "fieldtype": "Table", - "label": "Item Mapping", - "options": "e-Invoice Mapping" } ], "in_create": 1, @@ -152,7 +140,7 @@ "link_fieldname": "irn" } ], - "modified": "2024-09-24 17:51:10.730928", + "modified": "2024-10-14 23:09:39.350076", "modified_by": "Administrator", "module": "GST India", "name": "e-Invoice Log", diff --git a/india_compliance/gst_india/doctype/e_invoice_mapping/e_invoice_mapping.json b/india_compliance/gst_india/doctype/e_invoice_mapping/e_invoice_mapping.json index 41e4ce036..0881e0a13 100644 --- a/india_compliance/gst_india/doctype/e_invoice_mapping/e_invoice_mapping.json +++ b/india_compliance/gst_india/doctype/e_invoice_mapping/e_invoice_mapping.json @@ -66,14 +66,26 @@ } ], "index_web_pages_for_search": 1, - "istable": 1, "links": [], - "modified": "2024-09-26 00:12:00.088452", + "modified": "2024-10-14 23:13:02.604854", "modified_by": "Administrator", "module": "GST India", "name": "e-Invoice Mapping", "owner": "Administrator", - "permissions": [], + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], "sort_field": "creation", "sort_order": "DESC", "states": [] diff --git a/india_compliance/gst_india/overrides/purchase_invoice.py b/india_compliance/gst_india/overrides/purchase_invoice.py index 725e238a4..838e17bff 100644 --- a/india_compliance/gst_india/overrides/purchase_invoice.py +++ b/india_compliance/gst_india/overrides/purchase_invoice.py @@ -168,28 +168,40 @@ def update_item_mapping(doc): ): return - log = frappe.get_doc( - "e-Invoice Log", - {"reference_name": doc.name, "reference_doctype": "Purchase Invoice"}, + item_mapping = frappe.get_all( + "e-Invoice Mapping", + filters={ + "erpnext_value": ["=", ""], + "item_row_name": ["in", [item.name for item in doc.items]], + }, + fields=["item_row_name", "rate", "erpnext_fieldname"], ) - item_mapping = {} - for item in doc.items: - key = (item.name, flt(item.rate, precision=2)) - item_mapping[key] = {"item_code": item.item_code, "uom": item.uom} + mapped_items = { + (item.item_row_name, flt(item.rate, precision=2), item.erpnext_fieldname) + for item in item_mapping + } - for item in log.item_mapping: - key = (item.item_row_name, flt(item.rate, precision=2)) + def update_mapping(item, fieldname): + frappe.db.set_value( + "e-Invoice Mapping", + { + "item_row_name": item.name, + "rate": item.rate, + "erpnext_fieldname": fieldname, + }, + "erpnext_value", + item.get("item_code" if fieldname == "item_name" else fieldname), + ) - if key in item_mapping: - mapped_item = item_mapping[key] + for item in doc.items: + rate = flt(item.rate, precision=2) - if item.erpnext_fieldname == "item_name": - item.erpnext_value = mapped_item.get("item_code") - else: - item.erpnext_value = mapped_item.get("uom") + for fieldname in ["item_name", "uom"]: + key = (item.name, rate, fieldname) - log.save(ignore_permissions=True) + if key in mapped_items: + update_mapping(item, fieldname) def get_dashboard_data(data): @@ -210,7 +222,6 @@ def get_dashboard_data(data): "e-Waybill Log", "Integration Request", "GST Inward Supply", - "e-Invoice Log", ) return data @@ -420,15 +431,15 @@ def map_keys(source, target, section): return data -def create_purchase_invoice(supplier, company, invoice): +def create_purchase_invoice(supplier, company, invoice_info): invoice_data = { "doctype": "Purchase Invoice", "supplier": supplier, "company": company, "due_date": frappe.utils.nowdate(), } - invoice["bill_date"] = getdate(invoice["bill_date"]) - invoice_data.update(invoice) + invoice_info["bill_date"] = getdate(invoice_info["bill_date"]) + invoice_data.update(invoice_info) doc = frappe.get_doc(invoice_data) doc.update( @@ -486,30 +497,38 @@ def get_mapped_data(mappings, fieldname): } def log_unmapped_item(fieldname, item): - e_invoice_log.append( - "item_mapping", + frappe.get_doc( { + "doctype": "e-Invoice Mapping", "party": supplier, "party_type": "Supplier", "erpnext_fieldname": fieldname, "e_invoice_value": item.get(fieldname), "rate": item.rate, "item_row_name": item.name, - }, - ) + } + ).save(ignore_permissions=True) mappings = frappe.get_all( "e-Invoice Mapping", - filters={"party": supplier}, + filters={"party": supplier, "erpnext_value": ["!=", ""]}, fields=["e_invoice_value", "erpnext_value", "erpnext_fieldname"], ) mapped_items = get_mapped_data(mappings, "item_name") mapped_uoms = get_mapped_data(mappings, "uom") + items = frappe.get_all( + "Item", + filters={"item_code": ["in", list(mapped_items.values())]}, + fields=["item_code", "item_name"], + ) + item_names = {item.item_code: item.item_name for item in items} + for item in doc.items: if item_code := mapped_items.get(item.item_name): item.item_code = item_code + item.item_name = item_names.get(item_code) else: log_unmapped_item("item_name", item) diff --git a/india_compliance/gst_india/overrides/sales_invoice.py b/india_compliance/gst_india/overrides/sales_invoice.py index 1c02e333e..d41498245 100644 --- a/india_compliance/gst_india/overrides/sales_invoice.py +++ b/india_compliance/gst_india/overrides/sales_invoice.py @@ -243,7 +243,6 @@ def update_dashboard_with_gst_logs(doctype, data, *log_doctypes): "e-Waybill Log": "reference_name", "Integration Request": "reference_docname", "GST Inward Supply": "link_name", - "e-Invoice Log": "reference_name", } )