From 3406b6db397506c3fedb974f0fe470e2d99b9a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 12 May 2014 14:56:01 +0200 Subject: [PATCH] [CLEAN] sale, sale_crm: cleaning before merging. Cleaned calculation of default section id + removed duplicated code. --- addons/crm/__openerp__.py | 2 +- ...e_section_view.xml => sales_team_view.xml} | 0 .../sale_crm_goals_demo.xml | 2 +- addons/sale/__init__.py | 8 +-- addons/sale/__openerp__.py | 2 +- addons/sale/sale.py | 63 ++++++++++++++----- .../{crm_case_section.py => sales_team.py} | 30 +-------- ...e_section_view.xml => sales_team_view.xml} | 0 addons/sale_crm/sale_crm.py | 1 - 9 files changed, 56 insertions(+), 52 deletions(-) rename addons/crm/{crm_case_section_view.xml => sales_team_view.xml} (100%) rename addons/sale/{crm_case_section.py => sales_team.py} (74%) rename addons/sale/{crm_case_section_view.xml => sales_team_view.xml} (100%) diff --git a/addons/crm/__openerp__.py b/addons/crm/__openerp__.py index ecdefb9e051..1bf4c611be6 100644 --- a/addons/crm/__openerp__.py +++ b/addons/crm/__openerp__.py @@ -91,7 +91,7 @@ Dashboard for CRM will include: 'res_config_view.xml', 'base_partner_merge_view.xml', - 'crm_case_section_view.xml', + 'sales_team_view.xml', 'views/crm.xml', ], 'demo': [ diff --git a/addons/crm/crm_case_section_view.xml b/addons/crm/sales_team_view.xml similarity index 100% rename from addons/crm/crm_case_section_view.xml rename to addons/crm/sales_team_view.xml diff --git a/addons/gamification_sale_crm/sale_crm_goals_demo.xml b/addons/gamification_sale_crm/sale_crm_goals_demo.xml index a4720b1e566..b149af31f48 100644 --- a/addons/gamification_sale_crm/sale_crm_goals_demo.xml +++ b/addons/gamification_sale_crm/sale_crm_goals_demo.xml @@ -6,6 +6,6 @@ inprogress - + diff --git a/addons/sale/__init__.py b/addons/sale/__init__.py index 509364643ae..c99fc936467 100644 --- a/addons/sale/__init__.py +++ b/addons/sale/__init__.py @@ -19,16 +19,10 @@ # ############################################################################## -#---------------------------------------------------------- -# Init Sales -#---------------------------------------------------------- - import sale -import crm_case_section +import sales_team import res_partner import wizard import report import edi import res_config - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/sale/__openerp__.py b/addons/sale/__openerp__.py index 52baff32ab7..039e765cd71 100644 --- a/addons/sale/__openerp__.py +++ b/addons/sale/__openerp__.py @@ -71,7 +71,7 @@ The Dashboard for the Sales Manager will include 'sale_report.xml', 'sale_data.xml', 'sale_view.xml', - 'crm_case_section_view.xml', + 'sales_team_view.xml', 'res_partner_view.xml', 'report/sale_report_view.xml', 'edi/sale_order_action_data.xml', diff --git a/addons/sale/sale.py b/addons/sale/sale.py index 34886a22991..e3b59c00015 100644 --- a/addons/sale/sale.py +++ b/addons/sale/sale.py @@ -19,11 +19,7 @@ # ############################################################################## -import calendar -from openerp import tools -from datetime import date, datetime, timedelta -from dateutil.relativedelta import relativedelta -from dateutil import relativedelta +from datetime import datetime, timedelta import time from openerp.osv import fields, osv from openerp.tools.translate import _ @@ -166,12 +162,29 @@ class sale_order(osv.osv): if not company_id: raise osv.except_osv(_('Error!'), _('There is no default company for the current user!')) return company_id - + def _get_default_section_id(self, cr, uid, context=None): """ Gives default section by checking if present in the context """ - section_id = self.pool.get('res.users').browse(cr, uid, uid, context).default_section_id.id or False + section_id = self._resolve_section_id_from_context(cr, uid, context=context) or False + if not section_id: + section_id = self.pool.get('res.users').browse(cr, uid, uid, context).default_section_id.id or False return section_id + def _resolve_section_id_from_context(self, cr, uid, context=None): + """ Returns ID of section based on the value of 'section_id' + context key, or None if it cannot be resolved to a single + Sales Team. + """ + if context is None: + context = {} + if type(context.get('default_section_id')) in (int, long): + return context.get('default_section_id') + if isinstance(context.get('default_section_id'), basestring): + section_ids = self.pool.get('crm.case.section').name_search(cr, uid, name=context['default_section_id'], context=context) + if len(section_ids) == 1: + return int(section_ids[0][0]) + return None + _columns = { 'name': fields.char('Order Reference', size=64, required=True, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True), @@ -252,7 +265,6 @@ class sale_order(osv.osv): 'partner_shipping_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').address_get(cr, uid, [context['partner_id']], ['delivery'])['delivery'], 'note': lambda self, cr, uid, context: self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.sale_note, 'section_id': lambda s, cr, uid, c: s._get_default_section_id(cr, uid, c), - } _sql_constraints = [ ('name_uniq', 'unique(name, company_id)', 'Order Reference must be unique per Company!'), @@ -1199,13 +1211,40 @@ class mail_compose_message(osv.Model): self.pool.get('sale.order').signal_quotation_sent(cr, uid, [context['default_res_id']]) return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context) + class account_invoice(osv.Model): _inherit = 'account.invoice' - + + def _get_default_section_id(self, cr, uid, context=None): + """ Gives default section by checking if present in the context """ + section_id = self._resolve_section_id_from_context(cr, uid, context=context) or False + if not section_id: + section_id = self.pool.get('res.users').browse(cr, uid, uid, context).default_section_id.id or False + return section_id + + def _resolve_section_id_from_context(self, cr, uid, context=None): + """ Returns ID of section based on the value of 'section_id' + context key, or None if it cannot be resolved to a single + Sales Team. + """ + if context is None: + context = {} + if type(context.get('default_section_id')) in (int, long): + return context.get('default_section_id') + if isinstance(context.get('default_section_id'), basestring): + section_ids = self.pool.get('crm.case.section').name_search(cr, uid, name=context['default_section_id'], context=context) + if len(section_ids) == 1: + return int(section_ids[0][0]) + return None + _columns = { 'section_id': fields.many2one('crm.case.section', 'Sales Team'), } - + + _defaults = { + 'section_id': lambda self, cr, uid, c=None: self._get_default_section_id(cr, uid, context=c) + } + def confirm_paid(self, cr, uid, ids, context=None): sale_order_obj = self.pool.get('sale.order') res = super(account_invoice, self).confirm_paid(cr, uid, ids, context=context) @@ -1224,10 +1263,6 @@ class account_invoice(osv.Model): for id in ids: workflow.trg_validate(uid, 'account.invoice', id, 'invoice_cancel', cr) return super(account_invoice, self).unlink(cr, uid, ids, context=context) - - _defaults = { - 'section_id': lambda self, cr, uid, c=None: self.pool.get('res.users').browse(cr, uid, uid, c).default_section_id.id or False, - } class procurement_order(osv.osv): diff --git a/addons/sale/crm_case_section.py b/addons/sale/sales_team.py similarity index 74% rename from addons/sale/crm_case_section.py rename to addons/sale/sales_team.py index 11c8cdc8bb4..e8161d7325c 100644 --- a/addons/sale/crm_case_section.py +++ b/addons/sale/sales_team.py @@ -1,35 +1,11 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## import calendar -from openerp import tools -from datetime import date, datetime, timedelta +from datetime import date from dateutil.relativedelta import relativedelta -from dateutil import relativedelta -import time + +from openerp import tools from openerp.osv import fields, osv -from openerp.tools.translate import _ -from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP, float_compare -import openerp.addons.decimal_precision as dp -from openerp import workflow class crm_case_section(osv.osv): diff --git a/addons/sale/crm_case_section_view.xml b/addons/sale/sales_team_view.xml similarity index 100% rename from addons/sale/crm_case_section_view.xml rename to addons/sale/sales_team_view.xml diff --git a/addons/sale_crm/sale_crm.py b/addons/sale_crm/sale_crm.py index 1b91ef4ef49..92c0abf2df0 100644 --- a/addons/sale_crm/sale_crm.py +++ b/addons/sale_crm/sale_crm.py @@ -29,7 +29,6 @@ from openerp.osv import osv, fields class sale_order(osv.osv): _inherit = 'sale.order' _columns = { - 'section_id': fields.many2one('crm.case.section', 'Sales Team'), 'categ_ids': fields.many2many('crm.case.categ', 'sale_order_category_rel', 'order_id', 'category_id', 'Tags', \ domain="['|', ('section_id', '=', section_id), ('section_id', '=', False), ('object_id.model', '=', 'crm.lead')]", context="{'object_name': 'crm.lead'}") }