<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2017 Eficent Business and IT Consulting Services S.L.
     License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->

<odoo>

    <record id="qc_issue_tree" model="ir.ui.view">
        <field name="name">qc.issue.tree</field>
        <field name="model">qc.issue</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                <field name="inspector_id"/>
                <field name="product_id"/>
                <field name="responsible_id"/>
                <field name="state"/>
            </tree>
        </field>
    </record>

    <record id="qc_issue_form" model="ir.ui.view">
        <field name="name">qc.issue.form</field>
        <field name="model">qc.issue</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_confirm"
                            type="object" states="new"
                            string="Confirm" class="oe_highlight" />
                    <button name="action_done"
                            type="object" states="progress"
                            string="Set to Done" class="oe_highlight"/>
                    <button name="scrap_products"
                            string="Scrap Products" type="object"
                            states="progress"/>
                    <button name="action_cancel"
                            type="object" states="new,progress"
                            string="Cancel"/>
                    <field name="stage_id" widget="statusbar" clickable="True"
                           options="{'fold_field': 'fold'}"
                           domain="['|', ('qc_team_id', '=', qc_team_id), ('qc_team_id', '=', False)]"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button type="object"  name="action_view_stock_scrap"
                                class="oe_stat_button"
                                icon="fa-recycle"
                                context="{'default_qc_issue_id': active_id}">
                            <field name="stock_scrap_qty"
                                   widget="statinfo"
                                   string="Scrapped products"/>
                        </button>
                    </div>
                    <div class="oe_title">
                        <label for="name" string="Quality Control Issue"/>
                        <h1>
                            <field name="name"/>
                        </h1>
                    </div>
                    <group name="top">
                        <group>
                            <field name="inspector_id"/>
                            <field name="responsible_id"/>
                            <field name="company_id"
                                   groups="base.group_multi_company"/>
                            <field name="qc_team_id" widget="selection"/>
                            <field name="state"/>
                        </group>
                        <group>
                            <field name="lot_id"
                                   groups="stock.group_production_lot"
                                   attrs="{'required': [('product_tracking', 'in', ('serial', 'lot'))]}"/>
                            <field name="product_id"/>
                            <field name="product_tracking" invisible="1"/>
                            <field name="product_qty"
                                   attrs="{'invisible': [('product_tracking', '=', 'serial')]}"/>
                            <field name="product_uom"
                                   groups="uom.group_uom"
                                   attrs="{'invisible': [('product_tracking', '=', 'serial')]}"/>
                            <field name="location_id"/>
                        </group>
                    </group>
                    <group name="bottom">
                        <field name="qc_problem_ids" widget="many2many_tags"
                               options="{'color_field': 'color'}"/>
                        <field name="description"/>
                    </group>
                </sheet>
                <div class="oe_chatter">
                    <field name="message_follower_ids" widget="mail_followers" />
                    <field name="activity_ids" widget="mail_activity"/>
                    <field name="message_ids" widget="mail_thread" />
                </div>
            </form>
        </field>
    </record>

    <record id="qc_issue_kanban_view" model="ir.ui.view">
        <field name="name">qc.issue.kanban</field>
        <field name="model">qc.issue</field>
        <field name="arch" type="xml">
            <kanban class="o_kanban_small_column" default_group_by="stage_id"
                    quick_create="false" create="true">
                <field name="stage_id"/>
                <field name="color"/>
                <templates>
                    <t t-name="kanban-box">
                        <div t-attf-class="oe_kanban_color_#{kanban_getcolor(record.color.raw_value)} oe_kanban_card oe_kanban_global_click">
                            <div class="oe_kanban_content">
                                <div class="o_kanban_record_top">
                                    <div class="o_kanban_record_headings">
                                        <strong class="o_kanban_record_title"><field name="name"/></strong><br/>

                                    </div>
                                    <div class="o_dropdown_kanban dropdown" groups="base.group_user">
                                        <a role="button" class="dropdown-toggle o-no-caret btn" data-toggle="dropdown"
                                           href="#" aria-label="Dropdown menu" title="Dropdown menu">
                                            <span class="fa fa-ellipsis-v"/>
                                        </a>
                                        <div class="dropdown-menu" role="menu">
                                            <a t-if="widget.editable" role="menuitem" type="edit" class="dropdown-item">Edit</a>
                                            <a t-if="widget.deletable" role="menuitem" type="delete" class="dropdown-item">Delete</a>
                                            <div role="separator" class="dropdown-divider"/>
                                            <ul class="oe_kanban_colorpicker" data-field="color"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="o_kanban_record_body">
                                    <div>
                                        <field name="qc_problem_ids" widget="many2many_tags" options="{'color_field': 'color'}"/>
                                    </div>
                                    <div class="text-muted o_kanban_record_subtitle">
                                        Product: <field name="product_id"/>
                                        Qty: <field name="product_qty"/> <field name="product_uom"/><br/>
                                        Location: <field name="location_id"/>
                                    </div>

                                </div>
                                <div class="o_kanban_record_bottom">
                                    <!--To be inhereted.-->
                                </div>
                            </div>
                            <div class="oe_clear"/>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <record model="ir.ui.view" id="qc_issue_search">
        <field name="name">qc.issue.search</field>
        <field name="model">qc.issue</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="inspector_id"/>
                <field name="product_id"/>
                <field name="responsible_id"/>
                <field name="state"/>
                <field name="qc_team_id"/>
                <field name="stage_id"/>
                <field name="location_id"/>
                <filter name="my_inspections"
                        string="My inspections"
                        domain="[('inspector_id', '=', uid)]"/>
                <separator/>
                <filter string="My Activities" name="activities_my"
                    domain="[('activity_ids.user_id', '=', uid)]"/>
                <separator/>
                <filter string="Late Activities" name="activities_overdue"
                    domain="[('activity_ids.date_deadline', '&lt;', context_today().strftime('%Y-%m-%d'))]"
                    help="Show all records which has next action date is before today"/>
                <filter string="Today Activities" name="activities_today"
                    domain="[('activity_ids.date_deadline', '=', context_today().strftime('%Y-%m-%d'))]"/>
                <filter string="Future Activities" name="activities_upcoming_all"
                    domain="[('activity_ids.date_deadline', '&gt;', context_today().strftime('%Y-%m-%d'))
                    ]"/>
                <group expand="0" string="Group By...">
                    <filter name="groupby_stage"
                            string="Stage" domain="[]"
                            context="{'group_by':'stage_id'}"/>
                    <filter name="groupby_product"
                            string="Product" domain="[]"
                            context="{'group_by': 'product_id'}"/>
                    <filter name="groupby_location"
                            string="Location" domain="[]"
                            context="{'group_by': 'location_id'}"/>
                    <filter name="groupby_company"
                            string="Company" context="{'group_by':'company_id'}"
                            groups="base.group_multi_company"/>
                </group>
            </search>
        </field>
    </record>

    <record id="qc_issue_action" model="ir.actions.act_window">
        <field name="name">QC Issues</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">qc.issue</field>
        <field name="view_type">form</field>
        <field name="view_mode">kanban,tree,form</field>
    </record>

    <menuitem id="menu_qc_issue"
              parent="quality_control.qc_menu"
              sequence="50" name="Issues"/>

    <menuitem id="menu_qc_issue_child"
              parent="quality_control_issue.menu_qc_issue"
              action="qc_issue_action"
              sequence="20" name="Issues"/>

</odoo>
