~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
# -*- 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
from osv import fields

from tools.translate import _

import logging
from os import path
import math
import re
import tools

class stock_location(osv.osv):
    '''
    Change the order and parent_order field.
    '''
    _name = "stock.location"
    _inherit = 'stock.location'
    _parent_order = 'location_id, posz'
    _order = 'location_id, posz'


    def _get_input_output(self, cr, uid, ids, field_name, args, context=None):
        '''
        Return True if the location is the input/output location of a warehouse or a children of it
        '''
        res = {}
        wh_ids = self.pool.get('stock.warehouse').search(cr, uid, [])
        output_loc = []
        input_loc = []
        for wh in self.pool.get('stock.warehouse').browse(cr, uid, wh_ids):
            output_loc.extend(self.search(cr, uid, [('location_id', 'child_of', wh.lot_output_id.id)]))
            input_loc.extend(self.search(cr, uid, [('location_id', 'child_of', wh.lot_input_id.id)]))
            
        for id in ids:
            if field_name == 'output_ok':
                res[id] = id in output_loc
            elif field_name == 'input_ok':
                res[id] = id in input_loc
            else:
                res[id] = False
            
        return res
    
    def _src_input_output(self, cr, uid, obj, name, args, context=None):
        '''
        Return all input/output locations
        '''
        res = []
        wh_ids = self.pool.get('stock.warehouse').search(cr, uid, [])
        output_loc = []
        input_loc = []
        for wh in self.pool.get('stock.warehouse').browse(cr, uid, wh_ids):
            output_loc.extend(self.search(cr, uid, [('location_id', 'child_of', wh.lot_output_id.id)]))
            input_loc.extend(self.search(cr, uid, [('location_id', 'child of', wh.lot_input_id.id)]))
            
        operator = 'in'
        if (args[0][1] == '=' and args[0][2] == False) or (args[0][1] and '!=' and args[0][2] == True):
            operator = 'not in'
            
        if args[0][0] == 'output_ok':
            return [('id', operator, output_loc)]
        elif args[0][0] == 'input_ok':
            return [('id', operator, input_loc)]
        
        return res
    
    def _get_virtual(self, cr, uid, ids, field_name, args, context=None):
        '''
        Return True if the location is under the Virtual locations view
        '''
        res = {}
        try:
            virtual_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_locations_virtual')[1]
            virtual_ids = self.search(cr, uid, [('location_id', 'child_of', virtual_id)], context=context)
        except:
            virtual_ids = []
        for id in ids:
            res[id] = False
            if id in virtual_ids:
                res[id] = True
                
        return res
    
    def _src_virtual(self, cr, uid, obj, name, args, context=None):
        '''
        Returns all virtual locations
        '''
        res = []
        try:
            virtual_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_locations_virtual')[1]
            virtual_ids = self.search(cr, uid, [('location_id', 'child_of', virtual_id)], context=context)
        except:
            return res
        
        operator = 'in'
        if (args[0][1] == '=' and args[0][2] == False) or (args[0][1] and '!=' and args[0][2] == True):
            operator = 'not in'
            
        return [('id', operator, virtual_ids)]
    
    def _get_dummy(self, cr, uid, ids, field_name, args, context=None):
        '''
        Set all object to true
        '''
        res = {}
        for id in ids:
            res[id] = True
        return res
    
    def _src_pick_src(self, cr, uid, obj, name, args, context=None):
        '''
        Returns the available locations for source location of a picking ticket according to the product.
        '''
        res = [('usage', '=', 'internal')]
        for arg in args:
            if arg[0] == 'picking_ticket_src' and arg[1] == '=':
                if arg[2] == False:
                    break
                if type(arg[2]) != type(1):
                    raise osv.except_osv(_('Error'), _('Bad operand'))
                product = self.pool.get('product.product').browse(cr, uid, arg[2])
                if product.type in ('service_recep', 'consu'):
                    res = [('cross_docking_location_ok', '=', True)]
                else:
                    res = [('usage', '=', 'internal'), ('quarantine_location', '=', False), ('output_ok', '=', False)]
                    
        return res
    
    def _dest_inc_ship(self, cr, uid, obj, name, args, context=None):
        '''
        Returns the available locations for destination location of an incoming shipment according to the product.
        '''
        res = ['|', '|', '|', '|', ('usage', '=', 'internal'), ('service_location', '=', True), ('non_stockable_ok', '=', True), ('cross_docking_location_ok', '=', True), ('virtual_ok', '=', True)]
        for arg in args:
            if arg[0] == 'incoming_dest' and arg[1] == '=':
                if arg[2] == False:
                    break
                if type(arg[2]) != type(1):
                    raise osv.except_osv(_('Error'), _('Bad operand'))
                product = self.pool.get('product.product').browse(cr, uid, arg[2])
                if product.type in ('service', 'service_recep'):
                    res = ['|', ('cross_docking_location_ok', '=', True), ('service_location', '=', True)]
                elif product.type == 'consu':
                    res = ['|', '|', ('cross_docking_location_ok', '=', True), ('non_stockable_ok', '=', True), ('virtual_ok', '=', True)]
                else:
                    res = [('non_stockable_ok', '=', False), '|', ('usage', '=', 'internal'), ('virtual_ok', '=', True)]
                    
        return res
    
    def _src_out(self, cr, uid, obj, name, args, context=None):
        '''
        Returns the available locations for source location of an outgoing delivery according to the product
        '''
        res = ['|', ('cross_docking_location_ok', '=', True), ('usage', '=', 'internal'), ('quarantine_location', '=', False), ('output_ok', '=', False), ('input_ok', '=', False)]
        for arg in args:
            # TODO: Refactorize this
            if arg[0] == 'outgoing_src' and arg[1] == '=':
                if arg[2] == False:
                    break
                if type(arg[2]) != type(1):
                    raise osv.except_osv(_('Error'), _('Bad operand'))
                product = self.pool.get('product.product').browse(cr, uid, arg[2])
                if product.type in ('service_recep', 'consu'):
                    # Cross-docking locations
                    res = [('cross_docking_location_ok', '=' ,True)]
                else:
                    # All internal locations except Quarantine (both), Output (& children) and Input locations
                    res = [('usage', '=', 'internal'), ('quarantine_location', '=', False), ('output_ok', '=', False), ('input_ok', '=', False)]
                    
        return res
    
    def _src_int(self, cr, uid, obj, name, args, context=None):
        '''
        Return the available locations for source location of an internal picking according to the product
        '''
        res = [('service_location', '=', False), '|', '|', '|', ('cross_docking_location_ok', '=', True), ('quarantine_location', '=', True), ('usage', '=', 'internal'), ('virtual_ok', '=', True)]
        for arg in args:
            if arg[0] == 'internal_src' and arg[1] == '=':
                if arg[2] == False:
                    break
                if type(arg[2]) != type(1):
                    raise osv.except_osv(_('Error'), _('Bad operand'))
                product = self.pool.get('product.product').browse(cr, uid, arg[2])
                if product.type == 'consu':
                    # Cross docking and quarantine locations
                    res = [('service_location', '=', False), '|', ('cross_docking_location_ok', '=', True), ('quarantine_location', '=', True)]
                elif product.type == 'service_recep':
                    # Cross docking locations
                    res = [('cross_docking_location_ok', '=', True)]
                else:
                    # All internal and virtual locations
                    res = [('non_stockable_ok', '=', False), ('service_location', '=', False), '|', ('usage', '=', 'internal'), ('virtual_ok', '=', True)]
                    
        return res
    
    def _dest_int(self, cr, uid, obj, name, args, context=None):
        '''
        Returns the available locations for destination location of an internal picking according to the product
        '''
        # Inventory, destruction, quarantine and all internal and virtual locations
        res = [('service_location', '=', False), '|', '|', '|', ('usage', 'in', ('internal', 'inventory')), ('destruction_location', '=', True), ('quarantine_location', '=', True), ('virtual_ok', '=', True)]
        for arg in args:
            if arg[0] == 'internal_dest' and arg[1] == '=':
                if arg[2] == False:
                    break
                if type(arg[2]) != type(1):
                    raise osv.except_osv(_('Error'), _('Bad operand'))
                product = self.pool.get('product.product').browse(cr, uid, arg[2])
                if product.type == 'consu':
                    # Inventory, destruction and quarantine location
                    res = [('service_location', '=', False), '|', '|', ('usage', '=', 'inventory'), ('destruction_location', '=', True), ('quarantine_location', '=', True)]
                elif product.type == 'service_recep':
                    # Service location
                    res = [('service_location', '=', True)]
                else:
                    # All internal and virtual locations
                    res = [('non_stockable_ok', '=', False), ('service_location', '=', False), '|', ('usage', '=', 'internal'), ('virtual_ok', '=', True)]
                    
        return res
    
    def _get_warehouse_input(self, cr, uid, ids, context=None):
        res = []
        for wh in self.browse(cr, uid, ids, context=context):
            res.append(wh.lot_input_id.id)
            
        input_ids = self.pool.get('stock.location').search(cr, uid, [('input_ok', '=', True), ('active', 'in', ('t', 'f'))])
        res.extend(input_ids)
                    
        return res
    
    def _get_warehouse_output(self, cr, uid, ids, context=None):
        res = []
        for wh in self.browse(cr, uid, ids, context=context):
            res.append(wh.lot_output_id.id)
            
        output_ids = self.pool.get('stock.location').search(cr, uid, [('output_ok', '=', True), ('active', 'in', ('t', 'f'))])
        res.extend(output_ids)
                    
        return res
    
    _columns = {
        'central_location_ok': fields.boolean(string='If check, all products in this location are unallocated.'),
        'non_stockable_ok': fields.boolean(string='Non-stockable', help="If checked, the location will be used to store non-stockable products"),
        'output_ok': fields.function(_get_input_output, method=True, string='Output Location', type='boolean',
                                     store={'stock.location': (lambda self, cr, uid, ids, c={}: ids, ['location_id'], 20),
                                            'stock.warehouse': (_get_warehouse_output, ['lot_input_id'], 10)},
                                     help='If checked, the location is the output location of a warehouse or a children.'),
        'input_ok': fields.function(_get_input_output,  method=True, string='Input Location', type='boolean',
                                    store={'stock.location': (lambda self, cr, uid, ids, c={}: ids, ['location_id'], 20),
                                           'stock.warehouse': (_get_warehouse_input, ['lot_input_id'], 10)},
                                     help='If checked, the location is the input location of a warehouse or a children.'),
        'virtual_ok': fields.function(_get_virtual,  method=True, string='Virtual Location', type='boolean',
                                      store={'stock.location': (lambda self, cr, uid, ids, c={}: ids, ['location_id'], 20)},
                                     help='If checked, the location is a virtual location.'),
        'picking_ticket_src': fields.function(_get_dummy, fnct_search=_src_pick_src, method=True, string='Picking Ticket Src. Loc.', type='boolean',
                                              help='Returns the available locations for source location of a picking ticket according to the product.'),
        'incoming_dest': fields.function(_get_dummy, fnct_search=_dest_inc_ship, method=True, string='Incoming shipment Dest. Loc.', type='boolean',
                                         help="Returns the available locations for destination location of an incoming shipment according to the product."),
        'outgoing_src': fields.function(_get_dummy, fnct_search=_src_out, method=True, string='Outgoing delivery Src. Loc.', type='boolean',
                                        help='Returns the available locations for source location of an outgoing delivery according to the product.'),
        'internal_src': fields.function(_get_dummy, fnct_search=_src_int, method=True, string='Internal Picking Src. Loc.', type='boolean',
                                        help='Returns the available locations form source loctaion of an internal picking according to the product.'),
        'internal_dest': fields.function(_get_dummy, fnct_search=_dest_int, method=True, string='Internal Picking Dest. Loc.', type='boolean',
                                         help='Returns the available locations for destination location of an internal picking according to the product.'),
    }
    
stock_location()


class stock_location_configuration_wizard(osv.osv_memory):
    _name = 'stock.location.configuration.wizard'
    _inherit = 'res.config'
    
    _columns = {
        'location_name': fields.char(size=64, string='Location name', required=True),
        'location_usage': fields.selection([('stock', 'Stock'), ('consumption_unit', 'Consumption Unit'), ('eprep', 'EPREP')],
                                          string='Location usage'),
        'location_type': fields.selection([('internal', 'Internal'), ('customer', 'External')], string='Location type'),
        'location_id': fields.many2one('stock.location', string='Inactive location to re-activate'),
        'reactivate': fields.boolean(string='Reactivate location ?'),
    }
    
    _defaults = {
        'reactivate': lambda *a: False,
    }
    
    def action_add(self, cr, uid, ids, context=None):
        self.confirm_creation(cr, uid, ids[0], context=context)
        return {
            'view_type': 'form',
            "view_mode": 'form',
            'res_model': 'stock.location.configuration.wizard',
            'view_id':self.pool.get('ir.ui.view')\
                .search(cr,uid,[('name','=','Configurable Locations Configuration')]),
            'type': 'ir.actions.act_window',
            'target':'new',
            }
        
    def action_stop(self, cr, uid, ids, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        self.confirm_creation(cr, uid, ids, context=context)
        return self.action_next(cr, uid, ids, context=context)
    
    def execute(self, cr, uid, ids, context=None):
        pass
    
    def name_on_change(self, cr, uid, ids, location_name, usage, type, context=None):
        '''
        Check if a location with the same parameter exists on
        the instance warehouse
        '''
        res = {}
        warning = {}
        location_obj = self.pool.get('stock.location')
        
        if location_name:
            inactive_location_ids = location_obj.search(cr, uid, [('name', '=', location_name),
                                                                  ('active', '=', False), 
                                                                  ('usage', '=', usage or 'internal'),
                                                                  ('location_category', '=', type)], context=context)
            active_location_ids = location_obj.search(cr, uid, [('name', '=', location_name), 
                                                                ('usage', '=', usage or 'internal'),
                                                                ('location_category', '=', type)], context=context)
            
            if inactive_location_ids:
                warning.update({'title': _('Warning !'),
                                'message': _('An existing but inactive location already exists with the same parameters ! '\
                                           'Please, change the name of the new location, or re-activate the existing location '\
                                           'by clicking on \'Re-activate\' button.')})
                res.update({'reactivate': True, 'location_id': inactive_location_ids[0]})
            elif active_location_ids:
                warning.update({'title': _('Error !'),
                                'message': _('A location with the same name and the parameters already exists and is active. '\
                                           'You cannot have two locations with the same name and parameters. ' \
                                           'Please change the name of the new location before create it.')})
                res.update({'reactivate': False, 'loc_exists': True})
            else:
                res.update({'reactivate': False, 'location_id': False})
        
        return {'value': res,
                'warning': warning}
        
    def confirm_creation2(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        context.update({'reactivate_loc': True})
        return self.confirm_creation(cr, uid, ids, context=context)

    def confirm_creation(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        data_obj = self.pool.get('ir.model.data')
        location_obj = self.pool.get('stock.location')
        parent_location_id = False
        location_category = False
        location_usage = False
        location_name = False
        chained_location_type = 'none'
        chained_auto_packing = 'manual'
        chained_picking_type = 'internal'
        chained_location_id = False
        location = False
        
        for wizard in self.browse(cr, uid, ids, context=context):
            # Check if errors on location name
            errors = self.name_on_change(cr, uid, ids, wizard.location_name, wizard.location_type, wizard.location_usage, context=context)
            warning = errors.get('warning', {})
            values = errors.get('value', {})
            if warning and (values.get('loc_exists', False) or (values.get('reactivate', True) and not context.get('reactivate_loc', False))):
                raise osv.except_osv(errors.get('warning', {}).get('title', ''), errors.get('warning', {}).get('message', ''))
            # Returns an error if no given location name
            if not wizard.location_name:
#                # Raise error if location with the same name and the same parameters already exists
#                if location_obj.search(cr, uid, [('name', '=', wizard.location_name), 
#                                                 ('usage', '=', wizard.location_type or 'internal'),
#                                                 ('location_category', '=', wizard.location_usage)], context=context):
                raise osv.except_osv(_('Error'), _('You should give a name for the new location !'))
            location = wizard.location_id
            location_name = wizard.location_name
            # Check if all parent locations are activated in the system
            if wizard.location_usage in ('stock', 'eprep') or (wizard.location_type == 'internal' and wizard.location_usage == 'consumption_unit'):
                # Check if 'Configurable locations' location is active − If not, activate it !
                location_id = data_obj.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_internal_client_view')
                if not location_id:
                    raise osv.except_osv(_('Error'), _('Location \'Configurable locations\' not found in the instance or is not activated !'))
                
                if not location_obj.browse(cr, uid, location_id, context=context).active:
                    location_obj.write(cr, uid, [location_id[1]], {'active': True}, context=context)
                
                if wizard.location_usage in ('stock', 'eprep'):
#                    if wizard.location_usage == 'stock':
                    location_category = 'stock'
                    location_usage = 'internal'
                        # Check if 'Intermediate Stocks' is active − If note activate it !
                    parent_location_id = data_obj.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_intermediate_client_view')
#                    else:
#                        location_stock_id = data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_stock')
#                        if not location_stock_id:
#                            raise osv.except_osv(_('Error'), _('Location \'Stock\' not found in the instance or is not activated !'))
#                        location_category = 'eprep'
#                        location_usage = 'internal'
#                        chained_location_type = 'fixed'
#                        chained_auto_packing = 'manual'
#                        chained_picking_type = 'internal'
#                        chained_location_id = location_stock_id[1]
#                        parent_location_id = location_id
                
                    if not parent_location_id:
                        raise osv.except_osv(_('Error'), _('Location \'Intermediate Stocks\' not found in the instance or is not activated !'))
                    
                    parent_location_id = parent_location_id[1]
                    
                    if not location_obj.browse(cr, uid, parent_location_id, context=context).active:
                        location_obj.write(cr, uid, [parent_location_id], {'active': True}, context=context)
                elif wizard.location_usage == 'consumption_unit':
                    location_category = 'consumption_unit'
                    location_usage = 'internal'
                    # Check if 'Internal Consumption Units' is active − If note activate it !
                    parent_location_id = data_obj.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_consumption_units_view')
                    
                    parent_location_id = parent_location_id[1]
                
                    if not parent_location_id:
                        raise osv.except_osv(_('Error'), _('Location \'Internal Consumption Units\' not found in the instance or is not activated !'))
                    
                    if not location_obj.browse(cr, uid, parent_location_id, context=context).active:
                        location_obj.write(cr, uid, [parent_location_id], {'active': True}, context=context)
                else:
                    raise osv.except_osv(_('Error'), _('The type of the new location is not correct ! Please check the parameters and retry.'))
            elif wizard.location_type == 'customer' and wizard.location_usage == 'consumption_unit':
                location_category = 'consumption_unit'
                location_usage = 'customer'
                chained_picking_type = 'out'
                # Check if 'MSF Customer' location is active − If not, activate it !
                parent_location_id = data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_internal_customers')
                if not parent_location_id:
                    raise osv.except_osv(_('Error'), _('Location \'MSF Customer\' not found in the instance or is not activated !'))
                                    
                parent_location_id = parent_location_id[1]
                    
                if not location_obj.browse(cr, uid, parent_location_id, context=context).active:
                    location_obj.write(cr, uid, [parent_location_id], {'active': True}, context=context)
            else:
                raise osv.except_osv(_('Error'), _('The type of the new location is not correct ! Please check the parameters and retry.'))
        
        if not parent_location_id or not location_category or not location_usage:
            raise osv.except_osv(_('Error'), _('Parent stock location not found for the new location !'))
        
        if not location:
            # Create the new location
            location_obj.create(cr, uid, {'name': location_name,
                                          'location_id': parent_location_id,
                                          'location_category': location_category,
                                          'usage': location_usage,
                                          'chained_location_type': chained_location_type,
                                          'chained_auto_packing': chained_auto_packing,
                                          'chained_picking_type': chained_picking_type,
                                          'chained_location_id': chained_location_id,
                                          'optional_loc': True,
                                          }, context=context)
        else:
            # Reactivate the location
            location_obj.write(cr, uid, [location.id], {'active': True}, context=context)
        
        return_view_id = data_obj.get_object_reference(cr, uid, 'stock', 'view_location_tree')
        
        if return_view_id:
            return {'type': 'ir.actions.act_window',
                    'name': 'Locations Structure',
                    'res_model': 'stock.location',
                    'domain': [('location_id','=',False)],
                    'view_type': 'tree',
                    'target': 'crush',
                    'view_id': [return_view_id[1]],
                    }
        else:
            return {'type': 'ir.actions.act_window'}
    
stock_location_configuration_wizard()


class stock_remove_location_wizard(osv.osv_memory):
    _name = 'stock.remove.location.wizard'
    
    _columns = {
        'location_id': fields.many2one('stock.location', string='Location to remove'),
        'location_usage': fields.selection([('internal', 'Internal'), ('customer', 'External')], string='Location type'),
        'location_category': fields.selection([('eprep', 'EPREP'), ('stock', 'Stock'), ('consumption_unit', 'Consumption Unit')],
                                              string='Location type'),
        'error_message': fields.text(string='Information Message', readonly=True),
        'error': fields.boolean(string='Error'),
        'move_from_to': fields.boolean(string='Has a move from/to the location'),
        'not_empty': fields.boolean(string='Location not empty'),
        'has_child': fields.boolean(string='Location has children locations'),
    }
    
    def location_id_on_change(self, cr, uid, ids, location_id, context=None):
        '''
        Check if no moves to this location aren't done
        Check if there is no stock in this location
        '''
        res = {'error_message': '', 
               'move_from_to': False, 
               'not_empty': False,
               'has_child': False}
        warning = {}
        error = False
        
        if location_id:
            location = self.pool.get('stock.location').browse(cr, uid, location_id, context=context)
            # Check if no moves to this location aren't done
            move_from_to = self.pool.get('stock.move').search(cr, uid, [('state', 'not in', ('done', 'cancel')), '|', ('location_id', '=', location.id), ('location_dest_id', '=', location.id)])
            if move_from_to:
                error = True
                res['move_from_to'] = True
                res['error_message'] += '''* You have at least one move from or to the location '%s' which is not 'Done'.
Please click on the 'See moves' button to see which moves are still in progress from/to this location.''' %location.name
                res['error_message'] += '\n' + '\n'
            # Check if no stock in the location
            if location.stock_real and location.usage == 'internal':
                error = True
                res['not_empty'] = True
                res['error_message'] += '''* The location '%s' is not empty of products. 
Please click on the 'Products in location' button to see which products are still in the location.''' %location.name
                res['error_message'] += '\n' + '\n'

            # Check if the location has children locations
            if location.child_ids:
                error = True
                res['has_child'] = True
                res['error_message'] += '''* The location '%s' has children locations.
Please remove all children locations before remove it. 
Please click on the 'Children locations' button to see all children locations.''' %location.name
                res['error_message'] += '\n' + '\n'
                
        if error:
            warning.update({'title': 'Be careful !',
                            'message': 'You have a problem with this location − Please see the message in the form for more information.'})
            
        res['error'] = error
        
        return {'value': res,
                'warning': warning}
        
    def check_error(self, cr, uid, ids, context=None):
        '''
        Check if errors are always here
        '''
        for wizard in self.browse(cr, uid, ids, context=context):
            errors = self.location_id_on_change(cr, uid, ids, wizard.location_id.id, context=context)
            self.write(cr, uid, ids, errors.get('value', {}), context=context)
            
        return {'type': 'ir.actions.act_window',
                'res_model': 'stock.remove.location.wizard',
                'res_id': wizard.id,
                'view_type': 'form',
                'view_mode': 'form',
                'target': 'new',}
        
    def location_usage_change(self, cr, uid, ids, usage, context=None):
        if usage and usage == 'customer':
            return {'value': {'location_category': 'consumption_unit'}} 
        
        return {}
        
    def deactivate_location(self, cr, uid, ids, context=None):
        '''
        Deactivate the selected location
        '''
        location = False
        data_obj = self.pool.get('ir.model.data')
        location_obj = self.pool.get('stock.location')
        configurable_loc_id = data_obj.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_internal_client_view')[1]
        intermediate_loc_id = data_obj.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_intermediate_client_view')[1]
        internal_cu_loc_id = data_obj.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_consumption_units_view')[1]
        
        for wizard in self.browse(cr, uid, ids, context=context):
            if wizard.error or wizard.has_child or wizard.not_empty or wizard.move_from_to:
                raise osv.except_osv(_('Error'), _('You cannot remove this location because some errors are still here !'))
            
            location = wizard.location_id
        
        # De-activate the location
        location_obj.write(cr, uid, [location.id], {'active': False}, context=context)
            
        # Check if parent location should be also de-activated
        if location.location_id.id in (intermediate_loc_id, internal_cu_loc_id, configurable_loc_id):
            empty = True
            for child in location.location_id.child_ids:
                if child.active:
                    empty = False
            if empty:
                location_obj.write(cr, uid, [location.location_id.id], {'active': False}, context=context)
                
                if location.location_id.location_id.id == configurable_loc_id:
                    empty2 = True
                    for child in location.location_id.location_id.child_ids:
                        if child.active:
                            empty2 = False
                    if empty2:
                        location_obj.write(cr, uid, [location.location_id.location_id.id], {'active': False}, context=context)
            
        return_view_id = data_obj.get_object_reference(cr, uid, 'stock', 'view_location_tree')
        
        if return_view_id:
            return {'type': 'ir.actions.act_window',
                    'res_model': 'stock.location',
                    'domain': [('location_id','=',False)],
                    'view_type': 'tree',
                    'target': 'crush',
                    'view_id': [return_view_id[1]],
                    }
        else:
            return {'type': 'ir.actions.act_window'}
    
    def see_moves(self, cr, uid, ids, context=None):
        '''
        Returns all stock.picking containing a stock move not done from/to the location
        '''
        location = False
        picking_ids = []
        
        for wizard in self.browse(cr, uid, ids, context=context):
            location = wizard.location_id
            
        move_ids = self.pool.get('stock.move').search(cr, uid, [('state', 'not in', ('done', 'cancel')), '|', ('location_id', '=', location.id), ('location_dest_id', '=', location.id)])
        for move in self.pool.get('stock.move').browse(cr, uid, move_ids, context=context):
            if move.picking_id and move.picking_id.id not in picking_ids:
                picking_ids.append(move.picking_id.id)
                
        view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'vpicktree')[1]
        if location.usage == 'customer':
            view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'view_picking_out_tree')[1]
        
        return {'type': 'ir.actions.act_window',
                'res_model': 'stock.picking',
                'domain': [('id', 'in', picking_ids)],
                'view_id': [view_id],
                'view_type': 'form',
                'view_mode': 'tree,form',
                'target': 'current',
                }
        
    def products_in_location(self, cr, uid, ids, context=None):
        '''
        Returns a list of products in the location
        '''
        if context is None:
            context = {}
        location = False
        
        for wizard in self.browse(cr, uid, ids, context=context):
            location = wizard.location_id
            
        context.update({'contact_display': 'partner', 'search_default_real':1, 
                        'search_default_location_type_internal':1,
                        'search_default_group_product':1,
                        'group_by':[], 'group_by_no_leaf':1})
        context.update({'search_default_location_id': location.id})
        
        return {'type': 'ir.actions.act_window',
                'res_model': 'report.stock.inventory',
                'view_type': 'form',
                'view_mode': 'tree',
                'domain': [('location_id', '=', location.id)],
                'context': context,
                'target': 'current'}
        
    def children_location(self, cr, uid, ids, context=None):
        '''
        Returns the list of all children locations
        '''
        location_ids = []
        location = False
        
        for wizard in self.browse(cr, uid, ids, context=context):
            location = wizard.location_id
            
        for loc in location.child_ids:
            location_ids.append(loc.id)
            
        return {'type': 'ir.actions.act_window',
                'res_model': 'stock.location',
                'view_type': 'form',
                'view_mode': 'tree,form',
                'domain': [('id', 'in', location_ids)],
                'target': 'current',}
    
stock_remove_location_wizard()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: