From edca7ac412dfbf6e8a1926b6cdfb6a76f9d9500e Mon Sep 17 00:00:00 2001 From: fkantelberg Date: Mon, 14 Oct 2024 14:53:28 +0200 Subject: [PATCH] [IMP] datev_export_dtvf_nitrokey: Try to get the best references for the financial advisor --- .../models/datev_export_dtvf.py | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/datev_export_dtvf_nitrokey/models/datev_export_dtvf.py b/datev_export_dtvf_nitrokey/models/datev_export_dtvf.py index c6cb484..5979df6 100644 --- a/datev_export_dtvf_nitrokey/models/datev_export_dtvf.py +++ b/datev_export_dtvf_nitrokey/models/datev_export_dtvf.py @@ -6,16 +6,47 @@ class DatevExportDtvfExport(models.Model): _inherit = "datev_export_dtvf.export" + def _get_matching_references(self, move, ref1, ref2): + # Incoming invoices will use the vendor reference + if move.move_type.startswith("in_"): + return move.ref, ref1 + + # Outgoing invoices will use the sale.order reference + if move.move_type == "out_invoice" and move.invoice_origin: + return move.invoice_origin, ref1 + + # Outgoing refunds will use the sale.order reference from the reversed invoice + if move.move_type == "out_refund" and move.reversed_entry_id.invoice_origin: + return move.reversed_entry_id.invoice_origin, ref1 + + # All non-entry will use the default + if move.move_type != "entry": + return ref1, ref2 + + # Try to find the reconciled invoices + matching = move.mapped("line_ids.full_reconcile_id.reconciled_line_ids") + if not matching: + return ref1, ref2 + + # First invoices if possible + domain = [("move_type", "=like", "%_invoice")] + invs = matching.filtered_domain(domain) + if len(invs) == 1: + return self._get_matching_references(invs, ref1, ref2) + + # Fall back to refunds if possible + domain = [("move_type", "=like", "%_refund")] + invs = matching.filtered_domain(domain) + if len(invs) == 1: + return self._get_matching_references( + invs.reversed_entry_id or invs, ref1, ref2 + ) + + # Fall back to the default + return ref1, ref2 + def _get_data_transaction(self, move): - """Try to export whatever looks like an SO number as "Belegfeld 1" """ for data in super()._get_data_transaction(move): - for line in move.line_ids: - for field_name in ("ref", "name", "move_name"): - for token in (line[field_name] or "").split(): - if ( - token.startswith("SO") - or token.startswith("EK") - or token.startswith("GSV") - ): - data["Belegfeld 1"] = token + ref1, ref2 = self._get_matching_references(move, move.name, "") + data.update({"Belegfeld 1": ref1, "Belegfeld 2": ref2}) yield data