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
|
# -*- 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/>.
#
##############################################################################
from osv import osv, fields
import time
from tools.translate import _
from dateutil.relativedelta import relativedelta
from datetime import datetime
# import partner_type from msf_partner
from msf_partner import PARTNER_TYPE
from msf_order_date import TRANSPORT_TYPE
from msf_order_date import ZONE_SELECTION
from purchase_override import PURCHASE_ORDER_STATE_SELECTION
from sale_override import SALE_ORDER_STATE_SELECTION
class res_partner(osv.osv):
_name = 'res.partner'
_inherit = 'res.partner'
_columns = {
'leadtime': fields.integer(string='Lead Time'),
}
_defaults = {
'leadtime': lambda *a: 2,
}
res_partner()
fields_date = ['date_order', 'delivery_requested_date', 'delivery_confirmed_date',
'est_transport_lead_time', 'ready_to_ship_date', 'shipment_date',
'arrival_date', 'receipt_date']
fields_date_line = ['date_planned', 'confirmed_delivery_date']
def get_type(self):
'''
return type corresponding to object
'''
if self._name == 'sale.order':
return 'so'
if self._name == 'purchase.order':
return 'po'
return False
def get_field_description(self, cr, uid, field, context=None):
'''
Returns the description of the field
'''
if context is None:
context = {}
field_obj = self.pool.get('ir.model.fields')
field_ids = field_obj.search(cr, uid, [('model', '=', self._name), ('name', '=', field)])
if not field_ids:
return field
return field_obj.browse(cr, uid, field_ids[0]).field_description
def check_date_order(self, cr, uid, date=False, context=None):
'''
Checks if the creation date of the order is in an opened period
@param : date : Creation date
@return True if the date is in an opened period, False if not
deprecated
'''
if context is None:
context = {}
if not date:
return True
if isinstance(date, datetime):
date = date.strftime('%Y-%m-%d')
res = False
period_obj = self.pool.get('account.period')
## TODO: See if the period state changed in financial part of Unifiedl
period_ids = period_obj.search(cr, uid, [('state', '=', 'draft')], context=context)
for p in period_obj.browse(cr, uid, period_ids, context=context):
if date >= p.date_start and date <= p.date_stop:
return True
return res
def check_delivery_requested(self, date=False, context=None):
'''
Checks if the delivery requested date is equal of a date between today and today + 24 months
@param: Date to test (Delivery requested date)
@return : False if the date is not in the scale
deprecated
'''
if context is None:
context = {}
if not date:
return True
return True
if isinstance(date, datetime):
date = date.strftime('%Y-%m-%d')
res = False
two_years = (datetime.now() + relativedelta(years=2)).strftime('%Y-%m-%d')
if date >= (datetime.now()).strftime('%Y-%m-%d') and date <= two_years:
res = True
return res
def check_delivery_confirmed(self, confirmed_date=False, date_order=False, context=None):
'''
Checks if the delivery confirmed date is older than the creation date
@param: Date to test (Delivery requested date)
@return : False if the date is not in the scale
deprecated
'''
if context is None:
context = {}
if not confirmed_date or not date_order:
return True
return True
if isinstance(confirmed_date, datetime):
confirmed_date = confirmed_date.strftime('%Y-%m-%d')
if isinstance(date_order, datetime):
date_order = date_order.strftime('%Y-%m-%d')
res = False
if confirmed_date > date_order:
res = True
return res
def check_dates(self, cr, uid, data, context=None):
'''
Runs all tests on dates
deprecated
'''
if context is None:
context = {}
# Comment this line if you would check date on PO/SO creation/write
return True
date_order = data.get('date_order', False)
requested_date = data.get('delivery_requested_date', False)
confirmed_date = data.get('delivery_confirmed_date', False)
ready_to_ship = data.get('ready_to_ship_date', False)
# Check if the creation date is in an opened period
if not check_date_order(self, cr, uid, date_order, context=context):
raise osv.except_osv(_('Error'), _('The creation date is not in an opened period !'))
if not check_delivery_requested(self, requested_date, context=context):
raise osv.except_osv(_('Error'), _('The Delivery Requested Date should be between today and today + 24 months !'))
if not check_delivery_confirmed(self, confirmed_date, date_order, context=context):
raise osv.except_osv(_('Error'), _('The Delivery Confirmed Date should be older than the Creation date !'))
if not check_delivery_confirmed(self, ready_to_ship, date_order, context=context):
raise osv.except_osv(_('Error'), _('The Ready to Ship Date should be older than the Creation date !'))
return True
def common_internal_type_change(self, cr, uid, ids, internal_type, rts, shipment_date, context=None):
'''
Common function when type of order is changing
deprecated
'''
if context is None:
context = {}
v = {}
# if internal_type == 'international' and rts and not shipment_date:
# v.update({'shipment_date': rts})
return v
def common_ready_to_ship_change(self, cr, uid, ids, ready_to_ship, date_order, shipment, context=None):
'''
Common function when ready_to_ship date is changing
deprecated
'''
if context is None:
context = {}
message = {}
v = {}
if not ready_to_ship or not date_order:
return {}
# Set the message if the user enter a wrong ready to ship date
# if not check_delivery_confirmed(self, ready_to_ship, date_order, context=context):
# message = {'title': _('Warning'),
# 'message': _('The Ready To Ship Date should be older than the Creation date !')}
# else:
# if not shipment:
# v.update({'shipment_date': ready_to_ship})
if not shipment:
v.update({'shipment_date': ready_to_ship})
return {'warning': message, 'value': v}
def compute_rts(self, cr, uid, requested_date=False, transport_lt=0, type=False, context=None):
'''
requested - transport lt - shipment lt
'''
if context is None:
context = {}
company_obj = self.pool.get('res.company')
# need request_date for computation
if requested_date:
company_id = company_obj._company_default_get(cr, uid, False, context=context)
if type == 'so':
company_id = company_obj._company_default_get(cr, uid, 'sale.order', context=context)
if type == 'po':
company_id = company_obj._company_default_get(cr, uid, 'purchase.order', context=context)
shipment_lt = company_obj.read(cr, uid, [company_id], ['shipment_lead_time'], context=context)[0]['shipment_lead_time']
requested = datetime.strptime(requested_date, '%Y-%m-%d')
rts = requested - relativedelta(days=transport_lt or 0)
rts = rts - relativedelta(days=shipment_lt or 0)
rts = rts.strftime('%Y-%m-%d')
return rts
return False
def compute_requested_date(self, cr, uid, part=False, date_order=False, type=False, context=None):
'''
compute requested date according to type
SPRINT3 validated - yaml test ok
'''
if context is None:
context = {}
# part and date_order are need for computation
if part and date_order:
partner = self.pool.get('res.partner').browse(cr, uid, part)
requested_date = datetime.strptime(date_order, '%Y-%m-%d')
if type == 'so':
requested_date = requested_date + relativedelta(days=partner.customer_lt)
if type == 'po':
requested_date = requested_date + relativedelta(days=partner.supplier_lt)
requested_date = requested_date.strftime('%Y-%m-%d')
return requested_date
return False
def compute_transport_type(self, cr, uid, part=False, type=False, context=None):
'''
return the preferred transport type of partner
'''
if context is None:
context = {}
field = 'transport_0'
if part:
partner_obj = self.pool.get('res.partner')
res = partner_obj.read(cr, uid, [part], [field], context=context)[0][field]
return res
return False
def compute_internal_type(self, cr, uid, part=False, type=False, context=None):
'''
return the zone of partner
'''
if context is None:
context = {}
field = 'zone'
if part:
partner_obj = self.pool.get('res.partner')
res = partner_obj.read(cr, uid, [part], [field], context=context)[0][field]
return res
return False
def compute_partner_type(self, cr, uid, part=False, type=False, context=None):
'''
return the partner type
'''
if context is None:
context = {}
field = 'partner_type'
if part:
partner_obj = self.pool.get('res.partner')
res = partner_obj.read(cr, uid, [part], [field], context=context)[0][field]
return res
return False
def common_requested_date_change(self, cr, uid, ids, part=False, date_order=False, requested_date=False, transport_lt=0, type=False, res=None, context=None):
'''
Common function when requested date is changing
'''
if context is None:
context = {}
if res is None:
res = {}
# compute rts - only for so
if type == 'so':
rts = compute_rts(self, cr, uid, requested_date=requested_date, transport_lt=transport_lt, type=type, context=context)
res.setdefault('value', {}).update({'ready_to_ship_date': rts})
# display warning if requested date - creation date < default supplier lead time
if type == 'po' and part and requested_date and date_order:
# objects
date_tools = self.pool.get('date.tools')
db_date_format = date_tools.get_db_date_format(cr, uid, context=context)
partner = self.pool.get('res.partner').browse(cr, uid, part, context=context)
# compute delta
requested = datetime.strptime(requested_date, db_date_format)
creation = datetime.strptime(date_order, db_date_format)
delta = requested - creation
# if < supplier_lt, display warning
if delta.days < partner.supplier_lt:
res.setdefault('warning', {}).update({'title': _('Warning'),
'message': _('The number of days between Creation Date and Delivery Expected Date (%s) is less than the supplier lead-time (%s).')%(delta.days,partner.supplier_lt,)})
return res
def common_onchange_transport_lt(self, cr, uid, ids, requested_date=False, transport_lt=0, type=False, res=None, context=None):
'''
Common fonction when transport lead time is changed
'''
if context is None:
context = {}
if res is None:
res = {}
# compute rts - only for so
if type == 'so':
rts = compute_rts(self, cr, uid, requested_date=requested_date, transport_lt=transport_lt, type=type, context=context)
res.setdefault('value', {}).update({'ready_to_ship_date': rts})
return res
def common_onchange_transport_type(self, cr, uid, ids, part=False, transport_type=False, requested_date=False, type=False, res=None, context=None):
'''
fills the estimated transport lead time corresponding to the selected transport,
0.0 if no transport selected
'''
if context is None:
context = {}
if res is None:
res = {}
# get the value if partner
partner_obj = self.pool.get('res.partner')
# returns a dictionary
lead_time = partner_obj.get_transport_lead_time(cr, uid, part, transport_type, context=context)
# if part, we get a dictionary from get_transport_lead_time
if lead_time:
lead_time = lead_time.get(part)
res.setdefault('value', {}).update({'est_transport_lead_time': lead_time,})
# call onchange_transport_lt and update **VALUE** of res
res_transport_lt = self.onchange_transport_lt(cr, uid, ids, requested_date=requested_date, transport_lt=res['value']['est_transport_lead_time'], context=None)
res.setdefault('value', {}).update(res_transport_lt.setdefault('value', {}))
return res
def common_onchange_date_order(self, cr, uid, ids, part=False, date_order=False, transport_lt=0, type=False, res=None, context=None,):
'''
Common function when the Creation date (order_date) is changed
- modify requested date
- call on_change_requested_date
'''
if context is None:
context = {}
if res is None:
res = {}
# compute requested date
requested_date = compute_requested_date(self, cr, uid, part=part, date_order=date_order, type=type, context=context)
res.setdefault('value', {}).update({'delivery_requested_date': requested_date})
# call onchange_requested_date and update **VALUE** of res
res_requested_date = self.onchange_requested_date(cr, uid, ids, part=part, date_order=date_order, requested_date=res['value']['delivery_requested_date'], transport_lt=transport_lt, context=context)
res.setdefault('value', {}).update(res_requested_date.setdefault('value', {}))
return res
def common_onchange_partner_id(self, cr, uid, ids, part=False, order_type=False, date_order=False, transport_lt=0, type=False, res=None, context=None):
'''
Common function when Partner is changing
'''
if context is None:
context = {}
if res is None:
res = {}
# compute requested date
requested_date = compute_requested_date(self, cr, uid, part=part, date_order=date_order, type=type, context=context)
res.setdefault('value', {}).update({'delivery_requested_date': requested_date})
# call onchange_requested_date and update **VALUE** of res
res_requested_date = self.onchange_requested_date(cr, uid, ids, part=part, date_order=date_order, requested_date=res['value']['delivery_requested_date'], transport_lt=transport_lt, context=context)
res.setdefault('value', {}).update(res_requested_date.setdefault('value', {}))
# compute transport type
transport_type = compute_transport_type(self, cr, uid, part=part, type=type, context=context)
res.setdefault('value', {}).update({'transport_type': transport_type,})
# call onchange_transport_type and update **VALUE** of res
res_transport_type = self.onchange_transport_type(cr, uid, ids, part=part, transport_type=res['value']['transport_type'], requested_date=res['value']['delivery_requested_date'], context=context)
res.setdefault('value', {}).update(res_transport_type.setdefault('value', {}))
# reset confirmed date
res.setdefault('value', {}).update({'delivery_confirmed_date': False,})
# compute partner type
partner_type = compute_partner_type(self, cr, uid, part=part, type=type, context=context)
res.setdefault('value', {}).update({'partner_type': partner_type,})
# internal type
internal_type = compute_internal_type(self, cr, uid, part=part, type=type, context=context)
res.setdefault('value', {}).update({'internal_type': internal_type,})
return res
def common_dates_change_on_line(self, cr, uid, ids, requested_date, confirmed_date, parent_class, context=None):
'''
Checks if dates are later than header dates
deprecated
'''
if context is None:
context = {}
min_confirmed = min_requested = False
order_id = context.get('active_id', [])
if isinstance(order_id, (int, long)):
order_id = [order_id]
if order_id:
min_confirmed = self.pool.get(parent_class).browse(cr, uid, order_id[0]).delivery_confirmed_date
min_requested = self.pool.get(parent_class).browse(cr, uid, order_id[0]).delivery_requested_date
for line in self.browse(cr, uid, ids, context=context):
min_confirmed = line.order_id.delivery_confirmed_date
min_requested = line.order_id.delivery_requested_date
return {'value': {'date_planned': requested_date,}}
# 'confirmed_delivery_date': confirmed_date}}
def common_create(self, cr, uid, data, type, context=None):
'''
common for create function so and po
'''
if context is None:
context = {}
# fill partner_type data
if data.get('partner_id', False):
partner = self.pool.get('res.partner').browse(cr, uid, data.get('partner_id'), context=context)
# partner type - always set
data.update({'partner_type': partner.partner_type,})
# internal type (zone) - always set
data.update({'internal_type': partner.zone,})
# transport type - only if not present (can be modified by user to False)
if 'transport_type' not in data:
data.update({'transport_type': partner.transport_0,})
# est_transport_lead_time - only if not present (can be modified by user to False)
if 'est_transport_lead_time' not in data:
data.update({'est_transport_lead_time': partner.transport_0_lt,})
# by default delivery requested date is equal to today + supplier lead time - filled for compatibility because requested date is now mandatory
if not data.get('delivery_requested_date', False):
# PO - supplier lead time / SO - customer lead time
if type == 'so':
requested_date = (datetime.today() + relativedelta(days=partner.customer_lt)).strftime('%Y-%m-%d')
if type == 'po':
requested_date = (datetime.today() + relativedelta(days=partner.supplier_lt)).strftime('%Y-%m-%d')
data['delivery_requested_date'] = requested_date
return data
class purchase_order(osv.osv):
_name = 'purchase.order'
_inherit= 'purchase.order'
def write(self, cr, uid, ids, data, context=None):
'''
Checks if dates are good before writing
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
if not 'date_order' in data:
data.update({'date_order': self.browse(cr, uid, ids[0]).date_order})
# fill partner_type and zone
if data.get('partner_id', False):
partner = self.pool.get('res.partner').browse(cr, uid, data.get('partner_id'), context=context)
# partner type - always set
data.update({'partner_type': partner.partner_type,})
# internal type (zone) - always set
data.update({'internal_type': partner.zone,})
# erase delivery_confirmed_date if partner_type is internal or section and the date is not filled by synchro - considered updated by synchro by default
if partner.partner_type in ('internal', 'section') and not data.get('confirmed_date_by_synchro', True):
data.update({'delivery_confirmed_date': False,})
check_dates(self, cr, uid, data, context=context)
return super(purchase_order, self).write(cr, uid, ids, data, context=context)
def create(self, cr, uid, data, context=None):
'''
Checks if dates are good before creation
Delivery Requested Date: creation date + supplier lead time from partner (supplier_lt)
'''
if context is None:
context = {}
# common function for so and po
data = common_create(self, cr, uid, data, type=get_type(self), context=context)
# fill partner_type data
if data.get('partner_id', False):
partner = self.pool.get('res.partner').browse(cr, uid, data.get('partner_id'), context=context)
# erase delivery_confirmed_date if partner_type is internal or section and the date is not filled by synchro - considered updated by synchro by default
if partner.partner_type in ('internal', 'section') and not data.get('confirmed_date_by_synchro', True):
data.update({'delivery_confirmed_date': False,})
# deprecated ?
check_dates(self, cr, uid, data, context=context)
return super(purchase_order, self).create(cr, uid, data, context=context)
def _get_receipt_date(self, cr, uid, ids, field_name, arg, context=None):
'''
Returns the date of the first picking for the the PO
'''
if context is None:
context = {}
res = {}
pick_obj = self.pool.get('stock.picking')
for order in self.browse(cr, uid, ids, context=context):
pick_ids = pick_obj.search(cr, uid, [('purchase_id', '=', order.id)], offset=0, limit=1, order='date_done', context=context)
if not pick_ids:
res[order.id] = False
else:
res[order.id] = pick_obj.browse(cr, uid, pick_ids[0]).date_done
return res
def _get_vals_order_dates(self, cr, uid, ids, field_name, arg, context=None):
'''
Return function values
'''
if context is None:
context = {}
res = {}
pick_obj = self.pool.get('stock.picking')
if isinstance(field_name, str):
field_name = [field_name]
for obj in self.browse(cr, uid, ids, context=context):
# default dic
res[obj.id] = {}
# default value
for f in field_name:
res[obj.id].update({f:False})
# get corresponding partner type
if obj.partner_id:
partner_type = obj.partner_id.partner_type
res[obj.id]['partner_type'] = partner_type
return res
_columns = {
'delivery_requested_date': fields.date(string='Delivery Requested Date', required=True),
'delivery_confirmed_date': fields.date(string='Delivery Confirmed Date'),
'ready_to_ship_date': fields.date(string='Ready To Ship Date'),
'shipment_date': fields.date(string='Shipment Date', help='Date on which picking is created at supplier'),
'arrival_date': fields.date(string='Arrival date in the country', help='Date of the arrival of the goods at custom'),
'receipt_date': fields.function(_get_receipt_date, type='date', method=True, store=True,
string='Receipt Date', help='for a PO, date of the first godd receipt.'),
# BETA - to know if the delivery_confirmed_date can be erased - to be confirmed
'confirmed_date_by_synchro': fields.boolean(string='Confirmed Date by Synchro'),
# FIELDS PART OF CREATE/WRITE methods
# not a function because can be modified by user - **ONLY IN CREATE only if not in vals**
'transport_type': fields.selection(selection=TRANSPORT_TYPE, string='Transport Mode',
help='Number of days this field has to be associated with a transport mode selection'),
# not a function because can be modified by user - **ONLY IN CREATE only if not in vals**
'est_transport_lead_time': fields.float(digits=(16,2), string='Est. Transport Lead Time',
help="Estimated Transport Lead-Time in weeks"),
# not a function because a function value is only filled when saved, not with on change of partner id
# from partner_id object
'partner_type': fields.selection(string='Partner Type', selection=PARTNER_TYPE, readonly=True,),
# not a function because a function value is only filled when saved, not with on change of partner id
# from partner_id object
'internal_type': fields.selection(string='Type', selection=ZONE_SELECTION, readonly=True,),
}
_defaults = {
'date_order': lambda *a: time.strftime('%Y-%m-%d'),
'confirmed_date_by_synchro': False,
}
def copy(self, cr, uid, id, default=None, context=None):
new_id = super(purchase_order, self).copy(cr, uid, id, default, context)
if new_id:
data = self.read(cr, uid, new_id, ['order_line', 'delivery_requested_date'])
if data['order_line'] and data['delivery_requested_date']:
self.pool.get('purchase.order.line').write(cr, uid, data['order_line'], {'date_planned': data['delivery_requested_date']})
return new_id
def copy_data(self, cr, uid, id, default=None, context=None):
'''
erase dates
'''
if default is None:
default = {}
if context is None:
context = {}
fields_to_reset = ['delivery_requested_date', 'ready_to_ship_date', 'date_order', 'delivery_confirmed_date', 'arrival_date', 'shipment_date', 'arrival_date', 'date_approve']
to_del = []
for ftr in fields_to_reset:
if ftr not in default:
to_del.append(ftr)
res = super(purchase_order, self).copy_data(cr, uid, id, default=default, context=context)
for ftd in to_del:
if ftd in res:
del(res[ftd])
return res
def internal_type_change(self, cr, uid, ids, internal_type, rts, shipment_date, context=None):
'''
Set the shipment date if the internal_type == international
deprecated
'''
if context is None:
context = {}
return {}
return {'value': common_internal_type_change(self, cr, uid, ids, internal_type, rts, shipment_date, context=context)}
def ready_to_ship_change(self, cr, uid, ids, ready_to_ship, date_order, shipment, context=None):
'''
Checks the entered value
deprecated
'''
if context is None:
context = {}
return {}
return common_ready_to_ship_change(self, cr, uid, ids, ready_to_ship, date_order, shipment, context=context)
def onchange_requested_date(self, cr, uid, ids, part=False, date_order=False, requested_date=False, transport_lt=0, order_type=False, context=None):
'''
Set the confirmed date with the requested date if the first is not fill
And the delivery_confirmed_date takes the value of the computed requested date by default only if order_type == 'purchase_list'
'''
if context is None:
context = {}
res = {}
res['value'] = {}
if order_type == 'purchase_list':
res['value'].update({'delivery_confirmed_date': requested_date})
else:
res['value'].update({'delivery_confirmed_date': False})
# compute ready to ship date
res = common_requested_date_change(self, cr, uid, ids, part=part, date_order=date_order, requested_date=requested_date, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def onchange_internal_type(self, cr, uid, ids, order_type, partner_id, categ, dest_partner_id=False, warehouse_id=False, delivery_requested_date=False):
"""
Set the delivery_confirmed_date if order_type == 'purchase_list'
"""
res = super(purchase_order, self).onchange_internal_type(cr, uid, ids, order_type, partner_id, categ, dest_partner_id, warehouse_id, delivery_requested_date)
if not 'value' in res:
res['value'] = {}
if order_type == 'purchase_list' and delivery_requested_date:
res['value'].update({'delivery_confirmed_date': delivery_requested_date})
# UF-1440: Add today's date if no date and you choose "purchase_list" PO
elif order_type == 'purchase_list' and not delivery_requested_date:
res['value'].update({'delivery_requested_date': time.strftime('%Y-%m-%d'), 'delivery_confirmed_date': time.strftime('%Y-%m-%d')})
else:
res['value'].update({'delivery_confirmed_date': False})
return res
def onchange_transport_lt(self, cr, uid, ids, requested_date=False, transport_lt=0, context=None):
'''
Fills the Ready to ship date
SPRINT3 validated - YAML ok
'''
if context is None:
context = {}
res = {}
# compute ready to ship date
res = common_onchange_transport_lt(self, cr, uid, ids, requested_date=requested_date, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def onchange_date_order(self, cr, uid, ids, part=False, date_order=False, transport_lt=0, context=None):
'''
date_order is changed (creation date)
'''
if context is None:
context = {}
res = {}
# compute requested date
res = common_onchange_date_order(self, cr, uid, ids, part=part, date_order=date_order, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def onchange_transport_type(self, cr, uid, ids, part=False, transport_type=False, requested_date=False, context=None):
'''
transport type changed
requested date is in the signature because it is needed for children on_change call
'''
if context is None:
context = {}
res = {}
res = common_onchange_transport_type(self, cr, uid, ids, part=part, transport_type=transport_type, requested_date=requested_date, type=get_type(self), res=res, context=context)
return res
def onchange_partner_id(self, cr, uid, ids, part=False, date_order=False, transport_lt=0, context=None):
'''
Fills the Requested and Confirmed delivery dates
SPRINT3 validated - YAML ok
'''
if isinstance(ids, (int, long)):
ids = [ids]
if context is None:
context = {}
res = super(purchase_order, self).onchange_partner_id(cr, uid, ids, part)
# compute requested date and transport type
res = common_onchange_partner_id(self, cr, uid, ids, part=part, date_order=date_order, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def requested_data(self, cr, uid, ids, context=None):
'''
data for requested
'''
if context is None:
context = {}
return {'name': _('Do you want to update the Requested Date of all order lines ?'),}
def confirmed_data(self, cr, uid, ids, context=None):
'''
data for confirmed
'''
if context is None:
context = {}
return {'name': _('Do you want to update the Confirmed Delivery Date of all order lines ?'),}
def update_date(self, cr, uid, ids, context=None):
'''
open the update lines wizard
'''
# we need the context
if context is None:
context = {}
# field name
field_name = context.get('field_name', False)
assert field_name, 'The button is not correctly set.'
# data
data = getattr(self, field_name + '_data')(cr, uid, ids, context=context)
name = data['name']
model = 'update.lines'
obj = self.pool.get(model)
wiz_obj = self.pool.get('wizard')
# open the selected wizard
return wiz_obj.open_wizard(cr, uid, ids, name=name, model=model, context=context)
purchase_order()
class purchase_order_line(osv.osv):
_name= 'purchase.order.line'
_inherit = 'purchase.order.line'
def _vals_get_order_date(self, cr, uid, ids, fields, arg, context=None):
'''
get values for functions
'''
if context is None:
context = {}
if isinstance(fields, str):
fields = [fields]
result = {}
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = {}
for f in fields:
result[obj.id].update({f: False})
# po state
result[obj.id]['po_state_stored'] = obj.order_id.state
# po partner type
result[obj.id]['po_partner_type_stored'] = obj.order_id.partner_type
return result
def _get_line_ids_from_po_ids(self, cr, uid, ids, context=None):
'''
self is purchase.order
'''
if context is None:
context = {}
result = self.pool.get('purchase.order.line').search(cr, uid, [('order_id', 'in', ids)], context=context)
return result
def _get_planned_date(self, cr, uid, context=None):
'''
Returns planned_date
SPRINT3 validated
'''
if context is None:
context = {}
order_obj= self.pool.get('purchase.order')
res = (datetime.now() + relativedelta(days=+2)).strftime('%Y-%m-%d')
if context.get('purchase_id', False):
po = order_obj.browse(cr, uid, context.get('purchase_id'), context=context)
res = po.delivery_requested_date
return res
def _get_confirmed_date(self, cr, uid, context=None):
'''
Returns confirmed date
SPRINT3 validated
'''
if context is None:
context = {}
order_obj= self.pool.get('purchase.order')
#The PO counterpart created should get for all line a delivery confirmed date blank
if context.get('is_a_counterpart') or context.get('purchase_id', False) and order_obj.browse(cr, uid, context.get('purchase_id'), context=context).is_a_counterpart:
return
res = (datetime.now() + relativedelta(days=+2)).strftime('%Y-%m-%d')
if context.get('purchase_id', False):
po = order_obj.browse(cr, uid, context.get('purchase_id'), context=context)
res = po.delivery_confirmed_date
return res
def _get_default_state(self, cr, uid, context=None):
'''
default value for state fields.related
why, beacause if we try to pass state in the context,
the context is simply reset without any values specified...
'''
if context is None:
context= {}
if context.get('purchase_id', False):
order_obj= self.pool.get('purchase.order')
po = order_obj.browse(cr, uid, context.get('purchase_id'), context=context)
return po.state
return False
_columns = {'date_planned': fields.date(string='Delivery Requested Date', required=True, select=True,
help='Header level dates has to be populated by default with the possibility of manual updates'),
'confirmed_delivery_date': fields.date(string='Delivery Confirmed Date',
help='Header level dates has to be populated by default with the possibility of manual updates.'),
# not replacing the po_state from sale_followup - should ?
'po_state_stored': fields.related('order_id', 'state', type='selection', selection=PURCHASE_ORDER_STATE_SELECTION, string='Po State', readonly=True,),
'po_partner_type_stored': fields.related('order_id', 'partner_type', type='selection', selection=PARTNER_TYPE, string='Po Partner Type', readonly=True,),
}
_defaults = {'po_state_stored': _get_default_state,
'po_partner_type_stored': lambda obj, cr, uid, c: c and c.get('partner_type', False),
'date_planned': _get_planned_date,
'confirmed_delivery_date': _get_confirmed_date,
}
def copy_data(self, cr, uid, id, default=None, context=None):
'''
erase dates
'''
if default is None:
default = {}
if context is None:
context = {}
if not context.get('keepDateAndDistrib'):
if 'confirmed_delivery_date' not in default:
default['confirmed_delivery_date'] = False
if 'date_planned' not in default:
default['date_planned'] = (datetime.now() + relativedelta(days=+2)).strftime('%Y-%m-%d')
return super(purchase_order_line, self).copy_data(cr, uid, id, default=default, context=context)
def dates_change(self, cr, uid, ids, requested_date, confirmed_date, context=None):
'''
Checks if dates are later than header dates
deprecated
'''
if context is None:
context = {}
return common_dates_change_on_line(self, cr, uid, ids, requested_date, confirmed_date, 'purchase.order', context=context)
purchase_order_line()
class sale_order(osv.osv):
_name = 'sale.order'
_inherit = 'sale.order'
def write(self, cr, uid, ids, data, context=None):
'''
Checks if dates are good before writing
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
for order in self.browse(cr, uid, ids, context=context):
# Fill partner type
partner = self.pool.get('res.partner').browse(cr, uid, data.get('partner_id', order.partner_id.id), context=context)
# partner type - always set
data.update({'partner_type': partner.partner_type,})
if not 'date_order' in data:
data.update({'date_order': self.browse(cr, uid, ids[0]).date_order})
check_dates(self, cr, uid, data, context=context)
return super(sale_order, self).write(cr, uid, ids, data, context=context)
def create(self, cr, uid, data, context=None):
'''
Checks if dates are good before creation
'''
if context is None:
context = {}
company_obj = self.pool.get('res.company')
# common function for so and po
data = common_create(self, cr, uid, data, type=get_type(self), context=context)
# ready_to_ship_date only mandatory for so
if 'ready_to_ship_date' not in data:
rts = compute_rts(self, cr, uid, data.get('delivery_requested_date'), data.get('est_transport_lead_time'), type=get_type(self), context=context)
data.update({'ready_to_ship_date': rts,})
# deprecated ?
check_dates(self, cr, uid, data, context=context)
return super(sale_order, self).create(cr, uid, data, context=context)
def copy_data(self, cr, uid, id, default=None, context=None):
'''
erase dates
'''
if default is None:
default = {}
if context is None:
context = {}
default.update({'shipment_date': False,})
fields_to_reset = ['delivery_requested_date', 'ready_to_ship_date', 'date_order', 'delivery_confirmed_date', 'arrival_date']
to_del = []
for ftr in fields_to_reset:
if ftr not in default:
to_del.append(ftr)
res = super(sale_order, self).copy_data(cr, uid, id, default=default, context=context)
for ftd in to_del:
if ftd in res:
del(res[ftd])
return res
def _get_receipt_date(self, cr, uid, ids, field_name, arg, context=None):
'''
Returns the date of the first picking for the the PO
'''
if context is None:
context = {}
res = {}
pick_obj = self.pool.get('stock.picking')
for order in self.browse(cr, uid, ids, context=context):
pick_ids = pick_obj.search(cr, uid, [('sale_id', '=', order.id)], offset=0, limit=1, order='date_done', context=context)
if not pick_ids:
res[order.id] = False
else:
res[order.id] = pick_obj.browse(cr, uid, pick_ids[0]).date_done
return res
_columns = {
'date_order':fields.date(string='Creation Date', required=True, select=True, readonly=True, help="Date on which this document has been created.", states={'draft': [('readonly', False)]}),
'delivery_requested_date': fields.date(string='Delivery Requested Date', required=True),
'delivery_confirmed_date': fields.date(string='Delivery Confirmed Date'),
'ready_to_ship_date': fields.date(string='Ready To Ship Date', required=True),
'shipment_date': fields.date(string='Shipment Date', readonly=True, help='Date on which picking is created at supplier'),
'arrival_date': fields.date(string='Arrival date in the country', help='Date of the arrival of the goods at custom'),
'receipt_date': fields.function(_get_receipt_date, type='date', method=True, store=True,
string='Receipt Date', help='for a PO, date of the first godd receipt.'),
# BETA - to know if the delivery_confirmed_date can be erased - to be confirmed
'confirmed_date_by_synchro': fields.boolean(string='Confirmed Date by Synchro'),
# FIELDS PART OF CREATE/WRITE methods
# not a function because can be modified by user - **ONLY IN CREATE only if not in vals**
'transport_type': fields.selection(selection=TRANSPORT_TYPE, string='Transport Mode',
help='Number of days this field has to be associated with a transport mode selection'),
# not a function because can be modified by user - **ONLY IN CREATE only if not in vals**
'est_transport_lead_time': fields.float(digits=(16,2), string='Est. Transport Lead Time', help="Estimated Transport Lead-Time in weeks"),
# not a function because a function value is only filled when saved, not with on change of partner id
# from partner_id object
'partner_type': fields.selection(string='Partner Type', selection=PARTNER_TYPE, readonly=True,),
# not a function because a function value is only filled when saved, not with on change of partner id
# from partner_id object
'internal_type': fields.selection(string='Type', selection=ZONE_SELECTION, readonly=True,),
}
_defaults = {
'date_order': lambda *a: time.strftime('%Y-%m-%d'),
'confirmed_date_by_synchro': False,
}
def internal_type_change(self, cr, uid, ids, internal_type, rts, shipment_date, context=None):
'''
Set the shipment date if the internal_type == international
deprecated
'''
if context is None:
context = {}
return {}
return {'value': common_internal_type_change(self, cr, uid, ids, internal_type, rts, shipment_date, context=context)}
def ready_to_ship_change(self, cr, uid, ids, ready_to_ship, date_order, shipment, context=None):
'''
Checks the entered value
deprecated
'''
if context is None:
context = {}
return {}
return common_ready_to_ship_change(self, cr, uid, ids, ready_to_ship, date_order, shipment, context=context)
def onchange_requested_date(self, cr, uid, ids, part=False, date_order=False, requested_date=False, transport_lt=0, context=None):
'''
Set the confirmed date with the requested date if the first is not fill
'''
if context is None:
context = {}
res = {}
# compute ready to ship date
res = common_requested_date_change(self, cr, uid, ids, part=part, date_order=date_order, requested_date=requested_date, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def onchange_transport_lt(self, cr, uid, ids, requested_date=False, transport_lt=0, context=None):
'''
Fills the Ready to ship date
SPRINT3 validated - YAML ok
'''
if context is None:
context = {}
res = {}
# compute ready to ship date
res = common_onchange_transport_lt(self, cr, uid, ids, requested_date=requested_date, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def onchange_transport_type(self, cr, uid, ids, part=False, transport_type=False, requested_date=False, context=None):
'''
transport type changed
requested date is in the signature because it is needed for children on_change call
SPRINT3 validated - YAML ok
'''
if context is None:
context = {}
res = {}
res = common_onchange_transport_type(self, cr, uid, ids, part=part, transport_type=transport_type, requested_date=requested_date, type=get_type(self), res=res, context=context)
return res
def onchange_partner_id(self, cr, uid, ids, part=False, order_type=False, date_order=False, transport_lt=0, context=None):
'''
Fills the Requested and Confirmed delivery dates
SPRINT3 validated - YAML ok
'''
if isinstance(ids, (int, long)):
ids = [ids]
if context is None:
context = {}
res = super(sale_order, self).onchange_partner_id(cr, uid, ids, part, order_type)
# compute requested date and transport type
res = common_onchange_partner_id(self, cr, uid, ids, part=part, order_type=order_type, date_order=date_order, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
if res.get('value', {}).get('pricelist_id') and part:
if ids:
if isinstance(ids, (int, long)):
ids = [ids]
order = self.pool.get('sale.order').browse(cr, uid, ids[0])
partner = self.pool.get('res.partner').browse(cr, uid, part)
pricelist_ids = self.pool.get('product.pricelist').search(cr, uid, [('type', '=', 'sale'), ('in_search', '=', partner.partner_type)])
pricelist_id = res['value'].pop('pricelist_id')
if order.pricelist_id.id not in pricelist_ids:
res.update({'warning': {'title': 'Warning',
'message': 'The currency used currently on the order is not compatible with the new partner. Please change the currency to choose a compatible currency.'}})
return res
def onchange_date_order(self, cr, uid, ids, part=False, date_order=False, transport_lt=0, context=None):
'''
date_order is changed (creation date)
SPRINT3 validated - YAML ok
'''
if context is None:
context = {}
res = {}
# compute requested date
res = common_onchange_date_order(self, cr, uid, ids, part=part, date_order=date_order, transport_lt=transport_lt, type=get_type(self), res=res, context=context)
return res
def requested_data(self, cr, uid, ids, context=None):
'''
data for requested for change line wizard
'''
if context is None:
context = {}
return {'name': _('Do you want to update the Requested Date of all order lines ?'),}
def confirmed_data(self, cr, uid, ids, context=None):
'''
data for confirmed for change line wizard
'''
if context is None:
context = {}
return {'name': _('Do you want to update the Confirmed Delivery Date of all order lines ?'),}
def update_date(self, cr, uid, ids, context=None):
'''
open the update lines wizard
'''
# we need the context
if context is None:
context = {}
# field name
field_name = context.get('field_name', False)
assert field_name, 'The button is not correctly set.'
# data
data = getattr(self, field_name + '_data')(cr, uid, ids, context=context)
name = data['name']
model = 'update.lines'
obj = self.pool.get(model)
wiz_obj = self.pool.get('wizard')
# open the selected wizard
return wiz_obj.open_wizard(cr, uid, ids, name=name, model=model, context=context)
def _hook_ship_create_stock_move(self, cr, uid, ids, context=None, *args, **kwargs):
'''
Please copy this to your module's method also.
This hook belongs to the action_ship_create method from sale>sale.py
- allow to modify the data for stock move creation
'''
if context is None:
context = {}
# objects
date_tools = self.pool.get('date.tools')
fields_tools = self.pool.get('fields.tools')
db_date_format = date_tools.get_db_date_format(cr, uid, context=context)
# call super
move_data = super(sale_order, self)._hook_ship_create_stock_move(cr, uid, ids, context=context, *args, **kwargs)
order = kwargs['order']
# get shipment lead time
shipment_lt = fields_tools.get_field_from_company(cr, uid, object=self._name, field='shipment_lead_time', context=context)
# date = rts of so - by default, a picking is created
rts = order.ready_to_ship_date
# date, date_expected
move_data.update({'date': rts,'date_expected': rts})
return move_data
def _hook_ship_create_procurement_order(self, cr, uid, ids, context=None, *args, **kwargs):
'''
Please copy this to your module's method also.
This hook belongs to the action_ship_create method from sale>sale.py
- allow to modify the data for procurement order creation
'''
if context is None:
context = {}
# objects
date_tools = self.pool.get('date.tools')
fields_tools = self.pool.get('fields.tools')
db_date_format = date_tools.get_db_date_format(cr, uid, context=context)
# call to super
result = super(sale_order, self)._hook_ship_create_procurement_order(cr, uid, ids, context=context, *args, **kwargs)
# date_planned = rts - company.prep_lt
order = kwargs['order']
# get value from company
prep_lt = fields_tools.get_field_from_company(cr, uid, object=self._name, field='preparation_lead_time', context=context)
# rts - prep_lt
rts = datetime.strptime(order.ready_to_ship_date, db_date_format)
rts = rts - relativedelta(days=prep_lt or 0)
rts = rts.strftime(db_date_format)
result['date_planned'] = rts
# update from yml flag
result['from_yml_test'] = order.from_yml_test
return result
sale_order()
class sale_order_line(osv.osv):
_name= 'sale.order.line'
_inherit = 'sale.order.line'
def _get_planned_date(self, cr, uid, context=None):
'''
Returns planned_date
'''
if context is None:
context = {}
order_obj= self.pool.get('sale.order')
res = (datetime.now() + relativedelta(days=+2)).strftime('%Y-%m-%d')
if context.get('sale_id', False):
so = order_obj.browse(cr, uid, context.get('sale_id'), context=context)
res = so.delivery_requested_date
return res
def _get_confirmed_date(self, cr, uid, context=None):
'''
Returns confirmed date
'''
if context is None:
context = {}
order_obj= self.pool.get('sale.order')
res = (datetime.now() + relativedelta(days=+2)).strftime('%Y-%m-%d')
if context.get('sale_id', False):
so = order_obj.browse(cr, uid, context.get('sale_id'), context=context)
res = so.delivery_confirmed_date
return res
def _get_default_state(self, cr, uid, context=None):
'''
default value for state fields.related
why, beacause if we try to pass state in the context,
the context is simply reset without any values specified...
'''
if context is None:
context= {}
if context.get('sale_id', False):
order_obj= self.pool.get('sale.order')
so = order_obj.browse(cr, uid, context.get('sale_id'), context=context)
return so.state
return False
def _get_uom_def(self, cr, uid, context=None):
if context is None:
context= {}
try:
ids = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_unit')
except ValueError:
return False
if ids:
return ids[1]
return False
_columns = {'date_planned': fields.date(string='Delivery Requested Date', required=True, select=True,
help='Header level dates has to be populated by default with the possibility of manual updates'),
'confirmed_delivery_date': fields.date(string='Delivery Confirmed Date',
help='Header level dates has to be populated by default with the possibility of manual updates.'),
'so_state_stored': fields.related('order_id', 'state', type='selection', selection=SALE_ORDER_STATE_SELECTION, string='So State', readonly=True,),
}
_defaults = {'date_planned': _get_planned_date,
'confirmed_delivery_date': _get_confirmed_date,
'so_state_stored': _get_default_state,
'type': lambda *a: 'make_to_order',
'product_uom': _get_uom_def,
}
def copy_data(self, cr, uid, id, default=None, context=None):
'''
erase dates
'''
if default is None:
default = {}
if context is None:
context = {}
if not context.get('keepDateAndDistrib'):
if 'confirmed_delivery_date' not in default:
default['confirmed_delivery_date'] = False
if 'date_planned' not in default:
default['date_planned'] = (datetime.now() + relativedelta(days=+2)).strftime('%Y-%m-%d')
return super(sale_order_line, self).copy_data(cr, uid, id, default=default, context=context)
def dates_change(self, cr, uid, ids, requested_date, confirmed_date, context=None):
'''
Checks if dates are later than header dates
deprecated
'''
if context is None:
context = {}
return common_dates_change_on_line(self, cr, uid, ids, requested_date, confirmed_date, 'sale.order', context=context)
sale_order_line()
class procurement_order(osv.osv):
'''
date modifications
'''
_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 context is None:
context = {}
line = super(procurement_order, self).po_line_values_hook(cr, uid, ids, context=context, *args, **kwargs)
procurement = kwargs['procurement']
# date_planned (requested date) = date_planned from procurement order (rts - prepartion lead time)
# confirmed_delivery_date (confirmed date) = False
line.update({'date_planned': procurement.date_planned, 'confirmed_delivery_date': False,})
return line
def po_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 creation
'''
if context is None:
context = {}
values = super(procurement_order, self).po_values_hook(cr, uid, ids, context=context, *args, **kwargs)
line = kwargs['line']
procurement = kwargs['procurement']
# update from yml flag
values['from_yml_test'] = procurement.from_yml_test
# date_planned (requested date) = date_planned from procurement order (rts - prepartion lead time)
# confirmed_delivery_date (confirmed date) = False
# both values are taken from line
values.update({'delivery_requested_date': line['date_planned'], 'delivery_confirmed_date': line['confirmed_delivery_date'],})
return values
procurement_order()
class stock_picking(osv.osv):
_name = 'stock.picking'
_inherit = 'stock.picking'
def get_min_max_date(self, cr, uid, ids, field_name, arg, context=None):
'''
call super - modify logic for min_date (Expected receipt date)
'''
if context is None:
context = {}
date_tools = self.pool.get('date.tools')
fields_tools = self.pool.get('fields.tools')
db_date_format = date_tools.get_db_date_format(cr, uid, context=context)
result = super(stock_picking, self).get_min_max_date(cr, uid, ids, field_name, arg, context=context)
# modify the min_date value for delivery_confirmed_date from corresponding purchase_order if exist
for obj in self.browse(cr, uid, ids, context=context):
if not result[obj.id].get('min_date') and obj.manual_min_date_stock_picking:
result.setdefault(obj.id, {}).update({'min_date': obj.manual_min_date_stock_picking})
elif result[obj.id].get('min_date'):
continue
else:
if obj.purchase_id:
result.setdefault(obj.id, {}).update({'min_date': obj.purchase_id.delivery_confirmed_date})
if obj.sale_id:
# rts is a mandatory field
if obj.subtype == 'standard':
# rts + shipment lt
shipment_lt = fields_tools.get_field_from_company(cr, uid, object=self._name, field='shipment_lead_time', context=context)
rts = datetime.strptime(obj.sale_id.ready_to_ship_date, db_date_format)
rts = rts + relativedelta(days=shipment_lt or 0)
rts = rts.strftime(db_date_format)
result.setdefault(obj.id, {}).update({'min_date': rts})
elif obj.subtype == 'picking':
# rts
result.setdefault(obj.id, {}).update({'min_date': obj.sale_id.ready_to_ship_date})
elif obj.subtype == 'ppl':
# today
today = time.strftime(db_date_format)
result.setdefault(obj.id, {}).update({'min_date': today})
return result
def _set_minimum_date(self, cr, uid, ids, name, value, arg, context=None):
'''
set the manual conterpart of min_date
'''
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
self.write(cr, uid, ids, {'manual_min_date_stock_picking': value}, context=context)
return True
# utp-360: I rename the date 'Actual Receipt Date' because before it was 'Creation Date'
_columns = {'date': fields.datetime('Actual Receipt Date', help="Date of Order", select=True),
'min_date': fields.function(get_min_max_date, fnct_inv=_set_minimum_date, multi="min_max_date",
method=True, store=True, type='datetime', string='Expected Date', select=1,
help="Expected date for the picking to be processed"),
'manual_min_date_stock_picking': fields.datetime(string='Manual Date'),
'min_date_manually': fields.boolean(string='change manually')
}
_defaults = {'manual_min_date_stock_picking': False,
'min_date_manually': False,}
def change_min_date(self, cr, uid, ids, context=None):
return {'value': {'min_date_manually': True}}
def create(self, cr, uid, vals, context=None):
if not vals.get('purchase id') or not vals.get('sale_id'):
vals['manual_min_date_stock_picking'] = vals.get('min_date')
return super(stock_picking, self).create(cr, uid, vals, context=context)
def write(self, cr, uid, ids, vals, context=None):
'''
Update all stock moves if the min_date of the picking was changed manually
'''
move_obj = self.pool.get('stock.move')
for pick in self.browse(cr, uid, ids, context=context):
if vals.get('min_date_manually', pick.min_date_manually) and vals.get('min_date', vals.get('manual_min_date_stock_picking')):
vals.update({'manual_min_date_stock_picking': vals.get('min_date', vals.get('manual_min_date_stock_picking'))})
move_ids = move_obj.search(cr, uid, [('picking_id', '=', pick.id), ('state', 'not in', ('done', 'cancel'))], context=context)
move_obj.write(cr, uid, move_ids, {'date_expected': vals.get('min_date', vals.get('manual_min_date_stock_picking'))}, context=context)
return super(stock_picking, self).write(cr, uid, ids, vals, context=context)
# @@@override stock>stock.py>stock_picking>do_partial
def do_partial(self, cr, uid, ids, partial_datas, context=None):
'''
shipment date of sale order is updated and logged
'''
if context is None:
context = {}
date_tools = self.pool.get('date.tools')
res = super(stock_picking, self).do_partial(cr, uid, ids, partial_datas, context=context)
so_obj = self.pool.get('sale.order')
for picking in self.browse(cr, uid, ids, context=context):
if picking.sale_id and not picking.sale_id.shipment_date and not picking.sale_id.procurement_request:
sale_id = picking.sale_id.id
date_format = date_tools.get_date_format(cr, uid, context=context)
db_date_format = date_tools.get_db_date_format(cr, uid, context=context)
today = time.strftime(date_format)
today_db = time.strftime(db_date_format)
so_obj.write(cr, uid, [sale_id], {'shipment_date': today_db})
so_obj.log(cr, uid, sale_id, _("Shipment Date of the Field Order '%s' has been updated to %s.")%(picking.sale_id.name, today))
return res
stock_picking()
class stock_move(osv.osv):
'''
shipment date of sale order is updated
'''
_inherit = 'stock.move'
def default_get(self, cr, uid, fields, context=None):
if not context:
context = {}
res = super(stock_move, self).default_get(cr, uid, fields, context=context)
res['date'] = res['date_expected'] = context.get('date_expected', time.strftime('%Y-%m-%d %H:%M:%S'))
return res
def do_partial(self, cr, uid, ids, partial_datas, context=None):
'''
update shipment date and logged
'''
if context is None:
context = {}
date_tools = self.pool.get('date.tools')
res = super(stock_move, self).do_partial(cr, uid, ids, partial_datas, context=context)
so_obj = self.pool.get('sale.order')
for obj in self.browse(cr, uid, ids, context=context):
if obj.picking_id and obj.picking_id.sale_id and not obj.picking_id.sale_id.shipment_date:
sale_id = obj.picking_id.sale_id.id
date_format = date_tools.get_date_format(cr, uid, context=context)
db_date_format = date_tools.get_db_date_format(cr, uid, context=context)
today = time.strftime(date_format)
today_db = time.strftime(db_date_format)
so_obj.write(cr, uid, [sale_id], {'shipment_date': today_db})
so_obj.log(cr, uid, sale_id, _("Shipment Date of the Field Order '%s' has been updated to %s.")%(obj.picking_id.sale_id.name, today))
return res
stock_move()
class res_company(osv.osv):
'''
add time related fields
'''
_inherit = 'res.company'
_columns = {'shipment_lead_time': fields.float(digits=(16,2), string='Shipment Lead Time'),
'preparation_lead_time': fields.float(digits=(16,2), string='Preparation Lead Time'),
}
_defaults = {'shipment_lead_time': 0.0,
'preparation_lead_time': 0.0,
'po_lead': lambda *a: 0.0, # removed from processes - set to 0.0 for security
}
res_company()
class product_template(osv.osv):
'''
set delay to 0 to be sure it does not take part to processes
'''
_inherit = "product.template"
_defaults = {'sale_delay': lambda *a: 0,
'produce_delay': lambda *a: 0,
}
product_template()
|