1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
# Developer: Olivier DOSSMANN
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv
from osv import fields
from tools.translate import _
from time import strftime
from account_tools import get_period_from_date
from account_tools import get_date_in_period
from collections import defaultdict
class purchase_order(osv.osv):
_name = 'purchase.order'
_inherit = 'purchase.order'
def _get_distribution_line_count(self, cr, uid, ids, name, args, context={}):
"""
Return analytic distribution line count (given by analytic distribution)
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
res = {}
# Browse given invoices
for po in self.browse(cr, uid, ids, context=context):
tmp = po.analytic_distribution_id and po.analytic_distribution_id.lines_count or ''
# Transform result with just CC line count
if tmp != '':
res[po.id] = tmp.split(';')[0]
return res
_columns = {
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
'analytic_distribution_line_count': fields.function(_get_distribution_line_count, method=True, type='char', size=256,
string="Analytic distribution count", readonly=True, store=False),
'commitment_ids': fields.one2many('account.commitment', 'purchase_id', string="Commitment Vouchers", readonly=True),
}
def inv_line_create(self, cr, uid, account_id, order_line):
"""
Add a link between the new invoice line and the order line that it come from
"""
# Retrieve data
res = super(purchase_order, self).inv_line_create(cr, uid, account_id, order_line)
# Add order_line_id to data
if res and res[2]:
res[2].update({'order_line_id': order_line.id,})
# Return result
return res
def action_invoice_create(self, cr, uid, ids, *args):
"""
Take all new invoice lines and give them analytic distribution that was linked on each purchase order line (if exists)
"""
# Retrieve some data
res = super(purchase_order, self).action_invoice_create(cr, uid, ids, *args) # invoice_id
# Set analytic distribution from purchase order to invoice
for po in self.browse(cr, uid, ids):
# Copy analytic_distribution
self.pool.get('account.invoice').fetch_analytic_distribution(cr, uid, [x.id for x in po.invoice_ids])
return res
def button_analytic_distribution(self, cr, uid, ids, context={}):
"""
Launch analytic distribution wizard on a purchase order
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
purchase = self.browse(cr, uid, ids[0], context=context)
amount = purchase.amount_total or 0.0
# Search elements for currency
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
currency = purchase.currency_id and purchase.currency_id.id or company_currency
# Get analytic_distribution_id
distrib_id = purchase.analytic_distribution_id and purchase.analytic_distribution_id.id
# Prepare values for wizard
vals = {
'total_amount': amount,
'purchase_id': purchase.id,
'currency_id': currency or False,
'state': 'cc',
}
if distrib_id:
vals.update({'distribution_id': distrib_id,})
# Create the wizard
wiz_obj = self.pool.get('analytic.distribution.wizard')
wiz_id = wiz_obj.create(cr, uid, vals, context=context)
# Update some context values
context.update({
'active_id': ids[0],
'active_ids': ids,
})
# Open it!
return {
'type': 'ir.actions.act_window',
'res_model': 'analytic.distribution.wizard',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
'res_id': [wiz_id],
'context': context,
}
def copy_data(self, cr, uid, id, default={}, context={}):
"""
Copy global distribution and give it to new purchase.
Delete commitment_ids link.
"""
# Some verifications
if not context:
context = {}
# Update default
default.update({'commitment_ids': False,})
# Default method
res = super(purchase_order, self).copy_data(cr, uid, id, default=default, context=context)
# Update analytic distribution
if res:
po = self.browse(cr, uid, res, context=context)
if res and po.analytic_distribution_id:
new_distrib_id = self.pool.get('analytic.distribution').copy(cr, uid, po.analytic_distribution_id.id, {}, context=context)
if new_distrib_id:
self.write(cr, uid, [res], {'analytic_distribution_id': new_distrib_id}, context=context)
return res
def action_create_commitment(self, cr, uid, ids, type=False, context={}):
"""
Create commitment from given PO, but only for external and esc partner_types
"""
# Some verifications
if not type or type not in ['external', 'esc']:
return False
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
commit_obj = self.pool.get('account.commitment')
eng_ids = self.pool.get('account.analytic.journal').search(cr, uid, [('type', '=', 'engagement')], context=context)
for po in self.browse(cr, uid, ids, context=context):
# fetch analytic distribution, period from delivery date, currency, etc.
vals = {
'journal_id': eng_ids and eng_ids[0] or False,
'currency_id': po.currency_id and po.currency_id.id or False,
'partner_id': po.partner_id and po.partner_id.id or False,
'purchase_id': po.id or False,
'type': po.partner_id and po.partner_id.partner_type or 'manual',
}
# prepare some values
today = strftime('%Y-%m-%d')
period_ids = get_period_from_date(self, cr, uid, po.delivery_requested_date or today, context=context)
period_id = period_ids and period_ids[0] or False
date = get_date_in_period(self, cr, uid, po.delivery_requested_date or today, period_id, context=context)
po_lines = defaultdict(list)
# update period and date
vals.update({
'date': date,
'period_id': period_id,
})
# Create commitment
commit_id = commit_obj.create(cr, uid, vals, context=context)
# Add analytic distribution from purchase
if po.analytic_distribution_id:
new_distrib_id = self.pool.get('analytic.distribution').copy(cr, uid, po.analytic_distribution_id.id, {}, context=context)
# Update this distribution not to have a link with purchase but with new commitment
if new_distrib_id:
self.pool.get('analytic.distribution').write(cr, uid, [new_distrib_id], {'purchase_id': False, 'commitment_id': commit_id}, context=context)
# Create funding pool lines if needed
self.pool.get('analytic.distribution').create_funding_pool_lines(cr, uid, [new_distrib_id], context=context)
# Update commitment with new analytic distribution
self.pool.get('account.commitment').write(cr, uid, [commit_id], {'analytic_distribution_id': new_distrib_id}, context=context)
# Browse purchase order lines and group by them by account_id
for pol in po.order_line:
# Search product account_id
if pol.product_id:
a = pol.product_id.product_tmpl_id.property_account_expense.id
if not a:
a = pol.product_id.categ_id.property_account_expense_categ.id
if not a:
raise osv.except_osv(_('Error !'), _('There is no expense account defined for this product: "%s" (id:%d)') % (ol.product_id.name, ol.product_id.id,))
else:
a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category').id
fpos = po.fiscal_position or False
a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, a)
# Write
po_lines[a].append(pol)
# Commitment lines process
created_commitment_lines = []
for account_id in po_lines:
total_amount = 0.0
for line in po_lines[account_id]:
total_amount += line.price_subtotal
# Create commitment lines
line_id = self.pool.get('account.commitment.line').create(cr, uid, {'commit_id': commit_id, 'amount': total_amount, 'initial_amount': total_amount, 'account_id': account_id, 'purchase_order_line_ids': [(6,0,[x.id for x in po_lines[account_id]])]}, context=context)
created_commitment_lines.append(line_id)
# Create analytic distribution on this commitment line
self.pool.get('account.commitment.line').create_distribution_from_order_line(cr, uid, created_commitment_lines, context=context)
# Display a message to inform that a commitment was created
commit_data = self.pool.get('account.commitment').read(cr, uid, commit_id, ['name'], context=context)
commit_name = commit_data and commit_data.get('name') or ''
message = _("Commitment Voucher %s has been created.") % commit_name
view_ids = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution', 'account_commitment_form')
view_id = view_ids and view_ids[1] or False
self.pool.get('account.commitment').log(cr, uid, commit_id, message, context={'view_id': view_id})
return True
def wkf_approve_order(self, cr, uid, ids, context={}):
"""
Checks:
1/ if all purchase line could take an analytic distribution
2/ if a commitment voucher should be created after PO approbation
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Analytic distribution verification
ana_obj = self.pool.get('analytic.distribution')
for po in self.browse(cr, uid, ids, context=context):
if not po.analytic_distribution_id:
for line in po.order_line:
if not line.analytic_distribution_id:
dummy_cc = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution', 'analytic_account_project_dummy')
ana_id = ana_obj.create(cr, uid, {'purchase_ids': [(4,po.id)], 'cost_center_lines': [(0, 0, {'analytic_id': dummy_cc[1] , 'percentage':'100', 'currency_id': po.currency_id.id})]})
break
# Default behaviour
res = super(purchase_order, self).wkf_approve_order(cr, uid, ids, context=context)
# Create commitments for each PO only if po is "from picking"
for po in self.browse(cr, uid, ids, context=context):
if po.invoice_method in ['picking', 'order']:
self.action_create_commitment(cr, uid, [po.id], po.partner_id and po.partner_id.partner_type, context=context)
return res
def _finish_commitment(self, cr, uid, ids, context={}):
"""
Change commitment(s) to Done state from given Purchase Order.
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Browse PO
for po in self.browse(cr, uid, ids, context=context):
# Change commitment state if exists
if po.commitment_ids:
self.pool.get('account.commitment').action_commitment_done(cr, uid, [x.id for x in po.commitment_ids], context=context)
return True
def action_cancel(self, cr, uid, ids, context={}):
"""
Delete commitment from purchase before 'cancel' state.
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Change commitments state if exists
self._finish_commitment(cr, uid, ids, context=context)
return super(purchase_order, self).action_cancel(cr, uid, ids, context=context)
def action_done(self, cr, uid, ids, context={}):
"""
Delete commitment from purchase before 'done' state.
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Change commitments state if all shipments have been invoiced (not in "to be invoiced" state)
to_process = []
for po in self.browse(cr, uid, ids, context=context):
is_totally_done = True
# If one shipment (stock.picking) is '2binvoiced', we shouldn't change commitments ' state
for pick in po.picking_ids:
if pick.invoice_state == '2binvoiced':
is_totally_done = False
# Else shipment is fully done. We could change commitments ' state to Done.
if is_totally_done:
to_process.append(po.id)
self._finish_commitment(cr, uid, to_process, context=context)
return super(purchase_order, self).action_done(cr, uid, ids, context=context)
purchase_order()
class purchase_order_line(osv.osv):
_name = 'purchase.order.line'
_inherit = 'purchase.order.line'
def _get_distribution_line_count(self, cr, uid, ids, name, args, context={}):
"""
Return analytic distribution line count (given by analytic distribution)
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
res = {}
# Browse given invoices
for pol in self.browse(cr, uid, ids, context=context):
tmp = pol.analytic_distribution_id and pol.analytic_distribution_id.lines_count or ''
# Transform result with just CC line count
if tmp != '':
res[pol.id] = tmp.split(';')[0]
return res
def _have_analytic_distribution_from_header(self, cr, uid, ids, name, arg, context={}):
if isinstance(ids, (int, long)):
ids = [ids]
res = {}
for line in self.read(cr, uid, ids, ['analytic_distribution_id']):
if line['analytic_distribution_id']:
res[line['id']] = False
else:
res[line['id']] = True
return res
_columns = {
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
'analytic_distribution_line_count': fields.function(_get_distribution_line_count, method=True, type='char', size=256,
string="Analytic distribution count", readonly=True, store=False),
'have_analytic_distribution_from_header': fields.function(_have_analytic_distribution_from_header, method=True, type='boolean', string='Header Distrib.?'),
'commitment_line_ids': fields.many2many('account.commitment.line', 'purchase_line_commitment_rel', 'purchase_id', 'commitment_id',
string="Commitment Voucher Lines", readonly=True),
}
_defaults = {
'have_analytic_distribution_from_header': lambda *a: True,
}
def button_analytic_distribution(self, cr, uid, ids, context={}):
"""
Launch analytic distribution wizard on a purchase order
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
purchase_line = self.browse(cr, uid, ids[0], context=context)
amount = purchase_line.price_subtotal or 0.0
# Search elements for currency
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
currency = purchase_line.order_id.currency_id and purchase_line.order_id.currency_id.id or company_currency
# Get analytic_distribution_id
distrib_id = purchase_line.analytic_distribution_id and purchase_line.analytic_distribution_id.id
# Prepare values for wizard
vals = {
'total_amount': amount,
'purchase_line_id': purchase_line.id,
'currency_id': currency or False,
'state': 'cc',
}
if distrib_id:
vals.update({'distribution_id': distrib_id,})
# Create the wizard
wiz_obj = self.pool.get('analytic.distribution.wizard')
wiz_id = wiz_obj.create(cr, uid, vals, context=context)
# Update some context values
context.update({
'active_id': ids[0],
'active_ids': ids,
})
# Open it!
return {
'type': 'ir.actions.act_window',
'res_model': 'analytic.distribution.wizard',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
'res_id': [wiz_id],
'context': context,
}
def copy_data(self, cr, uid, id, default={}, context={}):
"""
Copy global distribution and give it to new purchase line
Copy global distribution and give it to new purchase line.
"""
# Some verifications
if not context:
context = {}
# Update default
default.update({'commitment_line_ids': [(6, 0, [])],})
# Copy analytic distribution
pol = self.browse(cr, uid, [id], context=context)[0]
if pol.analytic_distribution_id:
new_distrib_id = self.pool.get('analytic.distribution').copy(cr, uid, pol.analytic_distribution_id.id, {}, context=context)
if new_distrib_id:
default.update({'analytic_distribution_id': new_distrib_id})
return super(purchase_order_line, self).copy_data(cr, uid, id, default, context)
purchase_order_line()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|