|
| 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(_("Invoice to request validation for has not been found")) |
| 32 | + composer = self.env["mail.compose.message"].create({}) |
| 33 | + values = { |
| 34 | + "composition_mode": "mass_mail", |
| 35 | + "invoice_id": invoice.id, |
| 36 | + "composer_id": composer.id, |
| 37 | + } |
| 38 | + if invoice.user_validation_responsible_id: |
| 39 | + values["partner_ids"] = [ |
| 40 | + (4, invoice.user_validation_responsible_id.partner_id.id, 0) |
| 41 | + ] |
| 42 | + if hasattr(invoice, "attachment_ids") and invoice.attachment_ids: |
| 43 | + values["attachment_ids"] = invoice.attachment_ids.ids |
| 44 | + res.update(values) |
| 45 | + return res |
| 46 | + |
| 47 | + @api.onchange("template_id") |
| 48 | + def onchange_template_id(self): |
| 49 | + for wizard in self: |
| 50 | + if wizard.composer_id: |
| 51 | + wizard.composer_id.template_id = wizard.template_id.id |
| 52 | + wizard.composer_id.onchange_template_id_wrapper() |
| 53 | + |
| 54 | + def send_action(self): |
| 55 | + self.ensure_one() |
| 56 | + if not self.partner_ids: |
| 57 | + raise ValidationError(_("An email recipient is required.")) |
| 58 | + self.composer_id.send_mail() |
| 59 | + return {"type": "ir.actions.act_window_close"} |
0 commit comments