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

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

    <!--
    ======================
    Marketing Campaign
    ======================
    -->

    <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="type">form</field>
        <field name="arch" type="xml">
            <form string="Campaign">
                <group colspan="4" col="6">
                    <group colspan="2" col="2">
                        <separator string="Campaign" colspan="2" />
                        <field name="name" select="1"/>
                        <field name="mode"/>
                    </group>
                    <group colspan="2" col="2">
                        <separator string="Resource" colspan="2" />
                        <field name="object_id"/>
                        <field name="partner_field_id"/>
                    </group>
                    <group colspan="2" col="2">
                        <separator string="Cost" colspan="2" />
                        <field name="fixed_cost"/>
                    </group>
                </group>
                <field name="activity_ids" nolabel = "1" colspan="4" context="{'default_object_id': object_id, 'default_campaign_id': active_id}" />
                <separator string="" colspan="4" />
                <group col="10" colspan="4">
                    <field name="state" readonly="1" />
                    <button name="state_running_set" string="Run" states="draft,done,cancelled" icon="gtk-apply"/>
                    <button name="state_draft_set" string="Set to Draft" states="done,cancelled" icon="gtk-convert"/>
                    <button name="state_done_set" string="Close" states="running" icon="terp-dialog-close"/>
                    <button name="state_cancel_set" string="Cancel" states="running" icon="terp-gtk-stop"/>
                </group>
              </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="type">tree</field>
        <field name="arch" type="xml">
            <tree colors="blue:state in ('draft');black:state in ('running');gray:state in ('done','cancelled')" string="Campaigns">
                <field name="name" select="1"/>
                <field name="object_id" select="1"/>
                <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="type">diagram</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="type">search</field>
       <field name="arch" type="xml">
           <search string="Campaigns">
               <filter icon="terp-document-new" string="Draft" name = "draft" domain="[('state','=','draft')]"/>
               <filter icon="terp-check" string="Running" domain="[('state','=','running')]"/>
               <separator orientation="vertical"/>
               <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')]"/>
               <separator orientation="vertical"/>
               <field name="name" select="1"/>
               <field name="object_id" select="1" string="Resource"/>
               <newline/>
               <group expand="0" string="Group By..." colspan="10" col="12">
                   <filter string="Resource" name="Object" icon="terp-accessories-archiver" context="{'group_by':'object_id'}" />
                   <separator orientation="vertical"/>
                   <filter string="Mode" name="Mode" icon="terp-gtk-jump-to-rtl" context="{'group_by':'mode'}" />
                   <separator orientation="vertical"/>
                   <filter string="State" name="State" 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"/>

    </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="10" />

    <!--
    ======================
    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="type">form</field>
        <field name="arch" type="xml">
            <form string="Segments">
                <group colspan="2" col="2">
                    <separator string="Segment" colspan="4"/>
                    <field name="name"/>
                    <field name="campaign_id" select="1" on_change="onchange_campaign_id(campaign_id)"/>
                    <field name="object_id" invisible="1"/>
                </group>
                <group colspan="2" col="2">
                    <separator string="Filter" colspan="4"/>
                    <field name="ir_filter_id" domain="[('model_id', '=', object_id)]"/>
                </group>
                <newline/>
                <group colspan="2" col="4">
                   <separator string="Synchronization" colspan="4"/>
                   <field name="sync_mode" required="True" colspan="4"/>
                   <field name="sync_last_date" colspan="3"/>
                   <button string="Synchronize Manually" states="running" name="synchroniz" icon="terp-project" type="object"/>
                   <field name="date_next_sync" colspan="3"/>
                </group>
                <group colspan="2" col="2">
                    <separator string="History" colspan="2"/>
                    <field name="date_run" readonly="1"/>
                    <field name="date_done" readonly="1"/>
                </group>
                <separator string="" colspan="4"/>
                <group col="6" colspan="4">
                    <field name="state" readonly="1"/>
                    <button name="state_running_set" string="Run" states="draft" icon="gtk-apply"/>
                    <button name="state_done_set" string="Close" states="running" icon="terp-dialog-close"/>
                    <button name="state_cancel_set" string="Cancel" states="running" icon="terp-gtk-stop"/>
                    <button name="state_draft_set" string="Set to Draft" states="done,cancelled" icon="gtk-convert"/>
                </group>
            </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="type">tree</field>
        <field name="arch" type="xml">
            <tree colors="blue:state in ('draft');black:state in ('running');gray:state in ('done','cancelled')" string="Segments">
                <field name="name"/>
                <field name="campaign_id"/>
                <field name="date_run"/>
                <field name="sync_last_date"/>
                <button string="Synchronize Manually" states="running" name="synchroniz" icon="terp-project" type="object"/>
                <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="type">search</field>
       <field name="arch" type="xml">
           <search string="Segments">
               <filter icon="terp-check" string="Running" name="running" domain="[('state','=','running')]"/>
               <filter icon="terp-document-new" string="Draft" domain="[('state','=','draft')]"/>
               <separator orientation="vertical"/>
               <filter icon="terp-project" string="Newly Created" help="Sync mode: only records created after last sync" domain="[('sync_mode','=','create_date')]"/>
               <filter icon="terp-project" string="Newly Modified" help="Sync mode: only records updated after last sync" domain="[('sync_mode','=','write_date')]"/>
               <filter icon="terp-project" string="All" help="Sync mode: all records" domain="[('sync_mode','=','all')]"/>
               <separator orientation="vertical"/>
               <field name="name" select="1"/>
               <field name="campaign_id" select="1"/>
               <field name="object_id" select="1"/>
               <field name="date_run" select="1"/>
               <newline/>
               <group expand="0" string="Group By..." colspan="10" col="12">
                   <filter string="Campaign" name="Campaign" icon="terp-gtk-jump-to-rtl" context="{'group_by':'campaign_id'}" />
                   <separator orientation="vertical"/>
                   <filter string="State" name="State" icon="terp-stock_effects-object-colorize" context="{'group_by':'state'}" />
                   <separator orientation="vertical"/>
                   <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': [], 'search_default_running': 1}</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="type">form</field>
        <field name="arch" type="xml">
            <form string="Activities">
                <group colspan='2' col='2'>
                    <separator string="Activity" colspan="4"/>
                    <field name="name" select="1" colspan='4' />
                    <field name="signal" groups="base.group_extended"/>
                    <field name="start"/>
                    <field name="object_id" invisible="1"/>
                    <field name="campaign_id" invisible="1"/>
                </group>
                <group colspan='2' col='2'>
                    <separator string="Cost / Revenue" colspan="4"/>
                    <field name="variable_cost"/>
                    <field name="revenue"/>
                </group>
                <newline/>
                <group colspan='4' col='2'>
                    <separator string="Condition" colspan="4"/>
                    <field name="condition" widget="char" colspan="2"/>
                    <field name="keep_if_condition_not_met"/>
                </group>
                <group colspan='4' col='4'>
                    <separator string="Action" colspan="4"/>
                    <field name="type"/>
                    <group colspan='2' col='1'>
                        <field name="email_template_id" attrs="{'required':[('type','=','email')], 'invisible':[('type','!=','email')]}"
                               context="{'default_object_name':object_id}" />
                        <group attrs="{'invisible':[('type','!=','report')]}" >
                            <field name="report_id" attrs="{'required':[('type','=','report')]}" context="{'object_id':object_id}"/>
                            <field name="report_directory_id" attrs="{'required':[('type','=','report')]}" />
                        </group>
                        <field name="server_action_id" attrs="{'required':[('type','=','action')],'invisible':[('type','!=','action')]}" domain="[('model_id','=',object_id)]" />
                    </group>
                </group>
                <separator string="Transitions" colspan="4"/>
                <field name="from_ids" nolabel="1" mode="tree,form" context="{'default_activity_to_id': active_id}" colspan="2">
                    <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">
                        <field name="activity_from_id" domain="[('campaign_id', '=', parent.campaign_id)]"/>
                        <field name='trigger'/>
                        <field name="interval_nbr"/>
                        <field name="interval_type"/>
                    </form>
                </field>
                <field name="to_ids" nolabel="1" mode="tree,form" context="{'default_activity_from_id': active_id}" colspan="2">
                    <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">
                        <field name="activity_to_id" domain="[('campaign_id', '=', parent.campaign_id)]"/>
                        <field name='trigger'/>
                        <field name="interval_nbr"/>
                        <field name="interval_type"/>
                    </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="type">tree</field>
        <field name="arch" type="xml">
            <tree string="Activities">
                <field name="name" select="1"/>
                <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="type">tree</field>
        <field name="arch" type="xml">
            <tree colors="red:state in ('exception');black:state in ('todo');gray:state in ('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="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"/>
                <button string="Cancel" states="todo" name="button_cancel" type="object" icon="terp-gtk-stop"/>
            </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="type">form</field>
        <field name="arch" type="xml">
            <form string="Marketing Campaign Activities">
                <group colspan="4" col="6">
                    <group colspan="2" col="2">
                        <separator string="Campaign Step" colspan="2"/>
                        <field name="campaign_id" select="1"/>
                        <field name="segment_id" select="1"/>
                        <field name="activity_id" context="{'segment_id':segment_id}" select="1"/>
                    </group><group colspan="2" col="2">
                        <separator string="Related Resource" colspan="2"/>
                        <field name="object_id" readonly="True"/>
                        <field name="res_name"/>
                        <field name="res_id"/>
                        <field name="partner_id" select="1"/>
                    </group><group colspan="2" col="2">
                        <separator string="Date" colspan="2"/>
                        <field name="date"/>
                        <label string="" colspan="1"/>
                        <button string="Preview" states="todo" name="preview" icon="gtk-zoom-fit" type="object"/>
                    </group>
                </group>
                <group colspan="4" col="4" attrs="{'invisible':[('state','!=','exception')]}">
                    <separator string="Error Message" colspan="4"/>
                    <field name="error_msg" nolabel="1" colspan="4"/>
                </group>
                <separator string="" colspan="4"/>
                <group colspan="4" col="11">
                    <field name="state" readonly="True"/>
                    <button string="Reset" states="exception,cancelled" name="button_draft" type="object" icon="gtk-undo"/>
                    <button string="Process" states="todo" name="process" type="object" icon="terp-gtk-go-back-rtl"/>
                    <button string="Cancel" states="todo,exception" name="button_cancel" type="object" icon="terp-gtk-stop"/>
                </group>
            </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="type">search</field>
       <field name="arch" type="xml">
           <search string="Marketing Campaign Activities">
               <group colspan="4" col="10">
                <filter icon="terp-go-today" string="Today" name="today" domain="[('date','&lt;', time.strftime('%%Y-%%m-%%d 23:59:59')), ('date','&gt;=', time.strftime('%%Y-%%m-%%d 00:00:00'))]"/>
                <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')]"/>
                <separator orientation="vertical"/>
                <field name="campaign_id"/>
                <field name="segment_id"/>
                <field name="date"/>
                <newline/>
                <field name="object_id"/>
                <field name="res_name"/>
                <field name="res_id"/>
                <field name="partner_id"/>
               </group>
               <newline/>
               <group expand="0" string="Group By..." colspan="10" col="12">
                   <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'}" />
                   <separator orientation="vertical"/>
                   <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'}" />
                   <separator orientation="vertical"/>
                   <filter string="State" name="State" icon="terp-stock_effects-object-colorize" context="{'group_by':'state'}" />
                   <separator orientation="vertical"/>
                   <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 Followup</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': [], 'search_default_todo': 1, 'search_default_today': 1}</field>
    </record>

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

    <act_window name="All Segments"
        res_model="marketing.campaign.segment"
        src_model="marketing.campaign"
        view_type="form"
        view_mode="tree,form"
        id="act_marketing_campaing_segment_opened"
        domain="[('campaign_id','=',active_id)]"
    />

    <!-- Campaign Followups -->
    <act_window domain="[('campaign_id', '=', active_id)]"
        id="act_marketing_campaing_followup"
        name="Campaign Follow-up" res_model="marketing.campaign.workitem"
        src_model="marketing.campaign" view_mode="tree,form"
        view_type="form" />

    <!-- Campaign Statistics -->
    <act_window domain="[('campaign_id', '=', active_id)]"
        id="act_marketing_campaing_stat"
        name="Campaign Statistics" res_model="campaign.analysis"
        src_model="marketing.campaign" view_mode="tree,form"
        view_type="form" />

    </data>
</openerp>
