Reputation: 10189
I need to show a string in the header of several reports which must change depending on the report which is being printed.
By now, I had done this:
<template id="external_layout_header" inherit_id="report.external_layout_header">
...
<t t-if="o._table=='sale_order'">
... Print here what I need to show in sale order reports ...
</t>
...
</template>
It worked well for me, but now, the string does not depend on the model/table, but on the printed report.
I have a model which has two different reports to print. If one is printed, I have to show 'X' in the header, if the other one is printed, I have to show 'Y' in the header. There is no difference between them, I mean, there is no attribute in the model which allows me identify them.
For example, in a previous case, despite having the same model, I was able to show the correct string due to the state
field value:
<template id="external_layout_header" inherit_id="report.external_layout_header">
...
<t t-if="o._table=='sale_order'">
<t t-if="o.state=='draft'">
... Print Sale Quotation ...
</t>
<t t-if="o.state!='draft'">
... Print Sale Order ...
</t>
</t>
...
</template>
But in this case there is no field which helps me. Only the report name, so I need to get it from the header.
Does anyone know how to achieve this?
Upvotes: 4
Views: 1807
Reputation: 14721
I tried this and it worked hope you get the idea. First there is some fact that I know (but not for sure) that made me came with this logic.
docs
or o
(RecordSet passed to report) on Qweb.o.env.context
.Instead of adding attribute why we don't just add a key to the magic attribute context using o.with_context
using the power of t-set
we can add an extra key
but we need to make sure we do this before
we call <t t-call="report.external_layout">
in the report template so the external_layout
can find
that extra key
on the context ^^
<!-- add extra key to context -->
<!-- you can set it one time on docs it's better -->
<t t-set="o" t-value="o.with_context(report_name='hello_report')"/>
<t t-call="report.external_layout">
Now because context is frozendict instance or something like it (I'm not sure), we can use get
with default
value to check if that special key is passed on the context.
<template id="external_layout_header" inherit_id="report.external_layout_header">
...
<t t-if="o.env.context.get('report_name', False) == 'hello_report'">
<!-- show what you want to show for this report -->
Or you can make it even better by making this behavior generale by just passing the value itself in the context
this will minimize number of if statements
on you external_layout .
<template id="external_layout_header" inherit_id="report.external_layout_header">
...
<t t-if="o.env.context.get('extra_value', False)">
<p t-esc="o.env.context.get('extra_value')"></p>
So for any report that you need it to have this behavior.
if it's existing one inherit the template and make sure that you add the key before calling external_layout
using x-path
.
Edits:
an example of the result this is in odoo 8.0
I didn't use inherit I directly updated the remplates.
sale.order template
<template id="report_saleorder_document">
<t t-set="o" t-value="o.with_context(extra_info='show this on header')"/>
<t t-call="report.external_layout">
external layout on report
<template id="external_layout_header">
<div class="header">
<div class="row">
<t t-if=" 'extra_info' in o.env.context" >
<p t-esc="o.env.context.get('extra_info')"></p>
</t>
<div class="col-xs-3">
<img t-if="company.logo" t-att-src="'data:image/png;base64,%s' % company.logo" style="max-height: 45px;"/>
</div>
<div class="col-xs-9 text-right" style="margin-top:20px;" t-field="company.rml_header1"/>
</div>
....
.....
...
result is this
Upvotes: 3