<?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="type">tree</field>
            <field name="arch" type="xml">
            <tree string="Leads Analysis">
                <field name="name" invisible="1"/>
                <field name="state"  invisible="1"/>
                <field name="stage_id"  invisible="1"/>
                <field name="categ_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="month" 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="day" invisible="1"/>
                <field name="nbr" sum="# Leads"/>
                <field name="email" sum="# Mails"/>
                <field name="delay_open"/>
                <field name="delay_close"/>
                <field name="avg_answers"/>
             </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="type">graph</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="type">graph</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="type">search</field>
            <field name="arch" type="xml">
                <search string="Leads Analysis">
                    <group col="20" colspan="8">
                        <filter string="  Year  " icon="terp-go-year"
                            domain="[('create_date','&lt;=', time.strftime('%%Y-%%m-%%d')),('create_date','&gt;=',time.strftime('%%Y-01-01'))]"
                            help="Leads/Opportunities created in current year"/>
                        <filter string="   Month   " icon="terp-go-month" name="this_month"
                            domain="[('create_date','&lt;=', time.strftime('%%Y-%%m-%%d')),('create_date','&gt;=',time.strftime('%%Y-%%m-01'))]"
                            help="Leads/Opportunities created in current month"/>
                        <filter icon="terp-go-month" string="    Month-1    " separator="1"
                            domain="[('create_date','&lt;=', (datetime.date (int(time.strftime('%%Y')), datetime.date.today().month, 1) - datetime.timedelta (days = 1)).strftime('%%Y-%%m-%%d')),('create_date','&gt;',(datetime.date (int(time.strftime('%%Y')), datetime.date.today().month-1, 1)).strftime('%%Y-%%m-%%d'))]"
                            help="Leads/Opportunities created in last month"/>
                            
                        <separator orientation="vertical" />
                        <filter icon="terp-check"
                            string="Active"
                            domain="[('state','in',('draft','open'))]"
                            help="Leads/Opportunities which are in draft and 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 orientation="vertical" />
                        <field name="section_id"
                            default="context.get('section_id', False)"
                            context="{'invisible_section': False}">
                            <filter icon="terp-crm"
                                context="{'invisible_section': False}"
                                domain="[('section_id.user_id','=',uid)]"
                                help="My team" />
                        </field>
                        <field name="user_id" string="Salesman">
                            <filter icon="terp-personal+" string="My Case" help="My Case" domain="[('user_id','=',uid)]" />
                        </field>
                   </group>
                   <newline/>
                  <group expand="0" string="Extended Filters..." groups="base.group_extended">
                      <field name="partner_id"/>
                      <separator orientation="vertical"/>
                      <field name="stage_id" widget="selection" domain="[('object_id.model', '=', 'crm.lead')]" />
                      <field name="categ_id" widget="selection"/>
                      <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="creation_date"/>
                      <field name="opening_date"/>
                      <field name="date_closed"/>
                  </group>
                   <newline/>
                   <group expand="1" string="Group By...">
                        <filter string="User" 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-personal" context="{'group_by':'partner_id'}" />
                       <filter string="Country" icon="terp-go-home" context="{'group_by':'country_id'}" />
                        <separator orientation="vertical" />
                        <separator orientation="vertical" />
                        <filter string="Stage" icon="terp-stage" domain="[]" context="{'group_by':'stage_id'}"/>
                        <filter string="Priority"  icon="terp-rating-rated" domain="[]" context="{'group_by':'priority'}" />
                        <filter string="Category" icon="terp-stock_symbol-selection"
                            domain="[]" context="{'group_by':'categ_id'}" />
                        <filter string="Campaign" icon="terp-stock_symbol-selection"
                            domain="[]" context="{'group_by':'type_id'}" />
                        <filter string="Channel" icon="terp-stock_symbol-selection"
                            domain="[]" context="{'group_by':'channel_id'}" />
                        <separator orientation="vertical" />
                        <filter string="State" icon="terp-stock_effects-object-colorize"
                            domain="[]" context="{'group_by':'state'}" />
                        <separator orientation="vertical" />
                        <filter string="Company" icon="terp-go-home"
                            domain="[]"
                            context="{'group_by':'company_id'}" />
                        <separator orientation="vertical" />
                        <filter string="Day" icon="terp-go-today"
                            domain="[]" context="{'group_by':'day'}" help="Day on which lead/opportunity is created"/>
                        <filter string="Month" icon="terp-go-month"
                            domain="[]" context="{'group_by':'month'}" help="Month in which lead/opportunity is created"/>
                        <filter string="Year" icon="terp-go-year"
                            domain="[]" context="{'group_by':'name'}" help="Year in which lead/opportunity is created"/>
                  </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="type">tree</field>
            <field name="arch" type="xml">
            <tree colors="blue:state in ('draft');black:state in ('open','pending','done');gray:state in ('cancel') " string="Opportunities Analysis">
                <field name="name" invisible="1"/>
                <field name="month" invisible="1"/>
                <field name="section_id" invisible="1" groups="base.group_extended"/>
                <field name="user_id" invisible="1"/>
                <field name="partner_id" invisible="1"/>
                <field name="country_id" invisible="1"/>
                <field name="day" invisible="1"/>
                <field name="state"  invisible="1"/>
                <field name="stage_id"  invisible="1"/>
                <field name="priority"  invisible="1"/>
                <field name="categ_id" 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="email" sum="# of Emails"/>
                <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_filter_lead': 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">[('type', '=', 'lead')]</field>
            <field name="help">Leads Analysis allows you to consult different informations relative to CRM. Check for treatments delays, number of responses given and emails send. You can sort out your leads analysis on different groups to get fine 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_filter_opportunity":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="domain">[('type', '=', 'opportunity')]</field>
            <field name="help">Opportunities Analysis menu gives you an instant access to your opportunities like expected revenue, planned cost, overpassed deadline or the number of exchanges by opportunity.</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_extended"
           parent="base.next_id_64" action="action_report_crm_lead" sequence="3"/>

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

    </data>
</openerp>
