<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_mrporder">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="o">
            <t t-call="web.internal_layout">
                <div class="page">
                    <div class="oe_structure"/>
                    <div class="row">
                        <div class="col-12">
                            <h2><span t-field="o.name"/></h2>
                        </div>
                    </div>
                    <div class="row mt32 mb32">
                        <div class="col-3" t-if="o.origin">
                            <strong>Source Document:</strong><br/>
                            <span t-field="o.origin"/>
                        </div>
                        <div class="col-3">
                            <strong>Responsible:</strong><br/>
                            <span t-field="o.user_id"/>
                        </div>
                        <div class="col-3">
                            <strong>Scheduled Date:</strong><br/>
                            <span t-field="o.date_planned_start"/>
                            <span t-if="o.date_planned_finished"> - <span t-field="o.date_planned_finished"/></span>
                        </div>
                    </div>

                    <div class="row mt32 mb32">
                        <div class="col-3">
                            <strong>Finished Product:</strong><br/>
                            <span t-field="o.product_id"/>
                        </div>
                        <div class="col-3">
                            <strong>Quantity to Produce:</strong><br/>
                            <span t-field="o.product_qty"/>
                            <span t-field="o.product_uom_id.name" groups="uom.group_uom"/>
                        </div>
                        <div class="col-3">
                            <strong>Printing date:</strong><br/>
                            <span t-esc="time.strftime('%Y-%m-%d')"
                                  t-options='{"widget": "date"}'/>
                        </div>
                    </div>

                    <div t-if="o.workorder_ids">
                        <h3>
                            <t t-if="o.state == 'done'">Operations Done</t>
                            <t t-else="">Operations Planned</t>
                        </h3>
                        <table class="table table-sm">
                            <tr>
                                <th><strong>Operation</strong></th>
                                <th><strong>WorkCenter</strong></th>
                                <th><strong>No. Of Minutes</strong></th>
                            </tr>
                            <tr t-foreach="o.workorder_ids" t-as="line2">
                                <td><span t-field="line2.name"/></td>
                                <td><span t-field="line2.workcenter_id.name"/></td>
                                <td>
                                    <span t-if="o.state != 'done'" t-field="line2.duration_expected"/>
                                    <span t-if="o.state == 'done'" t-field="line2.duration"/>
                                </td>
                            </tr>
                        </table>
                    </div>

                    <h3 t-if="o.move_raw_ids">
                        <t t-if="o.state == 'done'">
                            Consumed Products
                        </t>
                        <t t-else="">
                            Products to Consume
                        </t>
                    </h3>

                    <table class="table table-sm" t-if="o.move_raw_ids">
                        <t t-set="has_location" t-value="any(o.move_raw_ids.filtered(lambda x: x.move_line_ids.filtered(lambda x: x.location_id.id)))" groups="stock.group_stock_multi_locations"/>
                        <t t-set="has_serial_number" t-value="any(o.move_raw_ids.filtered(lambda x: x.move_line_ids.filtered(lambda x: x.lot_id or x.lot_name)))" groups="stock.group_production_lot"/>
                        <t t-set="has_product_barcode" t-value="any(o.move_raw_ids.filtered(lambda x: x.product_id.barcode))"/>
                        <t t-set="has_product_available" t-value="any(o.move_raw_ids.filtered(lambda x: x.move_line_ids.filtered(lambda x: x.product_qty)))"/>
                        <t t-if="o.state=='done'">
                            <t t-set="has_product_available" t-value="any(o.move_raw_ids.filtered(lambda x: x.move_line_ids.filtered(lambda x: x.qty_done)))"/>
                        </t>
                        <thead>
                            <tr t-if="has_product_available">
                                <th>Product</th>
                                <th>Quantity</th>
                                <th><span t-if="has_location">From</span></th>
                                <th t-if="has_serial_number" width="15%">Lot/Serial barcode</th>
                                <th t-if="has_product_barcode" width="15%" class="text-center">Barcode</th>
                            </tr>
                        </thead>
                        <tbody>
                            <t t-if="has_product_available and o.move_raw_ids" >
                                <t t-set="lines" t-value="o.move_raw_ids"/>
                                <t t-foreach="lines" t-as="line">
                                    <t t-set="move_lines" t-value="line.move_line_ids.filtered(lambda x: x.state != 'done' and x.product_qty)"/>
                                    <t t-if="o.state == 'done'">
                                        <t t-set="move_lines" t-value="line.move_line_ids.filtered(lambda x: x.state == 'done' and x.qty_done)"/>
                                    </t>
                                    <t t-foreach="move_lines" t-as="ml">
                                        <tr>
                                            <td>
                                                <span t-field="line.product_id"/>
                                            </td>
                                            <td>
                                                <span t-esc="ml.product_uom_qty" t-if="ml.state !='done'"/>
                                                <span t-esc="ml.qty_done"  t-if="ml.state =='done'"/>
                                                <span t-field="ml.product_uom_id" groups="uom.group_uom"/>
                                            </td>
                                            <td>
                                                <span t-if="has_location">
                                                    <span t-field="ml.location_id"/>
                                                </span>
                                            </td>
                                            <td t-if="has_serial_number" width="15%">
                                                <t t-if="has_serial_number and (ml.lot_id or ml.lot_name)">
                                                    <img t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s' % ('Code128', ml.lot_id.name, 600, 100)" style="width:100%;height:35px" alt="Barcode"/>
                                                </t>
                                            </td>
                                            <td t-if="has_product_barcode" width="15%" class="text-center">
                                                <t t-if="line.product_id.barcode">
                                                    <img t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s' % ('Code128', line.product_id.barcode, 600, 100)" style="width:100%;height:35px" alt="Barcode"/>
                                                </t>
                                            </td>
                                        </tr>
                                    </t>
                                </t>
                            </t>
                        </tbody>
                    </table>
                    <t t-set="has_product_reserved" t-value="o.move_raw_ids.filtered(lambda x: x.product_uom_qty != x.reserved_availability)"/>
                    <p t-if="o.state !='done' and (not has_product_available or has_product_reserved)" class="fa fa-exclamation-triangle">
                        All components could not be reserved. Click on the "Check Availability button" to try to reserve components.
                    </p>
                    <div class="oe_structure"/>
                </div>
            </t>
        </t>
    </t>
</template>

<template id="production_message">
    <t t-if="move.move_id.raw_material_production_id">
        <t t-set="message">Consumed</t>
    </t>
    <t t-if="move.move_id.production_id">
        <t t-set="message">Produced</t>
    </t>
    <strong><t t-esc="message"/> quantity has been updated.</strong>
</template>

<template id="track_production_move_template">
    <div>
        <t t-call="mrp.production_message"/>
        <t t-call="stock.message_body"/>
    </div>
</template>
</odoo>
