pythonodoo-10

NameError: global name 'amount_total' is not defined (Odoo10/Pyhton)


I fixed percentage from margin / cost to margin / amount_total (in model invoice), but get the below error:

NameError: global name 'amount_total' is not defined

The full code is here:

class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    margin_amount = fields.Char(compute='_get_average_margin_percentage', string='Margin Amount')
    margin_percentage = fields.Char(compute='_get_average_margin_percentage', string='Margin Percentage')

    @api.one
    @api.depends('amount_total','invoice_line_ids','invoice_line_ids.quantity','invoice_line_ids.price_unit', 'invoice_line_ids.discount')
    def _get_average_margin_percentage(self):
        sale_price = discount = cost = margin_amount = 0.0
        line_cost = line_margin_amount = margin_percentage = 0.0
        for record in self:
            if record.invoice_line_ids:
                for line in record.invoice_line_ids:
                    sale_price = line.price_unit * line.quantity
                    discount = (sale_price * line.discount)/100
                    cost = line.product_id.standard_price * line.quantity
                    line_cost += cost
                    margin_amount = (sale_price - discount) - cost
                    line_margin_amount += margin_amount
                    if line_cost:
                        margin_percentage = (line_margin_amount / amount_total) * 100
                    else:
                        margin_percentage = 100
            record.margin_amount = line_margin_amount
            record.margin_percentage = str(round(margin_percentage,2)) + '%

error Log

Traceback\ (most\ recent\ call\ last): \ \ File
"/opt/odoo/odoo/http.py",\ line\ 642,\ in\ _handle_exception \ \ \
return\ super(JsonRequest,\ self)._handle_exception(exception)
\ File\ "/opt/odoo/odoo/http.py",\ line\ 684,\ in\ dispatch \ \ \
result\ =\ self._call_function(**self.params) \ \ File
"/opt/odoo/odoo/http.py",\ line\ 334,\ in\ call_function \ \ \
return\ checked_call(self.db,\ *args,\ **kwargs) \ \ File
"/opt/odoo/odoo/service/model.py",\ line\ 101,\ in\ wrapper \ \ \
return\ f(dbname,\ *args,\ **kwargs) \ \ File
"/opt/odoo/odoo/http.py",\ line\ 327,\ in\ checked_call \ \ \
result\ =\ self.endpoint(*a,\ **kw) \ \ File
"/opt/odoo/odoo/http.py",\ line\ 942,\ in\ call \ \ \ \ return
self.method(*args,\ **kw) \ \ File\ "/opt/odoo/odoo/http.py",
line\ 507,\ in\ response_wrap \ \ \ \ response\ =\ f(*args,
**kw) \ \ File\ "/opt/odoo/addons/web/controllers/main.py",\ line
837,\ in\ search_read \ \ \ \ return\ self.do_search_read(model,
fields,\ offset,\ limit,\ domain,\ sort) \ \ File
"/opt/odoo/addons/web/controllers/main.py",\ line\ 859,\ in
do_search_read \ \ \ \ offset=offset\ or\ 0,\ limit=limit\ or\ False,
order=sort\ or\ False)your text \ \ File
"/opt/odoo/odoo/models.py",\ lineyour text\ 4757,\ in\ search_read \ \ \ \ result\ =\ records.read(fields) \ \ File
"/opt/odoo/odoo/models.py",\ line\ 3045,\ in\ read \ \ \
data[record][name]\ =\ convert(record[name],\ record,
use_name_get) \ \ File\ "/opt/odoo/odoo/models.py",\ line\ 5272,
in\ getitem \ \ \ \ return\ self.fields[key].get
(self,
type(self)) \ \ File\ "/opt/odoo/odoo/fields.py",\ line\ 931,\ in
get \ \ \ \ self.determine_value(record) \ \ File\ "/opt/odoo/odoo/fields.py",\ line\ 1045,\ in\ determine_value \ \ \
self.compute_value(recs) \ \ File\ "/opt/odoo/odoo/fields.py",
line\ 997,\ in\ compute_value \ \ \ \ self._compute_value(records) \ \ File\ "/opt/odoo/odoo/fields.py",\ line\ 988,\ in\ _compute_value \ \ \ \ getattr(records,\ self.compute)() \ \ File\ "",\ line
2,\ in\ _get_average_margin_percentage \ \ File
"/opt/odoo/odoo/api.py",\ line\ 369,\ in\ loop \ \ \ \ result\ =
[method(rec,\ *args,\ **kwargs)\ for\ rec\ in\ self] \ \ File
"/opt/odoo/addons/customer_invoice_margin/models/account_invoice.py", line 35, in _get_average_margin_percentage amount_total NameError: global name 'amount_total' is not defined

define amount_total field from table/model account_invoice


Solution

  • Add record. to amount_total

    margin_percentage = (line_margin_amount / record.amount_total) * 100
    

    Thanks, Kenly