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

« back to all changes in this revision

Viewing changes to financing_contract/format.py

  • Committer: Quentin THEURET
  • Date: 2011-12-12 08:02:59 UTC
  • mto: This revision was merged to the branch mainline in revision 724.
  • Revision ID: qt@tempo-consulting.fr-20111212080259-oul1f0g37hcpubyc
UF-641 [ADD] Added the empty purchase_followup module

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    
28
28
    _columns = {
29
29
        'format_name': fields.char('Name', size=64, required=True),
30
 
        'reporting_type': fields.selection([('project','Total project only'),
31
 
                                            ('allocated','Earmarked only'),
32
 
                                            ('all', 'Earmarked and total project')], 'Reporting type', required=True),
33
 
        # For contract only, but needed for line domain;
34
 
        # we need to keep them available
35
 
        'overhead_percentage': fields.float('Overhead percentage'),
36
 
        'overhead_type': fields.selection([('cost_percentage','Percentage of direct costs'),
37
 
                                           ('grant_percentage','Percentage of grant')], 'Overhead calculation mode'),
38
 
        'eligibility_from_date': fields.date('Eligibility date from'),
39
 
        'eligibility_to_date': fields.date('Eligibility date to'),
40
 
        'funding_pool_ids': fields.one2many('financing.contract.funding.pool.line', 'contract_id', 'Funding Pools'),
41
 
        'cost_center_ids': fields.many2many('account.analytic.account', 'financing_contract_cost_center', 'contract_id', 'cost_center_id', string='Cost Centers'),
 
30
        'reporting_type': fields.selection([('project','Total project costs'),
 
31
                                            ('allocated','Funded costs'),
 
32
                                            ('all', 'Total project and funded costs')], 'Reporting type', required=True),
 
33
        'overhead_type': fields.selection([('cost_percentage','Percentage of total costs'),
 
34
                                           ('grant_percentage','Percentage of direct costs'),
 
35
                                           ('amount', 'Lump sum')], 'Overhead type', required=True),
 
36
        'overhead_percentage': fields.float('Percentage overhead'),
 
37
        'budget_allocated_overhead': fields.float('Funded overhead - Budget'),
 
38
        'budget_project_overhead': fields.float('Total project overhead - Budget'),
 
39
        'allocated_overhead': fields.float('Funded overhead - Actuals'),
 
40
        'project_overhead': fields.float('Total project overhead - Actuals'),
 
41
        'budget_allocated_lump_sum': fields.float('Funded lump sum - Budget'),
 
42
        'budget_project_lump_sum': fields.float('Total project lump sum - Budget'),
 
43
        'allocated_lump_sum': fields.float('Funded lump sum - Actuals'),
 
44
        'project_lump_sum': fields.float('Total project lump sum - Actuals'),
 
45
        'budget_allocated_consumption': fields.float('Funded consumption - Budget'),
 
46
        'budget_project_consumption': fields.float('Total project consumption - Budget'),
 
47
        'allocated_consumption': fields.float('Funded consumption - Actuals'),
 
48
        'project_consumption': fields.float('Total project consumption - Actuals'),
42
49
    }
43
50
    
44
51
    _defaults = {
45
52
        'format_name': 'Format',
46
53
        'reporting_type': 'all',
47
54
        'overhead_type': 'cost_percentage',
 
55
        'overhead_percentage': 0.0,
 
56
        'budget_allocated_overhead': 0.0,
 
57
        'budget_project_overhead': 0.0,
 
58
        'allocated_overhead': 0.0,
 
59
        'project_overhead': 0.0,
 
60
        'budget_allocated_lump_sum': 0.0,
 
61
        'budget_project_lump_sum': 0.0,
 
62
        'allocated_lump_sum': 0.0,
 
63
        'project_lump_sum': 0.0,
 
64
        'budget_allocated_consumption': 0.0,
 
65
        'budget_project_consumption': 0.0,
 
66
        'allocated_consumption': 0.0,
 
67
        'project_consumption': 0.0,
48
68
    }
49
69
    
50
70
    def name_get(self, cr, uid, ids, context=None):
54
74
            format_name = rs.format_name
55
75
            res += [(rs.id, format_name)]
56
76
        return res
57
 
 
58
 
    _sql_constraints = [
59
 
        ('date_overlap', 'check(eligibility_from_date < eligibility_to_date)', 'The "Eligibility Date From" should be sooner than the "Eligibility Date To".'),
60
 
    ]
61
 
    
62
 
    def get_data_for_quadruplets(self, cr, format_id):
63
 
        # Get all existing account/destination links
64
 
        cr.execute('''select id from account_destination_link''')
65
 
        account_destination_ids = [x[0] for x in cr.fetchall()]
66
 
        # Get funding pools
67
 
        cr.execute('''select distinct funding_pool_id 
68
 
                      from financing_contract_funding_pool_line
69
 
                      where contract_id = %s ''' % (format_id))
70
 
        funding_pool_ids = [x[0] for x in cr.fetchall()]
71
 
        # Get cost centers
72
 
        cr.execute('''select distinct cost_center_id
73
 
                      from financing_contract_cost_center
74
 
                      where contract_id = %s ''' % (format_id))
75
 
        cost_center_ids = [x[0] for x in cr.fetchall()]
76
 
        
77
 
        return {'account_destination_ids': account_destination_ids,
78
 
                'funding_pool_ids': funding_pool_ids,
79
 
                'cost_center_ids': cost_center_ids}
80
 
        
81
 
    def create(self, cr, uid, vals, context=None):
82
 
        result = super(financing_contract_format, self).create(cr, uid, vals, context=context)
83
 
        if 'cost_center_ids' in vals or 'funding_pool_ids' in vals:
84
 
            # Create quadruplets accordingly
85
 
            data = self.get_data_for_quadruplets(cr, result)
86
 
            quad_obj = self.pool.get('financing.contract.account.quadruplet')
87
 
            # for each funding pool, add all quadruplets
88
 
            for funding_pool_id in data['funding_pool_ids']:
89
 
                for cost_center_id in data['cost_center_ids']:
90
 
                    for account_destination_id in data['account_destination_ids']:
91
 
                        quad_obj.create(cr, uid,
92
 
                                        {'format_id': result,
93
 
                                         'account_destination_id': account_destination_id,
94
 
                                         'cost_center_id': cost_center_id,
95
 
                                         'funding_pool_id': funding_pool_id}, context=context)
96
 
        return result
97
 
        
98
 
    def write(self, cr, uid, ids, vals, context=None):
99
 
        # Only for CC; FPs and Accts/Dests are edited in their objects
100
 
        if 'cost_center_ids' in vals and len(vals['cost_center_ids']) > 0:
101
 
            quad_obj = self.pool.get('financing.contract.account.quadruplet')
102
 
            
103
 
            # Compare "before" and "after" in order to delete/create quadruplets
104
 
            for id in ids:
105
 
                data = self.get_data_for_quadruplets(cr, id)
106
 
                old_cost_centers = data['cost_center_ids']
107
 
                new_cost_centers = vals['cost_center_ids'][0][2]
108
 
            
109
 
                # create "diffs" for CC and FP
110
 
                cc_to_add = [cc_id for cc_id in new_cost_centers if cc_id not in old_cost_centers]
111
 
                cc_to_remove = [cc_id for cc_id in old_cost_centers if cc_id not in new_cost_centers]
112
 
                # remove quadruplets accordingly
113
 
                
114
 
                quads_to_delete = quad_obj.search(cr, uid, [('cost_center_id', 'in', cc_to_remove)], context=context)
115
 
                quad_obj.unlink(cr, uid, quads_to_delete, context=context)
116
 
                # add missing cost center's quadruplets
117
 
                for funding_pool_id in data['funding_pool_ids']:
118
 
                    for cost_center_id in cc_to_add:
119
 
                        for account_destination_id in data['account_destination_ids']:
120
 
                            quad_obj.create(cr, uid,
121
 
                                            {'format_id': id,
122
 
                                             'account_destination_id': account_destination_id,
123
 
                                             'cost_center_id': cost_center_id,
124
 
                                             'funding_pool_id': funding_pool_id}, context=context)
125
 
                            
126
 
        return super(financing_contract_format, self).write(cr, uid, ids, vals, context=context)
127
 
    
128
 
    def unlink(self, cr, uid, ids, context=None):
129
 
        # for unlink, simple: remove all lines for that format
130
 
        quad_obj = self.pool.get('financing.contract.account.quadruplet')
131
 
        quads_to_delete = quad_obj.search(cr, uid, [('format_id', 'in', ids)], context=context)
132
 
        quad_obj.unlink(cr, uid, quads_to_delete, context=context)
133
 
                            
134
 
        return super(financing_contract_format, self).unlink(cr, uid, ids, context=context)
135
 
 
 
77
    
136
78
financing_contract_format()
137
79
 
138
 
class account_destination_link(osv.osv):
139
 
    _name = 'account.destination.link'
140
 
    _inherit = 'account.destination.link'
 
80
class financing_contract_actual_line(osv.osv):
 
81
    
 
82
    _name = "financing.contract.actual.line"
141
83
 
142
 
    def _get_used_in_contract(self, cr, uid, ids, field_name, arg, context=None):
 
84
    def _get_number_of_childs(self, cr, uid, ids, field_name=None, arg=None, context={}):
 
85
        # Verifications
 
86
        if not context:
 
87
            context = {}
 
88
        if isinstance(ids, (int, long)):
 
89
            ids = [ids]
 
90
        # Prepare some values
143
91
        res = {}
144
 
        if context is None:
145
 
            context = {}
146
 
        exclude = {}
147
 
 
148
 
        if not context.get('contract_id') and not context.get('donor_id'):
149
 
            for id in ids:
150
 
                res[id] = False
151
 
            return res
152
 
 
153
 
        if context.get('contract_id'):
154
 
            ctr_obj = self.pool.get('financing.contract.contract')
155
 
            id_toread = context['contract_id']
156
 
        elif context.get('donor_id'):
157
 
            ctr_obj = self.pool.get('financing.contract.donor')
158
 
            id_toread = context['donor_id']
159
 
 
160
 
        exclude = {}
161
 
        for line in ctr_obj.browse(cr, uid, id_toread).actual_line_ids:
162
 
            if not context.get('active_id', False) or line.id != context['active_id']:
163
 
                for account_destination in line.account_destination_ids:
164
 
                    exclude[account_destination.id] = True
165
 
                for account_quadruplet in line.account_quadruplet_ids:
166
 
                    exclude[account_quadruplet.account_destination_id.id] = True
167
 
        for id in ids:
168
 
            res[id] = id in exclude
169
 
        return res
170
 
 
171
 
    def _search_used_in_contract(self, cr, uid, obj, name, args, context=None):
172
 
        if not args:
173
 
            return []
174
 
        if context is None:
175
 
            context = {}
176
 
        assert args[0][1] == '=' and args[0][2], 'Filter not implemented'
177
 
        if not context.get('contract_id') and not context.get('donor_id'):
178
 
            return []
179
 
 
180
 
        if context.get('contract_id'):
181
 
            ctr_obj = self.pool.get('financing.contract.contract')
182
 
            id_toread = context['contract_id']
183
 
        elif context.get('donor_id'):
184
 
            ctr_obj = self.pool.get('financing.contract.donor')
185
 
            id_toread = context['donor_id']
186
 
 
187
 
        exclude = {}
188
 
        for line in ctr_obj.browse(cr, uid, id_toread).actual_line_ids:
189
 
            if not context.get('active_id', False) or line.id != context['active_id']:
190
 
                for account_destination in line.account_destination_ids:
191
 
                    exclude[account_destination.id] = True
192
 
                for account_quadruplet in line.account_quadruplet_ids:
193
 
                    exclude[account_quadruplet.account_destination_id.id] = True
194
 
 
195
 
        return [('id', 'not in', exclude.keys())]
196
 
 
 
92
        for line in self.browse(cr, uid, ids, context=context):
 
93
            res[line.id] = line.child_ids and len(line.child_ids) or 0
 
94
        return res
 
95
    
 
96
    def _get_parent_ids(self, cr, uid, ids, context=None):
 
97
        res = []
 
98
        for line in self.browse(cr, uid, ids, context=context):
 
99
            if line.parent_id:
 
100
                res.append(line.parent_id.id)
 
101
        return res
 
102
    
197
103
    _columns = {
198
 
        'used_in_contract': fields.function(_get_used_in_contract, method=True, type='boolean', string='Used', fnct_search=_search_used_in_contract),
199
 
    }
200
 
 
201
 
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
202
 
        if context is None:
203
 
            context = {}
204
 
        if view_type == 'tree' and (context.get('contract_id') or context.get('donor_id')) :
205
 
            view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'financing_contract', 'view_account_destination_link_for_contract_tree')[1]
206
 
        return super(account_destination_link, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
 
104
        'name': fields.char('Name', size=64, required=True),
 
105
        'code': fields.char('Code', size=16, required=True),
 
106
        'format_id': fields.many2one('financing.contract.format', 'Format'),
 
107
        'account_ids': fields.many2many('account.account', 'financing_contract_actual_accounts', 'actual_line_id', 'account_id', string='Accounts'),
 
108
        'parent_id': fields.many2one('financing.contract.actual.line', 'Parent line', ondelete='cascade'),
 
109
        'child_ids': fields.one2many('financing.contract.actual.line', 'parent_id', 'Child lines'),
 
110
        'line_type': fields.selection([('view','View'),
 
111
                                       ('normal','Normal')], 'Line type', required=True),
 
112
        'allocated_amount': fields.float('Funded amount - Budget'),
 
113
        'project_amount': fields.float('Total project amount - Budget'),
 
114
    }
 
115
    
 
116
    _defaults = {
 
117
        'line_type': 'normal',
 
118
    }
207
119
    
208
120
    def create(self, cr, uid, vals, context=None):
209
 
        result = super(account_destination_link, self).create(cr, uid, vals, context=context)
210
 
        # Add quadruplets for each format/CC/FP combination
211
 
        quad_obj = self.pool.get('financing.contract.account.quadruplet')
212
 
        format_obj = self.pool.get('financing.contract.format')
213
 
        # get all formats
214
 
        cr.execute('''select id from financing_contract_format''')
215
 
        format_ids = [x[0] for x in cr.fetchall()]
216
 
        for format_id in format_ids:
217
 
            data = format_obj.get_data_for_quadruplets(cr, format_id)
218
 
            # for each funding pool, add all quadruplets
219
 
            for funding_pool_id in data['funding_pool_ids']:
220
 
                for cost_center_id in data['cost_center_ids']:
221
 
                    quad_obj.create(cr, uid,
222
 
                                    {'format_id': format_id,
223
 
                                     'account_destination_id': result,
224
 
                                     'cost_center_id': cost_center_id,
225
 
                                     'funding_pool_id': funding_pool_id}, context=context)
226
 
        return result
 
121
        if not context:
 
122
            context={}
 
123
        # if the account is set as view, remove budget and account values
 
124
        if 'line_type' in vals and vals['line_type'] == 'view':
 
125
            vals['allocated_amount'] = 0.0
 
126
            vals['project_amount'] = 0.0
 
127
            vals['account_ids'] = []
 
128
        return super(financing_contract_actual_line, self).create(cr, uid, vals, context=context)
 
129
    
 
130
    def write(self, cr, uid, ids, vals, context=None):
 
131
        if not context:
 
132
            context={}
 
133
        if isinstance(ids, (int, long)):
 
134
            ids = [ids]
 
135
        # if the account is set as view, remove budget and account values
 
136
        if 'line_type' in vals and vals['line_type'] == 'view':
 
137
            vals['allocated_amount'] = 0.0
 
138
            vals['project_amount'] = 0.0
 
139
            vals['account_ids'] = [(6, 0, [])]
 
140
        return super(financing_contract_actual_line, self).write(cr, uid, ids, vals, context=context)
 
141
    
 
142
    def copy_format_line(self, cr, uid, browse_source_line, destination_format_id, parent_id=None, context=None):
 
143
        if destination_format_id:
 
144
            format_line_vals = {
 
145
                'name': browse_source_line.name,
 
146
                'code': browse_source_line.code,
 
147
                'format_id': destination_format_id,
 
148
                'parent_id': parent_id,
 
149
                'line_type': browse_source_line.line_type,
 
150
                'allocated_amount': browse_source_line.allocated_amount,
 
151
                'project_amount': browse_source_line.project_amount,
 
152
            }
 
153
            account_ids = []
 
154
            for account in browse_source_line.account_ids:
 
155
                account_ids.append(account.id)
 
156
            format_line_vals['account_ids'] = [(6, 0, account_ids)]
 
157
            parent_line_id = self.pool.get('financing.contract.actual.line').create(cr, uid, format_line_vals, context=context)
 
158
            for child_line in browse_source_line.child_ids:
 
159
                self.copy_format_line(cr, uid, child_line, destination_format_id, parent_line_id, context=context)
 
160
        return
 
161
            
 
162
financing_contract_actual_line()
 
163
 
 
164
class financing_contract_format(osv.osv):
 
165
    
 
166
    _name = "financing.contract.format"
 
167
    _inherit = "financing.contract.format"
 
168
    
 
169
    _columns = {
 
170
        'actual_line_ids': fields.one2many('financing.contract.actual.line', 'format_id', 'Actual lines'),
 
171
    }
 
172
    
 
173
    def copy_format_lines(self, cr, uid, source_id, destination_id, context=None):
 
174
        # remove all old report lines
 
175
        destination_obj = self.browse(cr, uid, destination_id, context=context)
 
176
        for to_remove_line in destination_obj.actual_line_ids:
 
177
            self.pool.get('financing.contract.actual.line').unlink(cr, uid, to_remove_line.id, context=context)
 
178
        source_obj = self.browse(cr, uid, source_id, context=context)
 
179
        # Method to copy a format
 
180
        # copy format lines
 
181
        for source_line in source_obj.actual_line_ids:
 
182
            if not source_line.parent_id:
 
183
                self.pool.get('financing.contract.actual.line').copy_format_line(cr,
 
184
                                                                                 uid,
 
185
                                                                                 source_line,
 
186
                                                                                 destination_id,
 
187
                                                                                 parent_id=None,
 
188
                                                                                 context=context)
 
189
        return
227
190
        
228
 
    def write(self, cr, uid, ids, vals, context=None):
229
 
        # Nothing to be done, since the id does not change
230
 
        return super(account_destination_link, self).write(cr, uid, ids, vals, context=context)
231
 
    
232
 
    def unlink(self, cr, uid, ids, context=None):
233
 
        # for unlink, simple: remove all lines for that account/destination
234
 
        quad_obj = self.pool.get('financing.contract.account.quadruplet')
235
 
        quads_to_delete = quad_obj.search(cr, uid, [('account_destination_id', 'in', ids)], context=context)
236
 
        quad_obj.unlink(cr, uid, quads_to_delete, context=context)
237
 
                            
238
 
        return super(account_destination_link, self).unlink(cr, uid, ids, context=context)
 
191
financing_contract_format()
239
192
 
240
 
account_destination_link()
241
193
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: