Skip to content

Commit

Permalink
[FIX] account: get accounting date from invoicing date for vendor bills
Browse files Browse the repository at this point in the history
In the first days of the month, many of the previous month's invoices
could still to be posted since the lock date is not yet set to the last
day of the previous month. To avoid the user having to modify the
default accounting date on each such bill, detect whether the conditions
allow the invoice to be posted on the very last day of the oldest
possible month, and then adapt the accounting date to the last day of
that month.

task-2575560

closes odoo#72934

Signed-off-by: Laurent Smet <[email protected]>
  • Loading branch information
william-andre committed Jul 9, 2021
1 parent 5ed7be3 commit 3b4c6c7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
48 changes: 41 additions & 7 deletions addons/account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,50 @@ def _cleanup_write_orm_values(self, record, vals):
# ONCHANGE METHODS
# -------------------------------------------------------------------------

@api.onchange('invoice_date')
def _get_accounting_date(self, invoice_date, has_tax):
"""Get correct accounting date for previous periods, taking tax lock date into account.
When registering an invoice in the past, we still want the sequence to be increasing.
We then take the last day of the period, depending on the sequence format.
If there is a tax lock date and there are taxes involved, we register the invoice at the
last date of the first open period.
:param invoice_date (datetime.date): The invoice date
:param has_tax (bool): Iff any taxes are involved in the lines of the invoice
:return (datetime.date):
"""
tax_lock_date = self.company_id.tax_lock_date
today = fields.Date.today()
if invoice_date and tax_lock_date and has_tax and invoice_date <= tax_lock_date:
invoice_date = tax_lock_date + timedelta(days=1)

if self.is_sale_document(include_receipts=True):
return invoice_date
elif self.is_purchase_document(include_receipts=True):
highest_name = self.highest_name or self._get_last_sequence(relaxed=True)
number_reset = self._deduce_sequence_number_reset(highest_name)
if not highest_name or number_reset == 'month':
if (today.year, today.month) > (invoice_date.year, invoice_date.month):
return date_utils.get_month(invoice_date)[1]
else:
return max(invoice_date, today)
elif number_reset == 'year':
if today.year > invoice_date.year:
return date(invoice_date.year, 12, 31)
else:
return max(invoice_date, today)
return invoice_date

@api.onchange('invoice_date', 'highest_name', 'company_id')
def _onchange_invoice_date(self):
if self.invoice_date:
if not self.invoice_payment_term_id and (not self.invoice_date_due or self.invoice_date_due < self.invoice_date):
self.invoice_date_due = self.invoice_date
if (
self.is_sale_document() and self.date != self.invoice_date
or self.is_purchase_document() and not self.date
):
self.date = self.invoice_date

has_tax = bool(self.line_ids.tax_ids or self.line_ids.tax_tag_ids)
accounting_date = self._get_accounting_date(self.invoice_date, has_tax)
if accounting_date != self.date:
self.date = accounting_date
self._onchange_currency()

@api.onchange('journal_id')
Expand Down Expand Up @@ -2475,7 +2509,7 @@ def _post(self, soft=True):
# /!\ 'check_move_validity' must be there since the dynamic lines will be recomputed outside the 'onchange'
# environment.
if (move.company_id.tax_lock_date and move.date <= move.company_id.tax_lock_date) and (move.line_ids.tax_ids or move.line_ids.tax_tag_ids):
move.date = move.company_id.tax_lock_date + timedelta(days=1)
move.date = move._get_accounting_date(move.invoice_date or move.date, True)
move.with_context(check_move_validity=False)._onchange_currency()

# Create the analytic lines in batch is faster as it leads to less cache invalidation.
Expand Down
11 changes: 11 additions & 0 deletions addons/account/tests/test_account_move_in_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ def setUp(self):
self.term_line_vals_1,
], self.move_vals)

def test_in_invoice_onchange_invoice_date(self):
for tax_date, invoice_date, accounting_date in [
('2019-03-31', '2019-05-12', '2019-05-31'),
('2019-03-31', '2019-02-10', '2019-04-30'),
('2019-05-31', '2019-06-15', '2019-06-30'),
]:
self.invoice.company_id.tax_lock_date = tax_date
with Form(self.invoice) as move_form:
move_form.invoice_date = invoice_date
self.assertEqual(self.invoice.date, fields.Date.to_date(accounting_date))

def test_in_invoice_line_onchange_product_1(self):
move_form = Form(self.invoice)
with move_form.invoice_line_ids.edit(0) as line_form:
Expand Down
11 changes: 11 additions & 0 deletions addons/account/tests/test_account_move_out_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ def setUp(self):
self.term_line_vals_1,
], self.move_vals)

def test_out_invoice_onchange_invoice_date(self):
for tax_date, invoice_date, accounting_date in [
('2019-03-31', '2019-05-12', '2019-05-12'),
('2019-03-31', '2019-02-10', '2019-04-1'),
('2019-05-31', '2019-06-15', '2019-06-15'),
]:
self.invoice.company_id.tax_lock_date = tax_date
with Form(self.invoice) as move_form:
move_form.invoice_date = invoice_date
self.assertEqual(self.invoice.date, fields.Date.to_date(accounting_date))

def test_out_invoice_line_onchange_product_1(self):
move_form = Form(self.invoice)
with move_form.invoice_line_ids.edit(0) as line_form:
Expand Down

0 comments on commit 3b4c6c7

Please sign in to comment.