<?xml version="1.0" encoding="utf-8" ?>
<!--
  Copyright 2017-2021 Akretion France (http://www.akretion.com/)
  @author: Alexis de Lattre <alexis.delattre@akretion.com>
  License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>
     <record id="view_stock_quant_form_editable" model="ir.ui.view">
        <field name="name">product_expiry_simple.stock.quant.form</field>
        <field name="model">stock.quant</field>
        <field name="inherit_id" ref="stock.view_stock_quant_form_editable" />
        <field name="arch" type="xml">
            <field name="product_id" position="before">
                    <!-- Cannot use context_today() nor current_date in a form view,
                         only in a tree/search view.
                         So I added a computed boolean field 'expired' -->
                <field name="expired" invisible="1" />
                <span
                    class="badge badge-danger"
                    attrs="{'invisible': [('expired', '=', False)]}"
                >Expired</span>
            </field>
        </field>
    </record>

    <record id="view_stock_quant_tree" model="ir.ui.view">
        <field name="name">product_expiry_simple.stock.quant.tree</field>
        <field name="model">stock.quant</field>
        <field name="inherit_id" ref="stock.view_stock_quant_tree" />
        <field name="arch" type="xml">
            <field name="lot_id" position="after">
                <!-- We show expiry_date in tree view in order to do an "order by" on this column via the interface
            But the information is already in the lot_id column via name_get() -->
                <field name="expiry_date" optional="hide" />
            </field>
            <!-- I prefer to continue to use the full line in red
                    instead of using widget="badge" decoration-danger=""
                    on the field expiry_date. That way, we can keep the field
            expiry_date hidden by default, which avoids the double info -->
            <xpath expr="/tree" position="attributes">
                  <attribute
                    name="decoration-danger"
                >expiry_date and expiry_date &lt; current_date</attribute>
            </xpath>
        </field>
    </record>
    <record id="view_stock_quant_tree_editable" model="ir.ui.view">
        <field name="name">product_expiry_simple.stock.quant.tree.editable</field>
        <field name="model">stock.quant</field>
        <field name="inherit_id" ref="stock.view_stock_quant_tree_editable" />
        <field name="arch" type="xml">
            <field name="lot_id" position="after">
                <!-- We show expiry_date in tree view in order to do an "order by" on this column via the interface
            But the information is already in the lot_id column via name_get() -->
                <field name="expiry_date" optional="hide" />
            </field>
            <xpath expr="/tree" position="attributes">
                  <attribute
                    name="decoration-danger"
                >expiry_date and expiry_date &lt; current_date</attribute>
            </xpath>
        </field>
    </record>
    <record id="view_stock_quant_tree_inventory_editable" model="ir.ui.view">
        <field
            name="name"
        >product_expiry_simple.stock.quant.tree.inventory.editable</field>
        <field name="model">stock.quant</field>
        <field name="inherit_id" ref="stock.view_stock_quant_tree_inventory_editable" />
        <field name="arch" type="xml">
            <field name="lot_id" position="after">
                <!-- We show expiry_date in tree view in order to do an "order by" on this column via the interface
            But the information is already in the lot_id column via name_get() -->
                <field name="expiry_date" optional="hide" />
            </field>
            <xpath expr="/tree" position="attributes">
                  <attribute
                    name="decoration-danger"
                >expiry_date and expiry_date &lt; current_date</attribute>
            </xpath>
        </field>
    </record>

    <record id="quant_search_view" model="ir.ui.view">
        <field name="name">product_expiry_simple.stock.quant.search</field>
        <field name="model">stock.quant</field>
        <field name="inherit_id" ref="stock.quant_search_view" />
        <field name="arch" type="xml">
            <filter name="reserved" position="after">
                <separator />
                <filter name="expiry_date" date="expiry_date" string="Expiry Date" />
                <separator />
                <filter
                    domain="[('expiry_date', '&lt;', context_today().strftime('%Y-%m-%d'))]"
                    groups="stock.group_production_lot"
                    name="expired"
                    string="Expired"
                />
                <filter
                    domain="[('expiry_date', '&gt;=', context_today().strftime('%Y-%m-%d'))]"
                    groups="stock.group_production_lot"
                    name="no-expired"
                    string="Not Expired"
                />
            </filter>
            <filter context="{'group_by': 'lot_id'}" position="after">
                <filter
                    context="{'group_by': 'expiry_date:month'}"
                    groups="stock.group_production_lot"
                    name="expiry_date_groupby"
                    string="Expiry Date"
                />
            </filter>
        </field>
    </record>
</odoo>
