Odoo Help

0

python code in rml to generate empty table dynamically

By
GonG
on 3/1/14, 1:41 PM 2,341 views

In my modified purchase_requisition report, I want the table of line_ids to contains 8 table row, so I try to add an empty table according to the number of line_ids. For example, if the line_ids contain only one product, therefore, I have to add more 7 empty table below. This way, I can fix the table height up to 8 rows.

The procedure I try is write a python method to generate an empty table dynamically. However, it doesn't generate the empty table that I want. Please help me. Here's my code:

requisition.py

class requisition(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(requisition, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'empty_table': self.empty_table,
        })

    def empty_table(self, line_id):
        if len(line_id) < 8:
            for i in range(8 - len(line_id)):
                table += """<blockTable rowHeights="1cm" colWidths="1.5cm,9.5cm,3.0cm,1.5cm,1.5cm" style="table_info">
                                <tr>
                                  <td><para style="table_info_num"> </para></td>
                                  <td><para style="table_info_text"> </para></td>
                                  <td><para style="table_info_num"> </para></td>
                                  <td><para style="table_info_num"> </para></td>
                                  <td><para style="table_info_num"> </para></td>
                                </tr>
                            </blockTable>"""
            return table

purchase_requisition.rml

<section>
    [[ repeatIn(requisition.line_ids,'line_ids') ]]
      <blockTable rowHeights="1cm" colWidths="1.5cm,9.5cm,3.0cm,1.5cm,1.5cm" style="table_info">
        <tr>
          <td><para style="table_info_num"><seq id="number"/></para></td>
          <td><para style="table_info_text">[[ line_ids.product_id.name ]]</para></td>
          <td><para style="table_info_num">[[ repeatIn(line_ids.partner_ids,'partner') ]] [[ partner.name ]]</para></td>
          <td><para style="table_info_num">[[ line_ids.product_qty ]]</para></td>
          <td><para style="table_info_num">[[ line_ids.product_uom_id.name ]]</para></td>
        </tr>
      </blockTable>
    </section>

    [[ empty_table(requisition.line_ids) ]]
0
herbert6453
On 3/4/14, 10:14 AM

You could try output a second static table use repeatIn over a list with 8- len(requisition.line_ids) items

Let me know If you find a solution for dynamic rml.

<blockTable rowHeights="1cm" colWidths="1.5cm,9.5cm,3.0cm,1.5cm,1.5cm" style="table_info">
 [[ repeatIn( myList(8,requisition.line_ids),'l') ]]
   <tr>
             <td><para style="table_info_num"> </para></td>
            <td><para style="table_info_text"> </para></td>
            <td><para style="table_info_num"> </para></td>
            <td><para style="table_info_num"> </para></td>
             <td><para style="table_info_num"> </para></td>
   </tr>

</blocktable>"""


def myList(self,max,moveline):
    i=0
    l=[]
    while i<max-len(moveline):
        l.append(i)
        i=i+1
    return l
0
GonG
On 3/4/14, 11:40 AM

Thanks for your reply. My solution is the same as yours with a little condition. Here's my code:

purchase_requisition.rml

<section>
    [[ repeatIn(empty_table(requisition.line_ids), 'l') ]]
      <blockTable rowHeights="1cm" colWidths="1.5cm,9.5cm,3.0cm,1.5cm,1.5cm" style="table_info">
        <tr>
          <td><para style="table_info_num"> </para></td>
          <td><para style="table_info_text"> </para></td>
          <td><para style="table_info_num"> </para></td>
          <td><para style="table_info_num"> </para></td>
          <td><para style="table_info_num"> </para></td>
        </tr>
      </blockTable>
    </section>

requisition.py

def empty_table(self, line_id):
        n = len(line_id) % 8
        if n != 0:
            return [{'a':i} for i in range(8 - n)]
        else:
            return {'a':1}

As I want table to contains 8 rows. My solution leads to some scenarios.

One is if the table has 1 product, therefore my table will include 7 empty rows.

Second is if the table contains 12 products, therefore my table will split into 2 pages, first page with 8 rows and the second page will be 4 products include 4 empty rows.

There is one drawbacks here, if my table contains exactly 8 products, then it will be 2 pages with 1 empty row in the second page, which leads to me to print only 1 page. The problem is because this solution, the rml file will generate at least 1 table for sure.

This is the best I can do right now. Do you have any suggestions to overcome this drawback?

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)

Stats

Asked: 3/1/14, 1:41 PM
Seen: 2341 times
Last updated: 3/16/15, 8:10 AM