<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) EduSense BV <http://www.edusense.nl>
  All rights reserved.
  The licence is in the file __terp__.py
-->
<openerp>
    <data>
        <!-- Create new submenu in finance/periodical processing -->
        <menuitem name="Banking"
            id="account_banking.menu_finance_banking_actions"
            parent="account.menu_finance_periodical_processing"
            />
        <!-- Create new submenu in finance/configuration -->
        <menuitem name="Banking"
            id="account_banking.menu_finance_banking_settings"
            parent="account.menu_finance_configuration"
            />
        <menuitem action="account.action_view_bank_statement_tree"
            id="account.journal_cash_move_lines"
            sequence="18"
            parent="account.menu_finance_bank_and_cash"/>
        <!-- Add a shortcut menu for bank accounts -->
        <record model="ir.actions.act_window" id="action_account_banking_res_partner_banks">
            <field name="name">Bank Accounts</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">res.partner.bank</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>
        <menuitem name="Bank Accounts"
            id="menu_action_account_banking_bank_accounts"
            parent="account_banking.menu_finance_banking_settings"
            action="action_account_banking_res_partner_banks"
            sequence="10"
         />

        <!-- Create new view on default journals for bank accounts -->
        <record model="ir.ui.view" id="view_banking_account_settings_form">
            <field name="name">account.banking.account.settings.form</field>
            <field name="model">account.banking.account.settings</field>
            <field name="arch" type="xml">
                <form string="Default Import Settings for Bank Account">
                    <field name="company_id"
                           widget='selection'
                           groups="base.group_multi_company"
                           on_change="onchange_company_id(company_id)" />
                    <separator string="Bank Account Details" colspan="4" />
                    <field name="partner_id" invisible="1"/>
                    <field name="partner_bank_id"
                           domain="[('partner_id', '=', partner_id)]"
                           on_change="onchange_partner_bank_id(partner_bank_id)" />
                    <field name="journal_id"
                           domain="[('type','=','bank'),
                                    ('company_id', '=', company_id)]" />
                    <separator string="Default Accounts for Unknown Movements" colspan="4" />
                    <field name="default_credit_account_id"
                           domain="[('company_id', '=', company_id)]" />
                    <field name="default_debit_account_id"
                           domain="[('company_id', '=', company_id)]" />
                    <separator string="Generation of Bank Costs Invoices" colspan="4" />
                    <field name="bank_partner_id" />
                    <field name="costs_account_id"
                           attrs="{'required': [('bank_partner_id', '!=', False)]}"
                           domain="[('company_id', '=', company_id)]" />
                    <field name="invoice_journal_id"
                           attrs="{'required': [('bank_partner_id', '!=', False)]}"
                           domain="[('company_id', '=', company_id)]" />
                </form>
            </field>
        </record>
        <record model="ir.ui.view" id="view_banking_account_settings_tree">
            <field name="name">account.banking.account.settings.tree</field>
            <field name="model">account.banking.account.settings</field>
            <field name="arch" type="xml">
                <tree string="Default Import Settings for Bank Account">
                    <field name="company_id" />
                    <field name="partner_bank_id" />
                    <field name="journal_id" />
                </tree>
            </field>
        </record>
        <record model="ir.actions.act_window" id="action_account_banking_journals">
            <field name="name">Default Import Settings for Bank Accounts</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">account.banking.account.settings</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>

        <!-- Create new submenu for finance configuration -->
        <menuitem name="Default Import Settings for Bank Accounts"
            id="menu_action_account_banking_bank_journals"
            parent="account_banking.menu_finance_banking_settings"
            action="action_account_banking_journals"
            sequence="20"
         />

        <!-- Create new view on imported statement files -->
        <record model="ir.ui.view" id="view_account_banking_imported_file_form">
            <field name="name">account.banking.imported.file.form</field>
            <field name="model">account.banking.imported.file</field>
            <field name="arch" type="xml">
                <form string="Imported Bank Statements">
                    <notebook colspan="4">
                        <page string="Import Details">
                            <field name="company_id" />
                            <field name="date" />
                            <field name="user_id" />
                            <field name="state" />
                            <field name="file" filename="file_name"/>
                            <field name="file_name" invisible="1"/>
                            <field name="format" />
                        </page>
                        <page string="Statements">
                            <field name="statement_ids" colspan="4" nolabel="1"/>
                        </page>
                        <page string="Import Log">
                            <field name="log" colspan="4" nolabel="1"/>
                        </page>
                    </notebook>
                </form>
            </field>
        </record>
        <record model="ir.ui.view" id="view_account_banking_imported_file_tree">
            <field name="name">account.banking.imported.file.tree</field>
            <field name="model">account.banking.imported.file</field>
            <field name="arch" type="xml">
                <tree string="Imported Bank Statements Files" colors="red:state=='error';blue:state=='unfinished'">
                    <field name="company_id" />
                    <field name="date" />
                    <field name="user_id" />
                    <field name="state" />
                </tree>
            </field>
        </record>

        <record model="ir.actions.act_window" id="action_account_banking_imported_files">
            <field name="name">Imported Bank Statements Files</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">account.banking.imported.file</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>

        <!-- Add a menu item for it -->
        <menuitem name="Imported Bank Statements Files"
            id="menu_action_account_banking_imported_files"
            parent="account.menu_finance_bank_and_cash"
            action="action_account_banking_imported_files"
            sequence="15"
         />

        <!-- Add the import wizard to the menu -->
        <menuitem name="Import Bank Statements File"
            id="menu_account_banking_import_wizard"
            parent="account.menu_finance_bank_and_cash"
            action="wizard_account_banking_import_file"
            sequence="12"/>

        <!-- Add the import wizard to the statement's right menu -->
        <act_window name="Import Bank Statements File"
            res_model="account.banking.bank.import"
            src_model="account.bank.statement"
            view_mode="form"
            target="new"
            key2="client_action_multi"
            id="act_account_banking_import_wizard"/>

        <!-- Create right menu entry to see statements -->
        <act_window name="Bank Statements File"
            domain="[('id','=',banking_id)]"
            res_model="account.banking.imported.file"
            src_model="account.bank.statement"
            view_type="form"
            view_mode="tree,form"
            id="act_account_payment_account_bank_statement"/>

        <!-- Move period_id from bank_statement form to bank_statement_line form
        -->
        <record id="view_banking_bank_statement_tree_1" model="ir.ui.view">
            <field name="name">account.bank.statement.tree.banking</field>
            <field name="inherit_id" ref="account.view_bank_statement_tree" />
            <field name="model">account.bank.statement</field>
            <field name="arch" type="xml">
                <!-- Remove period from bank statement -->
                <field name="period_id" position="replace">
                    <!-- Add invisible column for identification of import file
                    -->
                    <field name="banking_id" invisible="True" />
                </field>
            </field>
        </record>
        <record id="view_banking_bank_statement_form_1" model="ir.ui.view">
            <field name="name">account.bank.statement.form.banking-1</field>
            <field name="inherit_id" ref="account.view_bank_statement_form" />
            <field name="model">account.bank.statement</field>
            <field name="arch" type="xml">
                <data>
                    <page string="Transactions" position="after">
                        <page string="Journal Entries" name="move_live_ids">
                            <field colspan="4" name="move_line_ids" nolabel="1"/>
                        </page>
                    </page>
                    <field name="period_id" position="replace"/>

                    <xpath expr="//page[@string='Transactions']/field/tree"
                           position="attributes">
                        <attribute name="colors">black:state == 'confirmed';darkmagenta:match_multi == True;crimson:duplicate == True;grey:state == 'draft';</attribute>
                    </xpath>

                    <xpath expr="//field[@name='line_ids']/tree/field[@name='name']"
                           position="attributes">
                        <attribute name="required">1</attribute>
                    </xpath>

                    <xpath expr="//field[@name='line_ids']/form//field[@name='name']"
                           position="attributes">
                        <attribute name="required">1</attribute>
                    </xpath>

                    <!-- Add invisible field for identification of import file
                         on bank statements
                    -->
                    <field name="balance_end_real" position="after">
                        <field name="banking_id" invisible="True"/>
                    </field>

                    <!-- Show bank accounts in account_bank_statement_line to
                         enable manualcoupling of bank account numbers to
                         statement lines and harvest info for future matching
                         in the process.
                    -->
                    <xpath expr="//field[@name='line_ids']/tree/field[@name='partner_id']"
                           position="after">
                        <field name="link_partner_ok" invisible="1" />
                        <button name="link_partner"
                                string="Link partner"
                                icon="terp-partner"
                                type="object"
                                attrs="{'invisible': [('link_partner_ok', '=', False)]}"
                                />
                        <!-- TODO set partner_id when partner_bank_id changes -->
                        <field name="partner_bank_id"/>
                    </xpath>
                    <xpath expr="//field[@name='line_ids']/form//field[@name='partner_id']"
                           position="after">
                        <field name="partner_bank_id"/>
                    </xpath>
                    <xpath expr="//field[@name='line_ids']/tree//field[@name='account_id']"
                           position="attributes">
                       <attribute name="attrs">{'readonly': ['|', ('state', '!=', 'draft'), ('match_type', '!=', False)]}</attribute>
                    </xpath>
                    <xpath expr="//field[@name='line_ids']/tree/field[@name='amount']"
                           position="after">
                        <field name="match_type"/>
                        <field name="residual"/>
                        <field name="parent_id" invisible="1" />
                        <button name="match_wizard"
                                string="Match"
                                icon="terp-gtk-jump-to-ltr"
                                attrs="{'invisible': ['|', ('parent_id', '!=', False),
                                                           ('state', '!=', 'draft')]}"
                                type="object"
                                />
                        <field name="match_multi" invisible="1"/>
                        <field name="duplicate" invisible="1"/>
                        <field name="state"/>
                        <button name="confirm" states="draft"
                                string="Confirm transaction"
                                icon="gtk-ok"
                                type="object"/>
                        <button name="cancel" states="confirmed"
                                string="Cancel transaction"
                                icon="gtk-cancel"
                                type="object"/>
                        <field name="invoice_id"/>
                        <field name="reconcile_id"/>
                    </xpath>
                    <!-- Many fields readonly when state != draft -->
                    <xpath
                        expr="//field[@name='line_ids']/tree/field[@name='amount']"
                        position="attributes"
                    >
                        <attribute name="attrs">{'readonly': [('state','!=','draft'),]}</attribute>
                    </xpath>
                    <xpath
                        expr="//field[@name='line_ids']/tree/field[@name='ref']"
                        position="attributes"
                    >
                        <attribute name="attrs">{'readonly': [('state','!=','draft'),]}</attribute>
                    </xpath>
                    <xpath
                        expr="//field[@name='line_ids']/tree/field[@name='name']"
                        position="attributes"
                    >
                        <attribute name="attrs">{'readonly': [('state','!=','draft'),]}</attribute>
                    </xpath>
                    <xpath
                        expr="//field[@name='line_ids']/tree/field[@name='date']"
                        position="attributes"
                    >
                        <attribute name="attrs">{'readonly': [('state','!=','draft'),]}</attribute>
                    </xpath>
                    <xpath
                        expr="//field[@name='line_ids']/tree/field[@name='partner_bank_id']"
                        position="attributes"
                    >
                        <attribute name="attrs">{'readonly': [('state','!=','draft'),]}</attribute>
                    </xpath>

                    <xpath expr="//field[@name='line_ids']/form//field[@name='amount']"
                           position="after">
                        <field name="state"/>
                        <field name="invoice_id"/>
                        <field name="reconcile_id"/>
                    </xpath>
                </data>
            </field>
        </record>

        <record model="ir.ui.view" id="view_bank_statement_line_tree">
            <field name="name">Bank statement line tree view</field>
            <field name="model">account.bank.statement.line</field>
            <field name="arch" type="xml">
                <tree string="Statement lines" colors="black:state == 'confirmed';darkmagenta:match_multi == True;crimson:duplicate == True;grey:state=='draft';">
                    <field name="sequence" readonly="1" invisible="1"/>
                    <field
                        name="date"
                        attrs="{'readonly': [('state','!=','draft'),]}"
                    />
                    <field
                        name="name"
                        attrs="{'readonly': [('state','!=','draft'),]}"
                    />
                    <field
                        name="ref"
                        attrs="{'readonly': [('state','!=','draft'),]}"
                    />
                    <field name="partner_id" on_change="onchange_partner_id(partner_id)"/>
                    <field name="link_partner_ok" invisible="1" />
                    <button name="link_partner"
                            string="Link partner"
                            icon="terp-partner"
                            type="object"
                            attrs="{'invisible': [('link_partner_ok', '=', False)]}"
                            />
                    <!-- TODO set partner_id when partner_bank_id changes -->
                    <field
                        name="partner_bank_id"
                        attrs="{'readonly': [('state','!=','draft'),]}"
                    />
                    <field name="type" on_change="onchange_type(partner_id, type)"/>
                    <!-- TODO note the references to parent from the statement form view -->
                    <field
                        domain="[('journal_id','=',parent.journal_id)]"
                        attrs="{'readonly': ['|', ('state', '!=', 'draft'), ('match_type', '!=', '')]}"
                        name="account_id"
                    />
                    <field
                        name="analytic_account_id"
                        groups="analytic.group_analytic_accounting"
                        domain="[('company_id', '=', parent.company_id), ('type', '&lt;&gt;', 'view')]"
                    />
                    <field
                        name="amount"
                        attrs="{'readonly': [('state','!=','draft'),]}"
                    />
                    <field name="match_type"/>
                    <field name="residual"/>
                    <field name="parent_id" invisible="1" />
                    <button name="match_wizard" states="draft"
                            string="Match"
                            icon="terp-gtk-jump-to-ltr"
                            attrs="{'invisible': ['|', ('parent_id', '!=', False),
                                                       ('state', '!=', 'draft')]}"
                            type="object"
                            />
                    <field name="match_multi" invisible="1"/>
                    <field name="duplicate" invisible="1"/>
                    <field name="state"/>
                    <button name="confirm" states="draft"
                            string="Confirm transaction"
                            icon="gtk-ok"
                            type="object"/>
                    <button name="cancel" states="confirmed"
                            string="Cancel transaction"
                            icon="gtk-cancel"
                            type="object"/>
                    <field name="invoice_id"/>
                    <field name="reconcile_id"/>
                </tree>
            </field>
        </record>

	<!-- search view for bank statement lines -->
        <record id="view_account_bank_statement_line_search" model="ir.ui.view">
            <field name="name">account.bank.statement.line.search</field>
            <field name="model">account.bank.statement.line</field>
            <field name="arch" type="xml">
                <search string="Search Bank Transactions ">
                    <group>
                        <filter string="Draft" domain="[('state','=','draft')]" icon="terp-camera_test"/>
                        <filter string="Confirmed" domain="[('state','=','confirmed')]" icon="terp-dialog-close"/>
                        <separator orientation="vertical"/>
                        <field name="ref"/>
                        <field name="type"/>
                        <field name="period_id"/>
                    </group>
                    <newline/>
                    <group>
                        <field name="date"/>
                        <field name="partner_id"/>
                        <field name="partner_bank_id"/>
                        <field name="account_id"/>
                    </group>
                    <newline/>
                    <group expand="0" string="Group By...">
                        <filter string="State" context="{'group_by': 'state'}" icon="terp-stock_effects-object-colorize"/>
                    </group>
                </search>
            </field>
        </record>


        <!-- Add a shortcut menu for bank accounts -->
        <record model="ir.actions.act_window" id="action_bank_statement_line_tree">
            <field name="name">Bank Transactions</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">account.bank.statement.line</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="search_view_id" ref="view_account_bank_statement_line_search"/>
        </record>
        <menuitem string="Bank Transactions"
		  action="action_bank_statement_line_tree"
		  id="menu_bank_statement_line_tree"
		  parent="account.menu_finance_bank_and_cash" sequence="8"/>
     </data>
</openerp>
