This question has been flagged
4 Replies
12326 Views

Hello,

please! I need your help. I want to add a field "weight_tot" in Account.invoice I do like this:

from openerp.osv import fields, osv

class account_invoice(osv.osv):
    _inherit = 'account.invoice'

    _columns = {
        'weight_tot': fields.float('Sum TEST ',help='Sum weight'),
    }
account_invoice()

I want now to read "account.invoce.line" line by line and add the weight fields to insert the result in "weight_tot" help me please

Avatar
Discard
Best Answer

Hi, you can use fields.function : doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/field_type.html/#functional-fields

Hope this helps.

Avatar
Discard
Best Answer

use a functional field

Avatar
Discard
Author

Okay, thank you, After creating my fields, I try in my class that inherits from "account.invoice.line" to this:

    class account_invoice_line(osv.osv):
    _inherit = 'account.invoice.line'
    # ajout des champs Poid et poid total   
    def _weight_per_line_get_fnc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
        res = {}

        for line in self.browse(cr, uid, ids):
            res[line.id] = line.weight * line.quantity
        return res

    _columns = {
        'weight': fields.float('Poids', help="Le poids du produit."),
        'weight_per_line': fields.function(_weight
Author Best Answer

Okay, thank you, After creating my fields, I try in my class that inherits from "account.invoice.line" to this:

    class account_invoice_line(osv.osv):
    _inherit = 'account.invoice.line'
    # ajout des champs Poid et poid total   
    def _weight_per_line_get_fnc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
        res = {}

        for line in self.browse(cr, uid, ids):
            res[line.id] = line.weight * line.quantity
        return res

    _columns = {
        'weight': fields.float('Poids', help="Le poids du produit."),
        'weight_per_line': fields.function(_weight_per_line_get_fnc, type='float', string='Poids par ligne'),
    }

    # ---NOT WORK-----
    def _amount_weight(self, cr, uid, ids, name, args, context=None):
        tot= {}
        for invoice in self.browse(cr, uid, ids, context=context):
            tot[invoice.id] = {
                'amount_weight': 0.0,
            }
            for line in invoice.invoice_line:
                tot[invoice.id]['amount_weight'] += line.weight
        return tot

        _columns={
            'weight_tot_weight': fields.function(_amount_weight, type='float', string='Poids Total'),
            'weight_per_line  ': fields.function(_weight_per_line_get_fnc, type='float', string='Poids par ligne'),
        }

    # Ici on réécrit la fonction "product_id_change" pour qu'elle remplisse le champ "weight" (créé ci-dessus) avec le poids
    # de l'article qui sera sélectionné dans la ligne de commande
    def product_id_change(self, cr, uid, ids, product, uom_id, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, currency_id=False, context=None, company_id=None):
        res=super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom_id, qty, name, type, partner_id, fposition_id=fposition_id, price_unit=price_unit, currency_id=currency_id, context=context, company_id=company_id)

        # On va rechercher les données dans la table product_product
        context_partner = {'partner_id': partner_id}
        product_obj = self.pool.get('product.product')
        product_obj = product_obj.browse(cr, uid, product, context=context_partner) 

        if product_obj.id:
            res['value']['weight'] = product_obj.weight_net
        return res



account_invoice_line()
Avatar
Discard
Author Best Answer

Okay, thank you, After creating my fields, I try in my class that inherits from "account.invoice.line" to this:

    class account_invoice_line(osv.osv):
    _inherit = 'account.invoice.line'
    # ajout des champs Poid et poid total   
    def _weight_per_line_get_fnc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
        res = {}

        for line in self.browse(cr, uid, ids):
            res[line.id] = line.weight * line.quantity
        return res

    _columns = {
        'weight': fields.float('Poids', help="Le poids du produit."),
        'weight_per_line': fields.function(_weight
Avatar
Discard