Skip to content

Commit c7f5c99

Browse files
committed
fix: make e-invoice mapping separate table
1 parent bce0eb2 commit c7f5c99

File tree

4 files changed

+61
-43
lines changed

4 files changed

+61
-43
lines changed

india_compliance/gst_india/doctype/e_invoice_log/e_invoice_log.json

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
"cancelled_on",
2323
"column_break_3",
2424
"cancel_reason_code",
25-
"cancel_remark",
26-
"section_break_clwh",
27-
"item_mapping"
25+
"cancel_remark"
2826
],
2927
"fields": [
3028
{
@@ -133,16 +131,6 @@
133131
"label": "Reference Document Name",
134132
"options": "reference_doctype",
135133
"read_only": 1
136-
},
137-
{
138-
"fieldname": "section_break_clwh",
139-
"fieldtype": "Section Break"
140-
},
141-
{
142-
"fieldname": "item_mapping",
143-
"fieldtype": "Table",
144-
"label": "Item Mapping",
145-
"options": "e-Invoice Mapping"
146134
}
147135
],
148136
"in_create": 1,
@@ -152,7 +140,7 @@
152140
"link_fieldname": "irn"
153141
}
154142
],
155-
"modified": "2024-09-24 17:51:10.730928",
143+
"modified": "2024-10-14 23:09:39.350076",
156144
"modified_by": "Administrator",
157145
"module": "GST India",
158146
"name": "e-Invoice Log",

india_compliance/gst_india/doctype/e_invoice_mapping/e_invoice_mapping.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,26 @@
6666
}
6767
],
6868
"index_web_pages_for_search": 1,
69-
"istable": 1,
7069
"links": [],
71-
"modified": "2024-09-26 00:12:00.088452",
70+
"modified": "2024-10-14 23:13:02.604854",
7271
"modified_by": "Administrator",
7372
"module": "GST India",
7473
"name": "e-Invoice Mapping",
7574
"owner": "Administrator",
76-
"permissions": [],
75+
"permissions": [
76+
{
77+
"create": 1,
78+
"delete": 1,
79+
"email": 1,
80+
"export": 1,
81+
"print": 1,
82+
"read": 1,
83+
"report": 1,
84+
"role": "System Manager",
85+
"share": 1,
86+
"write": 1
87+
}
88+
],
7789
"sort_field": "creation",
7890
"sort_order": "DESC",
7991
"states": []

india_compliance/gst_india/overrides/purchase_invoice.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,40 @@ def update_item_mapping(doc):
168168
):
169169
return
170170

171-
log = frappe.get_doc(
172-
"e-Invoice Log",
173-
{"reference_name": doc.name, "reference_doctype": "Purchase Invoice"},
171+
item_mapping = frappe.get_all(
172+
"e-Invoice Mapping",
173+
filters={
174+
"erpnext_value": ["=", ""],
175+
"item_row_name": ["in", [item.name for item in doc.items]],
176+
},
177+
fields=["item_row_name", "rate", "erpnext_fieldname"],
174178
)
175179

176-
item_mapping = {}
177-
for item in doc.items:
178-
key = (item.name, flt(item.rate, precision=2))
179-
item_mapping[key] = {"item_code": item.item_code, "uom": item.uom}
180+
mapped_items = {
181+
(item.item_row_name, flt(item.rate, precision=2), item.erpnext_fieldname)
182+
for item in item_mapping
183+
}
180184

181-
for item in log.item_mapping:
182-
key = (item.item_row_name, flt(item.rate, precision=2))
185+
def update_mapping(item, fieldname):
186+
frappe.db.set_value(
187+
"e-Invoice Mapping",
188+
{
189+
"item_row_name": item.name,
190+
"rate": item.rate,
191+
"erpnext_fieldname": fieldname,
192+
},
193+
"erpnext_value",
194+
item.get("item_code" if fieldname == "item_name" else fieldname),
195+
)
183196

184-
if key in item_mapping:
185-
mapped_item = item_mapping[key]
197+
for item in doc.items:
198+
rate = flt(item.rate, precision=2)
186199

187-
if item.erpnext_fieldname == "item_name":
188-
item.erpnext_value = mapped_item.get("item_code")
189-
else:
190-
item.erpnext_value = mapped_item.get("uom")
200+
for fieldname in ["item_name", "uom"]:
201+
key = (item.name, rate, fieldname)
191202

192-
log.save(ignore_permissions=True)
203+
if key in mapped_items:
204+
update_mapping(item, fieldname)
193205

194206

195207
def get_dashboard_data(data):
@@ -210,7 +222,6 @@ def get_dashboard_data(data):
210222
"e-Waybill Log",
211223
"Integration Request",
212224
"GST Inward Supply",
213-
"e-Invoice Log",
214225
)
215226

216227
return data
@@ -420,15 +431,15 @@ def map_keys(source, target, section):
420431
return data
421432

422433

423-
def create_purchase_invoice(supplier, company, invoice):
434+
def create_purchase_invoice(supplier, company, invoice_info):
424435
invoice_data = {
425436
"doctype": "Purchase Invoice",
426437
"supplier": supplier,
427438
"company": company,
428439
"due_date": frappe.utils.nowdate(),
429440
}
430-
invoice["bill_date"] = getdate(invoice["bill_date"])
431-
invoice_data.update(invoice)
441+
invoice_info["bill_date"] = getdate(invoice_info["bill_date"])
442+
invoice_data.update(invoice_info)
432443

433444
doc = frappe.get_doc(invoice_data)
434445
doc.update(
@@ -486,30 +497,38 @@ def get_mapped_data(mappings, fieldname):
486497
}
487498

488499
def log_unmapped_item(fieldname, item):
489-
e_invoice_log.append(
490-
"item_mapping",
500+
frappe.get_doc(
491501
{
502+
"doctype": "e-Invoice Mapping",
492503
"party": supplier,
493504
"party_type": "Supplier",
494505
"erpnext_fieldname": fieldname,
495506
"e_invoice_value": item.get(fieldname),
496507
"rate": item.rate,
497508
"item_row_name": item.name,
498-
},
499-
)
509+
}
510+
).save(ignore_permissions=True)
500511

501512
mappings = frappe.get_all(
502513
"e-Invoice Mapping",
503-
filters={"party": supplier},
514+
filters={"party": supplier, "erpnext_value": ["!=", ""]},
504515
fields=["e_invoice_value", "erpnext_value", "erpnext_fieldname"],
505516
)
506517

507518
mapped_items = get_mapped_data(mappings, "item_name")
508519
mapped_uoms = get_mapped_data(mappings, "uom")
509520

521+
items = frappe.get_all(
522+
"Item",
523+
filters={"item_code": ["in", list(mapped_items.values())]},
524+
fields=["item_code", "item_name"],
525+
)
526+
item_names = {item.item_code: item.item_name for item in items}
527+
510528
for item in doc.items:
511529
if item_code := mapped_items.get(item.item_name):
512530
item.item_code = item_code
531+
item.item_name = item_names.get(item_code)
513532
else:
514533
log_unmapped_item("item_name", item)
515534

india_compliance/gst_india/overrides/sales_invoice.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ def update_dashboard_with_gst_logs(doctype, data, *log_doctypes):
243243
"e-Waybill Log": "reference_name",
244244
"Integration Request": "reference_docname",
245245
"GST Inward Supply": "link_name",
246-
"e-Invoice Log": "reference_name",
247246
}
248247
)
249248

0 commit comments

Comments
 (0)