Thursday, December 15, 2011

Using widgets in openerp 6.1

We have seen in OpenERP web/gtk client that some fields which are defined as many2one fields are shown as selection field or a float field which are shown in web/gtk client as a progress bar. I was confused when I first saw this. Then I came to know about widget given in XML file.
To apply widget for a field, go to the XML file, find the line where that field is specified then add widget='widget_name'
Example:
<field name="my_field" widget='widget_name'/>

In this blog I'm specifying different types of widgets that we can use in OpenERP.
  • selection :I have a many2one field to 'res.partner' and I don't want other users to get the data of 'res.partner' from that field. So I make the field as a selection by applying the widget so that only the name of the records will be shown.
    <field name="partner_id" widget='selection'/>
Progress Bar
  • progressbar : I have a float field and I want to show it as a progress bar. Then I use widget="progressbar". This field will be always a functional field which returns a float number. You can see this type of field in project.
    <field name="progress_rate" widget='progressbar'/>  

Status bar in Sale Order
  • statusbar : You may have seen in sale, purchase, invoice etc a selection field called "state". We can change the way this selection field appears. To change its view to statusbar,
    <field name="selection_field" widget="statusbar" statusbar_visible="state1,state4,state7" statusbar_colors='{"state2":"red","state3":"red","state5":"blue","state6":"blue"}'/> 

    Here state1 to state7 are the different states in the selection_field. In 'statusbar_visible' we specify the different states which should appear in the view. In 'statusbar_color' we specify the color for the status bar according to the states.
  • one2many : If there is a many2many field and we want to view it as a one2many field. Then we use 'one2many'.
  • many2many : If there is a one2many field and we want to view it as a many2many field, then we use 'many2many'
  • float_time : This is used for showing a float type field in 00:00 format
  • image : If we have a binary field for adding pictures/images, then by providing widget='image', we van view the image.
  • url : If we have a char field for adding website name, then by adding widget='url', the data in the field will have a hyperlink and by clicking on it, the website will be opened.
  • email: If we have a char field for adding the email address, and if we want to sent an e-mail to that address using our systems default mail client, use widget='email'

Tuesday, July 26, 2011

Group Operator in OpenERP for Grouping

When defining search views, we may sometimes need to add "group_by" filters for showing the list view in groups. For example,







 The image shown above is from sale order. Here the list view grouping is done by user_id(salesman). Let focus on Demo User. Here four sale orders are created by the Demo user.

This is the extended view. Here the name written in bold is the group name. Now focus on the Untaxed Amount and Total and the value writen inside the red marker. The value shown inside the red marker are actually the sum of float or integer fields comes under the group. Here comes the use of group operator. The default value for group_operator is sum. Thats why we are getting the sum when grouping. To change the default value goto our python file, in the field definition add group_operator="avg" to get the average, group_operator="min" to get the minimum value from the group, group_operator="max" to get the maximum value from the group.
For example,

 Here I added group_operator="avg" in the untaxed amount. Now we have to verify it by grouping.
 Here the value shown inside the red marker is the average of that group. Now you can try the min and max by changing the group_operator value.

Friday, July 1, 2011

Creating an Excel file or a spreadsheet using Python's xlwt (xlutils) module

A spreadsheet is used to simulates a paper accounting worksheet. It displays multiple cells usually in a two-dimensional matrix or grid consisting of rows and columns. We have so many reporting software s that will simply create spreadsheets. But the problem is, these reporting software s may not support on all systems and also take lot of CPU time. Then arise an idea to use some method to create spreadsheet from my python code. Then I found about the module xlutils in python.

XLUTILS module contain packages for reading from(xlrd) and writing to(xlwt) excel files. Since I only need the package to write to excel file I'm installing the xlwt package.

Installation
We can install any python package to our repository using pip, pip is an easy way to install python packages.
To install pip
omal@zbeanz:~$sudo apt-get install python-pip
After installing pip you can install python packages by
omal@zbeanz:~$sudo pip install xlwt
This will install xlwt package to python modules

 Now in our python code,
to import the package functionality to the our program.
We can define the title, header, number, date, and normal style using xlwt.easyxf(). For example title style is given below
We can define as many styles we want. We can also define how the border should appear. For example,
After defining the styles we can now define the workbook. Workbook is actually what we view in our spreadsheet. To define workbook,
A spreadsheet may contain more than one sheet, so to add sheets,
Now we can start adding data to the sheet we have created.  To write data to the 'sheet1' use the reference ws and ws.write(row, column, 'Value or text',style). For example:
To change the width and height of a cell,
 We can add complete data to the spreadsheet by writing and changing the height and width according to our need. After completing this we can save the workbook to a buffer. So First define the buffer and then save the workbook to the  buffer. This doesn't complete the process.
Now we have to encode the data in the buffer and write it to a file say 'test.xls'. To encode the data in the buffer import base64 module. Now open the file test.xls to view our excel file we have created.


I have not mentioned the last few steps (from encoding) so that you can try it out.

Tuesday, March 29, 2011

Creating a Jasper Report using osv_memory in OpenERP 6

I first started using Jasper Report for OpenERP 5. I used a wizard( created using wizard.interface) and the data provided in the wizard is passed to the Jasper Report module and that's it. A well aligned attractive report is created in pdf, xls, html or txt format respective to the report type I provide in the wizard. This works well in openerp-client as well as openerp-web. Then I get a chance to  create the Jasper Report in OpenERP v6. It was simple to change the same jasper report module  of openerp5 to openerp6 and it works fine in openerp-client. But when I opened openrp- web and tried to create the report some unknown characters are printed in the html page and only the html report is working correctly. I spent hours in this issue and then Jabir, one of the hardcore programmer of zbeanz told me that openerp6 had stopped providing support to wizard.interface and told me to use the osv_memory to create the wizard. So I started creating Jasper Report  using osv_memory.

When creating a wizard using osv.osv_memory we need to create a model and view for the wizard, the same process we use to create a  model and view using osv.osv. The main difference inusing osv.osv and osv.osv_memory is that it osv_memory doesnt create tables in the database and osv_memory creates a table in the memory only when the wizard model is active.

If we are creating a multi-stage(multi-state) wizard then each stage depends on the value 'state' defined in the field of the wizard model.




For the state field we have to provide a default value. When we click on the wizard the view depends on this default state. We also have to define some functions that return a value to the next state or returns a value to the report. In my wizard there is only one stage in which when I click on the create button a report(Jasper report ) is created. The function is given below.

Here 'report_name' is the name of the report. This is the place where our jasper report is called. I'm providing the name of the jasper report that is already created myself. At the report module we get the 'datas' from the data. And data['form'] will give all the values provided in the wizard model. Here id and ids are not necessary. 'nodestroy' is for not destroying the wizard when creating the report.

VIEW PART 

Define the record then define form. Inside the form define group for each state.
Inside this group define all the fields that should display in this stage. Then create buttons, one for cancelling the wizard and other for creating the report.

The object type button has the name create_report which is a function defined in the wizard model. After creating the report define the actions and also specify the jasper report file location. Create the menuitem where you want to display the wizard. 

Our wizard creation is now complete and now restart our server and update our module, the new wizard will start working.


note: If anybody have any doubts or need the module I have created fell free to contact me. mail me to omalbastin@gmail.com

Wednesday, March 16, 2011

Jasper Reports- New Era of Reporting in openERP

       Before Jasper Reports, openERP depend mainly on RML reporting for creating reports and RML reports supports pdf and some other formats that are not usually used for reporting. For RML reports, it has so many disadvantages. In RML reports we can add as many rows, but the number of adding columns is fixed. But in Jasper Reports we can add any number of rows and columns. In RML reports we can't add graphics and also can't use expressions for finding values of a field using other field values. These are possible in Jasper reports. 

A customer may need excel reports and html reports. RML reports cant be used for creating excel and html reports. Jasper report does these works. In RML report we cant add graphs, charts, crosstabs, subreports etc. By using Jasper Report we can add graphs, charts, crosstabs, subreports etc. In RML reports we cant add header and footer dynamically. But in Jasper Reports space is provided  title, page header, page footer, column header, column footer, details and summary. Main advantage of using Jasper Report is that jasper report gives more control over data.

Some examples of jasper reports are given below.


Charts-->Pie Chart
The above figure is just a small part of report for user analysis.

Crosstab:
This figure is also a part of report for user analysis.


Creating Jasper Reports- Installation Guide

  Jasper Reports are mainly used for creating stylish reports other than black and white report. We can add tables, charts,crosstabs etc., in to the jasper report and also we can generate different forms of reports like pdf,xls,html etc. For creating Jasper Report, iReport plays an important role. Using ireport we can simply create Jasper Report. I'm using iReport 4.0.1.
        Jasper report and openERP integration can be done by adding jasper_reports module to our openERP server. Now Open the opneERP client. In  customization menu under the administration menu  we can find the a menu for creating jasper report template.
Pop-up window opened for creating data template
Click on it, provide the model name and depth and then click create. Then an xml file wil be generated. This file is needed for the creating Jasper report. Save the file. For now our  work in the openERP client is completed. Now go to iReport open a newblank report template. Change the datasource or create new datasource by clicking on in iReport toolbar. Add a new xml datasource and providing the path of xml file generated from the openerp client.
xml data source configuring
After creating the xml datasource change the query language by clicking on   
 
in the iReport designer bar. Then a window will open for changing the query language.

window for changing query language

These are the basic things to be done for creating a jasper report. Now we can create a module in our openerp server and start creating jasper report.

Wednesday, February 16, 2011

More on OERPScenario


       Before reading this please read my blog post on OERPScenario . Now go to our OERPScenario folder that we have downloaded from bazaar, there is a folder named features. All the user defined test cases are written in a folder inside this folder. I want to create a new scenarios for testing products, so I create a directory called product inside features. Inside the product directory a file called product.feature is created. We have to write our scenarios in this file.

       In writing the scenario we are following the Gherkin Syntax. Gherkin is the language that Cucumber and OERPScenario can understand. The source files have *.feature extension. Gherkin is a line-oriented language that uses indentation to define structure. Most lines start with a keyword.
   
       For writing the scenario I'm creating a file called product.feature inside the folder product. In writing the scenario first write <Feature:> explaining about the scenario. Then our scenario <Scenario:>.Statements following the scenario must start with any of the keyword 'Given', 'When', 'Then' or 'And'. 'And' can be used for extending 'Given' , 'When' and 'Then'.The following figure will give an idea  about each keyword.



 Before writing the feature, tags are given and this tag can be used for testing the scenarios under this feature. We can also specify tags for each scenario and by using this tags we can test the scenario that come under this tag. The statements written between Feature and Scenario are omitted by the cucumber. So we don't need to concentrate much on it. After writing the scenarios we can run the scenarios. But before running the scenario we have to compile the whole OERPScenario  folder using the command rake compile.This is just for testing the connection between openERP server and OERPScenario. If this fails then everything fails(I think so).
When I was testing I  found that the scenario written inside  the file  _init.rb  in feature>_basic>step_definions did not satisfy the conditions written inside the  _init.feature file in feature>_basic. So I changed the code . Figure of the changed code is given below
After all the changes are made, we can compile it using command rake compile. If we get all test scenarios printed in green color then we can proceed. If we get it in red color then an error is present in it.

Now we  can run our scenario.


By running this the test cases that needed for testing product are automatically generated by the cucumber. Copy these test cases and paste them to a ruby file (say product_steps.rb) created inside a directory called "step_definition" inside product.  

//not completed

OERP Scenario

       In OpenERP a time consuming task is to check all the modules by entering data to each field and checking whether the view is changing according to the data given in the field or changing according to our plan. It is also not possible to check all the information given in the fields. And also testing is important in locating a bug and fixing an error. For this purpose we are using OERPScenario. OERPScenario do the same process as the database query do. OERPScenario search for the condition data from the database satisfying the scenario statements.

      OERPScenario allowing to write business oriented test cases in OpenERP. OERPScenario is a framework based on the Cucumber Library and the Ruby Language; it uses the Ooor connector developed by Akretion to make Business Driven Development (BDD) among OpenERP. It is a tool that can execute plain-text functional descriptions as automated tests.

 OERPScenario splits tasks in two, allowing business individuals to write and describe the different test scenarios, while technical individuals code the tests.

Install and Configure OERPScenario in Ubuntu 10.04

==$sudo apt-get install ruby irb ri rdoc rubygems
==$sudo gem install cucumber -v0.6.3
==$sudo gem install rspec
==$sudo gem install parseconfig
==$sudo gem install ooor --source http://gemcutter.org
==$sudo apt-get install libopenssl-ruby
==$sudo gem install rake
==$sudo gem install htmlentities
==$# Optional for pdf output only
==$sudo gem install prawn --version 0.6.3
==$sudo gem install prawn-format --version 0.2.3
==$# Finally add '/var/lib/gems/1.8/bin' to your PATH :
==$export PATH=$PATH:/var/lib/gems/1.8/bin

Then install the "bzr" for downloading the OERPScenario. After installing "bzr" retrieve the stable branch:
==$bzr branch lp:oerpscenario

Configure

We need to instruct OERPScenario on how to connect to the OpenERP server in a
configuration file. In order to do that, copy the file « sample_base.conf » as «base.conf » into the OERPScenario folder and change the following information according to your settings:
port = 8069
user = «username »
#if it is administrator then provide "admin"
database = «mydatabasename »
password = «secrete »
host = localhost

Once it's done, go into the OERPScenario folder and run the following command to test the connection:
==$rake compile
You should then have an output confirming everything is alright.

As the tests are running under a specific user, you can always change the one that has been set-up as default (by default, all tests are running with the user defined in the config file). If you need to run the tests under another user, edit the following file:
« /features/_basic/_init.feature » and change the sentence according to your needs

The configuration is done!

Using tags

 The tag system implemented in Cucumber is very powerful and allows you to run complex test cases according to your own OpenERP settings. Basically, the following tag types are present in OERPScenario:
• Bazaar branch (like @addons), which tests all existing modules in that branch;
• Modules (likes @account), which tests everything having to do with a specific
   module;
• Bugs related (like @bug452854), which launches the related scenario;
• Business oriented (like @invoicing), which runs every related scenario;
• System (like @init, @demo), which executes a set of tasks in OpenERP.

Launching the OERPScenario
 You can launch the test suite from the OERPScenario folder with the following commands:
cucumber feature --tag=@account
This will execute all scenarios related to the OpenERP account module (notice that tags can be set on features or scenarios). The feature tags are true for all included scenarios, where the scenario tags are true only for the current scenario.


Friday, January 28, 2011

Wizards and Links in openERP

    Wizard describe interaction sequences between the client and the server. Wizards provide a form for the client to complete it. Then client can sent the completed form back to the server and server executes some function and return another form to the user or will save the information provided by the client in the database.

To define a wizard, you have to create a class inheriting from wizard.interface and instantiate it. Each wizard must have a unique name. The wizard must define a dictionary named states which defines all its steps. 


 The ‘states’ dictionary define all the states of the wizard.In this example; init, test1, test2 and test3.
 A wizard always starts in the init state and ends in the end state. A state defines two things: a list of actions and a result.
  • List of actions: Each state of a wizard defines a list of actions which are executed when the wizard enters the state. This list can be empty.
  • Result : Specifies the next state/step. The wizard will continue to the next state and if the next state given is "end" then the wizard will stop.

    • The result have different types. They are form, action, choice, print etc.
    • If the type is form then the client will get a form view. 
    • If the type is action then a user_defined_function(action) will be executed and the resulting action will be performed.
    • If the type is choice then also a function will be executed. This function will return the name of the next state of the wizard.
    • If the type is print then we have to provide the name of the report to be printed.

  The type=form indicate that this step is a dialog to the client. We have to provide the fields and the xml view part inside our wizard.


Each step/state of a wizard can have several buttons.Those are located on the bottom right of the dialog box. The list of buttons for each step of the wizard is declared in the state key of its result dictionary.



  • the next step name (determine which state will be next)



  • the button string (to display for the client)



  • the gtk stock item without the stock prefix



  • Dynamic Forms and Fields in Wizard

    Here I create a function "_get_defaults" which returns a dictionary  self.states['test']['result']. Here I'm dynamically providing values to the dictionary "states". The function "_get_defaults" given below.

     The function gets the limit of fields and forms from the previous wizard state using its field name mainly I'm assuming that the data to be added to the field is a character field and I had created the fields according to this assumption. Try yourself and find out its output.


    The XML file

            After completing the code in the wizard we have to add the wizard to the view part. We have to add the following statement to the xml file for getting the wizard in the client interface

    --->id--> id is just a unique name.
    --->model-->model is the place where we want to display the wizard.
    --->name-->name given here is passed in the instance of the class test_wizard_college

    By adding the following statement inside our student view a new button will be created inside . 

     If we want the wizard as a menu item then


     here action is the wizard id, and parent is given as the root menu


     LINKS
            A link is different from wizard. A link creates a connection to a model from a model. To add a link from one model to another model we have to use act_window

    Here res_model is the resulting model in which the link will take us to this model, src_model is the source model in which where the link will be shown.

    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.