1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 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
23
import decimal_precision as dp
24
from tools.misc import flatten
25
from time import strftime
27
class analytic_distribution(osv.osv):
28
_name = "analytic.distribution"
31
'name': fields.char('Name', size=12),
32
'global_distribution': fields.boolean('Is this distribution copied from the global distribution'),
33
'analytic_lines': fields.one2many('account.analytic.line', 'distribution_id', 'Analytic Lines'),
34
'invoice_ids': fields.one2many('account.invoice', 'analytic_distribution_id', string="Invoices"),
35
'invoice_line_ids': fields.one2many('account.invoice.line', 'analytic_distribution_id', string="Invoice Lines"),
36
'register_line_ids': fields.one2many('account.bank.statement.line', 'analytic_distribution_id', string="Register Lines"),
37
'move_line_ids': fields.one2many('account.move.line', 'analytic_distribution_id', string="Move Lines"),
41
'name': lambda *a: 'Distribution',
42
'global_distribution': lambda *a: False,
45
def copy(self, cr, uid, id, defaults={}, context={}):
47
Copy an analytic distribution without the one2many links
50
'analytic_lines': False,
52
'invoice_line_ids': False,
53
'register_line_ids': False,
54
'move_line_ids': False,
56
return super(osv.osv, self).copy(cr, uid, id, defaults, context=context)
58
analytic_distribution()
60
class distribution_line(osv.osv):
61
_name = "distribution.line"
64
'name': fields.char('Name', size=64),
65
"distribution_id": fields.many2one('analytic.distribution', 'Associated Analytic Distribution', ondelete='cascade'),
66
"analytic_id": fields.many2one('account.analytic.account', 'Analytical Account'),
67
"amount": fields.float('Amount'),
68
"percentage": fields.float('Percentage'),
69
"currency_id": fields.many2one('res.currency', 'Currency', required=True),
70
"date": fields.date(string="Date"),
71
"source_date": fields.date(string="Source Date", help="This date is for source_date for analytic lines"),
75
'name': 'Distribution Line',
76
'date': lambda *a: strftime('%Y-%m-%d'),
77
'source_date': lambda *a: strftime('%Y-%m-%d'),
82
class cost_center_distribution_line(osv.osv):
83
_name = "cost.center.distribution.line"
84
_inherit = "distribution.line"
86
cost_center_distribution_line()
88
class funding_pool_distribution_line(osv.osv):
89
_name = "funding.pool.distribution.line"
90
_inherit = "distribution.line"
92
"cost_center_id": fields.many2one('account.analytic.account', 'Cost Center Account'),
95
funding_pool_distribution_line()
97
class free_1_distribution_line(osv.osv):
98
_name = "free.1.distribution.line"
99
_inherit = "distribution.line"
101
free_1_distribution_line()
103
class free_2_distribution_line(osv.osv):
104
_name = "free.2.distribution.line"
105
_inherit = "distribution.line"
107
free_2_distribution_line()
109
class analytic_distribution(osv.osv):
110
_inherit = "analytic.distribution"
113
'cost_center_lines': fields.one2many('cost.center.distribution.line', 'distribution_id', 'Cost Center Distribution'),
114
'funding_pool_lines': fields.one2many('funding.pool.distribution.line', 'distribution_id', 'Funding Pool Distribution'),
115
'free_1_lines': fields.one2many('free.1.distribution.line', 'distribution_id', 'Free 1 Distribution'),
116
'free_2_lines': fields.one2many('free.2.distribution.line', 'distribution_id', 'Free 2 Distribution'),
119
def copy_from_global_distribution(self, cr, uid, source_id, destination_id, destination_amount, destination_currency, context={}):
120
cc_distrib_line_obj = self.pool.get('cost.center.distribution.line')
121
fp_distrib_line_obj = self.pool.get('funding.pool.distribution.line')
122
f1_distrib_line_obj = self.pool.get('free.1.distribution.line')
123
f2_distrib_line_obj = self.pool.get('free.2.distribution.line')
124
source_obj = self.browse(cr, uid, source_id, context=context)
125
destination_obj = self.browse(cr, uid, destination_id, context=context)
127
for cost_center_line in destination_obj.cost_center_lines:
128
cc_distrib_line_obj.unlink(cr, uid, cost_center_line.id)
129
for funding_pool_line in destination_obj.funding_pool_lines:
130
fp_distrib_line_obj.unlink(cr, uid, funding_pool_line.id)
131
for free_1_line in destination_obj.free_1_lines:
132
f1_distrib_line_obj.unlink(cr, uid, free_1_line.id)
133
for free_2_line in destination_obj.free_2_lines:
134
f2_distrib_line_obj.unlink(cr, uid, free_2_line.id)
137
vals['name'] = source_obj.name
138
vals['global_distribution'] = True
139
for source_cost_center_line in source_obj.cost_center_lines:
140
distrib_line_vals = {
141
'name': source_cost_center_line.name,
142
'analytic_id': source_cost_center_line.analytic_id.id,
143
'percentage': source_cost_center_line.percentage,
144
'amount': round(source_cost_center_line.percentage * destination_amount) / 100.0,
145
'distribution_id': destination_id,
146
'currency_id': destination_currency,
147
'date': source_cost_center_line.date or False,
148
'source_date': source_cost_center_line.source_date or False,
150
cc_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
151
for source_funding_pool_line in source_obj.funding_pool_lines:
152
distrib_line_vals = {
153
'name': source_funding_pool_line.name,
154
'analytic_id': source_funding_pool_line.analytic_id.id,
155
'cost_center_id': source_funding_pool_line.cost_center_id.id,
156
'percentage': source_funding_pool_line.percentage,
157
'amount': round(source_funding_pool_line.percentage * destination_amount) / 100.0,
158
'distribution_id': destination_id,
159
'currency_id': destination_currency,
160
'date': source_funding_pool_line.date or False,
161
'source_date': source_funding_pool_line.source_date or False,
163
fp_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
164
for source_free_1_line in source_obj.free_1_lines:
165
distrib_line_vals = {
166
'name': source_free_1_line.name,
167
'analytic_id': source_free_1_line.analytic_id.id,
168
'percentage': source_free_1_line.percentage,
169
'amount': round(source_free_1_line.percentage * destination_amount) / 100.0,
170
'distribution_id': destination_id,
171
'currency_id': destination_currency,
172
'date': source_free_1_line.date or False,
173
'source_date': source_free_1_line.source_date or False,
175
f1_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
176
for source_free_2_line in source_obj.free_2_lines:
177
distrib_line_vals = {
178
'name': source_free_2_line.name,
179
'analytic_id': source_free_2_line.analytic_id.id,
180
'percentage': source_free_2_line.percentage,
181
'amount': round(source_free_2_line.percentage * destination_amount) / 100.0,
182
'distribution_id': destination_id,
183
'currency_id': destination_currency,
184
'date': source_free_2_line.date or False,
185
'source_date': source_free_2_line.source_date or False,
187
f2_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
188
if destination_obj.invoice_line_ids:
189
self.pool.get('account.invoice.line').create_engagement_lines(cr, uid, [x.id for x in destination_obj.invoice_line_ids])
190
return super(analytic_distribution, self).write(cr, uid, [destination_id], vals, context=context)
192
def update_distribution_line_amount(self, cr, uid, ids, amount=False, context={}):
194
Update amount on distribution lines for given distribution (ids)
199
if isinstance(ids, (int, long)):
203
# Process distributions
204
for distrib_id in ids:
205
for dl_name in ['cost.center.distribution.line', 'funding.pool.distribution.line', 'free.1.distribution.line', 'free.2.distribution.line']:
206
dl_obj = self.pool.get(dl_name)
207
dl_ids = dl_obj.search(cr, uid, [('distribution_id', '=', distrib_id)], context=context)
208
for dl in dl_obj.browse(cr, uid, dl_ids, context=context):
210
'amount': round(dl.percentage * amount) / 100.0,
212
dl_obj.write(cr, uid, [dl.id], dl_vals, context=context)
215
analytic_distribution()
216
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: