Search code examples
pythonodooodoo-12odoo-13

Dynamically create and update table rows and columns in odoo qweb report with python


enter image description here

I have this table above as the final output of my qweb report. This table should be dynamically generated.

What kind of data structure would you need to represent this data such that the value of the table cells which are currently zero is equivalent to the value after splitting a variable.

I want such that if you have a variable m-x = 20, the table cell at col m, row x will be equal to 20 and so on. I f no such combination then the table cell is equals to 0.

Please let me know if this is making sense and possibly any clarification. Thank you!

This is what I am doing right now. Not sure if I am in the right direction. The report is being printed from the manufacturing model. I search though the product moves in inventory looking for all moves whose origin or reference matches a manufacturing order's name.

I then take the lot_id's name, join the keys in the dictionary and check whether they match and there I update the values.

size = {
            'u/s': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0}
            'w': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0}
            'bhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0}
        }
for output in self.env['stock.move.line'].search(['|', ('origin', '=', docs.name), ('reference', '=', docs.name)]):
    lot_id = output.lot_id.name
        for i in size:
            for j in size[i]:
                if (j+'-'+i) == lot_id:
                   i[j] = output.qty_done

Solution

  • I got this working by using dictionaries.

    Python code:

    size = {
                'u/s': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'w': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'bhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'bhp': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'rhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'rhp': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'yhl': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'yhp': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                'stains': {'mp/sup': 0, 'tr': 0, 'd2':0,'v':0,'v1':0,'d/rej':0},
                
            }
            
    for output in self.env['stock.move.line'].search(['|', ('origin', '=', docs.name), ('reference', '=', docs.name)]):
        lot_id = output.lot_id.name
        if lot_id:
           for i in size:
               for j in size[i]:
                   if (f"{j}-{i}") == lot_id.lower():
                       size[i][j] = output.qty_done
    

    Xml code:

    <thead>
        <tr>
          <th>SIZE</th>
          <th>MP/SUP</th>
          <th>TR</th>
          <th>D2</th>
          <th>V</th>
          <th>V1</th>
          <th>D/REJ</th>
          <th>TOTAL</th>
          <th>PERCENTAGE (%)</th>
        </tr>
      </thead>
      <tbody>
         <t t-foreach="size" t-as="i">
             <tr>
                <th>
                   <t t-esc="i.upper()" />
                </th>
             <t t-foreach="size[i]" t-as="j">
                <td>
                   <t t-esc="size[i][j]" />
                </td>
             </t>
           </tr>
         </t>
     </tbody>
    

    Ps. Pandas seems like a really quick way to get things done.