RIzu
RIzu

Reputation: 21

How to setup variable condition on qweb odoo?

I have a line that sums all the values ​​above but i want to doesn't take values ​​with 'cancel' state

this is my python code for the definition of my state

sale_order_draft_ids = self.sale_order_draft_ids.ids
            sale_order_draft_ids = list(set(sale_order_draft_ids))

            sale_draft_ids = self.env['sale.order'].sudo().browse(sale_order_draft_ids)

            repartition_sales_d = {}

            for sale in sale_draft_ids:
                my_state = dict(self.env['sale.order']._fields['state'].selection).get(sale.state)

                repartition_sales_d[sale.id] = dict(
                    sale_draft_id=sale.id,
                    sale_number=sale.sale_number,
                    sale_name=sale.name,
                    sale_title=sale.sale_title,
                    sale_state=my_state,
                    sale_amount=sale.amount_untaxed,
                    sale_paid=sale.invoice_amount_total,
                    date_order=sale.date_order,
                )

            values['repartition_sales_d'] = repartition_sales_d

This is what i tried but doesnt work and i dont understand

<t t-if="sale['sale_state'] != 'cancel'">
<tr>
    <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:white !important;color:white;">
    </td>
    <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:white !important;color:white;">
    </td>
    <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:white !important;color:white;"/>
    <td class="text-right" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;">
            <t t-set="sum_amount_and_report" t-value="sum_amount_and_report+ sum_amount + sum_report"/>
            <t t-esc="'{:,}'.format(int(sum_amount_and_report)).replace(',', ' ')"/> €
    </td>
    <td class="text-right" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;">
            <t t-esc="'{:,}'.format(int(sum_paid)).replace(',', ' ')"/> €
    </td>
    <td class="text-right" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;">
        <t t-if="sum_amount > 0">
            <t t-set="sum_percent" t-value="(sum_paid/sum_amount) * 100"/>
        </t>
        <t t-esc="'{:,}'.format(int(sum_percent)).replace(',', ' ')"/> %
    </td>
    <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;"/>
    </tr>
    </t>
</t>

This is the full template asked of my qweb, i tried a lot of thing but nothing worked i would like to understand. Every time i got the same error " raise QWebException("Error to render compiling AST", e, path, node and etree.tostring(node[0], encoding='unicode'), name) Exception "

                                            <t t-set="sum_amount_and_report" t-value="0"/>
                                            <t t-set="sum_amount" t-value="0"/>
                                            <t t-set="sum_report" t-value="0"/>
                                            <t t-set="sum_paid" t-value="0"/>
                                            <t t-set="sum_percent" t-value="0"/>
                                            <t t-set="sale_percent" t-value="0"/>
                                            <t t-set="sale_paid" t-value="0"/>
                                            <t t-if="repartition_sales_d">
                                                <t t-foreach="repartition_sales_d" t-as="sale_draft_id">
                                                    <t t-set="sale" t-value="repartition_sales_d[sale_draft_id]"/>
                                                    <tr>
                                                        <td class="text-center">
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98">
                                                                <t t-esc="sale['sale_number']"/>
                                                            </a>
                                                        </td>
                                                        <td class="text-center">
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98" style="text-decoration: none !important;color:#666666;">
                                                                <t t-esc="sale['date_order']"/>
                                                            </a>
                                                        </td>
                                                        <td class="text-center">
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98" style="text-decoration: none !important;color:#666666;">
                                                                <t t-esc="sale['sale_title']"/>
                                                            </a>
                                                        </td>
                                                        <td class="text-center">
                                                            <t t-set="sum_amount" t-value="sum_amount + int(sale['sale_amount'])"/>
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98" style="text-decoration: none !important;color:#666666;">
                                                                <t t-esc="'{:,}'.format(int(sale['sale_amount'])).replace(',', ' ')"/> €
                                                            </a>
                                                        </td>
                                                        <td class="text-center">
                                                            <t t-set="sum_paid" t-value="sum_paid + int(sale['sale_paid'])"/>
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98" style="text-decoration: none !important;color:#666666;">
                                                                <t t-esc="'{:,}'.format(int(sale['sale_paid'])).replace(',', ' ')"/> €
                                                            </a>
                                                        </td>
                                                        <td class="text-center">
                                                            <t t-if="int(sale['sale_amount']) > 0">
                                                                <t t-set="sale_percent" t-value="int(sale['sale_paid']) / (int(sale['sale_amount'])) * 100"/>
                                                            </t>
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98" style="text-decoration: none !important;color:#666666;">
                                                                <t t-esc="'{:,}'.format(int(sale_percent)).replace(',', ' ')"/> %
                                                            </a>
                                                        </td>
                                                        <td class="text-center">
                                                            <a t-attf-href="/web#id=#{sale['sale_draft_id']}&amp;active_id=#{sale['sale_draft_id']}&amp;model=sale.order&amp;view_type=form&amp;cids=&amp;menu_id=98" style="text-decoration: none !important;color:#666666;">
                                                                <t t-if="sale['sale_state'] != 'cancel'">
                                                                    <t t-esc="sale['sale_state']"/>
                                                                </t>
                                                            </a>
                                                        </td>
                                                    </tr>
                                                </t>
                                            </t>
                                            <tr>
                                                <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:white !important;color:white;">
                                                </td>
                                                <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:white !important;color:white;">
                                                </td>
                                                <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:white !important;color:white;"/>
                                                <td class="text-right" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;">
                                                        <t t-set="sum_amount_and_report" t-value="sum_amount_and_report+ sum_amount + sum_report "/>
                                                        <t t-esc="'{:,}'.format(int(sum_amount_and_report)).replace(',', ' ')"/> €
                                                </td>
                                                <td class="text-right" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;">
                                                        <t t-esc="'{:,}'.format(int(sum_paid)).replace(',', ' ')"/> €
                                                </td>
                                                <td class="text-right" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;">
                                                    <t t-if="sum_amount > 0">
                                                        <t t-set="sum_percent" t-value="(sum_paid/sum_amount) * 100"/>
                                                    </t>
                                                    <t t-esc="'{:,}'.format(int(sum_percent)).replace(',', ' ')"/> %
                                                </td>
                                                <td class="text-center" style="vertical-align: middle;font-weight:bold;padding-top:10px;background-color:#171c8f !important;color:white;"/>
                                            </tr>

Upvotes: 0

Views: 946

Answers (1)

Kenly
Kenly

Reputation: 26698

You can either change the my_state expression to:

my_state = sale.state

Or change the condition in the QWEB template to:

<t t-if="sale['sale_state'] != 'Cancelled'">

Edit:

You used sale variable outside the t-if, Odoo will always try to get a value from sale and if repartition_sales_d is not defined it will raise an error.

The sum_amount_and_report variable has not been initialized, Odoo will raise an error. You need to initialize it before:

<t t-set="sum_amount_and_report" 
   t-value="sum_amount_and_report+ sum_amount + sum_report"/>

Upvotes: 2

Related Questions