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"
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'),
38
'total_project': True,
41
financing_contract_funding_pool_line()
43
class financing_contract_contract(osv.osv):
45
_name = "financing.contract.contract"
46
_inherits = {"financing.contract.format": "format_id"}
48
def contract_open(self, cr, uid, ids, *args):
49
self.write(cr, uid, ids, {
51
'open_date': datetime.date.today().strftime('%Y-%m-%d'),
52
'soft_closed_date': None
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')
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')
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')
76
general_domain = format_line_obj._get_general_domain(cr,
78
browse_contract.format_id,
79
browse_contract.reporting_type,
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'])
88
if reporting_type is None:
89
reporting_type = browse_contract.reporting_type
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],
97
eval(general_domain['funding_pool_domain'])]
99
analytic_domain = [date_domain[0],
101
eval(account_domain),
102
eval(general_domain['funding_pool_domain']),
103
eval(general_domain['cost_center_domain'])]
105
return analytic_domain
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'),
122
('soft_closed', 'Soft-closed'),
123
('hard_closed', 'Hard-closed')], 'State'),
124
'currency_table_id': fields.many2one('res.currency.table', 'Currency Table'),
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)
133
def onchange_donor_id(self, cr, uid, ids, donor_id, format_id, actual_line_ids, context={}):
135
if donor_id and format_id:
136
donor = self.pool.get('financing.contract.donor').browse(cr, uid, donor_id, context=context)
138
source_format = donor.format_id
140
'format_name': source_format.format_name,
141
'reporting_type': source_format.reporting_type,
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}
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)
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}
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,
173
browse_contract.reporting_type,
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,
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
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,
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,
205
vals = {'name': contract.name,
206
'code': contract.code},
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)
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)
233
# retrieve the corresponding_view
234
model_data_obj = self.pool.get('ir.model.data')
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))],
240
if len(view_ids) > 0:
241
view_id = model_data_obj.browse(cr, uid, view_ids[0]).res_id
243
'type': 'ir.actions.act_window',
244
'res_model': 'financing.contract.donor.reporting.line',
246
'view_id': [view_id],
248
'domain': [('id', '=', contract_line_id)],
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)
259
'type': 'ir.actions.act_window',
260
'res_model': 'wizard.expense.report',
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)
275
'type': 'ir.actions.act_window',
276
'res_model': 'wizard.expense.report',
284
financing_contract_contract()
286
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: