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

<!-- Leads by user and section Tree View -->

        <record id="view_report_crm_lead_tree" model="ir.ui.view">
            <field name="name">crm.lead.report.tree</field>
            <field name="model">crm.lead.report</field>
            <field name="arch" type="xml">
            <tree string="Leads Analysis" create="false">
                <field name="creation_year" invisible="1"/>
                <field name="creation_month" invisible="1"/>
                <field name="creation_day" invisible="1"/>
                <field name="deadline_month" invisible="1"/>
                <field name="state"  invisible="1"/>
                <field name="stage_id"  invisible="1"/>
                <field name="type_id" invisible="1"/>
                <field name="channel_id" invisible="1"/>
                <field name="type" invisible="1"/>
                <field name="priority" invisible="1"/>
                <field name="section_id" invisible="1"/>
                <field name="user_id" invisible="1"/>
                <field name="company_id" invisible="1"/>
                <field name="partner_id" invisible="1"/>
                <field name="country_id" invisible="1"/>
                <field name="nbr" sum="# Leads"/>
                <field name="delay_open"/>
                <field name="delay_close"/>
                <field name="planned_revenue"/>
             </tree>
            </field>
        </record>

<!-- Leads by user and section Graph View -->

        <record id="view_report_crm_lead_graph" model="ir.ui.view">
            <field name="name">crm.lead.report.graph</field>
            <field name="model">crm.lead.report</field>
            <field name="arch" type="xml">
                <graph orientation="vertical" string="Leads Analysis" type="bar">
                    <field name="stage_id"/>
                    <field name="nbr" operator="+"/>
                    <field group="True" name="user_id"/>
                </graph>
            </field>
        </record>

        <record id="view_report_crm_opportunity_graph" model="ir.ui.view">
            <field name="name">crm.opportunity.report.graph</field>
            <field name="model">crm.lead.report</field>
            <field name="arch" type="xml">
                <graph orientation="vertical" string="Leads Analysis" type="bar">
                    <field name="stage_id"/>
                    <field name="planned_revenue" operator="+"/>
                    <field group="True" name="user_id"/>
                </graph>
            </field>
        </record>

<!-- Leads by user and section Search View -->

        <record id="view_report_crm_lead_filter" model="ir.ui.view">
            <field name="name">crm.lead.report.select</field>
            <field name="model">crm.lead.report</field>
            <field name="arch" type="xml">
                <search string="Leads Analysis">
                    <filter icon="terp-personal" name="lead" string="Lead" domain="[('type','=', 'lead')]" help="Show only lead"/>
                    <filter icon="terp-personal+" string="Opportunity" name="opportunity" domain="[('type','=','opportunity')]" help="Show only opportunity"/>
                    <separator/>
                    <filter icon="terp-check" string="New" domain="[('state','=','draft')]" help="Leads/Opportunities which are in New state"/>
                    <filter icon="terp-camera_test" string="Open" domain="[('state','=','open')]" help="Leads/Opportunities which are in open state"/>
                    <filter icon="gtk-media-pause" string="Pending" domain="[('state','=','pending')]" help="Leads/Opportunities which are in pending state"/>
                    <filter icon="terp-dialog-close" string="Closed" domain="[('state','=','done')]" help="Leads/Opportunities which are in done state"/>
                    <separator/>
                    <filter string="My Sales Team(s)" icon="terp-personal+" context="{'invisible_section': False}" domain="[('section_id.user_id','=',uid)]"
                            help="Leads/Opportunities that are assigned to one of the sale teams I manage"/>
                    <separator/>
                    <filter icon="terp-personal" string="My Case(s)" help="Leads/Opportunities that are assigned to me" domain="[('user_id','=',uid)]"/>
                    <field name="section_id" context="{'invisible_section': False}"/>
                    <field name="user_id" string="Salesperson"/>
                    <group expand="0" string="Extended Filters...">
                        <field name="partner_id" filter_domain="[('partner_id','child_of',self)]"/>
                        <field name="stage_id" domain="[('section_ids', '=', 'section_id')]" />
                        <field name="type_id"/>
                        <field name="channel_id"/>
                        <field name="company_id" groups="base.group_multi_company"/>
                        <separator orientation="vertical"/>
                        <field name="stage_id" widget="selection" domain="[('section_ids', '=', 'section_id')]" />
                        <field name="type_id" widget="selection"/>
                        <field name="channel_id" widget="selection"/>
                        <separator orientation="vertical"/>
                        <field name="company_id" widget="selection" groups="base.group_multi_company"/>
                        <newline/>
                        <field name="create_date"/>
                        <field name="opening_date"/>
                        <field name="date_closed"/>
                    </group>
                    <group expand="1" string="Group By...">
                        <filter string="Salesperson" name="user" icon="terp-personal" domain="[]" context="{'group_by':'user_id'}" />
                        <filter string="Sales Team" icon="terp-personal+" domain="[]" context="{'group_by':'section_id'}" />
                        <filter string="Partner" icon="terp-partner" context="{'group_by':'partner_id'}" />
                        <filter string="Country" icon="terp-go-home" context="{'group_by':'country_id'}" />
                        <filter string="Company" icon="terp-go-home" domain="[]" context="{'group_by':'company_id'}" groups="base.group_multi_company"/>
                        <filter string="Stage" name="Stage" icon="terp-stage" domain="[]" context="{'group_by':'stage_id'}"/>
                        <filter string="Priority"  icon="terp-rating-rated" domain="[]" context="{'group_by':'priority'}" />
                        <filter string="Campaign" icon="terp-gtk-jump-to-rtl"
                            domain="[]" context="{'group_by':'type_id'}" />
                        <filter string="Channel" icon="terp-call-start"
                            domain="[]" context="{'group_by':'channel_id'}" />
                        <separator orientation="vertical" />
                        <filter string="Year" icon="terp-go-year"
                            domain="[]" context="{'group_by':'creation_year'}"/>
                        <filter string="Month" icon="terp-go-month"
                            domain="[]" context="{'group_by':'creation_month'}"/>
                        <filter string="Day" icon="terp-go-today"
                            domain="[]" context="{'group_by':'creation_day'}"/>
                        <separator orientation="vertical" />
                        <filter string="Exp. Closing"  icon="terp-go-month"
                            domain="[]" context="{'group_by':'deadline_month'}"/>
                    </group>
                </search>
            </field>
        </record>

<!--     Opportunity tree view  -->

        <record id="view_report_crm_opportunity_tree" model="ir.ui.view">
            <field name="name">crm.lead.report.tree</field>
            <field name="model">crm.lead.report</field>
            <field name="arch" type="xml">
            <tree colors="blue:state == 'draft';black:state in ('open','pending','done');gray:state == 'cancel' " create="false" string="Opportunities Analysis">
                <field name="creation_year" invisible="1"/>
                <field name="creation_month" invisible="1"/>
                <field name="creation_day" invisible="1"/>
                <field name="deadline_month" invisible="1"/>
                <field name="section_id" invisible="1"/>
                <field name="user_id" invisible="1"/>
                <field name="partner_id" invisible="1"/>
                <field name="country_id" invisible="1"/>
                <field name="state"  invisible="1"/>
                <field name="stage_id"  invisible="1"/>
                <field name="priority"  invisible="1"/>
                <field name="type_id" invisible="1"/>
                <field name="channel_id" invisible="1"/>
                <field name="type" invisible="1"/>
                <field name="company_id" invisible="1" groups="base.group_multi_company"/>
                <field name="nbr" string="#Opportunities" sum="#Opportunities"/>
                <field name="planned_revenue" sum="Planned Revenues"/>
                <field name="delay_open" sum='Delay to open'/>
                <field name="delay_close" sum='Delay to close'/>
                <field name="delay_expected"/>
                <field name="probability" widget="progressbar"/>
                <field name="probable_revenue"/>
             </tree>
            </field>
        </record>

<!-- Leads by user and section Action -->

       <record id="action_report_crm_lead" model="ir.actions.act_window">
           <field name="name">Leads Analysis</field>
           <field name="res_model">crm.lead.report</field>
           <field name="view_type">form</field>
           <field name="context">{'search_default_year': 1,'search_default_lead': 1, "search_default_user":1, "search_default_this_month":1, 'group_by_no_leaf':1, 'group_by':[]}</field>
           <field name="view_mode">tree,graph</field>
           <field name="domain">[]</field>
           <field name="help">Leads Analysis allows you to check different CRM related information like the treatment delays or number of leads per state. You can sort out your leads analysis by different groups to get accurate grained analysis.</field>
       </record>
       <record model="ir.actions.act_window.view" id="action_report_crm_lead_tree">
            <field name="sequence" eval="1"/>
            <field name="view_mode">tree</field>
            <field name="view_id" ref="view_report_crm_lead_tree"/>
            <field name="act_window_id" ref="action_report_crm_lead"/>
       </record>
       <record model="ir.actions.act_window.view" id="action_report_crm_lead_graph">
            <field name="sequence" eval="2"/>
            <field name="view_mode">graph</field>
            <field name="view_id" ref="view_report_crm_lead_graph"/>
            <field name="act_window_id" ref="action_report_crm_lead"/>
       </record>

       <record id="action_report_crm_opportunity" model="ir.actions.act_window">
            <field name="name">Opportunities Analysis</field>
            <field name="res_model">crm.lead.report</field>
            <field name="view_type">form</field>
            <field name="context">{"search_default_year":1,"search_default_opportunity":1, "search_default_user":1,"search_default_this_month":1,'group_by_no_leaf':1,'group_by':[]}</field>
            <field name="view_mode">tree,graph</field>
            <field name="help">Opportunities Analysis gives you an instant access to your opportunities with information such as the expected revenue, planned cost, missed deadlines or the number of interactions per opportunity. This report is mainly used by the sales manager in order to do the periodic review with the teams of the sales pipeline.</field>
        </record>

       <record model="ir.actions.act_window.view" id="action_report_crm_opportunity_tree">
            <field name="sequence" eval="1"/>
            <field name="view_mode">tree</field>
            <field name="view_id" ref="view_report_crm_opportunity_tree"/>
            <field name="act_window_id" ref="action_report_crm_opportunity"/>
       </record>

       <record model="ir.actions.act_window.view" id="action_report_crm_opportunity_graph">
            <field name="sequence" eval="2"/>
            <field name="view_mode">graph</field>
            <field name="view_id" ref="view_report_crm_opportunity_graph"/>
            <field name="act_window_id" ref="action_report_crm_opportunity"/>
       </record>

       <menuitem name="Leads Analysis" id="menu_report_crm_leads_tree"
            groups="base.group_sale_manager"
            parent="base.next_id_64" action="action_report_crm_lead" sequence="1"/>

       <menuitem name="Opportunities Analysis" id="menu_report_crm_opportunities_tree"
            parent="base.next_id_64" action="action_report_crm_opportunity" sequence="5"/>

    </data>
</openerp>
