| 1 |
import contextlib |
| 2 |
try: |
| 3 |
import phonenumbers |
| 4 |
except ImportError: |
| 5 |
phonenumbers = None |
| 6 |
|
| 7 |
from odoo import _, api, fields, models, modules |
| 8 |
from odoo.exceptions import RedirectWarning, UserError, ValidationError |
| 9 |
|
| 10 |
from odoo.addons.account_edi_proxy_client.models.account_edi_proxy_user import AccountEdiProxyError |
| 11 |
from odoo.addons.l10n_dk_nemhandel.tools.demo_utils import handle_demo |
| 12 |
|
| 13 |
|
| 14 |
class NemhandelRegistration(models.TransientModel): |
| 15 |
_name = 'nemhandel.registration' |
| 16 |
_description = "Nemhandel Registration" |
| 17 |
|
| 18 |
company_id = fields.Many2one( |
| 19 |
comodel_name='res.company', |
| 20 |
required=True, |
| 21 |
default=lambda self: self.env.company, |
| 22 |
) |
| 23 |
contact_email = fields.Char( |
| 24 |
related='company_id.nemhandel_contact_email', |
| 25 |
readonly=False, |
| 26 |
required=True, |
| 27 |
) |
| 28 |
edi_mode = fields.Selection( |
| 29 |
string='EDI mode', |
| 30 |
selection=[('demo', 'Demo'), ('test', 'Test'), ('prod', 'Live')], |
| 31 |
compute='_compute_edi_mode', |
| 32 |
inverse='_inverse_edi_mode', |
| 33 |
readonly=False, |
| 34 |
) |
| 35 |
edi_user_id = fields.Many2one( |
| 36 |
comodel_name='account_edi_proxy_client.user', |
| 37 |
string='EDI user', |
| 38 |
compute='_compute_edi_user_id', |
| 39 |
) |
| 40 |
phone_number = fields.Char(related='company_id.nemhandel_phone_number', readonly=False, inverse='_inverse_phone_number') |
| 41 |
l10n_dk_nemhandel_proxy_state = fields.Selection(related='company_id.l10n_dk_nemhandel_proxy_state', readonly=False) |
| 42 |
verification_code = fields.Char(related='edi_user_id.nemhandel_verification_code', readonly=False) |
| 43 |
identifier_type = fields.Selection(related='company_id.nemhandel_identifier_type', readonly=False, required=True) |
| 44 |
identifier_value = fields.Char(related='company_id.nemhandel_identifier_value', readonly=False, required=True) |
| 45 |
|
| 46 |
# ------------------------------------------------------------------------- |
| 47 |
# ONCHANGE METHODS |
| 48 |
# ------------------------------------------------------------------------- |
| 49 |
|
| 50 |
@api.onchange('identifier_value') |
| 51 |
def _onchange_identifier_value(self): |
| 52 |
for wizard in self: |
| 53 |
if wizard.identifier_value: |
| 54 |
wizard.identifier_value = ''.join(char for char in wizard.identifier_value if char.isalnum()) |
| 55 |
|
| 56 |
@api.onchange('phone_number') |
| 57 |
def _onchange_phone_number(self): |
| 58 |
self.env['res.company']._check_phonenumbers_import() |
| 59 |
if self.phone_number: |
| 60 |
# The `phone_number` we set is not necessarily valid (may fail `_sanitize_nemhandel_phone_number`) |
| 61 |
with contextlib.suppress(phonenumbers.NumberParseException): |
| 62 |
parsed_phone_number = phonenumbers.parse( |
| 63 |
self.phone_number, |
| 64 |
region=self.company_id.country_code, |
| 65 |
) |
| 66 |
self.phone_number = phonenumbers.format_number( |
| 67 |
parsed_phone_number, |
| 68 |
phonenumbers.PhoneNumberFormat.E164, |
| 69 |
) |
| 70 |
|
| 71 |
# ------------------------------------------------------------------------- |
| 72 |
# COMPUTE METHODS |
| 73 |
# ------------------------------------------------------------------------- |
| 74 |
|
| 75 |
@api.depends('company_id.account_edi_proxy_client_ids') |
| 76 |
def _compute_edi_user_id(self): |
| 77 |
for wizard in self: |
| 78 |
wizard.edi_user_id = wizard.company_id.nemhandel_edi_user |
| 79 |
|
| 80 |
@api.depends('edi_user_id') |
| 81 |
def _compute_edi_mode(self): |
| 82 |
edi_mode = self.env['ir.config_parameter'].sudo().get_param('l10n_dk_nemhandel.edi.mode') |
| 83 |
for wizard in self: |
| 84 |
if wizard.edi_user_id: |
| 85 |
wizard.edi_mode = wizard.edi_user_id.edi_mode |
| 86 |
else: |
| 87 |
wizard.edi_mode = edi_mode or 'prod' |
| 88 |
|
| 89 |
def _inverse_edi_mode(self): |
| 90 |
for wizard in self: |
| 91 |
if not wizard.edi_user_id and wizard.edi_mode: |
| 92 |
self.env['ir.config_parameter'].sudo().set_param('l10n_dk_nemhandel.edi.mode', wizard.edi_mode) |
| 93 |
|
| 94 |
# ------------------------------------------------------------------------- |
| 95 |
# BUSINESS ACTIONS |
| 96 |
# ------------------------------------------------------------------------- |
| 97 |
|
| 98 |
def _action_open_nemhandel_form(self, reopen=True): |
| 99 |
if reopen: |
| 100 |
return self._get_records_action( |
| 101 |
name=_("Send via Nemhandel"), |
| 102 |
res_id=self.id, |
| 103 |
target='new', |
| 104 |
context={**self.env.context, 'disable_sms_verification': True}, |
| 105 |
) |
| 106 |
|
| 107 |
return self._get_records_action( |
| 108 |
name=_("Send via Nemhandel"), |
| 109 |
target='new', |
| 110 |
) |
| 111 |
|
| 112 |
def _action_send_notification(self, title, message): |
| 113 |
move_ids = self.env.context.get('active_ids') |
| 114 |
if move_ids and self.env.context.get('active_model') == 'account.move': |
| 115 |
next_action = self.env['account.move'].browse(move_ids).action_send_and_print() |
| 116 |
next_action['views'] = [(False, 'form')] |
| 117 |
else: |
| 118 |
next_action = {'type': 'ir.actions.act_window_close'} |
| 119 |
|
| 120 |
return { |
| 121 |
'type': 'ir.actions.client', |
| 122 |
'tag': 'display_notification', |
| 123 |
'params': { |
| 124 |
'title': title, |
| 125 |
'type': 'success', |
| 126 |
'message': message, |
| 127 |
'next': next_action, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@handle_demo |
| 132 |
def button_nemhandel_registration_sms(self): |
| 133 |
""" |
| 134 |
The first step of the Nemhandel onboarding. |
| 135 |
- Creates an EDI proxy user on the iap side, then the client side |
| 136 |
- Calls /activate_participant to mark the EDI user as nemhandel user |
| 137 |
- Sends an SMS code |
| 138 |
""" |
| 139 |
self.ensure_one() |
| 140 |
|
| 141 |
if self.l10n_dk_nemhandel_proxy_state != 'not_registered': |
| 142 |
raise UserError(_('Cannot register a user with a %s application', self.l10n_dk_nemhandel_proxy_state)) |
| 143 |
|
| 144 |
if not self.phone_number: |
| 145 |
raise ValidationError(_("Please enter a phone number to verify your application.")) |
| 146 |
if not self.contact_email: |
| 147 |
raise ValidationError(_("Please enter a primary contact email to verify your application.")) |
| 148 |
if not self.env.company.vat: |
| 149 |
raise RedirectWarning( |
| 150 |
_("Please fill in your company's VAT"), |
| 151 |
self.env.ref('base.action_res_company_form').id, |
| 152 |
_('Company settings') |
| 153 |
) |
| 154 |
|
| 155 |
if not self.edi_user_id: |
| 156 |
edi_user = self.edi_user_id.sudo()._register_proxy_user(self.company_id, 'nemhandel', self.edi_mode) |
| 157 |
self.edi_user_id = edi_user |
| 158 |
|
| 159 |
# if there is an error when activating the participant below, |
| 160 |
# the client side is rolled back and the edi user is deleted on the client side |
| 161 |
# but remains on the proxy side. |
| 162 |
# it is important to keep these two in sync, so commit before activating. |
| 163 |
if not modules.module.current_test: |
| 164 |
self.env.cr.commit() |
| 165 |
|
| 166 |
self.edi_user_id._check_user_on_alternative_service() |
| 167 |
|
| 168 |
if self.edi_user_id.edi_mode != 'demo': |
| 169 |
return self.send_nemhandel_verification_code() |
| 170 |
return self._action_open_nemhandel_form() |
| 171 |
|
| 172 |
@handle_demo |
| 173 |
def button_nemhandel_receiver_registration(self): |
| 174 |
""" |
| 175 |
The user is registered on the Nemhandel network, i.e. can receive documents from other Nemhandel participants. |
| 176 |
""" |
| 177 |
self.ensure_one() |
| 178 |
try: |
| 179 |
self.edi_user_id._nemhandel_register_as_receiver() |
| 180 |
except (UserError, AccountEdiProxyError) as e: |
| 181 |
self.button_deregister_nemhandel_participant() |
| 182 |
registration_form_action = self._action_open_nemhandel_form() |
| 183 |
registration_form_action['views'] = [(False, 'form')] |
| 184 |
return { |
| 185 |
'type': 'ir.actions.client', |
| 186 |
'tag': 'display_notification', |
| 187 |
'params': { |
| 188 |
'type': 'danger', |
| 189 |
'message': e, |
| 190 |
'next': registration_form_action, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if self.company_id.l10n_dk_nemhandel_proxy_state == 'receiver': |
| 195 |
return self._action_send_notification( |
| 196 |
title=_("Registered to receive documents."), |
| 197 |
message=_("You can now receive documents via Nemhandel."), |
| 198 |
) |
| 199 |
return self._action_open_nemhandel_form() |
| 200 |
|
| 201 |
@handle_demo |
| 202 |
def button_update_nemhandel_user_data(self): |
| 203 |
""" |
| 204 |
Action for the user to be able to update their contact details any time |
| 205 |
Calls /update_user on the iap server |
| 206 |
""" |
| 207 |
self.ensure_one() |
| 208 |
|
| 209 |
if not self.contact_email: |
| 210 |
raise ValidationError(_("Contact email and phone number are required.")) |
| 211 |
|
| 212 |
params = { |
| 213 |
'update_data': { |
| 214 |
'nemhandel_contact_email': self.contact_email, |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
self.edi_user_id._call_nemhandel_proxy( |
| 219 |
endpoint='/api/nemhandel/1/update_user', |
| 220 |
params=params, |
| 221 |
) |
| 222 |
|
| 223 |
def send_nemhandel_verification_code(self): |
| 224 |
""" |
| 225 |
Request user verification via SMS |
| 226 |
Calls the /send_verification_code to activate the participant and send the 6-digit verification code |
| 227 |
""" |
| 228 |
self.ensure_one() |
| 229 |
self.edi_user_id._call_nemhandel_proxy( |
| 230 |
endpoint='/api/nemhandel/1/send_verification_code', |
| 231 |
params={ |
| 232 |
'company_details': self.edi_user_id._get_nemhandel_company_details(), |
| 233 |
'message': _('Your confirmation code is'), |
| 234 |
}, |
| 235 |
) |
| 236 |
self.l10n_dk_nemhandel_proxy_state = 'in_verification' |
| 237 |
return self._action_open_nemhandel_form() |
| 238 |
|
| 239 |
@handle_demo |
| 240 |
def button_check_nemhandel_verification_code(self): |
| 241 |
""" |
| 242 |
Calls /verify_phone_number to compare user's input and the |
| 243 |
code generated on the IAP server |
| 244 |
""" |
| 245 |
self.ensure_one() |
| 246 |
if self.l10n_dk_nemhandel_proxy_state != 'in_verification': |
| 247 |
raise ValidationError(_("Please first verify your phone number by clicking on 'Send a registration code by SMS'.")) |
| 248 |
|
| 249 |
if not self.verification_code or len(self.verification_code) != 6: |
| 250 |
raise ValidationError(_("The verification code should contain six digits.")) |
| 251 |
|
| 252 |
company = self.company_id |
| 253 |
response = self.edi_user_id._call_nemhandel_proxy( |
| 254 |
endpoint='/api/nemhandel/1/verify_phone_number', |
| 255 |
params={'verification_code': self.verification_code}, |
| 256 |
) |
| 257 |
|
| 258 |
if error_code := response.get('warning', {}).get('code'): |
| 259 |
errors = { |
| 260 |
'code_incorrect': _('The verification code is not correct'), |
| 261 |
'code_expired': _('This verification code has expired. Please request a new one.'), |
| 262 |
'too_many_attempts': _('Too many attempts to request an SMS code. Please try again later.'), |
| 263 |
} |
| 264 |
raise UserError(errors.get(error_code) or _('Connection error, please try again later.')) |
| 265 |
|
| 266 |
self.verification_code = False |
| 267 |
company.l10n_dk_nemhandel_proxy_state = 'receiver' |
| 268 |
return self._action_send_notification( |
| 269 |
title=_("Registered to receive documents."), |
| 270 |
message=_("You can now receive documents via Nemhandel."), |
| 271 |
) |
| 272 |
|
| 273 |
@handle_demo |
| 274 |
def button_deregister_nemhandel_participant(self): |
| 275 |
""" |
| 276 |
Deregister the edi user from Nemhandel network |
| 277 |
""" |
| 278 |
self.ensure_one() |
| 279 |
|
| 280 |
if self.edi_user_id: |
| 281 |
self.edi_user_id._nemhandel_deregister_participant() |
| 282 |
self.verification_code = False |
| 283 |
return True |