Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
6113 Vistas

Hey everybody

Today I'm trying to build my own Odoo templates but I'm stuck on one thing.
So far I managed to get the fields name, product_uom, product_uom_quantity price_unit etc from the model sale.order
But now I need to get the tax amount from the database, which is stored in the model sale.order.line under the field tax_id which is a many2many field.

How exactly can I get the tax_id from the model sale.order.line when I'm using the model sale.order? So far I have the following code:

class sale_order(osv.Model):
    _inherit = 'sale.order'

    _columns = {
        'is_template': fields.boolean('Template'),
        'template_id': fields.many2one('sale.order', 'Offer', domain=[('is_template', '=', True)]),
    }

    def onchange_template(self, cr, uid, ids, template=False, partner_id=False, pricelist_id=False, fiscal_position=False):
        line_obj = self.pool.get('sale.order.line')
        result = {'order_line': []}
        lines = []
 
        if not template:
            return {'value': result}

        if not partner_id:
            raise osv.except_osv(_('No Customer Defined!'), _('Before choosing a template,\n select a customer in the template form.'))

        template = self.browse(cr, uid, template)
        order_lines = template.order_line
        for line in order_lines: 
            vals = line_obj.product_id_change(cr, uid, [],
                pricelist = pricelist_id,
                product = line.product_id and line.product_id.id or False,
                qty = 0.0,
                uom = False,
                qty_uos = 0.0,
                uos = False,
                name = '',
                partner_id = partner_id,
                lang = False,
                update_tax = True,
                date_order = False,
                packaging = False,
                fiscal_position = fiscal_position,
                flag = False)
            vals['value']['discount'] = line.discount
            vals['value']['product_id'] = line.product_id and line.product_id.id or False
            vals['value']['name'] = line.name
            vals['value']['state'] = 'draft'
            vals['value']['product_uom_qty'] = line.product_uom_qty
            vals['value']['product_uom'] = line.product_uom and line.product_uom.id or False
            vals['value']['price_unit'] = line.price_unit
            #vals['value']['tax_id'] = line.tax_id
            lines.append(vals['value'])
        result['order_line'] = lines

Any information or help is much appreciated!
Yenthe

Avatar
Descartar
Mejor respuesta

you can get many2many value using the below code

vals['value']['tax_id'] = [(6, 0, [x.id for x in line.tax_id])]

 

Avatar
Descartar
Autor

Awesome! This does exactly what I wanted it to do! Could you please explain me what every value does after the = ? = [(6, 0, [x.id for x in line.tax_id])] I understand that the for gets every id for every record but what about the 6,0? Where does this come from?

please see this document https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/methods/ For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics (6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs) Example: [(6, 0, [8, 5, 6, 4])] sets the many2many to ids [8, 5, 6, 4]

Publicaciones relacionadas Respuestas Vistas Actividad
1
mar 15
7252
1
abr 24
6405
1
jul 23
2325
4
jun 22
20811
4
nov 20
6533