|
| 1 | +# Copyright 2021 Camptocamp SA |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) |
| 3 | + |
| 4 | +from odoo import _, api, fields, models |
| 5 | +from odoo.exceptions import UserError, ValidationError |
| 6 | +from odoo.fields import first |
| 7 | + |
| 8 | + |
| 9 | +class AccountInvoiceSendValidation(models.TransientModel): |
| 10 | + _name = 'account.invoice.send.validation' |
| 11 | + _inherits = {'mail.compose.message': 'composer_id'} |
| 12 | + _description = 'Account Invoice Send Validation Request' |
| 13 | + |
| 14 | + invoice_id = fields.Many2one('account.move', string='Invoice') |
| 15 | + composer_id = fields.Many2one( |
| 16 | + 'mail.compose.message', |
| 17 | + string='Composer', |
| 18 | + required=True, |
| 19 | + ondelete='cascade', |
| 20 | + ) |
| 21 | + template_id = fields.Many2one( |
| 22 | + 'mail.template', index=True, domain="[('model', '=', 'account.move')]" |
| 23 | + ) |
| 24 | + |
| 25 | + @api.model |
| 26 | + def default_get(self, fields): |
| 27 | + res = super().default_get(fields) |
| 28 | + res_ids = self._context.get('active_ids') |
| 29 | + invoice = first(self.env['account.move'].browse(res_ids)) |
| 30 | + if not invoice: |
| 31 | + raise UserError( |
| 32 | + _("Invoice to request validation for has not been found") |
| 33 | + ) |
| 34 | + composer = self.env['mail.compose.message'].create({}) |
| 35 | + values = { |
| 36 | + 'composition_mode': "mass_mail", |
| 37 | + 'invoice_id': invoice.id, |
| 38 | + 'composer_id': composer.id, |
| 39 | + } |
| 40 | + if invoice.user_validation_responsible_id: |
| 41 | + values["partner_ids"] = [ |
| 42 | + (4, invoice.user_validation_responsible_id.partner_id.id, 0) |
| 43 | + ] |
| 44 | + if hasattr(invoice, 'attachment_ids') and invoice.attachment_ids: |
| 45 | + values['attachment_ids'] = invoice.attachment_ids.ids |
| 46 | + res.update(values) |
| 47 | + return res |
| 48 | + |
| 49 | + @api.onchange('template_id') |
| 50 | + def onchange_template_id(self): |
| 51 | + for wizard in self: |
| 52 | + if wizard.composer_id: |
| 53 | + wizard.composer_id.template_id = wizard.template_id.id |
| 54 | + wizard.composer_id.onchange_template_id_wrapper() |
| 55 | + |
| 56 | + def send_action(self): |
| 57 | + self.ensure_one() |
| 58 | + if not self.partner_ids: |
| 59 | + raise ValidationError(_("An email recipient is required.")) |
| 60 | + self.composer_id.send_mail() |
| 61 | + return {'type': 'ir.actions.act_window_close'} |
0 commit comments