Journal entries in the event that a financial period is not opened

This commit is contained in:
Mazen Abdo 2024-10-06 11:16:25 +03:00
parent 888c656a51
commit 9e125ce637
2 changed files with 91 additions and 37 deletions

View File

@ -8,14 +8,15 @@
from odoo import api, fields, models, _
from odoo.exceptions import Warning, ValidationError
import datetime
from odoo.exceptions import RedirectWarning, UserError, ValidationError, AccessError
from odoo.tools.misc import formatLang, format_date, get_lang
class AccountMove(models.Model):
_name = "account.move"
_inherit = "account.move"
period_id = fields.Many2one('fiscalyears.periods',
string='Period', required=True, readonly=True,
string='Period', readonly=True,
states={'draft': [('readonly', False)]},
help='''The fiscalyear period
used for this receipt.''')
@ -34,41 +35,92 @@ class AccountMove(models.Model):
raise ValidationError(
_('There is no openning fiscal year periods in this date.'))
@api.constrains('date', 'period_id')
def _check_date_period(self):
"""
Check date and period_id are in the same date range
"""
for rec in self:
if rec.date and rec.period_id:
date = fields.Date.from_string(rec.date)
period_start_date = fields.Date.from_string(
rec.period_id.date_from)
period_end_date = fields.Date.from_string(
rec.period_id.date_to)
if not (date >= period_start_date and
date <= period_end_date):
raise ValidationError(
_('''Date and period must be in the same date range'''))
else:
raise ValidationError(
_('''You must enter date and period for this record'''))
# @api.constrains('date', 'period_id')
# def _check_date_period(self):
# """
# Check date and period_id are in the same date range
# """
# for rec in self:
# if rec.date and rec.period_id:
# date = fields.Date.from_string(rec.date)
# period_start_date = fields.Date.from_string(
# rec.period_id.date_from)
# period_end_date = fields.Date.from_string(
# rec.period_id.date_to)
# if not (date >= period_start_date and
# date <= period_end_date):
# raise ValidationError(
# _('''Date and period must be in the same date range'''))
# else:
# raise ValidationError(
# _('''You must enter date and period for this record'''))
@api.model
def create(self, vals):
date = vals.get('date', False)
if not date:
date = datetime.date.today()
period_id = vals.get('period_id', False)
if date and not period_id:
periods = self.env['fiscalyears.periods'].search( [('state', '=', 'open'),
('date_from', '<=', date),
('date_to', '>=', date)])
if periods:
vals.update({'period_id': periods[0].id})
else:
raise Warning(_('Their is no open periods for date %s') % (date))
return super(AccountMove, self).create(vals)
def _post(self, soft=True):
"""Post/Validate the documents."""
if soft:
future_moves = self.filtered(lambda move: move.date > fields.Date.context_today(self))
future_moves.auto_post = True
for move in future_moves:
msg = _('This move will be posted at the accounting date: %(date)s',
date=format_date(self.env, move.date))
move.message_post(body=msg)
to_post = self - future_moves
else:
to_post = self
# `user_has_group` won't be bypassed by `sudo()` since it doesn't change the user anymore.
if not self.env.su and not self.env.user.has_group('account.group_account_invoice'):
raise AccessError(_("You don't have the access rights to post an invoice."))
for move in to_post:
if not move.period_id:
period = self.env['fiscalyears.periods'].search([
('date_from', '<=', move.date),
('date_to', '>=', move.date),
], limit=1)
if period:
move.period_id = period.id
else:
raise UserError(_("No valid open period found for the date: %s") % move.date)
if move.partner_bank_id and not move.partner_bank_id.active:
raise UserError(
_("The recipient bank account link to this invoice is archived.\nSo you cannot confirm the invoice."))
if move.state == 'posted':
raise UserError(_('The entry %s (id %s) is already posted.') % (move.name, move.id))
if not move.line_ids.filtered(lambda line: not line.display_type):
raise UserError(_('You need to add a line before posting.'))
to_post.mapped('line_ids').create_analytic_lines()
to_post.write({
'state': 'posted',
'posted_before': True,
})
for move in to_post:
move.message_subscribe([p.id for p in [move.partner_id] if p not in move.sudo().message_partner_ids])
to_post._check_balanced()
return to_post
# @api.model
# def create(self, vals):
# date = vals.get('date', False)
# if not date:
# date = datetime.date.today()
# period_id = vals.get('period_id', False)
# if date and not period_id:
# periods = self.env['fiscalyears.periods'].search( [('state', '=', 'open'),
# ('date_from', '<=', date),
# ('date_to', '>=', date)])
# if periods:
# vals.update({'period_id': periods[0].id})
# else:
# raise Warning(_('Their is no open periods for date %s') % (date))
# return super(AccountMove, self).create(vals)
class AccountMoveLine(models.Model):

View File

@ -387,12 +387,14 @@ class AccountAsset(models.Model):
depreciation_date, already_depreciated_amount, amount_change_ids)
newline_vals_list = []
for newline_vals in newlines:
# no need of amount field, as it is computed and we don't want to trigger its inverse function
if 'period_id' in newline_vals:
del newline_vals['period_id']
del (newline_vals['amount_total'])
newline_vals_list.append(newline_vals)
new_moves = self.env['account.move'].create(newline_vals_list)
for move in new_moves:
commands.append((4, move.id))
return self.write({'depreciation_move_ids': commands})
def _recompute_board(self, depreciation_number, starting_sequence, amount_to_depreciate, depreciation_date,