~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to c2c_budget/c2c_budget.py

  • Committer: Launchpad Translations on behalf of openerp-commiter
  • Date: 2012-11-24 07:05:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5825.
  • Revision ID: launchpad_translations_on_behalf_of_openerp-commiter-20121124070522-2i7pnpw5hq4wuz4g
Launchpad automatic translations update.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*- 
2
 
##############################################################################
3
 
#
4
 
# Copyright (c) Camptocamp SA - http://www.camptocamp.com
5
 
# Author: Arnaud Wüst
6
 
#
7
 
#    This file is part of the c2c_budget module
8
 
#
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
14
 
# Service Company
15
 
#
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.
20
 
#
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.
25
 
#
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.
29
 
#
30
 
##############################################################################
31
 
 
32
 
from osv import fields, osv
33
 
import time
34
 
import pooler
35
 
 
36
 
 
37
 
class c2c_budget(osv.osv):
38
 
    """ camptocamp budget. The module's main object.  """
39
 
    
40
 
    _name = "c2c_budget"
41
 
    _description = "Budget"
42
 
    _columns = {
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)
52
 
        }
53
 
    
54
 
    _defaults = {
55
 
        'active' : lambda *a : True,         
56
 
        }
57
 
    
58
 
    _order = 'name'   
59
 
    
60
 
 
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 """
63
 
                
64
 
        if not args:
65
 
            args=[]
66
 
        if not context:
67
 
            context={}
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)        
71
 
 
72
 
    
73
 
    
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)
77
 
        for l in lines:
78
 
            if l.end_date < l.start_date:
79
 
                return False
80
 
        return True        
81
 
     
82
 
    def get_periods(self, cr, uid, ids, context={}):
83
 
        """ return the list of budget's periods ordered by date_start"""
84
 
        
85
 
        period_obj = pooler.get_pool(cr.dbname).get('account.period')
86
 
 
87
 
        result = []
88
 
 
89
 
        if type(ids)==int:
90
 
            budget_ids = [ids]
91
 
            
92
 
        budgets = self.browse(cr, uid, budget_ids, context)
93
 
        
94
 
        start_date = None
95
 
        end_date = None
96
 
        
97
 
        for b in budgets:
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))
100
 
            
101
 
        if type(ids)==int:
102
 
            result = result[0]
103
 
        
104
 
        return result
105
 
    
106
 
    
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
110
 
        """
111
 
        
112
 
        period_obj = pooler.get_pool(cr.dbname).get('account.period')
113
 
 
114
 
        result = []
115
 
        if type(ids)==int:
116
 
            budget_ids = [ids]
117
 
        else: 
118
 
            budget_ids = ids
119
 
            
120
 
        budgets = self.browse(cr, uid, budget_ids, context)
121
 
 
122
 
        #find the earliest start_date en latest end_date
123
 
        start_date = None
124
 
        end_date = None
125
 
        for b in budgets:
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
130
 
                   
131
 
                   
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)
135
 
            
136
 
        if type(ids)==int:
137
 
            return result[0]
138
 
        else: 
139
 
            return result
140
 
 
141
 
                
142
 
        
143
 
        
144
 
    
145
 
    def unlink(self, cr, uid, ids, context={}):
146
 
        """delete all budget versions when deleting a budget """
147
 
        
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)
150
 
        
151
 
        budget_version_obj.unlink(cr, uid, lines_ids, context)
152
 
        return super(c2c_budget, self).unlink(cr, uid, ids, context)
153
 
        
154
 
             
155
 
     
156
 
        
157
 
    _constraints = [
158
 
        (_check_start_end_dates, 'Date Error: The end date is defined before the start date', ['start_date', 'end_date']),
159
 
    ]
160
 
    
161
 
c2c_budget()
162