<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <template id="report_mrp_bom">
        <div class="o_mrp_bom_report_page container py-3 py-lg-5 px-0 bg-view">
            <div t-if="data.get('lines')">
                <div>
                    <h1>BoM Overview</h1>
                    <h3 t-out="data['name']"/>
                    <hr t-if="data['bom_code']"/>
                    <h6 t-if="data['bom_code']">Reference: <t t-out="data['bom_code']"/></h6>
                </div>
                <t t-set="currency" t-value="data['currency']"/>
                <table class="o_mrp_bom_expandable table table-borderless">
                    <thead>
                        <tr>
                            <th name="th_mrp_bom_h">Product</th>
                            <th class="text-end">Quantity</th>
                            <th class="text-end" groups="uom.group_uom"/>
                            <th t-if="data['forecast_mode']" class="text-center">Free to Use / On Hand</th>
                            <th t-if="data['forecast_mode']" class="text-center">Status</th>
                            <th t-if="data['forecast_mode']" class="text-center">Lead Time</th>
                            <th t-if="data['forecast_mode']">Route</th>
                            <th class="text-end" t-attf-colspan="{{ 1 if data['forecast_mode'] else 2 }}">Cost</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td name="td_mrp_bom" t-out="data['name']"/>
                            <td class="text-end" t-out="data['quantity']" t-options='{"widget": "float", "decimal_precision": "Product Unit"}'/>
                            <td class="text-start" groups="uom.group_uom" t-out="data['uom_name']"/>
                            <td t-if="data['forecast_mode']" class="text-center">
                                <t t-out="data['quantity_available']" t-options='{"widget": "float", "decimal_precision": "Product Unit"}'/> /
                                <t t-out="data['quantity_on_hand']" t-options='{"widget": "float", "decimal_precision": "Product Unit"}'/>
                            </td>
                            <td t-if="data['forecast_mode']" class="text-center">
                                <t t-if="data['status']">
                                    <span t-attf-class="{{'text-danger' if data.get('availability_state') == 'unavailable' else ''}}
                                                        {{'text-success' if data.get('producible_qty') else ''}}"
                                                        t-out="data['status']">
                                    </span>
                                </t>
                            </td>
                            <td t-if="data['forecast_mode']" class="text-end">
                                <span t-if="data['lead_time'] is not False">
                                    <t t-out="data['lead_time']" t-options="{'widget': 'float', 'precision': 0}"/>
                                    Days
                                </span>
                            </td>
                            <td t-if="data['forecast_mode']">
                                <span t-if="data['route_name'] and data['route_detail']" t-attf-class="{{'text-danger' if data.get('route_alert') else ''}}">
                                    <t t-out="data['route_name']"/>:</span>
                                <span t-out="data['route_detail']"/>
                            </td>
                            <td class="text-end"
                                t-out="data['bom_cost']"
                                t-attf-colspan="{{ 1 if data['forecast_mode'] else 2 }}" t-options='{"widget": "monetary", "display_currency": currency}'/>
                        </tr>
                        <t t-call="mrp.report_mrp_bom_pdf_line"/>
                    </tbody>
                    <tfoot t-if="data['quantity'] &gt; 0">
                        <tr>
                            <td name="td_mrp_bom_f" class="text-end" t-attf-colspan="2">
                                <t t-if="data['byproducts']" t-out="data['name']"/>
                            </td>
                            <td t-if="(data['byproducts'] or data['product_uom_name'] != data['uom_name'])" groups="uom.group_uom" t-out="data['product_uom_name']"/>
                            <td name="free_to_use" t-if="data['forecast_mode']"/>
                            <td name="status" t-if="data['forecast_mode']"/>
                            <td name="lead_time" t-if="data['forecast_mode']"/>
                            <td name="route_column" class="text-end" t-attf-colspan="{{ 1 if len(data['byproducts']) or not data['show_uom'] or data['show_uom'] and data['product_uom_name'] != data['uom_name'] else 2 }}">
                                <strong>Unit Cost</strong>
                            </td>
                            <td name="cost" class="text-end" width="5rem">
                                <t t-out="data['bom_unit_cost'] / data['quantity']" t-options='{"widget": "monetary", "display_currency": currency}'/>
                            </td>
                        </tr>
                        <t t-if="data['byproducts']" t-foreach="data['byproducts']" t-as="byproduct">
                            <tr t-if="byproduct['quantity'] &gt; 0">
                                <td name="td_mrp_bom_byproducts_f" class="text-end" colspan="2" t-out="byproduct['name']"/>
                                <td class="text-start" groups="uom.group_uom" t-out="byproduct['product_uom_name']"/>
                                <td name="free_to_use" t-if="data['forecast_mode']"/>
                                <td name="status" t-if="data['forecast_mode']"/>
                                <td name="lead_time" t-if="data['forecast_mode']"/>
                                <td name="route_column" class="text-end"><strong>Unit Cost</strong></td>
                                <td class="text-end" width="5rem">
                                    <t t-out="byproduct['bom_unit_cost'] / byproduct['quantity']" t-options='{"widget": "monetary", "display_currency": currency}'/>
                                </td>
                            </tr>
                        </t>
                    </tfoot>
                </table>
            </div>
            <div t-else="" class="d-flex align-items-center justify-content-center h-50">
                <h4 class="text-muted">No data available.</h4>
            </div>
        </div>
    </template>

    <template id="report_mrp_bom_pdf_line">
        <t t-set="currency" t-value="data['currency']"/>
        <t t-foreach="data['lines']" t-as="l">
            <tr t-if="l['visible']">
                <td name="td_mrp_code">
                    <span t-attf-style="margin-left: {{ str(l['level'] * 20) }}px"/>
                    <span t-out="l['name']"/>
                </td>
                <td class="text-end">
                    <t t-if="l['type'] == 'operation'" t-out="l['quantity']" t-options="{'widget': 'float_time', 'unit': 'minutes', 'show_seconds': True}"/>
                    <t t-else="" t-out="l['quantity']" t-options='{"widget": "float", "decimal_precision": "Product Unit"}'/>
                </td>
                <td class="text-start" groups="uom.group_uom" t-out="l['uom']"/>
                <td t-if="data['forecast_mode']" class="text-center">
                    <t t-if="l.get('is_storable', False)">
                        <t t-out="l['quantity_available']" t-options='{"widget": "float", "decimal_precision": "Product Unit"}'/> /
                        <t t-out="l['quantity_on_hand']" t-options='{"widget": "float", "decimal_precision": "Product Unit"}'/>
                    </t>
                </td>
                <td t-if="data['forecast_mode']" class="text-center">
                    <t t-if="l.get('status', '') != '' and l.get('availability_state', '') != ''">
                    <span t-attf-class="{{'text-success' if l['availability_state'] == 'available' else ''}}
                                        {{'text-warning' if l['availability_state'] == 'expected' else ''}}
                                        {{'text-danger' if (l['availability_state'] == 'unavailable') else ''}}" t-out="l['status']"/>
                    </t>
                </td>
                <td t-if="data['forecast_mode']" class="text-end">
                    <span t-if="l.get('lead_time', False) is not False">
                        <t t-out="l['lead_time']" t-options="{'widget': 'float', 'precision': 0}"/>
                        Days
                    </span>
                </td>
                <td t-if="data['forecast_mode']">
                    <span t-if="l.get('route_name') and l.get('route_detail')" t-attf-class="{{'text-danger' if l.get('route_alert') else ''}}">
                        <t t-out="l['route_name']"/>: <t t-out="l['route_detail']"/>
                    </span>
                </td>
                <td t-if="not l['uom'] and (l['type'] == 'byproduct' or l['type'] == 'subcontract')" groups="uom.group_uom"/>
                <td t-attf-class="text-end {{ 'text-muted' if l['type'] == 'component' else '' }}" t-attf-colspan="{{ 1 if data['forecast_mode'] else 2 }}" t-out="l['bom_cost']" t-options='{"widget": "monetary", "display_currency": currency}'/>
            </tr>
        </t>
    </template>
    <template id="report_bom_structure">
        <t t-set="data_report_landscape" t-value="True"/>
        <t t-call="web.basic_layout">
            <t t-foreach="docs" t-as="data">
                <div class="page">
                    <t t-call="mrp.report_mrp_bom"/>
                </div>
                <p style="page-break-before:always;"> </p>
            </t>
        </t>
    </template>
</odoo>
