To show how to generate report from wizard let me give a simple definition of report in odoo
- The report is a xml template read and show data from python file
so if we want to generate report we have to pass the data form the python file to the template we designed , so we can say that we have 2 cases here
- we want to generate report for single record , so we can linked the model with the template directly and read the fields that we want to show in our template
- we want to show data from multi records and depends on some conditions , so we must get the data we want first and then pass it to the template and that can make by using wizard by following the steps below
- create the transient model "the wizard model" which will contain the function that will pass the needed data to the report template , and of course create it view "the wizard view"
- create template and design it's form
- link the model with the template to make data move form the model to the template
Now let us make an example
let us say we have model contain the student data by name student.py as below , and we want to generate report of students who age is in range defined by the user
from openerp import models, fields, api, _
class Student(models.Model):
_name = 'student.student'
"""
contain all student data
"""
name = fields.Char('Name', required=True)
age = fields.Integer('Age', required=True)
phone = fields.Integer('Phone', required=True)
email = fields.Char('Email', required=True)
so first we gonna create wizard by name student_report_wiz.py and we gonna add two integers fields to make the user define the range of age , after that we will take the two values which entered by the user and go to the student model and get the id of any student whose age is less than the max value and larger than the min value which are defined by the user , and push all ids in dictionary which passed to the template , as below
from openerp import models, api
class student_report_wiz(models.TransientModel):
"""
get students by age range
"""
_name = 'student.report.wiz'
class student_report_wiz(models.TransientModel):
"""
get students by age range
"""
_name = 'student.report.wiz'
max_age = fields.Integer('Max Age', required=True)
min_age = fields.Integer('Min Age', required=True)
@api.multi
def get_students(self):
@api.multi
def get_students(self):
student_ids = self.env['student.student'].search([])
student_list = []
for rec in student_ids :
if rec.age > self.min_age and rec.age < self.max_age"
student_list.append(rec.id)
student_dic = {stud_list: student_list}
return student_dic
return self.pool['report'].get_action(cr, uid, [], 'module_name.students_age_report_temp', data=student_dic, context=context)
Now we get all student we want in student_dic , then we will create the report py file by name students_age_report.py as below
class students_age_report(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
self.context = context
super(students_age_report, self).__init__(cr, uid, name, context)
self.localcontext.update({
'line':self._getdata,
})
def _getdata(self,data):
"""
method that return student's in age range
"""
lines = {}
lines = data['form']
return lines
class students_age_report_details(osv.AbstractModel):
_name = 'report.module_name.students_age_report_temp'
_inherit = 'report.abstract_report'
_template = 'module_name.students_age_report_temp'
_wrapped_report_class = students_age_report
def __init__(self, cr, uid, name, context):
self.context = context
super(students_age_report, self).__init__(cr, uid, name, context)
self.localcontext.update({
'line':self._getdata,
})
def _getdata(self,data):
"""
method that return student's in age range
"""
lines = {}
lines = data['form']
return lines
class students_age_report_details(osv.AbstractModel):
_name = 'report.module_name.students_age_report_temp'
_inherit = 'report.abstract_report'
_template = 'module_name.students_age_report_temp'
_wrapped_report_class = students_age_report
then we will create template by id students_age_report_temp as below
<?xml version="1.0" encoding="utf-8" ?>
<openerp>
<data >
<!-- ...Define Template... -->
<template id="students_age_report_temp">
<t t-call="report.html_container">
<div class="page">
<header>
<center>
<div style="float:left; width:80%;">
<div style="float:right; width:75%;">
<b style="font-family:Georgia, serif;font-size:16px;">
student by age range
</b>
<br></br><br></br>
</div>
<openerp>
<data >
<!-- ...Define Template... -->
<template id="students_age_report_temp">
<t t-call="report.html_container">
<div class="page">
<header>
<center>
<div style="float:left; width:80%;">
<div style="float:right; width:75%;">
<b style="font-family:Georgia, serif;font-size:16px;">
student by age range
</b>
<br></br><br></br>
</div>
<div>
<table>
<th>
<td>
<b style="size:16px;">
name
</b>
name
</b>
</td>
<td>
<b style="size:16px;">
age
</b>
</td>age
</b>
<td>
<b style="size:16px;">
phone
</b>
</td>phone
</b>
<td>
<b style="size:16px;">
email
</b>
</td> </b>
</th>
<t t-foreach="line(data)" t-as="line" >
<tr>
<td>
<b style="size:16px;">
<span t-esc="line(data).name"/>
</b>
</td><span t-esc="line(data).name"/>
</b>
<td>
<b style="size:16px;">
<span t-esc="line(data).age"/>
</b>
</td><span t-esc="line(data).age"/>
</b>
<td>
<b style="size:16px;">
<span t-esc="line(data).phone"/>
</b>
</td><span t-esc="line(data).phone"/>
</b>
<td>
<b style="size:16px;">
<span t-esc="line(data).email"/>
</b>
</td> <span t-esc="line(data).email"/>
</b>
</tr>
</table
</div>
</div>
</p>
</div>
</t>
</template>
<!-- """create report """ -->
<report
id="students_age_report_id"
string="Students By Age Report"
model="student.student"
report_type="qweb-pdf"
name="maodule_name.students_age_report_temp"
menu="False"
/>
<record id="report.paperformat_euro" model="report.paperformat">
<field name="margin_top">15</field>
<field name="margin_bottom">15</field>
<field name="margin_left">15</field>
<field name="margin_right">15</field>
<field name="header_spacing">15</field>
</record>
</data>
</openerp>
</div>
</p>
</div>
</t>
</template>
<!-- """create report """ -->
<report
id="students_age_report_id"
string="Students By Age Report"
model="student.student"
report_type="qweb-pdf"
name="maodule_name.students_age_report_temp"
menu="False"
/>
<record id="report.paperformat_euro" model="report.paperformat">
<field name="margin_top">15</field>
<field name="margin_bottom">15</field>
<field name="margin_left">15</field>
<field name="margin_right">15</field>
<field name="header_spacing">15</field>
</record>
</data>
</openerp>
>>>DONE
No comments:
Post a Comment