Debian Codegraph @

addons/l10n_dk_nemhandel/models/res_company.py

raw symbols SCIP
1 from urllib.parse import urljoin
2
3 from odoo import _, api, fields, models
4 from odoo.exceptions import ValidationError
5
6 from odoo.addons.account.models.company import PEPPOL_LIST
7
8 try:
9 import phonenumbers
10 except ImportError:
11 phonenumbers = None
12
13
14 class ResCompany(models.Model):
15 _inherit = 'res.company'
16
17 nemhandel_contact_email = fields.Char(
18 string='Nemhandel Contact email',
19 compute='_compute_nemhandel_contact_email', store=True, readonly=False,
20 help='Primary contact email for Nemhandel-related communication',
21 )
22 nemhandel_phone_number = fields.Char(
23 string='Nemhandel Phone number (for validation)',
24 compute='_compute_nemhandel_phone_number', store=True, readonly=False,
25 help='You will receive a verification code to this phone number',
26 )
27 l10n_dk_nemhandel_proxy_state = fields.Selection(
28 selection=[
29 ('not_registered', 'Not registered'),
30 ('in_verification', 'In verification'),
31 ('receiver', 'Can send and receive'),
32 ('rejected', 'Rejected'),
33 ],
34 string='Nemhandel status', required=True, default='not_registered',
35 )
36 nemhandel_identifier_type = fields.Selection(related='partner_id.nemhandel_identifier_type', readonly=False)
37 nemhandel_identifier_value = fields.Char(related='partner_id.nemhandel_identifier_value', readonly=False)
38 nemhandel_purchase_journal_id = fields.Many2one(
39 comodel_name='account.journal',
40 string='Nemhandel Purchase Journal',
41 domain=[('type', '=', 'purchase')],
42 compute='_compute_nemhandel_purchase_journal_id', store=True, readonly=False,
43 )
44 nemhandel_edi_user = fields.Many2one(
45 comodel_name='account_edi_proxy_client.user',
46 compute='_compute_nemhandel_edi_user',
47 )
48
49 # -------------------------------------------------------------------------
50 # HELPER METHODS
51 # -------------------------------------------------------------------------
52
53 @api.model
54 def _check_phonenumbers_import(self):
55 if not phonenumbers:
56 raise ValidationError(_("Please install the phonenumbers library."))
57
58 def _sanitize_nemhandel_phone_number(self, phone_number=None):
59 self.ensure_one()
60
61 error_message = _(
62 "Please enter the phone number in the correct international format.\n"
63 "For example: +32123456789, where +32 is the country code.\n"
64 "Currently, only European countries are supported."
65 )
66
67 self._check_phonenumbers_import()
68
69 phone_number = phone_number or self.nemhandel_phone_number
70 if not phone_number:
71 return
72
73 if not phone_number.startswith('+'):
74 phone_number = f'+{phone_number}'
75
76 try:
77 phone_nbr = phonenumbers.parse(phone_number)
78 except phonenumbers.phonenumberutil.NumberParseException:
79 raise ValidationError(error_message)
80
81 country_code = phonenumbers.phonenumberutil.region_code_for_number(phone_nbr)
82 if country_code not in PEPPOL_LIST or not phonenumbers.is_valid_number(phone_nbr):
83 raise ValidationError(error_message)
84
85 # -------------------------------------------------------------------------
86 # CONSTRAINTS
87 # -------------------------------------------------------------------------
88
89 @api.constrains('nemhandel_phone_number')
90 def _check_nemhandel_phone_number(self):
91 for company in self:
92 if company.nemhandel_phone_number:
93 company._sanitize_nemhandel_phone_number()
94
95 @api.constrains('nemhandel_purchase_journal_id')
96 def _check_nemhandel_purchase_journal_id(self):
97 for company in self:
98 if company.nemhandel_purchase_journal_id and company.nemhandel_purchase_journal_id.type != 'purchase':
99 raise ValidationError(_("A purchase journal must be used to receive Nemhandel documents."))
100
101 # -------------------------------------------------------------------------
102 # COMPUTE METHODS
103 # -------------------------------------------------------------------------
104
105 @api.depends('l10n_dk_nemhandel_proxy_state')
106 def _compute_nemhandel_purchase_journal_id(self):
107 for company in self:
108 if not company.nemhandel_purchase_journal_id and company.l10n_dk_nemhandel_proxy_state not in {'not_registered', 'rejected'}:
109 company.nemhandel_purchase_journal_id = self.env['account.journal'].search([
110 *self.env['account.journal']._check_company_domain(company),
111 ('type', '=', 'purchase'),
112 ], limit=1)
113 company.nemhandel_purchase_journal_id.is_nemhandel_journal = True
114 else:
115 company.nemhandel_purchase_journal_id = company.nemhandel_purchase_journal_id
116
117 @api.depends('email')
118 def _compute_nemhandel_contact_email(self):
119 for company in self:
120 if not company.nemhandel_contact_email:
121 company.nemhandel_contact_email = company.email
122
123 @api.depends('phone')
124 def _compute_nemhandel_phone_number(self):
125 for company in self:
126 if not company.nemhandel_phone_number:
127 try:
128 # precompute only if it's a valid phone number
129 company._sanitize_nemhandel_phone_number(company.phone)
130 company.nemhandel_phone_number = company.phone
131 except ValidationError:
132 continue
133
134 @api.depends('account_edi_proxy_client_ids')
135 def _compute_nemhandel_edi_user(self):
136 for company in self:
137 company.nemhandel_edi_user = company.account_edi_proxy_client_ids.filtered(lambda u: u.proxy_type == 'nemhandel')
138
139 # -------------------------------------------------------------------------
140 # PEPPOL PARTICIPANT MANAGEMENT
141 # -------------------------------------------------------------------------
142
143 def _get_nemhandel_edi_mode(self):
144 self.ensure_one()
145 config_param = self.env['ir.config_parameter'].sudo().get_param('l10n_dk_nemhandel.edi.mode')
146 return self.sudo().nemhandel_edi_user.edi_mode or config_param or 'prod'
147
148 def _get_nemhandel_webhook_endpoint(self):
149 self.ensure_one()
150 return urljoin(self.get_base_url(), '/nemhandel/webhook')