Skip to content

Commit

Permalink
[ADD] sale_channel_notification
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudelva committed Jul 8, 2024
1 parent 892de57 commit e29bca3
Show file tree
Hide file tree
Showing 15 changed files with 714 additions and 0 deletions.
76 changes: 76 additions & 0 deletions sale_channel_notification/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
=========================
Sale Channel Notification
=========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:4f88f5cef4adddfd9f7c2262daadde531bd4d5e907be63fb53e4b8d8b0838bf9
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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%2Fsale--channel-lightgray.png?logo=github
:target: https://github.com/OCA/sale-channel/tree/16.0/sale_channel_notification
:alt: OCA/sale-channel
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-channel-16-0/sale-channel-16-0-sale_channel_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/sale-channel&target_branch=16.0
:alt: Try me on Runboat

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

Simple module that adds the posibility to link an email template to an action.

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-channel/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/sale-channel/issues/new?body=module:%20sale_channel_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
~~~~~~~

* Akretion

Contributors
~~~~~~~~~~~~

* Mathieu Delva <[email protected]>

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/sale-channel <https://github.com/OCA/sale-channel/tree/16.0/sale_channel_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 sale_channel_notification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
27 changes: 27 additions & 0 deletions sale_channel_notification/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Mathieu Delva <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Sale Channel Notification",
"summary": "",
"version": "16.0.1.0.1",
"category": "Sale Channel",
"website": "https://github.com/OCA/sale-channel",
"author": "Akretion,Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"external_dependencies": {
"python": [],
"bin": [],
},
"depends": [
"sale_channel",
],
"data": [
"security/ir.model.access.csv",
"views/sale_channel_notification_view.xml",
],
"demo": [],
}
4 changes: 4 additions & 0 deletions sale_channel_notification/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import sale_channel_notification
from . import sale_channel
from . import sale_order
from . import stock_picking
35 changes: 35 additions & 0 deletions sale_channel_notification/models/sale_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Mathieu Delva <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo.tools.translate import _


class SaleChannel(models.Model):
_inherit = "sale.channel"

notification_ids = fields.One2many(
"sale.channel.notification",
"sale_channel_id",
"Notification",
help="Send mail for predefined events",
)

def _send_notification(self, notification, record):
self.ensure_one()
record.ensure_one()
notifs = self.env["sale.channel.notification"].search(
[
("sale_channel_id", "=", self.id),
("notification_type", "=", notification),
]
)
description = _("Notify %s for %s,%s") % (
notification,
record._name,
record.id,
)
for notif in notifs:
notif.send(record.id)
return True
67 changes: 67 additions & 0 deletions sale_channel_notification/models/sale_channel_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Mathieu Delva <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo.tools.translate import _


class SaleChannelNotification(models.Model):
_name = "sale.channel.notification"

sale_channel_id = fields.Many2one("sale.channel")
notification_type = fields.Selection(
selection="_selection_notification_type",
string="Notification Type",
required=True,
)
model_id = fields.Many2one("ir.model", "Model", required=True, ondelete="cascade")
template_id = fields.Many2one("mail.template", "Mail Template", required=True)


def _selection_notification_type(self):
notifications = self._get_all_notification()
return [(key, notifications[key]["name"]) for key in notifications]

def _get_all_notification(self):
return {
"sale_confirmation": {
"name": _("Sale Confirmation"),
"model": "sale.order",
},
"invoice_open": {
"name": _("Invoice Validated"),
"model": "account.move",
},
"picking_shipped": {
"name": _("Picking Shipped"),
"model": "stock.picking",
}
}


@api.onchange("notification_type")
def on_notification_type_change(self):
self.ensure_one()
notifications = self._get_all_notification()
if self.notification_type:
model = notifications[self.notification_type].get("model")
if model:
self.model_id = self.env["ir.model"].search([("model", "=", model)])
return {"domain": {"model_id": [("id", "=", self.model_id.id)]}}
else:
return {"domain": {"model_id": []}}

def send(self, record_id):
self.ensure_one()
return (
self.sudo()
.template_id.with_context(**self._get_template_context())
.send_mail(record_id)
)

def _get_template_context(self):
return {
"notification_type": self.notification_type,
"sale_channel": self.sale_channel_id,
}
26 changes: 26 additions & 0 deletions sale_channel_notification/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Mathieu Delva <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from odoo.tools.translate import _


class SaleOrder(models.Model):
_inherit = "sale.order"


def action_confirm(self):
if 'send_email' in self._context and self._context.get('send_email'):
self.env.context = dict(self.env.context)
self.env.context.pop('send_email')
res = super().action_confirm()
for record in self:
if record.state != "draft" and record.sale_channel_id:
record.sale_channel_id._send_notification(
"sale_confirmation", record
)
return res

def _get_confirmation_template(self):
return False
17 changes: 17 additions & 0 deletions sale_channel_notification/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Mathieu Delva <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo.tools.translate import _


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

@api.onchange("carrier_id")
def onchange_carrier_id(self):
if self.carrier_id and self.sale_id.sale_channel_id:
self.sale_id.sale_channel_id._send_notification(
"picking_shipped", self.env["stock.picking"].browse(self.ids)
)
1 change: 1 addition & 0 deletions sale_channel_notification/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Mathieu Delva <[email protected]>
1 change: 1 addition & 0 deletions sale_channel_notification/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simple module that adds the posibility to link an email template to an action.
2 changes: 2 additions & 0 deletions sale_channel_notification/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_sale_channel_notification,sale_channel_notification,model_sale_channel_notification,base.group_user,1,1,1,1
Loading

0 comments on commit e29bca3

Please sign in to comment.