From cd377d43236f46ad7f8c87b31a876a57737649f3 Mon Sep 17 00:00:00 2001 From: ?ngel ?lvarez Date: Mon, 28 Jul 2014 15:10:53 +0200 Subject: [PATCH] add type, busniess unit, parties fields on campaign --- __init__.py | 1 + locale/ca_ES.po | 16 ++- locale/es_ES.po | 16 ++- opportunity.py | 127 ++++++++++++++++++----- view/create_campaign_start_view_form.xml | 2 + view/sale_opportunity_campaign_form.xml | 4 + 6 files changed, 132 insertions(+), 34 deletions(-) diff --git a/__init__.py b/__init__.py index 28e6965..6c4b1ca 100644 --- a/__init__.py +++ b/__init__.py @@ -8,6 +8,7 @@ def register(): Pool.register( Campaign, ProductCampaign, + PartyCampaign, Opportunity, CreateCampaignStart, module='sale_opportunity_campaign', type_='model') diff --git a/locale/ca_ES.po b/locale/ca_ES.po index d793618..2e356e5 100644 --- a/locale/ca_ES.po +++ b/locale/ca_ES.po @@ -30,6 +30,10 @@ msgctxt "field:sale.opportunity.campaign,name:" msgid "Name" msgstr "Nom" +msgctxt "field:sale.opportunity.campaign,parties:" +msgid "Parties" +msgstr "Tercers" + msgctxt "field:sale.opportunity.campaign,products:" msgid "Products" msgstr "Productes" @@ -110,6 +114,10 @@ msgctxt "model:sale.opportunity.campaign,name:" msgid "Campaign" msgstr "Campanya" +msgctxt "model:sale.opportunity.campaign-party.party,name:" +msgid "Campaign - Party" +msgstr "Campanya - Tercer" + msgctxt "model:sale.opportunity.campaign-product.product,name:" msgid "Campaign - Product" msgstr "Campanya - Producte" @@ -126,6 +134,10 @@ msgctxt "view:sale.opportunity.campaign:" msgid "Campaigns" msgstr "Campanyes" +msgctxt "view:sale.opportunity.campaign:" +msgid "Create Leads" +msgstr "Crear iniciatives" + msgctxt "view:sale.opportunity.create_campaign.start:" msgid "Create Campaign" msgstr "Crea campanya" @@ -133,7 +145,3 @@ msgstr "Crea campanya" msgctxt "wizard_button:sale.opportunity.create_campaign,start,end:" msgid "Cancel" msgstr "Cancel·lat" - -msgctxt "wizard_button:sale.opportunity.create_campaign,start,leads:" -msgid "Create" -msgstr "Crear" diff --git a/locale/es_ES.po b/locale/es_ES.po index 3fb3f40..0c0117a 100644 --- a/locale/es_ES.po +++ b/locale/es_ES.po @@ -30,6 +30,10 @@ msgctxt "field:sale.opportunity.campaign,name:" msgid "Name" msgstr "Nombre" +msgctxt "field:sale.opportunity.campaign,parties:" +msgid "Parties" +msgstr "Terceros" + msgctxt "field:sale.opportunity.campaign,products:" msgid "Products" msgstr "Productos" @@ -110,6 +114,10 @@ msgctxt "model:sale.opportunity.campaign,name:" msgid "Campaign" 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:" msgid "Campaign - Product" msgstr "Campaña - Producto" @@ -126,6 +134,10 @@ msgctxt "view:sale.opportunity.campaign:" msgid "Campaigns" msgstr "Campañas" +msgctxt "view:sale.opportunity.campaign:" +msgid "Create Leads" +msgstr "Crear iniciativas" + msgctxt "view:sale.opportunity.create_campaign.start:" msgid "Create Campaign" msgstr "Crear campaña" @@ -133,7 +145,3 @@ msgstr "Crear campaña" msgctxt "wizard_button:sale.opportunity.create_campaign,start,end:" msgid "Cancel" msgstr "Cancel·lar" - -msgctxt "wizard_button:sale.opportunity.create_campaign,start,leads:" -msgid "Create" -msgstr "Crear" diff --git a/opportunity.py b/opportunity.py index 92ad957..ca6be7d 100644 --- a/opportunity.py +++ b/opportunity.py @@ -2,10 +2,12 @@ # copyright notices and license terms. from trytond.model import ModelView, ModelSQL, fields from trytond.pool import Pool, PoolMeta +from trytond.pyson import Bool, Eval 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'] __metaclass__ = PoolMeta @@ -20,6 +22,27 @@ class ProductCampaign(ModelSQL): 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): 'Campaign' __name__ = 'sale.opportunity.campaign' @@ -29,8 +52,49 @@ class Campaign(ModelSQL, ModelView): end_date = fields.Date('End Date') products = fields.Many2Many('sale.opportunity.campaign-product.product', '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' pool = Pool() Opportunity = pool.get('sale.opportunity') @@ -52,6 +116,8 @@ class CreateCampaignStart(ModelView): __name__ = 'sale.opportunity.create_campaign.start' 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): @@ -61,38 +127,47 @@ class CreateCampaign(Wizard): start = StateView('sale.opportunity.create_campaign.start', 'sale_opportunity_campaign.create_campaign_start_view_form', [ 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') - def _get_opportunities(self, campaign, party): - ''' - Returns a list with the values of the opportunities to create - related to the campaing and the party - ''' - opportunity = campaign.create_lead() - opportunity.party = party - opportunity.description += ' - %s' % party.rec_name - opportunity._save_values - return [opportunity._save_values] - + def transition_create_(self): + pool = Pool() + Campaing = pool.get('sale.opportunity.campaign') + parties = set(Transaction().context.get('active_ids')) + campaign = self.start.campaign + existing = set([p.id for p in campaign.parties]) + to_add = parties - existing + if to_add: + Campaing.write([campaign], { + 'parties': [('add', list(to_add))] + }) + if self.start.create_leads: + return 'leads' + return 'end' def do_leads(self, action): pool = Pool() - Party = pool.get('party.party') + Campaing = pool.get('sale.opportunity.campaign') 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 = { - 'res_id': [l.id for l in leads], + 'res_id': list(leads), } if len(leads) == 1: action['views'].reverse() diff --git a/view/create_campaign_start_view_form.xml b/view/create_campaign_start_view_form.xml index b11fd3e..e5a7c3d 100644 --- a/view/create_campaign_start_view_form.xml +++ b/view/create_campaign_start_view_form.xml @@ -4,4 +4,6 @@ contains the full copyright notices and license terms. -->