Search code examples
python-3.xodooodoo-12

close wizard after clicking button in odoo 12


can you please help me regarding close wizard.
i have created a wizard from xml when i add dates and click on xlsx button,
xlsx generated and wizard close it self. it works fine.
but when i click on pdf, pdf generate successfully but wizard remains open.
how can i close it.

here is my code of xml.

<record id="payment_invoice_wizard_form" model="ir.ui.view">
        <field name="name">Invoice Payment Report</field>
        <field name="model">invoice.payment_report</field>
        <field name="arch" type="xml">
            <form string="Invoice Payment Report">
                <group>
                    <field name="start_date"/>
                    <field name="end_date"/>
                    <field name="status"/>
                </group>
                <!-- other fields -->
                 <footer>
                    <button name="print_pdf" string="Print" type="object" class="btn-primary"/>
                    <button name="print_xls" string="Print in XLS" type="object" class="btn-primary"/>
                    <button string="Cancel" class="btn-default" special="cancel" />   
                </footer>

            </form>          
        </field>
    </record>


on py side i am getting all necessary data and returning this function

    @api.multi
    def print_pdf(self):
        #mycode
        return self.env.ref('customer_products.pdf_products').report_action(self)

Solution

  • When Odoo launch the download action of a report it will check if close_on_report_download action attribute is set to true, if so it will return action of type ir.actions.act_window_close which will close the wizard.

    @api.multi
    def print_pdf(self):
        action = self.env.ref('customer_products.pdf_products').report_action(self)
        action.update({'close_on_report_download': True})
        return action
    

    Edit:

    You can implement the same logic, override QWEBActionManager and check if the option is passed through the action definition and if yes close the window.

    var ActionManager = require('web.ActionManager');
    var session = require('web.session');
    
    ActionManager.include({
        ir_actions_report: function (action, options) {
            var self = this;
            return $.when(this._super.apply(this, arguments), session.is_bound).then(function() {
                if (action && action.report_type === 'qweb-pdf' && action.close_on_report_download) {
                    return self.do_action({ type: 'ir.actions.act_window_close' });
                }
            });
        },
    });