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.