Skip to content

Commit

Permalink
⚡ Message-template;
Browse files Browse the repository at this point in the history
  • Loading branch information
KolushovAlexandr authored and trojikman committed May 8, 2020
1 parent 8e06d84 commit ced8767
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 28 deletions.
1 change: 1 addition & 0 deletions pos_mail/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Credits
Contributors
------------
* `Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>`__
* `Anvar Kildebekov <https://it-projects.info/team/fedoranvar>`__

Sponsors
--------
Expand Down
1 change: 1 addition & 0 deletions pos_mail/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"views/views.xml",
],
"demo": [
"data/mail_body_template_demo.xml",
],
"qweb": [
"static/src/xml/templates.xml",
Expand Down
21 changes: 21 additions & 0 deletions pos_mail/data/mail_body_template_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Copyright 2019 Anvar Kildebekov <https://it-projects.info/team/fedoranvar>
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->
<odoo>
<record id="pos_mail_body_template_demo" model="mail.template">
<field name="name">pos_mail demo-template</field>
<field name="body_html"><![CDATA[
<p>Dear, ${partner.name}</p>
<p>Thanks for purchasing in our shop</p>
<p>Your order is: ${order.id}</p>
<p>Best wishes.</p>
]]></field>
</record>

<record id="mail_message_default" model="ir.config_parameter">
<field name="key">pos_mail.mail_message</field>
<field name="value" ref="pos_mail_body_template_demo"></field>
</record>
</odoo>
33 changes: 33 additions & 0 deletions pos_mail/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,36 @@ Installation
============

* `Install <https://odoo-development.readthedocs.io/en/latest/odoo/usage/install-module.html>`__ this module in a usual way

Usage
=====

* `Activate Developer Mode <https://odoo-development.readthedocs.io/en/latest/odoo/usage/debug-mode.html>`__
* Fill the fields
* For `properly-handled` signature: in html-editor enter ``'Code View'``-mode by clicking ``"</>"``-icon

Template example:

---

<p>Dear, ${partner.name}</p>

<p>Thanks for purchasing in our shop</p>

<p>Your order is: ${order.pos_reference}</p>

<p>Best wishes.</p>


Will be converted to

---

<p>Dear, Bob</p>

<p>Thanks for purchasing in our shop</p>

<p>Your order is: ${order.pos_reference}</p>

<p>Best wishes.</p>

76 changes: 69 additions & 7 deletions pos_mail/models/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,76 @@
# Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# Copyright 2019 Anvar Kildebekov <https://it-projects.info/team/fedoranvar>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import base64
from odoo import models, fields, api
import re
from odoo import models, fields, api, tools
try:
from odoo.addons.mail.models.mail_template import mako_template_env
except ImportError:
pass

from html.parser import HTMLParser

TAG_WHITELIST = [
'p',
'img',
]


class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.strict = False
self.convert_charrefs = True
self.fed = []

def get_data(self):
return ''.join(self.fed)

def handle_starttag(self, tag, attrs):
if tag in TAG_WHITELIST:
if tag == 'img':
self.fed.append("<%s " % tag)
attrs = dict(attrs)
if attrs.get('src'):
value = attrs['src']
pttrn = re.compile(r"b\'(.*?)'")
if pttrn.search(value):
repl = pttrn.search(value).group(1)
value = value.replace(pttrn.search(value).group(0), repl)
self.fed.append("src=\"%s" % value)
self.fed.append("\">")
else:
self.fed.append("<%s>" % tag)

def handle_endtag(self, tag):
if tag in TAG_WHITELIST:
self.fed.append("</%s>" % tag)

def handle_data(self, data):
self.fed.append(data)


def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()


class PosConfig(models.Model):
_inherit = 'pos.config'

send_receipt_by_mail = fields.Boolean('Mail a Receipt')

@api.model
def render_body_html(self, template, partner, order):
mako = mako_template_env.from_string(tools.ustr(template))
html = mako.render({'partner': partner,
'order': order})
html = strip_tags(html)
return html

@api.model
def send_receipt_via_mail(self, partner_id, body_from_ui, pos_reference):
base64_pdf = self.env['ir.actions.report']._run_wkhtmltopdf(
Expand All @@ -25,10 +86,11 @@ def send_receipt_via_mail(self, partner_id, body_from_ui, pos_reference):
'res_model': 'res.partner',
'res_id': partner_id,
})

partner = self.env['res.partner'].browse(partner_id)
mail_body = 'Receipt from ' + self.env.user.company_id.name + '\n'
mail_body += self.env["ir.config_parameter"].sudo().get_param("pos_mail.mail_message", default='')
order = self.env["pos.order"].search([('pos_reference', '=', pos_reference)])
mail_template_id = int(self.env["ir.config_parameter"].sudo().get_param("pos_mail.mail_message", default=''))
body_template = self.env["mail.template"].browse(mail_template_id).body_html
mail_body = self.render_body_html(body_template, partner, order)
# wizard model creation
composer = self.env['mail.compose.message'].create({
'partner_ids': [(6, False, [partner.id])],
Expand All @@ -44,21 +106,21 @@ def send_receipt_via_mail(self, partner_id, body_from_ui, pos_reference):
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

mail_message = fields.Text('Mail Message Template')
mail_message = fields.Many2one('mail.template', string='Mail Message Template', help='Keep empty to edit template manually')

@api.multi
def set_values(self):
super(ResConfigSettings, self).set_values()
config_parameters = self.env["ir.config_parameter"].sudo()
for record in self:
config_parameters.sudo().set_param("pos_mail.mail_message", record.mail_message)
config_parameters.sudo().set_param("pos_mail.mail_message", record.mail_message.id)

@api.multi
def get_values(self):
res = super(ResConfigSettings, self).get_values()
config_parameters = self.env["ir.config_parameter"].sudo()
mail_message = config_parameters.sudo().get_param("pos_mail.mail_message", default='')
res.update(
mail_message=mail_message,
mail_message=int(mail_message),
)
return res
33 changes: 20 additions & 13 deletions pos_mail/static/src/js/pos.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
* Copyright 2019 Anvar Kildebekov <https://it-projects.info/team/fedoranvar2>
* License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). */
odoo.define('pos_mail.pos', function (require) {

Expand All @@ -9,10 +10,16 @@ odoo.define('pos_mail.pos', function (require) {
var QWeb = core.qweb;
var _t = core._t;

screens.PaymentScreenWidget.include({
show: function() {
this._super();
this.gui.screen_instances.receipt.able_mail_button();
},
});

screens.ReceiptScreenWidget.include({
show: function() {
this._super();
this.able_mail_button();
},

renderElement: function() {
Expand All @@ -34,10 +41,13 @@ odoo.define('pos_mail.pos', function (require) {
return this.able_mail_button().addClass('disable');
},

mail_receipt_action: function() {
mail_receipt_action: function(new_client = false) {
var self = this;
var partner = this.pos.get_order().get_client();
if (partner) {
if (new_client) {
partner = new_client
}
if (partner && partner.email) {
return this.send_mail_receipt(partner.id).done(function(res){
console.log("Mail's sent");
}).fail(function(error){
Expand All @@ -48,6 +58,10 @@ odoo.define('pos_mail.pos', function (require) {
self.able_mail_button();
});
} else {
this.gui.show_popup('error',{
'title': _t('Customer has no email address'),
'body': _t('Please add customer email or select another one'),
});
this.set_mail_customer = true;
this.$el.zIndex(-6);
this.pos.gui.screen_instances.clientlist.show();
Expand All @@ -56,11 +70,11 @@ odoo.define('pos_mail.pos', function (require) {

send_mail_receipt: function(partner_id) {
var receipt = QWeb.render('PosMailTicket', this.get_receipt_render_env());
var order_name = this.pos.get_order().name;
var order = this.pos.get_order().name;
return rpc.query({
model: 'pos.config',
method: 'send_receipt_via_mail',
args: [partner_id, receipt, order_name],
args: [partner_id, receipt, order],
}, {
shadow: true,
});
Expand Down Expand Up @@ -90,17 +104,10 @@ odoo.define('pos_mail.pos', function (require) {
this.pos.config.send_receipt_by_mail &&
this.gui.current_screen.set_mail_customer) {

if (!this.new_client.email) {
this.gui.show_popup('error',{
'title': _t('Customer has no email address'),
'body': _t('Please add customer email or select another one'),
});
return;
}
var receipt_screen = this.gui.screen_instances.receipt;
receipt_screen.send_mail_receipt(this.new_client.id);
receipt_screen.$el.zIndex(0);
this.$el.zIndex(-1);
receipt_screen.mail_receipt_action(this.new_client);
} else {
this._super();
}
Expand Down
15 changes: 7 additions & 8 deletions pos_mail/views/assets.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). -->
<odoo>

<!--
<template id="assets" inherit_id="point_of_sale.assets">
<xpath expr="." position="inside">
<script type="text/javascript" src="/pos_mail/static/src/js/pos.js"></script>
<link rel="stylesheet" href="/pos_mail/static/src/css/pos.css"/>
</xpath>
</template>
<!-- <template id="demo_assets_backend" inherit_id="web.assets_backend">-->
<!-- <xpath expr="." position="inside">-->
<!-- <script type="text/javascript" src="/pos_mail/static/src/js/test_pos_mail.js"></script>-->
<!-- </xpath>-->
<!-- </template>-->

<template id="demo_assets_backend" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/pos_mail/static/src/js/test_pos_mail.js"></script>
</xpath>
</template>
-->
</odoo>

0 comments on commit ced8767

Please sign in to comment.