<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2011 Alexis de Lattre <alexis.delattre@akretion.com>
     Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
     License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

    <record id="account_loan_search" model="ir.ui.view">
        <field name="name">account.loan.tree</field>
        <field name="model">account.loan</field>
        <field name="arch" type="xml">
            <search>
                <field name="partner_id" />
                <field name="company_id" groups="base.group_multi_company" />
            </search>
        </field>
    </record>
    <record id="account_loan_tree" model="ir.ui.view">
        <field name="name">account.loan.tree</field>
        <field name="model">account.loan</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name" />
                <field name="partner_id" optional="hide" />
                <field name="company_id" />
                <field name="is_leasing" />
                <field name="state" />
                <field name="rate" optional="hide" />
                <field name="loan_amount" optional="hide" />
                <field name="pending_principal_amount" optional="hide" />
                <field name="periods" optional="hide" />
                <field name="start_date" optional="hide" />
            </tree>
        </field>
    </record>
    <record id="account_loan_form" model="ir.ui.view">
        <field name="name">account.loan.form</field>
        <field name="model">account.loan</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="compute_lines" type="object" string="Compute items" />
                    <button
                        name="%(account_loan_post_action)d"
                        states="draft"
                        type="action"
                        string="Post"
                        groups="account.group_account_manager"
                    />
                    <field name="state" widget="statusbar" />
                </header>
                <sheet>
                    <div name="button_box" class="oe_button_box">
                        <button
                            name="view_account_moves"
                            class="oe_stat_button"
                            icon="fa-bars"
                            attrs="{'invisible': [('state', '=', 'draft')]}"
                            type="object"
                            string="Moves"
                        />
                        <button
                            name="view_account_invoices"
                            class="oe_stat_button"
                            icon="fa-pencil-square-o"
                            attrs="{'invisible': ['|', ('state', '=', 'draft'), ('is_leasing', '=', False)]}"
                            type="object"
                            string="Invoices"
                        />
                        <button
                            name="%(account_loan_pay_amount_action)d"
                            class="oe_stat_button"
                            icon="fa-arrow-down"
                            attrs="{'invisible': [('state', '!=', 'posted')]}"
                            type="action"
                            groups="account.group_account_manager"
                        >
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Deduct</span>
                                <span class="o_stat_text">Debt</span>
                            </div>
                        </button>
                        <button
                            name="%(account_loan_increase_amount_act_window)d"
                            class="oe_stat_button"
                            icon="fa-arrow-up"
                            attrs="{'invisible': [('state', '!=', 'posted')]}"
                            type="action"
                            groups="account.group_account_manager"
                        >
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Increase</span>
                                <span class="o_stat_text">Debt</span>
                            </div>
                        </button>
                    </div>
                    <h1>
                        <field name="name" />
                    </h1>
                    <group>
                        <group>
                            <field name="company_id" options="{'no_create': True}" />
                            <field name="loan_type" />
                            <field name="loan_amount" />
                        </group>
                        <group>
                            <field name="rate_type" />
                            <field name="rate" />
                            <field name="rate_period" />
                        </group>
                    </group>
                    <group>
                        <group>
                            <field name="partner_id" />
                            <field name="start_date" />
                            <field name="periods" />
                            <field name="method_period" />
                        </group>
                        <group>
                            <field name="is_leasing" />
                            <field name="round_on_end" />
                            <field name="payment_on_first_period" />
                        </group>
                    </group>
                    <group attrs="{'invisible':[('state', '=', 'draft')]}">
                        <group>
                            <field name="pending_principal_amount" />
                            <field name="payment_amount" />
                            <field name="interests_amount" />
                        </group>
                    </group>
                    <notebook>
                        <page string="Items" id="items">
                            <field
                                name="line_ids"
                                context="{'tree_view_ref': 'account_loan.account_loan_line_tree'}"
                            />
                        </page>
                        <page string="Accounts" id="accounting">
                            <group>
                                <group>
                                    <field name="journal_id" />
                                    <field name="short_term_loan_account_id" />
                                    <field name="journal_type" invisible="1" />
                                </group>
                                <group>
                                    <field name="long_term_loan_account_id" />
                                    <field name="interest_expenses_account_id" />
                                    <field name="currency_id" invisible="1" />
                                </group>
                            </group>
                        </page>
                        <page
                            string="Leasing"
                            id="leasing"
                            attrs="{'invisible': [('is_leasing', '=', False)]}"
                        >
                            <group>
                                <group>
                                    <field
                                        name="leased_asset_account_id"
                                        attrs="{'required': [('is_leasing', '=', True)]}"
                                    />
                                    <field name="residual_amount" />
                                </group>
                                <group>
                                    <field
                                        name="product_id"
                                        attrs="{'required': [('is_leasing', '=', True)]}"
                                    />
                                    <field
                                        name="interests_product_id"
                                        attrs="{'required': [('is_leasing', '=', True)]}"
                                    />
                                    <field name="post_invoice" />
                                </group>
                            </group>
                        </page>
                    </notebook>
                </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="account_loan_action" model="ir.actions.act_window">
        <field name="name">Loans</field>
        <field name="res_model">account.loan</field>
    </record>

    <menuitem
        id="loan_menu"
        name="Loans"
        parent="account.menu_finance_entries"
        sequence="75"
    />

    <menuitem
        id="account_loan_menu"
        parent="loan_menu"
        sequence="10"
        name="Loans"
        action="account_loan_action"
    />
</odoo>
