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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2011 MSF, TeMPO Consulting
#
# 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 datetime import datetime, timedelta, date
from dateutil.relativedelta import relativedelta, relativedelta
from osv import osv, fields
from osv.orm import browse_record, browse_null
from tools.translate import _
import decimal_precision as dp
import netsvc
import pooler
import time
class sale_order(osv.osv):
_inherit = 'sale.order'
_description = 'Sales Order'
_columns = {'sequence_id': fields.many2one('ir.sequence', 'Lines Sequence', help="This field contains the information related to the numbering of the lines of this order.", required=True, ondelete='cascade'),
}
def create_sequence(self, cr, uid, vals, context=None):
"""
Create new entry sequence for every new order
@param cr: cursor to database
@param user: id of current user
@param ids: list of record ids to be process
@param context: context arguments, like lang, time zone
@return: return a result
"""
seq_pool = self.pool.get('ir.sequence')
seq_typ_pool = self.pool.get('ir.sequence.type')
name = 'Sale Order'
code = 'sale.order'
types = {
'name': name,
'code': code
}
seq_typ_pool.create(cr, uid, types)
seq = {
'name': name,
'code': code,
'prefix': '',
'padding': 0,
}
return seq_pool.create(cr, uid, seq)
def create(self, cr, uid, vals, context=None):
'''
create from sale_order
create the sequence for the numbering of the lines
'''
if context is None:
context = {}
if not context.get('keepClientOrder') or not context.get('keepDateAndDistrib') or not vals.get('sequence_id'):
vals.update({'sequence_id': self.create_sequence(cr, uid, vals, context)})
return super(sale_order, self).create(cr, uid, vals, context)
def reorder_line_numbering(self, cr, uid, ids, context=None):
'''
test function
'''
# objects
tools_obj = self.pool.get('sequence.tools')
tools_obj.reorder_sequence_number(cr, uid, 'sale.order', 'sequence_id', 'sale.order.line', 'order_id', ids, 'line_number', context=context)
return True
def allow_resequencing(self, cr, uid, so_browse, context=None):
'''
allow resequencing criteria
'''
if so_browse.state == 'draft' and not so_browse.client_order_ref:
return True
return False
sale_order()
class sale_order_line(osv.osv):
'''
override of sale_order_line class
'''
_inherit = 'sale.order.line'
_description = 'Sales Order Line'
_columns = {'line_number': fields.integer(string='Line', required=True),
}
_order = 'order_id desc, line_number, id'
def create(self, cr, uid, vals, context=None):
'''
_inherit = 'sale.order.line'
add the corresponding line number
'''
# objects
so_obj = self.pool.get('sale.order')
seq_pool = self.pool.get('ir.sequence')
# gather the line number from the sale order sequence if not specified in vals
# either line_number is not specified or set to False from copy, we need a new value
if vals.get('order_id', False):
if not vals.get('line_number', False):
# new number needed - gather the line number from the sequence
sequence_id = so_obj.read(cr, uid, [vals['order_id']], ['sequence_id'], context=context)[0]['sequence_id'][0]
line = seq_pool.get_id(cr, uid, sequence_id, code_or_id='id', context=context)
vals.update({'line_number': line})
# create the new sale order line
result = super(sale_order_line, self).create(cr, uid, vals, context=context)
return result
def copy_data(self, cr, uid, id, default=None, context=None):
'''
if the line_number is not in the default, we set it to False
'''
if default is None:
default = {}
if context is None:
context = {}
# we set line_number, so it will not be copied in copy_data - keepLineNumber - the original Line Number will be kept
if 'line_number' not in default and not context.get('keepLineNumber', False):
default.update({'line_number': False})
return super(sale_order_line, self).copy_data(cr, uid, id, default, context=context)
def unlink(self, cr, uid, ids, context=None):
'''
check the numbering on deletion
'''
# Some verifications
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# objects
tools_obj = self.pool.get('sequence.tools')
if not context.get('skipResequencing', False):
# re sequencing only happen if field order is draft and not synchronized (PUSH flow) (behavior 1)
draft_not_synchronized_ids = self.allow_resequencing(cr, uid, ids, context=context)
tools_obj.reorder_sequence_number_from_unlink(cr, uid, draft_not_synchronized_ids, 'sale.order', 'sequence_id', 'sale.order.line', 'order_id', 'line_number', context=context)
return super(sale_order_line, self).unlink(cr, uid, ids, context=context)
def allow_resequencing(self, cr, uid, ids, context=None):
'''
define if a resequencing has to be performed or not
return the list of ids for which resequencing will can be performed
linked to Fo + Fo draft + Fo not sync
'''
# objects
so_obj = self.pool.get('sale.order')
resequencing_ids = [x.id for x in self.browse(cr, uid, ids, context=context) if x.order_id and so_obj.allow_resequencing(cr, uid, x.order_id, context=context)]
return resequencing_ids
sale_order_line()
class purchase_order(osv.osv):
_inherit = 'purchase.order'
_description = 'Purchase Order'
_columns = {'sequence_id': fields.many2one('ir.sequence', 'Lines Sequence', help="This field contains the information related to the numbering of the lines of this order.", required=True, ondelete='cascade'),
}
def create_sequence(self, cr, uid, vals, context=None):
"""
Create new entry sequence for every new order
@param cr: cursor to database
@param user: id of current user
@param ids: list of record ids to be process
@param context: context arguments, like lang, time zone
@return: return a result
"""
seq_pool = self.pool.get('ir.sequence')
seq_typ_pool = self.pool.get('ir.sequence.type')
name = 'Purchase Order'
code = 'purchase.order'
types = {
'name': name,
'code': code
}
seq_typ_pool.create(cr, uid, types)
seq = {
'name': name,
'code': code,
'prefix': '',
'padding': 0,
}
return seq_pool.create(cr, uid, seq)
def create(self, cr, uid, vals, context=None):
'''
create from purchase_order
create the sequence for the numbering of the lines
'''
vals.update({'sequence_id': self.create_sequence(cr, uid, vals, context)})
return super(purchase_order, self).create(cr, uid, vals, context)
def reorder_line_numbering(self, cr, uid, ids, context=None):
'''
test function
'''
# objects
tools_obj = self.pool.get('sequence.tools')
tools_obj.reorder_sequence_number(cr, uid, 'purchase.order', 'sequence_id', 'purchase.order.line', 'order_id', ids, 'line_number', context=context)
return True
def allow_resequencing(self, cr, uid, po_browse, context=None):
'''
allow resequencing criteria
'''
if po_browse.state == 'draft':
return True
return False
purchase_order()
class purchase_order_line(osv.osv):
'''
override of purchase_order_line class
'''
_inherit = 'purchase.order.line'
_description = 'Purchase Order Line'
_columns = {'line_number': fields.integer(string='Line', required=True),
}
_order = 'line_number, id'
def copy_data(self, cr, uid, id, default=None, context=None):
'''
if the line_number is not in the default, we set it to False
'''
if default is None:
default = {}
if context is None:
context = {}
# we set line_number, so it will not be copied in copy_data - keepLineNumber - the original Line Number will be kept
if 'line_number' not in default and not context.get('keepLineNumber', False):
default.update({'line_number': False})
return super(purchase_order_line, self).copy_data(cr, uid, id, default, context=context)
def unlink(self, cr, uid, ids, context=None):
'''
check the numbering on deletion
'''
# Some verifications
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# objects
tools_obj = self.pool.get('sequence.tools')
if not context.get('skipResequencing', False):
# re sequencing only happen if purchase order is draft (behavior 1)
# get ids with corresponding po at draft state
draft_ids = self.allow_resequencing(cr, uid, ids, context=context)
tools_obj.reorder_sequence_number_from_unlink(cr, uid, draft_ids, 'purchase.order', 'sequence_id', 'purchase.order.line', 'order_id', 'line_number', context=context)
return super(purchase_order_line, self).unlink(cr, uid, ids, context=context)
def allow_resequencing(self, cr, uid, ids, context=None):
'''
define if a resequencing has to be performed or not
return the list of ids for which resequencing will can be performed
linked to Po + Po allows resequencing
'''
# objects
po_obj = self.pool.get('purchase.order')
resequencing_ids = [x.id for x in self.browse(cr, uid, ids, context=context) if x.order_id and po_obj.allow_resequencing(cr, uid, x.order_id, context=context)]
return resequencing_ids
purchase_order_line()
class procurement_order(osv.osv):
'''
inherit po_line_values_hook
'''
_inherit = 'procurement.order'
def po_line_values_hook(self, cr, uid, ids, context=None, *args, **kwargs):
'''
Please copy this to your module's method also.
This hook belongs to the make_po method from purchase>purchase.py>procurement_order
- allow to modify the data for purchase order line creation
'''
if isinstance(ids, (int, long)):
ids = [ids]
# objects
po_obj = self.pool.get('purchase.order')
procurement = kwargs['procurement']
line = super(procurement_order, self).po_line_values_hook(cr, uid, ids, context=context, *args, **kwargs)
# if we are updating the sale order from the corresponding on order purchase order
# the purchase order to merge the new line to is locked and provided in the procurement
# we split a line or dekitting, we keep original line_number if the Po does *not* allow_resequencing: behavior 2
# if it allows, we use behavior 1
if procurement.so_back_update_dest_po_id_procurement_order:
if not po_obj.allow_resequencing(cr, uid, procurement.so_back_update_dest_po_id_procurement_order, context=context):
line.update({'line_number': procurement.so_back_update_dest_pol_id_procurement_order.line_number})
return line
procurement_order()
class supplier_catalogue(osv.osv):
_inherit = 'supplier.catalogue'
_description = 'Supplier catalogue'
_columns = {'sequence_id': fields.many2one('ir.sequence', 'Lines Sequence', help="This field contains the information related to the numbering of the lines of this order.", required=True, ondelete='cascade'),
}
def create_sequence(self, cr, uid, vals, context=None):
"""
Create new entry sequence for every new order
@param cr: cursor to database
@param user: id of current user
@param ids: list of record ids to be process
@param context: context arguments, like lang, time zone
@return: return a result
"""
seq_pool = self.pool.get('ir.sequence')
seq_typ_pool = self.pool.get('ir.sequence.type')
name = 'Supplier catalogue'
code = 'supplier.catalogue'
types = {
'name': name,
'code': code
}
seq_typ_pool.create(cr, uid, types)
seq = {
'name': name,
'code': code,
'prefix': '',
'padding': 0,
}
return seq_pool.create(cr, uid, seq)
def create(self, cr, uid, vals, context=None):
'''
create from purchase_order
create the sequence for the numbering of the lines
'''
vals.update({'sequence_id': self.create_sequence(cr, uid, vals, context)})
return super(supplier_catalogue, self).create(cr, uid, vals, context)
supplier_catalogue()
class supplier_catalogue_line(osv.osv):
'''
override of purchase_order_line class
'''
_inherit = 'supplier.catalogue.line'
_description = 'Supplier Catalogue Line'
_columns = {
'line_number': fields.integer(string='Line', required=True),
}
_order = 'line_number'
def create(self, cr, uid, vals, context=None):
'''
_inherit = 'supplier.catalogue.line'
add the corresponding line number
'''
if self._name != 'supplier.catalogue.merged.line':
# gather the line number from the sale order sequence
order = self.pool.get('supplier.catalogue').browse(cr, uid, vals['catalogue_id'], context)
sequence = order.sequence_id
line = sequence.get_id(code_or_id='id', context=context)
vals.update({'line_number': line})
# create the new sale order line
result = super(supplier_catalogue_line, self).create(cr, uid, vals, context=context)
return result
supplier_catalogue_line()
class ir_sequence(osv.osv):
'''
override of ir_sequence from account as of a bug when the id is a list
'''
_inherit = 'ir.sequence'
def get_id(self, cr, uid, sequence_id, code_or_id='id', context=None):
'''
correct a bug as sequence_id is passed as an array, which
is not taken into account in the override in account
'''
if not context:
context = {}
if isinstance(sequence_id, list):
return super(ir_sequence, self).get_id(cr, uid, sequence_id[0], code_or_id, context=context)
return super(ir_sequence, self).get_id(cr, uid, sequence_id, code_or_id, context=context)
def _get_instance(self, cr, uid):
company = self.pool.get('res.users').browse(cr, uid, uid).company_id
return company and hasattr(company, 'instance_id') and company.instance_id and company.instance_id.po_fo_cost_center_id and company.instance_id.po_fo_cost_center_id.code or ''
def _get_instance_code(self, cr, uid):
company = self.pool.get('res.users').browse(cr, uid, uid).company_id
return company and hasattr(company, 'instance_id') and company.instance_id and company.instance_id.code or ''
def _get_hqcode(self, cr, uid):
company = self.pool.get('res.users').browse(cr, uid, uid).company_id
parent_id = company and hasattr(company, 'instance_id') and company.instance_id and company.instance_id.parent_id or False
code = company and hasattr(company, 'instance_id') and company.instance_id and company.instance_id.po_fo_cost_center_id and company.instance_id.po_fo_cost_center_id.code or ''
parent_seen = []
# UFTP-341: If it's an HQ instance, then also take the code of prop instance as the prefix of FO PO
if parent_id is False:
code = company and hasattr(company, 'instance_id') and company.instance_id and company.instance_id.code
while parent_id:
code = parent_id.po_fo_cost_center_id and parent_id.po_fo_cost_center_id.code or ''
hq_instance_code = parent_id.code
parent_id = parent_id.parent_id or False
if parent_id in parent_seen:
raise osv.except_osv(_('Error'), _('Loop detected in Proprietary Instance tree, you should have a top level instance without any parent.'))
# UFTP-341: When it come to HQ code, just take the prop instance code instead of the cost center code, to avoid having same code for different HQs
if parent_id is False:
code = hq_instance_code
parent_seen.append(parent_id)
return code
def _process(self, cr, uid, s):
data = {
'year':time.strftime('%Y'),
'month': time.strftime('%m'),
'day':time.strftime('%d'),
'y': time.strftime('%y'),
'doy': time.strftime('%j'),
'woy': time.strftime('%W'),
'weekday': time.strftime('%w'),
'h24': time.strftime('%H'),
'h12': time.strftime('%I'),
'min': time.strftime('%M'),
'sec': time.strftime('%S'),
}
if s and '%(instance)s' in s:
data['instance'] = self._get_instance(cr, uid)
if s and '%(hqcode)s' in s:
data['hqcode'] = self._get_hqcode(cr, uid)
if s and '%(instance_code)s' in s:
data['instance_code'] = self._get_instance_code(cr, uid)
return (s or '') % data
ir_sequence()
|