Files

41 lines
1.3 KiB
Python
Raw Permalink Normal View History

2015-08-24 17:24:43 +02:00
# 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
2017-01-31 16:37:34 +01:00
from trytond.pool import PoolMeta
2015-08-24 17:24:43 +02:00
2018-06-26 19:11:44 +02:00
class LandedCost(metaclass=PoolMeta):
2015-08-24 17:24:43 +02:00
__name__ = 'account.landed_cost'
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
2015-08-24 17:24:43 +02:00
cls.allocation_method.selection.append(('weight', 'By Weight'))
def allocate_cost_by_weight(self):
self.factors = self._get_factors('weight')
2021-12-16 22:31:08 +01:00
self._allocate_cost(self.factors)
2015-08-24 17:24:43 +02:00
2020-10-11 13:37:32 +02:00
def unallocate_cost_by_weight(self):
factors = self.factors or self._get_factors('weight')
2021-12-16 22:31:08 +01:00
self._allocate_cost(factors, sign=-1)
2020-10-11 13:37:32 +02:00
2015-08-24 17:24:43 +02:00
def _get_weight_factors(self):
"Return the factor for each move based on weight"
moves = self.stock_moves()
2015-08-24 17:24:43 +02:00
sum_weight = Decimal(0)
weights = {}
for move in moves:
2017-01-31 16:37:34 +01:00
weight = Decimal(str(move.internal_weight or 0))
2015-08-24 17:24:43 +02:00
weights[move.id] = weight
sum_weight += weight
factors = {}
length = Decimal(len(moves))
for move in moves:
if not sum_weight:
2021-12-16 22:31:08 +01:00
factors[str(move.id)] = 1 / length
2015-08-24 17:24:43 +02:00
else:
2021-12-16 22:31:08 +01:00
factors[str(move.id)] = weights[move.id] / sum_weight
2015-08-24 17:24:43 +02:00
return factors