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

Error Field is accesed before being computed.

By
Artur Bertram
on 8/12/14, 11:16 AM 1,671 views

I've written a custom modul which adds percent discount and fix discount to the invoice. Now i have rewrite it, so that it uses the new api, but every time i chage the selection field, I get the error "Warning: Field account.invoice.amount_untaxed is accessed before being computed.". There is an onchange event on that selection field and if I remove it, the error isn't occurring.

To me it looks like the onchange event is trying to get the compute field before it is set.

Code following.

modul.py

import itertools
from lxml import etree

from openerp import models, fields, api, _
from openerp.exceptions import except_orm, Warning, RedirectWarning
import openerp.addons.decimal_precision as dp


class eq_account_invoice(models.Model):
    _inherit = ['account.invoice']
    _name= "account.invoice"
    
    @api.one
    @api.depends('invoice_line.price_subtotal', 'tax_line.amount',)
    def _compute_amount(self):
        if self.eq_percent_discount:
            self.amount_tax = sum(line.amount for line in self.tax_line) * (1 - ((self.eq_percent_discount or 0) / 100))
            self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
            self.eq_amount_discount = self.amount_untaxed * ((self.eq_percent_discount or 0) / 100)
            self.eq_amount_net = self.amount_untaxed - self.eq_amount_discount
            self.amount_total = self.eq_amount_net + self.amount_tax
        elif self.eq_fix_discount:
            self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
            self.amount_discount = eq_fix_discount
            self.eq_amount_net = self.amount_untaxed - self.eq_amount_discount
            self.amount_tax = sum(line.amount for line in self.tax_line) + self.eq_amount_net / self.amount_untaxed
            self.amount_total = self.eq_amount_net + self.amount_tax
        else:
            self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
            self.amount_tax = sum(line.amount for line in self.tax_line)
            self.amount_total = self.amount_untaxed + self.amount_tax
        
    eq_discount_type = fields.Selection((('1', 'Percent'), ('2', 'Fixed'), ), 'Type', readonly=True, states={'draft': [('readonly', False)]})
    eq_percent_discount = fields.Float("Percentage", readonly=True, states={'draft': [('readonly', False)]})
    eq_fix_discount = fields.Float("Fixed", readonly=True, states={'draft': [('readonly', False)]})
    
    eq_amount_discount = fields.Float(string='Discount', digits=dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_amount')
    eq_amount_net = fields.Float(string='Net Amount', digits=dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_amount')
    amount_untaxed = fields.Float(string='Subtotal', digits=dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_amount', track_visibility='always')
    amount_tax = fields.Float(string='Tax', digits=dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_amount')
    amount_total = fields.Float(string='Total', digits=dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_amount')

    @api.one
    def on_change_discount_type(self):
        result = {'value': {
            'eq_fix_discount': False,
            'eq_percent_discount': False
        }}
        return result

modul_view.xml

<openerp>
    <data>

        <record id="eq_invoice_record_additional_discount" model="ir.ui.view">
            <field name="name">account.invoice.form</field>
            <field name="model">account.invoice</field>
            <field name="type">form</field>
            <field name="inherit_id" ref="account.invoice_form"/>
            <field name="arch" type="xml">
                <xpath expr="//page[@string='Invoice Lines']//group[@class='oe_subtotal_footer oe_right']" position="replace">
                    <group colspan="2" col="4">
                        <group colspan="2">
                            <h3>
                            <label string="Discount" class="oe_inline"/>
                            </h3>
                            <newline/>
                            <field name="eq_discount_type" style="width:50%%" on_change="on_change_discount_type()"/>
                            <field name="eq_percent_discount" style="width:25%%" attrs="{'invisible':[('eq_discount_type', '!=', '1')]}"/>
                            <field name="eq_fix_discount" style="width:50%%" widget='monetary' options="{'currency_field': 'currency_id'}" attrs="{'invisible':[('eq_discount_type', '!=', '2')]}"/>
                        </group>
                        <group class="oe_subtotal_footer oe_right" colspan="2" name="sale_total">
                            <field name="amount_untaxed" widget='monetary' options="{'currency_field': 'currency_id'}"/>
                            <field name="eq_amount_discount" widget='monetary' options="{'currency_field': 'currency_id'}"/>
                            <field name="eq_amount_net" widget='monetary' options="{'currency_field': 'currency_id'}"/>
                            <field name="amount_tax" widget='monetary' options="{'currency_field': 'currency_id'}"/>
                            <div class="oe_subtotal_footer_separator oe_inline">
                                <label for="eq_amount_total"/>
                                <button name="button_reset_taxes" states="draft,proforma2" string="(update)" class="oe_link oe_edit_only" type="object" help="Recompute taxes and total"/>
                            </div>
                            <field name="amount_total" nolabel="1" class="oe_subtotal_footer_separator" widget='monetary' options="{'currency_field': 'currency_id'}"/>
                            <field name="residual" groups="account.group_account_user" widget="monetary" options="{'currency_field': 'currency_id'}"/>
                            <field name="reconciled" invisible="1"/>
                        </group>
                    </group>
                </xpath>
            </field>
        </record>
    </data>
</openerp>

Am i doing something wrong, or is it just an bug in the new api?

Even I had faced same issue, i renamed the function field, and initialized all values to Zero first.. then proceeded with logic

deep
on 10/16/14, 6:57 AM

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

2 follower(s)

Stats

Asked: 8/12/14, 11:16 AM
Seen: 1671 times
Last updated: 3/16/15, 8:10 AM