[IMP] : black, isort

This commit is contained in:
ahenriquez
2020-02-21 13:17:53 +01:00
committed by Miquel Raïch
parent c4fc6a2714
commit 9501a48e2e
5 changed files with 222 additions and 176 deletions

View File

@@ -2,18 +2,14 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Account Lock To Date',
'summary': """
"name": "Account Lock To Date",
"summary": """
Allows to set an account lock date in the future.""",
'version': '12.0.1.0.0',
'license': 'AGPL-3',
'author': 'ForgeFlow, Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/account-financial-tools',
'installable': True,
'depends': [
'account',
],
'data': [
'wizards/account_update_lock_to_date.xml',
],
"version": "12.0.1.0.0",
"license": "AGPL-3",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-financial-tools",
"installable": True,
"depends": ["account"],
"data": ["wizards/account_update_lock_to_date.xml"],
}

View File

@@ -1,31 +1,37 @@
# Copyright 2019 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models, _
from odoo import _, api, models
from odoo.exceptions import UserError
class AccountMove(models.Model):
_inherit = 'account.move'
_inherit = "account.move"
@api.multi
def _check_lock_date(self):
res = super()._check_lock_date()
for move in self:
lock_to_date = min(
move.company_id.period_lock_to_date,
move.company_id.fiscalyear_lock_to_date) or False
if self.user_has_groups('account.group_account_manager'):
lock_to_date = (
min(
move.company_id.period_lock_to_date,
move.company_id.fiscalyear_lock_to_date,
)
or False
)
if self.user_has_groups("account.group_account_manager"):
lock_to_date = move.company_id.fiscalyear_lock_to_date or False
if lock_to_date and move.date >= lock_to_date:
if self.user_has_groups('account.group_account_manager'):
message = _("You cannot add/modify entries after and "
"inclusive of the lock to date %s") % (
lock_to_date)
if self.user_has_groups("account.group_account_manager"):
message = _(
"You cannot add/modify entries after and "
"inclusive of the lock to date %s"
) % (lock_to_date)
else:
message = _("You cannot add/modify entries after and "
"inclusive of the lock to date %s. "
"Check the company settings or ask someone "
"with the 'Adviser' role") % (
lock_to_date)
message = _(
"You cannot add/modify entries after and "
"inclusive of the lock to date %s. "
"Check the company settings or ask someone "
"with the 'Adviser' role"
) % (lock_to_date)
raise UserError(message)
return res

View File

@@ -1,69 +1,83 @@
# Copyright 2019 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import datetime
from dateutil.relativedelta import relativedelta
import calendar
import time
from datetime import datetime
from time import mktime
from odoo import api, fields, models, SUPERUSER_ID, _
from odoo.tools.misc import DEFAULT_SERVER_DATE_FORMAT
from dateutil.relativedelta import relativedelta
from odoo import SUPERUSER_ID, _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.misc import DEFAULT_SERVER_DATE_FORMAT
class ResCompany(models.Model):
_inherit = 'res.company'
_inherit = "res.company"
period_lock_to_date = fields.Date(
string="Lock To Date for Non-Advisers",
help="Only users with the 'Adviser' role can edit "
"accounts after this date. "
"Use it for period locking inside an open fiscal year, "
"for example.")
"accounts after this date. "
"Use it for period locking inside an open fiscal year, "
"for example.",
)
fiscalyear_lock_to_date = fields.Date(
string="Lock To Date",
help="No users, including Advisers, can edit accounts after "
"this date. Use it for fiscal year locking for example.")
"this date. Use it for fiscal year locking for example.",
)
@api.multi
def write(self, vals):
# fiscalyear_lock_date can't be set to a prior date
if 'fiscalyear_lock_to_date' in vals or 'period_lock_to_date' in vals:
if "fiscalyear_lock_to_date" in vals or "period_lock_to_date" in vals:
self._check_lock_to_dates(vals)
return super(ResCompany, self).write(vals)
@api.multi
def _check_lock_to_dates(self, vals):
'''Check the lock to dates for the current companies.
"""Check the lock to dates for the current companies.
:param vals: The values passed to the write method.
'''
period_lock_to_date = vals.get('period_lock_to_date')
fiscalyear_lock_to_date = vals.get('fiscalyear_lock_to_date')
"""
period_lock_to_date = vals.get("period_lock_to_date")
fiscalyear_lock_to_date = vals.get("fiscalyear_lock_to_date")
next_month = datetime.now() + relativedelta(months=+1)
days_next_month = calendar.monthrange(next_month.year,
next_month.month)
next_month = next_month.replace(
day=days_next_month[1]).timetuple()
days_next_month = calendar.monthrange(next_month.year, next_month.month)
next_month = next_month.replace(day=days_next_month[1]).timetuple()
next_month = datetime.fromtimestamp(mktime(next_month)).date()
for company in self:
old_fiscalyear_lock_to_date = company.fiscalyear_lock_to_date
# The user attempts to remove the lock date for advisors
if old_fiscalyear_lock_to_date and \
not fiscalyear_lock_to_date and \
'fiscalyear_lock_to_date' in vals and \
not self._uid == SUPERUSER_ID:
raise ValidationError(_('The lock date for advisors is '
'irreversible and can\'t be removed.'))
if (
old_fiscalyear_lock_to_date
and not fiscalyear_lock_to_date
and "fiscalyear_lock_to_date" in vals
and not self._uid == SUPERUSER_ID
):
raise ValidationError(
_(
"The lock date for advisors is "
"irreversible and can't be removed."
)
)
# The user attempts to set a lock date for advisors prior
# to the previous one
if old_fiscalyear_lock_to_date and fiscalyear_lock_to_date and \
fiscalyear_lock_to_date > old_fiscalyear_lock_to_date:
if (
old_fiscalyear_lock_to_date
and fiscalyear_lock_to_date
and fiscalyear_lock_to_date > old_fiscalyear_lock_to_date
):
raise ValidationError(
_('The new lock to date for advisors must be set after '
'the previous lock to date.'))
_(
"The new lock to date for advisors must be set after "
"the previous lock to date."
)
)
# In case of no new fiscal year in vals, fallback to the oldest
if not fiscalyear_lock_to_date:
@@ -76,17 +90,20 @@ class ResCompany(models.Model):
# the first day of next month
if fiscalyear_lock_to_date < next_month:
raise ValidationError(
_('You cannot lock a period that is not finished yet. '
'Please make sure that the lock date for advisors is '
'not set after the last day of the previous month.'))
_(
"You cannot lock a period that is not finished yet. "
"Please make sure that the lock date for advisors is "
"not set after the last day of the previous month."
)
)
# In case of no new period lock to date in vals,
# fallback to the one defined in the company
if not period_lock_to_date:
if company.period_lock_date:
period_lock_to_date = time.strptime(
company.period_lock_to_date,
DEFAULT_SERVER_DATE_FORMAT)
company.period_lock_to_date, DEFAULT_SERVER_DATE_FORMAT
)
else:
continue
@@ -94,21 +111,31 @@ class ResCompany(models.Model):
# prior to the lock to date for users
if period_lock_to_date > fiscalyear_lock_to_date:
raise ValidationError(
_('You cannot define stricter conditions on advisors '
'than on users. Please make sure that the lock date '
'on advisor is set after the lock date for users.'))
_(
"You cannot define stricter conditions on advisors "
"than on users. Please make sure that the lock date "
"on advisor is set after the lock date for users."
)
)
@api.multi
def _validate_fiscalyear_lock(self, values):
res = super()._validate_fiscalyear_lock(values)
if values.get('fiscalyear_lock_to_date'):
nb_draft_entries = self.env['account.move'].search([
('company_id', 'in', self.ids),
('state', '=', 'draft'),
('date', '>=', values['fiscalyear_lock_to_date'])], limit=1)
if values.get("fiscalyear_lock_to_date"):
nb_draft_entries = self.env["account.move"].search(
[
("company_id", "in", self.ids),
("state", "=", "draft"),
("date", ">=", values["fiscalyear_lock_to_date"]),
],
limit=1,
)
if nb_draft_entries:
raise ValidationError(
_('There are still unposted entries in the period to date'
' you want to lock. '
'You should either post or delete them.'))
_(
"There are still unposted entries in the period to date"
" you want to lock. "
"You should either post or delete them."
)
)
return res

View File

@@ -1,128 +1,137 @@
# Copyright 2019 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError, ValidationError
from odoo.tools.misc import DEFAULT_SERVER_DATE_FORMAT
from datetime import datetime
from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import TransactionCase
from odoo.tools.misc import DEFAULT_SERVER_DATE_FORMAT
class TestAccountLockToDateUpdate(TransactionCase):
def setUp(self):
super(TestAccountLockToDateUpdate, self).setUp()
self.company = self.env.ref('base.main_company')
self.demo_user = self.env.ref('base.user_demo')
self.adviser_group = self.env.ref('account.group_account_manager')
self.UpdateLockToDateUpdateObj = self.env[
'account.update.lock_to_date'
].sudo(self.demo_user)
self.AccountObj = self.env['account.account']
self.AccountJournalObj = self.env['account.journal']
self.AccountMoveObj = self.env['account.move']
self.bank_journal = self.AccountJournalObj.create(
{'name': 'Bank Journal - BJ',
'code': 'BJ',
'type': 'bank',
'company_id': self.company.id,
}
self.company = self.env.ref("base.main_company")
self.demo_user = self.env.ref("base.user_demo")
self.adviser_group = self.env.ref("account.group_account_manager")
self.UpdateLockToDateUpdateObj = self.env["account.update.lock_to_date"].sudo(
self.demo_user
)
self.account_type_recv = self.env.ref(
'account.data_account_type_receivable')
self.account_type_rev = self.env.ref(
'account.data_account_type_revenue')
self.AccountObj = self.env["account.account"]
self.AccountJournalObj = self.env["account.journal"]
self.AccountMoveObj = self.env["account.move"]
self.bank_journal = self.AccountJournalObj.create(
{
"name": "Bank Journal - BJ",
"code": "BJ",
"type": "bank",
"company_id": self.company.id,
}
)
self.account_type_recv = self.env.ref("account.data_account_type_receivable")
self.account_type_rev = self.env.ref("account.data_account_type_revenue")
self.account_recv = self.AccountObj.create({
'code': 'RECV_DR',
'name': "Receivable (test)",
'reconcile': True,
'user_type_id': self.account_type_recv.id,
})
self.account_sale = self.AccountObj.create({
'code': 'SALE_DR',
'name': "Receivable (sale)",
'reconcile': True,
'user_type_id': self.account_type_rev.id,
})
self.account_recv = self.AccountObj.create(
{
"code": "RECV_DR",
"name": "Receivable (test)",
"reconcile": True,
"user_type_id": self.account_type_recv.id,
}
)
self.account_sale = self.AccountObj.create(
{
"code": "SALE_DR",
"name": "Receivable (sale)",
"reconcile": True,
"user_type_id": self.account_type_rev.id,
}
)
def create_account_move(self, date_str):
return self.AccountMoveObj.create({
'journal_id': self.bank_journal.id,
'date': date_str,
'line_ids': [
(0, 0, {
'name': "Debit",
'debit': 1000,
'account_id': self.account_recv.id,
}),
(0, 0, {
'name': "Credit",
'credit': 1000,
'account_id': self.account_sale.id,
}),
]
})
return self.AccountMoveObj.create(
{
"journal_id": self.bank_journal.id,
"date": date_str,
"line_ids": [
(
0,
0,
{
"name": "Debit",
"debit": 1000,
"account_id": self.account_recv.id,
},
),
(
0,
0,
{
"name": "Credit",
"credit": 1000,
"account_id": self.account_sale.id,
},
),
],
}
)
def create_account_lock_date_update(self):
return self.UpdateLockToDateUpdateObj.create({
'company_id': self.company.id,
})
return self.UpdateLockToDateUpdateObj.create({"company_id": self.company.id})
def test_01_update_without_access(self):
wizard = self.create_account_lock_date_update()
wizard.write({
'period_lock_to_date': '2900-01-01',
'fiscalyear_lock_to_date': '2900-01-01',
})
self.demo_user.write({
'groups_id': [(3, self.adviser_group.id)],
})
wizard.write(
{
"period_lock_to_date": "2900-01-01",
"fiscalyear_lock_to_date": "2900-01-01",
}
)
self.demo_user.write({"groups_id": [(3, self.adviser_group.id)]})
with self.assertRaises(UserError):
wizard.sudo(self.demo_user.id).execute()
def test_02_update_with_access(self):
wizard = self.create_account_lock_date_update()
wizard.write({
'period_lock_to_date': '2900-01-01',
'fiscalyear_lock_to_date': '2900-02-01',
})
self.demo_user.write({
'groups_id': [(4, self.adviser_group.id)],
})
wizard.write(
{
"period_lock_to_date": "2900-01-01",
"fiscalyear_lock_to_date": "2900-02-01",
}
)
self.demo_user.write({"groups_id": [(4, self.adviser_group.id)]})
wizard.sudo(self.demo_user.id).execute()
self.assertEqual(
self.company.period_lock_to_date,
datetime.strptime(
'2900-01-01',
DEFAULT_SERVER_DATE_FORMAT).date())
datetime.strptime("2900-01-01", DEFAULT_SERVER_DATE_FORMAT).date(),
)
self.assertEqual(
self.company.fiscalyear_lock_to_date,
datetime.strptime(
'2900-02-01',
DEFAULT_SERVER_DATE_FORMAT).date())
datetime.strptime("2900-02-01", DEFAULT_SERVER_DATE_FORMAT).date(),
)
def test_03_create_move_outside_period(self):
"""We test that we cannot create journal entries after the
locked date"""
self.company.period_lock_to_date = '2900-01-01'
self.company.fiscalyear_lock_to_date = '2900-02-01'
move = self.create_account_move('2900-01-01')
self.company.period_lock_to_date = "2900-01-01"
self.company.fiscalyear_lock_to_date = "2900-02-01"
move = self.create_account_move("2900-01-01")
with self.assertRaises(UserError):
move.sudo(self.demo_user.id).post()
def test_04_create_move_inside_period(self):
"""We test that we can successfully create a journal entry
within period that is not locked"""
self.company.period_lock_to_date = '2900-01-01'
self.company.fiscalyear_lock_to_date = '2900-02-01'
move = self.create_account_move('2800-01-01')
self.company.period_lock_to_date = "2900-01-01"
self.company.fiscalyear_lock_to_date = "2900-02-01"
move = self.create_account_move("2800-01-01")
move.sudo(self.demo_user.id).post()
self.assertEqual(move.state, 'posted')
self.assertEqual(move.state, "posted")
def test_05_lock_period_with_draft_moves(self):
"""We test that we cannot change the fiscal year lock to date
if there are draft journal entries after that date."""
self.create_account_move('2900-02-01')
self.create_account_move("2900-02-01")
with self.assertRaises(ValidationError):
self.company.period_lock_to_date = '2900-01-01'
self.company.fiscalyear_lock_to_date = '2900-02-01'
self.company.period_lock_to_date = "2900-01-01"
self.company.fiscalyear_lock_to_date = "2900-02-01"

View File

@@ -1,44 +1,50 @@
# Copyright 2019 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, SUPERUSER_ID, _
from odoo import SUPERUSER_ID, _, api, fields, models
from odoo.exceptions import UserError
class AccountUpdateLockToDate(models.TransientModel):
_name = 'account.update.lock_to_date'
_description = 'Account Update Lock_to_date'
_name = "account.update.lock_to_date"
_description = "Account Update Lock_to_date"
company_id = fields.Many2one(
comodel_name='res.company', string="Company", required=True,
default=lambda self: self.env.user.company_id)
comodel_name="res.company",
string="Company",
required=True,
default=lambda self: self.env.user.company_id,
)
period_lock_to_date = fields.Date(
string="Lock To Date for Non-Advisers",
help="Only users with the 'Adviser' role can edit accounts after "
"and inclusive of this date. Use it for period locking inside an "
"open fiscal year, for example.")
"and inclusive of this date. Use it for period locking inside an "
"open fiscal year, for example.",
)
fiscalyear_lock_to_date = fields.Date(
string="Lock To Date",
help="No users, including Advisers, can edit accounts after and "
"inclusive of this date. Use it for fiscal year locking for "
"example.")
"inclusive of this date. Use it for fiscal year locking for "
"example.",
)
@api.model
def default_get(self, field_list):
res = super(AccountUpdateLockToDate, self).default_get(field_list)
company = self.env.user.company_id
res.update({
'company_id': company.id,
'period_lock_to_date': company.period_lock_to_date,
'fiscalyear_lock_to_date': company.fiscalyear_lock_to_date,
})
res.update(
{
"company_id": company.id,
"period_lock_to_date": company.period_lock_to_date,
"fiscalyear_lock_to_date": company.fiscalyear_lock_to_date,
}
)
return res
@api.multi
def _check_execute_allowed(self):
self.ensure_one()
has_adviser_group = self.env.user.has_group(
'account.group_account_manager')
has_adviser_group = self.env.user.has_group("account.group_account_manager")
if not (has_adviser_group or self.env.uid == SUPERUSER_ID):
raise UserError(_("You are not allowed to execute this action."))
@@ -46,7 +52,9 @@ class AccountUpdateLockToDate(models.TransientModel):
def execute(self):
self.ensure_one()
self._check_execute_allowed()
self.company_id.sudo().write({
'period_lock_to_date': self.period_lock_to_date,
'fiscalyear_lock_to_date': self.fiscalyear_lock_to_date,
})
self.company_id.sudo().write(
{
"period_lock_to_date": self.period_lock_to_date,
"fiscalyear_lock_to_date": self.fiscalyear_lock_to_date,
}
)