Debian Codegraph @

addons/l10n_my_edi/models/res_company.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from odoo import api, fields, models
3
4
5 class ResCompany(models.Model):
6 _inherit = 'res.company'
7
8 # ------------------
9 # Fields declaration
10 # ------------------
11
12 l10n_my_edi_proxy_user_id = fields.Many2one(
13 comodel_name="account_edi_proxy_client.user",
14 compute="_compute_l10n_my_edi_proxy_user_id",
15 )
16 l10n_my_identification_type = fields.Selection(related='partner_id.l10n_my_identification_type', readonly=False)
17 l10n_my_identification_number = fields.Char(related='partner_id.l10n_my_identification_number', readonly=False)
18 l10n_my_identification_number_placeholder = fields.Char(compute="_compute_l10n_my_identification_number_placeholder")
19 l10n_my_edi_industrial_classification = fields.Many2one(related='partner_id.l10n_my_edi_industrial_classification', readonly=False)
20 l10n_my_edi_mode = fields.Selection(
21 selection=[
22 ('test', 'Pre-Production'),
23 ('prod', 'Production'),
24 ],
25 # Nothing will happen until the user register, so it can be set by default.
26 default="test",
27 )
28 # /!\ this was a planned feature that got scrapped due to API limitations. It may come back if their system provides better support for it.
29 l10n_my_edi_default_import_journal_id = fields.Many2one(
30 comodel_name="account.journal",
31 domain="[('type', '=', 'purchase')]",
32 string="Default import journal",
33 help="The journal on which invoices imported from MyInvois will be booked. Leave empty to use the default purchase journal.",
34 )
35
36 # --------------------------------
37 # Compute, inverse, search methods
38 # --------------------------------
39
40 @api.depends("account_edi_proxy_client_ids", 'l10n_my_edi_mode')
41 def _compute_l10n_my_edi_proxy_user_id(self):
42 """ Each company is expected to have at most one proxy user for malaysia for each mode.
43 Thus, we can easily find said user.
44 """
45 for company in self:
46 company.l10n_my_edi_proxy_user_id = company.account_edi_proxy_client_ids.filtered(
47 lambda u: u.proxy_type == 'l10n_my_edi' and u.edi_mode == company.l10n_my_edi_mode
48 )[:1]
49
50 @api.depends('l10n_my_identification_type')
51 def _compute_l10n_my_identification_number_placeholder(self):
52 """ Computes a dynamic placeholder that depends on the selected type to help the user inputs their data.
53 The placeholders have been taken from the MyInvois doc.
54 """
55 for company in self:
56 placeholder = 'N/A'
57 if company.l10n_my_identification_type == 'NRIC':
58 placeholder = '830503114923'
59 elif company.l10n_my_identification_type == 'BRN':
60 placeholder = '202201234565'
61 elif company.l10n_my_identification_type == 'PASSPORT':
62 placeholder = 'A00000000'
63 elif company.l10n_my_identification_type == 'ARMY':
64 placeholder = '830805134983'
65 company.l10n_my_identification_number_placeholder = placeholder
66
67 # ----------------
68 # Business methods
69 # ----------------
70
71 def _l10n_my_edi_create_proxy_user(self):
72 """ This method will create a new proxy user for the current company based on the selected mode, if no users already exists. """
73 self.ensure_one()
74 if not self.l10n_my_edi_proxy_user_id:
75 self.env['account_edi_proxy_client.user']._register_proxy_user(self, 'l10n_my_edi', self.l10n_my_edi_mode)
76
77 def _l10n_my_edi_enabled(self):
78 self.ensure_one()
79 return bool(self.sudo().l10n_my_edi_proxy_user_id)