fix: extract site code from invoice

This commit is contained in:
Rohan Bansal 2025-06-17 12:05:10 +05:30
parent bf0c3130e6
commit 0230da2812
2 changed files with 56 additions and 15 deletions

View file

@ -1,4 +1,5 @@
import json
import re
import frappe
from erpnext.accounts.doctype.account.account import update_account_number
@ -253,12 +254,13 @@ def import_invoices():
if frappe.db.exists("Purchase Invoice", {"name": invoice.get("name")}):
continue
site_code = get_site_code(invoice)
invoice_doc = frappe.new_doc("Purchase Invoice")
invoice_doc.update(
{
"supplier": invoice.get("supplier"),
"bill_no": invoice.get("supplier_invoice_number"),
"site_code": invoice.get("site_code"),
"site_code": site_code or "",
"set_posting_time": True,
"bill_date": getdate(invoice.get("invoice_date")),
"posting_date": getdate(invoice.get("invoice_date")),
@ -292,16 +294,35 @@ def import_invoices():
continue
if invoice.get("street"):
address = create_address(
{
**invoice,
"title": invoice.get("supplier"),
"ref_doctype": "Supplier",
"ref_name": invoice.get("supplier"),
}
)
invoice_doc.supplier_address = address.name
invoice_doc.save()
if site_code:
if frappe.db.exists("Site Code", site_code):
site_code_doc = frappe.get_doc("Site Code", site_code)
else:
site_code_doc = frappe.new_doc("Site Code")
site_code_doc.site_code = site_code
site_code_doc.save(ignore_permissions=True)
address = create_address(
{
**invoice,
"title": site_code,
"ref_doctype": "Site Code",
"ref_name": site_code_doc.name,
}
)
invoice_doc.dispatch_address = address.name
invoice_doc.save()
else:
address = create_address(
{
**invoice,
"title": invoice.get("supplier"),
"ref_doctype": "Supplier",
"ref_name": invoice.get("supplier"),
}
)
invoice_doc.supplier_address = address.name
invoice_doc.save()
try:
invoice_doc.submit()
@ -338,3 +359,16 @@ def import_invoices():
payment_doc.submit()
except Exception as e:
print(f"Error importing payment for invoice {invoice_doc.bill_no}: {e}")
def get_site_code(invoice):
if invoice.get("site_code"):
return invoice["site_code"].upper()
# try to find it in the notes
site_pattern = re.compile(r"\bsite\s+([A-Z0-9]+)", re.IGNORECASE)
notes = invoice.get("notes", "")
if notes:
matches = site_pattern.findall(notes)
if len(matches) > 0:
return ", ".join(matches).upper()

View file

@ -1,10 +1,10 @@
{
"actions": [],
"allow_rename": 1,
"autoname": "field:site_code",
"creation": "2025-06-17 01:51:35.709515",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": ["site_code", "column_break_pwyn", "address_html"],
"field_order": ["site_code", "column_break_pwyn", "addresses_section", "address_html"],
"fields": [
{
"fieldname": "site_code",
@ -29,14 +29,20 @@
"fieldtype": "HTML",
"label": "Address HTML",
"read_only": 1
},
{
"fieldname": "addresses_section",
"fieldtype": "Section Break",
"label": "Addresses"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2025-06-17 02:11:43.902766",
"modified": "2025-06-17 02:29:32.191624",
"modified_by": "Administrator",
"module": "Suppliers Portal",
"name": "Site Code",
"naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{
@ -54,5 +60,6 @@
],
"sort_field": "creation",
"sort_order": "DESC",
"states": []
"states": [],
"title_field": "site_code"
}