Skip to content

Commit

Permalink
fix: make e-invoice mapping separate table
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanket322 committed Oct 15, 2024
1 parent bce0eb2 commit 58e439e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
"cancelled_on",
"column_break_3",
"cancel_reason_code",
"cancel_remark",
"section_break_clwh",
"item_mapping"
"cancel_remark"
],
"fields": [
{
Expand Down Expand Up @@ -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,
Expand All @@ -152,7 +140,7 @@
"link_fieldname": "irn"
}
],
"modified": "2024-09-24 17:51:10.730928",
"modified": "2024-09-19 18:59:01.195753",
"modified_by": "Administrator",
"module": "GST India",
"name": "e-Invoice Log",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
69 changes: 44 additions & 25 deletions india_compliance/gst_india/overrides/purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -210,7 +222,6 @@ def get_dashboard_data(data):
"e-Waybill Log",
"Integration Request",
"GST Inward Supply",
"e-Invoice Log",
)

return data
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion india_compliance/gst_india/overrides/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
)

Expand Down

0 comments on commit 58e439e

Please sign in to comment.