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

[16.0][ADD] stock_picking_status_notification: Add new module to notify user after change picking states #1680

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/stock_picking_status_notification/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
135 changes: 135 additions & 0 deletions stock_picking_status_notification/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
==========================
Notify Users about Picking
==========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:584e6579cf1f7315aeb3162a9b846dc43e8cb3d7a0f432b774fb822db18f43e1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/stock-logistics-workflow/tree/16.0/stock_picking_status_notification
:alt: OCA/stock-logistics-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/stock-logistics-workflow-16-0/stock-logistics-workflow-16-0-stock_picking_status_notification
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to send notifications to selected backend users when
the state of stock picking changes . For this configurable notification
rules are used. Rules are checked using flexible patterns. The first one
rule matching the pattern is triggered.

By default notifications are sent when a picking is set to the "Waiting"
or "Ready" status. However you can enable notifications for other
pickings too.

**Table of contents**

.. contents::
:local:

Use Cases / Context
===================

Imagine you have a warehouse that is serving a retail store and online
shop.

Each time a sale is done in the retail shop you would like to process
that order immediately. For this you need to notify selected employees
about such order so that they could switch to it **asap**.

Configuration
=============

To configure notification rules:

- Go to
``"Inventory -> Configuration -> Warehouse Management -> New Picking Notifications"``
and create a new notification
- Put the following data in the notification form:

- ``Priority``. Defines the order in which notification rules are
checked. The lower is the number the higher is the priority.
Default is ``10``
- ``Operation Types``, required. Specifies the operation types for
which this notification will be triggered
- ``Notified Users``, required. List of users to be notified
- ``Source Document Pattern``, optional. Notification will be sent
if source document name matches the specified regex pattern. Leave
blank to match any source
- ``Custom Message``, optional. Custom message to be used instead of
the default one. Leave blank to use the default message
- ``Notification Sound``, optional. Custom sound to be used with
notification. Leave blank to use notification without sound
- ``Draft`` - optional. If enabled, notifications will be sent when
a picking is in the "Draft" state
- ``Waiting Another Operation`` - optional. If enabled,
notifications will be sent when a picking is in the "Waiting
Another Operation" state
- ``Waiting`` - optional. If enabled, notifications will be sent
when a picking is in the "Waiting" state
- ``Ready`` - optional. If enabled, notifications will be sent when
a picking is in the "Ready" state.
- ``Done`` - optional. If enabled, notifications will be sent when a
picking is in the "Done" state.
- ``Cancel`` - optional. If enabled, notifications will be sent when
a picking is in the "Cancelled" state

Usage
=====

When a new picking is created and this picking matches some notification
rule a notification will be shown for selected users:

- Document number in the notification title
- Message text in the notification body
- "Open document" button. Click it to open the related document

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-workflow/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20stock_picking_status_notification%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Cetmix

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

This module is part of the `OCA/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/16.0/stock_picking_status_notification>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions stock_picking_status_notification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
21 changes: 21 additions & 0 deletions stock_picking_status_notification/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2024 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Notify Users about Picking",
"version": "16.0.1.0.0",
"category": "Inventory/Inventory",
"summary": "Notify selected internal users of changes in picking states",
"depends": ["stock", "web_notify"],
"website": "https://github.com/OCA/stock-logistics-workflow",
"author": "Cetmix, Odoo Community Association (OCA)",
"installable": True,
"data": [
"security/ir.model.access.csv",
"views/stock_picking_notification_template_view.xml",
"views/menuitems.xml",
],
"demo": [
"demo/stock_picking_notification_template_demo.xml",
],
"license": "AGPL-3",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record
id="delivery_picking_notification_template1"
model="stock.picking.notification.template"
>
<field name="picking_type_id" ref="stock.picking_type_out" />
<field name="user_ids" eval="[(6, 0, [ref('base.user_admin')])]" />
<field name="allow_notify_draft">True</field>
<field name="allow_notify_waiting">True</field>
<field name="allow_notify_done">True</field>
<field name="allow_notify_cancel">True</field>
<field
name="notification_sound_file"
type="base64"
file="mail/static/src/audio/ting.mp3"
/>
<field name="filename">ting.mp3</field>
</record>

</odoo>
2 changes: 2 additions & 0 deletions stock_picking_status_notification/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import stock_picking
from . import stock_picking_notification_template
53 changes: 53 additions & 0 deletions stock_picking_status_notification/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (C) 2024 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, registry


class StockPicking(models.Model):
_inherit = "stock.picking"

to_web_notify = fields.Boolean(
default=False,
help="Technical field for storing the “to notify” attribute, "
"which is used to retrieve all records for this attribute "
"and to send a notification.",
)

def _write(self, vals):
# We use the _write method instead of write because _write is a low-level implementation
# that bypasses certain restrictions related to computed fields. In this case, it is
# crucial to ensure that notifications are triggered correctly when the state of the
# picking changes, even if this field (state) was computed by dependent fields
if "state" in vals:
vals = dict(vals)
vals.setdefault("to_web_notify", True)

res = super()._write(vals)

dbname = self.env.cr.dbname
context = self.env.context
uid = self.env.uid

# only the latest state needs to be sent
@self.env.cr.postcommit.add
def trigger_picking_notification():
db_registry = registry(dbname)
with db_registry.cursor() as cr:
env = api.Environment(cr, uid, context)
to_notify = env["stock.picking"].search([("to_web_notify", "=", True)])
if to_notify:
to_notify.sudo()._trigger_picking_notification()
to_notify.to_web_notify = False

return res

def _trigger_picking_notification(self):
"""
Check notification rules and trigger notifications if conditions are met.
"""
notify_template_obj = self.env["stock.picking.notification.template"]
for picking in self:
template = notify_template_obj._get_matching_template(picking)
if template:
template._notify_picking_users(picking)
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Copyright (C) 2024 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import re

from odoo import _, fields, models


class StockPickingNotificationTemplate(models.Model):
_name = "stock.picking.notification.template"
_description = "Picking Notification Template"
_order = "sequence, id"
_rec_name = "picking_type_id"

sequence = fields.Integer(
default=10,
string="Priority",
)
active = fields.Boolean(
default=True,
)
picking_type_id = fields.Many2one(
comodel_name="stock.picking.type",
string="Operation Type",
required=True,
help="Operation types for which the notification will be triggered",
)
user_ids = fields.Many2many(
string="Notified Users",
comodel_name="res.users",
relation="picking_notify_template_user_rel",
column1="picking_notify_template_id",
column2="user_id",
required=True,
)
source_document_regex = fields.Char(
string="Source Document Pattern",
help="Notification will be sent if source document name matches "
"the specified regex pattern. Leave blank to match any source",
)
allow_notify_draft = fields.Boolean(
string="Allow Notify DRAFT",
default=False,
)
allow_notify_waiting = fields.Boolean(
string="Allow Notify WAITING ANOTHER OPERATION",
default=False,
)
allow_notify_confirmed = fields.Boolean(
string="Allow notify WAITING",
default=True,
)
allow_notify_assigned = fields.Boolean(
string="Allow notify READY",
default=True,
)
allow_notify_done = fields.Boolean(
string="Allow notify DONE",
default=False,
)
allow_notify_cancel = fields.Boolean(
string="Allow notify CANCEL",
default=False,
)
message = fields.Text(
string="Custom Message",
help="Custom message to be used instead of the default one. "
"Leave blank to use the default message",
)
notification_sound_file = fields.Binary(
string="Notification Sound",
attachment=True,
help="Select the sound file that will be used for notifications. This setting "
"allows you to customize the notification sound played when certain actions "
"occur, such as when a stock picking status is updated.",
)
filename = fields.Char()

def _get_matching_template(self, picking):
"""
Return the first matching notification template for the given picking.
"""
templates = self.search(
[
("picking_type_id", "=", picking.picking_type_id.id),
(f"allow_notify_{picking.state}", "=", True),
],
order="sequence ASC",
)
for template in templates:
if template._matches_picking(picking):
return template
return None

Check warning on line 92 in stock_picking_status_notification/models/stock_picking_notification_template.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_status_notification/models/stock_picking_notification_template.py#L92

Added line #L92 was not covered by tests

def _matches_picking(self, picking):
"""
Check if the picking matches this notification template.
"""
if self.source_document_regex and not re.match(
self.source_document_regex, picking.origin or ""
):
return False

return True

def _get_sound_path(self):
"""
Return sound notification path if sound is specified
"""
self.ensure_one()
if self.notification_sound_file:
attachment = self.env["ir.attachment"].search(

Check warning on line 111 in stock_picking_status_notification/models/stock_picking_notification_template.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_status_notification/models/stock_picking_notification_template.py#L111

Added line #L111 was not covered by tests
[
("res_model", "=", self._name),
("res_field", "=", "notification_sound_file"),
("res_id", "=", self.id),
]
)
attachment.generate_access_token()
return (

Check warning on line 119 in stock_picking_status_notification/models/stock_picking_notification_template.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_status_notification/models/stock_picking_notification_template.py#L118-L119

Added lines #L118 - L119 were not covered by tests
f"/web/content/{attachment.id}?access_token={attachment.access_token}"
)

def _notify_picking_users(self, picking):
"""
Send notifications to the users about the picking.
"""
self.ensure_one()
state = dict(picking._fields.get("state").selection).get(picking.state)
return self.user_ids.sudo().notify_info(
message=self.message
or _("Picking changed status to '%(state)s'.", state=state),
title=picking.name,
sticky=True,
action={
"type": "ir.actions.act_window",
"name": picking.name,
"res_model": "stock.picking",
"res_id": picking.id,
"view_mode": "form",
"view_type": "form",
"target": "current",
},
sound=self._get_sound_path(),
)
Loading
Loading