From e8dc76ee0bc1db9dbf33a2101d40805ee8a3f442 Mon Sep 17 00:00:00 2001 From: William Braeckman Date: Tue, 19 Oct 2021 15:35:48 +0000 Subject: [PATCH] [IMP] lunch: add possibility to notify users on lunch arrival There was no way for the lunch manager to notify users that their meal had arrived prior to this commit besides manually sending a message to the concerned users. There now is a dedicated button after the order has been confirmed by the lunch manager to send notifications. TaskId-2587476 closes odoo/odoo#78649 Signed-off-by: Kevin Baptiste --- addons/lunch/data/lunch_data.xml | 9 ++++++++ addons/lunch/models/lunch_order.py | 27 ++++++++++++++++++++++ addons/lunch/models/res_company.py | 3 +++ addons/lunch/models/res_config_settings.py | 1 + addons/lunch/views/lunch_orders_views.xml | 6 +++++ addons/lunch/views/res_config_settings.xml | 16 +++++++++++++ 6 files changed, 62 insertions(+) diff --git a/addons/lunch/data/lunch_data.xml b/addons/lunch/data/lunch_data.xml index 5e9d0f55e41..4a7c4284977 100644 --- a/addons/lunch/data/lunch_data.xml +++ b/addons/lunch/data/lunch_data.xml @@ -53,6 +53,15 @@ code records.action_cancel() + + + Lunch: Send notifications + + + list + code + records.action_notify() + diff --git a/addons/lunch/models/lunch_order.py b/addons/lunch/models/lunch_order.py index 794b00803c0..d2ea25a8723 100644 --- a/addons/lunch/models/lunch_order.py +++ b/addons/lunch/models/lunch_order.py @@ -35,6 +35,7 @@ class LunchOrder(models.Model): ('confirmed', 'Received'), ('cancelled', 'Cancelled')], 'Status', readonly=True, index=True, default='new') + notified = fields.Boolean(default=False) company_id = fields.Many2one('res.company', default=lambda self: self.env.company.id) currency_id = fields.Many2one(related='company_id.currency_id', store=True) quantity = fields.Float('Quantity', required=True, default=1) @@ -229,3 +230,29 @@ class LunchOrder(models.Model): def action_reset(self): self.write({'state': 'ordered'}) + + def action_notify(self): + self -= self.filtered('notified') + if not self: + return + notified_users = set() + # (company, lang): (subject, body) + translate_cache = dict() + for order in self: + user = order.user_id + if user in notified_users: + continue + _key = (order.company_id, user.lang) + if _key not in translate_cache: + context = {'lang': user.lang} + translate_cache[_key] = (_('Lunch notification'), order.company_id.with_context(lang=user.lang).lunch_notify_message) + del context + subject, body = translate_cache[_key] + user.partner_id.message_notify( + subject=subject, + body=body, + partner_ids=user.partner_id.ids, + email_layout_xmlid='mail.mail_notification_light', + ) + notified_users.add(user) + self.write({'notified': True}) diff --git a/addons/lunch/models/res_company.py b/addons/lunch/models/res_company.py index 95d91d09596..fe9df868291 100644 --- a/addons/lunch/models/res_company.py +++ b/addons/lunch/models/res_company.py @@ -8,3 +8,6 @@ class Company(models.Model): _inherit = 'res.company' lunch_minimum_threshold = fields.Float() + lunch_notify_message = fields.Html( + default="""Your lunch has been delivered. +Enjoy your meal!""", translate=True) diff --git a/addons/lunch/models/res_config_settings.py b/addons/lunch/models/res_config_settings.py index faa8f256f49..8c65ad579e4 100644 --- a/addons/lunch/models/res_config_settings.py +++ b/addons/lunch/models/res_config_settings.py @@ -9,3 +9,4 @@ class ResConfigSettings(models.TransientModel): currency_id = fields.Many2one('res.currency', related='company_id.currency_id') company_lunch_minimum_threshold = fields.Float(string="Maximum Allowed Overdraft", readonly=False, related='company_id.lunch_minimum_threshold') + company_lunch_notify_message = fields.Html(string="Lunch notification message", readonly=False, related="company_id.lunch_notify_message") diff --git a/addons/lunch/views/lunch_orders_views.xml b/addons/lunch/views/lunch_orders_views.xml index 7ee821f4c44..f88744e6e2f 100644 --- a/addons/lunch/views/lunch_orders_views.xml +++ b/addons/lunch/views/lunch_orders_views.xml @@ -46,10 +46,12 @@ +