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 date
23 from dateutil import relativedelta
25 from openerp import tools
26 from openerp.osv import osv, fields
29 class sale_order(osv.osv):
30 _inherit = 'sale.order'
32 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
33 'categ_ids': fields.many2many('crm.case.categ', 'sale_order_category_rel', 'order_id', 'category_id', 'Categories', \
34 domain="['|',('section_id','=',section_id),('section_id','=',False), ('object_id.model', '=', 'crm.lead')]")
37 def _prepare_invoice(self, cr, uid, order, lines, context=None):
38 invoice_vals = super(sale_order, self)._prepare_invoice(cr, uid, order, lines, context=context)
39 if order.section_id and order.section_id.id:
40 invoice_vals['section_id'] = order.section_id.id
44 class crm_case_section(osv.osv):
45 _inherit = 'crm.case.section'
47 def _get_sale_orders_data(self, cr, uid, ids, field_name, arg, context=None):
48 obj = self.pool.get('sale.order')
49 res = dict.fromkeys(ids, False)
50 month_begin = date.today().replace(day=1)
51 groupby_begin = (month_begin + relativedelta.relativedelta(months=-4)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
54 created_domain = [('section_id', '=', id), ('state', 'in', ['draft', 'sent']), ('date_order', '>=', groupby_begin)]
55 res[id]['monthly_quoted'] = self.__get_bar_values(cr, uid, obj, created_domain, ['amount_total', 'date_order'], 'amount_total', 'date_order', context=context)
56 validated_domain = [('section_id', '=', id), ('state', 'not in', ['draft', 'sent']), ('date_confirm', '>=', groupby_begin)]
57 res[id]['monthly_confirmed'] = self.__get_bar_values(cr, uid, obj, validated_domain, ['amount_total', 'date_confirm'], 'amount_total', 'date_confirm', context=context)
60 def _get_invoices_data(self, cr, uid, ids, field_name, arg, context=None):
61 obj = self.pool.get('account.invoice.report')
62 res = dict.fromkeys(ids, False)
63 month_begin = date.today().replace(day=1)
64 groupby_begin = (month_begin + relativedelta.relativedelta(months=-4)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
66 created_domain = [('section_id', '=', id), ('state', 'not in', ['draft', 'cancel']), ('date', '>=', groupby_begin)]
67 res[id] = self.__get_bar_values(cr, uid, obj, created_domain, ['price_total', 'date'], 'price_total', 'date', context=context)
71 'invoiced_forecast': fields.integer(string='Invoice Forecast',
72 help="Forecast of the invoice revenue for the current month. This is the amount the sales \n"
73 "team should invoice this month. It is used to compute the progression ratio \n"
74 " of the current and forecast revenue on the kanban view."),
75 'invoiced_target': fields.integer(string='Invoice Target',
76 help="Target of invoice revenue for the current month. This is the amount the sales \n"
77 "team estimates to be able to invoice this month."),
78 'monthly_quoted': fields.function(_get_sale_orders_data,
79 type='string', readonly=True, multi='_get_sale_orders_data',
80 string='Rate of created quotation per duration'),
81 'monthly_confirmed': fields.function(_get_sale_orders_data,
82 type='string', readonly=True, multi='_get_sale_orders_data',
83 string='Rate of validate sales orders per duration'),
84 'monthly_invoiced': fields.function(_get_invoices_data,
85 type='string', readonly=True,
86 string='Rate of sent invoices per duration'),
89 def action_forecast(self, cr, uid, id, value, context=None):
90 return self.write(cr, uid, [id], {'invoiced_forecast': value}, context=context)
93 class res_users(osv.Model):
94 _inherit = 'res.users'
96 'default_section_id': fields.many2one('crm.case.section', 'Default Sales Team'),
100 class sale_crm_lead(osv.Model):
101 _inherit = 'crm.lead'
103 def on_change_user(self, cr, uid, ids, user_id, context=None):
104 """ Override of on change user_id on lead/opportunity; when having sale
106 - use user.default_section_id
107 - or fallback on previous behavior """
109 user = self.pool.get('res.users').browse(cr, uid, user_id, context=context)
110 if user.default_section_id and user.default_section_id.id:
111 return {'value': {'section_id': user.default_section_id.id}}
112 return super(sale_crm_lead, self).on_change_user(cr, uid, ids, user_id, context=context)
115 class account_invoice(osv.osv):
116 _inherit = 'account.invoice'
118 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
121 'section_id': lambda self, cr, uid, c=None: self.pool.get('res.users').browse(cr, uid, uid, c).default_section_id.id or False,
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: