[IMP] snailmail_account: Deal with invalid addresses

- Raise UserError when sending an invoice with an invalid address
- Notify when sending letters in batch mode with invalid addresses
This commit is contained in:
Antoine Prieels (ANP)
2018-11-22 15:38:42 +00:00
committed by Rémi Rahir
parent f68df47ddf
commit 8573b4647b
5 changed files with 72 additions and 12 deletions
+1
View File
@@ -11,6 +11,7 @@ Allows users to send invoices by post
'data': [
'views/res_config_settings_views.xml',
'views/snailmail_views.xml',
'views/assets.xml',
'wizard/account_invoice_send_views.xml',
],
'auto_install': True,
@@ -0,0 +1,35 @@
odoo.define('snailmail_account.NotificationManager', function (require) {
"use strict";
var AbstractService = require('web.AbstractService');
var core = require("web.core");
var SnailmailNotificationManager = AbstractService.extend({
dependencies: ['bus_service'],
/**
* @override
*/
start: function () {
this._super.apply(this, arguments);
this.call('bus_service', 'onNotification', this, this._onNotification);
},
_onNotification: function (notifs) {
var self = this;
_.each(notifs, function (notif) {
var model = notif[0][1];
var type = notif[1].type;
if (model === 'res.partner' && type === 'snailmail_invalid_address') {
self.do_warn(notif[1].title, notif[1].message);
}
});
}
});
core.serviceRegistry.add('snailmail_notification_service', SnailmailNotificationManager);
return SnailmailNotificationManager;
});
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="assets_backend" name="iap assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/snailmail_account/static/src/js/snailmail_account_notification_manager.js"></script>
</xpath>
</template>
</odoo>
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo import api, fields, models, _
from odoo.exceptions import UserError
class AccountInvoiceSend(models.TransientModel):
@@ -17,8 +18,15 @@ class AccountInvoiceSend(models.TransientModel):
@api.depends('snailmail_cost', 'letter_ids')
def _compute_invalid_addresses(self):
for wizard in self:
wizard.invalid_addresses = len(wizard.letter_ids) - wizard.snailmail_cost
for wizard in self:
count_invalid_addresses = 0
required_fields = ['street', 'city', 'zip', 'country_id']
for letter in wizard.letter_ids:
for field in required_fields:
if not letter.partner_id[field]:
count_invalid_addresses += 1
break
wizard.invalid_addresses = count_invalid_addresses
@api.multi
@api.onchange('invoice_ids')
@@ -77,6 +85,18 @@ class AccountInvoiceSend(models.TransientModel):
@api.multi
def send_and_print_action(self):
if self.snailmail_is_letter and self.invalid_addresses:
if self.composition_mode == "mass_mail":
self.env['bus.bus'].sendone(
(self._cr.dbname, 'res.partner', self.env.user.partner_id.id),
{'type': 'snailmail_invalid_address', 'title': _("Invalid Addresses"),
'message': _("%s of the selected invoice(s) had an invalid address and were not sent") % self.invalid_addresses}
)
else:
raise UserError(_(
'''Cannot send by post to an incomplete address.
Please update the customer's address or uncheck \'Send by Post\'.'''
))
res = super(AccountInvoiceSend, self).send_and_print_action()
if self.snailmail_is_letter:
self.snailmail_print_action()
@@ -28,27 +28,23 @@
<span attrs="{'invisible': [('invalid_addresses', '!=', 0)]}">
<div class="text-right text-muted d-inline-block" name="address">
<span> to: </span>
<field name="partner_id" readonly="1" force_save="1" context="{'show_address': not invalid_addresses, 'address_inline': 1}" options="{'always_reload': True, 'no_quick_create': True}"/>
<field name="partner_id" readonly="1" force_save="1" context="{'show_address': 1, 'address_inline': 1}" options="{'always_reload': True, 'no_quick_create': True}"/>
</div>
</span>
<span attrs="{'invisible': [('invalid_addresses', '=', 0)]}">
<div class="text-right d-inline-block" attrs="{'invisible': [('composition_mode', '=', 'mass_mail')]}">
<span class="text-muted">
Incomplete address for
<field name="partner_id" readonly="1" force_save="1" context="{'show_address': not invalid_addresses, 'address_inline': 1}" options="{'always_reload': True, 'no_quick_create': True}"/>
<span class="text-danger">
Incomplete customer address
</span>
</div>
</span>
</span>
<span attrs="{'invisible': [('composition_mode', '!=', 'mass_mail')]}">
<span attrs="{'invisible': [('snailmail_cost', '=', 0)]}">
<span class="text-muted" attrs="{'invisible': [('invalid_addresses', '=', 0)]}">
Only <field name="snailmail_cost" readonly="1" options="{'digits':[0,0]}"/> of the selected invoices have a valid address.
<span class="text-danger" attrs="{'invisible': [('invalid_addresses', '=', 0)]}">
<field name="invalid_addresses" readonly="1" options="{'digits':[0,0]}"/> invoice(s) do not have a complete customer address
</span>
</span>
<span class="text-muted" attrs="{'invisible': [('snailmail_cost', '!=', 0)]}">
None of the selected invoices have a valid address
</span>
</span>
</span>
</div>