~unifield-team/unifield-wm/us-826

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
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
# -*- 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 decimal_precision as dp

# category
from order_types import ORDER_CATEGORY
# integrity
from msf_outgoing import INTEGRITY_STATUS_SELECTION
# claim type
CLAIM_TYPE = [('supplier', 'Supplier'),
              ('customer', 'Customer'),
              ('transport', 'Transport')]

TYPES_FOR_SRC_LOCATION = ['supplier', 'transport']

TYPES_FOR_INTEGRITY = ['supplier']

CLAIM_TYPE_RELATION = {'in': 'supplier',
                       'out': 'customer'}
# claim state
CLAIM_STATE = [('draft', 'Draft'),
               ('in_progress', 'In Progress'),
               ('done', 'Closed')]
# claim rules - define which new event is available after the key designated event
# missing event as key does not accept any event after him
CLAIM_RULES = {'supplier': {'quarantine': ['accept', 'scrap', 'return']}}
# does the claim type allows event creation
CLAIM_TYPE_RULES = {'supplier': ['accept', 'quarantine', 'scrap', 'return'],
                    'customer': ['return'],
                    'transport': False,
                    }
# event type
CLAIM_EVENT_TYPE = [('accept', 'Accept'),
                    ('quarantine', 'Move to Quarantine'),
                    ('scrap', 'Scrap'),
                    ('return', 'Return')]
# event state
CLAIM_EVENT_STATE = [('draft', 'Draft'),
                     ('in_progress', 'In Progress'),
                     ('done', 'Done')]
# event type destination - for return event, the destination depends on 
EVENT_TYPE_DESTINATION = {'accept': 'stock.stock_location_stock', #move to stock
                          'quarantine': 'stock_override.stock_location_quarantine_analyze',
                          'scrap': 'stock_override.stock_location_quarantine_scrap',
                          }
# 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

import tools
from os import path
import logging


class return_claim(osv.osv):
    '''
    claim class
    '''
    _name = 'return.claim'
    
    def create(self, cr, uid, vals, context=None):
        '''
        - add sequence for events
        '''
        seq_tools = self.pool.get('sequence.tools')
        seq_id = seq_tools.create_sequence(cr, uid, vals, 'Return Claim', 'return.claim', prefix='', padding=5, context=context)
        vals.update({'sequence_id_return_claim': seq_id})
        return super(return_claim, self).create(cr, uid, vals, context=context)
    
    def copy_data(self, cr, uid, id, default=None, context=None):
        '''
        reset data
        
        sequence_id is reset in the create method
        '''
        if default is None:
            default = {}
        
        # state is set to draft
        default.update(state='draft')
        # reset the name to get default from sequence
        default.update(name=self.pool.get('ir.sequence').get(cr, uid, 'return.claim'))
        # reset creation date, get today
        default.update(creation_date_return_claim=time.strftime('%Y-%m-%d'))
        # no event
        default.update(event_ids_return_claim=[])
        # reset description
        default.update(description_return_claim=False)
        # reset follow up
        default.update(follow_up_return_claim=False)
        # return super
        return super(return_claim, self).copy_data(cr, uid, id, default, context=context)
    
    def add_event(self, cr, uid, ids, context=None):
        '''
        open add event wizard
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        fields_tools = self.pool.get('fields.tools')
        # we test if new event are allowed
        data = self.allow_new_event(cr, uid, ids, context=context)
        if not all(x['allow'] for x in data.values()):
            # we get an event type and new event are not allowed, the specified type does not allow further events
            event_type_name = [x['last_type'][1] for x in data.values() if (not x['allow'] and x['last_type'])]
            # we get a state, the state of the previous event does not allow further events
            state = [x['state'] for x in data.values() if not x['allow']]
            if event_type_name:
                # not allowed previous event (last_type is present)
                raise osv.except_osv(_('Warning !'), _('Previous event (%s) does not allow further event.')%event_type_name[0])
            elif state:
                # not allowed because of state of last event
                state_name = fields_tools.get_selection_name(cr, uid, object='claim.event', field='state', key=state[0], context=context)
                raise osv.except_osv(_('Warning !'), _('State of previous event (%s) does not allow further event.')%state_name)
            else:
                # not allowed claim type (no last_type)
                claim_type_name = [x['claim_type'][1] for x in data.values()][0]
                raise osv.except_osv(_('Warning !'), _('Claim Type (%s) does not allow events.')%claim_type_name)
        # claim data
        claim_data = claim_type = self.read(cr, uid, ids, ['type_return_claim', 'partner_id_return_claim', 'picking_id_return_claim'], context=context)[0]
        # gather the corresponding claim type
        claim_type = claim_data['type_return_claim']
        # gather the corresponding claim partner
        claim_partner_id = claim_data['partner_id_return_claim']
        # claim origin
        claim_picking_id = claim_data['picking_id_return_claim']
        # data
        name = _("Add an Event")
        model = 'add.event'
        step = 'default'
        wiz_obj = self.pool.get('wizard')
        # open the selected wizard
        res = wiz_obj.open_wizard(cr, uid, ids, name=name, model=model, step=step, context=dict(context,
                                                                                                claim_id=ids[0],
                                                                                                data=data,
                                                                                                claim_type=claim_type,
                                                                                                claim_partner_id=claim_partner_id,
                                                                                                claim_picking_id=claim_picking_id))
        return res
    
    def close_claim(self, cr, uid, ids, context=None):
        '''
        close the claim
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        
        self.write(cr, uid, ids, {'state': 'done'}, context=context)
        return True
    
    def allow_new_event(self, cr, uid, ids, context=None):
        '''
        return True if last event type allows successor event
        the tuple of the last event type (key, name)
        the available type tuple list
        '''
        # objects
        event_obj = self.pool.get('claim.event')
        field_trans = self.pool.get('ir.model.fields').get_selection
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            # allow flag
            allow = False
            # last event type
            last_event_type = False
            # available list
            list = []
            # claim type (key, name)
            claim_type = (obj.type_return_claim, [x[1] for x in self.get_claim_type() if x[0] == obj.type_return_claim][0])
            # state of last event
            state = False
            # we first check if the claim type supports events
            if self.get_claim_type_rules().get(claim_type[0]):
                # order by order_claim_event, so we can easily get the last event
                previous_id = self.get_last_event(cr, uid, obj.id, context=context)[obj.id]
                # if no event, True
                if not previous_id:
                    # depend on the claim type
                    available_list = self.get_claim_type_rules().get(claim_type[0])
                    list = [(x, field_trans(cr, uid, 'claim.event', 'type_claim_event', y[1], context)) for x in available_list for y in self.get_claim_event_type() if y[0] == x]
                    allow = True # list cannot be empty, because other we would not be here!
                else:
                    # we are interested in the last value of returned list -> -1
                    data = event_obj.read(cr, uid, previous_id, ['type_claim_event', 'state'], context=context)
                    # check event state, if not done, allow is False, and list empty
                    if data['state'] != 'done':
                        state = data['state']
                    else:
                        # event type key
                        last_event_type_key = data['type_claim_event']
                        # event type name
                        event_type = self.get_claim_event_type()
                        last_event_type_name = [x[1] for x in event_type if x[0] == last_event_type_key][0]
                        last_event_type = (last_event_type_key, last_event_type_name)
                        # get available selection
                        claim_rules = self.get_claim_rules()
                        # claim type level
                        available_list = claim_rules.get(claim_type[0], False)
                        # event type level
                        available_list = available_list and available_list.get(last_event_type_key, False) or False
                        if available_list:
                            allow = True
                            list = [(x, field_trans(cr, uid, 'claim.event', 'type_claim_event', y[1], context)) for x in available_list for y in self.get_claim_event_type() if y[0] == x]
            # update result
            result[obj.id] = {'allow': allow,
                              'last_type': last_event_type,
                              'list': list,
                              'claim_type': claim_type,
                              'state': state}
        return result
    
    def check_product_lines_integrity(self, cr, uid, ids, context=None):
        '''
        integrity check on product lines
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        
        # objects
        prod_obj = self.pool.get('product.product')
        lot_obj = self.pool.get('stock.production.lot')
        move_obj = self.pool.get('stock.move')
        
        # errors
        errors = {'missing_src_location': False, # source location is missing
                  'missing_lot': False, # production lot is missing for product with batch management or perishable
                  'wrong_lot_type_need_standard': False, # the selected production lot type is wrong
                  'wrong_lot_type_need_internal': False, # the selected production lot type is wrong
                  'no_lot_needed': False, # this line should not have production lot
                  'not_exist_in_picking': False, # corresponding product does not exist in the selected origin picking
                  'must_be_greater_than_0': False, # the selected quantity must be greater than 0.0
                  'not_available': False, # the selected quantity is not available for selected product and lot from the selected location
                  }
        for obj in self.browse(cr, uid, ids, context=context):
            # products must not be empty
            if not len(obj.product_line_ids_return_claim):
                raise osv.except_osv(_('Warning !'), _('Product list is empty.'))
            for item in obj.product_line_ids_return_claim:
                # reset the integrity status
                item.write({'integrity_status_claim_product_line': 'empty'}, context=context)
                # product management type
                data = prod_obj.read(cr, uid, [item.product_id_claim_product_line.id], ['batch_management', 'perishable', 'type', 'subtype'], context=context)[0]
                management = data['batch_management']
                perishable = data['perishable']
                asset = data['type'] == 'product' and data['subtype'] == 'asset'
                kit = data['type'] == 'product' and data['subtype'] == 'kit'
                # check qty
                if item.qty_claim_product_line <= 0.0:
                    errors.update(must_be_greater_than_0=True)
                    item.write({'integrity_status_claim_product_line': 'must_be_greater_than_0'}, context=context)
                # check availability
                if item.qty_claim_product_line > item.hidden_stock_available_claim_product_line:
                    errors.update(not_available=True)
                    item.write({'integrity_status_claim_product_line': 'not_available'}, context=context)
                # check the src location
                if obj.type_return_claim == 'supplier':
                    if not item.src_location_id_claim_product_line:
                        # src location is missing and claim type is supplier
                        errors.update(missing_src_location=True)
                        item.write({'integrity_status_claim_product_line': 'missing_src_location'}, context=context)
                # check management
                if management:
                    if not item.lot_id_claim_product_line:
                        # lot is needed
                        errors.update(missing_lot=True)
                        item.write({'integrity_status': 'missing_lot'}, context=context)
                    else:
                        # we check the lot type is standard
                        data = lot_obj.read(cr, uid, [item.lot_id_claim_product_line.id], ['life_date','name','type'], context=context)
                        lot_type = data[0]['type']
                        if lot_type != 'standard':
                            errors.update(wrong_lot_type_need_standard=True)
                            item.write({'integrity_status': 'wrong_lot_type_need_standard'}, context=context)
                elif perishable:
                    if not item.lot_id_claim_product_line:
                        # lot is needed
                        errors.update(missing_lot=True)
                        item.write({'integrity_status': 'missing_lot'}, context=context)
                    else:
                        # we check the lot type is internal
                        data = lot_obj.read(cr, uid, [item.lot_id_claim_product_line.id], ['life_date','name','type'], context=context)
                        lot_type = data[0]['type']
                        if lot_type != 'internal':
                            errors.update(wrong_lot_type_need_internal=True)
                            item.write({'integrity_status': 'wrong_lot_type_need_internal'}, context=context)
                else:
                    # no lot needed - no date needed
                    if item.lot_id_claim_product_line:
                        errors.update(no_lot_needed=True)
                        item.write({'integrity_status': 'no_lot_needed'}, context=context)
                # verify existence in selected picking
                move_ids = move_obj.search(cr, uid, [('picking_id', '=', obj.picking_id_return_claim.id),
                                                     ('product_id', '=', item.product_id_claim_product_line.id),
                                                     ('asset_id', '=', item.asset_id_claim_product_line.id),
                                                     ('composition_list_id', '=', item.composition_list_id_claim_product_line.id),
                                                     ('prodlot_id', '=', item.lot_id_claim_product_line.id),
                                                     ('product_qty', '>=', item.qty_claim_product_line),
                                                     ], context=context)
                if not move_ids:
                    errors.update(not_exist_in_picking=True)
                    item.write({'integrity_status_claim_product_line': 'not_exist_in_picking'}, context=context)
                
        # check the encountered errors
        return all([not x for x in errors.values()])
    
    def get_last_event(self, cr, uid, ids, pos=-1, context=None):
        '''
        get the last id of event for each claim
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        event_obj = self.pool.get('claim.event')
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            # false as default
            result[obj.id] = False
            # gather previous events - thanks to order, we have the event in the correct order
            previous_ids = event_obj.search(cr, uid, [('return_claim_id_claim_event', '=', obj.id)], order='order_claim_event', context=context)
            try:
                # -1, get the last one (by default)
                result[obj.id] = previous_ids[pos]
            except IndexError:
                # do nothing, we'll get False as a result
                pass
        
        return result
    
    def load_products(self, cr, uid, ids, context=None):
        '''
        load products data from selected origin
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        product_line_obj = self.pool.get('claim.product.line')
        for obj in self.browse(cr, uid, ids, context=context):
            # clear existing products
            line_ids = product_line_obj.search(cr, uid, [('claim_id_claim_product_line', '=', obj.id)], context=context)
            product_line_obj.unlink(cr, uid, line_ids, context=context)
            # create new ones
            for move in obj.picking_id_return_claim.move_lines:
                # create corresponding product line
                if obj.type_return_claim not in TYPES_FOR_SRC_LOCATION:
                    src_location_id = False
                else:
                    src_location_id = obj.default_src_location_id_return_claim.id
                product_line_values = {'qty_claim_product_line': move.product_qty,
                                       'claim_id_claim_product_line': obj.id,
                                       'product_id_claim_product_line': move.product_id.id,
                                       'uom_id_claim_product_line': move.product_uom.id,
                                       'lot_id_claim_product_line': move.prodlot_id.id,
                                       'expiry_date_claim_product_line': move.expired_date,
                                       'asset_id_claim_product_line' : move.asset_id.id,
                                       'composition_list_id_claim_product_line': move.composition_list_id.id,
                                       'src_location_id_claim_product_line': src_location_id}
                
                new_prod_id = product_line_obj.create(cr, uid, product_line_values, context=context)
            
            return True
            return {'name': _('Claim'),
                    'view_mode': 'form,tree',
                    'view_id': False,
                    'view_type': 'form',
                    'res_model': 'return.claim',
                    'res_id': [obj.id],
                    'type': 'ir.actions.act_window',
                    'target': 'crash',
                    'domain': '[]',
                    'context': context}
    
    def on_change_origin(self, cr, uid, ids, picking_id, context=None):
        '''
        origin on change function
        '''
        # objects
        pick_obj = self.pool.get('stock.picking')
        result = {'value': {'partner_id_return_claim': False,
                            'type_return_claim': False,
                            'category_return_claim': False,
                            'po_so_return_claim': False}}
        if picking_id:
            # partner from picking
            data = pick_obj.read(cr, uid, picking_id, ['partner_id2', 'type', 'order_category', 'origin'], context=context)
            partner_id = data.get('partner_id2', False)
            if partner_id:
                partner_id = partner_id[0]
            type = data['type']
            # convert the picking type for the corresponding claim type
            type = CLAIM_TYPE_RELATION.get(type, False)
            # category
            category = data['order_category']
            # origin
            origin = data['origin']
            # update result dictionary
            result['value'].update({'partner_id_return_claim': partner_id,
                                    'type_return_claim': type,
                                    'category_return_claim': category,
                                    'po_so_return_claim': origin})
        
        return result
    
    def _vals_get_claim(self, cr, uid, ids, fields, arg, context=None):
        '''
        multi fields function method
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        # results
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            result[obj.id] = {'contains_event_return_claim': len(obj.event_ids_return_claim) > 0}
            result[obj.id] = {'fake_state_return_claim': obj.state}
            
        return result
    
    _columns = {'name': fields.char(string='Reference', size=1024, required=True), # default value
                'creation_date_return_claim': fields.date(string='Creation Date', required=True), # default value
                'po_so_return_claim': fields.char(string='Order reference', size=1024),
                'order_line_number_return_claim': fields.char(string='Order line number', size=1024),
                'type_return_claim': fields.selection(CLAIM_TYPE, string='Type', required=True),
                'category_return_claim': fields.selection(ORDER_CATEGORY, string='Category'),
                'description_return_claim': fields.text(string='Description'),
                'follow_up_return_claim': fields.text(string='Follow Up'),
                'state': fields.selection(CLAIM_STATE, string='State', readonly=True), # default value
                # many2one
                'sequence_id_return_claim': fields.many2one('ir.sequence', 'Events Sequence', required=True, ondelete='cascade'), # from create function
                'partner_id_return_claim': fields.many2one('res.partner', string='Partner', required=True),
                'po_id_return_claim': fields.many2one('purchase.order', string='Purchase Order'),
                'so_id_return_claim': fields.many2one('sale.order', string='Sale Order'),
                'picking_id_return_claim': fields.many2one('stock.picking', string='Reception/Shipment reference', required=True), #origin
                'default_src_location_id_return_claim': fields.many2one('stock.location', string='Default Source Location', required=True), # default value
                # one2many
                'event_ids_return_claim': fields.one2many('claim.event', 'return_claim_id_claim_event', string='Events'),
                'product_line_ids_return_claim': fields.one2many('claim.product.line', 'claim_id_claim_product_line', string='Products'),
                # functions
                'contains_event_return_claim': fields.function(_vals_get_claim, method=True, string='Contains Events', type='boolean', readonly=True, multi='get_vals_claim'),
                'fake_state_return_claim': fields.function(_vals_get_claim, method=True, string='Fake State', type='selection', selection=CLAIM_STATE, readonly=True, multi='get_vals_claim'),
                }
    
    _defaults = {'creation_date_return_claim': lambda *a: time.strftime('%Y-%m-%d'),
                 'name': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'return.claim'),
                 'default_src_location_id_return_claim': lambda obj, cr, uid, c: obj.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_stock') and obj.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_stock')[1] or False,
                 'state': 'draft',
                 'fake_state_return_claim': 'draft',
                 'po_id_return_claim': False,
                 'so_id_return_claim': False,
                 }
    
    def _check_claim(self, cr, uid, ids, context=None):
        """
        claim checks
        """
        if not context:
            context={}
            
        # objects
        fields_tools = self.pool.get('fields.tools')
        for obj in self.browse(cr, uid, ids, context=context):
            # the selected origin must contain stock moves
            if not len(obj.picking_id_return_claim.move_lines) > 0:
                raise osv.except_osv(_('Warning !'), _('Selected Origin must contain at least one stock move.'))
            # the selected origin must be done
            if obj.picking_id_return_claim.state != 'done':
                raise osv.except_osv(_('Warning !'), _('Selected Origin must be in Done state.'))
            # origin type
            if obj.picking_id_return_claim.type not in ['in', 'out']:
                raise osv.except_osv(_('Warning !'), _('Selected Origin must be either an Incoming Shipment or a Delivery Order or a Picking Ticket.'))
            # origin subtype
            if obj.picking_id_return_claim.subtype not in ['standard', 'picking']:
                raise osv.except_osv(_('Warning !'), _('PPL or Packing should not be selected.'))
            # not draft picking ticket, even if done
            if obj.picking_id_return_claim.subtype == 'picking' and not obj.picking_id_return_claim.backorder_id:
                raise osv.except_osv(_('Warning !'), _('Draft Picking Tickets are not allowed as Origin, Picking Ticket must be selected.'))
            # if claim type does not allow events, no events should be present
            if not self.get_claim_type_rules().get(obj.type_return_claim) and (len(obj.event_ids_return_claim) > 0):
                raise osv.except_osv(_('Warning !'), _('Events are not allowed for selected Claim Type.'))
            # if claim type does not allow the selected event type, this event should not be present
            if self.get_claim_type_rules().get(obj.type_return_claim) and (len(obj.event_ids_return_claim) > 0):
                # event must be part of allowed types
                for event in obj.event_ids_return_claim:
                    if event.type_claim_event not in self.get_claim_type_rules().get(obj.type_return_claim):
                        event_name = fields_tools.get_selection_name(cr, uid, object='claim.event', field='type_claim_event', key=event.type_claim_event, context=context)
                        raise osv.except_osv(_('Warning !'), _('Event (%s) is not allowed for selected Claim Type.')%event_name)
            # if supplier, origin must be in
            if obj.type_return_claim == 'supplier' and obj.picking_id_return_claim.type != 'in':
                raise osv.except_osv(_('Warning !'), _('Origin for supplier claim must be Incoming Shipment.'))
            # if customer, origin must be out
            if obj.type_return_claim == 'customer' and obj.picking_id_return_claim.type != 'out':
                raise osv.except_osv(_('Warning !'), _('Origin for customer claim must be Deliery Order or Picking Ticket.'))
        return True

    _constraints = [
        (_check_claim, 'Claim Error', []),
    ]
    
    def get_claim_type(self):
        '''
        return claim types
        '''
        return CLAIM_TYPE
    
    def get_claim_rules(self):
        '''
        return claim rules
        '''
        return CLAIM_RULES
    
    def get_claim_type_rules(self):
        '''
        return claim_type_rules
        '''
        return CLAIM_TYPE_RULES
    
    def get_claim_event_type(self):
        '''
        return claim_event_type
        '''
        return CLAIM_EVENT_TYPE
    
    _order = 'name desc'
    
return_claim()


class claim_event(osv.osv):
    '''
    event for claims
    '''
    _name = 'claim.event'
    
    def create(self, cr, uid, vals, context=None):
        '''
        set default name value
        '''
        obj = self.pool.get('return.claim').browse(cr, uid, vals['return_claim_id_claim_event'], context=context)
        sequence = obj.sequence_id_return_claim
        line = sequence.get_id(code_or_id='id', context=context)
        vals.update({'name': 'EV/%s'%line, 'order_claim_event': int(line)})
        return super(claim_event, self).create(cr, uid, vals, context=context)
    
    def unlink(self, cr, uid, ids, context=None):
        '''
        only draft event can be deleted
        '''
        data = self.read(cr, uid, ids, ['state'], context=context)
        if not all([x['state'] == 'draft' for x in data]):
            raise osv.except_osv(_('Warning !'), _('Only Event in draft state can be deleted.'))
        return super(claim_event, self).unlink(cr, uid, ids, context=context)
    
    def get_location_for_event_type(self, cr, uid, context=None, *args, **kwargs):
        '''
        get the destination location for event type
        '''
        # objects
        obj_data = self.pool.get('ir.model.data')
        partner_obj = self.pool.get('res.partner')
        # location_id
        location_id = False
        # event type
        event_type = kwargs['event_type']
        # origin browse object
        origin = kwargs['claim_picking']
        # claim type
        claim_type = kwargs['claim_type']
        # partner
        partner_id = kwargs['claim_partner_id']
        # not event type
        if not event_type or not origin:
            return False
        # treat each event type
        if event_type == 'return':
            if claim_type == 'supplier':
                # property_stock_supplier from partner
                data = partner_obj.read(cr, uid, partner_id, ['property_stock_supplier'], context=context)
                location_id = data['property_stock_supplier'][0]
            elif claim_type == 'customer':
                # property_stock_customer from partner
                data = partner_obj.read(cr, uid, partner_id, ['property_stock_customer'], context=context)
                location_id = data['property_stock_customer'][0]
            else:
                # should not be called for other types
                pass
        else:
            # we find the corresponding data reference from the dic
            module = EVENT_TYPE_DESTINATION.get(event_type).split('.')[0]
            name = EVENT_TYPE_DESTINATION.get(event_type).split('.')[1]
            location_id = obj_data.get_object_reference(cr, uid, module, name)[1]
        # return the id of the corresponding location
        return location_id
    
    def _validate_picking(self, cr, uid, ids, context=None):
        '''
        validate the picking full process
        '''
        # objects
        picking_tools = self.pool.get('picking.tools')
        picking_tools.all(cr, uid, ids, context=context)
        return True
    
    def _do_process_accept(self, cr, uid, obj, context=None):
        '''
        process logic for accept event
        
        - no change to event picking
        '''
        # objects
        picking_tools = self.pool.get('picking.tools')
        # event picking object
        event_picking = obj.event_picking_id_claim_event
        # confirm the picking - in custom event function because we need to take the type of picking into account for self.log messages
        picking_tools.confirm(cr, uid, event_picking.id, context=context)
        # we check availability for created or wizard picking (wizard picking can be waiting as it is chained picking)
        picking_tools.check_assign(cr, uid, event_picking.id, context=context)
        # validate the event picking if not from picking wizard
        if not obj.from_picking_wizard_claim_event:
            self._validate_picking(cr, uid, event_picking.id, context=context)
        return True
        
    def _do_process_quarantine(self, cr, uid, obj, context=None):
        '''
        process logic for quarantine event
        
        - destination of picking moves becomes Quarantine (Analyze)
        '''
        # objects
        move_obj = self.pool.get('stock.move')
        picking_tools = self.pool.get('picking.tools')
        # event picking object
        event_picking = obj.event_picking_id_claim_event
        # confirm the picking - in custom event function because we need to take the type of picking into account for self.log messages
        picking_tools.confirm(cr, uid, event_picking.id, context=context)
        # we check availability for created or wizard picking (wizard picking can be waiting as it is chained picking)
        picking_tools.check_assign(cr, uid, event_picking.id, context=context)
        # update the destination location for each move
        move_ids = [move.id for move in event_picking.move_lines]
        move_obj.write(cr, uid, move_ids, {'location_dest_id': context['common']['quarantine_anal']}, context=context)
        # validate the event picking if not from picking wizard
        if not obj.from_picking_wizard_claim_event:
            self._validate_picking(cr, uid, event_picking.id, context=context)
        return True
        
    def _do_process_scrap(self, cr, uid, obj, context=None):
        '''
        process logic for scrap event
        
        - destination of picking moves becomes Quarantine (before scrap)
        '''
        # objects
        move_obj = self.pool.get('stock.move')
        picking_tools = self.pool.get('picking.tools')
        # event picking object
        event_picking = obj.event_picking_id_claim_event
        # confirm the picking - in custom event function because we need to take the type of picking into account for self.log messages
        picking_tools.confirm(cr, uid, event_picking.id, context=context)
        # we check availability for created or wizard picking (wizard picking can be waiting as it is chained picking)
        picking_tools.check_assign(cr, uid, event_picking.id, context=context)
        # update the destination location for each move
        move_ids = [move.id for move in event_picking.move_lines]
        move_obj.write(cr, uid, move_ids, {'location_dest_id': context['common']['quarantine_scrap']}, context=context)
        # validate the event picking if not from picking wizard
        if not obj.from_picking_wizard_claim_event:
            self._validate_picking(cr, uid, event_picking.id, context=context)
        return True
        
    def _do_process_return(self, cr, uid, obj, context=None):
        '''
        process logic for return event
        
        - depends on the type of claim - supplier or customer
        - destination of picking moves becomes original Supplier/Customer [property_stock_supplier or property_stock_customer from res.partner]
        - name of picking becomes IN/0001 -> IN/0001-return type 'out' for supplier
        - name of picking becomes OUT/0001 -> OUT/0001-return type 'in' for customer
        - (is not set to done - defined in _picking_done_cond)
        - if replacement is needed, we create a new picking
        '''
        context = context.copy()
        context.update({'from_claim': True})
        
        # objects
        move_obj = self.pool.get('stock.move')
        pick_obj = self.pool.get('stock.picking')
        picking_tools = self.pool.get('picking.tools')
        # event picking object
        event_picking = obj.event_picking_id_claim_event
        event_picking_id = event_picking.id
        # origin picking in/out
        origin_picking = obj.return_claim_id_claim_event.picking_id_return_claim
        # claim
        claim = obj.return_claim_id_claim_event
        # claim type
        claim_type = claim.type_return_claim
        # new name, previous name + -return
        new_name = origin_picking.name + '-return'
        # get the picking values and move values according to claim type
        picking_values = {'name': new_name,
                          'partner_id': claim.partner_id_return_claim.id, # both partner needs to be filled??
                          'partner_id2': claim.partner_id_return_claim.id,
                          'purchase_id': origin_picking.purchase_id.id,
                          'sale_id': origin_picking.sale_id.id,
                          'reason_type_id': context['common']['rt_goods_return'],
                          'invoice_state': '2binvoiced'}
        move_values = {'reason_type_id': context['common']['rt_goods_return']}
        if claim_type == 'supplier':
            picking_values.update({'type': 'out'})
            # moves go back to supplier, source location comes from input (if dynamic) or from claim product values
            move_values.update({'location_dest_id': claim.partner_id_return_claim.property_stock_supplier.id})
        elif claim_type == 'customer':
            picking_values.update({'type': 'in'})
            # receive return from customer, and go into input
            move_values.update({'location_id': claim.partner_id_return_claim.property_stock_customer.id,
                                'location_dest_id': context['common']['input_id']})
        # update the picking
        pick_obj.write(cr, uid, [event_picking_id], picking_values, context=context)
        # confirm the picking - in custom event function because we need to take the type of picking into account for self.log messages
        picking_tools.confirm(cr, uid, event_picking_id, context=context)
        # update the picking again - strange bug on runbot, the type was internal again...
        pick_obj.write(cr, uid, [event_picking_id], picking_values, context=context)
        # update the destination location for each move
        move_ids = [move.id for move in event_picking.move_lines]
        # get the move values according to claim type
        move_obj.write(cr, uid, move_ids, move_values, context=context)
        # check assign for event picking
        picking_tools.check_assign(cr, uid, event_picking.id, context=context)
        # do we need replacement?
        if obj.replacement_picking_expected_claim_event:
            # we update the replacement picking object and lines
            # new name, previous name + -return
            replacement_name = origin_picking.name + '-replacement'
            replacement_values = {'name': replacement_name,
                                  'partner_id': claim.partner_id_return_claim.id, #both partner needs to be filled??
                                  'partner_id2': claim.partner_id_return_claim.id,
                                  'reason_type_id': context['common']['rt_goods_replacement'],
                                  'purchase_id': origin_picking.purchase_id.id,
                                  'sale_id': origin_picking.sale_id.id,
                                  'invoice_state': '2binvoiced',
                                  }
            replacement_move_values = {'reason_type_id': context['common']['rt_goods_replacement']}
            
            if claim_type == 'supplier':
                replacement_values.update({'type': 'in'})
                # receive back from supplier, destination default input
                replacement_move_values.update({'location_id': claim.partner_id_return_claim.property_stock_supplier.id,
                                                'location_dest_id': context['common']['input_id']})
            elif claim_type == 'customer':
                replacement_values.update({'type': 'out'})
                # resend to customer, from stock by default (can be changed by user later)
                replacement_move_values.update({'location_id': context['common']['stock_id'],
                                                'location_dest_id': claim.partner_id_return_claim.property_stock_customer.id})
            # we copy the event return picking
            replacement_id = pick_obj.copy(cr, uid, event_picking.id, replacement_values, context=dict(context, keepLineNumber=True))
            # update the moves
            replacement_move_ids = move_obj.search(cr, uid, [('picking_id', '=', replacement_id)], context=context)
            # get the move values according to claim type
            move_obj.write(cr, uid, replacement_move_ids, replacement_move_values, context=context)
            # confirm and check availability of replacement picking
            picking_tools.confirm(cr, uid, replacement_id, context=context)
            picking_tools.check_assign(cr, uid, replacement_id, context=context)
                
        return True
    
    def do_process_event(self, cr, uid, ids, context=None):
        '''
        button function call - from_picking is False
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        claim_ids = context['active_ids']
        self._do_process_event(cr, uid, ids, context=context)
        
        return {'name': _('Claim'),
                'view_mode': 'form,tree',
                'view_id': False,
                'view_type': 'form',
                'res_model': 'return.claim',
                'res_id': claim_ids,
                'type': 'ir.actions.act_window',
                'target': 'crash',
                'domain': '[]',
                'context': context}
        
    def _do_process_event(self, cr, uid, ids, context=None):
        '''
        process the events
        
        - create a picking if not coming from chained picking processing
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        claim_obj = self.pool.get('return.claim')
        fields_tools = self.pool.get('fields.tools')
        data_tools = self.pool.get('data.tools')
        pick_obj = self.pool.get('stock.picking')
        move_obj = self.pool.get('stock.move')
        # load common data
        data_tools.load_common_data(cr, uid, ids, context=context)
        # base of function names
        base_func = '_do_process_'
        for obj in self.browse(cr, uid, ids, context=context):
            # event must be draft
            if obj.state != 'draft':
                raise osv.except_osv(_('Warning !'), _('Only events in state draft can be processed.'))
            # integrity check on product lines for corresponding claim - only for first event - only if from scratch - if from wizard, the checks must be done from the wizard logic
            # we do not check integrity from wizard because of force availability option at picking level -> processing even if no stock available
            events = obj.return_claim_id_claim_event.event_ids_return_claim
            integrity_check = True
            if len(events) == 1 and not events[0].from_picking_wizard_claim_event and obj.return_claim_id_claim_event.type_return_claim in TYPES_FOR_INTEGRITY:
                integrity_check = claim_obj.check_product_lines_integrity(cr, uid, obj.return_claim_id_claim_event.id, context=context)
            if not integrity_check:
                # return False
                return False
            # create picking object if not coming from chained IN picking process
            if not obj.from_picking_wizard_claim_event:
                # claim
                claim = obj.return_claim_id_claim_event
                # we use default values as if the picking was from chained creation (so type is 'internal')
                # different values need according to event type is then replaced during specific do_process functions
                event_picking_values = {'type': 'internal',
                                        'partner_id2': claim.partner_id_return_claim.id,
                                        'origin': claim.po_so_return_claim,
                                        'order_category': claim.category_return_claim,
                                        'reason_type_id': context['common']['rt_internal_supply'],
                                        }
                # create picking
                new_event_picking_id = pick_obj.create(cr, uid, event_picking_values, context=context)
                # we are interested in the previous value, as we are processing the last one, we must seek for -2 index
                previous_id = claim_obj.get_last_event(cr, uid, claim.id, pos=-2, context=context)[claim.id]
                if previous_id:
                    # previous event
                    previous = self.browse(cr, uid, previous_id, context=context)
                    # we've got a previous event, so we copy the moves from the previous event picking
                    # with destination as source for the new picking event
                    moves_lines = [(0, 0, {'name': x.name,
                                           'product_id': x.product_id.id,
                                           'asset_id': x.asset_id.id,
                                           'composition_list_id': x.composition_list_id.id,
                                           'prodlot_id': x.prodlot_id.id,
                                           'date': context['common']['date'],
                                           'date_expected': context['common']['date'],
                                           'product_qty': x.product_qty,
                                           'product_uom': x.product_uom.id,
                                           'product_uos_qty': x.product_uos_qty,
                                           'product_uos': x.product_uos.id,
                                           'location_id': x.location_dest_id.id,
                                           'location_dest_id': context['common']['stock_id'],
                                           'company_id': context['common']['company_id'],
                                           'reason_type_id': context['common']['rt_internal_supply']}) for x in previous.event_picking_id_claim_event.move_lines]
                else:
                    # no previous event, we generate the moves based on the claim's product lines
                    moves_lines = [(0, 0, {'name': x.name,
                                           'product_id': x.product_id_claim_product_line.id,
                                           'asset_id': x.asset_id_claim_product_line.id,
                                           'composition_list_id': x.composition_list_id_claim_product_line.id,
                                           'prodlot_id': x.lot_id_claim_product_line.id,
                                           'date': context['common']['date'],
                                           'date_expected': context['common']['date'],
                                           'product_qty': x.qty_claim_product_line,
                                           'product_uom': x.uom_id_claim_product_line.id,
                                           'product_uos_qty': x.qty_claim_product_line,
                                           'product_uos': x.uom_id_claim_product_line.id,
                                           'location_id': x.src_location_id_claim_product_line.id,
                                           'location_dest_id': context['common']['stock_id'],
                                           'company_id': context['common']['company_id'],
                                           'reason_type_id': context['common']['rt_internal_supply']}) for x in claim.product_line_ids_return_claim]
                # update the created picking with stock moves
                pick_obj.write(cr, uid, [new_event_picking_id], {'move_lines': moves_lines}, context=context)
                # update the claim setting the link to created event picking
                self.write(cr, uid, [obj.id], {'event_picking_id_claim_event': new_event_picking_id}, context=context)
        
        for obj in self.browse(cr, uid, ids, context=context):
            # we start a new loop in order to have browse object reloaded, taking into account possible previous modification to claim object
            result = getattr(self, base_func + obj.type_claim_event)(cr, uid, obj, context=context)
            # event is done
            obj.write({'state': 'done'}, context=context)
            # log process message
            event_type_name = fields_tools.get_selection_name(cr, uid, object=self, field='type_claim_event', key=obj.type_claim_event, context=context)
            self.log(cr, uid, obj.id, _('%s Event %s has been processed.')%(event_type_name, obj.name))
            # we force the state of claim to in_progress
            obj.return_claim_id_claim_event.write({'state': 'in_progress'}, context=context)
        return True
    
    def _vals_get_claim(self, cr, uid, ids, fields, arg, context=None):
        '''
        multi fields function method
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        # results
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            result[obj.id] = {}
            # associated location
            dest_loc_id = self.get_location_for_event_type(cr, uid, context=context,
                                                           event_type=obj.type_claim_event,
                                                           claim_partner_id=obj.return_claim_id_claim_event.partner_id_return_claim.id,
                                                           claim_type=obj.return_claim_id_claim_event.type_return_claim,
                                                           claim_picking=obj.return_claim_id_claim_event.picking_id_return_claim)
            result[obj.id].update({'location_id_claim_event': int(dest_loc_id)})
            # hidden state (attrs in tree view)
            result[obj.id].update({'hidden_state': obj.state})
            
        return result
    
    _columns = {'return_claim_id_claim_event': fields.many2one('return.claim', string='Claim', required=True, ondelete='cascade', readonly=True),
                'creation_date_claim_event': fields.date(string='Creation Date', required=True, readonly=True), # default value
                'type_claim_event': fields.selection(CLAIM_EVENT_TYPE, string='Type', required=True, readonly=True),
                'replacement_picking_expected_claim_event': fields.boolean(string='Replacement expected for Return Claim?', help="An Incoming Shipment will be automatically created corresponding to returned products."),
                'description_claim_event': fields.char(size=1024, string='Comment'),
                'state': fields.selection(CLAIM_EVENT_STATE, string='State', readonly=True), # default value
                'from_picking_wizard_claim_event': fields.boolean(string='From Picking Wizard', readonly=True),
                'event_picking_id_claim_event': fields.many2one('stock.picking', string='Event Picking'),
                # auto fields from create function
                'name': fields.char(string='Reference', size=1024, readonly=True), # from create function
                'order_claim_event': fields.integer(string='Creation Order', readonly=True), # from create function
                # functions
                'location_id_claim_event': fields.function(_vals_get_claim, method=True, string='Associated Location', type='many2one', relation='stock.location', readonly=True, multi='get_vals_claim'),
                'hidden_state': fields.function(_vals_get_claim, method=True, string='Hidden State', type='selection', selection=CLAIM_EVENT_STATE, readonly=True, multi='get_vals_claim'),
                }
    
    _defaults = {'creation_date_claim_event': lambda *a: time.strftime('%Y-%m-%d'),
                 'state': 'draft',
                 'from_picking_wizard_claim_event': False,
                 }
    
claim_event()


class claim_product_line(osv.osv):
    '''
    product line for claim
    '''
    _name = 'claim.product.line'
    
    def _orm_checks(self, cr, uid, vals, context=None):
        '''
        common checks for create/write
        '''
        # objects
        prod_obj = self.pool.get('product.product')
        prodlot_obj = self.pool.get('stock.production.lot')
        partner_obj = self.pool.get('res.partner')
        claim_obj = self.pool.get('return.claim')
        
        if 'product_id_claim_product_line' in vals:
            if vals['product_id_claim_product_line']:
                product_id = vals['product_id_claim_product_line']
                data = prod_obj.read(cr, uid, [product_id], ['name', 'perishable', 'batch_management', 'type', 'subtype'], context=context)[0]
                # update the name
                vals.update({'name': data['name']})
                # batch management
                management = data['batch_management']
                # perishable
                perishable = data['perishable']
                # if management and we have a lot_id, we fill the expiry date
                if management and vals.get('lot_id_claim_product_line'):
                    data_prodlot = prodlot_obj.read(cr, uid, [vals.get('lot_id_claim_product_line')], ['life_date'], context=context)
                    expired_date = data_prodlot[0]['life_date']
                    vals.update({'expiry_date_claim_product_line': expired_date})
                elif perishable:
                    # nothing special here
                    pass
                else:
                    # not perishable nor management, exp and lot are False
                    vals.update(lot_id_claim_product_line=False, expiry_date_claim_product_line=False)
                # check asset
                asset_check = data['type'] == 'product' and data['subtype'] == 'asset'
                if not asset_check:
                    vals.update(asset_id_claim_product_line=False)
                # kit check
                kit_check = data['type'] == 'product' and data['subtype'] == 'kit'
                if not kit_check:
                    vals.update(composition_list_id_claim_product_line=False)
            else:
                # product is False, exp and lot are set to False
                vals.update(lot_id_claim_product_line=False, expiry_date_claim_product_line=False,
                            asset_id_claim_product_line=False, composition_list_id_claim_product_line=False)
        
        if vals.get('claim_id_claim_product_line', False):
            # claim_id
            claim_id = vals.get('claim_id_claim_product_line')
            # check the type and set the location accordingly
            data = claim_obj.read(cr, uid, claim_id, ['type_return_claim', 'partner_id_return_claim'], context=context)
            claim_type = data['type_return_claim']
            if claim_type == 'customer':
                partner_id = data['partner_id_return_claim'][0]
                data = partner_obj.read(cr, uid, partner_id, ['property_stock_customer'], context=context)
                location_id = data['property_stock_customer'][0]
                vals.update({'src_location_id_claim_product_line': location_id})
        
        return True
    
    def create(self, cr, uid, vals, context=None):
        '''
        set the name
        '''
        # common check
        self._orm_checks(cr, uid, vals, context=context)
        return super(claim_product_line, self).create(cr, uid, vals, context=context)
    
    def write(self, cr, uid, ids, vals, context=None):
        '''
        set the name
        '''
        # common check
        self._orm_checks(cr, uid, vals, context=context)
        return super(claim_product_line, self).write(cr, uid, ids, vals, context=context)
    
    def common_on_change(self, cr, uid, ids, location_id, product_id, prodlot_id, uom_id=False, result=None, context=None):
        '''
        commmon qty computation
        '''
        if context is None:
            context = {}
        if result is None:
            result = {}
        if not product_id or not location_id:
            result.setdefault('value', {}).update({'qty_claim_product_line': 0.0, 'hidden_stock_available_claim_product_line': 0.0})
            return result
        
        # objects
        loc_obj = self.pool.get('stock.location')
        prod_obj = self.pool.get('product.product')
        # corresponding product object
        product_obj = prod_obj.browse(cr, uid, product_id, context=context)
        # uom from product is taken by default if needed
        uom_id = uom_id or product_obj.uom_id.id
        # we do not want the children location
        stock_context = dict(context, compute_child=False)
        # we check for the available qty (in:done, out: assigned, done)
        res = loc_obj.compute_availability(cr, uid, [location_id], False, product_id, uom_id, context=context)
        if prodlot_id:
            # if a lot is specified, we take this specific qty info - the lot may not be available in this specific location
            qty = res[location_id].get(prodlot_id, False) and res[location_id][prodlot_id]['total'] or 0.0
        else:
            # otherwise we take total according to the location
            qty = res[location_id]['total']
        # update the result
        result.setdefault('value', {}).update({'qty_claim_product_line': qty,
                                               'uom_id_claim_product_line': uom_id,
                                               'hidden_stock_available_claim_product_line': qty,
                                               })
        return result
    
    def change_lot(self, cr, uid, ids, location_id, product_id, prodlot_id, uom_id=False, context=None):
        '''
        prod lot changes, update the expiry date
        '''
        prodlot_obj = self.pool.get('stock.production.lot')
        result = {'value':{}}
        # reset expiry date or fill it
        if prodlot_id:
            result['value'].update(expiry_date_claim_product_line=prodlot_obj.browse(cr, uid, prodlot_id, context=context).life_date)
        else:
            result['value'].update(expiry_date_claim_product_line=False)
        # compute qty
        result = self.common_on_change(cr, uid, ids, location_id, product_id, prodlot_id, uom_id, result=result, context=context)
        return result
    
    def change_expiry(self, cr, uid, ids, expiry_date, product_id, type_check, location_id, prodlot_id, uom_id, context=None):
        '''
        expiry date changes, find the corresponding internal prod lot
        '''
        prodlot_obj = self.pool.get('stock.production.lot')
        result = {'value':{}}
        
        if expiry_date and product_id:
            prod_ids = prodlot_obj.search(cr, uid, [('life_date', '=', expiry_date),
                                                    ('type', '=', 'internal'),
                                                    ('product_id', '=', product_id)], context=context)
            if not prod_ids:
                if type_check == 'in':
                    # the corresponding production lot will be created afterwards
                    result['warning'] = {'title': _('Info'),
                                         'message': _('The selected Expiry Date does not exist in the system. It will be created during validation process.')}
                    # clear prod lot
                    result['value'].update(lot_id_claim_product_line=False)
                else:
                    # display warning
                    result['warning'] = {'title': _('Error'),
                                         'message': _('The selected Expiry Date does not exist in the system.')}
                    # clear date
                    result['value'].update(expiry_date_claim_product_line=False, lot_id_claim_product_line=False)
            else:
                # return first prodlot
                prodlot_id = prod_ids[0]
                result['value'].update(lot_id_claim_product_line=prodlot_id)
        else:
            # clear expiry date, we clear production lot
            result['value'].update(lot_id_claim_product_line=False,
                                   expiry_date_claim_product_line=False,
                                   )
        # compute qty
        result = self.common_on_change(cr, uid, ids, location_id, product_id, prodlot_id, uom_id, result=result, context=context)
        return result
    
    def on_change_location_id(self, cr, uid, ids, location_id, product_id, prodlot_id, uom_id=False, context=None):
        """ 
        location changes
        """
        result = {}
        # compute qty
        result = self.common_on_change(cr, uid, ids, location_id, product_id, prodlot_id, uom_id, result=result, context=context)
        return result
    
    def on_change_product_id(self, cr, uid, ids, location_id, product_id, prodlot_id, uom_id=False, context=None):
        '''
        the product changes, set the hidden flag if necessary
        '''
        result = {}
        # product changes, prodlot is always cleared
        result.setdefault('value', {})['lot_id_claim_product_line'] = False
        result.setdefault('value', {})['expiry_date_claim_product_line'] = False
        # clear uom
        result.setdefault('value', {})['uom_id_claim_product_line'] = False
        # reset the hidden flags
        result.setdefault('value', {})['hidden_batch_management_mandatory_claim_product_line'] = False
        result.setdefault('value', {})['hidden_perishable_mandatory_claim_product_line'] = False
        result.setdefault('value', {})['hidden_asset_claim_product_line'] = False
        result.setdefault('value', {})['hidden_kit_claim_product_line'] = False
        
        if product_id:
            product_obj = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
            # set the default uom
            uom_id = product_obj.uom_id.id
            result.setdefault('value', {})['uom_id_claim_product_line'] = uom_id
            result.setdefault('value', {})['hidden_batch_management_mandatory_claim_product_line'] = product_obj.batch_management
            result.setdefault('value', {})['hidden_perishable_mandatory_claim_product_line'] = product_obj.perishable
            asset_check = product_obj.type == 'product' and product_obj.subtype == 'asset'
            result.setdefault('value', {})['hidden_asset_claim_product_line'] = asset_check
            kit_check = product_obj.type == 'product' and product_obj.subtype == 'kit'
            result.setdefault('value', {})['hidden_kit_claim_product_line'] = kit_check
            
        # compute qty
        result = self.common_on_change(cr, uid, ids, location_id, product_id, prodlot_id, uom_id, result=result, context=context)
        return result
    
    def _vals_get_claim(self, cr, uid, ids, fields, arg, context=None):
        '''
        multi fields function method
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        
        # objects
        loc_obj = self.pool.get('stock.location')
        # results
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            result[obj.id] = {}
            # claim state
            result[obj.id].update({'claim_state_claim_product_line': obj.claim_id_claim_product_line.state})
            # claim_type_claim_product_line
            result[obj.id].update({'claim_type_claim_product_line': obj.claim_id_claim_product_line.type_return_claim})
            # batch management
            result[obj.id].update({'hidden_batch_management_mandatory_claim_product_line': obj.product_id_claim_product_line.batch_management})
            # perishable
            result[obj.id].update({'hidden_perishable_mandatory_claim_product_line': obj.product_id_claim_product_line.perishable})
            # hidden_asset_claim_product_line
            asset_check = obj.product_id_claim_product_line.type == 'product' and obj.product_id_claim_product_line.subtype == 'asset'
            result[obj.id].update({'hidden_asset_claim_product_line': asset_check})
            # hidden_kit_claim_product_line
            kit_check = obj.product_id_claim_product_line.type == 'product' and obj.product_id_claim_product_line.subtype == 'kit'
            result[obj.id].update({'hidden_kit_claim_product_line': kit_check})
            # product availability
            data = loc_obj.compute_availability(cr, uid, ids=obj.src_location_id_claim_product_line.id, consider_child_locations=False, product_id=obj.product_id_claim_product_line.id, uom_id=obj.uom_id_claim_product_line.id, context=context)
            # if we get a production lot, we take the available quantity corresponding to this lot
            location_id = obj.src_location_id_claim_product_line.id
            prodlot_id = obj.lot_id_claim_product_line.id
            # if the product has a production lot and the production lot exist in the specified location, we take corresponding stock
            available_qty = 0.0
            if prodlot_id and prodlot_id in data[location_id]:
                available_qty = data[location_id][prodlot_id]['total']
            else:
                # otherwise we take the total quantity for the selected location - no lot for this product
                available_qty = data[location_id]['total']
            result[obj.id].update({'hidden_stock_available_claim_product_line': available_qty})
            
        return result

    def onchange_uom_qty(self, cr, uid, ids, uom_id, qty):
        '''
        Check round of qty according to UoM
        '''
        res = {}

        if qty:
            res = self.pool.get('product.uom')._change_round_up_qty(cr, uid, uom_id, qty, 'qty_claim_product_line', result=res)

        return res
        
    _columns = {'integrity_status_claim_product_line': fields.selection(string=' ', selection=INTEGRITY_STATUS_SELECTION, readonly=True),
                'name': fields.char(string='Name', size=1024), # auto data from create/write
                'qty_claim_product_line': fields.float(string='Qty', digits_compute=dp.get_precision('Product UoM'), required=True),
                # many2one
                'claim_id_claim_product_line': fields.many2one('return.claim', string='Claim', required=True, ondelete='cascade'),
                'product_id_claim_product_line': fields.many2one('product.product', string='Product', required=True),
                'uom_id_claim_product_line': fields.many2one('product.uom', string='UoM', required=True),
                'lot_id_claim_product_line': fields.many2one('stock.production.lot', string='Batch N.'),
                'expiry_date_claim_product_line': fields.date(string='Exp'),
                'asset_id_claim_product_line' : fields.many2one('product.asset', string='Asset'),
                'composition_list_id_claim_product_line': fields.many2one('composition.kit', string='Kit'),
                'src_location_id_claim_product_line': fields.many2one('stock.location', string='Src Location'),
                'stock_move_id_claim_product_line': fields.many2one('stock.move', string='Corresponding IN stock move'), # value from wizard process
                'type_check': fields.char(string='Type Check', size=1024,), # default value
                # functions
                'claim_type_claim_product_line': fields.function(_vals_get_claim, method=True, string='Claim Type', type='selection', selection=CLAIM_TYPE, store=False, readonly=True, multi='get_vals_claim'),
                'hidden_stock_available_claim_product_line': fields.function(_vals_get_claim, method=True, string='Available Stock', type='float', digits_compute=dp.get_precision('Product UoM'), store=False, readonly=True, multi='get_vals_claim'),
                'claim_state_claim_product_line': fields.function(_vals_get_claim, method=True, string='Claim State', type='selection', selection=CLAIM_STATE, store=False, readonly=True, multi='get_vals_claim'),
                'hidden_perishable_mandatory_claim_product_line': fields.function(_vals_get_claim, method=True, type='boolean', string='Exp', store=False, readonly=True, multi='get_vals_claim'),
                'hidden_batch_management_mandatory_claim_product_line': fields.function(_vals_get_claim, method=True, type='boolean', string='B.Num', store=False, readonly=True, multi='get_vals_claim'),
                'hidden_asset_claim_product_line': fields.function(_vals_get_claim, method=True, type='boolean', string='Asset Check', store=False, readonly=True, multi='get_vals_claim'),
                'hidden_kit_claim_product_line': fields.function(_vals_get_claim, method=True, type='boolean', string='Kit Check', store=False, readonly=True, multi='get_vals_claim'),
                }
    
    def _get_default_location(self, cr, uid, context=None):
        '''
        get default location:
        - supplier: default_src from claim
        - customer: default customer location from claim partner
        '''
        # objects
        partner_obj = self.pool.get('res.partner')
        
        claim_type = context['claim_type']
        default_loc_id = context['default_src']
        claim_partner_id = context['claim_partner']
        
        if claim_type == 'supplier':
            location_id = default_loc_id
        elif claim_type == 'customer':
            data = partner_obj.read(cr, uid, claim_partner_id, ['property_stock_customer'], context=context)
            location_id = data['property_stock_customer'][0]
        
        return location_id
    
    _defaults = {'type_check': 'out',
                 'integrity_status_claim_product_line': 'empty',
                 'claim_type_claim_product_line': lambda obj, cr, uid, c: c.get('claim_type', False),
                 'src_location_id_claim_product_line': _get_default_location,
                 }
    
claim_product_line()


class stock_picking(osv.osv):
    '''
    add a column
    '''
    _inherit = 'stock.picking'
    
    def _vals_get_claim(self, cr, uid, ids, fields, arg, context=None):
        '''
        multi fields function method
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        move_obj = self.pool.get('stock.move')
        data_tools = self.pool.get('data.tools')
        # load common data
        data_tools.load_common_data(cr, uid, ids, context=context)
        # results
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            # set result dic
            result[obj.id] = {}
            # chained_from_in_stock_picking
            test_chained = True
            # type of picking must be 'internal'
            if obj.type != 'internal':
                test_chained = False
            # in picking
            in_picking_id = False
            for move in obj.move_lines:
                # source location must be input for all moves
                input_location_id = context['common']['input_id']
                if move.location_id.id != input_location_id:
                    test_chained = False
                # all moves must be created from another stock move from IN picking (chained location)
                src_ids = move_obj.search(cr, uid, [('move_dest_id', '=', move.id)], context=context)
                # no stock move source of current stock move
                if len(src_ids) != 1:
                    test_chained = False
                else:
                    move_browse = move_obj.browse(cr, uid, src_ids[0], context=context)
                    # all should belong to the same incoming shipment
                    if in_picking_id and in_picking_id != move_browse.picking_id.id:
                        test_chained = False
                    in_picking_id = move_browse.picking_id.id
                    if move_browse.picking_id.type != 'in':
                        # source stock move is not part of incoming shipment
                        test_chained = False
            result[obj.id].update({'chained_from_in_stock_picking': test_chained})
            # corresponding_in_picking_stock_picking
            if test_chained:
                result[obj.id].update({'corresponding_in_picking_stock_picking': in_picking_id})
            else:
                result[obj.id].update({'corresponding_in_picking_stock_picking': False})
            
        return result
    
    def _picking_done_cond(self, cr, uid, ids, context=None, *args, **kwargs):
        '''
        Please copy this to your module's method also.
        This hook belongs to the do_partial method from stock_override>stock.py>stock_picking
        
        - allow to conditionally execute the picking processing to done
        - no supposed to modify partial_datas
        '''
        partial_datas = kwargs['partial_datas']
        assert partial_datas is not None, 'missing partial_datas'
        # if a claim is needed:
        # if return claim: we do not close the processed picking, it is now an out picking which need to be processed
        if 'register_a_claim_partial_picking' in partial_datas and partial_datas['register_a_claim_partial_picking']:
            if partial_datas['claim_type_partial_picking'] == 'return':
                return False
        
        return True
    
    def _custom_code(self, cr, uid, ids, context=None, *args, **kwargs):
        '''
        Please copy this to your module's method also.
        This hook belongs to the do_partial method from stock_override>stock.py>stock_picking
        
        - allow to execute specific custom code before processing picking to done
        - no supposed to modify partial_datas
        '''
        # objects
        claim_obj = self.pool.get('return.claim')
        event_obj = self.pool.get('claim.event')
        move_obj = self.pool.get('stock.move')
        fields_tools = self.pool.get('fields.tools')
        # get the partial_datas from the wizard
        partial_datas = kwargs['partial_datas']
        # get the processed picking (pick if no backorder, new_picking if backorder, both under concerned_picking name) - this is an internal chained picking
        concerned_picking = kwargs['concerned_picking']
        # test if we need a claim
        if 'register_a_claim_partial_picking' in partial_datas and partial_datas['register_a_claim_partial_picking']:
            # this is theoretically only possible for internal picking, which are linked to an incoming shipment
            if concerned_picking.type != 'internal':
                raise osv.except_osv(_('Warning !'), _('Claim registration during picking process is only available for internal picking.'))
            # we create a claim
            # corresponding move_id from incoming shipment
            in_move_id = False
            if not len(concerned_picking.move_lines):
                # no lines, we cannot register a claim because the link to original picking is missing
                raise osv.except_osv(_('Warning !'), _('Processed an internal picking without moves, cannot find original Incoming shipment for claim registration.'))
            for move in concerned_picking.move_lines:
                src_move_ids = move_obj.search(cr, uid, [('move_dest_id', '=', move.id)], context=context)
                if not src_move_ids:
                    # we try to find the incoming shipment with by backorder link
                    back_ids = self.search(cr, uid, [('backorder_id', '=', concerned_picking.id)], context=context)
                    if len(back_ids) != 1:
                        # cannot find corresponding stock move in incoming shipment
                        raise osv.except_osv(_('Warning !'), _('Corresponding Incoming Shipment cannot be found. Registration of claim cannot be processed. (no back order)'))
                    else:
                        # we try with the backorder
                        for b_move in self.browse(cr, uid, back_ids[0], context=context).move_lines:
                            b_src_move_ids = move_obj.search(cr, uid, [('move_dest_id', '=', b_move.id)], context=context)
                            if not b_src_move_ids:
                                raise osv.except_osv(_('Warning !'), _('Corresponding Incoming Shipment cannot be found. Registration of claim cannot be processed. (no IN for back order moves)'))
                            else:
                                in_move_id = b_src_move_ids[0]
                else:
                    in_move_id = src_move_ids[0]
            # get corresponding stock move browse
            in_move = move_obj.browse(cr, uid, in_move_id, context=context)
            # check that corresponding picking is incoming shipment
            if in_move.picking_id.type != 'in':
                raise osv.except_osv(_('Warning !'), _('Corresponding picking object is not an Incoming Shipment. Registration of claim cannot be processed.'))
            # po reference
            po_reference = in_move.picking_id.origin
            # po_id
            po_id = in_move.picking_id.purchase_id.id
            # category
            category = in_move.picking_id.order_category
            # partner id - from the wizard, as partner is not mandatory for Incoming Shipment
            partner_id = partial_datas['partner_id_partial_picking']
            # picking id
            picking_id = in_move.picking_id.id
            # we get the stock move of incoming shipment, we get the origin of picking
            claim_values = {'po_so_return_claim': po_reference,
                            'po_id_return_claim': po_id,
                            'type_return_claim': 'supplier',
                            'category_return_claim': category,
                            'description_return_claim': partial_datas['description_partial_picking'],
                            'follow_up_return_claim': False,
                            'partner_id_return_claim': partner_id,
                            'picking_id_return_claim': picking_id,
                            'product_line_ids_return_claim': [(0, 0, {'qty_claim_product_line': x.product_qty,
                                                                      'product_id_claim_product_line': x.product_id.id,
                                                                      'uom_id_claim_product_line': x.product_uom.id,
                                                                      'lot_id_claim_product_line': x.prodlot_id.id,
                                                                      'expiry_date_claim_product_line': x.expired_date,
                                                                      'asset_id_claim_product_line': x.asset_id.id,
                                                                      'composition_list_id_claim_product_line': x.composition_list_id.id,
                                                                      'src_location_id_claim_product_line': x.location_id.id,
                                                                      'stock_move_id_claim_product_line': x.id}) for x in concerned_picking.move_lines]
                            }
            
            new_claim_id = claim_obj.create(cr, uid, claim_values, context=context)
            # log creation message
            claim_name = claim_obj.read(cr, uid, new_claim_id, ['name'], context=context)['name']
            claim_obj.log(cr, uid, new_claim_id, _('The new Claim %s to supplier has been registered during internal chained Picking process.')%claim_name)
            # depending on the claim type, we create corresponding event
            selected_event_type = partial_datas['claim_type_partial_picking']
            event_values = {'return_claim_id_claim_event': new_claim_id,
                            'type_claim_event': selected_event_type,
                            'replacement_picking_expected_claim_event': partial_datas['replacement_picking_expected_partial_picking'],
                            'description_claim_event': partial_datas['description_partial_picking'],
                            'from_picking_wizard_claim_event': True,
                            'event_picking_id_claim_event': concerned_picking.id,
                            }
            new_event_id = event_obj.create(cr, uid, event_values, context=context)
            event_type_name = fields_tools.get_selection_name(cr, uid, object='claim.event', field='type_claim_event', key=selected_event_type, context=context)
            event_obj.log(cr, uid, new_event_id, _('The new %s Event %s has been created.')%(event_type_name, claim_name))
            # we process the event
            event_obj._do_process_event(cr, uid, [new_event_id], context=context)
        
#        raise osv.except_osv(_('Warning !'), _('End'))
        return True
    
    _columns = {'chained_from_in_stock_picking': fields.function(_vals_get_claim, method=True, string='Chained Internal Picking from IN', type='boolean', readonly=True, multi='get_vals_claim'),
                'corresponding_in_picking_stock_picking': fields.function(_vals_get_claim, method=True, string='Corresponding IN for chained internal', type='many2one', relation='stock.picking', readonly=True, multi='get_vals_claim')}
    
stock_picking()


class product_product(osv.osv):
    _name = 'product.product'
    _inherit = 'product.product'


    def _vals_get_claim(self, cr, uid, ids, fields, arg, context=None):
        '''
        return false for all
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        
        res = {}
        for id in ids:
            res[id] = {}
            for f in fields:
                res[id].update({f:False})
                  
        return res
    
    def _search_picking_claim(self, cr, uid, obj, name, args, context=None):
        '''
        Filter the search according to the args parameter
        '''
        # Some verifications
        if context is None:
            context = {}
        # objects
        pick_obj = self.pool.get('stock.picking')
            
        # ids of products
        ids = []
            
        for arg in args:
            if arg[0] == 'picking_ids':
                if arg[1] == '=' and arg[2]:
                    picking = pick_obj.browse(cr, uid, int(arg[2]), context=context)
                    for move in picking.move_lines:
                        ids.append(move.product_id.id)
                else:
                    raise osv.except_osv(_('Error !'), _('Operator is not supported.'))
            else:
                return []
            
        return [('id', 'in', ids)]

    _columns = {
        'picking_ids': fields.function(_vals_get_claim, fnct_search=_search_picking_claim, 
                                    type='boolean', method=True, string='Picking', multi='get_vals_claim'),
    }

product_product()


class res_partner(osv.osv):
    '''
    add a link to claims
    '''
    _inherit = 'res.partner'
    
    _columns = {'claim_ids_res_partner': fields.one2many('return.claim', 'partner_id_return_claim', string='Claims')}
    
    def copy_data(self, cr, uid, id, default=None, context=None):
        '''
        erase claim_ids
        '''
        if default is None:
            default = {}
        if context is None:
            context = {}
        default.update({'claim_ids_res_partner': []})
        res = super(res_partner, self).copy_data(cr, uid, id, default=default, context=context)
        return res
    
res_partner()