res_company.py 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. import calendar
  3. from dateutil.relativedelta import relativedelta
  4. from odoo import fields, models, api, _
  5. from odoo.exceptions import UserError
  6. class ResCompany(models.Model):
  7. _inherit = 'res.company'
  8. def _autorise_lock_date_changes(self, vals):
  9. '''Check the lock dates for the current companies. This can't be done in a api.constrains because we need
  10. to perform some comparison between new/old values. This method forces the lock dates to be irreversible.
  11. * You cannot set stricter restrictions on accountants than on users.
  12. Therefore, the All Users Lock Date must be anterior (or equal) to the Invoice/Bills Lock Date.
  13. * You cannot lock a period that has not yet ended.
  14. Therefore, the All Users Lock Date must be anterior (or equal) to the last day of the previous month.
  15. * Any new All Users Lock Date must be posterior (or equal) to the previous one.
  16. * You cannot delete a tax lock date, lock a period that is not finished yet or the tax lock date must be set after
  17. the last day of the previous month.
  18. :param vals: The values passed to the write method.
  19. '''
  20. period_lock_date = vals.get('period_lock_date') and fields.Date.from_string(vals['period_lock_date'])
  21. fiscalyear_lock_date = vals.get('fiscalyear_lock_date') and fields.Date.from_string(vals['fiscalyear_lock_date'])
  22. tax_lock_date = vals.get('tax_lock_date') and fields.Date.from_string(vals['tax_lock_date'])
  23. previous_month = fields.Date.today() + relativedelta(months=-1)
  24. days_previous_month = calendar.monthrange(previous_month.year, previous_month.month)
  25. previous_month = previous_month.replace(day=days_previous_month[1])
  26. for company in self:
  27. old_fiscalyear_lock_date = company.fiscalyear_lock_date
  28. old_period_lock_date = company.period_lock_date
  29. old_tax_lock_date = company.tax_lock_date
  30. # The user attempts to remove the tax lock date
  31. if old_tax_lock_date and not tax_lock_date and 'tax_lock_date' in vals:
  32. raise UserError(_('The tax lock date is irreversible and can\'t be removed.'))
  33. # The user attempts to set a tax lock date prior to the previous one
  34. if old_tax_lock_date and tax_lock_date and tax_lock_date < old_tax_lock_date:
  35. raise UserError(_('The new tax lock date must be set after the previous lock date.'))
  36. # In case of no new tax lock date in vals, fallback to the oldest
  37. tax_lock_date = tax_lock_date or old_tax_lock_date
  38. # The user attempts to set a tax lock date prior to the last day of previous month
  39. if tax_lock_date and tax_lock_date > previous_month:
  40. raise UserError(_('You cannot lock a period that has not yet ended. Therefore, the tax lock date must be anterior (or equal) to the last day of the previous month.'))
  41. # The user attempts to remove the lock date for accountants
  42. if old_fiscalyear_lock_date and not fiscalyear_lock_date and 'fiscalyear_lock_date' in vals:
  43. raise UserError(_('The lock date for accountants is irreversible and can\'t be removed.'))
  44. # The user attempts to set a lock date for accountants prior to the previous one
  45. if old_fiscalyear_lock_date and fiscalyear_lock_date and fiscalyear_lock_date < old_fiscalyear_lock_date:
  46. raise UserError(_('Any new All Users Lock Date must be posterior (or equal) to the previous one.'))
  47. # In case of no new fiscal year in vals, fallback to the oldest
  48. fiscalyear_lock_date = fiscalyear_lock_date or old_fiscalyear_lock_date
  49. if not fiscalyear_lock_date:
  50. continue
  51. # The user attempts to set a lock date for accountants prior to the last day of previous month
  52. if fiscalyear_lock_date > previous_month:
  53. raise UserError(_('You cannot lock a period that has not yet ended. Therefore, the All Users Lock Date must be anterior (or equal) to the last day of the previous month.'))
  54. # In case of no new period lock date in vals, fallback to the one defined in the company
  55. period_lock_date = period_lock_date or old_period_lock_date
  56. if not period_lock_date:
  57. continue
  58. # The user attempts to set a lock date for accountants prior to the lock date for users
  59. if period_lock_date < fiscalyear_lock_date:
  60. raise UserError(_('You cannot set stricter restrictions on accountants than on users. Therefore, the All Users Lock Date must be anterior (or equal) to the Invoice/Bills Lock Date.'))
  61. def write(self, vals):
  62. # fiscalyear_lock_date can't be set to a prior date
  63. if 'fiscalyear_lock_date' in vals or 'period_lock_date' in vals or 'tax_lock_date' in vals:
  64. self._autorise_lock_date_changes(vals)
  65. return super(ResCompany, self).write(vals)