File: common.py

package info (click to toggle)
tryton-modules-product-kit 7.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: python: 1,450; xml: 124; makefile: 11; sh: 3
file content (454 lines) | stat: -rw-r--r-- 16,878 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
# 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 collections import defaultdict
from decimal import Decimal
from functools import wraps

from trytond.model import ModelStorage, ModelView, Workflow, fields
from trytond.pool import Pool
from trytond.pyson import Eval


def get_shipments_returns(model_name):
    def _get_shipments_returns(func):
        @wraps(func)
        def wrapper(self, name):
            pool = Pool()
            Model = pool.get(model_name)
            shipments = set(func(self, name))
            for line in self.lines:
                for component in line.components:
                    for move in component.moves:
                        if isinstance(move.shipment, Model):
                            shipments.add(move.shipment.id)
            return list(shipments)
        return wrapper
    return _get_shipments_returns


def search_shipments_returns(model_name):
    def _search_shipments_returns(func):
        @wraps(func)
        def wrapper(cls, name, clause):
            domain = func(cls, name, clause)
            _, operator, operand, *extra = clause
            nested = clause[0][len(name):]
            if not nested:
                if isinstance(operand, str):
                    nested = '.rec_name'
                else:
                    nested = '.id'
            return ['OR',
                domain,
                ('lines.components.moves.shipment' + nested,
                    operator, operand, model_name, *extra),
                ]
        return wrapper
    return _search_shipments_returns


def get_moves(func):
    @wraps(func)
    def wrapper(self, name):
        return func(self, name) + [
            m.id for l in self.lines for c in l.components for m in c.moves]
    return wrapper


def search_moves(func):
    @wraps(func)
    def wrapper(cls, name, clause):
        return ['OR',
            func(cls, name, clause),
            ('lines.components.' + clause[0], *clause[1:]),
            ]
    return wrapper


def order_mixin(prefix):
    class OrderMixin(ModelStorage):

        @classmethod
        def copy(cls, records, default=None):
            pool = Pool()
            Line = pool.get(prefix + '.line')
            if default is None:
                default = {}
            else:
                default = default.copy()
            default.setdefault(
                'lines.component_parent',
                lambda data: data['component_parent'])
            records = super().copy(records, default=default)
            lines = []
            for record in records:
                for line in record.lines:
                    if line.component_parent:
                        lines.append(line)
            Line.delete(lines)
            return records

        @classmethod
        @ModelView.button
        @Workflow.transition('draft')
        def draft(cls, records):
            pool = Pool()
            Line = pool.get(prefix + '.line')
            to_delete = []
            to_save = []
            for record in records:
                for line in record.lines:
                    if line.component_parent:
                        to_delete.append(line)
                    elif line.components:
                        line.components = None
                        to_save.append(line)
            Line.save(to_save)
            super().draft(records)
            Line.delete(to_delete)

        @classmethod
        @ModelView.button
        @Workflow.transition('quotation')
        def quote(cls, records):
            pool = Pool()
            Line = pool.get(prefix + '.line')
            removed = []
            for record in records:
                removed.extend(record.set_components())
            Line.delete(removed)
            cls.save(records)
            super().quote(records)

        def set_components(self):
            pool = Pool()
            Component = pool.get(prefix + '.line.component')
            removed = []
            lines = []
            sequence = 0
            for line in self.lines:
                if line.component_parent:
                    removed.append(line)
                    continue
                sequence += 1
                line.sequence = sequence
                lines.append(line)
                if line.product and line.product.components_used:
                    if line.product.type == 'kit':
                        components = []
                        for component in line.product.components_used:
                            components.append(line.get_component(component))
                        Component.set_price_ratio(components, line.quantity)
                        line.components = components
                    else:
                        for component in line.product.components_used:
                            order_line = line.get_component_order_line(
                                component)
                            sequence += 1
                            order_line.sequence = sequence
                            order_line.component_parent = line
                            lines.append(order_line)
            self.lines = lines
            return removed
    return OrderMixin


def order_line_mixin(prefix):
    class OrderLineMixin(ModelStorage, ModelView):

        component_parent = fields.Many2One(
            prefix + '.line', "Component Parent",
            readonly=True,
            states={
                'invisible': ~Eval('component_parent'),
                })
        component_children = fields.One2Many(
            prefix + '.line', 'component_parent', "Component Children",
            readonly=True,
            states={
                'invisible': ~Eval('component_children'),
                })

        components = fields.One2Many(
            prefix + '.line.component', 'line', "Components", readonly=True,
            states={
                'invisible': ~Eval('components', []),
                })

        @classmethod
        def view_attributes(cls):
            return super().view_attributes() + [
                ('//page[@id="components"]', 'states', {
                        'invisible': (
                            ~Eval('components', [])
                            & ~Eval('component_children', [])),
                        }),
                ]

        @classmethod
        def copy(cls, lines, default=None):
            if default is None:
                default = {}
            else:
                default = default.copy()
            default.setdefault('component_parent')
            default.setdefault('component_children')
            default.setdefault('components')
            return super().copy(lines, default=default)

        def get_component(self, component, **kwargs):
            pool = Pool()
            Component = pool.get(prefix + '.line.component')
            line = component.get_line(
                Component, self.quantity, self.unit, **kwargs)
            line.fixed = component.fixed
            if not line.fixed:
                line.quantity_ratio = component.quantity
            return line

        def get_component_order_line(self, component, **values):
            Line = self.__class__
            line = component.get_line(
                Line, self.quantity, self.unit, **values)
            line.type = 'line'
            line.on_change_product()
            return line

        def get_move(self, type_):
            move = super().get_move(type_)
            if self.components:
                move = None
            return move

        def get_moves_exception(self, name):
            exception = super().get_moves_exception(name)
            if self.components:
                exception = any(c.moves_exception for c in self.components)
            return exception

        def get_moves_progress(self, name):
            progress = super().get_moves_progress(name)
            if self.components:
                progress = 0.
                for component in self.components:
                    progress += component.moves_progress
                progress = round(progress / len(self.components), 4)
            return progress

        def _get_invoice_line_quantity(self):
            quantity = super()._get_invoice_line_quantity()
            if (getattr(self, prefix).invoice_method == 'shipment'
                    and self.components):
                ratio = min(c.get_moved_ratio() for c in self.components)
                quantity = self.unit.round(self.quantity * ratio)
            return quantity
    return OrderLineMixin


def order_line_component_mixin(prefix):
    class OrderLineComponentMixin(ModelStorage):

        line = fields.Many2One(
            prefix + '.line', "Line", required=True, ondelete='CASCADE',
            domain=[
                ('product.type', '=', 'kit'),
                ])
        moves = fields.One2Many('stock.move', 'origin', 'Moves', readonly=True)
        moves_ignored = fields.Many2Many(
            prefix + '.line.component-ignored-stock.move',
            'component', 'move', "Ignored Moves", readonly=True)
        moves_recreated = fields.Many2Many(
            prefix + '.line.component-recreated-stock.move',
            'component', 'move', "Recreated Moves", readonly=True)
        moves_exception = fields.Function(
            fields.Boolean('Moves Exception'), 'get_moves_exception')
        moves_progress = fields.Function(
            fields.Float("Moves Progress", digits=(1, 4)),
            'get_moves_progress')
        quantity_ratio = fields.Float(
            "Quantity Ratio", readonly=True,
            states={
                'required': ~Eval('fixed', False),
                })
        price_ratio = fields.Numeric(
            "Price Ratio", readonly=True, required=True)

        @classmethod
        def __setup__(cls):
            super().__setup__()
            cls.__access__.add('line')

        @fields.depends('line', '_parent_line.product')
        def on_change_with_parent_type(self, name=None):
            if self.line and self.line.product:
                return self.line.product.type

        @classmethod
        def set_price_ratio(cls, components, quantity):
            "Set price ratio between components"
            pool = Pool()
            Uom = pool.get('product.uom')
            list_prices = defaultdict(Decimal)
            sum_ = 0
            for component in components:
                product = component.product
                list_price = Uom.compute_price(
                    product.default_uom, product.list_price_used or 0,
                    component.unit)
                if component.fixed:
                    list_price *= Decimal(str(component.quantity / quantity))
                else:
                    list_price *= Decimal(str(component.quantity_ratio))
                list_prices[component] = list_price
                sum_ += list_price

            for component in components:
                if sum_:
                    ratio = list_prices[component] / sum_
                else:
                    ratio = Decimal(1) / len(components)
                if component.fixed:
                    ratio /= Decimal(str(component.quantity / quantity))
                else:
                    ratio /= Decimal(str(component.quantity_ratio))
                component.price_ratio = ratio

        def get_move(self, type_):
            raise NotImplementedError

        def _get_shipped_quantity(self, shipment_type):
            'Return the quantity already shipped'
            pool = Pool()
            Uom = pool.get('product.uom')

            quantity = 0
            skips = set(self.moves_recreated)
            for move in self.moves:
                if move not in skips:
                    quantity += Uom.compute_qty(
                        move.unit, move.quantity, self.unit)
            return quantity

        @property
        def _move_remaining_quantity(self):
            "Compute the remaining quantity to ship"
            pool = Pool()
            Uom = pool.get('product.uom')
            ignored = set(self.moves_ignored)
            quantity = abs(self.quantity)
            for move in self.moves:
                if move.state == 'done' or move in ignored:
                    quantity -= Uom.compute_qty(
                        move.unit, move.quantity, self.unit)
            return quantity

        def get_moves_exception(self, name):
            skips = set(self.moves_ignored)
            skips.update(self.moves_recreated)
            return any(
                m.state == 'cancelled' for m in self.moves if m not in skips)

        def get_moves_progress(self, name):
            progress = None
            quantity = self._move_remaining_quantity
            if quantity is not None and self.quantity:
                progress = round(
                    (abs(self.quantity) - quantity) / abs(self.quantity), 4)
                progress = max(0., min(1., progress))
            return progress

        def get_moved_ratio(self):
            pool = Pool()
            Uom = pool.get('product.uom')

            quantity = 0
            for move in self.moves:
                if move.state != 'done':
                    continue
                qty = Uom.compute_qty(move.unit, move.quantity, self.unit)
                dest_type = self.line.to_location.type
                if (move.to_location.type == dest_type
                        and move.from_location.type != dest_type):
                    quantity += qty
                elif (move.from_location.type == dest_type
                        and move.to_location.type != dest_type):
                    quantity -= qty
            if self.quantity < 0:
                quantity *= -1
            return quantity / self.quantity

        def get_rec_name(self, name):
            return super().get_rec_name(name) + (
                ' @ %s' % self.line.rec_name)

        @classmethod
        def search_rec_name(cls, name, clause):
            return super().search_rec_name(name, clause) + [
                ('line.rec_name',) + tuple(clause[1:]),
                ]
    return OrderLineComponentMixin


def handle_shipment_exception_mixin(prefix):
    class HandleShipmentExceptionMixin:
        def default_ask(self, fields):
            values = super().default_ask(fields)
            moves = values['domain_moves']
            for line in self.record.lines:
                for component in line.components:
                    skips = set(component.moves_ignored)
                    skips.update(component.moves_recreated)
                    for move in component.moves:
                        if move.state == 'cancelled' and move not in skips:
                            moves.append(move.id)
            return values

        def transition_handle(self):
            pool = Pool()
            Component = pool.get(prefix + '.line.component')

            result = super().transition_handle()

            to_write = []
            for line in self.record.lines:
                for component in line.components:
                    moves_ignored = []
                    moves_recreated = []
                    skips = set(component.moves_ignored)
                    skips.update(component.moves_recreated)
                    for move in component.moves:
                        if move not in self.ask.domain_moves or move in skips:
                            continue
                        if move in self.ask.recreate_moves:
                            moves_recreated.append(move.id)
                        else:
                            moves_ignored.append(move.id)

                    if moves_ignored or moves_recreated:
                        to_write.append([component])
                        to_write.append({
                                'moves_ignored': [('add', moves_ignored)],
                                'moves_recreated': [('add', moves_recreated)],
                                })
            if to_write:
                Component.write(*to_write)
            return result
    return HandleShipmentExceptionMixin


class AmendmentLineMixin:
    __slots__ = ()

    def _apply_line(self, record, line):
        pool = Pool()
        Uom = pool.get('product.uom')
        super()._apply_line(record, line)
        if line.components:
            quantity = Uom.compute_qty(
                line.unit, line.quantity,
                line.product.default_uom, round=False)
            for component in line.components:
                if not component.fixed:
                    component.quantity = component.unit.round(
                        quantity * component.quantity_ratio)
            line.components = line.components