File: account.py

package info (click to toggle)
tryton-modules-account-stock-shipment-cost-weight 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 328 kB
  • sloc: python: 233; makefile: 11; sh: 3
file content (36 lines) | stat: -rw-r--r-- 1,246 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
# 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 decimal import Decimal

from trytond.pool import PoolMeta


class ShipmentCost(metaclass=PoolMeta):
    __name__ = 'account.shipment_cost'

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls.allocation_method.selection.append(('weight', "By Weight"))

    def allocate_cost_by_weight(self):
        self.factors = self._get_factors('weight')
        self._allocate_cost(self.factors)

    def unallocate_cost_by_weight(self):
        factors = self.factors or self._get_factors('weight')
        self._allocate_cost(factors, sign=-1)

    def _get_weight_factors(self):
        "Return the factor for each shipment based on weight"
        shipments = self.all_shipments
        sum_weight = sum(Decimal(s.weight or 0) for s in shipments)
        length = Decimal(len(shipments))
        factors = {}
        for shipment in shipments:
            if not sum_weight:
                factors[str(shipment)] = 1 / length
            else:
                factors[str(shipment)] = (
                    Decimal(shipment.weight or 0) / sum_weight)
        return factors