setup_wizards.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import date, timedelta
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import ValidationError
  6. class FinancialYearOpeningWizard(models.TransientModel):
  7. _name = 'account.financial.year.op'
  8. _description = 'Opening Balance of Financial Year'
  9. company_id = fields.Many2one(comodel_name='res.company', required=True)
  10. opening_move_posted = fields.Boolean(string='Opening Move Posted', compute='_compute_opening_move_posted')
  11. opening_date = fields.Date(string='Opening Date', required=True, related='company_id.account_opening_date', help="Date from which the accounting is managed in Odoo. It is the date of the opening entry.", readonly=False)
  12. fiscalyear_last_day = fields.Integer(related="company_id.fiscalyear_last_day", required=True, readonly=False,
  13. help="The last day of the month will be used if the chosen day doesn't exist.")
  14. fiscalyear_last_month = fields.Selection(related="company_id.fiscalyear_last_month", readonly=False,
  15. required=True,
  16. help="The last day of the month will be used if the chosen day doesn't exist.")
  17. @api.depends('company_id.account_opening_move_id')
  18. def _compute_opening_move_posted(self):
  19. for record in self:
  20. record.opening_move_posted = record.company_id.opening_move_posted()
  21. @api.constrains('fiscalyear_last_day', 'fiscalyear_last_month')
  22. def _check_fiscalyear(self):
  23. # We try if the date exists in 2020, which is a leap year.
  24. # We do not define the constrain on res.company, since the recomputation of the related
  25. # fields is done one field at a time.
  26. for wiz in self:
  27. try:
  28. date(2020, int(wiz.fiscalyear_last_month), wiz.fiscalyear_last_day)
  29. except ValueError:
  30. raise ValidationError(
  31. _('Incorrect fiscal year date: day is out of range for month. Month: %s; Day: %s') %
  32. (wiz.fiscalyear_last_month, wiz.fiscalyear_last_day)
  33. )
  34. def write(self, vals):
  35. # Amazing workaround: non-stored related fields on company are a BAD idea since the 3 fields
  36. # must follow the constraint '_check_fiscalyear_last_day'. The thing is, in case of related
  37. # fields, the inverse write is done one value at a time, and thus the constraint is verified
  38. # one value at a time... so it is likely to fail.
  39. for wiz in self:
  40. wiz.company_id.write({
  41. 'fiscalyear_last_day': vals.get('fiscalyear_last_day') or wiz.company_id.fiscalyear_last_day,
  42. 'fiscalyear_last_month': vals.get('fiscalyear_last_month') or wiz.company_id.fiscalyear_last_month,
  43. 'account_opening_date': vals.get('opening_date') or wiz.company_id.account_opening_date,
  44. })
  45. wiz.company_id.account_opening_move_id.write({
  46. 'date': fields.Date.from_string(vals.get('opening_date') or wiz.company_id.account_opening_date) - timedelta(days=1),
  47. })
  48. vals.pop('opening_date', None)
  49. vals.pop('fiscalyear_last_day', None)
  50. vals.pop('fiscalyear_last_month', None)
  51. return super().write(vals)
  52. def action_save_onboarding_fiscal_year(self):
  53. self.env.company.sudo().set_onboarding_step_done('account_setup_fy_data_state')
  54. class SetupBarBankConfigWizard(models.TransientModel):
  55. _inherits = {'res.partner.bank': 'res_partner_bank_id'}
  56. _name = 'account.setup.bank.manual.config'
  57. _description = 'Bank setup manual config'
  58. _check_company_auto = True
  59. res_partner_bank_id = fields.Many2one(comodel_name='res.partner.bank', ondelete='cascade', required=True)
  60. new_journal_name = fields.Char(default=lambda self: self.linked_journal_id.name, inverse='set_linked_journal_id', required=True, help='Will be used to name the Journal related to this bank account')
  61. linked_journal_id = fields.Many2one(string="Journal",
  62. comodel_name='account.journal', inverse='set_linked_journal_id',
  63. compute="_compute_linked_journal_id",
  64. domain=lambda self: [('type', '=', 'bank'), ('bank_account_id', '=', False), ('company_id', '=', self.env.company.id)])
  65. bank_bic = fields.Char(related='bank_id.bic', readonly=False, string="Bic")
  66. num_journals_without_account = fields.Integer(default=lambda self: self._number_unlinked_journal())
  67. def _number_unlinked_journal(self):
  68. return self.env['account.journal'].search([('type', '=', 'bank'), ('bank_account_id', '=', False),
  69. ('id', '!=', self.default_linked_journal_id())], count=True)
  70. @api.onchange('acc_number')
  71. def _onchange_acc_number(self):
  72. for record in self:
  73. record.new_journal_name = record.acc_number
  74. @api.model_create_multi
  75. def create(self, vals_list):
  76. """ This wizard is only used to setup an account for the current active
  77. company, so we always inject the corresponding partner when creating
  78. the model.
  79. """
  80. for vals in vals_list:
  81. vals['partner_id'] = self.env.company.partner_id.id
  82. vals['new_journal_name'] = vals['acc_number']
  83. # If no bank has been selected, but we have a bic, we are using it to find or create the bank
  84. if not vals['bank_id'] and vals['bank_bic']:
  85. vals['bank_id'] = self.env['res.bank'].search([('bic', '=', vals['bank_bic'])], limit=1).id \
  86. or self.env['res.bank'].create({'name': vals['bank_bic'], 'bic': vals['bank_bic']}).id
  87. return super().create(vals_list)
  88. @api.onchange('linked_journal_id')
  89. def _onchange_new_journal_related_data(self):
  90. for record in self:
  91. if record.linked_journal_id:
  92. record.new_journal_name = record.linked_journal_id.name
  93. @api.depends('journal_id') # Despite its name, journal_id is actually a One2many field
  94. def _compute_linked_journal_id(self):
  95. for record in self:
  96. record.linked_journal_id = record.journal_id and record.journal_id[0] or record.default_linked_journal_id()
  97. def default_linked_journal_id(self):
  98. for journal_id in self.env['account.journal'].search([('type', '=', 'bank'), ('bank_account_id', '=', False)]):
  99. empty_journal_count = self.env['account.move'].search_count([('journal_id', '=', journal_id.id)])
  100. if empty_journal_count == 0:
  101. return journal_id.id
  102. return False
  103. def set_linked_journal_id(self):
  104. """ Called when saving the wizard.
  105. """
  106. for record in self:
  107. selected_journal = record.linked_journal_id
  108. if not selected_journal:
  109. new_journal_code = self.env['account.journal'].get_next_bank_cash_default_code('bank', self.env.company)
  110. company = self.env.company
  111. selected_journal = self.env['account.journal'].create({
  112. 'name': record.new_journal_name,
  113. 'code': new_journal_code,
  114. 'type': 'bank',
  115. 'company_id': company.id,
  116. 'bank_account_id': record.res_partner_bank_id.id,
  117. })
  118. else:
  119. selected_journal.bank_account_id = record.res_partner_bank_id.id
  120. selected_journal.name = record.new_journal_name
  121. def validate(self):
  122. """ Called by the validation button of this wizard. Serves as an
  123. extension hook in account_bank_statement_import.
  124. """
  125. self.linked_journal_id.mark_bank_setup_as_done_action()
  126. return {'type': 'ir.actions.client', 'tag': 'soft_reload'}