<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <!-- Tree View -->
    <record id="view_confirm_benefit_expense_tree" model="ir.ui.view">
        <field name="name">confirm.benefit.expense.tree</field>
        <field name="model">confirm.benefit.expense</field>
        <field name="arch" type="xml">
            <tree>
                <field name="family_expense_seq"/>
                <field name="date"/>
                <field name="name" string="Description"/>
                <field name="currency_id" invisible="1" groups="base.group_multi_currency"/>
                <field name="family_monthly_total" decoration-bf="1" widget="monetary"
                       options="{'currency_field': 'currency_id'}"/>
                <field name="family_monthly_othaime" decoration-bf="1" widget="monetary"
                       options="{'currency_field': 'currency_id'}"/>
                <field name="state" widget="badge"
                       decoration-info="state in ['draft','calculated']"
                       decoration-warning="state in ['assistant_general_manager']"
                       decoration-success="state in ['confirm']"
                       decoration-muted="state == 'cancel'"
                />
                <field name="payment_state"
                       widget="badge"
                       decoration-info="payment_state == 'none'"
                       decoration-warning="payment_state == 'waiting'"
                       decoration-success="payment_state == 'done'"
                />
                <field name="move_state"
                       widget="badge"
                       decoration-success="move_state == 'done'"
                       decoration-warning="move_state == 'waiting'"
                       decoration-muted="move_state == 'none'"/>
            </tree>
        </field>
    </record>

    <!-- Form View -->
    <record id="view_confirm_benefit_expense_form" model="ir.ui.view">
        <field name="name">confirm.benefit.expense.form</field>
        <field name="model">confirm.benefit.expense</field>
        <field name="arch" type="xml">
            <form string="Confirm Benefit Expense">
                <header>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,calculated,accounting_approve,assistant_general_manager,confirm"/>

                    <button name="action_calculate" string="Calculate"
                            groups="odex_benefit.group_family_services_manager"
                            type="object" states="draft" class="btn btn-info"/>

                    <button name="action_send_to_researcher" type="object" groups="odex_benefit.group_family_services_manager"
                            attrs="{'invisible': ['|','|',('state','!=','draft'),('is_return_calculation', '=', False),('has_draft_return_lines', '=', False)]}"
                            string="Send to Researchers" class="btn btn-warning"/>

                    <button string="Confirm" type="object" name="action_assistant_manager" class="oe_highlight"
                            states="calculated" groups="odex_benefit.group_family_services_manager"/>
                    <button string="Withdraw" type="object" name="action_reset_to_draft" class="btn btn-danger"
                            states="calculated" groups="odex_benefit.group_family_services_manager"/>
                    <button string="Recalculate" name="action_recalculate" class="btn btn-info"
                            type="object" states="calculated" groups="odex_benefit.group_family_services_manager"/>

                    <button string="Confirm" type="object" name="action_accounting_approve" class="oe_highlight"
                            states="assistant_general_manager"
                            groups="odex_benefit.group_benefit_manager,odex_benefit.group_family_services_manager"/>
                    <button string="Reset" type="object" name="action_reset_to_calculated" class="btn btn-danger"
                            states="assistant_general_manager"
                            groups="odex_benefit.group_benefit_manager,odex_benefit.group_family_services_manager"/>
                    <button string="Reset" type="object" name="action_reset_to_calculated" class="btn btn-danger"
                            states="accounting_approve" groups="odex_benefit.group_family_services_manager"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_open_related_payment_orders"
                                class="oe_stat_button"
                                icon="fa-file-text-o"
                                type="object" attrs="{'invisible': [('payment_order_id', '=', False)]}">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Payment Orders</span>
                                <span class="o_stat_value">
                                    <field name="payment_state"
                                           widget="badge"
                                           decoration-success="payment_state == 'done'"
                                           decoration-warning="payment_state == 'waiting'"
                                           decoration-muted="payment_state == 'none'"/>
                                </span>
                            </div>
                        </button>
                        <button name="action_open_related_move_records"
                                class="oe_stat_button"
                                icon="fa-file-text-o"
                                type="object" attrs="{'invisible': [('move_id', '=', False)]}">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Othaime Invoice</span>
                                <span class="o_stat_value">
                                    <field name="move_state"
                                           widget="badge"
                                           decoration-success="move_state == 'done'"
                                           decoration-warning="move_state == 'waiting'"
                                           decoration-muted="move_state == 'none'"/>
                                </span>
                            </div>
                        </button>
                    </div>
                    <field name="family_domain_ids" widget="many2many_tags" invisible="1"/>
                    <div class="oe_title">
                        <h1>
                            <field name="family_expense_seq" readonly="1"/>
                        </h1>
                    </div>
                    <group>
                        <group>
                            <field name="name" required="1" string="Description"
                                   attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            <label for="start_date" string="Period"/>
                            <div class="o_row">
                                <field name="start_date"
                                       attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                                <i class="fa fa-long-arrow-right mx-2 oe_edit_only"
                                   aria-label="Arrow icon" title="Arrow"/>
                                <field name="end_date"
                                       attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            </div>
                            <field name="is_return_calculation" widget="boolean_toggle"
                                   attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            <field name="branch_custom_ids" required="1"
                                   widget="many2many_tags" attrs="{'readonly':[('state', '!=', 'draft')]}"
                            />
                        </group>
                        <group>
                            <field name="date" required="1" attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            <field name="payment_order_id" invisible="1"/>
                            <field name="move_id" invisible="1"/>
                            <field name="cash_expense" widget="boolean_toggle"
                                   attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            <field name="meal_expense" widget="boolean_toggle"
                                   attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            <field name="cloth_expense" widget="boolean_toggle"
                                   attrs="{'readonly':[('state', '!=', 'draft')]}"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                            <field name="currency_id" invisible="1" groups="base.group_multi_currency"/>
                            <field name="available_payment_method_line_ids" invisible="1"/>
                            <field name="line_domain_ids" invisible="1"/>
                            <field name="has_draft_return_lines" invisible="1"/>
                        </group>
                    </group>
                    <!-- Notebook with Families page -->
                    <notebook>
                        <page string="Family Monthly Expense"
                              attrs="{'invisible': [('state', 'in', ['draft'])]}">
                            <div class="row mb-2">
                                <!-- Family Count (Monthly Expense) -->
                                <div class="col-6">
                                    <div class="d-flex flex-column align-items-center"
                                         style="background-color: #e3f2fd; padding: 8px; border-radius: 8px;">
                                        <i class="fa fa-home text-primary"
                                           style="font-size: 20px; margin-bottom: 4px;"/>
                                        <span style="font-size: 12px; color: #555;">Family Count (Monthly Expense)
                                        </span>
                                        <span class="badge badge-primary"
                                              style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                            <field name="family_count_expense"/>
                                        </span>
                                    </div>
                                </div>

                                <!-- Member Count (Monthly Expense) -->
                                <div class="col-6">
                                    <div class="d-flex flex-column align-items-center"
                                         style="background-color: #e0f7fa; padding: 8px; border-radius: 8px;">
                                        <i class="fa fa-users text-info" style="font-size: 20px; margin-bottom: 4px;"/>
                                        <span style="font-size: 12px; color: #555;">Member Count (Monthly Expense)
                                        </span>
                                        <span class="badge badge-info"
                                              style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                            <field name="member_count_expense"/>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <div class="row mb-2">
                                <div class="col-12">
                                    <div class="row text-center">
                                        <div class="col-3">
                                            <div class="d-flex flex-column align-items-center"
                                                 style="background-color: #e3f2fd; padding: 8px; border-radius: 8px;">
                                                <i class="fa fa-money text-success"
                                                   style="font-size: 20px; margin-bottom: 4px;"/>
                                                <span style="font-size: 12px; color: #555;">Total Monthly Income
                                                </span>
                                                <span class="badge badge-primary"
                                                      style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                                    <field name="family_monthly_income" widget="monetary"
                                                           options="{'currency_field': 'currency_id'}"/>
                                                </span>
                                            </div>
                                        </div>

                                        <!-- Total Monthly Meals -->
                                        <div class="col-3">
                                            <div class="d-flex flex-column align-items-center"
                                                 style="background-color: #fff3e0; padding: 8px; border-radius: 8px;">
                                                <i class="fa fa-money text-success"
                                                   style="font-size: 20px; margin-bottom: 4px;"/>
                                                <span style="font-size: 12px; color: #555;">Total Monthly Meals
                                                </span>
                                                <span class="badge badge-warning"
                                                      style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                                    <field name="family_monthly_meals" widget="monetary"
                                                           options="{'currency_field': 'currency_id'}"/>
                                                </span>
                                            </div>
                                        </div>

                                        <!-- Total Monthly Clothing -->
                                        <div class="col-3">
                                            <div class="d-flex flex-column align-items-center"
                                                 style="background-color: #e0f7fa; padding: 8px; border-radius: 8px;">
                                                <i class="fa fa-money text-success"
                                                   style="font-size: 20px; margin-bottom: 4px;"/>
                                                <span style="font-size: 12px; color: #555;">Total Monthly Clothing
                                                </span>
                                                <span class="badge badge-info"
                                                      style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                                    <field name="family_monthly_clotting" widget="monetary"
                                                           options="{'currency_field': 'currency_id'}"/>
                                                </span>
                                            </div>
                                        </div>

                                        <!-- Monthly Total -->
                                        <div class="col-3">
                                            <div class="d-flex flex-column align-items-center"
                                                 style="background-color: #e8f7ef;padding: 8px; border-radius: 8px;">
                                                <i class="fa fa-calculator text-success"
                                                   style="font-size: 20px; margin-bottom: 4px;"/>
                                                <span style="font-size: 12px; color: #555;">Total</span>
                                                <span class="badge badge-success"
                                                      style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                                    <field name="family_monthly_total" widget="monetary"
                                                           options="{'currency_field': 'currency_id'}"/>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <field name="benefit_expense_line_ids" readonly="1">
                                <tree>
                                    <field name="family_id"/>
                                    <field name="branch_id"/>
                                    <field name="family_category_id"/>
                                    <field name="start_date" invisible="1"/>
                                    <field name="end_date" invisible="1"/>
                                    <field name="meal_card"/>
                                    <field name="benefit_member_count"/>
                                    <field name="family_monthly_income"
                                           attrs="{'column_invisible': [('parent.cash_expense', '!=', True)]}"/>
                                    <field name="family_monthly_meals"
                                           attrs="{'column_invisible': [('parent.meal_expense', '!=', True)]}"/>
                                    <field name="family_monthly_clotting"
                                           attrs="{'column_invisible': [('parent.cloth_expense', '!=', True)]}"/>
                                    <field name="total_family_expenses"/>
                                    <field name="payment_order_id" invisible="1"/>
                                    <field name="is_return" optional="hide"/>
                                    <field name="return_reason_id" optional="hide"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Othaim Line"
                              attrs="{'invisible': ['|','|',('is_return_calculation', '=', True),('state', '=', 'draft'),('meal_expense', '=', False)]}">
                            <div class="row mb-2">
                                <!-- Family Count -->
                                <div class="col-4">
                                    <div class="d-flex flex-column align-items-center"
                                         style="background-color: #e3f2fd; padding: 8px; border-radius: 8px;">
                                        <i class="fa fa-home text-primary"
                                           style="font-size: 20px; margin-bottom: 4px;"/>
                                        <span style="font-size: 12px; color: #555;">Family Count (Othaim)</span>
                                        <span class="badge badge-primary"
                                              style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                            <field name="family_count_othaim"/>
                                        </span>
                                    </div>
                                </div>

                                <!-- Member Count -->
                                <div class="col-4">
                                    <div class="d-flex flex-column align-items-center"
                                         style="background-color: #e0f7fa; padding: 8px; border-radius: 8px;">
                                        <i class="fa fa-users text-info" style="font-size: 20px; margin-bottom: 4px;"/>
                                        <span style="font-size: 12px; color: #555;">Member Count (Othaim)</span>
                                        <span class="badge badge-info"
                                              style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                            <field name="member_count_othaim"/>
                                        </span>
                                    </div>
                                </div>

                                <!-- Total Othaim -->
                                <div class="col-4">
                                    <div class="d-flex flex-column align-items-center"
                                         style="background-color: #e8f5e9; padding: 8px; border-radius: 8px;">
                                        <i class="fa fa-money text-success"
                                           style="font-size: 20px; margin-bottom: 4px;"/>
                                        <span style="font-size: 12px; color: #555;">Total Othaim</span>
                                        <span class="badge badge-success"
                                              style="font-size: 14px; padding: 4px 8px; margin-top: 3px; border-radius: 8px;">
                                            <field name="family_monthly_othaime" widget="monetary"
                                                   options="{'currency_field': 'currency_id'}"/>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <field name="othaim_line_ids" readonly="1">
                                <tree>
                                    <field name="family_id"/>
                                    <field name="branch_id"/>
                                    <field name="family_category_id"/>
                                    <field name="start_date" invisible="1"/>
                                    <field name="end_date" invisible="1"/>
                                    <field name="meal_card"/>
                                    <field name="benefit_member_count"/>
                                    <field name="family_monthly_othaime"/>
                                    <field name="payment_order_id" invisible="1"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Families">
                            <field name="family_ids" attrs="{'readonly':[('state', '!=', 'draft')]}"
                                   domain="[('id', 'in', family_domain_ids)]">
                                <tree>
                                    <field name="code" decoration-bf="1"/>
                                    <field name="benefit_partner_name"/>
                                    <field name="benefit_category_id"/>
                                    <field name="create_date" optional="hide"/>
                                    <field name="meal_card"/>
                                    <field name="father_id_number" optional="hide"/>
                                    <field name="sms_phone"/>
                                    <field name="benefit_member_count"/>
                                    <field name="non_member_count" optional="hide"/>
                                    <field name="researcher_id"/>
                                    <field name="branch_custom_id"/>
                                    <field name="district_id"/>
                                    <field name="last_visit_date" optional="hide"/>
                                    <field name="acc_number" optional="hide"/>
                                    <field name="acc_holder_name" optional="hide"/>
                                    <field name="state" widget="badge"
                                           decoration-success="state in ['first_approve', 'second_approve']"
                                           decoration-muted="state in ['draft','new']"
                                           decoration-danger="state in ['refused','suspended_second_approve']"
                                           decoration-warning="state in ['first_refusal','waiting_approve']"
                                           decoration-info="state not in ('waiting_approve','draft','new','first_approve', 'second_approve','first_refusal','refused','suspended_second_approve')"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Return Lines" attrs="{'invisible': [('is_return_calculation', '=', False)]}">
                            <field name="return_expense_line_ids" widget="many2many"
                                   domain="[('id', 'in', line_domain_ids)]"
                                   attrs="{'readonly':[('state', '!=', 'draft')]}">
                                <tree>
                                    <field name="family_id"/>
                                    <field name="branch_id"/>
                                    <field name="meal_card"/>
                                    <field name="benefit_member_count"/>
                                    <field name="family_monthly_income"
                                           attrs="{'column_invisible': [('parent.cash_expense', '!=', True)]}"/>
                                    <field name="family_monthly_meals"
                                           attrs="{'column_invisible': [('parent.meal_expense', '!=', True)]}"/>
                                    <field name="family_monthly_clotting"
                                           attrs="{'column_invisible': [('parent.cloth_expense', '!=', True)]}"/>
                                    <field name="total_family_expenses"/>
                                    <field name="payment_order_id" invisible="1"/>
                                    <field name="is_return"/>
                                    <field name="return_reason_id"/>
                                    <field name="state" widget="badge"
                                           decoration-info="state == 'draft'"
                                           decoration-warning="state == 'waiting_processing'"
                                           decoration-success="state == 'processing_done'"
                                    />
                                    <field name="researcher_id" optional="hide"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
                <div class="oe_chatter">
                    <field name="message_follower_ids" widget="mail_followers" groups="base.group_user"/>
                    <field name="activity_ids"/>
                    <field name="message_ids" widget="mail_thread"/>
                </div>
            </form>
        </field>
    </record>

    <!-- Search view-->
    <record model="ir.ui.view" id="view_confirm_benefit_expense_search">
        <field name="name">confirm.benefit.expense</field>
        <field name="model">confirm.benefit.expense</field>
        <field name="arch" type="xml">
            <search>
                <separator/>
                <filter string="Draft" name="draft" domain="[('state','=','draft')]"/>
                <separator/>
                <separator/>
                <filter string="Cancel" name="cancel" domain="[('state', '=', 'cancel')]"/>
                <separator/>
                <separator/>
                <filter string="Confirm" name="confirm" domain="[('state', '=', 'confirm')]"/>
                <separator/>
            </search>
        </field>
    </record>

    <!-- Action -->
    <record id="action_confirm_benefit_expense" model="ir.actions.act_window">
        <field name="name">Confirm Benefit Expenses</field>
        <field name="res_model">confirm.benefit.expense</field>
        <field name="view_mode">tree,form</field>
        <field name="view_id" ref="view_confirm_benefit_expense_tree"/>
    </record>

    <record id="view_move_line_tree_new_code" model="ir.ui.view">
        <field name="name">account.move.line.form</field>
        <field name="model">account.move.line</field>
        <field name="inherit_id" ref="account.view_move_line_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='ref']" position="after">
                <field name="family_code"/>
            </xpath>
        </field>
    </record>

    <record id="view_move_line_tree_grouped_inherit_more" model="ir.ui.view">
        <field name="name">account.move.line.form</field>
        <field name="model">account.move.line</field>
        <field name="inherit_id" ref="account.view_move_line_tree_grouped"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='ref']" position="after">
                <field name="family_code"/>
            </xpath>
        </field>
    </record>


</odoo>
