[FIX] sale: sales_team: fixed use_quotations field named 'opportunities' instead...
[odoo/odoo.git] / addons / sale / sales_team.py
1 # -*- coding: utf-8 -*-
2
3 import calendar
4 from datetime import date
5 from dateutil import relativedelta
6
7 from openerp import tools
8 from openerp.osv import fields, osv
9
10
11 class crm_case_section(osv.osv):
12     _inherit = 'crm.case.section'
13
14     def _get_sale_orders_data(self, cr, uid, ids, field_name, arg, context=None):
15         obj = self.pool.get('sale.order')
16         res = dict.fromkeys(ids, False)
17         month_begin = date.today().replace(day=1)
18         date_begin = (month_begin - relativedelta.relativedelta(months=self._period_number - 1)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
19         date_end = month_begin.replace(day=calendar.monthrange(month_begin.year, month_begin.month)[1]).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
20         for id in ids:
21             res[id] = dict()
22             created_domain = [('section_id', '=', id), ('state', '=', 'draft'), ('date_order', '>=', date_begin), ('date_order', '<=', date_end)]
23             res[id]['monthly_quoted'] = self.__get_bar_values(cr, uid, obj, created_domain, ['amount_total', 'date_order'], 'amount_total', 'date_order', context=context)
24             validated_domain = [('section_id', '=', id), ('state', 'not in', ['draft', 'sent', 'cancel']), ('date_order', '>=', date_begin), ('date_order', '<=', date_end)]
25             res[id]['monthly_confirmed'] = self.__get_bar_values(cr, uid, obj, validated_domain, ['amount_total', 'date_order'], 'amount_total', 'date_order', context=context)
26         return res
27
28     def _get_invoices_data(self, cr, uid, ids, field_name, arg, context=None):
29         obj = self.pool.get('account.invoice.report')
30         res = dict.fromkeys(ids, False)
31         month_begin = date.today().replace(day=1)
32         date_begin = (month_begin - relativedelta.relativedelta(months=self._period_number - 1)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
33         date_end = month_begin.replace(day=calendar.monthrange(month_begin.year, month_begin.month)[1]).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
34         for id in ids:
35             created_domain = [('section_id', '=', id), ('state', 'not in', ['draft', 'cancel']), ('date', '>=', date_begin), ('date', '<=', date_end)]
36             res[id] = self.__get_bar_values(cr, uid, obj, created_domain, ['price_total', 'date'], 'price_total', 'date', context=context)
37         return res
38
39     _columns = {
40         'use_quotations': fields.boolean('Quotations', help="Check this box to manage quotations in this sales team."),
41         'invoiced_forecast': fields.integer(string='Invoice Forecast',
42             help="Forecast of the invoice revenue for the current month. This is the amount the sales \n"
43                     "team should invoice this month. It is used to compute the progression ratio \n"
44                     " of the current and forecast revenue on the kanban view."),
45         'invoiced_target': fields.integer(string='Invoice Target',
46             help="Target of invoice revenue for the current month. This is the amount the sales \n"
47                     "team estimates to be able to invoice this month."),
48         'monthly_quoted': fields.function(_get_sale_orders_data,
49             type='string', readonly=True, multi='_get_sale_orders_data',
50             string='Rate of created quotation per duration'),
51         'monthly_confirmed': fields.function(_get_sale_orders_data,
52             type='string', readonly=True, multi='_get_sale_orders_data',
53             string='Rate of validate sales orders per duration'),
54         'monthly_invoiced': fields.function(_get_invoices_data,
55             type='string', readonly=True,
56             string='Rate of sent invoices per duration'),
57     }
58
59     _defaults = {
60         'use_quotations': True,
61     }
62
63     def action_forecast(self, cr, uid, id, value, context=None):
64         return self.write(cr, uid, [id], {'invoiced_forecast': round(float(value))}, context=context)
65
66 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: