<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="view_bank_statement_form_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.bank.statement.form.bank_rec_widget</field>
            <field name="model">account.bank.statement</field>
            <field name="priority">10</field>
            <field name="arch" type="xml">
                <form string="Bank Statement">
                    <div class="alert alert-warning text-center" role="alert" invisible="is_valid and is_complete">
                        <field name="problem_description"/>
                    </div>
                    <sheet>
                        <div class="oe_button_box" name="button_box">
                            <button name="action_open_bank_reconcile_widget" type="object"
                                class="oe_stat_button" invisible="not line_ids">
                            <field name="line_ids" widget="statinfo" />
                        </button>
                        </div>
                        <group>
                            <field name="create_date" invisible="1"/>
                            <field name="is_valid" invisible="1"/>
                            <field name="is_complete" invisible="1"/>
                            <field name="currency_id" invisible="1"/>
                            <field name="journal_id" invisible="1"/>
                            <field name="name" required="1" />
                            <field name="balance_start"/>
                            <field name="balance_end_real"/>
                            <field name="attachment_ids" widget="many2many_binary"/>
                        </group>
                    </sheet>
                    <div class="o_attachment_preview"/>
                    <chatter/>
                </form>
            </field>
        </record>

        <record id="action_bank_statement_form_bank_rec_widget" model="ir.actions.act_window">
            <!-- Binding removed since the same can be achieved with multi-edit -->
            <field name="name">Create Statement</field>
            <field name="res_model">account.bank.statement</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_bank_statement_form_bank_rec_widget"/>
            <field name="target">new</field>
            <field name="context">{'dialog_size': 'medium', 'show_running_balance_latest': True}</field>
        </record>

        <!-- Search view on aml to be injected inside the form (view_bank_rec_widget_form) -->
        <record id="view_bank_statement_line_search_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.bank.statement.line.search.bank_rec_widget</field>
            <field name="model">account.bank.statement.line</field>
            <field name="priority">999</field>
            <field name="arch" type="xml">
                <search>
                    <field name="name"
                           string="Transaction"
                           filter_domain="['|', '|', '|', '|', '|', '|', ('payment_ref', 'ilike', self), ('narration', 'ilike', self), ('partner_id', 'ilike', self), ('partner_name', 'ilike', self), ('partner_bank_id', 'ilike', self), ('account_number', 'ilike', self), ('amount', 'ilike', self)]"/>
                    <field name="id" invisible="1"/>
                    <field name="payment_ref"/>
                    <field name="date"/>
                    <field name="statement_id"/>
                    <field name="partner_id"/>
                    <field name="journal_id"
                           domain="[('type', 'in', ('bank', 'cash'))]"
                           invisible="context.get('default_journal_id')"/>
                    <field name="narration" string="Notes"/>
                    <field name="transaction_type"/>
                    <field name="amount"/>
                    <field name="is_reconciled"/>
                    <field name="move_id"
                           string="Journal Entry"
                           filter_domain="['|', ('move_id.name', 'ilike', self), ('move_id.ref', 'ilike', self)]"/>
                    <field name="statement_line_id" string="Statement Line"/>
                    <separator/>
                    <filter name="deposits" string="Deposits" domain="[('amount', '>', 0.0)]"/>
                    <filter name="payments" string="Payments" domain="[('amount', '&lt;', 0.0)]"/>
                    <separator/>
                    <filter name="no_statement" string="No statement" domain="[('statement_id', '=', False)]"/>
                    <filter name="invalid_statement" string="Invalid statements"
                            domain="['|',('statement_complete', '=', False), ('statement_valid', '=', False)]"/>
                    <separator/>
                    <filter name="matched" string="Matched" domain="[('is_reconciled', '=', True)]"/>
                    <filter name="not_matched" string="Not Matched"
                            domain="[('is_reconciled', '=', False), ('checked', '=', True)]"/>
                    <filter name="to_check" string="To Check" domain="[('checked', '=', False)]"/>
                    <separator/>
                    <filter name="date" string="Date" date="date"/>
                    <separator/>
                    <filter name="statement_group" string="Statement" context="{'group_by': 'statement_id'}"/>
                    <filter name="journal_group" string="Journal" context="{'group_by': 'journal_id'}"/>
                    <filter name="date_group" string="Date" context="{'group_by': 'date'}"/>
                    <filter name="invoice_date_group" string="Invoice Date" domain="[]" context="{'group_by': 'invoice_date'}"/>
                </search>
            </field>
        </record>

        <!-- Kanban view on statement line injected inside the form (view_bank_rec_widget) -->
        <record id="view_bank_statement_line_kanban_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.bank.statement.line.kanban.bank_rec_widget</field>
            <field name="model">account.bank.statement.line</field>
            <field name="arch" type="xml">
                <kanban js_class="bank_rec_widget_kanban"
                        on_create="quick_create"
                        quick_create_view="odex30_account_accountant.view_bank_statement_line_quick_create_form_bank_rec_widget">
                    <field name="id"/>
                    <field name="state"/>
                    <field name="statement_complete"/>
                    <field name="statement_valid"/>
                    <field name="statement_balance_end_real"/>
                    <field name="statement_name"/>
                    <field name="journal_id"/>
                    <field name="statement_id"/>
                    <field name="is_reconciled"/>
                    <field name="checked"/>
                    <field name="currency_id"/>
                    <templates>
                        <t t-name="card">
                            <div t-if="!record.statement_id.raw_value"
                                 class="statement_separator text-center">
                                <a role="button" class="btn btn-secondary btn-sm"
                                   context="{'split_line_id': id, 'default_journal_id': journal_id}"
                                   tabindex="-1"
                                   type="action" name="%(odex30_account_accountant.action_bank_statement_form_bank_rec_widget)d">
                                    Statement
                                </a>
                            </div>

                            <div t-att-st-line-id="record.id.raw_value"
                                 t-att-st-line-todo="!record.is_reconciled.raw_value || !record.checked.raw_value"
                                 style="width:100%">

                                <t t-set="text_amount_class"
                                   t-value="record.amount.raw_value &lt; 0.0 and 'text-danger'"/>

                                <div class="d-flex">
                                    <!-- Row 1 -->
                                    <field name="date" class="fw-bolder text-nowrap" readonly="state in ['cancel', 'posted']"/>
                                    <field name="partner_id" class="flex-fill text-truncate ms-1 fst-italic ps-2"/>
                                    <div t-att-class="text_amount_class">
                                        <field name="amount" class="fw-bolder text-nowrap"/>
                                    </div>
                                </div>

                                    <!-- Row 2 -->
                                <div class="d-flex">
                                    <field name="payment_ref" class="text-truncate flex-fill"/>
                                    <div class="m-1 d-flex flex-row flex-nowrap">
                                        <span t-if="!record.checked.raw_value" class="badge text-bg-warning fw-normal">To check</span>
                                        <div t-if="record.is_reconciled.raw_value" class="badge text-bg-success fw-normal" title="Matched">
                                            <i class="fa fa-check"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </t>
                    </templates>
                </kanban>
            </field>
        </record>

        <record id="view_bank_statement_line_tree_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.bank.statement.line.list.bank_rec_widget</field>
            <field name="model">account.bank.statement.line</field>
            <field name="arch" type="xml">
                <list editable="top"
                      multi_edit="1"
                      js_class="bank_rec_list"
                      decoration-muted="state == 'draft'"
                      default_order="internal_index desc">
                    <!--Invisible fields-->
                    <field name="statement_complete" column_invisible="True"/>
                    <field name="statement_valid" column_invisible="True"/>
                    <field name="state" column_invisible="True"/>
                    <field name="is_reconciled" column_invisible="True"/>
                    <field name="checked" column_invisible="True"/>
                    <field name="country_code" column_invisible="True"/>
                    <field name="currency_id" column_invisible="True"/>
                    <field name="company_id" column_invisible="True"/>

                    <!--Visible fields-->
                    <field name="sequence" widget="handle" column_invisible="True"/> <!--Until we add the JS class-->
                    <field name="date" readonly="is_reconciled and amount != 0"
                           options="{'warn_future': true}"/>
                    <field name="move_id" optional="hidden" required="0"/>
                    <field name="payment_ref" required="1" readonly="is_reconciled and amount != 0"/>
                    <field name="partner_id" readonly="is_reconciled and amount != 0"/>
                    <field name="account_number" optional="hidden"/>
                    <field name="narration"
                           string="Notes"
                           readonly="is_reconciled and amount != 0"
                           optional="hidden"/>
                    <field name="ref" readonly="is_reconciled and amount != 0" optional="hidden"/>
                    <field name="transaction_type" optional="hidden"/>
                    <field name="foreign_currency_id"
                           optional="hidden"
                           groups="base.group_multi_currency"
                           domain="[('id', '!=', currency_id)]"
                           options="{'no_open': True, 'no_create': True}"
                           readonly="is_reconciled and amount != 0"/>
                    <field name="amount_currency"
                           optional="hidden"
                           groups="base.group_multi_currency"
                           invisible="not foreign_currency_id"
                           readonly="is_reconciled and amount != 0"/>
                    <field name="amount" readonly="is_reconciled and amount != 0" />
                    <field name="journal_id"
                           domain="[('type', 'in', ['bank','cash'])]"
                           column_invisible="bool(context.get('default_journal_id'))"
                           readonly="(statement_id or is_reconciled) and amount != 0"/>
                    <field name="running_balance" class="oe_read_only"/>
                    <field name="statement_id"
                           optional="show"
                           options="{'no_quick_create': True}"
                           domain="['|',('journal_id', '=', journal_id),('journal_id', '=', False)]"
                           context="{
                                'default_journal_id':journal_id,
                                'form_view_ref': 'odex30_account_accountant.view_bank_statement_form_bank_rec_widget',
                                'st_line_id':id,
                            }"
                           decoration-danger="statement_complete == False or statement_valid == False"
                           widget="bank_rec_list_many2one_multi_id"
                           readonly="not id"/>
                    <field name="id"
                           widget="bank_rec_list_view_switcher"
                           invisible="not id"/>
                </list>
            </field>
        </record>

        <!-- Form view for adding statement lines on the fly in a popup-->
        <record id="view_bank_statement_line_form_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.bank.statement.line.form.bank_rec_widget</field>
            <field name="model">account.bank.statement.line</field>
            <field name="arch" type="xml">
                <form string="New Transaction">
                    <field name="state" invisible="1"/>
                    <field name="statement_complete" invisible="1"/>
                    <field name="statement_valid" invisible="1"/>
                    <field name="is_reconciled" invisible="1"/>
                    <field name="suitable_journal_ids" invisible="1"/>
                    <field name="currency_id" invisible="1"/>
                    <field name="company_id" invisible="1"/>
                    <group>
                        <group>
                            <field name="date" readonly="is_reconciled and amount != 0"/>
                            <field name="payment_ref"
                                   required="1"
                                   readonly="is_reconciled and amount != 0"/>
                            <field name="partner_id"
                                   readonly="is_reconciled and amount != 0"/>
                        </group>
                        <group>
                            <field name="amount" readonly="is_reconciled and amount != 0"/>
                            <field name="foreign_currency_id"
                                   groups="base.group_multi_currency"
                                   domain="[('id', '!=', currency_id)]"
                                   options="{'no_open': True, 'no_create': True}"
                                   readonly="is_reconciled and amount != 0"/>
                            <field name="amount_currency"
                                   groups="base.group_multi_currency"
                                   invisible="not foreign_currency_id"
                                   readonly="is_reconciled and amount != 0"/>
                            <field name="journal_id"
                                   domain="[('type', 'in', ['bank', 'cash'])]"
                                   readonly="context.get('default_journal_id') or (is_reconciled and amount != 0)"/>
                        </group>
                    </group>
                    <footer>
                        <button string="Save &amp; Close" type="object"
                                name="action_save_close" class="btn btn-primary"
                                data-hotkey="q"/>
                        <button string="Save &amp; New" type="object"
                                name="action_save_new" class="btn btn-primary"
                                data-hotkey="n"/>
                        <button string="Cancel" class="btn-secondary"
                                special="cancel" data-hotkey="x"/>
                    </footer>
                </form>
            </field>
        </record>

        <!-- Form view for quick creating statement lines -->
        <record id="view_bank_statement_line_quick_create_form_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.bank.statement.line.form.bank_rec_widget</field>
            <field name="model">account.bank.statement.line</field>
            <field name="arch" type="xml">
                <form string="New Transaction">
                    <field name="journal_id" invisible="1"/>
                    <field name="currency_id" invisible="1"/>
                    <group>
                        <field name="date" readonly="is_reconciled and amount != 0" nolabel="1"/>
                        <field name="partner_id"
                            readonly="is_reconciled and amount != 0"
                            nolabel="1"
                            placeholder="Partner"/>
                        <field name="amount" readonly="is_reconciled and amount != 0" nolabel="1"/>
                    </group>
                    <field name="payment_ref"
                        required="1"
                        readonly="is_reconciled and amount != 0"
                        nolabel="1"
                        placeholder="Memo"/>
                </form>
            </field>
        </record>

        <record id="action_bank_statement_line_form_bank_rec_widget" model="ir.actions.act_window">
            <field name="name">New Transaction</field>
            <field name="res_model">account.bank.statement.line</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_bank_statement_line_form_bank_rec_widget"/>
            <field name="target">new</field>
        </record>

        <!--
            The action record of Bank Reconciliation has a duplicate one below it with different view_mode (and path).
            This was needed because the function "_action_open_bank_reconciliation_widget" was expected to return 2 
            different view_modes namely list,kanban and kanban,list depending on the boolean "kanban_first".
            The solution of changing the action dict was not useful because the dict view_mode would be erased on page refresh.
        -->
        <record id="action_bank_statement_line_transactions" model="ir.actions.act_window">
            <field name="name">Bank Reconciliation</field>
            <field name="res_model">account.bank.statement.line</field>
            <field name="view_mode">list,kanban</field>
            <field name="search_view_id" ref="view_bank_statement_line_search_bank_rec_widget"/>
            <field name="view_id" ref="view_bank_statement_line_tree_bank_rec_widget"/>
            <field name="path">reconciliation_list</field>
            <field name="domain">[('state', '!=', 'cancel')]</field>
            <field name="context">{'default_journal_id': active_id, 'search_default_journal_id': active_id}</field>
            <field name="help" type="html">
                <p class='o_view_nocontent_smiling_face'>Nothing to do here!</p>
                <p>No transactions matching your filters were found.</p>
            </field>
        </record>

        <record id="action_bank_statement_line_transactions_kanban" model="ir.actions.act_window">
            <field name="name">Bank Reconciliation</field>
            <field name="res_model">account.bank.statement.line</field>
            <field name="view_mode">kanban,list</field>
            <field name="search_view_id" ref="view_bank_statement_line_search_bank_rec_widget"/>
            <field name="view_id" ref="view_bank_statement_line_kanban_bank_rec_widget"/>
            <field name="path">reconciliation</field>
            <field name="domain">[('state', '!=', 'cancel')]</field>
            <field name="context">{'default_journal_id': active_id, 'search_default_journal_id': active_id}</field>
            <field name="help" type="html">
                <p class='o_view_nocontent_smiling_face'>Nothing to do here!</p>
                <p>No transactions matching your filters were found.</p>
            </field>
        </record>

        <record id="view_move_form_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.move.form.bank_rec_widget</field>
            <field name="model">account.move</field>
            <field name="priority">999</field>
            <field name="arch" type="xml">
                <form>
                    <chatter/>
                </form>
            </field>
        </record>

        <!-- Search view on aml to be injected inside the form (view_bank_rec_widget_form) -->
        <record id="view_account_move_line_search_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.move.line.search.bank_rec_widget</field>
            <field name="model">account.move.line</field>
            <field name="priority">999</field>
            <field name="arch" type="xml">
                <search>
                    <field name="name"
                           string="Journal Item"
                           filter_domain="['|', '|', '|', '|', '|', ('name', 'ilike', self), ('ref', 'ilike', self), ('partner_id', 'ilike', self), ('amount_residual', 'ilike', self), ('amount_residual_currency', 'ilike', self), ('discount_amount_currency', 'ilike', self)]"/>
                    <field name="invoice_date"/>
                    <field name="date"/>
                    <field name="account_id"/>
                    <field name="partner_id"/>
                    <field name="journal_id"/>
                    <field name="currency_id" groups="base.group_multi_currency"/>
                    <field name="move_id"
                           string="Journal Entry"
                           filter_domain="['|', ('move_id.name', 'ilike', self), ('move_id.ref', 'ilike', self)]"/>
                    <separator/>
                    <filter name="amount_received" string="Incoming" domain="[('balance', '>', 0.0)]"/>
                    <filter name="amount_paid" string="Outgoing" domain="[('balance', '&lt;', 0.0)]"/>
                    <separator name="inject_after"/>
                    <filter name="date" string="Date" date="date"/>
                    <filter name="invoice_date" string="Invoice Date" date="invoice_date"/>
                </search>
            </field>
        </record>

        <!-- List view on aml to be injected inside the form (view_bank_rec_widget_form) -->
        <record id="view_account_move_line_list_bank_rec_widget" model="ir.ui.view">
            <field name="name">account.move.line.list.bank_rec_widget</field>
            <field name="model">account.move.line</field>
            <field name="priority">999</field>
            <field name="arch" type="xml">
                <list string="Suggestions"
                      create="false"
                      edit="false"
                      limit="40"
                      count_limit="40"
                      js_class="bank_rec_amls_list_view">
                    <!-- Invisible fields -->
                    <field name="company_id" column_invisible="True"/>
                    <field name="payment_id" column_invisible="True"/>
                    <field name="currency_id" column_invisible="True"/>
                    <field name="company_currency_id" column_invisible="True"/>

                    <field name="account_id"
                           options="{'no_open': True, 'no_create': True}"
                           groups="account.group_account_readonly"
                           optional="show"/>
                    <field name="journal_id"
                           optional="hidden"/>
                    <field name="partner_id"
                           optional="show"/>
                    <field name="invoice_date" string="Invoice Date" optional="hide"/>
                    <field name="date"/>
                    <field name="move_id" string="Document" groups="!account.group_account_readonly"/>
                    <field name="move_id" groups="account.group_account_readonly"/>
                    <field name="name"/>
                    <field name="date_maturity" optional="hidden"/>
                    <field name="ref"
                           optional="hidden"/>
                    <field name="analytic_distribution" widget="analytic_distribution"
                           groups="analytic.group_analytic_accounting"
                           optional="hidden"
                           options="{'account_field': 'account_id', 'business_domain': 'general'}"/>
                    <field name="amount_residual_currency"
                           decoration-danger="amount_residual_currency &lt; 0"
                           groups="base.group_multi_currency"
                           string="Amount Due (in currency)"/>
                    <field name="amount_residual"
                           decoration-danger="amount_residual &lt; 0"
                           string="Amount Due"/>

                    <button name="action_open_business_doc"
                            type="object"
                            string="View"
                            class="btn btn-sm btn-secondary"/>
                </list>
            </field>
        </record>

        <!-- Add bank recon widget to the statements list view -->
        <record id="view_bank_statement_tree" model="ir.ui.view">
            <field name="name">account.bank.statement.list</field>
            <field name="model">account.bank.statement</field>
            <field name="inherit_id" ref="account.view_bank_statement_tree"/>
            <field name="arch" type="xml">
                <list position="inside">
                    <button name="action_open_bank_reconcile_widget" string="Transactions" type="object"/>
                </list>
            </field>
        </record>

        <record model="ir.actions.server" id="action_bank_statement_attachment">
            <field name="name">Statement</field>
            <field name="model_id" ref="model_account_bank_statement"/>
            <field name="binding_model_id" ref="model_account_bank_statement" />
            <field name="binding_type">report</field>
            <field name="state">code</field>
            <field name="code">
if records:
    action = records.action_generate_attachment()
            </field>
        </record>
    </data>
</odoo>
