File: common.py

package info (click to toggle)
tryton-modules-incoterm 7.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 588 kB
  • sloc: python: 825; xml: 538; makefile: 11; sh: 3
file content (178 lines) | stat: -rw-r--r-- 6,269 bytes parent folder | download | duplicates (2)
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
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

from trytond.i18n import lazy_gettext
from trytond.model import Model, fields
from trytond.pool import Pool
from trytond.pyson import Eval, If

__all__ = ['IncotermMixin', 'IncotermAvailableMixin']


class IncotermMixin(Model):

    incoterm = fields.Many2One(
        'incoterm.incoterm', lazy_gettext('incoterm.msg_incoterm'),
        ondelete='RESTRICT')
    incoterm_location = fields.Many2One(
        'party.address', lazy_gettext('incoterm.msg_incoterm_location'),
        ondelete='RESTRICT',
        search_order=[
            ('is_incoterm_related', 'DESC NULLS LAST'),
            ('party.distance', 'ASC NULLS LAST'),
            ('id', None),
            ])

    @classmethod
    def __setup__(cls):
        super().__setup__()
        readonly = cls._incoterm_readonly_state()
        cls.incoterm.states = {
            'readonly': readonly,
            }

        cls.incoterm_location.states = {
            'readonly': readonly,
            'invisible': ~Eval('incoterm', False),
            }
        related_party, related_party_depends = cls._incoterm_related_party()
        cls.incoterm_location.search_context = {
            'related_party': related_party,
            }
        cls.incoterm_location.depends = {'incoterm'} | related_party_depends

    @classmethod
    def _incoterm_readonly_state(cls):
        return ~Eval('state').in_(['draft'])

    @classmethod
    def _incoterm_related_party(cls):
        return Eval('party'), {'party'}

    @property
    def incoterm_name(self):
        name = ''
        if self.incoterm:
            name = self.incoterm.rec_name
            if self.incoterm_location:
                name += ' %s' % self.incoterm_location.rec_name
        return name


class IncotermAvailableMixin(IncotermMixin):

    available_incoterms = fields.Function(fields.Many2Many(
            'incoterm.incoterm', None, None, "Available Incoterms"),
        'on_change_with_available_incoterms')
    incoterm_location_required = fields.Function(fields.Boolean(
            lazy_gettext('incoterm.msg_incoterm_location_required')),
        'on_change_with_incoterm_location_required')

    @classmethod
    def __setup__(cls):
        super().__setup__()
        readonly = cls._incoterm_readonly_state()
        cls.incoterm.domain = [
            If(~readonly,
                ('id', 'in', Eval('available_incoterms', [])),
                ()),
            ]
        cls.incoterm_location.states['required'] = (
            Eval('incoterm_location_required', False))

    @fields.depends('company', 'party', methods=['_get_incoterm_pattern'])
    def on_change_with_available_incoterms(self, name=None):
        pool = Pool()
        Incoterm = pool.get('incoterm.incoterm')
        pattern = self._get_incoterm_pattern()
        incoterms = Incoterm.get_incoterms(self.company, pattern)
        if self.party:
            party_incoterms = {r.incoterm for r in self._party_incoterms}
        else:
            party_incoterms = set()
        return [
            i for i in incoterms
            if not party_incoterms or i in party_incoterms]

    @fields.depends()
    def _get_incoterm_pattern(self):
        return {}

    @fields.depends('incoterm')
    def on_change_with_incoterm_location_required(self, name=None):
        if self.incoterm:
            return self.incoterm.location

    @fields.depends(methods=['_set_default_incoterm'])
    def on_change_company(self):
        try:
            super_on_change = super().on_change_company
        except AttributeError:
            pass
        else:
            super_on_change()
        self._set_default_incoterm()

    @fields.depends(methods=['_set_default_incoterm'])
    def on_change_party(self):
        try:
            super_on_change = super().on_change_party
        except AttributeError:
            pass
        else:
            super_on_change()
        self._set_default_incoterm()

    @fields.depends('incoterm', 'party', 'company',
        methods=['_party_incoterms'])
    def on_change_incoterm(self):
        if self.incoterm:
            if self._party_incoterms:
                for record in self._party_incoterms:
                    if record.company and record.company != self.company:
                        continue
                    if record.incoterm == self.incoterm:
                        self.incoterm_location = record.incoterm_location
                        break
                else:
                    self.incoterm_location = None
        else:
            self.incoterm_location = None

    @fields.depends('incoterm', 'party', 'company',
        methods=['on_change_with_available_incoterms',
            '_incoterm_required', '_party_incoterms'])
    def _set_default_incoterm(self):
        self.available_incoterms = self.on_change_with_available_incoterms()
        if not self.available_incoterms:
            self.incoterm = None
            self.incoterm_location = None
        elif self._incoterm_required:
            if self.incoterm not in self.available_incoterms:
                if len(self.available_incoterms) == 1:
                    self.incoterm, = self.available_incoterms
                else:
                    self.incoterm = None
                self.incoterm_location = None
            if self.party and self._party_incoterms:
                for record in self._party_incoterms:
                    if record.company and record.company != self.company:
                        continue
                    if record.incoterm in self.available_incoterms:
                        self.incoterm = record.incoterm
                        self.incoterm_location = record.incoterm_location
                        break
                else:
                    self.incoterm = None
                    self.incoterm_location = None
        elif self.incoterm not in self.available_incoterms:
            self.incoterm = None
            self.incoterm_location = None

    @property
    def _party_incoterms(self):
        raise NotImplementedError

    @property
    def _incoterm_required(self):
        raise NotImplementedError