1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Copyright (c) Camptocamp SA - http://www.camptocamp.com
7
# This file is part of the c2c_budget module
9
# WARNING: This program as such is intended to be used by professional
10
# programmers who take the whole responsability of assessing all potential
11
# consequences resulting from its eventual inadequacies and bugs
12
# End users who are looking for a ready-to-use solution with commercial
13
# garantees and support are strongly adviced to contract a Free Software
16
# This program is Free Software; you can redistribute it and/or
17
# modify it under the terms of the GNU General Public License
18
# as published by the Free Software Foundation; either version 2
19
# of the License, or (at your option) any later version.
21
# This program is distributed in the hope that it will be useful,
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
# GNU General Public License for more details.
26
# You should have received a copy of the GNU General Public License
27
# along with this program; if not, write to the Free Software
28
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30
##############################################################################
32
from osv import fields, osv
37
class c2c_budget(osv.osv):
38
""" camptocamp budget. The module's main object. """
41
_description = "Budget"
43
'code' : fields.char('Code', size=50),
44
'name' : fields.char('Name', size=200, required=True),
45
'active' : fields.boolean('Active'),
46
'start_date' : fields.date('Start Date', required=True),
47
'end_date' : fields.date('End Date', required=True),
48
'budget_item_id' : fields.many2one('c2c_budget.item', 'Budget Structure', required=True),
49
'budget_version_ids' : fields.one2many('c2c_budget.version', 'budget_id', 'Budget Versions', readonly=True),
50
'note' : fields.text('Notes'),
51
'create_date' : fields.datetime('Creation Date', readonly=True)
55
'active' : lambda *a : True,
61
def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
62
"""search not only for a matching names but also for a matching codes """
68
ids = self.search(cr, user, [('code',operator,name)]+ args, limit=limit, context=context)
69
ids += self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
70
return self.name_get(cr, user, ids, context)
74
def _check_start_end_dates(self, cr, uid, ids):
75
""" check the start date is before the end date """
76
lines = self.browse(cr, uid, ids)
78
if l.end_date < l.start_date:
82
def get_periods(self, cr, uid, ids, context={}):
83
""" return the list of budget's periods ordered by date_start"""
85
period_obj = pooler.get_pool(cr.dbname).get('account.period')
92
budgets = self.browse(cr, uid, budget_ids, context)
98
periods_ids = period_obj.search(cr, uid, [('date_stop', '>', b.start_date), ('date_start', '<', b.end_date)], order="date_start ASC")
99
result.append(period_obj.browse(cr, uid, periods_ids, context))
107
def get_periods_union(self, cr, uid, ids, context={}):
108
""" return the list of budget's periods ordered by date_start
109
it returns a unique list that cover all given budgets ids
112
period_obj = pooler.get_pool(cr.dbname).get('account.period')
120
budgets = self.browse(cr, uid, budget_ids, context)
122
#find the earliest start_date en latest end_date
126
if start_date is None or start_date > b.start_date:
127
start_date = b.start_date
128
if end_date is None or end_date < b.end_date:
129
end_date = b.end_date
132
if start_date is not None :
133
periods_ids = period_obj.search(cr, uid, [('date_stop', '>', start_date), ('date_start', '<', end_date)], order="date_start ASC")
134
result = period_obj.browse(cr, uid, periods_ids, context)
145
def unlink(self, cr, uid, ids, context={}):
146
"""delete all budget versions when deleting a budget """
148
budget_version_obj = pooler.get_pool(cr.dbname).get('c2c_budget.version')
149
lines_ids = budget_version_obj.search(cr, uid, [('budget_id', 'in', ids)], context=context)
151
budget_version_obj.unlink(cr, uid, lines_ids, context)
152
return super(c2c_budget, self).unlink(cr, uid, ids, context)
158
(_check_start_end_dates, 'Date Error: The end date is defined before the start date', ['start_date', 'end_date']),