2
# -*- coding: utf-8 -*-
3
##############################################################################
5
# OpenERP, Open Source Management Solution
6
# Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
7
# Developer: Olivier DOSSMANN
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU Affero General Public License as
11
# published by the Free Software Foundation, either version 3 of the
12
# License, or (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU Affero General Public License for more details.
19
# You should have received a copy of the GNU Affero General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
##############################################################################
25
from osv import fields
26
from collections import defaultdict
28
class account_commitment(osv.osv):
29
_name = 'account.commitment'
30
_inherit = 'account.commitment'
33
'purchase_id': fields.many2one('purchase.order', string="Source document", readonly=True),
38
class account_commitment_line(osv.osv):
39
_name = 'account.commitment.line'
40
_inherit = 'account.commitment.line'
43
'purchase_order_line_ids': fields.many2many('purchase.order.line', 'purchase_line_commitment_rel', 'commitment_id', 'purchase_id',
44
string="Purchase Order Lines", readonly=True),
47
def create_distribution_from_order_line(self, cr, uid, ids, context=None):
49
Create an analytic distribution regarding those from attached PO lines (if exists).
54
if isinstance(ids, (int, long)):
56
# Browse commitment lines
57
for line in self.browse(cr, uid, ids, context=context):
58
cc_lines = defaultdict(list) # a dict that permits to show all amount for a specific cost_center (regarding analytic_id from cost_center_line)
59
# browse purchase order lines attached to this commitment line
60
for pol in line.purchase_order_line_ids:
61
# browse cost_center line if an analytic distribution exists for this purchase order line
62
if pol.analytic_distribution_id:
63
origin_cc_lines = pol.analytic_distribution_id.cost_center_lines
64
# else retrieve CC lines from PO
66
origin_cc_lines = pol.order_id.analytic_distribution_id.cost_center_lines
67
# Compute CC lines amounts
68
for aline in origin_cc_lines:
70
# Compute amount regarding pol subtotal and cost_center_line percentage
71
amount = (pol.price_subtotal * aline.percentage) / 100
72
cc_lines[(aline.analytic_id.id, aline.destination_id and aline.destination_id.id or False)].append(amount)
73
# Browse result and create corresponding analytic lines
75
# create distribution an link it to commitment line
76
distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {}, context=context)
77
self.write(cr, uid, [line.id], {'analytic_distribution_id': distrib_id}, context=context)
79
analytic_id = el[0] or False
81
'distribution_id': distrib_id,
82
'analytic_id': analytic_id,
83
'currency_id': line.commit_id.currency_id.id,
84
'destination_id': el[1] or False,
88
for amount in cc_lines[el]:
89
total_amount += amount
93
percentage = (total_amount / line.amount) * 100 or 0.0
94
vals.update({'percentage': percentage})
95
# create cost_center_line
96
distrib_line_id = self.pool.get('cost.center.distribution.line').create(cr, uid, vals, context=context)
97
# Create funding pool lines if needed
98
self.pool.get('analytic.distribution').create_funding_pool_lines(cr, uid, [distrib_id], line.account_id.id, context=context)
101
account_commitment_line()
102
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: