|
| 1 | +from odoo import api, fields, models |
| 2 | + |
| 3 | +from odoo.addons.l10n_it_edi.models.account_move import get_text |
| 4 | + |
| 5 | + |
| 6 | +class AccountMoveRelatedDocumentType(models.Model): |
| 7 | + _name = "account.move.related_document" |
| 8 | + _description = "E-invoice Related Documents" |
| 9 | + |
| 10 | + type = fields.Selection( |
| 11 | + [ |
| 12 | + ("order", "Order"), |
| 13 | + ("contract", "Contract"), |
| 14 | + ("agreement", "Agreement"), |
| 15 | + ("reception", "Reception"), |
| 16 | + ("invoice", "Related Invoice"), |
| 17 | + ], |
| 18 | + "Document", |
| 19 | + required=True, |
| 20 | + ) |
| 21 | + name = fields.Char("Document ID", size=20, required=True) |
| 22 | + lineRef = fields.Integer("Line Ref.") |
| 23 | + invoice_id = fields.Many2one( |
| 24 | + "account.move", |
| 25 | + "Related Invoice", |
| 26 | + ondelete="cascade", |
| 27 | + index=True, |
| 28 | + readonly=True, |
| 29 | + ) |
| 30 | + date = fields.Date() |
| 31 | + numitem = fields.Char("Item Num.", size=20) |
| 32 | + code = fields.Char("Order Agreement Code", size=100) |
| 33 | + cig = fields.Char("CIG Code", size=15) |
| 34 | + cup = fields.Char("CUP Code", size=15) |
| 35 | + invoice_line_id = fields.Many2one( |
| 36 | + "account.move.line", |
| 37 | + "Related Invoice Line", |
| 38 | + ondelete="cascade", |
| 39 | + index=True, |
| 40 | + readonly=True, |
| 41 | + ) |
| 42 | + |
| 43 | + @api.model_create_multi |
| 44 | + def create(self, vals_list): |
| 45 | + line_obj = self.env["account.move.line"] |
| 46 | + for vals in vals_list: |
| 47 | + if ( |
| 48 | + vals.get("lineRef") |
| 49 | + and not vals.get("invoice_line_id") |
| 50 | + and vals.get("invoice_id") |
| 51 | + ): |
| 52 | + line = line_obj.search( |
| 53 | + [ |
| 54 | + ("move_id", "=", vals["invoice_id"]), |
| 55 | + ("sequence", "=", vals["lineRef"]), |
| 56 | + ], |
| 57 | + limit=1, |
| 58 | + ) |
| 59 | + if line: |
| 60 | + vals["invoice_line_id"] = line.id |
| 61 | + return super().create(vals_list) |
| 62 | + |
| 63 | + |
| 64 | +class AccountMove(models.Model): |
| 65 | + _inherit = "account.move" |
| 66 | + |
| 67 | + related_document_ids = fields.One2many( |
| 68 | + "account.move.related_document", "invoice_id", copy=False |
| 69 | + ) |
| 70 | + |
| 71 | + def _l10n_it_edi_get_values(self, pdf_values=None): |
| 72 | + res = super()._l10n_it_edi_get_values(pdf_values=pdf_values) |
| 73 | + updated_values = self.remove_redundant_values(res) |
| 74 | + return updated_values |
| 75 | + |
| 76 | + def remove_redundant_values(self, values): |
| 77 | + redundant_list = [ |
| 78 | + "cig", |
| 79 | + "cup", |
| 80 | + "origin_document_type", |
| 81 | + "origin_document_name", |
| 82 | + "origin_document_date", |
| 83 | + ] |
| 84 | + for key in redundant_list: |
| 85 | + values.pop(key, None) |
| 86 | + return values |
| 87 | + |
| 88 | + def _l10n_it_edi_import_invoice(self, invoice, data, is_new): |
| 89 | + res = super()._l10n_it_edi_import_invoice(invoice, data, is_new) |
| 90 | + tree = data["xml_tree"] |
| 91 | + rel_docs_dict = { |
| 92 | + "order": tree.xpath(".//DatiOrdineAcquisto"), |
| 93 | + "contract": tree.xpath(".//DatiContratto"), |
| 94 | + "agreement": tree.xpath(".//DatiConvenzione"), |
| 95 | + "reception": tree.xpath(".//DatiRicezione"), |
| 96 | + "invoice": tree.xpath(".//DatiFattureCollegate"), |
| 97 | + } |
| 98 | + self.create_related_document(invoice, rel_docs_dict) |
| 99 | + return res |
| 100 | + |
| 101 | + def create_related_document(self, invoice, rel_docs_dict): |
| 102 | + result = [] |
| 103 | + invoice_line_model = self.env["account.move.line"] |
| 104 | + for key, rel_doc in rel_docs_dict.items(): |
| 105 | + for element in rel_doc: |
| 106 | + invoice_lineid = False |
| 107 | + lineRef = get_text(element, "./RiferimentoNumeroLinea") |
| 108 | + if lineRef: |
| 109 | + invoice_line = invoice_line_model.search( |
| 110 | + [ |
| 111 | + ("move_id", "=", invoice.id), |
| 112 | + ("sequence", "=", int(lineRef)), |
| 113 | + ], |
| 114 | + limit=1, |
| 115 | + ) |
| 116 | + if invoice_line: |
| 117 | + invoice_lineid = invoice_line.id |
| 118 | + entry = { |
| 119 | + "type": key, |
| 120 | + "lineRef": lineRef, |
| 121 | + "name": get_text(element, "./IdDocumento"), |
| 122 | + "date": get_text(element, "./Data"), |
| 123 | + "numitem": get_text(element, "./NumItem"), |
| 124 | + "code": get_text(element, "./CodiceCommessaConvenzione"), |
| 125 | + "cup": get_text(element, "./CodiceCUP"), |
| 126 | + "cig": get_text(element, "./CodiceCIG"), |
| 127 | + "invoice_id": invoice.id, |
| 128 | + "invoice_line_id": invoice_lineid, |
| 129 | + } |
| 130 | + entry = {k: v for k, v in entry.items() if v} |
| 131 | + result.append(entry) |
| 132 | + model = self.env["account.move.related_document"] |
| 133 | + model.create(result) |
| 134 | + return result |
0 commit comments