|
6 | 6 | class AccountMove(models.Model): |
7 | 7 | _inherit = "account.move" |
8 | 8 |
|
9 | | - def _l10n_it_edi_document_type_mapping(self): |
10 | | - """Deferred invoices (not direct) require TD24 FatturaPA Document Type.""" |
11 | | - res = super()._l10n_it_edi_document_type_mapping() |
12 | | - for document_type, infos in res.items(): |
13 | | - if document_type == "TD07": |
14 | | - continue |
15 | | - infos["direct_invoice"] = True |
16 | | - res["TD24"] = { |
17 | | - "move_types": ["out_invoice"], |
18 | | - "import_type": "in_invoice", |
19 | | - "direct_invoice": False, |
20 | | - } |
21 | | - return res |
22 | | - |
23 | 9 | def _l10n_it_edi_invoice_is_direct(self): |
24 | 10 | """An invoice is direct if ddt are all done the same day as the invoice.""" |
25 | 11 | for ddt in self.delivery_note_ids: |
26 | 12 | if not ddt.date or ddt.date != self.invoice_date: |
27 | 13 | return False |
28 | | - return True |
| 14 | + return super()._l10n_it_edi_invoice_is_direct() |
| 15 | + |
| 16 | + def _l10n_it_edi_get_values(self, pdf_values=None): |
| 17 | + """Extend to add dati_ddt_list for delivery notes.""" |
| 18 | + values = super()._l10n_it_edi_get_values(pdf_values) |
| 19 | + values["dati_ddt_list"] = self._get_dati_ddt() |
| 20 | + return values |
| 21 | + |
| 22 | + def _get_dati_ddt(self): |
| 23 | + """ |
| 24 | + Get the data for rendering DatiDDT. |
| 25 | +
|
| 26 | + :return: a list of dictionaries, with one dictionary per involved DdT. |
| 27 | + Each dictionary has shape: |
| 28 | + { |
| 29 | + '_delivery_note': <stock.delivery.note record of the involved the DdT>, |
| 30 | + 'NumeroDDT': <string representing the DdT>, |
| 31 | + 'DataDDT': <date of the DdT>, |
| 32 | + '_invoice_lines': (optional) |
| 33 | + <account.move.line records of invoice lines involved in the DdT>, |
| 34 | + 'RiferimentoNumeroLinea': (optional) |
| 35 | + <list of integers representing invoice line numbers>, |
| 36 | + } |
| 37 | + """ |
| 38 | + self.ensure_one() |
| 39 | + dati_ddt_list = [] |
| 40 | + |
| 41 | + if not self.delivery_note_ids: |
| 42 | + return dati_ddt_list |
| 43 | + |
| 44 | + e_invoice_lines = self.line_ids.filtered( |
| 45 | + lambda x: x.display_type == "product" |
| 46 | + ).sorted(lambda line: line.sequence) |
| 47 | + e_invoice_lines_list = list(e_invoice_lines) |
| 48 | + |
| 49 | + for delivery_note in self.delivery_note_ids: |
| 50 | + ddt_data = { |
| 51 | + "_delivery_note": delivery_note, |
| 52 | + "NumeroDDT": delivery_note.name, |
| 53 | + "DataDDT": delivery_note.date, |
| 54 | + } |
| 55 | + |
| 56 | + # Find invoice lines linked to this delivery note |
| 57 | + e_invoice_delivery_note_lines = e_invoice_lines.filtered( |
| 58 | + lambda line, dn=delivery_note: line.delivery_note_id == dn |
| 59 | + ) |
| 60 | + |
| 61 | + if e_invoice_delivery_note_lines: |
| 62 | + # RiferimentoNumeroLinea contains 1-based line numbers |
| 63 | + lines_refs_list = [ |
| 64 | + e_invoice_lines_list.index(line) + 1 |
| 65 | + for line in e_invoice_delivery_note_lines |
| 66 | + ] |
| 67 | + ddt_data.update( |
| 68 | + { |
| 69 | + "_invoice_lines": e_invoice_delivery_note_lines, |
| 70 | + "RiferimentoNumeroLinea": lines_refs_list, |
| 71 | + } |
| 72 | + ) |
| 73 | + elif len(self.delivery_note_ids) == 1: |
| 74 | + # If there's only one DDT and no lines are linked, include all lines |
| 75 | + # This handles the case where update_delivery_note_lines() wasn't called |
| 76 | + ddt_data.update( |
| 77 | + { |
| 78 | + "_invoice_lines": e_invoice_lines, |
| 79 | + "RiferimentoNumeroLinea": list( |
| 80 | + range(1, len(e_invoice_lines_list) + 1) |
| 81 | + ), |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + dati_ddt_list.append(ddt_data) |
29 | 86 |
|
30 | | - def _l10n_it_edi_features_for_document_type_selection(self): |
31 | | - res = super()._l10n_it_edi_features_for_document_type_selection() |
32 | | - res["direct_invoice"] = self._l10n_it_edi_invoice_is_direct() |
33 | | - return res |
| 87 | + return dati_ddt_list |
0 commit comments