2
#-*- encoding: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
27
class purchase_order(osv.osv):
28
_name = 'purchase.order'
29
_inherit = 'purchase.order'
32
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
35
def button_analytic_distribution(self, cr, uid, ids, context={}):
37
Launch analytic distribution wizard on a purchase order
42
if isinstance(ids, (int, long)):
45
ana_obj = self.pool.get('analytic.distribution')
46
purchase = self.browse(cr, uid, ids[0], context=context)
47
amount = purchase.amount_total or 0.0
48
child_distributions = []
49
# Get analytic_distribution_id
50
distrib_id = purchase.analytic_distribution_id and purchase.analytic_distribution_id.id
51
# Create an analytic_distribution_id if no one exists
53
res_id = ana_obj.create(cr, uid, {}, context=context)
54
super(purchase_order, self).write(cr, uid, ids, {'analytic_distribution_id': res_id}, context=context)
56
# Search analytic distribution to renew if necessary
57
for pl in purchase.order_line:
58
pl_amount = pl.price_subtotal or 0.0
59
if pl.analytic_distribution_id:
60
if pl.analytic_distribution_id.global_distribution or context.get('reset_all', False):
61
child_distributions.append((pl.analytic_distribution_id.id, pl_amount))
63
child_distrib_id = ana_obj.create(cr, uid, {'global_distribution': True}, context=context)
64
self.pool.get('purchase.order.line').write(cr, uid, [pl.id], {'analytic_distribution_id': child_distrib_id}, context=context)
65
child_distributions.append((child_distrib_id, pl_amount))
67
wiz_obj = self.pool.get('wizard.costcenter.distribution')
68
wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id}, context=context)
69
# Update some context values
73
'wizard_ids': {'cost_center': wiz_id},
74
'child_distributions': child_distributions
78
'type': 'ir.actions.act_window',
79
'res_model': 'wizard.costcenter.distribution',
89
class purchase_order_line(osv.osv):
90
_name = 'purchase.order.line'
91
_inherit = 'purchase.order.line'
94
'analytic_distribution_id': fields.many2one('analytic.distribution', string="Analytic Distribution"),
97
def button_analytic_distribution(self, cr, uid, ids, context={}):
99
Launch the analytic distribution wizard on the first given id (from ids)
104
if isinstance(ids, (int, long)):
106
# Prepare some values
107
pl = self.browse(cr, uid, ids[0], context=context) # purchase line
108
amount = pl.price_subtotal or 0.0
109
child_distributions = []
110
# Search elements for currency
111
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
112
currency = pl.order_id.currency_id and pl.order_id.currency_id.id or company_currency
113
# Get analytic distribution
114
distrib_id = pl.analytic_distribution_id and pl.analytic_distribution_id.id or False
116
raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution for the whole purchase order first!"))
118
wiz_obj = self.pool.get('wizard.costcenter.distribution')
119
wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
120
# Add some values in context
124
'wizard_ids': {'cost_center': wiz_id},
125
'child_distributions': child_distributions
129
'type': 'ir.actions.act_window',
130
'res_model': 'wizard.costcenter.distribution',
138
def create(self, cr, uid, vals, context={}):
140
Link analytic distribution on purchase order line after its creation
146
if vals.get('order_id', False):
147
# Search global distribution (those from purchase order)
148
po = self.pool.get('purchase.order').browse(cr, uid, vals.get('order_id'), context=context)
149
if po.analytic_distribution_id:
150
# Create a new global analytic distribution
151
ana_obj = self.pool.get('analytic.distribution')
152
child_distrib_id = ana_obj.create(cr, uid, {'global_distribution': True}, context=context)
153
vals.update({'analytic_distribution_id': child_distrib_id,})
154
res = super(purchase_order_line, self).create(cr, uid, vals, context=context)
155
total_amount = self._amount_line(cr, uid, [res], None, None, context=context)[res]
156
amount = total_amount or 0.0
157
# Search currency (by default those of company)
158
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
159
currency = po.currency_id and po.currency_id.id or company_currency
160
ana_obj.copy_from_global_distribution(cr, uid, po.analytic_distribution_id.id, child_distrib_id, amount, currency, context=context)
162
return super(purchase_order_line, self).create(cr, uid, vals, context=context)
164
def write(self, cr, uid, ids, vals, context={}):
166
Update analytic lines if an analytic distribution exists
171
if isinstance(ids, (int, long)):
173
# Write first new purchase order line
174
res = super(purchase_order_line, self).write(cr, uid, ids, vals, context=context)
175
for pl in self.browse(cr, uid, ids, context=context):
176
# do something only if analytic_distribution_id field is filled in
177
if pl.order_id.analytic_distribution_id:
178
# prepare some values
179
po_distrib = pl.order_id.analytic_distribution_id
180
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
181
currency = pl.order_id.currency_id and pl.order_id.currency_id.id or company_currency
182
if 'price_unit' in vals or 'product_qty' in vals or context.get('reset_all', False) or pl.analytic_distribution_id.global_distribution:
183
amount = pl.price_subtotal or 0.0
184
self.pool.get('analytic.distribution').copy_from_global_distribution(cr, uid, po_distrib.id, pl.analytic_distribution_id.id, amount, currency, context=context)
187
purchase_order_line()
188
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: