1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from datetime import datetime
23 from openerp.osv import osv, fields
26 class sale_order(osv.osv):
27 _inherit = 'sale.order'
29 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
30 'categ_ids': fields.many2many('crm.case.categ', 'sale_order_category_rel', 'order_id', 'category_id', 'Categories', \
31 domain="['|',('section_id','=',section_id),('section_id','=',False), ('object_id.model', '=', 'crm.lead')]")
35 class crm_case_section(osv.osv):
36 _inherit = 'crm.case.section'
38 def _get_sum_month_invoice(self, cr, uid, ids, field_name, arg, context=None):
39 res = dict.fromkeys(ids, 0)
40 obj = self.pool.get('account.invoice.report')
41 when = datetime.today()
42 for section_id in ids:
43 invoice_ids = obj.search(cr, uid, [("section_id", "=", section_id), ('state', 'not in', ['draft', 'cancel']), ('year', '=', when.year), ('month', '=', when.month > 9 and when.month or "0%s" % when.month)], context=context)
44 for invoice in obj.browse(cr, uid, invoice_ids, context=context):
45 res[section_id] += invoice.price_total
49 'quotation_ids': fields.one2many('sale.order', 'section_id',
50 string='Quotations', readonly=True,
51 domain=[('state', 'in', ['draft', 'sent', 'cancel'])]),
52 'sale_order_ids': fields.one2many('sale.order', 'section_id',
53 string='Sale Orders', readonly=True,
54 domain=[('state', 'not in', ['draft', 'sent', 'cancel'])]),
55 'invoice_ids': fields.one2many('account.invoice', 'section_id',
56 string='Invoices', readonly=True,
57 domain=[('state', 'not in', ['draft', 'cancel'])]),
58 'sum_month_invoice': fields.function(_get_sum_month_invoice,
59 string='Total invoiced this month',
60 type='integer', readonly=True),
64 class res_users(osv.Model):
65 _inherit = 'res.users'
67 'default_section_id': fields.many2one('crm.case.section', 'Default Sales Team'),
71 class sale_crm_lead(osv.Model):
74 def on_change_user(self, cr, uid, ids, user_id, context=None):
75 """ Override of on change user_id on lead/opportunity; when having sale
77 - use user.default_section_id
78 - or fallback on previous behavior """
80 user = self.pool.get('res.users').browse(cr, uid, user_id, context=context)
81 if user.default_section_id and user.default_section_id.id:
82 return {'value': {'section_id': user.default_section_id.id}}
83 return super(sale_crm_lead, self).on_change_user(cr, uid, ids, user_id, context=context)
86 class account_invoice(osv.osv):
87 _inherit = 'account.invoice'
89 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
92 'section_id': lambda self, cr, uid, c=None: self.pool.get('res.users').browse(cr, uid, uid, c).default_section_id.id or False,
95 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: