<?xml version="1.0"?>
<openerp>
    <data>
        <record id="view_crm_opportunity_geo_assign_form" model="ir.ui.view">
            <field name="name">crm.lead.geo_assign.inherit</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
            <field name="arch" type="xml">
                <data>
                    <xpath expr="//notebook/page[@string='Lead']" position="after">
                        <page string="Assigned Partner" groups="base.group_sale_salesman">
                            <group name="partner_assign_group">
                                <group string="Partner Assignation" col="3" colspan="1">
                                    <label for="partner_latitude" string="Geolocation" />
                                    <div class="oe_title">
                                        <h3>
                                            <span class="oe_grey">( </span>
                                            <field name="partner_latitude" nolabel="1" class="oe_inline"/>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_latitude','&lt;=',0)]}">N </span>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_latitude','&gt;=',0)]}">S </span>
                                            <field name="partner_longitude"  class="oe_inline" nolabel="1"/>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_longitude','&lt;=',0)]}">E </span>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_longitude','&gt;=',0)]}">W </span>
                                            <span class="oe_grey">) </span>
                                        </h3>
                                    </div>
                                    <button string="Automatic Assignation" name="action_assign_partner" type="object" colspan="1"
                                        class="oe_inline"/>
                                    <field name="partner_assigned_id" class="oe_inline" on_change="onchange_assign_id(partner_assigned_id)" domain="[('grade_id','&lt;&gt;',False)]"/>
                                    <div>
                                        <button string="Send Email"
                                            attrs="{'invisible':[('partner_assigned_id','=',False)]}"
                                            name="%(crm_lead_forward_to_partner_act)d"
                                            type="action"
                                            context="{'default_composition_mode': 'forward','hide_forward_type': 1 , 'default_partner_ids': [partner_assigned_id]}"/>
                                    </div>
                                </group>
                            </group>
                        </page>
                    </xpath>
                </data>
            </field>
        </record>

        <record id="view_crm_opportunity_geo_assign_tree" model="ir.ui.view">
            <field name="name">crm.lead.geo_assign.tree.inherit</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.crm_case_tree_view_oppor"/>
            <field name="arch" type="xml">
                <field name="partner_id" position="after">
                    <field name="partner_assigned_id"/>
                    <field name="date_assign" invisible="1"/>
                 </field>
            </field>
        </record>

        <record model="ir.ui.view" id="crm_opportunity_partner_filter">
            <field name="name">crm.opportunity.partner.filter.assigned</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.view_crm_case_opportunities_filter"/>
            <field name="arch" type="xml">
                <filter string="Stage" position="after">
                    <filter string="Assigned Partner" domain="[]" context="{'group_by':'partner_assigned_id'}"/>
                </filter>
                <filter name="unassigned" position="after">
                    <filter string="My Assigned Partners" domain="[('partner_assigned_id.user_id', '=', uid)]"/>
                </filter>
                <field name="partner_id" position="after">
                    <field name="partner_assigned_id"/>
                </field>
            </field>
        </record>

        <record id="view_crm_lead_geo_assign_form" model="ir.ui.view">
            <field name="name">crm.lead.lead.geo_assign.inherit</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.crm_case_form_view_leads"/>
            <field name="arch" type="xml">
                <data>
                    <xpath expr="//notebook/page[@string='Extra Info']" position="after">
                        <page string="Assigned Partner" groups="base.group_sale_salesman">
                            <group name="partner_assign_group">
                                <group string="Partner Assignation" col="3">
                                    <label for="partner_latitude" string="Geolocation" />
                                    <div>
                                        <h3>
                                            <span class="oe_grey">( </span>
                                            <field name="partner_latitude" nolabel="1" class="oe_inline"/>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_latitude','&lt;=',0)]}">N </span>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_latitude','&gt;=',0)]}">S </span>
                                            <field name="partner_longitude"  class="oe_inline" nolabel="1"/>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_longitude','&lt;=',0)]}">E </span>
                                            <span class="oe_grey oe_inline" attrs="{'invisible':[('partner_longitude','&gt;=',0)]}">W </span>
                                            <span class="oe_grey">) </span>
                                        </h3>
                                    </div>
                                    <button string="Automatic Assignation" name="action_assign_partner" type="object" colspan="1"
                                        class="oe_inline" />
                                    <field name="partner_assigned_id" class="oe_inline" on_change="onchange_assign_id(partner_assigned_id)" domain="[('grade_id','&lt;&gt;',False)]"/>
                                    <div>
                                        <button string="Send Email"
                                            attrs="{'invisible':[('partner_assigned_id','=',False)]}"
                                            name="%(crm_lead_forward_to_partner_act)d"
                                            type="action"
                                            context="{'default_composition_mode': 'forward','hide_forward_type': 1 , 'default_partner_ids': [partner_assigned_id]}"/>
                                    </div>
                                </group>
                            </group>
                        </page>
                    </xpath>
                </data>
            </field>
        </record>

        <record id="view_crm_lead_geo_assign_tree" model="ir.ui.view">
            <field name="name">crm.lead.lead.geo_assign.tree.inherit</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.crm_case_tree_view_leads"/>
            <field name="arch" type="xml">
                <field name="partner_id" position="after">
                    <field name="partner_assigned_id"/>
                </field>
            </field>
        </record>

        <record model="ir.ui.view" id="crm_lead_partner_filter">
            <field name="name">crm.lead.partner.filter.assigned</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.view_crm_case_leads_filter"/>
            <field name="arch" type="xml">
                <filter string="Customer" position="after">
                    <filter string="Assigned Partner" domain="[]" context="{'group_by':'partner_assigned_id'}"/>
                </filter>
                <filter name="unassigned" position="after">
                    <filter string="My Assigned Partners" domain="[('partner_assigned_id.user_id', '=', uid)]"/>
                </filter>
                <field name="partner_id" position="after">
                    <field name="partner_assigned_id"/>
                </field>
            </field>
        </record>

        <record id="action_assign_salesman_according_assigned_partner" model="ir.actions.server">
            <field name="name">Assign salesman of assigned partner</field>
            <field name="model_id" ref="model_crm_lead"/>
            <field name="state">code</field>
            <field name="code">
                if context.get('active_model') == 'crm.lead':
                    ids = []
                    if context.get('active_domain'):
                        ids = self.search(cr, uid, context['active_domain'], context=context)
                    elif context.get('active_ids'):
                        ids = context['active_ids']
                    if ids:
                        self.assign_salesman_of_assigned_partner(cr, uid, ids, context=context)
            </field>
        </record>

        <record model="ir.values" id="ir_assign_salesman_according_assigned_partner">
            <field name="model_id" ref="model_crm_lead" />
            <field name="name">Assign salesman of assigned partner</field>
            <field name="key2">client_action_multi</field>
            <field eval="'ir.actions.server,%d'%action_assign_salesman_according_assigned_partner" name="value"/>
            <field name="key">action</field>
            <field name="model">crm.lead</field>
        </record>

    </data>
</openerp>
