diff --git a/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.json b/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.json index 889a12b..d2bab50 100644 --- a/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.json +++ b/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.json @@ -21,6 +21,7 @@ "amended_from", "agreement_tab", "seller_section", + "seller_id", "seller_name", "seller_tax_id", "seller_address_line_1", @@ -35,6 +36,7 @@ "create_supplier_address", "buyer_section", "column_break_reul", + "buyer_id", "buyer_name", "buyer_address_line_1", "buyer_address_line_2", @@ -467,6 +469,18 @@ "fieldtype": "Text", "label": "Validation Warnings", "read_only": 1 + }, + { + "fieldname": "buyer_id", + "fieldtype": "Data", + "label": "Buyer ID", + "read_only": 1 + }, + { + "fieldname": "seller_id", + "fieldtype": "Data", + "label": "Seller ID", + "read_only": 1 } ], "index_web_pages_for_search": 1, @@ -477,7 +491,7 @@ "link_fieldname": "e_invoice_import" } ], - "modified": "2025-05-07 00:53:28.840615", + "modified": "2025-10-01 02:52:45.579946", "modified_by": "Administrator", "module": "European e-Invoice", "name": "E Invoice Import", diff --git a/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.py b/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.py index f21283e..57ddcff 100644 --- a/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.py +++ b/eu_einvoice/european_e_invoice/doctype/e_invoice_import/e_invoice_import.py @@ -52,6 +52,7 @@ class EInvoiceImport(Document): buyer_address_line_2: DF.Data | None buyer_city: DF.Data | None buyer_country: DF.Link | None + buyer_id: DF.Data | None buyer_name: DF.Data | None buyer_postcode: DF.Data | None charge_total: DF.Currency @@ -76,6 +77,7 @@ class EInvoiceImport(Document): seller_address_line_2: DF.Data | None seller_city: DF.Data | None seller_country: DF.Link | None + seller_id: DF.Data | None seller_name: DF.Data | None seller_postcode: DF.Data | None seller_tax_id: DF.Data | None @@ -105,6 +107,7 @@ def before_save(self): self.read_values_from_einvoice() self.guess_supplier() self.guess_company() + self.guess_company_and_supplier() self.guess_uom() self.guess_item_code() @@ -221,6 +224,7 @@ def _validate_schematron(self, xml_bytes): def parse_seller(self, seller: "TradeParty"): self.seller_name = str(seller.name) + self.seller_id = str(seller.id) self.seller_tax_id = ( seller.tax_registrations.children[0].id._text if seller.tax_registrations.children else None ) @@ -228,6 +232,7 @@ def parse_seller(self, seller: "TradeParty"): def parse_buyer(self, buyer: "TradeParty"): self.buyer_name = str(buyer.name) + self.buyer_id = str(buyer.id) self.parse_address(buyer.address, "buyer") def parse_address(self, address: "PostalTradeAddress", prefix: str) -> _dict: @@ -329,11 +334,19 @@ def guess_supplier(self): if self.supplier: return - if frappe.db.exists("Supplier", self.seller_name): + if self.seller_id and frappe.db.exists("Supplier", self.seller_id): + self.supplier = self.seller_id + return + + if self.seller_name and frappe.db.exists("Supplier", self.seller_name): self.supplier = self.seller_name + return - if self.seller_tax_id: - self.supplier = frappe.db.get_value("Supplier", {"tax_id": self.seller_tax_id}, "name") + if self.seller_tax_id and ( + supplier := frappe.db.get_value("Supplier", {"tax_id": self.seller_tax_id}, "name") + ): + self.supplier = supplier + return def guess_company(self): if self.company: @@ -341,7 +354,37 @@ def guess_company(self): if frappe.db.exists("Company", self.buyer_name): self.company = self.buyer_name - else: + + def guess_company_and_supplier(self): + """Guess company and supplier based on buyer ID. + + If the buyer ID is provided and we have already found either Company or + Supplier, we can find the other one. + """ + if not self.buyer_id: + return + + if self.company and not self.supplier: + suppliers = frappe.get_all( + "Customer Number At Supplier", + filters={"customer_number": self.buyer_id, "company": self.company, "parenttype": "Supplier"}, + pluck="parent", + limit=2, + ) + if len(suppliers) == 1: + self.supplier = suppliers[0] + + if self.supplier and not self.company: + companies = frappe.get_all( + "Customer Number At Supplier", + filters={"customer_number": self.buyer_id, "parent": self.supplier, "parenttype": "Supplier"}, + pluck="company", + limit=2, + ) + if len(companies) == 1: + self.company = companies[0] + + if not self.company: self.company = get_default_company() def guess_uom(self):