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

        <record id="view_op_media_movement_tree" model="ir.ui.view">
            <field name="name">op.media.movement.list</field>
            <field name="model">op.media.movement</field>
            <field name="priority" eval="8"/>
            <field name="arch" type="xml">
                <list string="media Movement">
                    <field name="library_card_id"/>
                    <field name="partner_id"/>
                    <field name="media_id"/>
                    <field name="media_type_id" optional="hide"/>
                    <field name="media_unit_id"/>
                    <field name="issued_date"/>
                    <field name="return_date"/>
                    <field name="actual_return_date"/>
                    <field name="penalty" widget="monetary" optional="hide"/>
                    <field name="state"/>
                    <field name="invoice_id" invisible="True"/>
                    <field name="type" optional="hide"/>
                </list>
            </field>
        </record>

        <record id="view_op_media_movement_form" model="ir.ui.view">
            <field name="name">op.media.movement.form</field>
            <field name="model">op.media.movement</field>
            <field name="priority" eval="8"/>
            <field name="arch" type="xml">
                <form string="media Movement">
                    <header>
                        <button name="issue_media" string="Issue media" type="object" class="oe_highlight"
                                invisible="state not in ('available','reserve')"
                                groups="openeducat_library.group_op_library"/>
                        <button name="%(return_media_act)d" string="Return Media"
                                type="action"
                                context="{'default_media_id': media_id, 'default_media_unit_id': media_unit_id}"
                                invisible="state != 'issue'"
                                class="oe_highlight" groups="openeducat_library.group_op_library"/>
                        <field name="state" widget="statusbar" statusbar_visible="available,issue"/>
                    </header>
                    <sheet>
                        <field name="active" invisible="True"/>
                        <widget name="web_ribbon" title="Archived" bg_color="bg-danger"
                                invisible="active == True"/>
                        <group>
                            <group>
                                <field name="media_id"/>
                                <field name="media_type_id"/>
                            </group>
                            <group>
                                <field name="media_unit_id"
                                       domain="['&amp;',('media_id','=',media_id),('state','=','available')]"
                                       context="{'default_media_id': media_id}"/>
                            </group>
                        </group>
                        <group string="Student/Faculty Detail">
                            <group class="pt-3">
                                <field name="library_card_id"/>
                                <field name="student_id" readonly="1" force_save="1"
                                    required="type == 'student'" invisible= "type != 'student'"
                                       context="{'form_view_ref':'openeducat_library.view_op_student_library_form'}"/>
                                <field name="faculty_id" readonly="1" force_save="1"
                                    required="type == 'faculty'" invisible= "type != 'faculty'"/>
                            </group>
                            <group class="pt-3">
                                <field name="type" readonly="1" force_save="1"/>
                            </group>
                        </group>
                        <group>
                            <group class="pt-3">
                                <field name="issued_date"/>
                                <field name="actual_return_date"/>
                                <field name="invoice_id"/>
                            </group>
                            <group class="pt-3">
                                <field name="return_date"/>
                                <label for="penalty" string="Penalty"/>
                                <div class="row" name="penalty">
                                    <div class="col-5 col-sm-5 col-md-6 col-lg-4">
                                        <field name="penalty" widget="monetary"/>
                                    </div>
                                    <div class="col-7 col-sm-7 col-md-6 col-lg-8">
                                        <button name="create_penalty_invoice" string="Create Invoice"
                                                type="object" class="oe_highlight"
                                                invisible = "state not in 'return' or invoice_id != False or penalty == 0"/>
                                    </div>
                                </div>
                            </group>
                        </group>
                        <group col="4" string="Reservation Detail" invisible="1">
                            <field name="partner_id"/>
                            <field name="reserver_name"/>
                        </group>
                    </sheet>
                    <chatter/>
                </form>
            </field>
        </record>

        <record id="view_op_media_movement_search" model="ir.ui.view">
            <field name="name">op.media.movement.search</field>
            <field name="model">op.media.movement</field>
            <field name="priority" eval="8"/>
            <field name="arch" type="xml">
                <search string="media Movement">
                    <field name="media_id"/>
                    <field name="student_id"/>
                    <field name="library_card_id"/>
                    <field name="issued_date"/>
                    <field name="return_date"/>
                    <field name="actual_return_date"/>
                    <field name="state"/>
                    <field name="penalty"/>
                    <filter string="Issued Today" name="group_by_lbr_media_mvmn_issue"
                            domain="[('issued_date', '=', context_today().strftime('%Y-%m-%d'))]"/>
                    <filter string="Returned Today" name="group_by_lbr_media_mvmn_return"
                            domain="[('return_date', '=', context_today().strftime('%Y-%m-%d'))]"/>
                    <filter string="Archived" name="inactive"
                            domain="[('active', '=', False)]"/>
                    <group >
                        <filter string="media" name="group_by_lbr_media_mvmn_media"
                                domain="[]" context="{'group_by':'media_id'}"/>
                        <filter string="Student" name="group_by_lbr_media_mvmn_student"
                                domain="[]" context="{'group_by':'student_id'}"/>
                        <filter string="Faculty" name="group_by_lbr_media_mvmn_faculty"
                                domain="[]" context="{'group_by':'faculty_id'}"/>
                        <filter string="Library Card" name="group_by_lbr_media_mvmn_library_card"
                                domain="[]" context="{'group_by':'library_card_id'}"/>
                        <filter string="Issued Date" name="group_by_lbr_media_mvmn_issue_date"
                                domain="[]" context="{'group_by':'issued_date'}"/>
                        <filter string="Return Date" name="group_by_lbr_media_mvmn_return_date"
                                domain="[]" context="{'group_by':'return_date'}"/>
                        <filter string="Actual Return Date" name="group_by_lbr_media_mvmn_issue_date"
                                domain="[]" context="{'group_by':'actual_return_date'}"/>
                        <filter string="Status" name="group_by_lbr_media_mvmn_state"
                                domain="[]" context="{'group_by':'state'}"/>
                    </group>
                </search>
            </field>
        </record>

        <record model="ir.actions.act_window" id="act_open_op_media_movement_view">
            <field name="name">Media Movements</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">op.media.movement</field>
            <field name="binding_view_types">form</field>
            <field name="view_mode">list,form</field>
            <field name="path">media-movement</field>
            <field name="search_view_id" ref="view_op_media_movement_search"/>
        </record>

        <record model="ir.actions.act_window.view" id="act_open_op_media_movement_view_form">
            <field name="act_window_id" ref="act_open_op_media_movement_view"/>
            <field name="sequence" eval="20"/>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_op_media_movement_form"/>
        </record>

        <record model="ir.actions.act_window.view" id="act_open_op_media_movement_view_tree">
            <field name="act_window_id" ref="act_open_op_media_movement_view"/>
            <field name="sequence" eval="10"/>
            <field name="view_mode">list</field>
            <field name="view_id" ref="view_op_media_movement_tree"/>
        </record>

</odoo>
