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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2012 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
class sale_order(osv.osv):
_name = 'sale.order'
_inherit = 'sale.order'
_columns = {
'analytic_distribution_id': fields.many2one('analytic.distribution', string="Analytic distribution"),
}
def button_analytic_distribution(self, cr, uid, ids, context=None):
"""
Launch analytic distribution wizard on a sale order
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
so = self.browse(cr, uid, ids[0], context=context)
amount = so.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 = so.currency_id and so.currency_id.id or company_currency
# Get analytic_distribution_id
distrib_id = so.analytic_distribution_id and so.analytic_distribution_id.id
# Prepare values for wizard
vals = {
'total_amount': amount,
'sale_order_id': so.id,
'partner_type': so.partner_type, # UF-2138: Add partner_type to the analytic distribution
'currency_id': currency or False,
'state': 'cc',
'posting_date': strftime('%Y-%m-%d'),
'document_date': strftime('%Y-%m-%d'),
}
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 {
'name': _('Global analytic distribution'),
'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 button_reset_distribution(self, cr, uid, ids, context=None):
"""
Reset analytic distribution on all sale order lines.
To do this, just delete the analytic_distribution id link on each sale order line.
"""
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
sale_obj = self.pool.get(self._name + '.line')
# Search lines
to_reset = sale_obj.search(cr, uid, [('order_id', 'in', ids)])
sale_obj.write(cr, uid, to_reset, {'analytic_distribution_id': False})
return True
def copy_data(self, cr, uid, s_id, default=None, context=None):
"""
Copy global distribution and give it to new sale order.
"""
# Some verifications
if not context:
context = {}
if default is None:
default = {}
# Default method
if 'analytic_distribution_id' not in default and not context.get('keepDateAndDistrib'):
default['analytic_distribution_id'] = False
new_data = super(sale_order, self).copy_data(cr, uid, s_id, default=default, context=context)
# UFTP-322: After the copy_data above, if successfully, the name should be exactly the same as of the original document, so it must be increased
# in the next statements. ::::::: One of the case that failed the copy_data call above is due to user right
seq_obj = self.pool.get('ir.sequence')
order = self.browse(cr, uid, s_id)
if new_data and not 'name' in new_data or new_data.get('name', False) == order.name:
name = (order.procurement_request or context.get('procurement_request', False)) and seq_obj.get(cr, uid, 'procurement.request') or seq_obj.get(cr, uid, 'sale.order')
new_data.update({'name': name})
if new_data and new_data.get('analytic_distribution_id'):
new_data['analytic_distribution_id'] = self.pool.get('analytic.distribution').copy(cr, uid, new_data['analytic_distribution_id'], {}, context=context)
return new_data
def _get_destination_ok(self, cr, uid, lines, context):
dest_ok = False
for line in lines:
dest_ok = line.account_4_distribution and line.account_4_distribution.destination_ids or False
if not dest_ok:
raise osv.except_osv(_('Error'), _('No destination found for this line: %s.') % (line.name or '',))
return dest_ok
def analytic_distribution_checks(self, cr, uid, order_brw_list, context=None):
"""
Check analytic distribution for each sale order line (except if we come from YAML tests)
Get a default analytic distribution if intermission.
Change analytic distribution if intermission.
"""
# Objects
ana_obj = self.pool.get('analytic.distribution')
data_obj = self.pool.get('ir.model.data')
acc_obj = self.pool.get('account.account')
sol_obj = self.pool.get('sale.order.line')
distrib_line_obj = self.pool.get('cost.center.distribution.line')
# Some verifications
if context is None:
context = {}
if not isinstance(order_brw_list, list):
order_brw_list = [order_brw_list]
for so in order_brw_list:
# Do not check analytic distribution on IR
if so.procurement_request:
continue
for line in so.order_line:
"""
UFTP-336: Do not check AD on FO lines if the lines are
created on a tender or a RfQ.
The AD must be added on the PO line and update the
AD at FO line at PO confirmation.
"""
if line.created_by_tender or line.created_by_rfq:
continue
# Search intermission
intermission_cc = data_obj.get_object_reference(
cr,
uid,
'analytic_distribution',
'analytic_account_project_intermission',
)
# Check distribution presence
l_ana_dist_id = line.analytic_distribution_id and line.analytic_distribution_id.id
o_ana_dist_id = so.analytic_distribution_id and so.analytic_distribution_id.id
distrib_id = l_ana_dist_id or o_ana_dist_id or False
# For inter-mission partner, define a default ana. distribution if no one is defined on FO
if not distrib_id and so.partner_id.partner_type == 'intermission':
account_id = line.account_4_distribution and line.account_4_distribution.id or False
# Search default destination_id
destination_id = acc_obj.read(cr, uid, account_id, ['default_destination_id']).get('default_destination_id', False)
distrib_id = ana_obj.create(cr, uid, {
'sale_order_line_ids': [(4, line.id)],
'cost_center_lines': [(0, 0, {
'destination_id': destination_id[0],
'analytic_id': intermission_cc[1],
'percentage': '100',
'currency_id': so.currency_id.id,
})],
}, context=context)
# UFTP-277: Check funding pool lines if missing
ana_obj.create_funding_pool_lines(cr, uid, [distrib_id], context=context)
sol_obj.write(cr, uid, [line.id], {'analytic_distribution_id': distrib_id}, context=context)
line = sol_obj.browse(cr, uid, line.id, context=context)
if not distrib_id and not so.from_yml_test and not so.order_type in ('loan', 'donation_st', 'donation_exp'):
raise osv.except_osv(
_('Warning'),
_('Analytic distribution is mandatory for this line: %s!') % (line.name or '',),
)
# Check distribution state
if distrib_id and line.analytic_distribution_state != 'valid' and not so.from_yml_test:
# Raise an error if no analytic distribution on line and NONE on header (because no possibility to change anything)
if (not line.analytic_distribution_id or line.analytic_distribution_state == 'none') and \
not so.analytic_distribution_id:
# We don't raise an error for these types
if so.order_type not in ('loan', 'donation_st', 'donation_exp'):
raise osv.except_osv(
_('Warning'),
_('Analytic distribution is mandatory for this line: %s') % (line.name or '',),
)
else:
continue
# Change distribution to be valid if needed by using those from header
id_ad = ana_obj.create(cr, uid, {}, context=context)
# Get the CC lines of the FO line if any, or the ones of the order
cc_lines = line.analytic_distribution_id and line.analytic_distribution_id.cost_center_lines
cc_lines = cc_lines or so.analytic_distribution_id.cost_center_lines
for x in cc_lines:
# fetch compatible destinations then use one of them:
# - destination if compatible
# - else default destination of given account
bro_dests = self._get_destination_ok(cr, uid, [line], context=context)
if x.destination_id in bro_dests:
bro_dest_ok = x.destination_id
else:
bro_dest_ok = line.account_4_distribution.default_destination_id
# Copy cost center line to the new distribution
distrib_line_obj.copy(cr, uid, x.id, {'distribution_id': id_ad, 'destination_id': bro_dest_ok.id}, context=context)
# Write new distribution and link it to the line
sol_obj.write(cr, uid, [line.id], {'analytic_distribution_id': id_ad}, context=context)
# UFTP-277: Check funding pool lines if missing
ana_obj.create_funding_pool_lines(cr, uid, [id_ad], context=context)
return True
sale_order()
class sale_order_line(osv.osv):
_name = 'sale.order.line'
_inherit = 'sale.order.line'
def _have_analytic_distribution_from_header(self, cr, uid, ids, name, arg, context=None):
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
# METHOD _get_analytic_distribution_available removed (become useless since analytic distribution is mandatory on ALL FO)
def _get_distribution_state(self, cr, uid, ids, name, args, context=None):
"""
Get state of distribution:
- if compatible with the sale order line, then "valid"
- if no distribution, take a tour of sale order distribution, if compatible, then "valid"
- if no distribution on sale order line and sale order, then "none"
- all other case are "invalid"
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
res = {}
# Browse all given lines
for line in self.browse(cr, uid, ids, context=context):
if line.order_id and line.order_id.from_yml_test:
res[line.id] = 'valid'
elif line.order_id and (not line.order_id.analytic_distribution_id or not line.order_id.analytic_distribution_id.cost_center_lines) and (not line.analytic_distribution_id or not line.analytic_distribution_id.cost_center_lines) :
res[line.id] = 'none'
else:
so_distrib_id = line.order_id and line.order_id.analytic_distribution_id and line.order_id.analytic_distribution_id.id or False
distrib_id = line.analytic_distribution_id and line.analytic_distribution_id.id or False
account_id = line.account_4_distribution and line.account_4_distribution.id or False
if not account_id:
res[line.id] = 'invalid'
continue
res[line.id] = self.pool.get('analytic.distribution')._get_distribution_state(cr, uid, distrib_id, so_distrib_id, account_id)
return res
def _get_distribution_state_recap(self, cr, uid, ids, name, arg, context=None):
if isinstance(ids, (int, long)):
ids = [ids]
res = {}
get_sel = self.pool.get('ir.model.fields').get_selection
for sol in self.read(cr, uid, ids, ['analytic_distribution_state', 'have_analytic_distribution_from_header']):
d_state = get_sel(cr, uid, self._name, 'analytic_distribution_state', sol['analytic_distribution_state'], context)
res[sol['id']] = "%s%s" % (d_state, sol['have_analytic_distribution_from_header'] and _(" (from header)") or "")
return res
def _get_distribution_account(self, cr, uid, ids, name, arg, context=None):
"""
Get account for given lines regarding:
- product expense account if product_id
- product category expense account if product_id but no product expense account
- product category expense account if no product_id (come from family's product category link)
"""
# Some verifications
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
res = {}
for line in self.browse(cr, uid, ids):
# Prepare some values
res[line.id] = False
a = False
# Fetch account
if line.product_id:
a = line.product_id.product_tmpl_id.property_account_income.id or False
if not a:
a = line.product_id.categ_id.property_account_income_categ.id or False
else:
a = line.nomen_manda_2 and line.nomen_manda_2.category_id and line.nomen_manda_2.category_id.property_account_income_categ and line.nomen_manda_2.category_id.property_account_income_categ.id or False
res[line.id] = a
return res
_columns = {
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
'have_analytic_distribution_from_header': fields.function(_have_analytic_distribution_from_header, method=True, type='boolean',
string='Header Distrib.?'),
'analytic_distribution_state': fields.function(_get_distribution_state, method=True, type='selection',
selection=[('none', 'None'), ('valid', 'Valid'), ('invalid', 'Invalid')],
string="Distribution state", help="Informs from distribution state among 'none', 'valid', 'invalid."),
'analytic_distribution_state_recap': fields.function(_get_distribution_state_recap, method=True, type='char', size=30, string="Distribution"),
'account_4_distribution': fields.function(_get_distribution_account, method=True, type='many2one', relation="account.account", string="Account for analytical distribution", readonly=True),
}
_defaults = {
'have_analytic_distribution_from_header': lambda *a: True,
}
def button_analytic_distribution(self, cr, uid, ids, context=None):
"""
Launch analytic distribution wizard on a sale order line
"""
# Some verifications
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# Prepare some values
sol = self.browse(cr, uid, ids[0], context=context)
amount = sol.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 = sol.order_id.currency_id and sol.order_id.currency_id.id or company_currency
# Get analytic_distribution_id
distrib_id = sol.analytic_distribution_id and sol.analytic_distribution_id.id
# Search account_id
account_id = sol.account_4_distribution and sol.account_4_distribution.id or False
if not account_id:
raise osv.except_osv(_('Error !'), _('There is no income account defined for this product: "%s" (id:%d)') % \
(sol.product_id.name, sol.product_id.id,))
# Prepare values for wizard
vals = {
'total_amount': amount,
'sale_order_line_id': sol.id,
'partner_type': sol.order_id.partner_type, # UF-2138: Add partner_type to the analytic distribution
'currency_id': currency or False,
'state': 'cc',
'account_id': account_id or False,
'posting_date': strftime('%Y-%m-%d'),
'document_date': strftime('%Y-%m-%d'),
}
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 {
'name': _('Analytic distribution'),
'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, l_id, default=None, context=None):
"""
Copy global distribution and give it to new sale order line
"""
# Some verifications
if not context:
context = {}
if default is None:
default = {}
# Copy analytic distribution
if 'analytic_distribution_id' not in default and not context.get('keepDateAndDistrib'):
default['analytic_distribution_id'] = False
new_data = super(sale_order_line, self).copy_data(cr, uid, l_id, default, context)
if new_data and new_data.get('analytic_distribution_id'):
new_data['analytic_distribution_id'] = self.pool.get('analytic.distribution').copy(cr, uid, new_data['analytic_distribution_id'], {}, context=context)
return new_data
sale_order_line()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|