<?xml version="1.0" encoding="utf-8"?>
<odoo>

        <record id="stock_scrap_search_view" model="ir.ui.view">
            <field name="name">stock.scrap.search</field>
            <field name="model">stock.scrap</field>
            <field name="arch" type="xml">
                <search string="Search Scrap">
                    <field name="product_id"/>
                    <field name="location_id"/>
                    <field name="scrap_location_id"/>
                    <field name="create_date"/>
                    <group expand="0" string="Group By">
                        <filter string="Product" name="product" domain="[]" context="{'group_by':'product_id'}"/>
                        <filter string="Location" name="location" domain="[]" context="{'group_by':'location_id'}"/>
                        <filter string="Scrap Location" name="scrap_location" domain="[]" context="{'group_by':'scrap_location_id'}"/>
                    </group>
                </search>
            </field>
        </record>

        <record id="stock_scrap_form_view" model="ir.ui.view">
            <field name="name">stock.scrap.form</field>
            <field name="model">stock.scrap</field>
            <field name="arch" type="xml">
                <form string="Scrap">
                    <header>
                        <button name="action_validate" states="draft" string="Validate" type="object" class="oe_highlight"/>
                        <field name="state" widget="statusbar" statusbar_visible="draft,done" />
                    </header>
                    <sheet>
                        <div class="oe_button_box" name="button_box">
                            <button class="oe_stat_button" name="action_get_stock_picking"
                                    string="Stock Operation" type="object"
                                    attrs="{'invisible':[('picking_id','=',False)]}" icon="fa-cogs"/>
                            <field name="picking_id" invisible="1"/>
                            <button class="oe_stat_button" name="action_get_stock_move_lines"
                                    string="Product Move" type="object"
                                    attrs="{'invisible':[('move_id','=',False)]}" icon="fa-arrows-v"/>
                            <field name="move_id" invisible="1"/>
                        </div>
                        <div class="oe_title">
                            <h1><field name="name" nolabel="1"/></h1>
                        </div>
                        <group>
                            <group>
                                <field name="product_id"/>
                                <label for="scrap_qty"/>
                                <div class="o_row">
                                    <field name="scrap_qty"/>
                                    <field name="product_uom_id" groups="uom.group_uom"/>
                                </div>
                            </group>
                            <group>
                                <field name="lot_id" attrs="{'invisible': [('tracking', '=', 'none')], 'required': [('tracking', '!=', 'none')]}" groups="stock.group_production_lot"/>
                                <field name="tracking" invisible="1"/>
                                <field name="package_id" groups="stock.group_tracking_lot"/>
                                <field name="owner_id" groups="stock.group_tracking_owner"/>
                                <field name="location_id" groups="stock.group_stock_multi_locations"/>
                                <field name="scrap_location_id" groups="stock.group_stock_multi_locations"/>
                                <field name="origin"/>
                                <field name="date_expected"/>
                                <field name="picking_id" attrs="{'invisible': [('picking_id', '=', False)]}"/>
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <record id="stock_scrap_view_kanban" model="ir.ui.view">
            <field name="name">stock.scrap.kanban</field>
            <field name="model">stock.scrap</field>
            <field name="arch" type="xml">
                <kanban class="o_kanban_mobile">
                    <field name="name"/>
                    <field name="product_id"/>
                    <field name="date_expected"/>
                    <field name="scrap_qty"/>
                    <field name="state"/>
                    <templates>
                        <t t-name="kanban-box">
                            <div class="oe_kanban_global_click">
                                <div class="row mb4">
                                    <strong class="col-6">
                                        <span t-esc="record.name.value"/>
                                    </strong>
                                    <strong t-if="record.date_expected.value" class="col-6 text-right">
                                        <i class="fa fa-clock-o" role="img" aria-label="Expected date" title="Expected date"/><span t-esc="record.date_expected.value"/>
                                    </strong>
                                    <div class="col-12">
                                        <span t-esc="record.product_id.value"/>
                                    </div>
                                    <div class="col-6">
                                        <span t-esc="record.scrap_qty.value"/>
                                    </div>
                                    <div class="col-6">
                                        <span class="float-right badge badge-secondary">
                                            <field name="state"/>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </t>
                    </templates>
                </kanban>
            </field>
        </record>

        <record id="stock_scrap_tree_view" model="ir.ui.view">
            <field name="name">stock.scrap.tree</field>
            <field name="model">stock.scrap</field>
            <field name="arch" type="xml">
                <tree decoration-danger="state == 'draft'">
                    <field name="name"/>
                    <field name="create_date" string="Create Date"/>
                    <field name="product_id"/>
                    <field name="scrap_qty"/>
                    <field name="product_uom_id" groups="uom.group_uom"/>
                    <field name="location_id" groups="stock.group_stock_multi_locations"/>
                    <field name="scrap_location_id" groups="stock.group_stock_multi_locations"/>
                    <field name="state"/>
                </tree>
            </field>
        </record>

        <record model="ir.actions.act_window" id="action_stock_scrap">
            <field name="name">Scrap Orders</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">stock.scrap</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form,kanban</field>
            <field name="help" type="html">
              <p class="o_view_nocontent_smiling_face">
                Scrap products
              </p><p>
                Scrapping a product will remove it from your stock. The product will
                end up in a scrap location that can be used for reporting purpose.
              </p>
            </field>
        </record>

        <record id="stock_scrap_form_view2" model="ir.ui.view">
            <field name="name">stock.scrap.form2</field>
            <field name="model">stock.scrap</field>
            <field name="arch" type="xml">
                <form string="Scrap">
                    <group>
                        <group>
                            <field name="state" invisible="1"/>
                            <field name="product_id" options="{'no_create': True}" domain="[('id', 'in', context.get('product_ids', []))]"/>
                            <label for="scrap_qty"/>
                            <div class="o_row">
                                <field name="scrap_qty"/>
                                <field name="product_uom_id" groups="uom.group_uom"/>
                            </div>
                        </group>
                        <group>
                            <field name="picking_id" invisible="1"/>
                            <field name="tracking" invisible="1"/>
                            <field name="lot_id" attrs="{'invisible': [('tracking', '=', 'none')], 'required': [('tracking', '!=', 'none')]}" groups="stock.group_production_lot"/>
                            <field name="package_id" groups="stock.group_tracking_lot"/>
                            <field name="owner_id" groups="stock.group_tracking_owner"/>
                            <field name="location_id" groups="stock.group_stock_multi_locations" options="{'no_open': True, 'no_create': True}"/>
                            <field name="scrap_location_id" groups="stock.group_stock_multi_locations" options="{'no_open': True, 'no_create': True}" />
                        </group>
                    </group>
                    <footer>
                        <button name="action_validate" string="Done" type="object" class="btn-primary"/>
                        <button string="Cancel" class="btn-secondary" special="cancel" />
                    </footer>
                </form>
            </field>
        </record>

    <menuitem
        id="menu_stock_scrap"
        name="Scrap"
        parent="menu_stock_warehouse_mgmt"
        action="action_stock_scrap"
        sequence="99"/>

</odoo>
