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

    <!-- ============================================================ -->
    <!--                    LOAN FORM VIEW                            -->
    <!-- ============================================================ -->
    <record id="fusion_loan_view_form" model="ir.ui.view">
        <field name="name">fusion.loan.form</field>
        <field name="model">fusion.loan</field>
        <field name="arch" type="xml">
            <form string="Loan">
                <header>
                    <button name="compute_amortization_schedule"
                            string="Compute Schedule"
                            type="object"
                            class="btn-primary"
                            invisible="state != 'draft'"/>
                    <button name="action_confirm"
                            string="Confirm"
                            type="object"
                            class="btn-primary"
                            invisible="state != 'draft'"
                            confirm="This will create the disbursement entry and start the loan. Continue?"/>
                    <button name="generate_entries"
                            string="Generate Due Entries"
                            type="object"
                            class="btn-secondary"
                            invisible="state != 'running'"/>
                    <button name="action_pay_early"
                            string="Early Repayment"
                            type="object"
                            class="btn-secondary"
                            invisible="state != 'running'"
                            confirm="This will settle the entire remaining balance. Continue?"/>
                    <button name="action_cancel"
                            string="Cancel"
                            type="object"
                            class="btn-danger"
                            invisible="state not in ('draft', 'running')"
                            confirm="This will reverse all entries and cancel the loan. Continue?"/>
                    <button name="action_reset_to_draft"
                            string="Reset to Draft"
                            type="object"
                            invisible="state != 'cancelled'"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,running,paid"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_entries"
                                type="object"
                                class="oe_stat_button"
                                icon="fa-book"
                                invisible="entries_count == 0">
                            <field name="entries_count" widget="statinfo"
                                   string="Entries"/>
                        </button>
                    </div>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Loan Details">
                            <field name="partner_id"
                                   readonly="state != 'draft'"/>
                            <field name="principal_amount"
                                   readonly="state != 'draft'"/>
                            <field name="interest_rate"
                                   readonly="state != 'draft'"/>
                            <field name="loan_term"
                                   readonly="state != 'draft'"/>
                            <field name="start_date"
                                   readonly="state != 'draft'"/>
                            <field name="payment_frequency"
                                   readonly="state != 'draft'"/>
                            <field name="amortization_method"
                                   readonly="state != 'draft'"/>
                        </group>
                        <group string="Accounting">
                            <field name="journal_id"
                                   readonly="state != 'draft'"/>
                            <field name="loan_account_id"
                                   readonly="state != 'draft'"/>
                            <field name="interest_account_id"
                                   readonly="state != 'draft'"/>
                            <field name="currency_id" invisible="1"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                    </group>
                    <group string="Summary" col="4">
                        <field name="total_amount"/>
                        <field name="total_interest"/>
                        <field name="remaining_balance"/>
                        <field name="paid_installments" string="Progress"
                               widget="progressbar"
                               options="{'max_value': 'installment_count'}"/>
                        <field name="installment_count" invisible="1"/>
                    </group>
                    <notebook>
                        <page string="Amortization Schedule" name="schedule">
                            <field name="line_ids" readonly="1" nolabel="1">
                                <list editable="bottom"
                                      decoration-success="is_paid"
                                      decoration-muted="is_paid"
                                      create="0" delete="0">
                                    <field name="sequence" string="#"/>
                                    <field name="date"/>
                                    <field name="principal_amount" sum="Total Principal"/>
                                    <field name="interest_amount" sum="Total Interest"/>
                                    <field name="total_payment" sum="Total Payments"/>
                                    <field name="remaining_balance"/>
                                    <field name="is_paid"/>
                                    <field name="move_id" optional="hide"/>
                                    <field name="currency_id" column_invisible="1"/>
                                    <button name="action_create_entry"
                                            string="Pay"
                                            type="object"
                                            class="btn-link"
                                            icon="fa-money"
                                            invisible="is_paid or parent.state != 'running'"/>
                                </list>
                            </field>
                        </page>
                        <page string="Journal Entries" name="entries">
                            <field name="move_ids" readonly="1" nolabel="1">
                                <list create="0" delete="0">
                                    <field name="date"/>
                                    <field name="name"/>
                                    <field name="ref"/>
                                    <field name="amount_total_signed" string="Amount"/>
                                    <field name="state"/>
                                </list>
                            </field>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ============================================================ -->
    <!--                    LOAN LIST VIEW                            -->
    <!-- ============================================================ -->
    <record id="fusion_loan_view_list" model="ir.ui.view">
        <field name="name">fusion.loan.list</field>
        <field name="model">fusion.loan</field>
        <field name="arch" type="xml">
            <list string="Loans"
                  decoration-info="state == 'draft'"
                  decoration-success="state == 'running'"
                  decoration-muted="state == 'cancelled'"
                  decoration-bf="state == 'running'">
                <field name="name"/>
                <field name="partner_id"/>
                <field name="principal_amount" sum="Total Principal"/>
                <field name="interest_rate" string="Rate (%)"/>
                <field name="loan_term" string="Term (Mo.)"/>
                <field name="start_date"/>
                <field name="payment_frequency"/>
                <field name="amortization_method"/>
                <field name="remaining_balance" sum="Total Remaining"/>
                <field name="state"
                       decoration-info="state == 'draft'"
                       decoration-success="state == 'running'"
                       decoration-danger="state == 'cancelled'"
                       widget="badge"/>
                <field name="currency_id" column_invisible="1"/>
            </list>
        </field>
    </record>

    <!-- ============================================================ -->
    <!--                    LOAN SEARCH VIEW                          -->
    <!-- ============================================================ -->
    <record id="fusion_loan_view_search" model="ir.ui.view">
        <field name="name">fusion.loan.search</field>
        <field name="model">fusion.loan</field>
        <field name="arch" type="xml">
            <search string="Search Loans">
                <field name="name"/>
                <field name="partner_id"/>
                <separator/>
                <filter name="filter_draft" string="Draft"
                        domain="[('state', '=', 'draft')]"/>
                <filter name="filter_running" string="Running"
                        domain="[('state', '=', 'running')]"/>
                <filter name="filter_paid" string="Paid"
                        domain="[('state', '=', 'paid')]"/>
                <separator/>
                <filter name="filter_french" string="French"
                        domain="[('amortization_method', '=', 'french')]"/>
                <filter name="filter_linear" string="Linear"
                        domain="[('amortization_method', '=', 'linear')]"/>
                <separator/>
                <group>
                    <filter name="group_state" string="Status"
                            context="{'group_by': 'state'}"/>
                    <filter name="group_partner" string="Lender"
                            context="{'group_by': 'partner_id'}"/>
                    <filter name="group_method" string="Method"
                            context="{'group_by': 'amortization_method'}"/>
                    <filter name="group_frequency" string="Frequency"
                            context="{'group_by': 'payment_frequency'}"/>
                    <filter name="group_start" string="Start Date"
                            context="{'group_by': 'start_date:month'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- ============================================================ -->
    <!--                    ACTION & MENUS                            -->
    <!-- ============================================================ -->
    <record id="action_fusion_loan" model="ir.actions.act_window">
        <field name="name">Loans</field>
        <field name="res_model">fusion.loan</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="fusion_loan_view_search"/>
        <field name="context">{'search_default_filter_running': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first loan
            </p>
            <p>
                Track loans, generate amortization schedules, and create
                journal entries automatically.
            </p>
        </field>
    </record>

    <!-- Menu: Accounting > Accounting > Loans -->
    <menuitem id="menu_fusion_loan"
              name="Loans"
              parent="account.menu_finance_entries"
              action="action_fusion_loan"
              sequence="70"
              groups="account.group_account_user"/>

    <!-- Import wizard action (toolbar) -->
    <record id="action_fusion_loan_import_wizard" model="ir.actions.act_window">
        <field name="name">Import Loans</field>
        <field name="res_model">fusion.loan.import.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
    </record>

    <!-- Menu: Import Loans sub-item -->
    <menuitem id="menu_fusion_loan_import"
              name="Import Loans"
              parent="menu_fusion_loan"
              action="action_fusion_loan_import_wizard"
              sequence="10"
              groups="account.group_account_manager"/>

</odoo>
