1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 MSF, TeMPO Consulting.
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.
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.
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/>.
20
##############################################################################
22
from osv import fields, osv
25
class financing_contract_funding_pool_line(osv.osv):
27
_name = "financing.contract.funding.pool.line"
28
_description = "Funding pool line"
31
'contract_id': fields.many2one('financing.contract.format', 'Contract', required=True),
32
'funding_pool_id': fields.many2one('account.analytic.account', 'Funding pool name', required=True),
33
'funded': fields.boolean('Earmarked'),
34
'total_project': fields.boolean('Total project'),
39
'total_project': True,
42
financing_contract_funding_pool_line()
44
class financing_contract_contract(osv.osv):
46
_name = "financing.contract.contract"
47
_inherits = {"financing.contract.format": "format_id"}
50
def contract_open(self, cr, uid, ids, *args):
51
self.write(cr, uid, ids, {
53
'open_date': datetime.date.today().strftime('%Y-%m-%d'),
54
'soft_closed_date': None
58
def contract_soft_closed(self, cr, uid, ids, *args):
59
self.write(cr, uid, ids, {
60
'state': 'soft_closed',
61
'soft_closed_date': datetime.date.today().strftime('%Y-%m-%d')
65
def contract_hard_closed(self, cr, uid, ids, *args):
66
self.write(cr, uid, ids, {
67
'state': 'hard_closed',
68
'hard_closed_date': datetime.date.today().strftime('%Y-%m-%d')
72
def get_contract_domain(self, cr, uid, browse_contract, reporting_type=None, context=None):
73
# we update the context with the contract reporting type and currency
74
format_line_obj = self.pool.get('financing.contract.format.line')
76
account_destination_ids = []
77
if reporting_type is None:
78
reporting_type = browse_contract.reporting_type
80
general_domain = format_line_obj._get_general_domain(cr,
82
browse_contract.format_id,
86
# parse parent lines (either value or sum of children's values)
87
for line in browse_contract.actual_line_ids:
88
if not line.parent_id:
89
account_destination_ids += format_line_obj._get_account_destination_ids(line, general_domain['funding_pool_account_destination_ids'])
93
account_domain = format_line_obj._create_account_destination_domain(account_destination_ids)
94
date_domain = eval(general_domain['date_domain'])
95
if reporting_type == 'allocated':
96
analytic_domain = [date_domain[0],
98
eval(general_domain['funding_pool_domain'])]
100
analytic_domain = [date_domain[0],
102
eval(general_domain['funding_pool_domain']),
103
eval(general_domain['cost_center_domain'])]
104
analytic_domain += account_domain
106
return analytic_domain
108
def _get_overhead_amount(self, cr, uid, ids, field_name=None, arg=None, context=None):
110
Method to compute the overhead amount
113
for budget in self.browse(cr, uid, ids, context=context):
116
if budget.overhead_type == 'cost_percentage':
117
res[budget.id] = round(budget.grant_amount * budget.overhead_percentage / (100.0 + budget.overhead_percentage))
118
elif budget.overhead_type == 'grant_percentage':
119
res[budget.id] = round(budget.grant_amount * budget.overhead_percentage / 100.0)
123
'name': fields.char('Financing contract name', size=64, required=True),
124
'code': fields.char('Financing contract code', size=16, required=True),
125
'donor_id': fields.many2one('financing.contract.donor', 'Donor', required=True),
126
'donor_grant_reference': fields.char('Donor grant reference', size=64),
127
'hq_grant_reference': fields.char('HQ grant reference', size=64),
128
'grant_amount': fields.float('Grant amount', required=True),
129
'overhead_amount': fields.function(_get_overhead_amount, method=True, store=False, string="Overhead amount", type="float", readonly=True),
130
'reporting_currency': fields.many2one('res.currency', 'Reporting currency', required=True),
131
'notes': fields.text('Notes'),
132
'open_date': fields.date('Open date'),
133
'soft_closed_date': fields.date('Soft-closed date'),
134
'hard_closed_date': fields.date('Hard-closed date'),
135
'state': fields.selection([('draft','Draft'),
137
('soft_closed', 'Soft-closed'),
138
('hard_closed', 'Hard-closed')], 'State'),
139
'currency_table_id': fields.many2one('res.currency.table', 'Currency Table'),
140
# Define for _inherits
141
'format_id': fields.many2one('financing.contract.format', 'Format', ondelete="cascade", required=True),
146
'reporting_currency': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
147
'format_id': lambda self,cr,uid,context: self.pool.get('financing.contract.format').create(cr, uid, {}, context=context)
150
def _check_unicity(self, cr, uid, ids, context=None):
153
for contract in self.browse(cr, uid, ids, context=context):
154
bad_ids = self.search(cr, uid, [('|'),('name', '=ilike', contract.name),('code', '=ilike', contract.code)])
155
if len(bad_ids) and len(bad_ids) > 1:
160
(_check_unicity, 'You cannot have the same code or name between contracts!', ['code', 'name']),
163
def copy(self, cr, uid, id, default=None, context=None, done_list=[], local=False):
164
contract = self.browse(cr, uid, id, context=context)
167
default = default.copy()
168
default['code'] = (contract['code'] or '') + '(copy)'
169
default['name'] = (contract['name'] or '') + '(copy)'
170
# Copy lines manually
171
default['actual_line_ids'] = []
172
copy_id = super(financing_contract_contract, self).copy(cr, uid, id, default, context=context)
173
copy = self.browse(cr, uid, copy_id, context=context)
174
self.pool.get('financing.contract.format').copy_format_lines(cr, uid, contract.format_id.id, copy.format_id.id, context=context)
177
def onchange_donor_id(self, cr, uid, ids, donor_id, format_id, actual_line_ids, context=None):
179
if donor_id and format_id:
180
donor = self.pool.get('financing.contract.donor').browse(cr, uid, donor_id, context=context)
182
source_format = donor.format_id
184
'format_name': source_format.format_name,
185
'reporting_type': source_format.reporting_type,
186
'overhead_type': source_format.overhead_type,
187
'overhead_percentage': source_format.overhead_percentage,
189
self.pool.get('financing.contract.format').copy_format_lines(cr, uid, donor.format_id.id, format_id, context=context)
190
return {'value': format_vals}
192
def onchange_currency_table(self, cr, uid, ids, currency_table_id, reporting_currency_id, context=None):
193
values = {'reporting_currency': False}
194
if reporting_currency_id:
195
# it can be a currency from another table
196
reporting_currency = self.pool.get('res.currency').browse(cr, uid, reporting_currency_id, context=context)
197
# Search if the currency is in the table, and active
198
if reporting_currency.reference_currency_id:
199
currency_results = self.pool.get('res.currency').search(cr, uid, [('reference_currency_id', '=', reporting_currency.reference_currency_id.id),
200
('currency_table_id', '=', currency_table_id),
201
('active', '=', True)], context=context)
203
currency_results = self.pool.get('res.currency').search(cr, uid, [('reference_currency_id', '=', reporting_currency_id),
204
('currency_table_id', '=', currency_table_id),
205
('active', '=', True)], context=context)
206
if len(currency_results) > 0:
207
# it's here, we keep the currency
208
values['reporting_currency'] = reporting_currency_id
209
# Restrain domain to selected table (or None if none selected
210
domains = {'reporting_currency': [('currency_table_id', '=', currency_table_id)]}
211
return {'value': values, 'domain': domains}
213
def create_reporting_line(self, cr, uid, browse_contract, browse_format_line, parent_report_line_id=None, context=None):
214
format_line_obj = self.pool.get('financing.contract.format.line')
215
reporting_line_obj = self.pool.get('financing.contract.donor.reporting.line')
216
analytic_domain = format_line_obj._get_analytic_domain(cr,
219
browse_contract.reporting_type,
221
vals = {'name': browse_format_line.name,
222
'code': browse_format_line.code,
223
'line_type': browse_format_line.line_type,
224
'allocated_budget': round(browse_format_line.allocated_budget),
225
'project_budget': round(browse_format_line.project_budget),
226
'allocated_real': round(browse_format_line.allocated_real),
227
'project_real': round(browse_format_line.project_real),
228
'analytic_domain': analytic_domain,
229
'parent_id': parent_report_line_id}
230
reporting_line_id = reporting_line_obj.create(cr,
235
for child_line in browse_format_line.child_ids:
236
self.create_reporting_line(cr, uid, browse_contract, child_line, reporting_line_id, context=context)
237
return reporting_line_id
239
def menu_interactive_report(self, cr, uid, ids, context=None):
242
# we update the context with the contract reporting type
243
contract = self.browse(cr, uid, ids[0], context=context)
244
context.update({'reporting_currency': contract.reporting_currency.id,
245
'reporting_type': contract.reporting_type,
246
'currency_table_id': contract.currency_table_id.id,
250
## INFO: display_fp in context permits to display Funding Pool column and its attached cost center.
251
reporting_line_obj = self.pool.get('financing.contract.donor.reporting.line')
252
# Create reporting lines
253
# Contract line first (we'll fill it later)
254
contract_line_id = reporting_line_obj.create(cr,
256
vals = {'name': contract.name,
257
'code': contract.code,
258
'line_type': 'view'},
267
# create "real" lines
268
for line in contract.actual_line_ids:
269
if not line.parent_id:
270
allocated_budget += line.allocated_budget
271
project_budget += line.project_budget
272
allocated_real += line.allocated_real
273
project_real += line.project_real
274
reporting_line_id = self.create_reporting_line(cr, uid, contract, line, contract_line_id, context=context)
276
# Refresh contract line with general infos
277
analytic_domain = self.get_contract_domain(cr, uid, contract, context=context)
278
contract_values = {'allocated_budget': allocated_budget,
279
'project_budget': project_budget,
280
'allocated_real': allocated_real,
281
'project_real': project_real,
282
'analytic_domain': analytic_domain}
283
reporting_line_obj.write(cr, uid, [contract_line_id], vals=contract_values, context=context)
285
# retrieve the corresponding_view
286
model_data_obj = self.pool.get('ir.model.data')
288
view_ids = model_data_obj.search(cr, uid,
289
[('module', '=', 'financing_contract'),
290
('name', '=', 'view_donor_reporting_line_tree_%s' % str(contract.reporting_type))],
292
if len(view_ids) > 0:
293
view_id = model_data_obj.browse(cr, uid, view_ids[0]).res_id
295
'type': 'ir.actions.act_window',
296
'res_model': 'financing.contract.donor.reporting.line',
298
'view_id': [view_id],
300
'domain': [('id', '=', contract_line_id)],
304
def menu_allocated_expense_report(self, cr, uid, ids, context=None):
307
wiz_obj = self.pool.get('wizard.expense.report')
308
wiz_id = wiz_obj.create(cr, uid, {'reporting_type': 'allocated',
309
'filename': 'allocated_expenses.csv',
310
'contract_id': ids[0]}, context=context)
313
'type': 'ir.actions.act_window',
314
'res_model': 'wizard.expense.report',
322
def menu_project_expense_report(self, cr, uid, ids, context=None):
325
wiz_obj = self.pool.get('wizard.expense.report')
326
wiz_id = wiz_obj.create(cr, uid, {'reporting_type': 'project',
327
'filename': 'project_expenses.csv',
328
'contract_id': ids[0]}, context=context)
331
'type': 'ir.actions.act_window',
332
'res_model': 'wizard.expense.report',
340
def menu_csv_interactive_report(self, cr, uid, ids, context=None):
343
wiz_obj = self.pool.get('wizard.interactive.report')
344
wiz_id = wiz_obj.create(cr, uid, {'filename': 'interactive_report.csv',
345
'contract_id': ids[0]}, context=context)
348
'type': 'ir.actions.act_window',
349
'res_model': 'wizard.interactive.report',
357
financing_contract_contract()
359
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: