diff --git a/date_range/README.rst b/date_range/README.rst new file mode 100644 index 0000000..b78b7f3 --- /dev/null +++ b/date_range/README.rst @@ -0,0 +1,108 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +========== +Date Range +========== + +This module lets you define global date ranges that can be used to filter +your values in tree views. + +Usage +===== + +To configure this module, you need to: + +* Go to Settings > Technical > Date ranges > Date Range Types where + you can create types of date ranges. + + .. figure:: static/description/date_range_type_create.png + :scale: 80 % + :alt: Create a type of date range + +* Go to Settings > Technical > Date ranges > Date Ranges where + you can create date ranges. + + .. figure:: static/description/date_range_create.png + :scale: 80 % + :alt: Date range creation + + It's also possible to launch a wizard from the 'Generate Date Ranges' menu. + + .. figure:: static/description/date_range_wizard.png + :scale: 80 % + :alt: Date range wizard + + The wizard is useful to generate recurring periods. + + .. figure:: static/description/date_range_wizard_result.png + :scale: 80 % + :alt: Date range wizard result + +* Your date ranges are now available in the search filter for any date or datetime fields + + Date range types are proposed as a filter operator + + .. figure:: static/description/date_range_type_as_filter.png + :scale: 80 % + :alt: Date range type available as filter operator + + Once a type is selected, date ranges of this type are porposed as a filter value + + .. figure:: static/description/date_range_as_filter.png + :scale: 80 % + :alt: Date range as filter value + + And the dates specified into the date range are used to filter your result. + + .. figure:: static/description/date_range_as_filter_result.png + :scale: 80 % + :alt: Date range as filter result + + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/149/9.0 + + +Known issues / Roadmap +====================== + +* The addon use the daterange method from postgres. This method is supported as of postgresql 9.2 + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Laurent Mignon + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/date_range/__init__.py b/date_range/__init__.py new file mode 100644 index 0000000..dcac9c0 --- /dev/null +++ b/date_range/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# © 2016 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models +from . import wizard diff --git a/date_range/__openerp__.py b/date_range/__openerp__.py new file mode 100644 index 0000000..02c75f5 --- /dev/null +++ b/date_range/__openerp__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# © 2016 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + "name": "Date Range", + "summary": "Manage all kind of date range", + "version": "9.0.1.0.0", + "category": "Uncategorized", + "website": "https://odoo-community.org/", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "web", + ], + "data": [ + "security/ir.model.access.csv", + "security/date_range_security.xml", + "views/assets.xml", + "views/date_range_view.xml", + "wizard/date_range_generator.xml", + ], + "qweb": [ + "static/src/xml/date_range.xml", + ] +} diff --git a/date_range/i18n/.empty b/date_range/i18n/.empty new file mode 100644 index 0000000..e69de29 diff --git a/date_range/models/__init__.py b/date_range/models/__init__.py new file mode 100644 index 0000000..feae177 --- /dev/null +++ b/date_range/models/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# © 2016 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import date_range_type +from . import date_range diff --git a/date_range/models/date_range.py b/date_range/models/date_range.py new file mode 100644 index 0000000..91111b4 --- /dev/null +++ b/date_range/models/date_range.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +# © 2016 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import api, fields, models +from openerp.tools.translate import _ +from openerp.exceptions import ValidationError + + +class DateRange(models.Model): + _name = "date.range" + _order = "type_name,date_start" + + @api.model + def _default_company(self): + return self.env['res.company']._company_default_get('date.range') + + name = fields.Char(required=True, translate=True) + date_start = fields.Date(string='Start date', required=True) + date_end = fields.Date(string='End date', required=True) + type_id = fields.Many2one( + comodel_name='date.range.type', string='Type', select=1, required=True) + type_name = fields.Char( + string='Type', related='type_id.name', readonly=True, store=True) + company_id = fields.Many2one( + comodel_name='res.company', string='Company', select=1, + default=_default_company) + active = fields.Boolean( + help="The active field allows you to hide the date range without " + "removing it.", default=True) + + _sql_constraints = [ + ('date_range_uniq', 'unique (name,type_id, company_id)', + 'A date range must be unique per company !')] + + @api.constrains('type_id', 'date_start', 'date_end', 'company_id') + def _validate_range(self): + for this in self: + start = fields.Date.from_string(this.date_start) + end = fields.Date.from_string(this.date_end) + if start >= end: + raise ValidationError( + _("%s is not a valid range (%s >= %s)") % ( + this.name, this.date_start, this.date_end)) + if this.type_id.allow_overlap: + continue + # here we use a plain SQL query to benefit of the daterange + # function available in PostgresSQL + # (http://www.postgresql.org/docs/current/static/rangetypes.html) + SQL = """ + SELECT + id + FROM + date_range dt + WHERE + DATERANGE(dt.date_start, dt.date_end, '[]') && + DATERANGE(%s::date, %s::date, '[]') + AND dt.id != %s + AND dt.active + AND dt.company_id = %s + AND dt.type_id=%s;""" + self.env.cr.execute(SQL, (this.date_start, + this.date_end, + this.id, + this.company_id.id or None, + this.type_id.id)) + res = self.env.cr.fetchall() + if res: + dt = self.browse(res[0][0]) + raise ValidationError( + _("%s overlaps %s") % (this.name, dt.name)) + + @api.multi + def get_domain(self, field_name): + self.ensure_one() + return [(field_name, '>=', self.date_start), + (field_name, '<=', self.date_end)] diff --git a/date_range/models/date_range_type.py b/date_range/models/date_range_type.py new file mode 100644 index 0000000..955e6e9 --- /dev/null +++ b/date_range/models/date_range_type.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# © 2016 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import api, fields, models + + +class DateRangeType(models.Model): + _name = "date.range.type" + + @api.model + def _default_company(self): + return self.env['res.company']._company_default_get('date.range') + + name = fields.Char(required=True, translate=True) + allow_overlap = fields.Boolean( + help="If sets date range of same type must not overlap.", + default=False) + active = fields.Boolean( + help="The active field allows you to hide the date range without " + "removing it.", default=True) + company_id = fields.Many2one( + comodel_name='res.company', string='Company', select=1, + default=_default_company) + + _sql_constraints = [ + ('date_range_type_uniq', 'unique (name,company_id)', + 'A date range type must be unique per company !')] diff --git a/date_range/security/date_range_security.xml b/date_range/security/date_range_security.xml new file mode 100644 index 0000000..20bea9b --- /dev/null +++ b/date_range/security/date_range_security.xml @@ -0,0 +1,19 @@ + + + + + Date Range Type multi-company + + + ['|',('company_id','=',user.company_id.id),('company_id','=',False)] + + + + Date Range multi-company + + + ['|',('company_id','=',user.company_id.id),('company_id','=',False)] + + + + diff --git a/date_range/security/ir.model.access.csv b/date_range/security/ir.model.access.csv new file mode 100644 index 0000000..2def3c2 --- /dev/null +++ b/date_range/security/ir.model.access.csv @@ -0,0 +1,5 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_date_range_date_range,date_range.date_range,model_date_range,base.group_user,1,0,0,0 +access_date_range_date_range_type,date_range.date_range_type,model_date_range_type,base.group_user,1,0,0,0 +access_date_range_date_range_config,date_range.date_range.config,model_date_range,base.group_configuration,1,1,1,1 +access_date_range_date_range_type_config,date_range.date_range_type.config,model_date_range_type,base.group_configuration,1,1,1,1 \ No newline at end of file diff --git a/date_range/static/description/date_range_as_filter.png b/date_range/static/description/date_range_as_filter.png new file mode 100644 index 0000000..4f61f48 Binary files /dev/null and b/date_range/static/description/date_range_as_filter.png differ diff --git a/date_range/static/description/date_range_as_filter_result.png b/date_range/static/description/date_range_as_filter_result.png new file mode 100644 index 0000000..5ba45b7 Binary files /dev/null and b/date_range/static/description/date_range_as_filter_result.png differ diff --git a/date_range/static/description/date_range_create.png b/date_range/static/description/date_range_create.png new file mode 100644 index 0000000..b7def10 Binary files /dev/null and b/date_range/static/description/date_range_create.png differ diff --git a/date_range/static/description/date_range_type_as_filter.png b/date_range/static/description/date_range_type_as_filter.png new file mode 100644 index 0000000..35148ff Binary files /dev/null and b/date_range/static/description/date_range_type_as_filter.png differ diff --git a/date_range/static/description/date_range_type_create.png b/date_range/static/description/date_range_type_create.png new file mode 100644 index 0000000..4c94af4 Binary files /dev/null and b/date_range/static/description/date_range_type_create.png differ diff --git a/date_range/static/description/date_range_wizard.png b/date_range/static/description/date_range_wizard.png new file mode 100644 index 0000000..a1537ad Binary files /dev/null and b/date_range/static/description/date_range_wizard.png differ diff --git a/date_range/static/description/date_range_wizard_result.png b/date_range/static/description/date_range_wizard_result.png new file mode 100644 index 0000000..4004299 Binary files /dev/null and b/date_range/static/description/date_range_wizard_result.png differ diff --git a/date_range/static/description/icon.png b/date_range/static/description/icon.png new file mode 100644 index 0000000..3a0328b Binary files /dev/null and b/date_range/static/description/icon.png differ diff --git a/date_range/static/description/icon.svg b/date_range/static/description/icon.svg new file mode 100644 index 0000000..a7a26d0 --- /dev/null +++ b/date_range/static/description/icon.svg @@ -0,0 +1,79 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/date_range/static/src/js/date_range.js b/date_range/static/src/js/date_range.js new file mode 100644 index 0000000..5535e6a --- /dev/null +++ b/date_range/static/src/js/date_range.js @@ -0,0 +1,117 @@ +/* © 2016 ACSONE SA/NV () + * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ +odoo.define('date_range.search_filters', function (require) { +"use strict"; + +var core = require('web.core'); +var data = require('web.data'); +var filters = require('web.search_filters'); +var Model = require('web.Model'); +var framework = require('web.framework'); + +var _t = core._t; +var _lt = core._lt; +filters.ExtendedSearchProposition.include({ + select_field: function(field) { + this._super.apply(this, arguments); + this.is_date_range_selected = false; + this.is_date = field.type == 'date' || field.type == 'datetime'; + this.$value = this.$el.find('.searchview_extended_prop_value, .o_searchview_extended_prop_value'); + if (this.is_date){ + var ds = new data.DataSetSearch(this, 'date.range.type', this.context, [[1, '=', 1]]); + ds.read_slice(['name'], {}).done(this.proxy('add_date_range_types_operator')); + } + }, + + add_date_range_types_operator: function(date_range_types){ + var self = this; + _.each(date_range_types, function(drt) { + $('