Hướng Dẫn Wizard Jasper Report

Thảo luận trong 'Python' bắt đầu bởi bruce.nguyen, 10/5/14.

  1. bruce.nguyen Active Member

    Hi All,

    - Today, i sharing document write module using wizard memory openerp 6.1 export data Report Jasper have multi table and pie chart.
    - I implement and overide module account_asset openerp. Adding multi option
    Example: Asset from Branch, Building, Floor and Mobile client scan Barcode
    - We all start.

    Struct Module:
    https://www.dropbox.com/s/8u9imp8mbe9b48h/Screenshot 2014-05-10 15.14.00.png

    1. Create module jasper_report_asset have 2 file .py required :

    __init__.py : using import module i write
    __openrp__.py: using import multi view, layout if i want

    2. Create 2 packge :

    - report:

    + I Using Jasper Design Tool create new template jrxml file: jasper_print_asset_analysis.jxrml
    + I create file .py inside location:
    Mã (text):
    1.  
    2.  
    3. from jasper_report_user_analysis import JasperDataParser
    4. from jasper_reports import jasper_report
    5. import pooler
    6. import os
    7.  
    8. # class general data filter for report template
    9.  
    10. class jasper_print_asset_analysis(JasperDataParser.JasperDataParser):
    11.     def __init__(self, cr, uid, ids, data, context):
    12.         super(jasper_print_asset_analysis, self).__init__(cr, uid, ids, data, context)
    13.  
    14.     def generate_data_source(self, cr, uid, ids, data, context):
    15.         return 'records'
    16.  
    17.     def generate_parameters(self, cr, uid, ids, data, context):
    18.         return {}
    19.  
    20.     def generate_properties(self, cr, uid, ids, data, context):
    21.         return {}
    22.  
    23.     def generate_records(self, cr, uid, ids, data, context):
    24.         print 'generate_records', data
    25.         pool= pooler.get_pool(cr.dbname)
    26.         result=[]
    27.         account_asset_ids = []
    28.  
    29.         if 'form' in data:
    30.             account_asset_ids = data['form']['account_asset_ids']
    31.  
    32.  
    33.  
    34.         for asset in self.pool.get('account.asset.asset').browse(cr, uid, account_asset_ids):
    35.             data = {
    36.                 'path':os.getcwd(),
    37.                 'name': asset.name if asset.name else 'None',
    38.                 'purchase_value': asset.purchase_value if asset.purchase_value != 0 else 0.00,
    39.                 'salvage_value': asset.salvage_value if asset.salvage_value != 0 else 0.00,
    40.                 'feosco_code': asset.feosco_code,
    41.                 'value_residual': asset.value_residual if asset.value_residual != 0 else 0.00,
    42.                 'feosco_branch_id': asset.feosco_branch_id.name,
    43.                 'feosco_building_id': asset.feosco_building_id.name,
    44.                 'feosco_floor_id': asset.feosco_floor_id.name
    45.  
    46.             }
    47.             result.append(data)
    48.  
    49.         result = sorted(result, key=lambda d: (d['name'], d['purchase_value'],))
    50.         print result
    51.         return result
    52.  
    53. jasper_report.report_jasper('report.jasper_print_asset_analysis', 'res.users', parser=jasper_print_asset_analysis, )
    54.  
    + After i adding logo my company to this location: logo.png, this is viewing to report export
    + Create file python: __init__.py import module
    Mã (text):
    1.  
    2. import jasper_print_asset_analysis
    3.  
    - wizard:

    + Create file : report_analysis.py , using parser and filter data
    Mã (text):
    1.  
    2. # -*- coding: utf-8 -*-
    3. # author: thanhchatvn@gmail.com from vietnamese
    4.  
    5. from osv import osv,fields
    6.  
    7. # class memory orm openerp
    8.  
    9. class print_asset_analysis(osv.osv_memory):
    10.     _name = "print.asset.analysis"
    11.  
    12.     _columns = {
    13.         'account_asset_ids':fields.many2many('account.asset.asset',
    14.                                     'print_asset_analysis_rel',
    15.                                     'print_asset_analysis_id',
    16.                                     'account_asset_id','Users', required=True),
    17.         'report_type':fields.selection([("pdf","PDF"),
    18.                                         ("xls","Excel"),
    19.                                         ("html","HTML")
    20.                                         ],'Type'),
    21.         'state': fields.selection([('choose','choose'),
    22.                                     ('get','get'),
    23.                                   ]),
    24.  
    25.     }
    26.  
    27.     def create_report(self, cr, uid, ids, context={}):
    28.         data = self.read(cr,uid,ids,)[-1]
    29.         print '*' * 100
    30.         return {
    31.             'type': 'ir.actions.report.xml',
    32.             'report_name': 'jasper_print_asset_analysis',
    33.             'datas': {
    34.                     'model': 'account.asset.asset',
    35.                     'id': context.get('active_ids') and context.get('active_ids')[0] or False,
    36.                     'ids': context.get('active_ids') and context.get('active_ids') or [],
    37.                     'report_type': data['report_type'],
    38.                     'form':data
    39.                 },
    40.             'nodestroy': False
    41.             }
    42.  
    43.         return {
    44.             'type': 'ir.actions.report.xml',
    45.             'report_name': 'jasper_report_user_print',
    46.             'datas': {
    47.                     'model': 'project.project',
    48.                     'id': context.get('active_ids') and context.get('active_ids')[0] or False,
    49.                     'ids': context.get('active_ids') and context.get('active_ids') or [],
    50.                     'report_type': data['report_type'],
    51.                     'form': data
    52.                 },
    53.             'nodestroy': False
    54.             }
    55.  
    56.  
    57.     _defaults = {
    58.         'report_type': lambda *a: 'pdf',
    59.         'state': lambda *a: 'choose',
    60.     }
    61. print_asset_analysis()
    62.  
    + Create View form, menu, action: report_analysis_view.xml
    Mã (text):
    1.  
    2. <?xml version="1.0"?>
    3. <openerp>
    4.     <data>
    5.  
    6.  
    7.         <record id='wizard_jasper_print_asset_analysis' model="ir.ui.view">
    8.             <field name="name">Asset Reporting</field>
    9.             <field name="model">print.asset.analysis</field>
    10.             <field name="type">form</field>
    11.             <field name="arch" type="xml">
    12.                 <form string="User Analysis" >
    13.                     <group col="2" fill="0" height="500" states="choose">
    14.                         <separator string="Asset List Print" colspan="4"/>
    15.                         <field name='account_asset_ids' nolabel="1" colspan="4" width="500"/>
    16.                         <label colspan="2" string="" />
    17.                         <field name="report_type"  />
    18.                         <separator colspan="2"/>
    19.                         <group colspan="2">
    20.                             <button special="cancel" icon="gtk-cancel" string="Cancel"/>
    21.                             <button type="object" name="create_report" icon="gtk-go-forward" string="Create"/>
    22.                         </group>
    23.                         <field invisible="1" name="state"/>
    24.                     </group>
    25.  
    26.                 </form>
    27.             </field>
    28.         </record>
    29.  
    30.         <record model="ir.actions.act_window" id="action_jasper_print_asset_analysis">
    31.             <field name="name">Asset Report</field>
    32.             <field name="res_model">print.asset.analysis</field>
    33.             <field name="view_type">form</field>
    34.             <field name="view_mode">form</field>
    35.             <field name="target">new</field>
    36.         </record>
    37.  
    38.         <report string="Asset Report"
    39.             model="account.asset.asset"
    40.             auto="True"
    41.             name="jasper_report_user_print"
    42.             rml="jasper_report_asset/report/jasper_print_asset_analysis.jrxml"
    43.             id="jasper_print_asset_analysis"
    44.             menu="False"
    45.             />
    46.  
    47.         <menuitem id="menu_jasper_print_asset_analysis" name="Report Asset"
    48.                     action="action_jasper_print_asset_analysis"
    49.                     />
    50.  
    51.     </data>
    52. </openerp>
    53.  
    + Create file __init__.py: import module
    Mã (text):
    1.  
    2. import report_analysis
    3.  
    3. Create file __init__.py and __openerp__.py of module:

    + __init__.py:
    Mã (text):
    1.  
    2. import wizard
    3. import report
    4. import JasperDataParser
    5.  
    + __openerp__.py:
    Mã (text):
    1.  
    2. {
    3.     "name" : "Asset Management Report",
    4.     "version" : "0.1",
    5.     "description" : "Module for Jasper Reports",
    6.     "author" : "thanhchatvn@gmail.com",
    7.     "website" : "thanhchatvn@gmail.com",
    8.     "depends" : ["base","jasper_reports", "feosco_account_asset"],
    9.     "category" : "thanhchatvn",
    10.     "init_xml" : [],
    11.     "demo_xml" : [],
    12.     "update_xml" : ['reports.xml', 'wizard/report_analysis_view.xml'],
    13.     "active": False,
    14.     "installable": True
    15. }
    16.  
    4. I create new module : JasperDataParser.py using filter function method of Jasper

    + JasperDataParser.py:
    Mã (text):
    1.  
    2. #author:thanhchatvn@gmail.com
    3. import pooler
    4.  
    5.  
    6. # object parameter filter function
    7.  
    8. class JasperDataParser(object):
    9.     def __init__(self, cr, uid, ids, data, context):
    10.         if not context: context = {}
    11.         self.cr = cr
    12.         self.uid = uid
    13.         self.ids = ids
    14.         self.data = data
    15.         self.context = context
    16.         self.pool = pooler.get_pool(cr.dbname)
    17.  
    18.     def get(self, parameter, default_value):
    19.         if parameter == 'ids':
    20.             return hasattr(self, 'generate_ids') and \
    21.                 self.generate_ids(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    22.         elif parameter == 'name':
    23.             return hasattr(self, 'generate_name') and \
    24.                 self.generate_name(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    25.         elif parameter == 'model':
    26.             return hasattr(self, 'generate_model') and \
    27.                 self.generate_model(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    28.         elif parameter == 'records':
    29.             return hasattr(self, 'generate_records') and \
    30.                 self.generate_records(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    31.         elif parameter == 'data_source':
    32.             return hasattr(self, 'generate_data_source') and \
    33.                 self.generate_data_source(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    34.         elif parameter == 'parameters':
    35.             return hasattr(self, 'generate_parameters') and \
    36.                 self.generate_parameters(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    37.         elif parameter == 'properties':
    38.             return hasattr(self, 'generate_properties') and \
    39.                 self.generate_properties(self.cr, self.uid, self.ids, self.data, self.context) or default_value
    40.         else: return default_value
    41.  
    42. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
    43.  
    Required: intalled JRE JDK

    CheckOut Source Code from GoogleCode
    - Linux, Ubuntu, Centos, Macos..
    - Windown using client checkout
    Mã (text):
    1.  
    2. svn co http://chuyenhangus.googlecode.com/svn/trunk/asset_management_v61/
    3.  
    Hope helpful. :)
    Rgds.

Chia sẻ trang này