How to generate XLS report through wizard in odoo10?

05

Jun

How to generate XLS report through wizard in odoo10?

This blog will help you to generate XLS report with wizard using XLWT library. I’ve created simple app which print the data of invoices based on input on wizard.

Step 1: Make sure xlwt is installed in your system, if doesn’t than follow the below command to install it in your system.

sudo apt-get update
sudo apt-get install python-xlwt

Step 2: Create new module with following directory structure.

Step 3: __manifest__.py

{
    'name':'Invoice XLS report',
    'author':"Anil R. Kesariya",
    'version':"1.0",
    'category':"Invoice",
    'description':"Invoice Report",
    'depends':['account'],
    'data':['wizard/invoice_xls_view.xml'],
    'website':"http://anilrk.com",
    'application':False,

}

Step 4 : Main __init__.py

from . import wizard

Step 5 :  Wizard __init__.py

from . import invoice_xls

Step 6 : wizard/invoice_xls.py

from odoo import fields, models, api, _

from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import xlwt
import base64
import cStringIO
from datetime import datetime

class InvoiceXLSWizard(models.Model):

    _name = 'invoice.xls.popup'

    #fields to generate xls
    date_from = fields.Date('Date From')
    date_to = fields.Date('Date To')

    # fields for download xls
    state = fields.Selection([('choose', 'choose'), ('get', 'get')],
                             default='choose')
    report = fields.Binary('Prepared file', filters='.xls', readonly=True)
    name =  fields.Char('File Name', size=32)

    @api.multi
    def generate_xls_report(self):

        self.ensure_one()

        wb1 = xlwt.Workbook(encoding='utf-8')
        ws1 = wb1.add_sheet('Invoices Details')
        fp = cStringIO.StringIO()


        #Content/Text style
        header_content_style = xlwt.easyxf("font: name Helvetica size 20 px, bold 1, height 170;")
        sub_header_style = xlwt.easyxf("font: name Helvetica size 10 px, bold 1, height 170;")
        sub_header_content_style = xlwt.easyxf("font: name Helvetica size 10 px, height 170;")
        line_content_style = xlwt.easyxf("font: name Helvetica, height 170;")
        row = 1
        col = 0
        ws1.row(row).height = 500
        ws1.write_merge(row,row, 2, 6, "Invoice information", header_content_style)
        row += 2
        ws1.write(row, col+1, "From :", sub_header_style)
        ws1.write(row, col+2, datetime.strftime(datetime.strptime(self.date_from,DEFAULT_SERVER_DATE_FORMAT),"%d/%m/%Y"), sub_header_content_style)
        row += 1
        ws1.write(row, col+1, "To :", sub_header_style)
        ws1.write(row, col+2, datetime.strftime(datetime.strptime(self.date_to,DEFAULT_SERVER_DATE_FORMAT),"%d/%m/%Y"), sub_header_content_style)
        row += 1
        ws1.write(row,col+1,"Customer",sub_header_style)
        ws1.write(row,col+2,"Invoice Date",sub_header_style)
        ws1.write(row,col+3,"Number",sub_header_style)
        ws1.write(row,col+4,"Sales person",sub_header_style)
        ws1.write(row,col+5,"Due date",sub_header_style)
        ws1.write(row,col+6,"Source document",sub_header_style)
        ws1.write(row,col+7,"Total",sub_header_style)
        ws1.write(row,col+8,"Amount Due",sub_header_style)
        ws1.write(row,col+9,"Status",sub_header_style)

        row += 1
        #Searching for customer invoices
        invoices = self.env['account.invoice'].search([('type','=','out_invoice')])
        all_inv_total = 0
        for invoice in invoices:

            ws1.write(row,col+1,invoice.partner_id.name,line_content_style)
            ws1.write(row,col+2,invoice.date_invoice,line_content_style)
            ws1.write(row,col+3,invoice.number,line_content_style)
            ws1.write(row,col+4,invoice.user_id.name,line_content_style)
            ws1.write(row,col+5,invoice.date_due,line_content_style)
            ws1.write(row,col+6,invoice.name,line_content_style)
            ws1.write(row,col+7,invoice.amount_total,line_content_style)
            ws1.write(row,col+8,invoice.residual,line_content_style)
            ws1.write(row,col+9,invoice.state.title(),line_content_style)
            row +=1
            all_inv_total += invoice.amount_total
        row +=1
        ws1.write(row,col+6,"Main total:",sub_header_style)
        ws1.write(row,col+7,all_inv_total,sub_header_style)
        wb1.save(fp)
        out = base64.encodestring(fp.getvalue())
        self.write({'state': 'get', 'report': out, 'name':'invoices_detail.xls'})
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'invoice.xls.popup',
            'view_mode': 'form',
            'view_type': 'form',
            'res_id': self.id,
            'views': [(False, 'form')],
            'target': 'new',
        }

 

Step 7 : wizard/invoice_xls_view.xml

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <data>

        <record id="invoice_xls_wizard_form" model="ir.ui.view">
            <field name="name">XLS Popup view</field>
            <field name="model">invoice.xls.popup</field>
            <field name="arch" type="xml">
                <form>
                    <field invisible="1" name="state"/>
                    <div states="choose">
                        <group >
                            <field name="date_from" required="1"/>
                            <field name="date_to" required="1"/>
                        </group>
                    </div>
                    <div states="get">
                        <group>
                            <field name="name" colspan="4" invisible="1"/>
                            <field name="report" filename="name" colspan="4"/>
                        </group>
                    </div>
                    <footer states="choose">
                        <button name="generate_xls_report" string="Export XLS" type="object" class="oe_highlight" />
                        <button special="cancel" string="Cancel" class="oe_highlight" />
                    </footer>
                    <footer states="get">
                        <button special="cancel" string="Cancel" class="oe_highlight" />
                    </footer>
                </form>
            </field>
        </record>

        <record id="action_account_xls_report" model="ir.actions.act_window">
            <field name="name">Generate XLS</field>
            <field name="res_model">invoice.xls.popup</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>
        </record>

        <menuitem id="account_report_xls" name="Invoices XLS" parent="account.menu_finance_reports" action="action_account_xls_report"/>

    </data>
</odoo>

Output

 

 

Happy odooing…

 

 

 

2 thoughts on - How to generate XLS report through wizard in odoo10?

  • Darshini trivedi
    Reply Jun 5, 2017 at 2:58 pm

    It is very helpful to generate xls report .Thank you sir

    • Anil Kesariya
      Reply Jun 5, 2017 at 4:00 pm

      You’re welcome 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *