From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/snailmail_account/wizard/__init__.py | 4 + .../wizard/account_invoice_send.py | 95 ++++++++++++++++++++++ .../wizard/account_invoice_send_views.xml | 65 +++++++++++++++ .../wizard/snailmail_confirm_invoice_send.py | 20 +++++ 4 files changed, 184 insertions(+) create mode 100644 addons/snailmail_account/wizard/__init__.py create mode 100644 addons/snailmail_account/wizard/account_invoice_send.py create mode 100644 addons/snailmail_account/wizard/account_invoice_send_views.xml create mode 100644 addons/snailmail_account/wizard/snailmail_confirm_invoice_send.py (limited to 'addons/snailmail_account/wizard') diff --git a/addons/snailmail_account/wizard/__init__.py b/addons/snailmail_account/wizard/__init__.py new file mode 100644 index 00000000..c720dbcf --- /dev/null +++ b/addons/snailmail_account/wizard/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import account_invoice_send +from . import snailmail_confirm_invoice_send diff --git a/addons/snailmail_account/wizard/account_invoice_send.py b/addons/snailmail_account/wizard/account_invoice_send.py new file mode 100644 index 00000000..ba0129d1 --- /dev/null +++ b/addons/snailmail_account/wizard/account_invoice_send.py @@ -0,0 +1,95 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, _ +from odoo.exceptions import UserError + + +class AccountInvoiceSend(models.TransientModel): + _name = 'account.invoice.send' + _inherit = 'account.invoice.send' + _description = 'Account Invoice Send' + + partner_id = fields.Many2one('res.partner', compute='_get_partner', string='Partner') + snailmail_is_letter = fields.Boolean('Send by Post', + help='Allows to send the document by Snailmail (conventional posting delivery service)', + default=lambda self: self.env.company.invoice_is_snailmail) + snailmail_cost = fields.Float(string='Stamp(s)', compute='_compute_snailmail_cost', readonly=True) + invalid_addresses = fields.Integer('Invalid Addresses Count', compute='_compute_invalid_addresses') + invalid_invoice_ids = fields.Many2many('account.move', string='Invalid Addresses', compute='_compute_invalid_addresses') + + @api.depends('invoice_ids') + def _compute_invalid_addresses(self): + for wizard in self: + invalid_invoices = wizard.invoice_ids.filtered(lambda i: not i.partner_id or not self.env['snailmail.letter']._is_valid_address(i.partner_id)) + wizard.invalid_invoice_ids = invalid_invoices + wizard.invalid_addresses = len(invalid_invoices) + + @api.depends('invoice_ids') + def _get_partner(self): + self.partner_id = self.env['res.partner'] + for wizard in self: + if wizard.invoice_ids and len(wizard.invoice_ids) == 1: + wizard.partner_id = wizard.invoice_ids.partner_id.id + + @api.depends('snailmail_is_letter') + def _compute_snailmail_cost(self): + for wizard in self: + wizard.snailmail_cost = len(wizard.invoice_ids.ids) + + def snailmail_print_action(self): + self.ensure_one() + letters = self.env['snailmail.letter'] + for invoice in self.invoice_ids: + letter = self.env['snailmail.letter'].create({ + 'partner_id': invoice.partner_id.id, + 'model': 'account.move', + 'res_id': invoice.id, + 'user_id': self.env.user.id, + 'company_id': invoice.company_id.id, + 'report_template': self.env.ref('account.account_invoices').id + }) + letters |= letter + + self.invoice_ids.filtered(lambda inv: not inv.is_move_sent).write({'is_move_sent': True}) + if len(self.invoice_ids) == 1: + letters._snailmail_print() + else: + letters._snailmail_print(immediate=False) + + def send_and_print_action(self): + if self.snailmail_is_letter: + if self.env['snailmail.confirm.invoice'].show_warning(): + wizard = self.env['snailmail.confirm.invoice'].create({'model_name': _('Invoice'), 'invoice_send_id': self.id}) + return wizard.action_open() + self._print_action() + return self.send_and_print() + + def _print_action(self): + if not self.snailmail_is_letter: + return + + if self.invalid_addresses and self.composition_mode == "mass_mail": + self.notify_invalid_addresses() + self.snailmail_print_action() + + def send_and_print(self): + res = super(AccountInvoiceSend, self).send_and_print_action() + return res + + def notify_invalid_addresses(self): + self.ensure_one() + 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)} + ) + + def invalid_addresses_action(self): + return { + 'name': _('Invalid Addresses'), + 'type': 'ir.actions.act_window', + 'view_mode': 'kanban,tree,form', + 'res_model': 'account.move', + 'domain': [('id', 'in', self.mapped('invalid_invoice_ids').ids)], + } diff --git a/addons/snailmail_account/wizard/account_invoice_send_views.xml b/addons/snailmail_account/wizard/account_invoice_send_views.xml new file mode 100644 index 00000000..b2efb714 --- /dev/null +++ b/addons/snailmail_account/wizard/account_invoice_send_views.xml @@ -0,0 +1,65 @@ + + + + + account.invoice.send.form.inherited.snailmail + account.invoice.send + + + +
+ +
+ + +
+ + + ( + + + + + ) + + + +
+ to: + +
+
+ +
+ + The customer address is not complete. + +
+
+
+ + + + Some customer addresses are not complete. + + + + +
+
+
+ + {'invisible': ['|', ('is_print', '=', False), '&', '&', ('is_print', '=', True), ('snailmail_is_letter', '=', False), ('is_email', '=', False)]} + + + {'invisible': ['|', ('is_print', '=', True), '&', '&', ('is_print', '=', False), ('snailmail_is_letter', '=', False), ('is_email', '=', False)]} + + + {'invisible': ['|', '|', ('is_print', '=', False), ('snailmail_is_letter', '=', True), ('is_email', '=', True)]} + +
+
+ +
+
diff --git a/addons/snailmail_account/wizard/snailmail_confirm_invoice_send.py b/addons/snailmail_account/wizard/snailmail_confirm_invoice_send.py new file mode 100644 index 00000000..a3c3da40 --- /dev/null +++ b/addons/snailmail_account/wizard/snailmail_confirm_invoice_send.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models + + +class SnailmailConfirmInvoiceSend(models.TransientModel): + _name = 'snailmail.confirm.invoice' + _inherit = ['snailmail.confirm'] + _description = 'Snailmail Confirm Invoice' + + invoice_send_id = fields.Many2one('account.invoice.send') + + def _confirm(self): + self.ensure_one() + self.invoice_send_id._print_action() + + def _continue(self): + self.ensure_one() + return self.invoice_send_id.send_and_print() -- cgit v1.2.3