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

Modify one field in invoice reports, using description instead of name for tax type.

By
E.M.
on 9/22/15, 6:07 PM 890 views

Hi, I am trying to modify one field in invoice reports.

			<div class="row" t-if="o.tax_line">
				<div class="col-xs-6">
					<table class="table table-condensed">
						<thead>
							<tr>
								<th>Invoice detail</th>
								<th class="text-right">Base</th>
								<th class="text-right">Amount</th>
							</tr>
						</thead>
						<tbody>
							<tr t-foreach="o.tax_line" t-as="t">
							        <td><span t-field="t.name"/></td>
								<td class="text-right">
									<span t-field="t.base" t-field-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: &quot;o.currency_id&quot;}"/>
								</td>
								<td class="text-right">
									<span t-field="t.amount" t-field-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: &quot;o.currency_id&quot;}"/>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>

Instead of tax name (<td><span t-field="t.name"/></td>) I would like to use tax description, but unfortunately description is not available in t object.

Is it possible to look for tax description from tax name? How and where should I start looking?

Thanks 

################################### EDITED AFTER AXEL'S ANSWER

Following Axel's answer (thanks a lot, that it is a really comprehensive answer which really helps to understand how to write a parser), I am trying to fully understand and adapt the code to my specific case as I guess it is a not so unusual need. 


My report is being defined in a custom module with the following structure (so far, so good):

custom_reports
¦
¦___ __init__.py
¦
¦___ __openerp__.py
¦
¦___ xml
¦
¦___ custom_reports.xml


In custom_reports.xml I define the new custom invoice report (again, so far, so good):

...
<report
id="report_invoice_custom"
model="account.invoice"
string="Custom invoice report"
report_type="qweb-pdf"
name="custom_reports.invoice_custom"
attachment_use="False"
file="custom_reports.invoice_custom"
attachment = "(object.state in ('open','paid')) and ('F_'+(object.number or '').replace('/','')+'.pdf')"
/>
...


I am adding the new parser to this module (which makes sense to have everything in the same module) and specifying the proper import in __init__,py so invoice_parser is read:

custom_reports
¦
¦___ __init__.py
¦
¦___ __openerp__.py
¦
¦___ invoice_parser.py <<-- NEW PARSER
¦
¦___ xml
¦
¦___ custom_reports.xml

 

I add the code from Axel, restart server and upgrade module but still select_value can't be evaluated:


Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/controllers/main.py", line 121, in report_download
response = self.report_routes(reportname, docids=docids, converter='pdf')
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 403, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/controllers/main.py", line 65, in report_routes
pdf = report_obj.get_pdf(cr, uid, docids, reportname, data=options_data, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 191, in get_pdf
html = self.get_html(cr, uid, ids, report_name, data=data, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 176, in get_html
return self.render(cr, uid, [], report.report_name, docargs, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 152, in render
return view_obj.render(cr, uid, template, values, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_ui_view.py", line 1029, in render
return self.pool[engine].render(cr, uid, id_or_xml_id, qcontext, loader=loader, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 261, in render
return self.render_node(self.get_template(id_or_xml_id, qwebcontext), qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 298, in render_node
result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 436, in render_tag_call
d[0] = self.render_element(element, template_attributes, generated_attributes, d)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 298, in render_node
result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 422, in render_tag_foreach
ru.append(self.render_element(element, template_attributes, generated_attributes, copy_qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 298, in render_node
result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 367, in render_tag_raw
inner = self.eval_str(template_attributes["raw"], qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 213, in eval_str
val = self.eval(expr, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 205, in eval
raise_qweb_exception(message="Could not evaluate expression %r" % expr, expression=expr, template=template)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 202, in eval
return qwebcontext.safe_eval(expr)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 88, in safe_eval
return eval(expr, None, locals_dict, nocopy=True, locals_builtins=True)
File "/usr/lib/python2.7/dist-packages/openerp/tools/safe_eval.py", line 314, in safe_eval
return eval(c, globals_dict, locals_dict)
File "", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 135, in translate_doc
return self.translate_doc(cr, uid, doc_id, model, lang_field, template, values, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 115, in translate_doc
return self.pool['ir.ui.view'].render(cr, uid, template, qcontext, context=ctx)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_ui_view.py", line 1029, in render
return self.pool[engine].render(cr, uid, id_or_xml_id, qcontext, loader=loader, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 261, in render
return self.render_node(self.get_template(id_or_xml_id, qwebcontext), qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 298, in render_node
result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 431, in render_tag_if
return self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 298, in render_node
result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 422, in render_tag_foreach
ru.append(self.render_element(element, template_attributes, generated_attributes, copy_qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 300, in render_node
result = self.render_element(element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 321, in render_element
g_inner.append(self.render_node(current_node, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 298, in render_node
result = self._render_tag[t_render](self, element, template_attributes, generated_attributes, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 373, in render_tag_esc
inner = widget.format(template_attributes['esc'], options, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 992, in format
return escape(self._format(inner, options, qwebcontext))
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 989, in _format
return self.pool['ir.qweb'].eval_str(inner, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 213, in eval_str
val = self.eval(expr, qwebcontext)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 205, in eval
raise_qweb_exception(message="Could not evaluate expression %r" % expr, expression=expr, template=template)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 202, in eval
return qwebcontext.safe_eval(expr)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 88, in safe_eval
return eval(expr, None, locals_dict, nocopy=True, locals_builtins=True)
File "/usr/lib/python2.7/dist-packages/openerp/tools/safe_eval.py", line 314, in safe_eval
return eval(c, globals_dict, locals_dict)
File "", line 1, in <module>
QWebException: ""'NoneType' object is not callable" while evaluating
'select_value(t.name)'" while evaluating
"translate_doc(doc_id, doc_model, 'partner_id.lang', 'custom_reports.invoice_custom_document')"

 


Your parser is not be used by Odoo.
Check the answer of https://www.odoo.com/nl_NL/forum/help-1/question/how-to-register-a-parser-for-a-qweb-report-91416
To see how you can define it to be picked

Axel Mendoza
on 9/26/15, 3:54 PM

Done! thanks Axel!

E.M.
on 9/27/15, 7:31 AM
2

Axel Mendoza

--Axel Mendoza--
10205
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 9/22/15, 7:39 PM

Hi E.M

At first glance you couldn't because the field that you wan't is at the model account.tax but the model that you are iterating is account.invoice.tax and the code that compute the values for create the invoice taxes don't use that field.

To do it you have two options:

You could create a parser to find the original account.tax record used to create your account.invoice.tax and return the description

Or you could extend in account.invoice.tax model:

 @api.v8
def compute(self, invoice):

to copy your field. This efforts seem to much for the result ... evaluate yourself

=============================================================

How to create a parser method to allow you to search the original tax description

=============================================================

from openerp.report import report_sxw
from openerp.osv import osv
from openerp import api

class invoice_parser(report_sxw.rml_parse):

def __init__(self, cr, uid, name, context):
super(invoice_parser, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'select_value': self._selection_value,
})

def _selection_value(self, value):
if value:
tax_pool = self.pool.get('account.tax')
tax_ids = tax_pool.search(self.cr, self.uid, [('name', '=', value)])
if tax_ids:
tax_id = tax_pool.browse(self.cr, self.uid, tax_ids[0])
return self._translate(tax_id.description)
return value

class report_custom_invoice_parser(osv.AbstractModel):
_name = 'report.module.invoice.wrapper'
_inherit = 'report.abstract_report'
_template = 'module.invoice_template'
_wrapped_report_class = invoice_parser

note that you need to change the name of the report template module.invoice_template

next in your report template you could do this:

<td><span t-esc="select_value(t.name)"></td>



I think I get the idea on extending account.invoice.tax model, but I am not sure on what do you mean with "create a parser to find the original account.tax record and return the description". The result/benefit might not be obvious but unfortunately Spanish Chart of Accounts are configured so description is the right field to use.

E.M.
on 9/23/15, 9:12 AM

The idea of the parser is to used in the report, to search for the value of the tax description on account.tax based on the invoice tax value you have, something like a translator for your case, but used directly in the report without change any other behavior or create any other field to store the tax description

Axel Mendoza
on 9/23/15, 12:19 PM

Is it difficult to achieve it? Could you point a basic example or basic steps? If I have understood it properly is something that you do in the QWeb report code.

E.M.
on 9/23/15, 5:22 PM

It's not qweb code, in qweb you only invoke a function in the parser(python class registered for the specific report) passing the t.name and in that function you search for the original account.tax with that value and return the corresponding description field value

Axel Mendoza
on 9/23/15, 5:38 PM

Thanks Axel, could you please help me with an example of function call from the QWeb report?. I can manage the python part, but not sure on how to call that parsing function from the report. Thank you very much indeed.

E.M.
on 9/24/15, 4:53 AM

example in the edited answer

Axel Mendoza
on 9/25/15, 9:46 AM
solved?
already?
Axel Mendoza
on 9/26/15, 1:58 AM

Thanks a lot for such comprehensive answer, I do really appreacite your time and knowledge sharing. I have spent quality time to understand your code but still miss how to import my custom report so the parser can find it. See my edited initial answer for details in case you can help further. Thanks.

E.M.
on 9/26/15, 12:24 PM

If, it was good, could you upvote?

Axel Mendoza
on 9/26/15, 1:04 PM

I finally manage to understand that the only thing to be modified in your code (correct me if I am wrong) is: _template = 'module.invoice_template'. I have tried with all the templates that apply to my custom report, but Odoo can not find the parser: QWebException: ""'NoneType' object is not callable" while evaluating 'select_value(t.name)'" while evaluating "translate_doc(doc_id, doc_model, 'partner_id.lang', 'custom_reports.invoice_custom_document')"

E.M.
on 9/26/15, 1:10 PM
1

Parikshit

--Parikshit--
939
| 6 3 8
Ahmedabad, India
--Parikshit--

Working as an OpenERP/Odoo developer both experience in web and addons. you can contact me at:

E-mail : parikshitvaghasiya@gmail.com

Blog : parikshitvaghasiya.blogspot.in

Stackoverflow : http://stackoverflow.com/users/5089106/parikshit-vaghasiya

Parikshit
On 9/23/15, 1:30 AM

Hello  E.M,

you can change this name to Description like this :

<td><span t-field="t.description"/></td>

hope this will help you !!

No, that does not work, as t has no description value.

E.M.
on 9/23/15, 3:42 AM

i have doing this in my local . its working fine. its take description value . t has object of account.tax so it take description value.

Parikshit
on 9/23/15, 3:44 AM

t is not account.tax -see Axel's comment-. In invoice report, there is another region where you can use t.description, but it is not the case here.

E.M.
on 9/23/15, 9:08 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

1 follower(s)

Stats

Asked: 9/22/15, 6:07 PM
Seen: 890 times
Last updated: 9/27/15, 7:31 AM