2021-09-07 00:42:41 +02:00
|
|
|
# Copyright 2021 Akretion France (http://www.akretion.com/)
|
|
|
|
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
2021-09-08 14:21:43 +02:00
|
|
|
from odoo import api, fields, models
|
2021-09-07 00:42:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AccountMove(models.Model):
|
|
|
|
_inherit = "account.move"
|
|
|
|
|
2021-09-08 14:21:43 +02:00
|
|
|
name = fields.Char(compute="_compute_name_by_sequence")
|
2023-03-15 11:45:31 +01:00
|
|
|
# highest_name, sequence_prefix, sequence_number are not needed any more
|
2021-09-08 14:21:43 +02:00
|
|
|
# -> compute=False to improve perf
|
|
|
|
highest_name = fields.Char(compute=False)
|
|
|
|
sequence_prefix = fields.Char(compute=False)
|
|
|
|
sequence_number = fields.Integer(compute=False)
|
2023-03-15 11:45:31 +01:00
|
|
|
# made_sequence_hole is not relevant anymore (since based on sequence_prefix/number)
|
|
|
|
# -> compute=False to improve perf and to avoid displaying warning
|
|
|
|
made_sequence_hole = fields.Boolean(compute=False)
|
2021-09-08 14:21:43 +02:00
|
|
|
|
2022-05-13 18:14:51 +02:00
|
|
|
_sql_constraints = [
|
|
|
|
(
|
|
|
|
"name_state_diagonal",
|
|
|
|
"CHECK(COALESCE(name, '') NOT IN ('/', '') OR state!='posted')",
|
|
|
|
'A move can not be posted with name "/" or empty value\n'
|
|
|
|
"Check the journal sequence, please",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2021-09-08 14:21:43 +02:00
|
|
|
@api.depends("state", "journal_id", "date")
|
|
|
|
def _compute_name_by_sequence(self):
|
|
|
|
for move in self:
|
|
|
|
name = move.name or "/"
|
|
|
|
# I can't use posted_before in this IF because
|
|
|
|
# posted_before is set to True in _post() at the same
|
|
|
|
# time as state is set to "posted"
|
2021-09-07 00:42:41 +02:00
|
|
|
if (
|
2021-09-08 14:21:43 +02:00
|
|
|
move.state == "posted"
|
|
|
|
and (not move.name or move.name == "/")
|
|
|
|
and move.journal_id
|
|
|
|
and move.journal_id.sequence_id
|
2021-09-07 00:42:41 +02:00
|
|
|
):
|
2021-09-08 14:21:43 +02:00
|
|
|
if (
|
|
|
|
move.move_type in ("out_refund", "in_refund")
|
|
|
|
and move.journal_id.type in ("sale", "purchase")
|
|
|
|
and move.journal_id.refund_sequence
|
|
|
|
and move.journal_id.refund_sequence_id
|
|
|
|
):
|
|
|
|
seq = move.journal_id.refund_sequence_id
|
|
|
|
else:
|
|
|
|
seq = move.journal_id.sequence_id
|
2022-07-07 16:56:27 +02:00
|
|
|
# next_by_id(date) only applies on ir.sequence.date_range selection
|
|
|
|
# => we use with_context(ir_sequence_date=date).next_by_id()
|
|
|
|
# which applies on ir.sequence.date_range selection AND prefix
|
|
|
|
name = seq.with_context(ir_sequence_date=move.date).next_by_id()
|
2021-09-08 14:21:43 +02:00
|
|
|
move.name = name
|
|
|
|
|
|
|
|
# We must by-pass this constraint of sequence.mixin
|
|
|
|
def _constrains_date_sequence(self):
|
|
|
|
return True
|
2022-05-26 00:58:09 +02:00
|
|
|
|
|
|
|
def _is_end_of_seq_chain(self):
|
|
|
|
invoices_no_gap_sequences = self.filtered(
|
|
|
|
lambda inv: inv.journal_id.sequence_id.implementation == "no_gap"
|
|
|
|
)
|
|
|
|
invoices_other_sequences = self - invoices_no_gap_sequences
|
|
|
|
if not invoices_other_sequences and invoices_no_gap_sequences:
|
|
|
|
return False
|
|
|
|
return super(AccountMove, invoices_other_sequences)._is_end_of_seq_chain()
|
2022-12-02 12:57:26 +01:00
|
|
|
|
|
|
|
def _fetch_duplicate_supplier_reference(self, only_posted=False):
|
|
|
|
moves = self.filtered(lambda m: m.is_purchase_document() and m.ref)
|
|
|
|
if moves:
|
|
|
|
self.flush_model(["name", "journal_id", "move_type", "state"])
|
|
|
|
return super()._fetch_duplicate_supplier_reference(only_posted=only_posted)
|
2023-01-20 12:11:00 +01:00
|
|
|
|
|
|
|
def _get_last_sequence(self, relaxed=False, with_prefix=None, lock=True):
|
|
|
|
return super()._get_last_sequence(relaxed, None, lock)
|