~camptocamp/account-financial-tools/credit_control_report_improvement_vre

« back to all changes in this revision

Viewing changes to currency_rate_update/company.py

  • Committer: Joël Grand-Guillaume
  • Date: 2011-08-12 12:35:23 UTC
  • Revision ID: joel.grandguillaume@camptocamp.com-20110812123523-hksk8nby1tp64rx1
[ADD] First commit of the first financial modules to move in our new public branch
(lp:c2c-addons/6.1  rev 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
#  company.py
 
3
#  c2c_currency_update
 
4
# @author Nicolas Bessi 
 
5
#  Copyright (c) 2009 CamptoCamp. All rights reserved.
 
6
##############################################################################
 
7
#
 
8
# WARNING: This program as such is intended to be used by professional
 
9
# programmers who take the whole responsability of assessing all potential
 
10
# consequences resulting from its eventual inadequacies and bugs
 
11
# End users who are looking for a ready-to-use solution with commercial
 
12
# garantees and support are strongly adviced to contract a Free Software
 
13
# Service Company
 
14
#
 
15
# This program is Free Software; you can redistribute it and/or
 
16
# modify it under the terms of the GNU General Public License
 
17
# as published by the Free Software Foundation; either version 2
 
18
# of the License, or (at your option) any later version.
 
19
#
 
20
# This program is distributed in the hope that it will be useful,
 
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
# GNU General Public License for more details.
 
24
#
 
25
# You should have received a copy of the GNU General Public License
 
26
# along with this program; if not, write to the Free Software
 
27
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
28
#
 
29
##############################################################################
 
30
 
 
31
import netsvc
 
32
from osv import fields, osv
 
33
class res_company(osv.osv):
 
34
    """override company to add currency update"""
 
35
    
 
36
    def _multi_curr_enable(self, cr, uid, ids, field_name, arg, context={}):
 
37
        "check if multi company currency is enabled"
 
38
        result = {}
 
39
        if self.pool.get('ir.model.fields').search(cr, uid, [('name', '=', 'company_id'), ('model', '=', 'res.currency')])==[]:
 
40
            enable = 0
 
41
        else:
 
42
            enable = 1
 
43
        for id in ids:
 
44
            result[id] = enable
 
45
        return result
 
46
        
 
47
        
 
48
    def button_refresh_currency(self, cr, uid, ids, context=None):
 
49
        """Refrech  the currency !!for all the company
 
50
        now"""
 
51
        currency_updater_obj = self.pool.get('currency.rate.update')
 
52
        try:
 
53
            currency_updater_obj.run_currency_update(cr, uid)
 
54
        except Exception, e:
 
55
            return False
 
56
        return True
 
57
        
 
58
        
 
59
    def _on_change_auto_currency_up(self, cr, uid, id, value):
 
60
        """handle the activation of the currecny update on compagnies.
 
61
        There are two ways of implementing multi_company currency, 
 
62
        the currency is shared or not. The module take care of the two
 
63
        ways. If the currency are shared, you will only be able to set 
 
64
        auto update on one company, this will avoid to have unusefull cron 
 
65
        object running. 
 
66
        If yours currency are not share you will be able to activate the
 
67
        auto update on each separated company"""
 
68
        
 
69
        if len(id) :
 
70
            id = id[0]
 
71
        else :
 
72
            return {}
 
73
        enable = self.browse(cr, uid, id).multi_company_currency_enable
 
74
        compagnies =  self.search(cr, uid, [])
 
75
        activate_cron = 'f'
 
76
        if not value :
 
77
            # this statement is here beacaus we do no want to save in case of error
 
78
            self.write(cr, uid, id,{'auto_currency_up':value})
 
79
            for comp in compagnies :
 
80
                if self.browse(cr, uid, comp).auto_currency_up: 
 
81
                    activate_cron = 't'
 
82
                    break
 
83
            self.pool.get('currency.rate.update').save_cron(
 
84
                                                            cr, 
 
85
                                                            uid, 
 
86
                                                            {'active':activate_cron}
 
87
                                                        )
 
88
            return {}
 
89
        else :
 
90
            for comp in compagnies :
 
91
                if comp != id and not enable:
 
92
                    if self.browse(cr, uid, comp).multi_company_currency_enable:
 
93
                        #we ensure taht we did not have write a true value
 
94
                        self.write(cr, uid, id,{'auto_currency_up':False})
 
95
                        return {
 
96
                                'value':{ 
 
97
                                            'auto_currency_up':False
 
98
                                        },
 
99
                                        
 
100
                                'warning':{
 
101
                                            'title':"Warning",
 
102
                                            'message': 'Yon can not activate auto currency '+\
 
103
                                            'update on more thant one company with this '+
 
104
                                            'multi company configuration'
 
105
                                            }
 
106
                                }
 
107
            self.write(cr, uid, id,{'auto_currency_up':value})
 
108
            for comp in compagnies :
 
109
                if self.browse(cr, uid, comp).auto_currency_up: 
 
110
                    activate_cron = 't'
 
111
                self.pool.get('currency.rate.update').save_cron(
 
112
                                                            cr, 
 
113
                                                            uid, 
 
114
                                                            {'active':activate_cron}
 
115
                                                        )
 
116
                break
 
117
            return {}
 
118
                    
 
119
            
 
120
    def _on_change_intervall(self, cr, uid, id, interval) :
 
121
        ###Function that will update the cron
 
122
        ###freqeuence
 
123
        self.pool.get('currency.rate.update').save_cron(
 
124
                                                            cr, 
 
125
                                                            uid, 
 
126
                                                            {'interval_type':interval}
 
127
                                                        )
 
128
        compagnies =  self.search(cr, uid, [])
 
129
        for comp in compagnies :
 
130
            self.write(cr, uid, comp,{'interval_type':interval})
 
131
        return {}
 
132
        
 
133
    _inherit = "res.company"
 
134
    _columns = {
 
135
        ### activate the currency update
 
136
        'auto_currency_up': fields.boolean('Automatical update of the currency this company'),
 
137
        'services_to_use' : fields.one2many(
 
138
                                            'currency.rate.update.service', 
 
139
                                            'company_id',
 
140
                                            'Currency update services' 
 
141
                                            ),
 
142
        ###predifine cron frequence
 
143
        'interval_type': fields.selection(
 
144
                                                [
 
145
                                                    ('days','Day(s)'), 
 
146
                                                    ('weeks', 'Week(s)'), 
 
147
                                                    ('months', 'Month(s)')
 
148
                                                ],
 
149
                                                'Currency update frequence',
 
150
                                                help="""changing this value will
 
151
                                                 also affect other compagnies"""
 
152
                                            ),
 
153
        ###function field that allows to know the
 
154
        ###mutli company currency implementation                                    
 
155
        'multi_company_currency_enable' : fields.function(
 
156
                                            _multi_curr_enable, 
 
157
                                            method=True, 
 
158
                                            type='boolean', 
 
159
                                            string="Multi company currency",
 
160
                                            help='if this case is not check you can'+\
 
161
                                            ' not set currency is active on two company'
 
162
                                        ),
 
163
    }    
 
164
res_company()