File: sale.py

package info (click to toggle)
tryton-modules-carrier-weight 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 376 kB
  • sloc: python: 500; xml: 48; makefile: 11; sh: 3
file content (40 lines) | stat: -rw-r--r-- 1,276 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
# 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 functools import partial
from itertools import groupby

from trytond.pool import PoolMeta
from trytond.tools import sortable_values

from .common import parcel_weight


class Sale(metaclass=PoolMeta):
    __name__ = 'sale.sale'

    def _group_parcel_key(self, lines, line):
        """
        The key to group lines by parcel
        """
        return ()

    def _parcel_weight(self, parcel):
        if self.carrier:
            return parcel_weight(parcel, self.carrier.weight_uom, 'unit')

    def _get_carrier_context(self, carrier):
        context = super(Sale, self)._get_carrier_context(carrier)

        if not carrier or carrier.carrier_cost_method != 'weight':
            return context
        context = context.copy()
        weights = []
        context['weights'] = weights

        lines = [l for l in self.lines or [] if l.quantity and l.quantity > 0]
        keyfunc = partial(self._group_parcel_key, lines)
        lines = sorted(lines, key=sortable_values(keyfunc))

        for key, parcel in groupby(lines, key=keyfunc):
            weights.append(self._parcel_weight(parcel))
        return context