Odoo Help

Welcome!

This community 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.

0

Add sum Field function

By
Anne-Sylvie Burri
on 11/4/13, 9:50 AM 2,910 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

1
Torsten Francke
On 11/4/13, 10:01 AM

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
J.-M. Burri - Bureau ing.-conseils informatique, Anne-Sylvie Burri
on 11/4/13, 10:23 AM
1
Jaakko Komulainen
On 11/4/13, 10:00 AM

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.

0
Anne-Sylvie Burri
On 11/4/13, 10:22 AM

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()
0
Anne-Sylvie Burri
On 11/4/13, 10:35 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 11/4/13, 9:50 AM
Seen: 2910 times
Last updated: 3/16/15, 8:10 AM