コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
124 ビュー

Hi there

I am in Switzerland/Zurich timezone and its set correctly on the user. I have configured the Sales Order Sequence like in the screenshot below. The goal is, that it shows always YEAR-MONTH-DAY-(Subsequence restarting at 1 every day).

It works, but the issue is that it is shifted by some hours. If I create a Sale order at 22pm on 23. August 2025, it will already show SO-2025-08-24-01 instead of SO-2025-08-23-01. Does anybody know how to solve this?


アバター
破棄
最善の回答

Hi,


Odoo’s ir.sequence uses fields.Datetime.now() which gives a UTC timestamp. When the prefix is evaluated with date placeholders (%(year)s, %(month)s, %(day)s), it uses UTC values — not your user timezone.


1- Custom Fix with Local Time.


You can override the _get_prefix_suffix method of ir.sequence to apply the user’s timezone:


from odoo import models, fields

from pytz import timezone


class IrSequence(models.Model):

    _inherit = "ir.sequence"


    def _get_prefix_suffix(self, date=None, date_range=None, seq_date=None):

        # Call super

        prefix, suffix = super()._get_prefix_suffix(date, date_range, seq_date)


        # Adjust seq_date to user timezone

        user_tz = self.env.user.tz or 'UTC'

        tz = timezone(user_tz)

        if seq_date:

            seq_date = fields.Datetime.context_timestamp(self.env.user, seq_date)


        # Recompute prefix/suffix with local date

        if seq_date:

            d = {

                'year': seq_date.strftime('%Y'),

                'month': seq_date.strftime('%m'),

                'day': seq_date.strftime('%d'),

            }

            if prefix:

                prefix = prefix % d

            if suffix:

                suffix = suffix % d


        return prefix, suffix


This way, the sequence date uses the user’s timezone instead of UTC.

The problem happens because Odoo generates sequences in UTC, so your SO created at 23:00 Zurich time gets the next day’s date in the sequence. To fix it, override ir.sequence to compute the prefix with the user’s timezone instead of UTC.


Hope it helps


アバター
破棄