diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/snailmail_account/wizard | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/snailmail_account/wizard')
4 files changed, 184 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + <data> + <record model="ir.ui.view" id="account_invoice_send_inherit_account_wizard_form"> + <field name="name">account.invoice.send.form.inherited.snailmail</field> + <field name="model">account.invoice.send</field> + <field name="inherit_id" ref="account.account_invoice_send_wizard_form"/> + <field name="arch" type="xml"> + <xpath expr="//div[@name='option_email']" position='before'> + <div name="option_letter"> + <field name="invalid_addresses" invisible="1"/> + <div name="option" class="text-left d-inline-block"> + <field name="snailmail_is_letter" /> + <b><label for="snailmail_is_letter"/></b> + </div> + <span attrs="{'invisible': [('snailmail_is_letter','=', False)]}"> + <span class="mr4" attrs="{'invisible': [('snailmail_cost', '=', 0)]}"> + <b>( + <span> + <field name="snailmail_cost" options="{'digits':[0,0]}" class="mr4"/> + <label for="snailmail_cost" class="mr4"/> + </span> + <i class="fa fa-info-circle" role="img" aria-label="Warning" title="Make sure you have enough Stamps on your account."/> + )</b> + </span> + <span attrs="{'invisible': [('composition_mode', '=', 'mass_mail')]}"> + <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': 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-danger"> + The customer address is not complete. + </span> + </div> + </span> + </span> + <span attrs="{'invisible': [('composition_mode', '!=', 'mass_mail')]}"> + <span attrs="{'invisible': [('invalid_addresses', '=', 0)]}"> + <span class="text-danger"> + Some customer addresses are not complete. + </span> + <button type="object" name="invalid_addresses_action" class="btn btn-link" role="button"><field name="invalid_addresses" readonly="1" options="{'digits':[0,0]}"/> invoices</button> + </span> + </span> + </span> + </div> + </xpath> + <xpath expr="//footer/button[hasclass('send_and_print')]" position='attributes'> + <attribute name="attrs">{'invisible': ['|', ('is_print', '=', False), '&', '&', ('is_print', '=', True), ('snailmail_is_letter', '=', False), ('is_email', '=', False)]}</attribute> + </xpath> + <xpath expr="//footer/button[hasclass('send')]" position='attributes'> + <attribute name="attrs">{'invisible': ['|', ('is_print', '=', True), '&', '&', ('is_print', '=', False), ('snailmail_is_letter', '=', False), ('is_email', '=', False)]}</attribute> + </xpath> + <xpath expr="//footer/button[hasclass('print')]" position='attributes'> + <attribute name="attrs">{'invisible': ['|', '|', ('is_print', '=', False), ('snailmail_is_letter', '=', True), ('is_email', '=', True)]}</attribute> + </xpath> + </field> + </record> + + </data> +</odoo> 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() |
