add type, busniess unit, parties fields on campaign

This commit is contained in:
?ngel ?lvarez 2014-07-28 15:10:53 +02:00
parent b681208e8f
commit cd377d4323
6 changed files with 132 additions and 34 deletions

View file

@ -8,6 +8,7 @@ def register():
Pool.register( Pool.register(
Campaign, Campaign,
ProductCampaign, ProductCampaign,
PartyCampaign,
Opportunity, Opportunity,
CreateCampaignStart, CreateCampaignStart,
module='sale_opportunity_campaign', type_='model') module='sale_opportunity_campaign', type_='model')

View file

@ -30,6 +30,10 @@ msgctxt "field:sale.opportunity.campaign,name:"
msgid "Name" msgid "Name"
msgstr "Nom" msgstr "Nom"
msgctxt "field:sale.opportunity.campaign,parties:"
msgid "Parties"
msgstr "Tercers"
msgctxt "field:sale.opportunity.campaign,products:" msgctxt "field:sale.opportunity.campaign,products:"
msgid "Products" msgid "Products"
msgstr "Productes" msgstr "Productes"
@ -110,6 +114,10 @@ msgctxt "model:sale.opportunity.campaign,name:"
msgid "Campaign" msgid "Campaign"
msgstr "Campanya" msgstr "Campanya"
msgctxt "model:sale.opportunity.campaign-party.party,name:"
msgid "Campaign - Party"
msgstr "Campanya - Tercer"
msgctxt "model:sale.opportunity.campaign-product.product,name:" msgctxt "model:sale.opportunity.campaign-product.product,name:"
msgid "Campaign - Product" msgid "Campaign - Product"
msgstr "Campanya - Producte" msgstr "Campanya - Producte"
@ -126,6 +134,10 @@ msgctxt "view:sale.opportunity.campaign:"
msgid "Campaigns" msgid "Campaigns"
msgstr "Campanyes" msgstr "Campanyes"
msgctxt "view:sale.opportunity.campaign:"
msgid "Create Leads"
msgstr "Crear iniciatives"
msgctxt "view:sale.opportunity.create_campaign.start:" msgctxt "view:sale.opportunity.create_campaign.start:"
msgid "Create Campaign" msgid "Create Campaign"
msgstr "Crea campanya" msgstr "Crea campanya"
@ -133,7 +145,3 @@ msgstr "Crea campanya"
msgctxt "wizard_button:sale.opportunity.create_campaign,start,end:" msgctxt "wizard_button:sale.opportunity.create_campaign,start,end:"
msgid "Cancel" msgid "Cancel"
msgstr "Cancel·lat" msgstr "Cancel·lat"
msgctxt "wizard_button:sale.opportunity.create_campaign,start,leads:"
msgid "Create"
msgstr "Crear"

View file

@ -30,6 +30,10 @@ msgctxt "field:sale.opportunity.campaign,name:"
msgid "Name" msgid "Name"
msgstr "Nombre" msgstr "Nombre"
msgctxt "field:sale.opportunity.campaign,parties:"
msgid "Parties"
msgstr "Terceros"
msgctxt "field:sale.opportunity.campaign,products:" msgctxt "field:sale.opportunity.campaign,products:"
msgid "Products" msgid "Products"
msgstr "Productos" msgstr "Productos"
@ -110,6 +114,10 @@ msgctxt "model:sale.opportunity.campaign,name:"
msgid "Campaign" msgid "Campaign"
msgstr "Campaña" msgstr "Campaña"
msgctxt "model:sale.opportunity.campaign-party.party,name:"
msgid "Campaign - Party"
msgstr "Campaña - Tercero"
msgctxt "model:sale.opportunity.campaign-product.product,name:" msgctxt "model:sale.opportunity.campaign-product.product,name:"
msgid "Campaign - Product" msgid "Campaign - Product"
msgstr "Campaña - Producto" msgstr "Campaña - Producto"
@ -126,6 +134,10 @@ msgctxt "view:sale.opportunity.campaign:"
msgid "Campaigns" msgid "Campaigns"
msgstr "Campañas" msgstr "Campañas"
msgctxt "view:sale.opportunity.campaign:"
msgid "Create Leads"
msgstr "Crear iniciativas"
msgctxt "view:sale.opportunity.create_campaign.start:" msgctxt "view:sale.opportunity.create_campaign.start:"
msgid "Create Campaign" msgid "Create Campaign"
msgstr "Crear campaña" msgstr "Crear campaña"
@ -133,7 +145,3 @@ msgstr "Crear campaña"
msgctxt "wizard_button:sale.opportunity.create_campaign,start,end:" msgctxt "wizard_button:sale.opportunity.create_campaign,start,end:"
msgid "Cancel" msgid "Cancel"
msgstr "Cancel·lar" msgstr "Cancel·lar"
msgctxt "wizard_button:sale.opportunity.create_campaign,start,leads:"
msgid "Create"
msgstr "Crear"

View file

@ -2,10 +2,12 @@
# copyright notices and license terms. # copyright notices and license terms.
from trytond.model import ModelView, ModelSQL, fields from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool, PoolMeta from trytond.pool import Pool, PoolMeta
from trytond.pyson import Bool, Eval
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.wizard import Wizard, StateAction, StateView, Button from trytond.wizard import Wizard, StateTransition, StateAction, StateView,\
Button
__all__ = ['Opportunity', 'Campaign', 'ProductCampaign', 'Campaign', __all__ = ['Opportunity', 'Campaign', 'ProductCampaign', 'PartyCampaign',
'CreateCampaignStart', 'CreateCampaign'] 'CreateCampaignStart', 'CreateCampaign']
__metaclass__ = PoolMeta __metaclass__ = PoolMeta
@ -20,6 +22,27 @@ class ProductCampaign(ModelSQL):
required=True, select=True, ondelete='CASCADE') required=True, select=True, ondelete='CASCADE')
class PartyCampaign(ModelSQL):
'Campaign - Party'
__name__ = 'sale.opportunity.campaign-party.party'
campaing = fields.Many2One('sale.opportunity.campaign', 'Campaign',
required=True, select=True, ondelete='CASCADE')
party = fields.Many2One('party.party', 'Party', required=True,
select=True, ondelete='CASCADE')
def _get_opportunities(self):
'''
Returns a list with the values of the opportunities to create
related to the campaing and the party
'''
opportunity = self.campaing.get_lead()
opportunity.party = self.party
opportunity.description += ' - %s' % self.party.rec_name
opportunity._save_values
return [opportunity._save_values]
class Campaign(ModelSQL, ModelView): class Campaign(ModelSQL, ModelView):
'Campaign' 'Campaign'
__name__ = 'sale.opportunity.campaign' __name__ = 'sale.opportunity.campaign'
@ -29,8 +52,49 @@ class Campaign(ModelSQL, ModelView):
end_date = fields.Date('End Date') end_date = fields.Date('End Date')
products = fields.Many2Many('sale.opportunity.campaign-product.product', products = fields.Many2Many('sale.opportunity.campaign-product.product',
'campaing', 'product', 'Products') 'campaing', 'product', 'Products')
parties = fields.Many2Many('sale.opportunity.campaign-party.party',
'campaing', 'party', 'Parties')
def create_lead(self): @classmethod
def __setup__(cls):
super(Campaign, cls).__setup__()
cls._buttons.update({
'create_leads': {
'invisible': ~Bool(Eval('parties', [])),
'icon': 'tryton-ok',
},
})
@classmethod
@ModelView.button
def create_leads(cls, campaigns):
pool = Pool()
Opportunity = pool.get('sale.opportunity')
CampaignParty = pool.get('sale.opportunity.campaign-party.party')
existing = set()
for opportunity in Opportunity.search([
('campaign', 'in', campaigns),
('party', '!=', None),
]):
existing.add((opportunity.campaign.id, opportunity.party.id))
campaign_party = set()
for campaign in campaigns:
for party in campaign.parties:
campaign_party.add((campaign.id, party.id))
to_create = []
for campaign, party in campaign_party - existing:
campaign_party, = CampaignParty.search([
('campaing', '=', campaign),
('party', '=', party),
])
opportunities = campaign_party._get_opportunities()
if opportunities:
to_create.extend(opportunities)
Opportunity.create(to_create)
def get_lead(self):
'Returns the correspoding lead for this campaing' 'Returns the correspoding lead for this campaing'
pool = Pool() pool = Pool()
Opportunity = pool.get('sale.opportunity') Opportunity = pool.get('sale.opportunity')
@ -52,6 +116,8 @@ class CreateCampaignStart(ModelView):
__name__ = 'sale.opportunity.create_campaign.start' __name__ = 'sale.opportunity.create_campaign.start'
campaign = fields.Many2One('sale.opportunity.campaign', 'Campaign') campaign = fields.Many2One('sale.opportunity.campaign', 'Campaign')
create_leads = fields.Boolean('Create Leads', help='If marked a lead will '
'be created for each party and related to the party')
class CreateCampaign(Wizard): class CreateCampaign(Wizard):
@ -61,38 +127,47 @@ class CreateCampaign(Wizard):
start = StateView('sale.opportunity.create_campaign.start', start = StateView('sale.opportunity.create_campaign.start',
'sale_opportunity_campaign.create_campaign_start_view_form', [ 'sale_opportunity_campaign.create_campaign_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'), Button('Cancel', 'end', 'tryton-cancel'),
Button('Create', 'leads', 'tryton-ok', True), Button('Create', 'create_', 'tryton-ok', True),
]) ])
create_ = StateTransition()
leads = StateAction('sale_opportunity.act_opportunity_form') leads = StateAction('sale_opportunity.act_opportunity_form')
def _get_opportunities(self, campaign, party): def transition_create_(self):
''' pool = Pool()
Returns a list with the values of the opportunities to create Campaing = pool.get('sale.opportunity.campaign')
related to the campaing and the party parties = set(Transaction().context.get('active_ids'))
''' campaign = self.start.campaign
opportunity = campaign.create_lead() existing = set([p.id for p in campaign.parties])
opportunity.party = party to_add = parties - existing
opportunity.description += ' - %s' % party.rec_name if to_add:
opportunity._save_values Campaing.write([campaign], {
return [opportunity._save_values] 'parties': [('add', list(to_add))]
})
if self.start.create_leads:
return 'leads'
return 'end'
def do_leads(self, action): def do_leads(self, action):
pool = Pool() pool = Pool()
Party = pool.get('party.party') Campaing = pool.get('sale.opportunity.campaign')
Opportunity = pool.get('sale.opportunity') Opportunity = pool.get('sale.opportunity')
parties = Party.browse(Transaction().context.get('active_ids'))
campaign = self.start.campaign
to_create = []
for party in parties:
opportunities = self._get_opportunities(campaign, party)
if opportunities:
to_create.extend(opportunities)
leads = Opportunity.create(to_create)
existing = set()
for opportunity in Opportunity.search([
('campaign', '=', self.start.campaign),
('party', '!=', None),
]):
existing.add(opportunity.id)
Campaing.create_leads([self.start.campaign])
new = set()
for opportunity in Opportunity.search([
('campaign', '=', self.start.campaign),
('party', '!=', None),
]):
new.add(opportunity.id)
leads = new - existing
data = { data = {
'res_id': [l.id for l in leads], 'res_id': list(leads),
} }
if len(leads) == 1: if len(leads) == 1:
action['views'].reverse() action['views'].reverse()

View file

@ -4,4 +4,6 @@ contains the full copyright notices and license terms. -->
<form string="Create Campaign"> <form string="Create Campaign">
<label name="campaign"/> <label name="campaign"/>
<field name="campaign" colspan="3"/> <field name="campaign" colspan="3"/>
<label name="create_leads"/>
<field name="create_leads"/>
</form> </form>

View file

@ -12,6 +12,10 @@ contains the full copyright notices and license terms. -->
<page name="description"> <page name="description">
<field name="description" colspan="4"/> <field name="description" colspan="4"/>
</page> </page>
<page name="parties">
<field name="parties" colspan="4"/>
<button name="create_leads" string="Create Leads"/>
</page>
<page name="products"> <page name="products">
<field name="products" colspan="4"/> <field name="products" colspan="4"/>
</page> </page>