Skip to content

Instantly share code, notes, and snippets.

@PierreCourt
Last active February 18, 2021 22:02
Show Gist options
  • Select an option

  • Save PierreCourt/cad494aa3d482abba0382fd6db08c396 to your computer and use it in GitHub Desktop.

Select an option

Save PierreCourt/cad494aa3d482abba0382fd6db08c396 to your computer and use it in GitHub Desktop.
[FIX] Sequence mixin constraint
# In case you find the following error, the scripts might be useful :
# "The %(date_field)s (%(date)s) doesn't match the %(sequence_field)s (%(sequence)s).\n"
# "You might want to clear the field %(sequence_field)s before proceeding with the change of the date.",
# The sequence mixin (addons/account/models/sequence_mixin.py) contains a new method, _constrains_date_sequence.
# That method checks that the date in the sequence is aligned with
# the date of the model (INV/2020/12/0001 for a date of 25/12/2020)
# This is a new constraints, and this can cause some problems for users that where using another format.
# Since v14, for account.move and other models, the sequence is defaulted in the code
# (on the format <JOURNAL_CODE>/YYYY/MONTH/NUMBER for accounting).
# But the next sequence is not based on the format and the next number (as in v13)
# but based on the automatic recognition of the previous format. (see comment of WAN on ticket 2445559)
# So if a user used ABC20120001 with ABC as the journal code, 20 for 2020, 12 for December and 0001 as the sequence number,
# the automatic recognition will see it as the year 2012. So the separator are a must now.
# That sequence check in the account.move can impact different types of account.move.
# For example, it can be a Vendor Bill or a Payment.
# For those two, if the client does not use the sequence
# (as the reference is usually the invoice number used by the vendor, the sequence here is mostly for internal ref),
# you can ask the client if you can recompute all the wrong sequence. For that, you can use the SCRIPT 1.
# On the other hand, if it is a Customer Invoice, it is used on the invoice sent to the customer, so it can't be changed.
# So, you have to set the correct sequence to a draft invoice using the SCRIPT 2.
# Once the correct sequence is set for a date, the following sequences will be correct.
# There are also 2 other options available:
# 1) Completely removed the constraint by setting a "sequence.mixin.constraint_start_date" in the ir.config_parameter
# The start_date is the date where the constrain will start to be checked (eg '1970-01-01')
# 2) Set a sequence_override_regex on the account.journal that will be use for the format recognition
# instead of the default yearly/monthly regex
#### SCRIPT 1 ###
# if you need this script for the account.payment (that inherits account.move), uncomment this part
#payments = env['account.payment'].search([])
#moves = [p.move_id for p in payments]
#seqfailures = []
# For the vendor bills, it is this part
# Journal set to vendor bills, to not impact the accounting of customer invoices
moves = env['account.move'].search([("journal_id","=",2)])
seqfailures = []
for record in moves:
date = record.date
sequence = record.name
if sequence and date:
format_values = record._get_sequence_format_param(sequence)[1]
if (format_values['year'] and format_values['year'] != date.year % 10**len(str(format_values['year'])) or format_values['month'] and format_values['month'] != date.month):
seqfailures.append(record.id)
# To check if a lot of tickets
#raise Warning(len(seqfailures))
if len(seqfailures) > 0:
env.cr.execute("update account_move set name = '/' where id in %s",[tuple(seqfailures)])
env.cr.commit()
seqfailures.sort()
moves = env['account.move'].browse(seqfailures)
for record in moves:
record._compute_name()
#### SCRIPT 2 ###
pre = 'INV'
padding = 4
move_id = 1500
move = env['account.move'].browse(move_id)
query = "select name from account_move where name like '" + pre + "/" + str(move.date.year) + "/"+ str(move.date.month) +"/%' order by name desc";
env.cr.execute(query)
seq = env.cr.fetchone()
if seq and seq[0]:
# if previous reference for that month, add 1 to the sequence
splits = seq[0].split("/")
split = splits[-1]
seq = int(split)
splits[-1] = str(seq+1).rjust(padding, '0')
name = "/".join(splits)
else:
## if no previous reference for that month
name = pre + "/" + str(move.date.year) + "/" + str(move.date.month) +"/" + "1".rjust(padding, '0')
#raise Warning(name)
move.write({"name":name})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment