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

    <!-- Add Filter for "Needs Payment" -->
    <record id="payslip_filter_needs_payment" model="ir.ui.view">
        <field name="name">hr.payslip.select</field>
        <field name="model">hr.payslip</field>
        <field name="priority">20</field>
        <field name="inherit_id" ref="hr_payroll.view_hr_payslip_filter"/>
        <field name="arch" type="xml">
        <data>
             <xpath expr="//filter[last()]" position="after">
                 <filter string="Needs Payment" domain="[('is_paid', '=', False)]" help="Needs Payment Slip"/>
             </xpath>
        </data>
        </field>
    </record>

    <!-- Wizard view to register payment -->
    <record id="hr_payroll_register_payment_view_form" model="ir.ui.view">
        <field name="name">hr.payroll.register.payment.wizard.form</field>
        <field name="model">hr.payroll.register.payment.wizard</field>
        <field name="arch" type="xml">
            <form string="Register Payment">
                <sheet>
                    <field name="id" invisible="1"/>
                    <div class="oe_title">
                        <h1>Draft Payment</h1>
                    </div>
                    <group>
                        <group>
                            <field name="partner_id" required="1" context="{'default_is_company': True, 'default_supplier': True}"/>
                            <field name="journal_id" widget="selection"/>
                            <field name="hide_payment_method" invisible="1"/>
                            <field name="payment_method_id" widget="radio" attrs="{'invisible': [('hide_payment_method', '=', True)]}"/>
                            <field name="payment_method_code" invisible="1"/>
                            <field name="payment_token_id" options="{'no_create': True}"
                                attrs="{'invisible': [('payment_method_code', '!=', 'electronic')],
                                        'required': [('payment_method_code', '=', 'electronic')]}"/>
                            <label for="amount"/>
                            <div name="amount_div" class="o_row">
                                <field name="amount"/>
                                <field name="currency_id" options="{'no_create': True, 'no_open': True}" groups="base.group_multi_currency"/>
                            </div>
                        </group>
                        <group>
                            <field name="payment_date"/>
                            <field name="communication"/>
                            <field name="payment_transaction_id"/>
                        </group>
                    </group>
                </sheet>
                <footer>
                    <button string='Validate' name="payroll_post_payment" type="object" class="btn-primary"/>
                    <button string="Cancel" class="btn-default" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- Action to launch Wizard for register payment -->
    <record id="hr_payroll_register_payment_wizard_action" model="ir.actions.act_window">
        <field name="name">Register Payment</field>
        <field name="res_model">hr.payroll.register.payment.wizard</field>
        <field name="view_type">form</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="hr_payroll_register_payment_view_form"/>
        <field name="target">new</field>
        <field name="context">{'default_payment_type': 'inbound'}</field>
        <field name="domain">[('partner_type', '=', 'customer')]</field>
    </record>

    <!-- Button on Payslip to launch Wizard for register payment -->
    <record id="hr_payslip_form" model="ir.ui.view">
        <field name="name">hr.payslip.form</field>
        <field name="model">hr.payslip</field>
        <field name="inherit_id" ref="hr_payroll.view_hr_payslip_form"/>
        <field name="arch" type="xml">
            <xpath expr="//button[@name='refund_sheet']" position="before">
                <button name="%(hr_payroll_payment.hr_payroll_register_payment_wizard_action)d" type="action" string="Register Payment" class="oe_highlight" attrs="{'invisible': ['|', ('state', '!=', 'done'), ('is_paid', '=', True)]}" groups="account.group_account_user"/>
            </xpath>
            <xpath expr="//field[@name='credit_note']" position="after">
                <field name="is_paid" readonly="1"/>
            </xpath>
        </field>
    </record>
</odoo>