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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
|
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 TeMPO Consulting, MSF
#
# 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/>.
#
##############################################################################
import netsvc
import time
from osv import osv, fields
from osv.orm import browse_record
from tools.translate import _
from order_types.stock import check_rw_warning
class stock_picking_processing_info(osv.osv_memory):
_name = 'stock.picking.processing.info'
_columns = {
'picking_id': fields.many2one(
'stock.picking',
string='Picking',
required=True,
readonly=True,
),
'progress_line': fields.char(
size=64,
string='Processing of lines',
readonly=True,
),
'create_invoice': fields.char(
size=64,
string='Create invoice',
readonly=True,
),
'create_bo': fields.char(
size=64,
string='Create Backorder',
readonly=True,
),
'close_in': fields.char(
size=64,
string='Close IN',
readonly=True,
),
'prepare_pick': fields.char(
size=64,
string='Prepare picking ticket',
readonly=True,
),
'start_date': fields.datetime(
string='Date start',
readonly=True,
),
'end_date': fields.datetime(
string='Date end',
readonly=True,
),
'error_msg': fields.text(
string='Error',
readonly=True,
),
}
_defaults = {
'progress_line': _('Not started'),
'create_invoice': _('Not started'),
'create_bo': _('Not started'),
'close_in': _('Not started'),
'prepare_pick': _('Not started'),
'end_date': False,
}
def refresh(self, cr, uid, ids, context=None):
'''
Just refresh the current page
'''
return {
'type': 'ir.actions.act_window',
'res_model': self._name,
'res_id': ids[0],
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
'context': context,
}
def close(self, cr, uid, ids, context=None):
'''
Just close the page
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
mem_brw = self.browse(cr, uid, ids[0], context=context)
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'view_picking_in_form')[1]
return {'type': 'ir.actions.act_window_close'}
def reset_incoming(self, cr, uid, ids, context=None):
'''
Delete the processing wizard and close the page
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = self.close(cr, uid, ids, context=context)
self.unlink(cr, uid, ids, context=context)
return res
stock_picking_processing_info()
class stock_move(osv.osv):
'''
new function to get mirror move
'''
_inherit = 'stock.move'
_columns = {'line_number': fields.integer(string='Line', required=True),
'change_reason': fields.char(string='Change Reason', size=1024, readonly=True),
'in_out_updated': fields.boolean(string='IN update OUT'),
'original_qty_partial': fields.integer(string='Original Qty for Partial process - only for sync and partial processed line', required=False),
}
_defaults = {
'line_number': 0,
'in_out_updated': False,
'original_qty_partial': -1,
}
_order = 'line_number, date_expected desc, id'
def copy_data(self, cr, uid, id, defaults=None, context=None):
'''
If the line_number is not in the defaults, we set it to False.
If we are on an Incoming Shipment: we reset purchase_line_id field
and we set the location_dest_id to INPUT.
'''
if defaults is None:
defaults = {}
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 defaults and not context.get('keepLineNumber', False):
defaults.update({'line_number': False})
# the tag 'from_button' was added in the web client (openerp/controllers/form.py in the method duplicate) on purpose
if context.get('from_button'):
# UF-1797: when we duplicate a doc we delete the link with the poline
defaults.update(purchase_line_id=False)
if context.get('subtype', False) == 'incoming':
# we reset the location_dest_id to 'INPUT' for the 'incoming shipment'
input_loc = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'msf_cross_docking', 'stock_location_input')[1]
defaults.update(location_dest_id=input_loc)
return super(stock_move, self).copy_data(cr, uid, id, defaults, 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_not_wkf_ids = self.allow_resequencing(cr, uid, ids, context=context)
tools_obj.reorder_sequence_number_from_unlink(
cr,
uid,
draft_not_wkf_ids,
'stock.picking',
'move_sequence_id',
'stock.move',
'picking_id',
'line_number',
context=context,
)
return super(stock_move, 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 Picking + Picking draft + not linked to Po/Fo
'''
# objects
pick_obj = self.pool.get('stock.picking')
resequencing_ids = [x.id for x in self.browse(cr, uid, ids, context=context)
if x.picking_id and pick_obj.allow_resequencing(cr, uid, x.picking_id, context=context)]
return resequencing_ids
def _create_chained_picking_move_values_hook(self, cr, uid, context=None, *args, **kwargs):
'''
Please copy this to your module's method also.
This hook belongs to the action_process method from stock>stock.py>stock_picking
- set the line number of the original picking, could have used the keepLineNumber flag, but used hook to modify original class minimally
'''
if context is None:
context = {}
move_data = super(stock_move, self)._create_chained_picking_move_values_hook(cr, uid, context=context, *args, **kwargs)
# get move reference
move = kwargs['move']
# set the line number from original stock move
move_data.update({'line_number': move.line_number})
return move_data
def _get_location_for_internal_request(self, cr, uid, context=None, **kwargs):
'''
Get the requestor_location_id in case of IR to update the location_dest_id of each move
'''
location_dest_id = super(stock_move, self)._get_location_for_internal_request(cr, uid, context=context, **kwargs)
move = kwargs['move']
if move.purchase_line_id:
proc = move.purchase_line_id.procurement_id
if proc and proc.sale_order_line_ids and proc.sale_order_line_ids[0].order_id and proc.sale_order_line_ids[0].order_id.procurement_request:
location_dest_id = proc.sale_order_line_ids[0].order_id.location_requestor_id.id
return location_dest_id
def _do_partial_hook(self, cr, uid, ids, context, *args, **kwargs):
'''
hook to update defaults data
- update the line number, keep original line number
>> performed for all cases (too few (copy - new numbering policy), complete (simple update - no impact), to many (simple update - no impact)
'''
# variable parameters
move = kwargs.get('move', False)
assert move, 'delivery_mechanism.py >> stock_move: _do_partial_hook - missing move'
# calling super method
defaults = super(stock_move, self)._do_partial_hook(cr, uid, ids, context, *args, **kwargs)
assert defaults is not None, 'delivery_mechanism.py >> stock_move: _do_partial_hook - missing defaults'
# update the line number, copy original line_number value
defaults.update({'line_number': move.line_number})
return defaults
def get_mirror_move(self, cr, uid, ids, data_back, context=None):
'''
return a dictionary with IN for OUT and OUT for IN, if exists, False otherwise
only one mirror object should exist for each object (to check)
return objects which are not done
same sale_line_id/purchase_line_id - same product - same quantity
IN: move -> po line -> procurement -> so line -> move
OUT: move -> so line -> procurement -> po line -> move
I dont use move.move_dest_id because of back orders both on OUT and IN sides
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# objects
so_line_obj = self.pool.get('sale.order.line')
res = {}
for obj in self.browse(cr, uid, ids, context=context):
res[obj.id] = {'move_id': False, 'picking_id': False, 'picking_version': 0, 'quantity': 0, 'moves': []}
if obj.picking_id and obj.picking_id.type == 'in':
# we are looking for corresponding OUT move from sale order line
if obj.purchase_line_id:
# linekd to a po
if obj.purchase_line_id.procurement_id:
# on order
procurement_id = obj.purchase_line_id.procurement_id.id
# find the corresponding sale order line
so_line_ids = so_line_obj.search(cr, uid, [('procurement_id', '=', procurement_id)], context=context)
# if the procurement comes from replenishment rules, there will be a procurement, but no associated sale order line
# we therefore do not raise an exception, but handle the case only if sale order lines are found
if so_line_ids:
# find the corresponding OUT move
move_ids = self.search(cr, uid, [('product_id', '=', data_back['product_id']),
('state', 'in', ('assigned', 'confirmed')),
('sale_line_id', '=', so_line_ids[0]),
('in_out_updated', '=', False),
('picking_id.type', '=', 'out'),
('processed_stock_move', '=', False),
], order="state desc", context=context)
# list of matching out moves
integrity_check = []
for move in self.browse(cr, uid, move_ids, context=context):
pick = move.picking_id
cond1 = move.picking_id.subtype == 'standard'
cond2 = move.product_qty != 0.00 and pick.subtype == 'picking' and not pick.backorder_id and pick.state == 'draft'
# move from draft picking or standard picking
if cond2 or cond1:
integrity_check.append(move)
# return the first one matching
if integrity_check:
if all([not move.processed_stock_move for move in integrity_check]):
# the out stock moves (draft picking or std out) have not yet been processed, we can therefore update them
res[obj.id].update({
'move_id': integrity_check[0].id,
'moves': integrity_check,
'picking_id': integrity_check[0].picking_id.id,
'picking_version': integrity_check[0].picking_id.update_version_from_in_stock_picking,
'quantity': integrity_check[0].product_qty,
})
else:
# the corresponding OUT move have been processed completely or partially,, we do not update the OUT
msg_log = _('The Stock Move %s from %s has already been processed and is '
'therefore not updated.') % (integrity_check[0].name, integrity_check[0].picking_id.name)
self.log(cr, uid, integrity_check[0].id, msg_log)
else:
# we are looking for corresponding IN from on_order purchase order
assert False, 'This method is not implemented for OUT or Internal moves'
return res
def create_data_back(self, move):
'''
build data_back dictionary
'''
res = {'id': move.id,
'name': move.product_id.partner_ref,
'product_id': move.product_id.id,
'product_uom': move.product_uom.id,
'product_qty': move.product_qty,
}
return res
def hook__create_chained_picking(self, cr, uid, pick_values, picking):
res = super(stock_move, self).hook__create_chained_picking(cr, uid, pick_values, picking)
if picking:
res['auto_picking'] = picking.type == 'in' and picking.move_lines[0]['direct_incoming']
return res
stock_move()
class stock_picking(osv.osv):
'''
do_partial modification
'''
_inherit = 'stock.picking'
def _get_progress_memory(self, cr, uid, ids, field_name, args, context=None):
'''
Returns the ID of the stock.picking.processing.info osv_memory linked
to the picking.
'''
mem_obj = self.pool.get('stock.picking.processing.info')
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = {}
for pick_id in ids:
res[pick_id] = {
'progress_memory': -1,
'progress_memory_not_done': 0,
'progress_memory_error': 0,
}
mem_ids = mem_obj.search(cr, uid, [
('picking_id', '=', pick_id),
('picking_id.state', '!=', 'done'),
], order='start_date desc', context=context)
if mem_ids:
res[pick_id]['progress_memory'] = mem_ids[0]
nd_ids = mem_obj.search(cr, uid, [
('end_date', '=', False),
('id', 'in', mem_ids),
], context=context)
if nd_ids:
res[pick_id]['progress_memory_not_done'] = 1
for nd in mem_obj.read(cr, uid, nd_ids, ['error_msg'], context=context):
if nd['error_msg']:
res[pick_id]['progress_memory_error'] = 1
break
return res
_columns = {
'move_sequence_id': fields.many2one(
'ir.sequence',
string='Moves Sequence',
help="This field contains the information related to the numbering of the moves of this picking.",
required=True,
ondelete='cascade',
),
'change_reason': fields.char(
string='Change Reason',
size=1024,
readonly=True,
),
'progress_memory': fields.function(
_get_progress_memory,
method=True,
string='Has processing info',
type='integer',
store=False,
readonly=True,
multi='process',
),
'progress_memory_not_done': fields.function(
_get_progress_memory,
method=True,
string='Is in progress',
type='boolean',
store=False,
readonly=True,
multi='process',
),
'progress_memory_error': fields.function(
_get_progress_memory,
method=True,
string='Processing error',
type='boolean',
store=False,
readonly=True,
multi='process',
),
'in_dpo': fields.boolean(
string='Incoming shipment of a DPO',
readonly=True,
),
}
def go_to_processing_wizard(self, cr, uid, ids, context=None):
'''
If the stock.picking has a stock.picking.processing.info linked to it,
open this wizard.
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
for picking in self.browse(cr, uid, ids, context=context):
if picking.progress_memory > 0:
return {
'type': 'ir.actions.act_window',
'res_model': 'stock.picking.processing.info',
'res_id': int(picking.progress_memory),
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
'context': context,
}
raise osv.except_osv(
_('Error'),
_('The picking has no processing in progress or is already processed'),
)
def _stock_picking_action_process_hook(self, cr, uid, ids, context=None, *args, **kwargs):
'''
Please copy this to your module's method also.
This hook belongs to the action_process method from stock>stock.py>stock_picking
- allow to modify the data for wizard display
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = super(stock_picking, self)._stock_picking_action_process_hook(cr, uid, ids, context=context, *args, **kwargs)
wizard_obj = self.pool.get('wizard')
res = wizard_obj.open_wizard(cr, uid, ids, w_type='update', context=dict(context,
wizard_ids=[res['res_id']],
wizard_name=res['name'],
model=res['res_model'],
step='default'))
return res
def create(self, cr, uid, vals, context=None):
'''
create the sequence for the numbering of the lines
'''
if not vals:
vals = {}
# object
seq_pool = self.pool.get('ir.sequence')
po_obj = self.pool.get('purchase.order')
new_seq_id = self.create_sequence(cr, uid, vals, context=context)
vals.update({'move_sequence_id': new_seq_id, })
# if from order, we udpate the sequence to match the order's one
# line number correspondance to be checked with Magali
# I keep that code deactivated, as when the picking is wkf, hide_new_button must always be true
seq_value = False
if vals.get('purchase_id') and False:
seq_id = po_obj.read(cr, uid, [vals.get('purchase_id')], ['sequence_id'], context=context)[0]['sequence_id'][0]
seq_value = seq_pool.read(cr, uid, [seq_id], ['number_next'], context=context)[0]['number_next']
elif vals.get('sale_id') and False:
seq_id = po_obj.read(cr, uid, [vals.get('sale_id')], ['sequence_id'], context=context)[0]['sequence_id'][0]
seq_value = seq_pool.read(cr, uid, [seq_id], ['number_next'], context=context)[0]['number_next']
if seq_value:
# update sequence value of stock picking to match order's one
seq_pool.write(cr, uid, [new_seq_id], {'number_next': seq_value, })
return super(stock_picking, self).create(cr, uid, vals, context=context)
def allow_resequencing(self, cr, uid, pick_browse, context=None):
'''
allow resequencing criteria
'''
if pick_browse.state == 'draft' and not pick_browse.purchase_id and not pick_browse.sale_id:
return True
return False
def _do_partial_hook(self, cr, uid, ids, context, *args, **kwargs):
'''
hook to update defaults data
'''
# variable parameters
move = kwargs.get('move')
assert move, 'delivery_mechanism.py >> stock_picking: _do_partial_hook - missing move'
# calling super method
defaults = super(stock_picking, self)._do_partial_hook(cr, uid, ids, context, *args, **kwargs)
assert defaults is not None, 'delivery_mechanism.py >> stock_picking: _do_partial_hook - missing defaults'
# update the line number, copy original line_number value
defaults.update({'line_number': move.line_number})
# UTP-972: Set the original total qty of the original move to the new partial move, for sync purpose only
orig_qty = move.product_qty
if move.original_qty_partial and move.original_qty_partial != -1:
orig_qty = move.original_qty_partial
defaults.update({'original_qty_partial': orig_qty})
return defaults
def _update_mirror_move(self, cr, uid, ids, data_back, diff_qty, out_move=False, context=None):
'''
update the mirror move with difference quantity diff_qty
if out_move is provided, it is used for copy if another cannot be found (meaning the one provided does
not fit anyhow)
# NOTE: the price is not update in OUT move according to average price computation. this is an open point.
if diff_qty < 0, the qty is decreased
if diff_qty > 0, the qty is increased
'''
# stock move object
move_obj = self.pool.get('stock.move')
# first look for a move - we search even if we get out_move because out_move
# may not be valid anymore (product changed) - get_mirror_move will validate it or return nothing
out_move_id = move_obj.get_mirror_move(cr, uid, [data_back['id']], data_back, context=context)[data_back['id']]['move_id']
if not out_move_id and out_move:
# copy existing out_move with move properties: - update the name of the stock move
# the state is confirmed, we dont know if available yet - should be in input location before stock
values = {'name': data_back['name'],
'product_id': data_back['product_id'],
'product_qty': 0,
'product_uos_qty': 0,
'product_uom': data_back['product_uom'],
'state': 'confirmed',
'prodlot_id': False, # reset batch number
'asset_id': False, # reset asset
}
out_move_id = move_obj.copy(cr, uid, out_move, values, context=context)
# update quantity
if out_move_id:
# decrease/increase depending on diff_qty sign the qty by diff_qty
data = move_obj.read(cr, uid, [out_move_id], ['product_qty', 'picking_id', 'name', 'product_uom'], context=context)[0]
picking_out_name = data['picking_id'][1]
stock_move_name = data['name']
uom_name = data['product_uom'][1]
present_qty = data['product_qty']
new_qty = max(present_qty + diff_qty, 0)
if new_qty > 0.00 and present_qty != 0.00:
new_move_id = move_obj.copy(cr, uid, out_move_id, {'product_qty': diff_qty,
'product_uom': data_back['product_uom'],
'product_uos': data_back['product_uom'],
'product_uos_qty': diff_qty, }, context=context)
move_obj.action_confirm(cr, uid, [new_move_id], context=context)
# if present_qty == 0.00:
# move_obj.write(cr, uid, [out_move_id], {'state': 'draft'})
# move_obj.unlink(cr, uid, out_move_id, context=context)
else:
move_obj.write(cr, uid, [out_move_id], {'product_qty': new_qty,
'product_uom': data['product_uom'][0],
'product_uos': data['product_uom'][0],
'product_uos_qty': new_qty, }, context=context)
# log the modification
# log creation message
msg_log = _('The Stock Move %s from %s has been updated to %s %s.') % (stock_move_name, picking_out_name, new_qty, uom_name)
move_obj.log(cr, uid, out_move_id, msg_log)
# return updated move or False
return out_move_id
def _do_incoming_shipment_first_hook(self, cr, uid, ids, context=None, *args, **kwargs):
'''
hook to update values for stock move if first encountered
'''
values = kwargs.get('values')
assert values is not None, 'missing values'
return values
def _get_db_data_dict(self, cr, uid):
"""
Get some data from data.xml file (like stock locations, Unifield setup...)
"""
# Objects
data_obj = self.pool.get('ir.model.data')
loc_obj = self.pool.get('stock.location')
setup = self.pool.get('unifield.setup.configuration').get_config(cr, uid)
cd_loc = loc_obj.get_cross_docking_location(cr, uid)
service_loc = loc_obj.get_service_location(cr, uid)
non_stock = loc_obj.search(cr, uid, [('non_stockable_ok', '=', True)])
if non_stock:
non_stock = non_stock[0]
input_loc = data_obj.get_object_reference(cr, uid, 'msf_cross_docking', 'stock_location_input')[1]
db_data = {
'setup': setup,
'cd_loc': cd_loc,
'service_loc': service_loc,
'non_stock': non_stock,
'input_loc': input_loc
}
return db_data
def _compute_average_values(self, cr, uid, move, line, product_availability, context=None):
"""
Compute the average price of the product according to processed quantities
"""
# Objects
uom_obj = self.pool.get('product.uom')
currency_obj = self.pool.get('res.currency')
product_obj = self.pool.get('product.product')
if context is None:
context = {}
average_values = {}
move_currency_id = move.company_id.currency_id.id
context['currency_id'] = move_currency_id
qty = line.quantity
if line.uom_id.id != line.product_id.uom_id.id:
qty = uom_obj._compute_qty(cr, uid, line.uom_id.id, line.quantity, line.product_id.uom_id.id)
product_availability.setdefault(line.product_id.id, line.product_id.qty_available)
if qty > 0.00:
new_price = line.cost
# Recompute unit price if the currency used is not the functional currency
if line.currency.id != move_currency_id:
new_price = currency_obj.compute(cr, uid, line.currency.id, move_currency_id,
new_price, round=False, context=context)
# Recompute unit price if the UoM received is not the default UoM of the product
if line.uom_id.id != line.product_id.uom_id.id:
new_price = uom_obj._compute_price(cr, uid, line.uom_id.id, new_price,
line.product_id.uom_id.id)
new_std_price = 0.00
if line.product_id.qty_available <= 0.00:
new_std_price = new_price
else:
# Get the current price
current_price = product_obj.price_get(cr, uid, [line.product_id.id], 'standard_price', context=context)[line.product_id.id]
# Check no division by zero
if product_availability[line.product_id.id]:
new_std_price = ((current_price * product_availability[line.product_id.id])
+ (new_price * qty)) / (product_availability[line.product_id.id] + qty)
# Write the field according to price type field
product_obj.write(cr, uid, [line.product_id.id], {'standard_price': new_std_price})
# Record the values that were chosen in the wizard, so they can be
# used for inventory valuation of real-time valuation is enabled.
average_values = {
'price_unit': new_price,
'price_currency_id': line.currency.id,
}
return average_values
def _get_values_from_line(self, cr, uid, move, line, db_data, context=None):
"""
Prepare the value for a processed move according to line values
"""
# Objects
uom_obj = self.pool.get('product.uom')
pol_obj = self.pool.get('purchase.order.line')
sol_obj = self.pool.get('sale.order.line')
if context is None:
context = {}
wizard = line.wizard_id
values = {
'name': line.product_id.partner_ref,
'product_id': line.product_id.id,
'original_qty_partial': move.product_qty,
'product_qty': line.quantity,
'product_uom': line.uom_id.id,
'product_uos_qty': line.quantity,
'product_uos': line.uom_id.id,
'prodlot_id': line.prodlot_id and line.prodlot_id.id or False,
'asset_id': line.asset_id and line.asset_id.id or False,
'change_reason': line.change_reason,
# Values from incoming wizard
'direct_incoming': line.wizard_id.direct_incoming,
# Values for Direct Purchase Order
'sync_dpo': move.dpo_line_id and True or move.sync_dpo,
'dpo_line_id': False,
}
if move.dpo_line_id:
if isinstance(move.dpo_line_id, int):
values['dpo_line_id'] = move.dpo_line_id
else:
values['dpo_line_id'] = move.dpo_line_id.id
# UTP-872: Don't change the quantity if the move is canceled
# If the quantity is changed to 0.00, a backorder is created
# for canceled moves
if move.state == 'cancel':
values.update({
'product_qty': move.product_qty,
'product_uos_qty': move.product_uos_qty
})
# UTP-872: Added also the state into the move line if the state comes from the sync
if line.state:
values['state'] = line.state
if line.cost:
values['price_unit'] = line.cost
elif line.uom_id.id != move.product_uom.id:
new_price = uom_obj._compute_price(cr, uid, move.product_uom.id, move.price_unit, line.uom_id.id)
values['price_unit'] = new_price
service_non_stock_ok = False
if move.purchase_line_id and line.product_id.type in ('consu', 'service_recept'):
sol_ids = pol_obj.get_sol_ids_from_pol_ids(cr, uid, [move.purchase_line_id.id], context=context)
for sol_brw in sol_obj.browse(cr, uid, sol_ids, context=context):
if sol_brw.order_id.procurement_request:
service_non_stock_ok = True
# We check the dest_type for INCOMING shipment (and not the source_type which is reserved for OUTGOING shipment)
if wizard.dest_type == 'to_cross_docking' and not service_non_stock_ok:
if db_data.get('setup').allocation_setup == 'unallocated':
raise osv.except_osv(
_('Error'),
_('You cannot made moves from/to Cross-docking locations when the Allocated stocks configuration is set to \'Unallocated\'.'),
)
# Below, "source_type" is only used for the outgoing shipment. We set it to "None" because by default it is
# "default" and we do not want that info on INCOMING shipment
wizard.source_type = None
values.update({
'location_dest_id': db_data.get('cd_loc'),
'cd_from_bo': True,
})
elif wizard.dest_type == 'to_stock' or service_non_stock_ok:
# Below, "source_type" is only used for the outgoing shipment. We set it to "None because by default it is
# "default" and we do not want that info on INCOMING shipment
if line.product_id.type == 'consu':
values['location_dest_id'] = db_data.get('non_stock')
elif line.product_id.type == 'service_recep':
values['location_dest_id'] = db_data.get('service_loc')
else:
# treat moves towards STOCK if NOT SERVICE
values['location_dest_id'] = db_data.get('input_loc')
values['cd_from_bo'] = False
if wizard.dest_type != 'to_cross_docking':
values['direct_incoming'] = wizard.direct_incoming
return values
def update_processing_info(self, cr, uid, picking, prog_id=False, values=None, context=None):
'''
Update the osv_memory processing info object linked to picking ID.
:param cr: Cursor to the database
:param uid: ID of the user that calls the method
:param picking: browse_record of a stock.picking or the ID of a
stock.picking
:param prog_id: ID of a stock.picking.processing.info to update
:param values: Dictoionary that contains the values to put on picking
processing object
:param context: Context of the call
:return: The ID of the stock.picking.processing.info that have been
updated.
'''
prog_obj = self.pool.get('stock.picking.processing.info')
if context is None:
context = {}
if context.get('sync_message_execution', False):
return False
if not prog_id:
if not isinstance(picking, browse_record) and isinstance(picking, (int, long)):
picking = self.browse(cr, uid, picking, context=context)
prog_ids = prog_obj.search(cr, uid, [('picking_id', '=', picking.id)], context=context)
if prog_ids:
prog_id = prog_ids[0]
else:
prog_id = prog_obj.create(cr, uid, {
'picking_id': picking.id,
}, context=context)
if not values:
return prog_id
prog_obj.write(cr, uid, [prog_id], values, context=context)
return prog_id
def do_incoming_shipment_new_cr(self, cr, uid, wizard_ids, context=None):
"""
Call the do_incoming_shipment() method with a new cursor.
"""
inc_proc_obj = self.pool.get('stock.incoming.processor')
# Create new cursor
import pooler
new_cr = pooler.get_db(cr.dbname).cursor()
res = True
try:
# Call do_incoming_shipment()
res = self.do_incoming_shipment(new_cr, uid, wizard_ids, context=context)
new_cr.commit()
except Exception, e:
new_cr.rollback()
for wiz in inc_proc_obj.read(new_cr, uid, wizard_ids, ['picking_id'], context=context):
self.update_processing_info(new_cr, uid, wiz['picking_id'][0], False, {
'error_msg': '%s\n\nPlease reset the incoming shipment '\
'processing and fix the source of the error'\
'before re-try the processing.' % str(e),
}, context=context)
finally:
# Close the cursor
new_cr.close()
return res
def do_incoming_shipment(self, cr, uid, wizard_ids, context=None):
"""
Take the data in wizard_ids and lines of stock.incoming.processor and
do the split of stock.move according to the data.
"""
# Objects
inc_proc_obj = self.pool.get('stock.incoming.processor')
move_proc_obj = self.pool.get('stock.move.in.processor')
loc_obj = self.pool.get('stock.location')
uom_obj = self.pool.get('product.uom')
move_obj = self.pool.get('stock.move')
sequence_obj = self.pool.get('ir.sequence')
cur_obj = self.pool.get('res.currency')
wf_service = netsvc.LocalService("workflow")
usb_entity = self._get_usb_entity_type(cr, uid)
if context is None:
context = {}
if isinstance(wizard_ids, (int, long)):
wizard_ids = [wizard_ids]
db_data_dict = self._get_db_data_dict(cr, uid)
# UF-1617: Get the sync_message case
sync_in = context.get('sync_message_execution', False)
if context.get('rw_sync', False):
sync_in = False
in_out_updated = True
if sync_in:
in_out_updated = False
backorder_id = False
internal_loc = loc_obj.search(cr, uid, [('usage', '=', 'internal'), ('cross_docking_location_ok', '=', False)])
proc_loc_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'location_procurement')[1]
context['location'] = internal_loc
product_availability = {}
picking_ids = []
for wizard in inc_proc_obj.browse(cr, uid, wizard_ids, context=context):
picking = wizard.picking_id
picking_ids.append(picking)
backordered_moves = [] # Moves that need to be put in a backorder
done_moves = [] # Moves that are completed
out_picks = set()
processed_out_moves = []
total_moves = len(picking.move_lines)
move_done = 0
prog_id = self.update_processing_info(cr, uid, picking, False, {
'progress_line': _('In progress (%s/%s)') % (move_done, total_moves),
'start_date': time.strftime('%Y-%m-%d %H:%M:%S')
}, context=context)
for move in picking.move_lines:
move_done += 1
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'progress_line': _('In progress (%s/%s)') % (move_done, total_moves),
}, context=context)
# Get all processed lines that processed this stock move
proc_ids = move_proc_obj.search(cr, uid, [('wizard_id', '=', wizard.id), ('move_id', '=', move.id)], context=context)
# The processed quantity
count = 0
need_split = False
data_back = move_obj.create_data_back(move)
mirror_data = move_obj.get_mirror_move(cr, uid, [move.id], data_back, context=context)[move.id]
out_moves = mirror_data['moves']
average_values = {}
for line in move_proc_obj.browse(cr, uid, proc_ids, context=context):
values = self._get_values_from_line(cr, uid, move, line, db_data_dict, context=context)
if not values.get('product_qty', 0.00):
continue
# Check if we must re-compute the price of the product
compute_average = picking.type == 'in' and line.product_id.cost_method
if values.get('location_dest_id', False):
dest_loc = loc_obj.browse(cr, uid, values['location_dest_id'], context=context)
compute_average = picking.type == 'in' and line.product_id.cost_method == 'average'
if compute_average:
average_values = self._compute_average_values(cr, uid, move, line, product_availability, context=context)
values.update(average_values)
# The quantity
if line.uom_id.id != move.product_uom.id:
count += uom_obj._compute_qty(cr, uid, line.uom_id.id, line.quantity, move.product_uom.id)
else:
count += line.quantity
values['processed_stock_move'] = True
if not need_split:
need_split = True
# Mark the done IN stock move as processed
move_obj.write(cr, uid, [move.id], values, context=context)
done_moves.append(move.id)
else:
values['state'] = 'assigned'
context['keepLineNumber'] = True
new_move_id = move_obj.copy(cr, uid, move.id, values, context=context)
context['keepLineNumber'] = False
done_moves.append(new_move_id)
values['processed_stock_move'] = False
out_values = values.copy()
# Remove sync. DPO fields
out_values.update({
'dpo_line_id': 0,
'sync_dpo': False,
'state': 'confirmed',
})
if out_values.get('location_dest_id', False):
out_values.pop('location_dest_id')
remaining_out_qty = line.quantity
out_move = None
# Sort the OUT moves to get the closest quantities as the IN quantity
out_moves = sorted(out_moves, key=lambda x: abs(x.product_qty-line.quantity))
for lst_out_move in out_moves:
if remaining_out_qty <= 0.00:
break
out_move = move_obj.browse(cr, uid, lst_out_move.id, context=context)
if values.get('price_unit', False) and out_move.price_currency_id.id != move.price_currency_id.id:
price_unit = cur_obj.compute(
cr,
uid,
move.price_currency_id.id,
out_move.price_currency_id.id,
values.get('price_unit'),
round=False,
context=context
)
out_values['price_unit'] = price_unit
# List the Picking Ticket that need to be created from the Draft Picking Ticket
if out_move.picking_id.type == 'out' \
and out_move.picking_id.subtype == 'picking' \
and out_move.picking_id.state == 'draft':
out_picks.add(out_move.picking_id.id)
if line.uom_id.id != out_move.product_uom.id:
uom_partial_qty = uom_obj._compute_qty(cr, uid, line.uom_id.id, remaining_out_qty, out_move.product_uom.id)
else:
uom_partial_qty = remaining_out_qty
# Manage OUT BO moves already processed (forced)
bo_moves = []
minus_qty = 0.00
if out_move.picking_id and out_move.picking_id.backorder_id:
bo_moves = move_obj.search(cr, uid, [
('picking_id', '=', out_move.picking_id.backorder_id.id),
('sale_line_id', '=', out_move.sale_line_id.id),
('state', '=', 'done'),
], context=context)
while bo_moves:
boms = move_obj.browse(cr, uid, bo_moves, context=context)
bo_moves = []
for bom in boms:
if bom.product_uom.id != out_move.product_uom.id:
minus_qty += uom_obj._compute_qty(cr, uid, bom.product_uom.id, bom.product_qty, out_move.product_uom.id)
else:
minus_qty += bom.product_qty
if bom.picking_id and bom.picking_id.backorder_id:
bo_moves.extend(move_obj.search(cr, uid, [
('picking_id', '=', bom.picking_id.backorder_id.id),
('sale_line_id', '=', bom.sale_line_id.id),
('state', '=', 'done'),
], context=context))
if uom_partial_qty < out_move.product_qty:
# Splt the out move
out_values.update({
'product_qty': remaining_out_qty,
'product_uom': line.uom_id.id,
'in_out_updated': in_out_updated,
})
context['keepLineNumber'] = True
new_out_move_id = move_obj.copy(cr, uid, out_move.id, out_values, context=context)
context['keepLineNumber'] = False
remaining_out_qty = 0.00
move_obj.write(cr, uid, [out_move.id], {
'product_qty': out_move.product_qty - uom_partial_qty,
'product_uos_qty': out_move.product_qty - uom_partial_qty,
}, context=context)
processed_out_moves.append(new_out_move_id)
elif uom_partial_qty == out_move.product_qty:
out_values.update({
'product_qty': remaining_out_qty,
'product_uom': line.uom_id.id,
'in_out_updated': in_out_updated,
})
remaining_out_qty = 0.00
move_obj.write(cr, uid, [out_move.id], out_values, context=context)
processed_out_moves.append(out_move.id)
elif uom_partial_qty > out_move.product_qty and out_moves[out_moves.index(out_move)] != out_moves[-1]:
# Just update the out move with the value of the out move with UoM of IN
out_qty = out_move.product_qty
if line.uom_id.id != out_move.product_uom.id:
out_qty = uom_obj._compute_qty(cr, uid, out_move.product_uom.id, out_move.product_qty, line.uom_id.id)
out_values.update({
'product_qty': out_qty,
'product_uom': line.uom_id.id,
'in_out_updated': in_out_updated,
})
remaining_out_qty -= out_qty
move_obj.write(cr, uid, [out_move.id], out_values, context=context)
processed_out_moves.append(out_move.id)
else:
# Just update the data of the initial out move
processed_qty = lst_out_move is out_moves[-1] and uom_partial_qty - minus_qty or out_move.product_qty
out_values.update({
'product_qty': processed_qty,
'product_uom': line.uom_id.id,
'in_out_updated': in_out_updated,
})
move_obj.write(cr, uid, [out_move.id], out_values, context=context)
processed_out_moves.append(out_move.id)
if line.uom_id.id != out_move.product_uom.id:
uom_processed_qty = uom_obj._compute_qty(cr, uid, out_move.product_uom.id, processed_qty, line.uom_id.id)
else:
uom_processed_qty = processed_qty
remaining_out_qty -= uom_processed_qty
# Decrement the inital move, cannot be less than zero
diff_qty = move.product_qty - count
# If there is remaining quantity for the move, put the ID of the move
# and the remaining quantity to list of moves to put in backorder
if diff_qty > 0.00 and move.state != 'cancel':
backordered_moves.append((move, diff_qty, average_values, data_back))
# UTP-967
if move.state != 'cancel' and move.purchase_line_id and move.purchase_line_id.procurement_id:
proc = move.purchase_line_id.procurement_id
if proc.move_id and proc.move_id.location_id.id == proc_loc_id:
if diff_qty > 0:
# REF-59: move of partial in,
# adapt proc order's move qty (for correct virtual stock)
move_obj.write(cr, uid, [proc.move_id.id],
{'product_qty': diff_qty}, context=context)
else:
# note: do not close move until a diff qty is applied above
move_obj.write(cr, uid, [proc.move_id.id], {'product_qty': 0.00, 'state': 'done'}, context=context)
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'progress_line': _('Done (%s/%s)') % (move_done, total_moves),
}, context=context)
# Create the backorder if needed
if backordered_moves:
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'create_bo': _('In progress'),
}, context=context)
initial_vals_copy = {
'name':sequence_obj.get(cr, uid, 'stock.picking.%s' % (picking.type)),
'move_lines':[],
'state':'draft',
'in_dpo': context.get('for_dpo', False),
}
if usb_entity == self.REMOTE_WAREHOUSE and not context.get('sync_message_execution', False): # RW Sync - set the replicated to True for not syncing it again
initial_vals_copy.update({
'already_replicated': False,
})
backorder_id = False
if context.get('for_dpo', False) and picking.purchase_id:
# Look for an available IN for the same purchase order in case of DPO
backorder_ids = self.search(cr, uid, [
('purchase_id', '=', picking.purchase_id.id),
('in_dpo', '=', True),
('state', '=', 'assigned'),
], limit=1, context=context)
if backorder_ids:
backorder_id = backorder_ids[0]
if not backorder_id:
backorder_id = self.copy(cr, uid, picking.id, initial_vals_copy, context=context)
back_order_post_copy_vals = {}
if usb_entity == self.CENTRAL_PLATFORM and context.get('rw_backorder_name', False):
new_name = context.get('rw_backorder_name')
del context['rw_backorder_name']
back_order_post_copy_vals['name'] = new_name
if picking.purchase_id:
# US-111: in case of partial reception invoice was not linked to PO
# => analytic_distribution_supply/stock.py _invoice_hook
# picking.purchase_id was False
back_order_post_copy_vals['purchase_id'] = picking.purchase_id.id
if back_order_post_copy_vals:
self.write(cr, uid, backorder_id, back_order_post_copy_vals, context=context)
for bo_move, bo_qty, av_values, data_back in backordered_moves:
if bo_move.product_qty != bo_qty:
# Create the corresponding move in the backorder - reset batch - reset asset_id
bo_values = {
'asset_id': False,
'product_qty': bo_qty,
'product_uos_qty': bo_qty,
'product_uom': data_back['product_uom'],
'product_uos': data_back['product_uom'],
'product_id': data_back['product_id'],
'prodlot_id': False,
'state': 'assigned',
'move_dest_id': False,
'change_reason': False,
'processed_stock_move': True,
'dpo_line_id': bo_move.dpo_line_id,
'purchase_line_id': bo_move.purchase_line_id and bo_move.purchase_line_id.id or False,
}
bo_values.update(av_values)
context['keepLineNumber'] = True
context['from_button'] = False
move_obj.copy(cr, uid, bo_move.id, bo_values, context=context)
context['keepLineNumber'] = False
# Put the done moves in this new picking
done_values = {'picking_id': backorder_id}
if not context.get('for_dpo'):
done_values['dpo_line_id'] = 0
move_obj.write(cr, uid, done_moves, done_values, context=context)
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'create_bo': _('Done'),
'close_in': _('In progress'),
}, context=context)
if sync_in:
# UF-1617: When it is from the sync., then just send the IN to shipped, then return the backorder_id
if context.get('for_dpo', False):
wf_service.trg_validate(uid, 'stock.picking', backorder_id, 'button_confirm', cr)
else:
wf_service.trg_validate(uid, 'stock.picking', backorder_id, 'button_shipped', cr)
return backorder_id
wf_service.trg_validate(uid, 'stock.picking', backorder_id, 'button_confirm', cr)
# Then we finish the good picking
self.write(cr, uid, [picking.id], {'backorder_id': backorder_id,'cd_from_bo': values.get('cd_from_bo', False),}, context=context)
self.action_move(cr, uid, [backorder_id])
wf_service.trg_validate(uid, 'stock.picking', backorder_id, 'button_done', cr)
wf_service.trg_write(uid, 'stock.picking', picking.id, cr)
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'close_in': _('Done'),
}, context=context)
else:
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'create_bo': _('N/A'),
'close_in': _('In progress'),
}, context=context)
if sync_in: # If it's from sync, then we just send the pick to become Available Shippde, not completely close!
if context.get('for_dpo', False):
self.write(cr, uid, [picking.id], {'in_dpo': True}, context=context)
else:
self.write(cr, uid, [picking.id], {'state': 'shipped'}, context=context)
return picking.id
else:
self.action_move(cr, uid, [picking.id], context=context)
wf_service.trg_validate(uid, 'stock.picking', picking.id, 'button_done', cr)
if picking.purchase_id:
so_ids = self.pool.get('purchase.order').get_so_ids_from_po_ids(cr, uid, picking.purchase_id.id, context=context)
for so_id in so_ids:
wf_service.trg_write(uid, 'sale.order', so_id, cr)
if usb_entity == self.REMOTE_WAREHOUSE:
self.write(cr, uid, [picking.id], {'already_replicated': False}, context=context)
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'close_in': _('Done'),
}, context=context)
if not sync_in:
move_obj.action_assign(cr, uid, processed_out_moves)
if not out_picks:
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'prepare_pick': _('N/A'),
}, context=context)
# Create the first picking ticket if we are on a draft picking ticket
for picking in self.browse(cr, uid, list(out_picks), context=context):
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'prepare_pick': _('In progress'),
}, context=context)
wiz = self.create_picking(cr, uid, [picking.id], context=context)
wiz_obj = self.pool.get(wiz['res_model'])
wiz_context = wiz.get('context', {})
moves_picking = wiz_obj.browse(cr, uid, wiz['res_id'], context=wiz_context).move_ids
nb_lines = len(moves_picking)
# We delete the lines which is not from the IN
# for line in moves_picking:
# if line.move_id.id not in pick_moves:
# self.pool.get('stock.move.memeroy.picking').unlink(cr, uid, [line.id], context=context)
# nb_lines -= 1
if nb_lines:
# We copy all data in lines
wiz_obj.copy_all(cr, uid, [wiz['res_id']], context=wiz_context)
#UF-2531: Pass the name of PICKxxx-y when creating a Pick from crossdocking from an IN partial
if context.get('associate_pick_name', False) and context.get('sync_message_execution', False):
wiz_context['associate_pick_name'] = context.get('associate_pick_name')
del context['associate_pick_name']
# We process the creation of the picking
res_wiz = wiz_obj.do_create_picking(cr, uid, [wiz['res_id']], context=wiz_context)
if 'res_id' in res_wiz:
new_pick_id = res_wiz['res_id']
if backorder_id and new_pick_id:
new_pick_name = self.read(cr, uid, new_pick_id, ['name'], context=context)['name']
self.write(cr, uid, backorder_id, {'associate_pick_name': new_pick_name,}, context=context)
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'prepare_pick': _('Done'),
}, context=context)
for picking in picking_ids:
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'end_date': time.strftime('%Y-%m-%d %H:%M:%S')
}, context=context)
# UF-2531: Run the creation of message if it's at RW at some important point
if usb_entity == self.REMOTE_WAREHOUSE and not context.get('sync_message_execution', False):
self._manual_create_rw_messages(cr, uid, context=context)
if context.get('rw_sync', False):
prog_id = self.update_processing_info(cr, uid, picking, prog_id, {
'end_date': time.strftime('%Y-%m-%d %H:%M:%S')
}, context=context)
if backorder_id:
return backorder_id
return wizard.picking_id.id
if context.get('from_simu_screen'):
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'view_picking_in_form')[1]
return {
'type': 'ir.actions.act_window',
'res_model': 'stock.picking',
'res_id': wizard.picking_id.id,
'view_id': [view_id],
'view_mode': 'form, tree',
'view_type': 'form',
'target': 'crush',
'context': context}
return {'type': 'ir.actions.act_window_close'}
def _manual_create_rw_messages(self, cr, uid, context=None):
return
@check_rw_warning
def enter_reason(self, cr, uid, ids, context=None):
'''
open reason wizard
'''
# we need the context for the wizard switch
if context is None:
context = {}
# data
name = _("Enter a Reason for Incoming cancellation")
model = 'enter.reason'
step = 'default'
wiz_obj = self.pool.get('wizard')
# open the selected wizard
return wiz_obj.open_wizard(cr, uid, ids, name=name, model=model, step=step, context=dict(context, picking_id=ids[0]))
def cancel_and_update_out(self, cr, uid, ids, context=None):
'''
update corresponding out picking if exists and cancel the picking
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
# objects
move_obj = self.pool.get('stock.move')
purchase_obj = self.pool.get('purchase.order')
# workflow
wf_service = netsvc.LocalService("workflow")
for obj in self.browse(cr, uid, ids, context=context):
# corresponding sale ids to be manually corrected after purchase workflow trigger
sale_ids = []
for move in obj.move_lines:
data_back = move_obj.create_data_back(move)
diff_qty = -data_back['product_qty']
# update corresponding out move - no move created, no need to handle line sequencing policy
out_move_id = self._update_mirror_move(cr, uid, ids, data_back, diff_qty, out_move=False, context=context)
# for out cancellation, two points:
# - if pick/pack/ship: check that nothing is in progress
# - if nothing in progress, and the out picking is canceled, trigger the so to correct the corresponding so manually
if out_move_id:
out_move = move_obj.browse(cr, uid, out_move_id, context=context)
cond1 = out_move.picking_id.subtype == 'standard'
cond2 = out_move.picking_id.subtype == 'picking'
cond3 = cond2 and out_move.picking_id.has_picking_ticket_in_progress(context=context)[out_move.picking_id.id]
if out_move.picking_id.subtype in ('standard', 'picking') and out_move.picking_id.type == 'out' and not out_move.product_qty:
move_id = False
if out_move.picking_id and out_move.sale_line_id:
# replace the stock move in the procurement order by the non cancelled stock move
sale_id = out_move.picking_id.sale_id.id
move_domain = [
('picking_id.type', '=', 'out'),
('picking_id.subtype', 'in', ('standard', 'picking')),
#('picking_id.sale_id', '=', sale_id),
('sale_line_id', '=', out_move.sale_line_id.id),
('id', '!=', out_move_id),
('processed_stock_move', '=', True),
]
move_domain_not_done = move_domain
move_domain_not_cancel = move_domain
move_domain_not_done.append(('state', 'not in', ['done', 'cancel']))
move_domain_not_cancel.append(('state', '!=', 'cancel'))
move_id = move_obj.search(cr, uid, move_domain_not_done, context=context)
if not move_id:
move_id = move_obj.search(cr, uid, move_domain_not_cancel, context=context)
if move_id:
proc_id = self.pool.get('procurement.order').search(cr, uid, [('move_id', '=', out_move_id)], context=context)
self.pool.get('procurement.order').write(cr, uid, proc_id, {'move_id': move_id[0]}, context=context)
# the corresponding move can be canceled - the OUT picking workflow is triggered automatically if needed
move_obj.action_cancel(cr, uid, [out_move_id], context=context)
# open points:
# - when searching for open picking tickets - we should take into account the specific move (only product id ?)
# - and also the state of the move not in (cancel done)
# correct the corresponding so manually if exists - could be in shipping exception
if out_move.picking_id and out_move.picking_id.sale_id:
if out_move.picking_id.sale_id.id not in sale_ids:
sale_ids.append(out_move.picking_id.sale_id.id)
# UFTP-345: Check if all lines from the original pick are either closed or cancel and qty is 0, then close this PICK
mirror_pick = out_move.picking_id
if mirror_pick and mirror_pick.id:
ptc = self.browse(cr, uid, mirror_pick.id, context=context)
if all(m.product_qty == 0.00 and m.state in ('done', 'cancel') for m in ptc.move_lines):
ptc.action_done(context=context)
elif mirror_pick.subtype == 'picking' and mirror_pick.state == 'draft':
# If there are still some lines available with qty 0, then check if any in progress PICK, if all complete, then close the PICK
self.validate(cr, uid, [mirror_pick.id], context=context)
if move.purchase_line_id and move.purchase_line_id.procurement_id:
procurement = move.purchase_line_id.procurement_id
if not procurement.sale_id and procurement.move_id:
self.pool.get('stock.move').action_cancel(cr, uid, [move.purchase_line_id.procurement_id.move_id.id])
wf_service.trg_validate(uid, 'procurement.order', move.purchase_line_id.procurement_id.id, 'button_cancel', cr)
# correct the corresponding po manually if exists - should be in shipping exception
if obj.purchase_id:
wf_service.trg_validate(uid, 'purchase.order', obj.purchase_id.id, 'picking_ok', cr)
msg_log = _('The Purchase Order %s is %s%% received.') % (obj.purchase_id.name, round(obj.purchase_id.shipped_rate, 2))
purchase_obj.log(cr, uid, obj.purchase_id.id, msg_log)
# correct the corresponding so
for sale_id in sale_ids:
wf_service.trg_write(uid, 'sale.order', sale_id, cr)
wf_service.trg_validate(uid, 'sale.order', sale_id, 'ship_corrected', cr)
return True
def write(self, cr, uid, ids, vals, context=None):
'''
Return True in case of context contains "go_to_processing_wizard" because
without that, the displaying of the wizard waits that the background
process (SQL LOCK on row)
'''
if context is None:
context = {}
if context.get('button', False) == 'go_to_processing_wizard':
return True
elif context.get('button', False) == 'enter_reason':
mem_obj = self.pool.get('stock.picking.processing.info')
mem_ids = mem_obj.search(cr, uid, [
('end_date', '=', False),
('picking_id', 'in', ids),
], context=context)
if mem_ids:
raise osv.except_osv(
_('Error'),
_('The processing of this picking is in progress - You can\'t cancel it.'),
)
return super(stock_picking, self).write(cr, uid, ids, vals, context=context)
stock_picking()
class purchase_order_line(osv.osv):
'''
add the link to procurement order
'''
_inherit = 'purchase.order.line'
_columns = {
'procurement_id': fields.many2one(
'procurement.order',
string='Procurement Reference',
readonly=True,
),
}
_defaults = {'procurement_id': False, }
purchase_order_line()
class procurement_order(osv.osv):
'''
inherit po_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]
sale_obj = self.pool.get('sale.order.line')
line = super(procurement_order, self).po_line_values_hook(cr, uid, ids, context=context, *args, **kwargs)
# give the purchase order line a link to corresponding procurement
procurement = kwargs['procurement']
line.update({'procurement_id': procurement.id, })
# for Internal Request (IR) on make_to_order we update PO line data according to the data of the IR (=sale_order)
sale_order_line_ids = sale_obj.search(cr, uid, [('procurement_id', '=', procurement.id)], context=context)
for sol in sale_obj.browse(cr, uid, sale_order_line_ids, context=context):
if sol.order_id.procurement_request and not sol.product_id and sol.comment:
line.update({'product_id': False,
'name': 'Description: %s' % sol.comment,
'comment': sol.comment,
'product_qty': sol.product_uom_qty,
'price_unit': sol.price_unit,
'date_planned': sol.date_planned,
'product_uom': sol.product_uom.id,
'nomen_manda_0': sol.nomen_manda_0.id,
'nomen_manda_1': sol.nomen_manda_1.id or False,
'nomen_manda_2': sol.nomen_manda_2.id or False,
'nomen_manda_3': sol.nomen_manda_3.id or False,
'nomen_sub_0': sol.nomen_sub_0.id or False,
'nomen_sub_1': sol.nomen_sub_1.id or False,
'nomen_sub_2': sol.nomen_sub_2.id or False,
'nomen_sub_3': sol.nomen_sub_3.id or False,
'nomen_sub_4': sol.nomen_sub_4.id or False,
'nomen_sub_5': sol.nomen_sub_5.id or False})
return line
procurement_order()
|