Debian Codegraph @

addons/account_edi/models/account_journal.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import api, models, fields, _
5 from odoo.exceptions import UserError
6
7 from collections import defaultdict
8
9
10 class AccountJournal(models.Model):
11 _inherit = 'account.journal'
12
13 edi_format_ids = fields.Many2many(comodel_name='account.edi.format',
14 string='Electronic invoicing',
15 help='Send XML/EDI invoices',
16 domain="[('id', 'in', compatible_edi_ids)]",
17 compute='_compute_edi_format_ids',
18 readonly=False, store=True)
19
20 compatible_edi_ids = fields.Many2many(comodel_name='account.edi.format',
21 compute='_compute_compatible_edi_ids',
22 help='EDI format that support moves in this journal')
23
24 def write(self, vals):
25 # OVERRIDE
26 # Don't allow the user to deactivate an edi format having at least one document to be processed.
27 if vals.get('edi_format_ids'):
28 old_edi_format_ids = self.edi_format_ids
29 res = super().write(vals)
30 diff_edi_format_ids = old_edi_format_ids - self.edi_format_ids
31 documents = self.env['account.edi.document'].search([
32 ('move_id.journal_id', 'in', self.ids),
33 ('edi_format_id', 'in', diff_edi_format_ids.ids),
34 ('state', 'in', ('to_cancel', 'to_send')),
35 ])
36 # If the formats we are unchecking do not need a webservice, we don't need them to be correctly sent
37 if documents.filtered(lambda d: d.edi_format_id._needs_web_services()):
38 raise UserError(_('Cannot deactivate (%s) on this journal because not all documents are synchronized', ', '.join(documents.edi_format_id.mapped('display_name'))))
39 # remove these documents which: do not need a web service & are linked to the edi formats we are unchecking
40 if documents:
41 documents.unlink()
42 return res
43 else:
44 return super().write(vals)
45
46 @api.depends('type', 'company_id', 'company_id.account_fiscal_country_id')
47 def _compute_compatible_edi_ids(self):
48 edi_formats = self.env['account.edi.format'].search([])
49
50 for journal in self:
51 compatible_edis = edi_formats.filtered(lambda e: e._is_compatible_with_journal(journal))
52 journal.compatible_edi_ids = compatible_edis
53
54 @api.depends('type', 'company_id', 'company_id.account_fiscal_country_id')
55 def _compute_edi_format_ids(self):
56 edi_formats = self.env['account.edi.format'].search([])
57 journal_ids = self.ids
58
59 if journal_ids:
60 self.env.cr.execute('''
61 SELECT
62 move.journal_id,
63 ARRAY_AGG(doc.edi_format_id) AS edi_format_ids
64 FROM account_edi_document doc
65 JOIN account_move move ON move.id = doc.move_id
66 WHERE doc.state IN ('to_cancel', 'to_send')
67 AND move.journal_id IN %s
68 GROUP BY move.journal_id
69 ''', [tuple(journal_ids)])
70 protected_edi_formats_per_journal = {r[0]: set(r[1]) for r in self.env.cr.fetchall()}
71 else:
72 protected_edi_formats_per_journal = defaultdict(set)
73
74 for journal in self:
75 enabled_edi_formats = edi_formats.filtered(lambda e: e._is_compatible_with_journal(journal) and
76 (e._is_enabled_by_default_on_journal(journal)
77 or (e in journal.edi_format_ids)))
78
79 # The existing edi formats that are already in use so we can't remove it.
80 protected_edi_format_ids = protected_edi_formats_per_journal.get(journal.id, set())
81 protected_edi_formats = journal.edi_format_ids.filtered(lambda e: e.id in protected_edi_format_ids)
82
83 journal.edi_format_ids = enabled_edi_formats + protected_edi_formats