Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.0] [IMP] ir_sequence : create sequence by year, fiscal year, month, week… #10742

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 30 additions & 3 deletions openerp/addons/base/ir/ir_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import pytz
import calendar
import time

from datetime import datetime, timedelta
Expand Down Expand Up @@ -131,6 +132,10 @@ def _set_number_next_actual(self):
company_id = fields.Many2one('res.company', 'Company',
default=lambda s: s.env['res.company']._company_default_get('ir.sequence'))
use_date_range = fields.Boolean('Use subsequences per date_range')
date_range_type = fields.Selection(
[('year', 'Year'), ('fiscalyear', 'Fiscal year'), ('month', 'Month'), ('week', 'Week'), ('day', 'Day')],
'Type of date range', required=True, default='year',
help="Type of date range that will be automatically create if doesn't exist.")
date_range_ids = fields.One2many('ir.sequence.date_range', 'sequence_id', 'Subsequences')

def init(self, cr):
Expand Down Expand Up @@ -232,9 +237,31 @@ def _interpolation_dict():
return interpolated_prefix + '%%0%sd' % self.padding % number_next + interpolated_suffix

def _create_date_range_seq(self, date):
year = fields.Date.from_string(date).strftime('%Y')
date_from = '{}-01-01'.format(year)
date_to = '{}-12-31'.format(year)
date = fields.Date.from_string(date)
if self.date_range_type == 'year':
date_from = date.replace(month=1, day=1)
date_to = date.replace(month=12, day=31)
elif self.date_range_type == 'fiscalyear':
fiscalyear_last_month = self.env.user.company_id.fiscalyear_last_month if hasattr(self.env.user.company_id, 'fiscalyear_last_month') else 12
fiscalyear_last_day = self.env.user.company_id.fiscalyear_last_day if hasattr(self.env.user.company_id, 'fiscalyear_last_day') else 31
date_to = date.replace(month=fiscalyear_last_month, day=fiscalyear_last_day)
if date > date_to:
date_to = date_to.replace(year=date_to.year + 1)
date_from = date_to + timedelta(days=1)
date_from = date_from.replace(year=date_from.year - 1)
elif self.date_range_type == 'month':
date_from = date.replace(day=1)
date_to = date.replace(day=calendar.monthrange(date.year, date.month)[1])
elif self.date_range_type == 'week':
weekday = date.weekday()
date_from = date + timedelta(days=-weekday)
date_to = date + timedelta(days=6-weekday)
elif self.date_range_type == 'day':
date_from = date
date_to = date
else:
date_from = date.replace(month=1, day=1)
date_to = date.replace(month=12, day=31)
date_range = self.env['ir.sequence.date_range'].search([('sequence_id', '=', self.id), ('date_from', '>=', date), ('date_from', '<=', date_to)], order='date_from desc')
if date_range:
date_to = datetime.strptime(date_range.date_from, '%Y-%m-%d') + timedelta(days=-1)
Expand Down
1 change: 1 addition & 0 deletions openerp/addons/base/ir/ir_sequence_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<field name="prefix"/>
<field name="suffix"/>
<field name="use_date_range"/>
<field name="date_range_type" attrs="{'invisible': [('use_date_range', '=', False)]}"/>
</group>
<group>
<field name="padding"/>
Expand Down