Avoid inconsistent dates

This commit is contained in:
Alexis de Lattre
2019-02-27 21:41:56 +01:00
parent 65b68b7d65
commit 172241022a
5 changed files with 44 additions and 29 deletions

View File

@@ -7,7 +7,6 @@ from odoo.exceptions import UserError
class AccountUpdateLockDate(models.TransientModel):
_name = 'account.update.lock_date'
_description = 'Account Update Lock_date'
@@ -45,7 +44,12 @@ class AccountUpdateLockDate(models.TransientModel):
def execute(self):
self.ensure_one()
self._check_execute_allowed()
self.company_id.sudo().write({
vals = {
'period_lock_date': self.period_lock_date,
'fiscalyear_lock_date': self.fiscalyear_lock_date,
})
}
if (
self.period_lock_date and self.fiscalyear_lock_date and
self.period_lock_date < self.fiscalyear_lock_date):
vals['period_lock_date'] = self.fiscalyear_lock_date
self.company_id.sudo().write(vals)

View File

@@ -10,7 +10,7 @@
<field name="arch" type="xml">
<form>
<group name="main">
<field name="company_id" invisible="1"/>
<field name="company_id" groups="base.group_multi_company"/>
<field name="period_lock_date"/>
<field name="fiscalyear_lock_date"/>
</group>
@@ -30,10 +30,12 @@
<field name="target">new</field>
</record>
<menuitem id="account_update_lock_date_menu"
parent="account.account_account_menu"
action="account_update_lock_date_act_window"
groups="account.group_account_manager"
sequence="70"/>
<record model="ir.ui.menu" id="account_update_lock_date_menu">
<field name="name">Update Accounting Lock Dates</field>
<field name="parent_id" ref="account.account_account_menu"/>
<field name="action" ref="account_update_lock_date_act_window"/>
<field name="groups_id" eval="[(6, 0, [ref('account.group_account_manager')])]"/>
<field name="sequence" eval="70"/>
</record>
</odoo>

View File

@@ -1,39 +1,49 @@
# -*- coding: utf-8 -*-
# © 2016 Camptocamp SA (Matthieu Dietrich)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo import _, fields, models
from odoo.exceptions import UserError
class PermanentLockDateWizard(models.TransientModel):
_name = 'permanent.lock.date.wizard'
_description = 'Wizard to update the permanent lock date'
lock_date = fields.Date(string="New Lock Date")
company_id = fields.Many2one(comodel_name='res.company',
string='Company')
lock_date = fields.Date(string="New Permanent Lock Date", required=True)
company_id = fields.Many2one(
comodel_name='res.company',
string='Company', required=True, readonly=True)
current_lock_date = fields.Date(
related='company_id.permanent_lock_date', readonly=True,
string='Current Lock Date')
@api.multi
def confirm_date(self):
self.ensure_one()
company = self.company_id
if (company.permanent_lock_date and
self.lock_date < company.permanent_lock_date):
raise UserError(
_("You cannot set the permanent lock date in the past.")
)
raise UserError(_(
"You cannot set the new permanent lock date before the "
"current permanent lock date."))
# Then check if unposted moves are present before the date
moves = self.env['account.move'].search(
[('company_id', '=', company.id),
('date', '<=', self.lock_date),
('state', '=', 'draft')])
if moves:
raise UserError(
_("You cannot set the permanent lock date since entries are "
"still unposted before this date.")
)
raise UserError(_(
"You cannot set the new permanent lock date to %s "
"since some journal entries before that date are still "
"unposted.") % self.lock_date)
company.permanent_lock_date = self.lock_date
vals = {'permanent_lock_date': self.lock_date}
if (
company.period_lock_date and
company.period_lock_date < self.lock_date):
vals['period_lock_date'] = self.lock_date
if (
company.fiscalyear_lock_date and
company.fiscalyear_lock_date < self.lock_date):
vals['fiscalyear_lock_date'] = self.lock_date
company.write(vals)
return {'type': 'ir.actions.act_window_close'}

View File

@@ -7,6 +7,7 @@
<form string="Lock permanently entries">
<label string="All journal entries prior to and included at the new lock date will be permanently locked."/>
<group>
<field name="company_id" groups="base.group_multi_company"/>
<field name="current_lock_date"/>
<field name="lock_date" required="1"/>
</group>

View File

@@ -6,7 +6,6 @@ from odoo import api, fields, models
class AccountUpdateLockDate(models.TransientModel):
_inherit = 'account.update.lock_date'
tmp_permanent_lock_date = fields.Date(string="Permanent Lock Date")
@@ -21,15 +20,14 @@ class AccountUpdateLockDate(models.TransientModel):
def execute(self):
res = super(AccountUpdateLockDate, self).execute()
if self.tmp_permanent_lock_date != self.company_id.permanent_lock_date:
wizard = self.env['permanent.lock.date.wizard'].create({
'company_id': self.company_id.id,
'lock_date': self.tmp_permanent_lock_date,
})
return {
'type': 'ir.actions.act_window',
'res_model': 'permanent.lock.date.wizard',
'view_mode': 'form',
'res_id': wizard.id,
'target': 'new',
}
'context': {
'default_company_id': self.company_id.id,
'default_lock_date': self.tmp_permanent_lock_date,
}
}
return res