File: quality.py

package info (click to toggle)
tryton-modules-quality 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 680 kB
  • sloc: python: 1,020; xml: 697; makefile: 11; sh: 3
file content (816 lines) | stat: -rw-r--r-- 28,171 bytes parent folder | download
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
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

import datetime as dt
from functools import wraps
from random import Random

from sql.functions import CharLength

from trytond.i18n import gettext, lazy_gettext
from trytond.model import (
    DeactivableMixin, DictSchemaMixin, MatchMixin, Model, ModelSingleton,
    ModelSQL, ModelStorage, ModelView, Workflow, dualmethod, fields)
from trytond.model.exceptions import ButtonActionException
from trytond.modules.company.model import (
    CompanyMultiValueMixin, CompanyValueMixin, employee_field, reset_employee,
    set_employee)
from trytond.pool import Pool
from trytond.pyson import Eval, Id, If
from trytond.transaction import Transaction
from trytond.wizard import Button, StateTransition, StateView, Wizard

from .exceptions import InspectionError, InspectionValidationError


class Configuration(
        ModelSingleton, CompanyMultiValueMixin, ModelSQL, ModelView):
    "Quality Configuration"
    __name__ = 'quality.configuration'

    inspection_sequence = fields.MultiValue(fields.Many2One(
            'ir.sequence', "Inspection Sequence", required=True,
            domain=[
                ('company', 'in', [
                        Eval('context', {}).get('company', -1),
                        None]),
                ('sequence_type', '=',
                    Id('quality', 'sequence_type_quality_inspection')),
                ]))
    alert_sequence = fields.MultiValue(fields.Many2One(
            'ir.sequence', "Alert Sequence", required=True,
            domain=[
                ('company', 'in', [
                        Eval('context', {}).get('company', -1),
                        None]),
                ('sequence_type', '=',
                    Id('quality', 'sequence_type_quality_alert')),
                ]))

    @classmethod
    def multivalue_model(cls, field):
        pool = Pool()
        if field in {'inspection_sequence', 'alert_sequence'}:
            return pool.get('quality.configuration.sequence')
        return super().multivalue_model(field)

    @classmethod
    def default_inspection_sequence(cls, **pattern):
        return (
            cls.multivalue_model('inspection_sequence')
            .default_inspection_sequence())

    @classmethod
    def default_alert_sequence(cls, **pattern):
        return (
            cls.multivalue_model('alert_sequence')
            .default_alert_sequence())


class ConfigurationSequence(CompanyValueMixin, ModelSQL):
    "Quality Configuration Sequence"
    __name__ = 'quality.configuration.sequence'
    inspection_sequence = fields.Many2One(
        'ir.sequence', "Inspection Sequence", required=True,
        domain=[
            ('company', 'in', [Eval('company', -1), None]),
            ('sequence_type', '=',
                Id('quality', 'sequence_type_quality_inspection')),
            ])
    alert_sequence = fields.Many2One(
        'ir.sequence', "Alert Sequence", required=True,
        domain=[
            ('company', 'in', [Eval('company', -1), None]),
            ('sequence_type', '=',
                Id('quality', 'sequence_type_quality_alert')),
            ])

    @classmethod
    def default_inspection_sequence(cls):
        pool = Pool()
        ModelData = pool.get('ir.model.data')
        try:
            return ModelData.get_id('quality', 'sequence_quality_inspection')
        except KeyError:
            return None

    @classmethod
    def default_alert_sequence(cls):
        pool = Pool()
        ModelData = pool.get('ir.model.data')
        try:
            return ModelData.get_id('quality', 'sequence_quality_alert')
        except KeyError:
            return None


class ControlledMixin(ModelStorage):
    __slots__ = ()

    quality_inspections = fields.One2Many(
        'quality.inspection', 'origin',
        lazy_gettext('quality.msg_quality_inspections'), readonly=True)

    @classmethod
    def copy(cls, records, default=None):
        default = default.copy() if default is not None else {}
        default.setdefault('quality_inspections')
        return super().copy(records, default=default)

    def quality_control_pattern(self, operation):
        return {}

    @dualmethod
    def quality_control_needed(cls, records, operation):
        pool = Pool()
        Control = pool.get('quality.control')
        return bool(Control.get_inspections(records, operation))

    def quality_controlled_for(self, operation):
        operation = f'{self.__name__}:{operation}'
        return any(
            i for i in self.quality_inspections
            if operation in i.control.operations)

    def quality_inspections_pending(self):
        return [i for i in self.quality_inspections if i.state == 'pending']

    def quality_inspections_failed(self):
        return [
            i for i in self.quality_inspections
            if i.state == 'failed'
            and (any(a.state in {'open', 'processing'} for a in i.alerts)
                or not i.alerts)]

    @staticmethod
    def control(operation, wizard):
        def decorator(func):
            @wraps(func)
            def wrapper(cls, records):
                if (cls.quality_control_needed(records, operation)
                        or any(
                            r.quality_inspections_pending() for r in records)):
                    raise ButtonActionException(wizard)
                if any(r.quality_inspections_failed() for r in records):
                    raise InspectionError(gettext(
                            'quality.msg_quality_inspection_failed'))
                return func(cls, records)
            return wrapper
        return decorator


class Control(DeactivableMixin, MatchMixin, ModelSQL, ModelView):
    "Quality Control"
    __name__ = 'quality.control'

    name = fields.Char("Name", required=True, translate=True)

    operations = fields.MultiSelection([
            ('stock.shipment.in:receive', "Supplier Shipment Received"),
            ('stock.shipment.in:done', "Supplier Shipment Done"),
            ('stock.shipment.out:pick', "Customer Shipment Picked"),
            ('stock.shipment.out:pack', "Customer Shipment Packed"),
            ('stock.shipment.out.return:receive',
                "Customer Shipment Return Received"),
            ('stock.shipment.out.return:done',
                "Customer Shipment Return Done"),
            ('stock.shipment.internal:ship', "Internal Shipment Shipped"),
            ('stock.shipment.internal:done', "Internal Shipment Done"),
            ('production:run', "Production Run"),
            ('production:done', "Production Done"),
            ], "Operations",
        help="The operations for which the control is performed.")
    frequency = fields.Float(
        "Frequency", digits=(1, 4), required=True,
        domain=[
            ('frequency', '>=', 0),
            ('frequency', '<=', 1),
            ],
        help="How often the control must be done.")
    company = fields.Many2One(
        'company.company', "Company", ondelete='RESTRICT',
        help="The company to which the control applies.")
    product_category = fields.Many2One(
        'product.category', "Product Category", ondelete='RESTRICT',
        help="The product category to which the control applies.")
    product = fields.Many2One(
        'product.product', 'Product', ondelete='RESTRICT',
        context={
            'company': Eval('company', Eval('context', {}).get('company')),
            },
        help="The product to which the control applies.")

    points = fields.One2Many(
        'quality.control.point', 'control', "Points", required=True)

    @classmethod
    def default_frequency(cls):
        return 1

    @classmethod
    def get_inspections(cls, records, operation):
        pool = Pool()
        Inspection = pool.get('quality.inspection')

        records = [
            r for r in records if not r.quality_controlled_for(operation)]
        inspections = []
        if records:
            name = records[0].__name__
            assert len({r.__name__ for r in records}) == 1

            controls = cls.search([
                    ('operations', 'in', f'{name}:{operation}'),
                    ])
            for control in controls:
                for record in records:
                    pattern = record.quality_control_pattern(operation)
                    if control.match(pattern) and control.choose(record):
                        inspections.append(
                            Inspection.get_from_control(control, record))
        return inspections

    def match(self, pattern, match_none=False):
        pool = Pool()
        Product = pool.get('product.product')
        pattern = pattern.copy()

        def parents(categories):
            for category in categories:
                while category:
                    yield category
                    category = category.parent

        products = set(pattern.pop('products', []))
        if pattern.get('product'):
            products.add(pattern.pop('product'))
        if products:
            products = Product.browse(products)
            if self.product and self.product not in products:
                return False
            if self.product_category:
                categories = {
                    c for p in products for c in parents(p.categories_all)}
                if self.product_category not in categories:
                    return False
        return super().match(pattern, match_none=match_none)

    def choose(self, record):
        random = Random(record.id)
        return random.random() <= self.frequency


class ControlPoint(DictSchemaMixin, ModelSQL, ModelView):
    "Quality Control Point"
    __name__ = 'quality.control.point'

    control = fields.Many2One('quality.control', "Control", required=True)
    tolerance_lower = fields.Float(
        "Tolerance Lower",
        states={
            'invisible': ~Eval('type_').in_(['integer', 'float', 'numeric']),
            })
    tolerance_upper = fields.Float(
        "Tolerance Upper",
        states={
            'invisible': ~Eval('type_').in_(['integer', 'float', 'numeric']),
            })

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls.__access__.add('control')
        cls.type_.selection = [
            ('boolean', lazy_gettext('ir.msg_dict_schema_boolean')),
            ('integer', lazy_gettext('ir.msg_dict_schema_integer')),
            ('float', lazy_gettext('ir.msg_dict_schema_float')),
            ('numeric', lazy_gettext('ir.msg_dict_schema_numeric')),
            ]

    def check(self, value):
        if self.type_ == 'boolean':
            return value
        elif self.type_ in {'integer', 'float', 'numeric'}:
            result = True
            if value is None:
                result = False
            elif (self.tolerance_lower is not None
                    and self.tolerance_lower > value):
                result = False
            elif (self.tolerance_upper is not None
                    and self.tolerance_upper < value):
                result = False
            return result


class Inspection(Workflow, ModelSQL, ModelView):
    "Quality Inspection"
    __name__ = 'quality.inspection'
    _rec_name = 'number'

    _states = {
        'readonly': Eval('state') != 'pending',
        }

    number = fields.Char("Number", required=True, readonly=True)
    company = fields.Many2One(
        'company.company', "Company", required=True, states=_states)
    origin = fields.Reference(
        "Origin", 'get_origins', states=_states,
        domain={
            'stock.shipment.in': [
                ('company', '=', Eval('company', -1)),
                ],
            'stock.shipment.out': [
                ('company', '=', Eval('company', -1)),
                ],
            'stock.shipment.out.return': [
                ('company', '=', Eval('company', -1)),
                ],
            'stock.shipment.internal': [
                ('company', '=', Eval('company', -1)),
                ],
            'production': [
                ('company', '=', Eval('company', -1)),
                ],
            })
    control = fields.Many2One(
        'quality.control', "Control", required=True, states=_states,
        domain=[
            ('company', 'in', [Eval('company', -1), None]),
            ])

    points = fields.Dict(
        'quality.control.point', "Points",
        domain=[
            ('control', '=', Eval('control', -1)),
            ],
        states=_states)

    alerts = fields.One2Many(
        'quality.alert', 'origin', "Alerts",
        states={
            'invisible': (
                (Eval('state') != 'failed')
                | ~Eval('alerts')),
            })

    processed_by = employee_field("Processed by", states=['passed', 'failed'])
    processed_at = fields.DateTime("Processed at", states=_states)
    passed_by = employee_field("Passed by", states=['passed'])
    failed_by = employee_field("Failed by", states=['failed'])

    state = fields.Selection([
            ('pending', "Pending"),
            ('passed', "Passed"),
            ('failed', "Failed"),
            ], "State", required=True, readonly=True, sort=False)

    @classmethod
    def __setup__(cls):
        cls.number.search_unaccented = False
        super().__setup__()
        cls._transitions |= {
            ('pending', 'passed'),
            ('pending', 'failed'),
            ('passed', 'failed'),
            ('passed', 'pending'),
            ('failed', 'passed'),
            ('failed', 'pending'),
            }
        cls._buttons.update({
                'pending': {
                    'invisible': ~Eval('state').in_(['passed', 'failed']),
                    'icon': 'tryton-back',
                    'depends': ['state'],
                    },
                'process': {
                    'invisible': Eval('state') != 'pending',
                    'icon': 'tryton-forward',
                    'depends': ['state'],
                    },
                'pass_': {
                    'invisible': Eval('state') != 'failed',
                    'icon': 'tryton-ok',
                    'depends': ['state'],
                    },
                'fail': {
                    'invisible': Eval('state') != 'passed',
                    'icon': 'tryton-cancel',
                    'depends': ['state'],
                    },
                })

    @classmethod
    def order_number(cls, tables):
        table, _ = tables[None]
        return [CharLength(table.number), table.number]

    @classmethod
    def default_company(cls):
        return Transaction().context.get('company')

    @classmethod
    def default_state(cls):
        return 'pending'

    @classmethod
    def _get_origins(cls):
        return [
            'stock.shipment.in',
            'stock.shipment.out',
            'stock.shipment.out.return',
            'stock.shipment.internal',
            'production',
            ]

    @classmethod
    def get_origins(cls):
        pool = Pool()
        IrModel = pool.get('ir.model')
        models = cls._get_origins()
        models = IrModel.search([
                ('model', 'in', models),
                ])
        return [(None, '')] + [(m.model, m.name) for m in models]

    @fields.depends('control', 'points')
    def on_change_control(self):
        if self.control:
            points = dict(self.points) if self.points is not None else {}
            points.update(
                (p.name, None) for p in self.control.points)
            self.points = points

    def validate_points(self):
        points = {p.name: p.string for p in self.control.points}
        missing = points.keys() - (self.points or {}).keys()
        if missing:
            raise InspectionValidationError(
                gettext('quality.msg_quality_inspection_missing_point',
                    inspection=self.rec_name,
                    points=', '.join(points[m] for m in missing),
                    ))

    def check(self):
        for point in self.control.points:
            if not point.check(self.points.get(point.name)):
                return False
        return True

    @classmethod
    def view_attributes(cls):
        attributes = super().view_attributes() + [
            ('/tree', 'visual', If(Eval('state') == 'failed', 'danger', '')),
            ]
        if Transaction().context.get('inspect'):
            attributes.extend([
                    ('//field[@name="control"]', 'readonly', 1),
                    ('//*[@name="origin"]', 'invisible', 1),
                    ('//page[@id="other"]', 'invisible', 1),
                    ('//*[@name="state"]', 'invisible', 1),
                    ('//group[@id="buttons"]', 'states', {'invisible': True}),
                    ])

        return attributes

    @classmethod
    @ModelView.button
    @Workflow.transition('pending')
    @reset_employee('processed_by', 'passed_by', 'failed_by')
    def pending(cls, inspections):
        cls.write(inspections, {
                'processed_at': None,
                })

    @dualmethod
    @ModelView.button
    @set_employee('processed_by')
    def process(cls, inspections):
        for inspection in inspections:
            inspection.validate_points()
        cls.write([i for i in inspections if not i.processed_at], {
                'processed_at': dt.datetime.now(),
                })
        to_pass, to_fail = [], []
        for inspection in inspections:
            if inspection.check():
                to_pass.append(inspection)
            else:
                to_fail.append(inspection)
        cls.pass_(to_pass)
        cls.fail(to_fail)

    @classmethod
    @ModelView.button
    @Workflow.transition('passed')
    @reset_employee('failed_by')
    @set_employee('passed_by')
    def pass_(cls, inspections):
        pass

    @classmethod
    @ModelView.button
    @Workflow.transition('failed')
    @reset_employee('passed_by')
    @set_employee('failed_by')
    def fail(cls, inspections):
        pool = Pool()
        Alert = pool.get('quality.alert')
        alerts = []
        for inspection in inspections:
            if not inspection.alerts:
                alerts.append(inspection.get_alert())
        Alert.save(alerts)

    def get_alert(self):
        pool = Pool()
        Alert = pool.get('quality.alert')
        return Alert(
            company=self.company,
            origin=self)

    @classmethod
    def create(cls, vlist):
        pool = Pool()
        Configuration = pool.get('quality.configuration')

        config = Configuration(1)
        default_company = cls.default_company()
        vlist = [v.copy() for v in vlist]
        for values in vlist:
            if not values.get('number'):
                values['number'] = config.get_multivalue(
                    'inspection_sequence',
                    company=values.get('company', default_company)).get()
        return super().create(vlist)

    @classmethod
    def copy(cls, inspections, default=None):
        default = default.copy() if default is not None else {}
        default.setdefault('number')
        default.setdefault('processed_by')
        default.setdefault('processed_at')
        default.setdefault('passed_by')
        default.setdefault('failed_by')
        return super().copy(inspections, default=default)

    @classmethod
    def get_from_control(cls, control, origin=None):
        return cls(control=control, origin=origin)


class Alert(Workflow, ModelSQL, ModelView):
    "Quality Alert"
    __name__ = 'quality.alert'
    _rec_name = 'number'

    _states = {
        'readonly': ~Eval('state').in_(['open', 'processing']),
        }

    number = fields.Char("Number", required=True, readonly=True)
    company = fields.Many2One(
        'company.company', "Company", required=True, states=_states)
    title = fields.Char("Title")
    origin = fields.Reference(
        "Origin", 'get_origins', required=True,
        domain={
            'quality.inspection': [
                ('company', '=', Eval('company', -1)),
                ],
            })
    description = fields.Text("Description")

    processed_by = employee_field(
        "Processed by", states=['processing', 'resolved', 'deferred'])
    deferred_by = employee_field(
        "Deferred by", states=['deferred', 'resolved'])
    resolved_by = employee_field("Resolved by", states=['resolved'])

    state = fields.Selection([
            ('open', "Open"),
            ('processing', "Processing"),
            ('deferred', "Deferred"),
            ('resolved', "Resolved"),
            ], "State", required=True, readonly=True, sort=False)

    @classmethod
    def __setup__(cls):
        cls.number.search_unaccented = False
        super().__setup__()
        cls._transitions |= {
            ('open', 'processing'),
            ('processing', 'resolved'),
            ('processing', 'deferred'),
            ('deferred', 'processing'),
            ('resolved', 'open'),
            }
        cls._buttons.update({
                'open': {
                    'invisible': Eval('state') != 'resolved',
                    'icon': 'tryton-back',
                    'depends': ['state'],
                    },
                'process': {
                    'invisible': ~Eval('state').in_(['open', 'deferred']),
                    'icon': 'tryton-forward',
                    'depends': ['state'],
                    },
                'defer': {
                    'invisible': Eval('state') != 'processing',
                    'icon': 'tryton-archive',
                    'depends': ['state'],
                    },
                'resolve': {
                    'invisible': Eval('state') != 'processing',
                    'icon': 'tryton-ok',
                    'depends': ['state'],
                    },
                })

    @classmethod
    def default_company(cls):
        return Transaction().context.get('company')

    @classmethod
    def default_state(cls):
        return 'open'

    @classmethod
    def _get_origins(cls):
        return ['quality.inspection']

    @classmethod
    def get_origins(cls):
        pool = Pool()
        IrModel = pool.get('ir.model')
        models = cls._get_origins()
        models = IrModel.search([
                ('model', 'in', models),
                ])
        return [(None, '')] + [(m.model, m.name) for m in models]

    @classmethod
    @ModelView.button
    @Workflow.transition('open')
    @reset_employee('processed_by', 'deferred_by', 'resolved_by')
    def open(cls, alerts):
        pass

    @classmethod
    @ModelView.button
    @Workflow.transition('processing')
    @set_employee('processed_by')
    def process(cls, alerts):
        pass

    @classmethod
    @ModelView.button
    @Workflow.transition('deferred')
    @reset_employee('processed_by')
    @set_employee('deferred_by')
    def defer(cls, alerts):
        pass

    @classmethod
    @ModelView.button
    @Workflow.transition('resolved')
    @set_employee('resolved_by')
    def resolve(cls, alerts):
        pass

    @classmethod
    def create(cls, vlist):
        pool = Pool()
        Configuration = pool.get('quality.configuration')

        config = Configuration(1)
        default_company = cls.default_company()
        vlist = [v.copy() for v in vlist]
        for values in vlist:
            if not values.get('number'):
                values['number'] = config.get_multivalue(
                    'alert_sequence',
                    company=values.get('company', default_company)).get()
        return super().create(vlist)

    @classmethod
    def copy(cls, inspections, default=None):
        default = default.copy() if default is not None else {}
        default.setdefault('number')
        default.setdefault('processed_by')
        default.setdefault('deferred_by')
        default.setdefault('resolved_by')
        return super().copy(inspections, default=default)


class InspectStateView(StateView):
    def get_view(self, wizard, state_name):
        with Transaction().set_context(inspect=True):
            return super().get_view(wizard, state_name)

    def get_defaults(self, wizard, state_name, fields):
        return {}


class Inspect(Wizard):
    "Inspect Quality"
    __name__ = 'quality.inspect'

    start = StateTransition()
    store = StateView('quality.inspect.store', None, [])
    next_ = StateTransition()
    inspection = InspectStateView(
        'quality.inspection', 'quality.quality_inspection_view_form', [
            Button("Cancel", 'end', 'tryton-cancel'),
            Button("Skip", 'next_', 'tryton-forward', validate=False),
            Button("Save", 'save', 'tryton-ok', default=True),
            ])
    save = StateTransition()

    def transition_start(self):
        pool = Pool()
        Control = pool.get('quality.control')
        Inspection = pool.get('quality.inspection')
        Store = pool.get('quality.inspect.store')

        context = Transaction().context
        operation = self.operation(context.get('action_id'))

        inspections = Control.get_inspections(self.records, operation)
        Inspection.save(inspections)

        inspections = []
        for record in self.records:
            inspections.extend(record.quality_inspections_pending())
        self.store = Store(inspections=inspections)
        return 'inspection' if self.store.inspections else 'end'

    @property
    def _operations(self):
        return {
            'quality.wizard_stock_shipment_in_inspect_receive': 'receive',
            'quality.wizard_stock_shipment_in_inspect_done': 'done',
            'quality.wizard_stock_shipment_out_inspect_pick': 'pick',
            'quality.wizard_stock_shipment_out_inspect_pack': 'pack',
            'quality.wizard_stock_shipment_out_return_inspect_receive':
            'receive',
            'quality.wizard_stock_shipment_out_return_inspect_done': 'done',
            'quality.wizard_stock_shipment_internal_inspect_ship': 'ship',
            'quality.wizard_stock_shipment_internal_inspect_done': 'done',
            'quality.wizard_production_inspect_run': 'run',
            'quality.wizard_production_inspect_done': 'done',
            }

    def operation(self, action_id):
        pool = Pool()
        ModelData = pool.get('ir.model.data')
        for xml_id, operation in self._operations.items():
            try:
                if action_id == ModelData.get_id(xml_id):
                    return operation
            except KeyError:
                continue

    def transition_next_(self):
        self.store.inspections = self.store.inspections[1:]
        return 'inspection' if self.store.inspections else 'end'

    def value_inspection(self, fields):
        inspection = self.store.inspections[0]
        values = {}
        for fieldname in fields:
            value = getattr(inspection, fieldname)
            if isinstance(value, Model):
                field = getattr(inspection.__class__, fieldname)
                if field._type == 'reference':
                    value = str(value)
                else:
                    value = value.id
            elif isinstance(value, (list, tuple)):
                value = [r.id for r in value]
            values[fieldname] = value

        if 'points' in fields:
            # Convert ImmutableDict to dict
            values['points'] = dict(values['points'] or {})
            for point in inspection.control.points:
                values['points'].setdefault(point.name)
        return values

    def transition_save(self):
        pool = Pool()
        Inspection = pool.get('quality.inspection')
        inspection = self.store.inspections[0]
        Inspection.write([inspection], self.inspection._save_values)
        inspection.process()
        return 'next_'


class InspectStore(ModelView):
    "Inspect Quality"
    __name__ = 'quality.inspect.store'

    inspections = fields.Many2Many(
        'quality.inspection', None, None, "Inspections")