Wednesday, January 5, 2011

Reports In openERP

      This feature helps to create PDF reports. Data taken from our database can be used to create reports. To create a report we need a rml file. The rml file can be generated from sxw file. More about sxw file is explained later in this blog.

      First of all create a new directory called “report” inside our module. Then create an init file and import the file names of the files that we are going to use for report generation. We have to also update the init file of our module. That is we have to import the folder name "report" in the init file. 
The python program is give below.

We have to import report_sxw from report and osv from osv.

class test_parser(report_sxw.rml_parse):-->we have to inherit the class report_sxw.rml_parse. 

super(test_parser, self).__init__(cr, uid, name, context=context)--->here is the function overiding
 
self.localcontext.update({})--> Inside braces if we are defining any functions for taking data from the database then we have to specify the function names here.
 
report_sxw.report_sxw('report.test_report','student.class','addons/dec13/report/new_details.rml',parser=test_parser)-->The instance of the function is called here. Parameters passing here are name of the report, Which class is using this, its rml file location, then the parser(the class which create the report).

Before reloading the server we have to generate the rml file from the sxw file. Open office helps to create the sxw file. Create the sxw file inside our module's report folder. Static information can be added by typing inside the sxw file.For dynamic information we have to use the following method.

[[repeatIn(objects,'o')]] --> Inside 2 square brackets(tagging) add repeatIn(). This will help to take data from the database. RepeatIn works same like browse(). The first parameter can be objects, function name etc. Second parameter is the short_name used for calling the object. If no function is defined in report then the object of the class in which we are using the report is passed as the object and we can use each field using short_name.field_name. 

Name:[[o.name]] --> Name is the text and o.name take the value from the database where fieldname= name
 If the field defined is one2many or many2many, then we have to first create a section in the sxw file and inside this define repeatIn() with the o.field_name as its first parameter.  

To convert the *.sxw to *.rml first locate the directory “openerp_sxw2rml” copy  file openerp_sxw2rml.py, [normalized_odt2rml.xsl and normalized_oo2rml.xsl --sometimes these two files will be needed]  to our report folder. Then  
./openerp_sxw2rml.py file_name.sxw > file_name.rml  
will convert the sxw file to rml file.


Now we have completed 80% of our report creation. We have to add report feature to our view. Go to our xml file(or create a new xml file add this xml file name to the terp file).
 <openerp>
<data>
   <report id="some_unique_id"
          string="Name_in_the_menu_that_calls_the_report"
          model="name_of_the_model_in_which_the_report_will_be_rendered"
          name="name_of_the_file"
          rml="PATH/filename.rml"    <!--PATH is relative to addons/ directory-->
          menu="True"    <!--whether the report will be able to be called directly via the client or not.  Setting menu to False is useful in case of reports called bywizards.-->
          auto="False"/>
 </data>
</openerp> 
  

This will create a menu inside the model we have specified. By clicking this field a pdf file will be opened with the values we have specified in the sxw file.

7 comments:

  1. I followed above steps but not able to convert sxw to rml through command specified(replaced tiny with openerp)..

    ReplyDelete
    Replies
    1. In OpenERP addons, there you can find base_report_designer module. In that there will be directory named 'openerp_sxw2rml', you can use that directory to convert our sxw file to rml file.

      Delete
  2. The link is coming in right panel but when clicked following error is coming : "TypeError: coercing to Unicode: need string or buffer, tuple found"

    ReplyDelete
    Replies
    1. This Error arise when you have done some mistakes in thw sxw file, fou example without adding section or table to use the repeatIn(). If you are modifying an existing report, then i can help you

      Delete
  3. please post with sample records

    ex : <report id= tag

    ReplyDelete
  4. how can i override a function which is in parser class
    ex. account_partner_ledger file ,inside third_party_ledger parser class i have set_context() function .how can i override this and make changes..

    ReplyDelete
    Replies
    1. import the account_partner_ledger and its third_party_ledger class.
      then in your new class specify the third_party_ledger along with other base classes you need. Now you will be able to get the value of set_context()

      Delete