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

« back to all changes in this revision

Viewing changes to financing_contract/contract.py

  • Committer: Quentin THEURET
  • Date: 2014-03-05 16:51:14 UTC
  • mto: (2004.1.33 unifield-wm)
  • mto: This revision was merged to the branch mainline in revision 2016.
  • Revision ID: qt@tempo-consulting.fr-20140305165114-m3n5p978sisi4n3f
REF-27 [FIX] Fix import of tools.translate

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
        'total_project': True,
40
40
    }
41
41
    
 
42
    def create(self, cr, uid, vals, context=None):
 
43
        result = super(financing_contract_funding_pool_line, self).create(cr, uid, vals, context=context)
 
44
        # Add the corresponding quadruplets (one for each CC in the format and all A/D)
 
45
        if 'contract_id' in vals and 'funding_pool_id' in vals:
 
46
            # Create quadruplets accordingly
 
47
            quad_obj = self.pool.get('financing.contract.account.quadruplet')
 
48
            format_obj = self.pool.get('financing.contract.format')
 
49
            data = format_obj.get_data_for_quadruplets(cr, vals['contract_id'])
 
50
            # for each funding pool, add all quadruplets
 
51
            for cost_center_id in data['cost_center_ids']:
 
52
                for account_destination_id in data['account_destination_ids']:
 
53
                        quad_obj.create(cr, uid,
 
54
                                        {'format_id': vals['contract_id'],
 
55
                                         'account_destination_id': account_destination_id,
 
56
                                         'cost_center_id': cost_center_id,
 
57
                                         'funding_pool_id': vals['funding_pool_id']}, context=context)
 
58
        return result
 
59
        
 
60
    def write(self, cr, uid, ids, vals, context=None):
 
61
        if 'funding_pool_id' in vals:
 
62
            # if the funding pool changes, add/remove accordingly
 
63
            quad_obj = self.pool.get('financing.contract.account.quadruplet')
 
64
            format_obj = self.pool.get('financing.contract.format')
 
65
            for id in ids:
 
66
                funding_pool_line = self.browse(cr, uid, id, context=context)
 
67
                format_id = funding_pool_line.contract_id.id
 
68
                data = format_obj.get_data_for_quadruplets(cr, format_id)
 
69
                old_funding_pool_id = funding_pool_line.funding_pool_id.id
 
70
                new_funding_pool_id = vals['funding_pool_id']
 
71
                
 
72
                quads_to_delete = quad_obj.search(cr, uid, [('funding_pool_id', '=', old_funding_pool_id)], context=context)
 
73
                quad_obj.unlink(cr, uid, quads_to_delete, context=context)
 
74
                # add missing quadruplets
 
75
                for cost_center_id in data['cost_center_ids']:
 
76
                    for account_destination_id in data['account_destination_ids']:
 
77
                        quad_obj.create(cr, uid,
 
78
                                        {'format_id': format_id,
 
79
                                         'account_destination_id': account_destination_id,
 
80
                                         'cost_center_id': cost_center_id,
 
81
                                         'funding_pool_id': new_funding_pool_id}, context=context)
 
82
                
 
83
        return super(financing_contract_funding_pool_line, self).write(cr, uid, ids, vals, context=context)
 
84
    
 
85
    def unlink(self, cr, uid, ids, context=None):
 
86
        # for unlink, simple: remove all lines for those funding pools
 
87
        quad_obj = self.pool.get('financing.contract.account.quadruplet')
 
88
        for funding_pool_line in self.browse(cr, uid, ids, context=context):
 
89
            quads_to_delete = quad_obj.search(cr, uid, [('funding_pool_id', '=', funding_pool_line.funding_pool_id.id)], context=context)
 
90
            quad_obj.unlink(cr, uid, quads_to_delete, context=context)
 
91
                            
 
92
        return super(financing_contract_funding_pool_line, self).unlink(cr, uid, ids, context=context)
 
93
    
42
94
financing_contract_funding_pool_line()
43
95
 
44
96
class financing_contract_contract(osv.osv):
122
174
        account_destination_ids = []
123
175
        if reporting_type is None:
124
176
            reporting_type = browse_contract.reporting_type
125
 
        # general domain
126
 
        general_domain = format_line_obj._get_general_domain(cr,
127
 
                                                             uid,
128
 
                                                             browse_contract.format_id,
129
 
                                                             reporting_type,
130
 
                                                             context=context)
131
177
        
 
178
        analytic_domain = []
132
179
        # parse parent lines (either value or sum of children's values)
133
180
        for line in browse_contract.actual_line_ids:
134
181
            if not line.parent_id:
135
 
                account_destination_ids += format_line_obj._get_account_destination_ids(line, general_domain['funding_pool_account_destination_ids'])
136
 
                
137
 
        # create the domain
138
 
        analytic_domain = []
139
 
        account_domain = format_line_obj._create_account_destination_domain(account_destination_ids)
140
 
        date_domain = eval(general_domain['date_domain'])
141
 
        analytic_domain = [date_domain[0],
142
 
                           date_domain[1],
143
 
                           ('is_reallocated', '=', False),
144
 
                           ('is_reversal', '=', False),
145
 
                           eval(general_domain['funding_pool_domain']),
146
 
                           eval(general_domain['cost_center_domain'])]
147
 
        analytic_domain += account_domain
 
182
                account_destination_ids += format_line_obj._get_analytic_domain(cr, uid, line, reporting_type, context=context)
148
183
            
149
184
        return analytic_domain
150
185
 
165
200
    _columns = {
166
201
        'name': fields.char('Financing contract name', size=64, required=True),
167
202
        'code': fields.char('Financing contract code', size=16, required=True),
168
 
        'donor_id': fields.many2one('financing.contract.donor', 'Donor', required=True),
 
203
        'donor_id': fields.many2one('financing.contract.donor', 'Donor', required=True, domain="[('active', '=', True)]"),
169
204
        'donor_grant_reference': fields.char('Donor grant reference', size=64),
170
205
        'hq_grant_reference': fields.char('HQ grant reference', size=64),
171
206
        'grant_amount': fields.float('Grant amount', required=True),
180
215
                                    ('soft_closed', 'Soft-closed'),
181
216
                                    ('hard_closed', 'Hard-closed')], 'State'),
182
217
        'currency_table_id': fields.many2one('res.currency.table', 'Currency Table'),
 
218
        'instance_id': fields.many2one('msf.instance','Proprietary Instance', required=True), 
183
219
        # Define for _inherits
184
 
        'format_id': fields.many2one('financing.contract.format', 'Format', ondelete="cascade", required=True),
 
220
        'format_id': fields.many2one('financing.contract.format', 'Format', ondelete="cascade"),
185
221
    }
186
222
    
187
223
    _defaults = {
188
224
        'state': 'draft',
189
225
        'reporting_currency': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
190
 
        'format_id': lambda self,cr,uid,context: self.pool.get('financing.contract.format').create(cr, uid, {}, context=context)
191
226
    }
192
227
 
193
228
    def _check_unicity(self, cr, uid, ids, context=None):
210
245
        default = default.copy()
211
246
        default['code'] = (contract['code'] or '') + '(copy)'
212
247
        default['name'] = (contract['name'] or '') + '(copy)'
213
 
        # Copy lines manually
 
248
        # Copy lines manually but remove CCs and FPs
 
249
        default['funding_pool_ids'] = []
 
250
        default['cost_center_ids'] = []
214
251
        default['actual_line_ids'] = []
215
252
        copy_id = super(financing_contract_contract, self).copy(cr, uid, id, default, context=context)
216
253
        copy = self.browse(cr, uid, copy_id, context=context)
219
256
    
220
257
    def onchange_donor_id(self, cr, uid, ids, donor_id, format_id, actual_line_ids, context=None):
221
258
        res = {}
222
 
        if donor_id and format_id:
 
259
        if donor_id:
223
260
            donor = self.pool.get('financing.contract.donor').browse(cr, uid, donor_id, context=context)
224
261
            if donor.format_id:
225
262
                source_format = donor.format_id
229
266
                    'overhead_type': source_format.overhead_type,
230
267
                    'overhead_percentage': source_format.overhead_percentage,
231
268
                }
232
 
                self.pool.get('financing.contract.format').copy_format_lines(cr, uid, donor.format_id.id, format_id, context=context)
233
269
                res = {'value': format_vals}
234
270
        return res
235
271
    
328
364
        # create "real" lines
329
365
        for line in contract.actual_line_ids:
330
366
            if not line.parent_id:
331
 
                allocated_budget += line.allocated_budget
332
 
                project_budget += line.project_budget
333
 
                allocated_real += line.allocated_real
334
 
                project_real += line.project_real
 
367
                # UTP-853: self.create_reporting_line rounds each line
 
368
                # (int value) so we add a round for sums equivalence
 
369
                allocated_budget += round(line.allocated_budget)
 
370
                project_budget += round(line.project_budget)
 
371
                allocated_real += round(line.allocated_real)
 
372
                project_real += round(line.project_real)
335
373
                reporting_line_id = self.create_reporting_line(cr, uid, contract, line, contract_line_id, context=context)
336
374
        
337
375
        # Refresh contract line with general infos
422
460
                'res_id': [wiz_id],
423
461
                'context': context,
424
462
        }
 
463
        
 
464
    def create(self, cr, uid, vals, context=None):
 
465
        # Do not copy lines from the Donor on create if coming from the sync server
 
466
        if context is None:
 
467
            context = {}
 
468
        result = super(financing_contract_contract, self).create(cr, uid, vals, context=context)
 
469
        if not context.get('sync_update_execution'):
 
470
            contract = self.browse(cr, uid, result, context=context)
 
471
            if contract.donor_id and contract.donor_id.format_id and contract.format_id:
 
472
                self.pool.get('financing.contract.format').copy_format_lines(cr, uid, contract.donor_id.format_id.id, contract.format_id.id, context=context)
 
473
        return result
425
474
    
 
475
        
 
476
    def write(self, cr, uid, ids, vals, context=None):
 
477
        if 'donor_id' in vals:
 
478
            donor = self.pool.get('financing.contract.donor').browse(cr, uid, vals['donor_id'], context=context)
 
479
            for contract in self.browse(cr, uid, ids, context=context):
 
480
                if contract.donor_id and contract.format_id and vals['donor_id'] != contract.donor_id.id:
 
481
                    self.pool.get('financing.contract.format').copy_format_lines(cr, uid, donor.format_id.id, contract.format_id.id, context=context)
 
482
 
 
483
        return super(financing_contract_contract, self).write(cr, uid, ids, vals, context=context)
 
484
 
426
485
financing_contract_contract()
427
486
 
428
487
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: