<?xml version="1.0"?>
<openerp>
    <data noupdate="1">

        <record id="action_client_marketing_menu" model="ir.actions.client">
            <field name="name">Open Marketing Menu</field>
            <field name="tag">reload</field>
            <field name="params" eval="{'menu_id': ref('base.marketing_menu')}"/>
        </record>

        <record id="base.open_menu" model="ir.actions.todo">
            <field name="action_id" ref="action_client_marketing_menu"/>
            <field name="state">open</field>
        </record>

    </data>
    <data>

    <menuitem name="Configuration" id="menu_marketing_configuration" parent="base.marketing_menu" sequence="1"/>

    <!-- Marketing Campaign -->
    
    <record id="act_marketing_campaing_segment_opened" model="ir.actions.act_window">
        <field name="res_model">marketing.campaign.segment</field>
        <field name="view_type">form</field>
        <field name="name">Segments</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{'search_default_campaign_id': [active_id], 'default_campaign_id': active_id}</field>
    </record>
    
    <!-- Campaign Follow-ups -->
        
    <record id="act_marketing_campaing_followup" model="ir.actions.act_window">
        <field name="res_model">marketing.campaign.workitem</field>
        <field name="view_type">form</field>
        <field name="name">Workitems</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{'search_default_campaign_id': [active_id], 'default_campaign_id': active_id}</field>
    </record>
    
    <!-- Campaign Statistics -->
    
    <record id="act_marketing_campaing_stat" model="ir.actions.act_window" >
        <field name="res_model">campaign.analysis</field>
        <field name="view_type">form</field>
        <field name="name">Campaign Follow-up</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{'search_default_campaign_id': [active_id], 'default_campaign_id': active_id}</field>
    </record>

    <!-- Campaign Followups -->

    <record model="ir.ui.view" id="view_marketing_campaign_form">
        <field name="name">marketing.campaign.form</field>
        <field name="model">marketing.campaign</field>
        <field name="arch" type="xml">
            <form string="Marketing Campaign" version="7.0">
            <header>
                <button name="state_running_set" string="Run" states="draft" class="oe_highlight"/>
                <button name="state_done_set" string="Done" states="running" class="oe_highlight"/>
                <button name="state_draft_set" string="Set to Draft" states="done,cancelled"/>
                <button name="state_cancel_set" string="Cancel Campaign" states="running"/>
                <field name="state" readonly="1" widget="statusbar" statusbar_visible="draft,running,done"/>
            </header>
            <sheet string="Campaign">
                <div class="oe_right oe_button_box" name="button_box">
                    <button type="action"
                        name="%(act_marketing_campaing_stat)d"
                        string="Campaign Statistics"/>
                    <button type="action"
                        name="%(act_marketing_campaing_segment_opened)d"
                        string="Segments"/>  
                    <button type="action"
                        name="%(act_marketing_campaing_followup)d"
                        string="Follow-Up"/>
                </div>
                <div class="oe_title">
                    <label for="name" class="oe_edit_only"/>
                    <h1><field name="name" class="oe_inline"/></h1>
                </div>
                <group>
                    <group>
                        <field name="mode"/>
                        <field name="fixed_cost"/>
                    </group>
                    <group>
                        <field name="object_id"/>
                        <field name="partner_field_id"/>
                        <field name="unique_field_id"/>
                    </group>
                </group>
                <separator string="Activities"/>
                <field name="activity_ids" context="{'default_object_id': object_id, 'default_campaign_id': active_id}"/>
            </sheet>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="view_marketing_campaign_tree">
        <field name="name">marketing.campaign.tree</field>
        <field name="model">marketing.campaign</field>
        <field name="arch" type="xml">
            <tree colors="blue:state == 'draft';black:state == 'running';gray:state in ('done','cancelled')" string="Campaigns">
                <field name="name"/>
                <field name="object_id"/>
                <field name="mode"/>
                <field name="fixed_cost"/>
                <field name="state"/>
            </tree>
        </field>
    </record>

    <record id="view_marketing_campaign_diagram" model="ir.ui.view">
        <field name="name">marketing.campaign.diagram</field>
        <field name="model">marketing.campaign</field>
        <field name="arch" type="xml">
            <diagram string="Campaign Editor">
                <node object="marketing.campaign.activity" shape="rectangle:type=='subcampaign'" bgcolor="gray:start==True">
                    <field name="name"/>
                    <field name="type"/>
                    <field name="start" invisible="1"/>
                    <field name="condition" widget="char"/>
                </node>
                <arrow object="marketing.campaign.transition" source="activity_from_id" destination="activity_to_id" label="['name']">
                    <field name="activity_from_id"/>
                    <field name="activity_to_id"/>
                    <field name="interval_nbr"/>
                    <field name="interval_type"/>
                </arrow>
            </diagram>
        </field>
    </record>

    <record id="view_marketing_campaign_search" model="ir.ui.view">
        <field name="name">marketing.campaign.search</field>
        <field name="model">marketing.campaign</field>
        <field name="arch" type="xml">
            <search string="Campaigns">
                <field name="name" string="Campaign"/>
                <filter icon="terp-document-new" string="Draft" name="draft" domain="[('state','=','draft')]"/>
                <filter icon="terp-check" string="Running" domain="[('state','=','running')]"/>
                <separator/>
                <filter icon="terp-gtk-jump-to-rtl" string="Test Mode" name="test" domain="[('mode','=','test')]"/>
                <filter icon="terp-gtk-jump-to-rtl" string="Manual Mode" domain="[('mode','=','manual')]"/>
                <field name="object_id" string="Resource"/>
                <group expand="0" string="Group By...">
                    <filter string="Resource" name="Object" icon="terp-accessories-archiver" context="{'group_by':'object_id'}"/>
                    <filter string="Mode" name="Mode" icon="terp-gtk-jump-to-rtl" context="{'group_by':'mode'}"/>
                    <filter string="Status" name="Status" icon="terp-stock_effects-object-colorize" context="{'group_by':'state'}"/>
                </group>
            </search>
        </field>
    </record>

    <record model="ir.actions.act_window" id="action_marketing_campaign_form">
        <field name="name">Campaigns</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">marketing.campaign</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form,diagram</field>
        <field name="view_id" ref="view_marketing_campaign_tree"/>
        <field name="context">{'group_by': []}</field>
        <field name="search_view_id" ref="view_marketing_campaign_search"/>
        <field name="help" type="html">
          <p class="oe_view_nocontent_create">
            Click to create a marketing campaign.
          </p><p>
            OpenERP's marketing campaign allows you to automate communication
            to your prospects. You can define a segment (set of conditions) on
            your leads and partners to fullfil the campaign.
          </p><p>
            A campaign can have many activities like sending an email, printing
            a letter, assigning to a team, etc. These activities are triggered
            from specific situations; contact form, 10 days after first
            contact, if a lead is not closed yet, etc.
          </p>
        </field>
    </record>

    <menuitem name="Campaigns" id="menu_marketing_campaign" parent="base.marketing_menu"/>
    <menuitem id="menu_marketing_campaign_form" parent="menu_marketing_campaign" action="action_marketing_campaign_form" sequence="30"/>

    <!-- Marketing Segments -->
    <record model="ir.ui.view" id="view_marketing_campaign_segment_form">
        <field name="name">marketing.campaign.segment.form</field>
        <field name="model">marketing.campaign.segment</field>
        <field name="arch" type="xml">
            <form string="Marketing Campaign Segment" version="7.0">
            <header>
                <button name="state_running_set" string="Run" states="draft" class="oe_highlight"/>
                <button name="state_done_set" string="Close" states="running" class="oe_highlight"/>
                <button name="state_draft_set" string="Set to Draft" states="done,cancelled"/>
                <button name="state_cancel_set" string="Cancel Segment" states="running"/>
                <field name="state" readonly="1" widget="statusbar" statusbar_visible="draft,running,done"/>
            </header>
            <sheet string="Segments" >
                <group>
                    <group>
                        <field name="name"/>
                        <field name="campaign_id" on_change="onchange_campaign_id(campaign_id)"/>
                        <field name="object_id" invisible="1"/>
                        <field name="sync_mode" required="True"/>
                       <label for="sync_last_date"/>
                       <div>
                           <field name="sync_last_date" class="oe_inline"/>
                           <button string="Synchronize Manually" states="running" name="synchroniz" type="object"/>
                       </div>
                       <field name="date_next_sync"/>
                    </group>
                    <group>
                        <field name="ir_filter_id"/>
                        <field name="date_run" readonly="1"/>
                        <field name="date_done" readonly="1"/>
                    </group>
                </group>
            </sheet>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="view_marketing_campaign_segment_tree">
        <field name="name">marketing.campaign.segment.tree</field>
        <field name="model">marketing.campaign.segment</field>
        <field name="arch" type="xml">
            <tree colors="blue:state == 'draft';black:state == 'running';gray:state in ('done','cancelled')" string="Segments">
                <field name="name"/>
                <field name="campaign_id"/>
                <field name="date_run"/>
                <field name="sync_last_date"/>
                <field name="state"/>
            </tree>
        </field>
    </record>

    <record id="view_marketing_campaign_segment_search" model="ir.ui.view">
        <field name="name">marketing.campaign.segment.search</field>
        <field name="model">marketing.campaign.segment</field>
        <field name="arch" type="xml">
            <search string="Segments">
                <field name="name" string="Segment"/>
                <field name="date_run"/>
                <filter icon="terp-check" string="Running" name="running" domain="[('state','=','running')]"/>
                <filter icon="terp-document-new" string="Draft" domain="[('state','=','draft')]"/>
                <separator/>
                <filter icon="terp-gtk-jump-to-ltr" string="Newly Created" help="Sync mode: only records created after last sync" domain="[('sync_mode','=','create_date')]"/>
                <filter icon="terp-stock_effects-object-colorize" string="Newly Modified" help="Sync mode: only records updated after last sync" domain="[('sync_mode','=','write_date')]"/>
                <filter icon="terp-emblem-documents" string="All" help="Sync mode: all records" domain="[('sync_mode','=','all')]"/>
                <field name="campaign_id"/>
                <field name="object_id"/>
                <group expand="0" string="Group By...">
                    <filter string="Campaign" name="Campaign" icon="terp-gtk-jump-to-rtl" context="{'group_by':'campaign_id'}"/>
                    <filter string="Status" name="Status" icon="terp-stock_effects-object-colorize" context="{'group_by':'state'}"/>
                    <filter string="Launch Date" name="Launch Date" icon="terp-go-month" context="{'group_by':'date_run'}"/>
                </group>
            </search>
        </field>
    </record>

    <record model="ir.actions.act_window" id="action_marketing_campaign_segment_form">
        <field name="name">Segments</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">marketing.campaign.segment</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="view_id" ref="view_marketing_campaign_segment_tree"/>
        <field name="search_view_id" ref="view_marketing_campaign_segment_search"/>
        <field name="context">{'group_by': []}</field>
    </record>

    <menuitem id="menu_marketing_campaign_segment_form" parent="menu_marketing_campaign" action="action_marketing_campaign_segment_form" sequence="20"/>

    <!--
        Marketing Activities
        These views are not intended to be used outside the campaign context.
    -->
    <record model="ir.ui.view" id="view_marketing_campaign_activity_form">
        <field name="name">marketing.campaign.activity.form</field>
        <field name="model">marketing.campaign.activity</field>
        <field name="arch" type="xml">
            <form string="Activities" version="7.0">
                <group>
                    <group>
                        <field name="name"/>
                        <field name="signal" groups="base.group_no_one"/>
                        <field name="start"/>
                        <field name="object_id" invisible="1"/>
                        <field name="campaign_id" invisible="1" required="0"/>
                    </group>
                    <group>
                        <field name="variable_cost"/>
                        <field name="revenue"/>
                    </group>
                </group>
                <group >
                    <group>
                        <field name="condition" widget="char"/>
                        <field name="keep_if_condition_not_met"/>
                    </group>
                    <group>
                        <field name="type"/>
                        <field name="email_template_id" attrs="{'required':[('type','=','email')], 'invisible':[('type','!=','email')]}"
                           context="{'default_model_id':object_id}"/>
                        <field name="server_action_id" attrs="{'required':[('type','=','action')],'invisible':[('type','!=','action')]}" domain="[('model_id','=',object_id)]"/>
                        <field name="report_id" attrs="{'required':[('type','=','report')],'invisible':[('type','!=','report')]}" context="{'object_id':object_id}"/>
                        <field name="report_directory_id" attrs="{'required':[('type','=','report')],'invisible':[('type','!=','report')]}"/>
                    </group>
                </group>
                <separator string="Previous Activities"/>
                <field name="from_ids" mode="tree" context="{'default_activity_to_id': active_id}">
                    <tree string="Incoming Transitions" editable="bottom">
                        <field name="activity_from_id" domain="[('campaign_id', '=', parent.campaign_id)]"/>
                        <field name='trigger'/>
                        <field name="interval_nbr"/>
                        <field name="interval_type"/>
                    </tree>
                    <form string="Incoming Transitions" version="7.0">
                        <group col="4">
                            <field name="activity_from_id" domain="[('campaign_id', '=', parent.campaign_id)]"/>
                            <field name='trigger'/>
                            <field name="interval_nbr"/>
                            <field name="interval_type"/>
                        </group>
                    </form>
                </field>
                <separator string="Next Activities"/>
                <field name="to_ids" mode="tree" context="{'default_activity_from_id': active_id}">
                    <tree string="Outgoing Transitions" editable="bottom">
                        <field name="activity_to_id" domain="[('campaign_id', '=', parent.campaign_id)]"/>
                        <field name='trigger'/>
                        <field name="interval_nbr"/>
                        <field name="interval_type"/>
                    </tree>
                    <form string="Outgoing Transitions" version="7.0">
                        <group col="4">
                            <field name="activity_to_id" domain="[('campaign_id', '=', parent.campaign_id)]"/>
                            <field name='trigger'/>
                            <field name="interval_nbr"/>
                            <field name="interval_type"/>
                        </group>
                    </form>
                </field>
            </form>
        </field>
    </record>

    <record model="ir.ui.view" id="view_marketing_campaign_activity_tree">
        <field name="name">marketing.campaign.activity.tree</field>
        <field name="model">marketing.campaign.activity</field>
        <field name="arch" type="xml">
            <tree string="Activities">
                <field name="name"/>
                <field name="condition"/>
                <field name="type"/>
                <field name="start"/>
            </tree>
        </field>
    </record>


    <!-- Marketing Workitems -->
    <record model="ir.ui.view" id="view_marketing_campaign_workitem_tree">
        <field name="name">marketing.campaign.workitem.tree</field>
        <field name="model">marketing.campaign.workitem</field>
        <field name="arch" type="xml">
            <tree colors="red:state == 'exception';black:state == 'todo';gray:state == 'cancelled'" string="Marketing Campaign Activities">
                <field name="campaign_id"/>
                <field name="segment_id"/>
                <field name="activity_id"/>
                <field name="object_id" invisible="1"/>
                <field name="res_id" invisible="1"/>
                <field name="res_name"/>
                <field name="partner_id"/>
                <field name="date"/>
                <field name="state"/>
                <button string="Cancel" states="todo" name="button_cancel" type="object" icon="terp-gtk-stop"/>
                <button string="Preview" states="todo" name="preview" icon="gtk-zoom-fit" type="object"/>
                <button string="Process" states="todo" name="process" type="object" icon="terp-gtk-go-back-rtl"/>
            </tree>
        </field>
    </record>

    <record model="ir.ui.view" id="view_marketing_campaign_workitem_form">
        <field name="name">marketing.campaign.workitem.form</field>
        <field name="model">marketing.campaign.workitem</field>
        <field name="arch" type="xml">
            <form string="Marketing Campaign Activities" version="7.0">
            <header>
                <button string="Process" states="todo" name="process" type="object"/>
                <button string="Reset" states="exception,cancelled" name="button_draft" type="object"/>
                <button string="Cancel Workitem" states="todo,exception" name="button_cancel" type="object"/>
                <field name="state" readonly="True" widget="statusbar" statusbar_visible="todo,done" statusbar_colors='{"exception":"red"}'/>
                <button string="Preview" states="todo" name="preview" icon="gtk-zoom-fit" type="object"/>
            </header>
            <sheet>
                <group>
                    <group>
                        <field name="campaign_id"/>
                        <field name="segment_id"/>
                        <field name="activity_id" context="{'segment_id':segment_id}"/>
                        <field name="date"/>
                    </group>
                    <group>
                        <field name="object_id" readonly="True"/>
                        <field name="res_name"/>
                        <field name="res_id"/>
                        <field name="partner_id"/>
                    </group>
                </group>
                <group attrs="{'invisible':[('state','!=','exception')]}" string="Error Message">
                    <field name="error_msg" nolabel="1" colspan="2"/>
                </group>
            </sheet>
            </form>
        </field>
    </record>

    <record id="view_marketing_campaign_workitem_search" model="ir.ui.view">
        <field name="name">marketing.campaign.workitem.search</field>
        <field name="model">marketing.campaign.workitem</field>
        <field name="arch" type="xml">
            <search string="Marketing Campaign Activities">
                <field name="res_name" string="Marketing Campaign Activity"/>
                <field name="date"/>
                <filter icon="terp-gtk-go-back-rtl" string="To Do" name="todo" domain="[('state','=','todo')]"/>
                <filter icon="terp-emblem-important" string="Exception" domain="[('state','=','exception')]"/>
                <field name="campaign_id"/>
                <field name="segment_id"/>
                <field name="object_id"/>
                <field name="partner_id"/>
                <field name="res_id"/>
                <group expand="0" string="Group By...">
                    <filter string="Campaign" name="campaign" icon="terp-gtk-jump-to-rtl" context="{'group_by':'campaign_id'}"/>
                    <filter string="Segment" name="segment" icon="terp-stock_symbol-selection" context="{'group_by':'segment_id'}"/>
                    <filter string="Activity" name="activity" icon="terp-stock_align_left_24" context="{'group_by':'activity_id'}"/>
                    <filter string="Resource" name="resource" icon="terp-accessories-archiver" context="{'group_by':'object_id'}"/>
                    <filter string="Resource ID" name="res_id" icon="terp-accessories-archiver" context="{'group_by':'res_id'}"/>
                    <filter string="Status" name="Status" icon="terp-stock_effects-object-colorize" context="{'group_by':'state'}"/>
                    <filter string="Execution Date" name="Date" icon="terp-go-month" context="{'group_by':'date'}"/>
                </group>
            </search>
        </field>
    </record>

    <record model="ir.actions.act_window" id="action_marketing_campaign_workitem">
        <field name="name">Campaign Follow-up</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">marketing.campaign.workitem</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="view_id" ref="view_marketing_campaign_workitem_tree"/>
        <field name="search_view_id" ref="view_marketing_campaign_workitem_search"/>
        <field name="context">{'group_by': []}</field>
    </record>

    <menuitem id="menu_action_marketing_campaign_workitem" parent="menu_marketing_campaign" action="action_marketing_campaign_workitem" sequence="10"/>

    </data>
</openerp>
