~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to financing_contract/contract.py

  • Committer: chloups208
  • Date: 2011-05-03 13:42:32 UTC
  • mto: (140.1.1 unifield-wm)
  • mto: This revision was merged to the branch mainline in revision 142.
  • Revision ID: chloups208@chloups208-laptop-20110503134232-omqmqbkrr8f3detr
[UF-33] correction of asset which is now linked to stock.move in a wizard when processing the stock.picking

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 MSF, TeMPO Consulting.
6
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
from osv import fields, osv
23
 
import datetime
24
 
 
25
 
class financing_contract_funding_pool_line(osv.osv):
26
 
    # 
27
 
    _name = "financing.contract.funding.pool.line"
28
 
    
29
 
    _columns = {
30
 
        'contract_id': fields.many2one('financing.contract.format', 'Contract', required=True),
31
 
        'funding_pool_id': fields.many2one('account.analytic.account', 'Funding pool name', required=True),
32
 
        'funded': fields.boolean('Funded'),
33
 
        'total_project': fields.boolean('Total project'),
34
 
    }
35
 
        
36
 
    _defaults = {
37
 
        'funded': False,
38
 
        'total_project': True,
39
 
    }
40
 
    
41
 
financing_contract_funding_pool_line()
42
 
 
43
 
class financing_contract_contract(osv.osv):
44
 
    
45
 
    _name = "financing.contract.contract"
46
 
    _inherits = {"financing.contract.format": "format_id"}
47
 
 
48
 
    def contract_open(self, cr, uid, ids, *args):
49
 
        self.write(cr, uid, ids, {
50
 
            'state': 'open',
51
 
            'open_date': datetime.date.today().strftime('%Y-%m-%d'),
52
 
            'soft_closed_date': None
53
 
        })
54
 
        return True
55
 
 
56
 
    def contract_soft_closed(self, cr, uid, ids, *args):
57
 
        self.write(cr, uid, ids, {
58
 
            'state': 'soft_closed',
59
 
            'soft_closed_date': datetime.date.today().strftime('%Y-%m-%d')
60
 
        })
61
 
        return True
62
 
 
63
 
    def contract_hard_closed(self, cr, uid, ids, *args):
64
 
        self.write(cr, uid, ids, {
65
 
            'state': 'hard_closed',
66
 
            'hard_closed_date': datetime.date.today().strftime('%Y-%m-%d')
67
 
        })
68
 
        return True
69
 
    
70
 
    def get_contract_domain(self, cr, uid, browse_contract, reporting_type=None, context={}):
71
 
        # we update the context with the contract reporting type and currency
72
 
        format_line_obj = self.pool.get('financing.contract.format.line')
73
 
        # Values to be set
74
 
        account_ids = []
75
 
        # general domain
76
 
        general_domain = format_line_obj._get_general_domain(cr,
77
 
                                                             uid,
78
 
                                                             browse_contract.format_id,
79
 
                                                             browse_contract.reporting_type,
80
 
                                                             context=context)
81
 
        
82
 
        # parse parent lines (either value or sum of children's values)
83
 
        for line in browse_contract.actual_line_ids:
84
 
            if not line.parent_id:
85
 
                account_ids += format_line_obj._get_account_ids(line, general_domain['funding_pool_account_ids'])
86
 
                
87
 
        # create the domain
88
 
        if reporting_type is None:
89
 
            reporting_type = browse_contract.reporting_type
90
 
        analytic_domain = []
91
 
        account_domain = format_line_obj._create_domain('general_account_id', account_ids)
92
 
        date_domain = eval(general_domain['date_domain'])
93
 
        if reporting_type == 'allocated':
94
 
            analytic_domain = [date_domain[0],
95
 
                               date_domain[1],
96
 
                               eval(account_domain),
97
 
                               eval(general_domain['funding_pool_domain'])]
98
 
        else: 
99
 
            analytic_domain = [date_domain[0],
100
 
                               date_domain[1],
101
 
                               eval(account_domain),
102
 
                               eval(general_domain['funding_pool_domain']),
103
 
                               eval(general_domain['cost_center_domain'])]
104
 
            
105
 
        return analytic_domain
106
 
    
107
 
    _columns = {
108
 
        'name': fields.char('Financing contract name', size=64, required=True),
109
 
        'code': fields.char('Financing contract code', size=16, required=True),
110
 
        'donor_id': fields.many2one('financing.contract.donor', 'Donor', required=True),
111
 
        'grant_name': fields.char('Grant name', size=64, required=True),
112
 
        'donor_grant_reference': fields.char('Donor grant reference', size=64),
113
 
        'hq_grant_reference': fields.char('HQ grant reference', size=64),
114
 
        'grant_amount': fields.float('Grant amount', size=64, required=True),
115
 
        'reporting_currency': fields.many2one('res.currency', 'Reporting currency', required=True),
116
 
        'notes': fields.text('Notes'),
117
 
        'open_date': fields.date('Open date'),
118
 
        'soft_closed_date': fields.date('Soft-closed date'),
119
 
        'hard_closed_date': fields.date('Hard-closed date'),
120
 
        'state': fields.selection([('draft','Draft'),
121
 
                                    ('open','Open'),
122
 
                                    ('soft_closed', 'Soft-closed'),
123
 
                                    ('hard_closed', 'Hard-closed')], 'State'),
124
 
        'currency_table_id': fields.many2one('res.currency.table', 'Currency Table'),
125
 
    }
126
 
    
127
 
    _defaults = {
128
 
        'state': 'draft',
129
 
        'reporting_currency': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
130
 
        'format_id': lambda self,cr,uid,context: self.pool.get('financing.contract.format').create(cr, uid, {}, context=context)
131
 
    }
132
 
    
133
 
    def onchange_donor_id(self, cr, uid, ids, donor_id, format_id, actual_line_ids, context={}):
134
 
        res = {}
135
 
        if donor_id and format_id:
136
 
            donor = self.pool.get('financing.contract.donor').browse(cr, uid, donor_id, context=context)
137
 
            if donor.format_id:
138
 
                source_format = donor.format_id
139
 
                format_vals = {
140
 
                    'format_name': source_format.format_name,
141
 
                    'reporting_type': source_format.reporting_type,
142
 
                }
143
 
                self.pool.get('financing.contract.format').copy_format_lines(cr, uid, donor.format_id.id, format_id, context=context)
144
 
        return {'value': format_vals}
145
 
    
146
 
    def onchange_currency_table(self, cr, uid, ids, currency_table_id, reporting_currency_id, context={}):
147
 
        values = {'reporting_currency': False}
148
 
        if reporting_currency_id:
149
 
            # it can be a currency from another table
150
 
            reporting_currency = self.pool.get('res.currency').browse(cr, uid, reporting_currency_id, context=context)
151
 
            # Search if the currency is in the table, and active
152
 
            if reporting_currency.reference_currency_id:
153
 
                currency_results = self.pool.get('res.currency').search(cr, uid, [('reference_currency_id', '=', reporting_currency.reference_currency_id.id),
154
 
                                                                                  ('currency_table_id', '=', currency_table_id),
155
 
                                                                                  ('active', '=', True)], context=context)
156
 
            else:
157
 
                currency_results = self.pool.get('res.currency').search(cr, uid, [('reference_currency_id', '=', reporting_currency_id),
158
 
                                                                                  ('currency_table_id', '=', currency_table_id),
159
 
                                                                                  ('active', '=', True)], context=context)
160
 
            if len(currency_results) > 0:
161
 
                # it's here, we keep the currency
162
 
                values['reporting_currency'] = reporting_currency_id
163
 
        # Restrain domain to selected table (or None if none selected
164
 
        domains = {'reporting_currency': [('currency_table_id', '=', currency_table_id)]}
165
 
        return {'value': values, 'domain': domains}
166
 
    
167
 
    def create_reporting_line(self, cr, uid, browse_contract, browse_format_line, parent_report_line_id=None, context=None):
168
 
        format_line_obj = self.pool.get('financing.contract.format.line')
169
 
        reporting_line_obj = self.pool.get('financing.contract.donor.reporting.line')
170
 
        analytic_domain = format_line_obj._get_analytic_domain(cr,
171
 
                                                               uid,
172
 
                                                               browse_format_line,
173
 
                                                               browse_contract.reporting_type,
174
 
                                                               context=context)
175
 
        vals = {'name': browse_format_line.name,
176
 
                'code': browse_format_line.code,
177
 
                'allocated_budget': round(browse_format_line.allocated_budget),
178
 
                'project_budget': round(browse_format_line.project_budget),
179
 
                'allocated_real': round(browse_format_line.allocated_real),
180
 
                'project_real': round(browse_format_line.project_real),
181
 
                'analytic_domain': analytic_domain,
182
 
                'parent_id': parent_report_line_id}
183
 
        reporting_line_id = reporting_line_obj.create(cr,
184
 
                                                      uid,
185
 
                                                      vals,
186
 
                                                      context=context)
187
 
        # create child lines
188
 
        for child_line in browse_format_line.child_ids:
189
 
            self.create_reporting_line(cr, uid, browse_contract, child_line, reporting_line_id, context=context)
190
 
        return reporting_line_id
191
 
    
192
 
    def menu_interactive_report(self, cr, uid, ids, context={}):
193
 
        # we update the context with the contract reporting type
194
 
        contract = self.browse(cr, uid, ids[0], context=context)
195
 
        context.update({'reporting_currency': contract.reporting_currency.id,
196
 
                        'reporting_type': contract.reporting_type,
197
 
                        'currency_table_id': contract.currency_table_id.id,
198
 
                        'active_id': ids[0],
199
 
                        'active_ids': ids})
200
 
        reporting_line_obj = self.pool.get('financing.contract.donor.reporting.line')
201
 
        # Create reporting lines
202
 
        # Contract line first (we'll fill it later)
203
 
        contract_line_id = reporting_line_obj.create(cr,
204
 
                                                     uid,
205
 
                                                     vals = {'name': contract.name,
206
 
                                                             'code': contract.code},
207
 
                                                     context=context)
208
 
        
209
 
        # Values to be set
210
 
        allocated_budget = 0
211
 
        project_budget = 0
212
 
        allocated_real = 0
213
 
        project_real = 0
214
 
        
215
 
        # create "real" lines
216
 
        for line in contract.actual_line_ids:
217
 
            if not line.parent_id:
218
 
                allocated_budget += line.allocated_budget
219
 
                project_budget += line.project_budget
220
 
                allocated_real += line.allocated_real
221
 
                project_real += line.project_real
222
 
                reporting_line_id = self.create_reporting_line(cr, uid, contract, line, contract_line_id, context=context)
223
 
        
224
 
        # Refresh contract line with general infos
225
 
        analytic_domain = self.get_contract_domain(cr, uid, contract, context=context)
226
 
        contract_values = {'allocated_budget': allocated_budget,
227
 
                           'project_budget': project_budget,
228
 
                           'allocated_real': allocated_real,
229
 
                           'project_real': project_real,
230
 
                           'analytic_domain': analytic_domain}
231
 
        reporting_line_obj.write(cr, uid, [contract_line_id], vals=contract_values, context=context)
232
 
        
233
 
        # retrieve the corresponding_view
234
 
        model_data_obj = self.pool.get('ir.model.data')
235
 
        view_id = False
236
 
        view_ids = model_data_obj.search(cr, uid, 
237
 
                                        [('module', '=', 'financing_contract'), 
238
 
                                         ('name', '=', 'view_donor_reporting_line_tree_%s' % str(contract.reporting_type))],
239
 
                                        offset=0, limit=1)
240
 
        if len(view_ids) > 0:
241
 
            view_id = model_data_obj.browse(cr, uid, view_ids[0]).res_id
242
 
        return {
243
 
               'type': 'ir.actions.act_window',
244
 
               'res_model': 'financing.contract.donor.reporting.line',
245
 
               'view_type': 'tree',
246
 
               'view_id': [view_id],
247
 
               'target': 'current',
248
 
               'domain': [('id', '=', contract_line_id)],
249
 
               'context': context
250
 
        }
251
 
        
252
 
    def menu_allocated_expense_report(self, cr, uid, ids, context={}):
253
 
        wiz_obj = self.pool.get('wizard.expense.report')
254
 
        wiz_id = wiz_obj.create(cr, uid, {'reporting_type': 'allocated',
255
 
                                          'filename': 'allocated_expenses.csv',
256
 
                                          'contract_id': ids[0]}, context=context)
257
 
        # we open a wizard
258
 
        return {
259
 
                'type': 'ir.actions.act_window',
260
 
                'res_model': 'wizard.expense.report',
261
 
                'view_type': 'form',
262
 
                'view_mode': 'form',
263
 
                'target': 'new',
264
 
                'res_id': [wiz_id],
265
 
                'context': context,
266
 
        }
267
 
        
268
 
    def menu_project_expense_report(self, cr, uid, ids, context={}):
269
 
        wiz_obj = self.pool.get('wizard.expense.report')
270
 
        wiz_id = wiz_obj.create(cr, uid, {'reporting_type': 'project',
271
 
                                          'filename': 'project_expenses.csv',
272
 
                                          'contract_id': ids[0]}, context=context)
273
 
        # we open a wizard
274
 
        return {
275
 
                'type': 'ir.actions.act_window',
276
 
                'res_model': 'wizard.expense.report',
277
 
                'view_type': 'form',
278
 
                'view_mode': 'form',
279
 
                'target': 'new',
280
 
                'res_id': [wiz_id],
281
 
                'context': context,
282
 
        }
283
 
    
284
 
financing_contract_contract()
285
 
 
286
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: