<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record model="ir.actions.act_window" id="action_sales_order">
            <field name="name">Sales Orders</field>
            <field name="res_model">sale.order</field>
            <field name="src_model">account.analytic.account</field>
            <field name="help" type="html">
              <p class="oe_view_nocontent_create">
                Click to create a quotation that can be converted into a sales
                order.
              </p><p>
                Use sale orders to track everything that should be invoiced
                at a fix price on a contract.
              </p>
            </field>
        </record>

        <!-- Inherited Analytic Account form for contracts -->
        <record id="account_analytic_account_form_form" model="ir.ui.view">
            <field name="name">account.analytic.account.invoice.form.inherit</field>
            <field name="model">account.analytic.account</field>
            <field name="inherit_id" ref="analytic.view_account_analytic_account_form"/>
            <field eval="40" name="priority"/>
            <field name="arch" type="xml">
                <xpath expr='//div[@name="duration"]' position="after" version="7.0">
                    <label for="quantity_max"/>
                    <div>
                        <field name="quantity_max"/>
                        <div attrs="{'invisible': [('hours_quantity','=',0)]}" class="oe_grey">
                            <field name="hours_quantity" class="oe_inline"/> Units Consumed
                        </div>
                        <div attrs="{'invisible': [('quantity_max','=',0)]}" class="oe_grey">
                            <field name="remaining_hours" class="oe_inline"/> Units Remaining
                        </div>
                    </div>
                </xpath>
                <field name="partner_id" position="attributes">
                    <attribute name="attrs">{'required': [('type','=','contract'),'|',('fix_price_invoices','=',True), ('invoice_on_timesheets', '=', True)]}</attribute>
                </field>
                <xpath expr='//group[@name="invoice_on_timesheets"]' position="replace">
                </xpath>
                <xpath expr='//separator[@name="description"]' position='before'>
                    <separator name="toinvoice" string="Invoicing"/>
                    <table class="oe_form_analytic_account">
                        <tr>
                            <th class="oe_timesheet_grey" width="160px"></th>
                            <th class="oe_timesheet_grey" width="25px"></th>
                            <th class="oe_timesheet_grey" width="100px"><label string="Expected"/></th>
                            <th class="oe_timesheet_grey" width="100px"><label string="Invoiced"/></th>
                            <th class="oe_timesheet_grey" width="100px"><label string="Remaining"/></th>
                            <th class="oe_timesheet_grey" width="100px"><label string="To Invoice"/></th>
                            <th width="30px"></th>
                            <th></th>
                        </tr><tr>
                            <td class="oe_timesheet_grey">
                                <label for="fix_price_invoices"/>
                            </td><td class="oe_timesheet_grey">
                                <field name="fix_price_invoices" class="oe_inline"/>
                            </td><td>
                                <field class="oe_inline" name="amount_max" attrs="{'invisible': [('fix_price_invoices','=',False)]}"/>
                            </td><td>
                                <field class="oe_inline" name="ca_invoiced" attrs="{'invisible': [('fix_price_invoices','=',False)]}"/>
                            </td><td>
                                <field class="oe_inline" name="remaining_ca" attrs="{'invisible': [('fix_price_invoices','=',False)]}"/>
                            </td><td>
                                <field class="oe_inline" name="fix_price_to_invoice" attrs="{'invisible': [('fix_price_invoices','=',False)]}"/>
                            </td><td attrs="{'invisible': [('fix_price_invoices','=',False)]}" class="oe_timesheet_action">
                                <span attrs="{'invisible': [('fix_price_to_invoice','=',0.0)]}" class="oe_grey">
                                    <button name="open_sale_order_lines"
                                        class="oe_link"
                                        string="⇒ Invoice" type="object"
                                        context="{'default_partner_id': [partner_id],'default_project_id': active_id,'search_default_uninvoiced': 1,'search_default_project_id': active_id,'search_default_partner_id': [partner_id]}"/>
                                    or view
                                </span>

                                <span attrs="{'invisible': [('fix_price_to_invoice','&lt;&gt;',0.0 )]}" class="oe_grey">
                                    No order to invoice, create
                                </span>
                                <button name="%(action_sales_order)d" string="Sales Orders"
                                    type="action"
                                    class="oe_link"
                                    context="{'default_partner_id': [partner_id], 'search_default_project_id': [active_id],'default_project_id': [active_id]}"
                                    />
                            </td>
                        </tr><tr>
                            <td class="oe_timesheet_grey">
                                <label for="invoice_on_timesheets"/>
                            </td><td class="oe_timesheet_grey">
                                <field name="invoice_on_timesheets" on_change="onchange_invoice_on_timesheets(invoice_on_timesheets)"/>
                            </td><td>
                                <field class="oe_inline" name="hours_qtt_est" attrs="{'invisible': [('invoice_on_timesheets','=',False)]}"/>
                            </td><td>
                                <field class="oe_inline" name="timesheet_ca_invoiced" attrs="{'invisible': [('invoice_on_timesheets','=',False)]}"/>
                            </td><td>
                                <field class="oe_inline" name="remaining_hours_to_invoice" attrs="{'invisible': [('invoice_on_timesheets','=',False)]}"/>
                            </td><td>
                                <field class="oe_inline" name="ca_to_invoice" attrs="{'invisible': [('invoice_on_timesheets','=',False)]}"/>
                            </td><td class="oe_timesheet_action" attrs="{'invisible': ['|',('invoice_on_timesheets','=',False),('type','=','template')]}">
                                <span attrs="{'invisible': [('ca_to_invoice','=',0.0)]}" class="oe_grey">
                                    <button name="hr_to_invoice_timesheets"
                                        type="object"
                                        class="oe_link"
                                        string="⇒ Invoice"/>
                                    or view 
                                </span>
                                <span attrs="{'invisible': [('ca_to_invoice','&lt;&gt;',0.0)]}" class="oe_grey">
                                    Nothing to invoice, create 
                                </span>

                                <button name="%(hr_timesheet.act_hr_timesheet_line_evry1_all_form)d"
                                    string="Timesheets" type="action"
                                    class="oe_link"
                                    context="{'default_account_id': active_id,'search_default_account_id': active_id}"/>
                            </td>
                        </tr><tr name='total'>
                            <th class="oe_timesheet_grey">
                                <label string="Total"/>
                            </th><td class="oe_timesheet_grey">
                            </td><td class="oe_timesheet_grey">
                                <field name="est_total" class="oe_inline" attrs="{'invisible': [('invoice_on_timesheets','=',False)]}"/>
                            </td><td class="oe_timesheet_grey">
                                <field name="invoiced_total" class="oe_inline"/>
                            </td><td class="oe_timesheet_grey">
                                <field name="remaining_total" class="oe_inline"/>
                            </td><td class="oe_timesheet_grey">
                                <field name="toinvoice_total" class="oe_inline"/>
                            </td><td>
                            </td>
                        </tr>
                    </table>
                    <group name='invoice_on_timesheets' attrs="{'invisible': [('invoice_on_timesheets','=',False)]}">
                        <p class="oe_grey oe_edit_only" colspan="2">
                            When reinvoicing costs, OpenERP uses the
                            pricelist of the contract which uses the price
                            defined on the product related (e.g timesheet 
                            products are defined on each employee). 
                        </p>
                        <group>
                            <field name="pricelist_id"
                                class="oe_inline"
                                attrs="{'required': [('invoice_on_timesheets', '=', True)]}"/>
                            <field name="to_invoice"
                                class="oe_inline"
                                widget="selection"
                                attrs="{'required': [('invoice_on_timesheets', '=', True)]}"/>
                        </group>
                    </group>
                 </xpath>
            </field>
        </record>

        <!-- Inherited Analytic Account form for template required -->
        <record id="view_account_analytic_account_template_required" model="ir.ui.view">
            <field name="name">account.analytic.account.form.template.required</field>
            <field name="model">account.analytic.account</field>
            <field name="groups_id" eval="[(6, 0, [ref('group_template_required')])]"/>
            <field name="inherit_id" ref="analytic.view_account_analytic_account_form"/>
            <field name="arch" type="xml">
                <field name="template_id" position="attributes">
                    <attribute name="attrs">{'required': [('type','=','contract')], 'invisible': [('type','in',['view', 'normal','template'])]}</attribute>
                </field>
            </field>
        </record>

        <!-- Inherited Analytic Account list for contracts -->
        <record id="view_account_analytic_account_tree_c2c_3" model="ir.ui.view">
            <field name="name">account.analytic.account.list.contract</field>
            <field name="model">account.analytic.account</field>
            <field name="inherit_id" ref="account.view_account_analytic_account_list"/>
            <field name="groups_id" eval="[(4, ref('account.group_account_invoice'))]"/>
            <field name="arch" type="xml">
                <field name="date_start" position="before">
                    <field name="last_invoice_date"/>
                    <field name="toinvoice_total"/>
                    <field name="remaining_hours"/>
                    <field name="pricelist_id" invisible="1"/>
                </field>
            </field>
        </record>

        <!-- Analytic Account search view for contract -->
        <record id="view_account_analytic_account_overdue_search" model="ir.ui.view">
            <field name="name">account.analytic.account.search</field>
            <field name="model">account.analytic.account</field>
            <field name="arch" type="xml">
                <search string="Contracts">
                    <field name="name" filter_domain="['|', ('name','ilike',self),('code','ilike',self)]" string="Contract"/>
                    <field name="date"/>
                    <field name="partner_id" filter_domain="[('partner_id','child_of',self)]"/>
                    <field name="manager_id"/>
                    <field name="parent_id"/>
                    <filter name="open" string="In Progress" domain="[('state','in',('open','draft'))]" help="Contracts in progress (open, draft)"/>
                    <filter name="pending" string="To Renew" domain="[('state','=','pending')]" help="Pending contracts"/>
                    <filter name="closed" string="Closed" domain="[('state','=','close')]" help="Closed contracts"/>
                    <filter name="cancelled" string="Cancelled" domain="[('state','=','cancelled')]" help="Cancelled contracts"/>
                    <separator/>
                    <filter
                        string="Expired or consumed"
                        domain="[('state','in',('open','draft','pending')), '|', '&amp;', ('date', '!=', False), ('date', '&lt;=', time.strftime('%%Y-%%m-%%d')), ('is_overdue_quantity', '=', True)]"
                        help="End date passed or prepaid unit consumed" />
                    <filter
                        string="Expiring soon"
                        domain="[('date', '!=', False), ('date', '&lt;=', (context_today() + datetime.timedelta(30)).strftime('%%Y-%%m-%%d') )]"
                        help="End date is in the next month" />
                    <separator/>
                    <filter string="Customer Contracts" help="Contracts assigned to a customer." name="has_partner" domain="[('partner_id', '!=', False)]"/>
                    <filter string="Contracts not assigned" help="Contracts that are not assigned to an account manager." domain="[('manager_id', '=', False)]"/>
                    <separator/>
                    <group expand="0" string="Group By...">
                        <filter string="Status" domain="[]" context="{'group_by':'state'}"/>
                        <filter string="Account Manager" domain="[]" context="{'group_by':'manager_id'}"/>
                        <filter string="Partner" domain="[]" context="{'group_by':'partner_id'}"/>
                        <filter string="Parent" domain="[]" context="{'group_by':'parent_id'}"/>
                        <filter string="Template" domain="[]" context="{'group_by':'template_id'}"/>
                        <filter string="Start Date" domain="[]" context="{'group_by' : 'date_start'}" />
                        <filter string="End Date" domain="[]" context="{'group_by' : 'date'}" />
                        <filter string="Pricelist" domain="[]" context="{'group_by' : 'pricelist_id'}" />
                    </group>
                </search>
            </field>
        </record>

        <!-- Action Sales/Sales/Contracts -->
        <record id="action_account_analytic_overdue_all" model="ir.actions.act_window">
            <field name="name">Contracts</field>
            <field name="res_model">account.analytic.account</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{'default_type':'contract', 'search_default_open':1, 'search_default_pending':1, 'default_manager_id':uid}</field>
            <field name="domain">[('type','=','contract')]</field>
            <field name="search_view_id" ref="view_account_analytic_account_overdue_search"/>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">
                    Click to create a new contract.
                </p><p>
                    Use contracts to follow tasks, issues, timesheets or invoicing based on
                    work done, expenses and/or sales orders. OpenERP will automatically manage
                    the alerts for the renewal of the contracts to the right salesperson.
                </p>
            </field>
        </record>
        <menuitem id="base.menu_sales" name="Sales" parent="base.menu_base_partner" sequence="1"/>
        <menuitem action="action_account_analytic_overdue_all" id="menu_action_account_analytic_overdue_all" sequence="7" parent="base.menu_sales"/>

        <!-- Action Sales/Invoicing/Time and Material to Invoice -->
        <record id="action_hr_tree_invoiced_all" model="ir.actions.act_window">
            <field name="name">Time &amp; Materials to Invoice</field>
            <field name="res_model">account.analytic.line</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="domain">[('invoice_id','=',False)]</field>
            <field name="context">{'search_default_to_invoice': 1}</field>
            <field name="search_view_id" ref="account.view_account_analytic_line_filter"/>
            <field name="help" type="html">
              <p>
                You will find here timesheets and purchases you did for
                contracts that can be reinvoiced to the customer.  If you want
                to record new activities to invoice, you should use the timesheet
                menu instead.
              </p>
            </field>
        </record>
        <menuitem id="base.menu_invoiced" name="Invoicing" parent="base.menu_base_partner" sequence="5"/>
        <menuitem action="action_hr_tree_invoiced_all" id="menu_action_hr_tree_invoiced_all" parent="base.menu_invoiced" sequence="5"/>

        <!-- Action Sales/Invoicing/Contract to renew -->
        <record id="action_account_analytic_overdue" model="ir.actions.act_window">
            <field name="name">Contracts to Renew</field>
            <field name="res_model">account.analytic.account</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{'search_default_manager_id':uid, 'search_default_pending':1, 'search_default_renew':1}</field>
            <field name="domain">[('type','=','contract')]</field>
            <field name="search_view_id" ref="view_account_analytic_account_overdue_search"/>
            <field name="help" type="html">
              <p class="oe_view_nocontent_create">
                Click to define a new contract.
              </p><p>
                You will find here the contracts to be renewed because the
                end date is passed or the working effort is higher than the
                maximum authorized one.
              </p><p>
                OpenERP automatically sets contracts to be renewed in a pending
                state. After the negociation, the salesman should close or renew
                pending contracts.
              </p>
            </field>
        </record>
        <menuitem action="action_account_analytic_overdue" id="menu_action_account_analytic_overdue" sequence="50" parent="base.menu_invoiced"/>

        <!-- Action Sales/Configuration/Contract template -->
        <record id="template_of_contract_action" model="ir.actions.act_window">
            <field name="name">Contract Template</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">account.analytic.account</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="domain">[('type','=','template')]</field>
            <field name="context">{'search_default_type':'template','default_type' : 'template'}</field>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">
                    Click here to create a template of contract.
                </p><p>
                    Templates are used to prefigure contract/project that 
                    can be selected by the salespeople to quickly configure the
                    terms and conditions of the contract.
                </p>
            </field>
        </record>
        <menuitem action="template_of_contract_action" id="menu_template_of_contract_action" parent="base.menu_base_config"/>

    </data>
</openerp>
