13 Maret 2012

Membuat laporan OpenERP dalam CSV/Excel (create excel/csv report on OpenERP)

Bismillahirrahmaanirrahiim

Alhamdulillah masih bulan Maret, hehe niatnya tiap bulan bisa menghasilkan 1 tulisan... Mudah2an selalu sempat dan ingin berbagi :)

Alhamdulillah juga menemukan cara untuk buat report OpenERP dalam bentuk Excel.. hmm masih csv sii, tapi kan tetap bisa dibuka di Excel :D

Sebelumnya say thanks for Wyden Silvan  yang sudah berbagi informasi cara membuat report dalam CSV.

Jadi, yang harus kita lakukan adalah buat class Report (report.py)

seperti berikut:

import base64
from osv import fields,osv
from tools.translate import _
import time

class Report(osv.osv_memory):
    """
    Wizard to create custom report
    """
    _name = "report"
    _description = "Create Report"
    _columns = {
                'start_date' : fields.date('Start Date', required=True),
                'end_date' : fields.date('End Date', required=True),
                'data': fields.binary('File', readonly=True),
                'name': fields.char('Filename', 16, readonly=True),
                'state': fields.selection( ( ('choose','choose'),   # choose date
                     ('get','get'),         # get the file
                   ) ),
                }
    
    def create_report(self,cr,uid,ids,context={}):
        this = self.browse(cr, uid, ids)[0]
        output = 'Start;Ende'
        output += '\n' + this.start_date + ';' + this.end_date
        print this.start_date
        out=base64.encodestring(output)
        return self.write(cr, uid, ids, {'state':'get',  'data':out,'name':'test.csv'}, context=context)
        
    _defaults = { 
                 'state': lambda *a: 'choose',
                 'start_date' : lambda *a: time.strftime('%Y-%m-%d'),
                 'end_date' : lambda *a: time.strftime('%Y-%m-%d'),
                }
   
Report()

Perhatikan yang saya highlight warna biru. Itu merupakan field yang harus tersedia.
Sedangkan yang saya highlight warna merah, merupakan proses inti dari export file ke CSV.

Kemudian untuk view xml nya (file report.xml)

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

<openerp>
    <data>
    
        <!--  =========== VIEWS =========== -->    
    
        <record model="ir.ui.view" id="view_wizard">
            <field name="name">Custom Report</field>
            <field name="model">report</field>
            <field name="type">form</field>
            <field name="priority">16</field>
            <field name="arch" type="xml">
                <form col="3" string="Custom Report">
                    <group col="2" fill="0" height="2500" states="choose">
                        <separator string="Report" colspan="2"/>
                        <field name="start_date" width="200"/>
                        <field name="end_date" width="200"/>
                        <separator colspan="2"/>
                        <group colspan="2">
                            <button special="cancel" icon="gtk-cancel" string="Cancel"/>
                            <button type="object" name="create_report" icon="gtk-go-forward" string="Create"/>
                        </group>
                        <field invisible="1" name="state"/>
                    </group>
                    <group col="3" fill="0" states="get">
                        <separator colspan="3" string="Export done"/>
                        <field name="name" invisible="1" width="100"/>
                        <field name="data" nolabel="1" readonly="1" width="100" fieldname="name"/>
                    </group>
                </form>
            </field>
        </record>
        
        <!--  =========== ACTIONS =========== -->    
        
        <record model="ir.actions.act_window" id="action_report">
            <field name="name">Custom Report</field>
            <field name="res_model">report</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>
        </record>
        
        <!--  =========== MENU ITEMS =========== -->
        <menuitem id="menu_report" name="Report"
                    action="action_report"
                    parent="account.menu_finance_reporting" 
                    sequence="40" />
        
        
    </data>
</openerp>

Yang saya highlight merah, adalah tombol proses yang akan mengeksekusi method create_report pada class Report. Proses tersebut hanya mengexport data yang akan dibuat file csv.
Jika proses berhasil, akan muncul tombol Save As untuk memilih lokasi file csv (default file name: test.csv lihat yang dihighligt kuning pada file report.py)


Nah hasil dari proses di atas, adalah menu Report yang berada di bawah menu ACCOUNTING > Reporting>Report
yang jika diklik akan muncul window berikut:




 Kemudian jika diklik tombol Create, akan muncul form berikut:






Klik tombol Save As untuk memunculkan file dialog.

Hasil file csv nya seperti ini deh:



Demikian...
Seterusnya bisa dimodif sesuka hati.
Kalau saya sendiri, saya tambahkan tombol Export to CSV bersisian dengan tombol yang menghasilkan report PDF seperti contoh ini:




Semoga membantu.......