diff --git a/web_date_warning/README.md b/web_date_warning/README.md new file mode 100644 index 00000000..14d7a2f3 --- /dev/null +++ b/web_date_warning/README.md @@ -0,0 +1,21 @@ +**Web Date Warning** + +Provides warnings for date field entries that fall outside of a user-specified range, with the goal of reducing data entry errors. +- For any date or date/time picker, set the threshold that should trigger the warning +- Allows you to set a threshold for past, future or both +- In addition to displaying warning text, will also display how many days in the past or future the selected date is +On a record with the date or date/time picket you wish to add the warning to, edit the form view and create an inherited view. Target the field name and include the following details: + + +`widget="date-warn"` if the field is a date picker + +`widget="datetime-warn"` if the field is a date/time picker + +`options="{'warn_future': 90}"` to set a threshold for future dates where 90 is the number of days + +And/or `options="{'warn_past': 30}"` to set a threshold for past dates where 30 is the number of days + + +Example: + +`` \ No newline at end of file diff --git a/web_date_warning/__init__.py b/web_date_warning/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/web_date_warning/__manifest__.py b/web_date_warning/__manifest__.py new file mode 100644 index 00000000..009c04e3 --- /dev/null +++ b/web_date_warning/__manifest__.py @@ -0,0 +1,41 @@ +{ + 'name': 'Web Date Warning', + 'version': '11.0.1.0.0', + 'author': 'Hibou Corp. ', + 'website': 'https://hibou.io/', + 'license': 'AGPL-3', + 'category': 'Tools', + 'complexity': 'expert', + 'description': """ +**Web Date Warning** + +Provides warnings for date field entries that fall outside of a user-specified range, with the goal of reducing data entry errors. +- For any date or date/time picker, set the threshold that should trigger the warning +- Allows you to set a threshold for past, future or both +- In addition to displaying warning text, will also display how many days in the past or future the selected date is +On a record with the date or date/time picket you wish to add the warning to, edit the form view and create an inherited view. Target the field name and include the following details: + + +`widget="date-warn"` if the field is a date picker + +`widget="datetime-warn"` if the field is a date/time picker + +`options="{'warn_future': 90}"` to set a threshold for future dates where 90 is the number of days + +And/or `options="{'warn_past': 30}"` to set a threshold for past dates where 30 is the number of days + + +Example: + +`` + """, + 'depends': [ + 'web', + ], + 'data': [ + 'views/web_views.xml', + ], + 'installable': True, + 'auto_install': False, + 'category': 'Hidden', +} diff --git a/web_date_warning/static/src/js/date_picker_warning.js b/web_date_warning/static/src/js/date_picker_warning.js new file mode 100644 index 00000000..71463e0e --- /dev/null +++ b/web_date_warning/static/src/js/date_picker_warning.js @@ -0,0 +1,43 @@ +odoo.define('web.datepicker.warn', function (require) { +"use strict"; + +var basicFields = require('web.basic_fields'); +var fieldRegistry = require('web.field_registry'); + +var _setValue = function(value) { + this._super.apply(this, value); + if (this.attrs.options.warn_future || this.options.warn_past) { + var now = moment(); + var val = moment(value); + var ms_diff = val - now; + var warning = false; + var days = Math.ceil(ms_diff / 1000 / (24 * 3600)); + if (days >= this.attrs.options.warn_future) { + warning = days + ' days in the future!'; + } else if (-days >= this.attrs.options.warn_past) { + warning = -days + ' days in the past!'; + } + if (!warning) { + this.$el.find('.o_date_warning').remove(); + } else { + if (this.$el.find('.o_date_warning').length) { + this.$el.find('.o_date_warning').text(warning); + } else { + this.$el.append('

' + warning + '

'); + } + } + } +} + +var FieldDateWarn = basicFields.FieldDate.extend({ + _setValue: _setValue, +}); + +var FieldDateTimeWarn = basicFields.FieldDateTime.extend({ + _setValue: _setValue, +}); + +fieldRegistry.add('date-warn', FieldDateWarn); +fieldRegistry.add('datetime-warn', FieldDateTimeWarn); + +}); diff --git a/web_date_warning/views/web_views.xml b/web_date_warning/views/web_views.xml new file mode 100644 index 00000000..eda7d563 --- /dev/null +++ b/web_date_warning/views/web_views.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file