2023-01-12 22:37:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# This file is part of the cashbook-module from m-ds.de for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains the
|
|
|
|
# full copyright notices and license terms.
|
|
|
|
|
|
|
|
from trytond.model import fields
|
|
|
|
from trytond.pyson import Eval, Bool, Or
|
|
|
|
from trytond.pool import Pool
|
2023-01-14 23:36:02 +00:00
|
|
|
from trytond.exceptions import UserError
|
|
|
|
from trytond.i18n import gettext
|
2023-01-12 22:37:20 +00:00
|
|
|
from trytond.modules.product.uom import uom_conversion_digits
|
|
|
|
from trytond.modules.cashbook.mixin import STATES, DEPENDS
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
STATESQ = {}
|
|
|
|
STATESQ.update(STATES)
|
|
|
|
DEPENDSQ = []
|
|
|
|
DEPENDSQ.extend(DEPENDS)
|
|
|
|
|
|
|
|
|
|
|
|
class SecondUomMixin(object):
|
|
|
|
""" two fields for second uom: quantity, rate
|
|
|
|
"""
|
2023-06-08 14:00:36 +00:00
|
|
|
__slots__ = ()
|
|
|
|
|
2023-06-08 12:00:59 +00:00
|
|
|
quantity_2nd_uom = fields.Numeric(
|
|
|
|
string='Quantity Second UOM',
|
2023-01-12 22:37:20 +00:00
|
|
|
digits=(16, Eval('quantity2nd_digits', 4)),
|
|
|
|
states={
|
|
|
|
'readonly': Or(
|
2023-12-03 16:31:42 +00:00
|
|
|
STATESQ['readonly'],
|
|
|
|
~Bool(Eval('quantity2nd'))),
|
2023-01-12 22:37:20 +00:00
|
|
|
'required': Bool(Eval('quantity2nd')),
|
2023-12-03 16:31:42 +00:00
|
|
|
'invisible': ~Bool(Eval('quantity2nd'))},
|
|
|
|
depends=DEPENDSQ+['quantity2nd_digits', 'quantity2nd'])
|
2023-06-08 12:00:59 +00:00
|
|
|
factor_2nd_uom = fields.Function(fields.Numeric(
|
|
|
|
string='Conversion factor',
|
|
|
|
help='Conversion factor between the units of the ' +
|
|
|
|
'participating cash books.',
|
2023-01-13 09:03:57 +00:00
|
|
|
digits=uom_conversion_digits,
|
2023-01-12 22:37:20 +00:00
|
|
|
states={
|
|
|
|
'readonly': Or(
|
2023-12-03 16:31:42 +00:00
|
|
|
STATESQ['readonly'],
|
|
|
|
~Bool(Eval('quantity2nd'))),
|
2023-01-12 22:37:20 +00:00
|
|
|
'required': Bool(Eval('quantity2nd')),
|
2023-12-03 16:31:42 +00:00
|
|
|
'invisible': ~Bool(Eval('quantity2nd'))},
|
|
|
|
depends=DEPENDSQ+['quantity2nd_digits', 'quantity2nd']),
|
2023-01-14 23:36:02 +00:00
|
|
|
'on_change_with_factor_2nd_uom', setter='set_factor_2nd_uom')
|
2023-01-12 22:37:20 +00:00
|
|
|
|
2023-06-08 12:00:59 +00:00
|
|
|
quantity2nd = fields.Function(fields.Many2One(
|
|
|
|
model_name='product.uom',
|
2023-01-12 22:37:20 +00:00
|
|
|
string="2nd UOM", readonly=True), 'on_change_with_quantity2nd')
|
2023-06-08 12:00:59 +00:00
|
|
|
quantity2nd_digits = fields.Function(fields.Integer(
|
|
|
|
string='2nd UOM Digits', readonly=True),
|
|
|
|
'on_change_with_quantity2nd_digits')
|
2023-01-12 22:37:20 +00:00
|
|
|
|
2023-01-14 23:36:02 +00:00
|
|
|
def quantize_quantity(self, value):
|
|
|
|
""" quantize for line-quantity
|
|
|
|
"""
|
2023-06-08 12:00:59 +00:00
|
|
|
return Decimal(value).quantize(
|
|
|
|
Decimal(Decimal(1) / 10 ** self.quantity_digits))
|
2023-01-14 23:36:02 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_2nd_quantity(cls, values, from_uom):
|
|
|
|
""" add second uom quantity if missing
|
|
|
|
"""
|
|
|
|
pool = Pool()
|
|
|
|
UOM = pool.get('product.uom')
|
|
|
|
Cashbook = pool.get('cashbook.book')
|
|
|
|
|
|
|
|
booktransf = values.get('booktransf', None)
|
|
|
|
quantity = values.get('quantity', None)
|
|
|
|
quantity_2nd_uom = values.get('quantity_2nd_uom', None)
|
|
|
|
|
|
|
|
if (quantity is not None) and (booktransf is not None) and \
|
2023-06-08 12:00:59 +00:00
|
|
|
(from_uom is not None):
|
2023-01-14 23:36:02 +00:00
|
|
|
if quantity_2nd_uom is None:
|
|
|
|
booktransf = Cashbook(booktransf)
|
|
|
|
if booktransf.quantity_uom:
|
|
|
|
if from_uom.id != booktransf.quantity_uom.id:
|
|
|
|
# deny impossible transfer
|
2023-06-08 12:00:59 +00:00
|
|
|
if from_uom.category.id != \
|
|
|
|
booktransf.quantity_uom.category.id:
|
2023-01-14 23:36:02 +00:00
|
|
|
raise UserError(gettext(
|
|
|
|
'cashbook_investment.msg_uomcat_mismatch',
|
2023-06-08 12:00:59 +00:00
|
|
|
cat1=from_uom.category.rec_name,
|
2023-12-03 16:31:42 +00:00
|
|
|
cat2=booktransf.quantity_uom.category.rec_name))
|
2023-01-14 23:36:02 +00:00
|
|
|
|
|
|
|
values['quantity_2nd_uom'] = Decimal(UOM.compute_qty(
|
2023-01-15 22:06:47 +00:00
|
|
|
from_uom,
|
|
|
|
float(quantity),
|
|
|
|
booktransf.quantity_uom,
|
|
|
|
round=False,
|
2023-01-14 23:36:02 +00:00
|
|
|
)).quantize(Decimal(
|
2023-06-08 12:00:59 +00:00
|
|
|
Decimal(1) /
|
2023-12-03 16:31:42 +00:00
|
|
|
10 ** booktransf.quantity_digits))
|
2023-01-14 23:36:02 +00:00
|
|
|
return values
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_factor_2nd_uom(cls, lines, name, value):
|
|
|
|
""" compute quantity_2nd_uom, write to db
|
|
|
|
"""
|
|
|
|
Line2 = Pool().get(cls.__name__)
|
|
|
|
|
|
|
|
to_write = []
|
|
|
|
|
|
|
|
if name != 'factor_2nd_uom':
|
|
|
|
return
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if line.booktransf is None:
|
|
|
|
continue
|
2023-01-15 10:03:33 +00:00
|
|
|
if (line.cashbook.quantity_uom is None) or \
|
2023-06-08 12:00:59 +00:00
|
|
|
(line.booktransf.quantity_uom is None):
|
2023-01-15 10:03:33 +00:00
|
|
|
continue
|
2023-01-14 23:36:02 +00:00
|
|
|
|
|
|
|
if line.cashbook.quantity_uom.id == line.booktransf.quantity_uom.id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
to_write.extend([
|
|
|
|
[line],
|
|
|
|
{
|
|
|
|
'quantity_2nd_uom': line.quantize_quantity(
|
|
|
|
line.booktransf.quantity_uom.round(
|
|
|
|
float(line.quantity * value))
|
|
|
|
),
|
|
|
|
}])
|
|
|
|
|
|
|
|
if len(to_write) > 0:
|
|
|
|
Line2.write(*to_write)
|
|
|
|
|
2023-06-08 12:00:59 +00:00
|
|
|
@fields.depends(
|
|
|
|
'booktransf', '_parent_booktransf.quantity_uom',
|
|
|
|
'quantity_uom', 'quantity_digits', 'quantity',
|
2023-01-14 23:36:02 +00:00
|
|
|
'quantity_2nd_uom', 'factor_2nd_uom')
|
|
|
|
def on_change_booktransf(self):
|
|
|
|
""" update quantity_2nd_uom
|
|
|
|
"""
|
|
|
|
self.on_change_factor_2nd_uom()
|
|
|
|
|
2023-06-08 12:00:59 +00:00
|
|
|
@fields.depends(
|
|
|
|
'booktransf', '_parent_booktransf.quantity_uom',
|
|
|
|
'quantity_uom', 'quantity_digits', 'quantity',
|
2023-01-14 23:36:02 +00:00
|
|
|
'quantity_2nd_uom', 'factor_2nd_uom')
|
|
|
|
def on_change_quantity(self):
|
|
|
|
""" update quantity_2nd_uom
|
|
|
|
"""
|
|
|
|
self.on_change_factor_2nd_uom()
|
|
|
|
|
2023-06-08 12:00:59 +00:00
|
|
|
@fields.depends(
|
|
|
|
'booktransf', '_parent_booktransf.quantity_uom',
|
|
|
|
'quantity_uom', 'quantity_digits', 'quantity',
|
2023-01-14 23:36:02 +00:00
|
|
|
'quantity_2nd_uom', 'factor_2nd_uom')
|
|
|
|
def on_change_factor_2nd_uom(self):
|
|
|
|
""" update quantity_2nd_uom + factor_2nd_uom
|
|
|
|
"""
|
|
|
|
UOM = Pool().get('product.uom')
|
|
|
|
|
|
|
|
if (self.quantity is None) or (self.booktransf is None):
|
|
|
|
self.quantity_2nd_uom = None
|
|
|
|
self.factor_2nd_uom = None
|
|
|
|
return
|
|
|
|
if (self.booktransf.quantity_uom is None) or \
|
2023-06-08 12:00:59 +00:00
|
|
|
(self.quantity_uom is None):
|
2023-01-14 23:36:02 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if self.factor_2nd_uom is None:
|
|
|
|
# no factor set, use factor of target-uom
|
|
|
|
self.quantity_2nd_uom = self.quantize_quantity(
|
|
|
|
UOM.compute_qty(
|
|
|
|
self.quantity_uom,
|
|
|
|
float(self.quantity),
|
|
|
|
self.booktransf.quantity_uom,
|
2023-12-03 16:31:42 +00:00
|
|
|
round=False))
|
2023-01-14 23:36:02 +00:00
|
|
|
if self.quantity != Decimal('0.0'):
|
|
|
|
self.factor_2nd_uom = (
|
|
|
|
self.quantity_2nd_uom / self.quantity
|
2023-06-08 12:00:59 +00:00
|
|
|
).quantize(Decimal(
|
|
|
|
Decimal(1) / 10 ** uom_conversion_digits[1]))
|
|
|
|
else:
|
2023-01-14 23:36:02 +00:00
|
|
|
self.quantity_2nd_uom = self.quantize_quantity(
|
|
|
|
self.quantity * self.factor_2nd_uom)
|
|
|
|
|
|
|
|
@fields.depends('quantity', 'quantity_2nd_uom', 'factor_2nd_uom')
|
|
|
|
def on_change_quantity_2nd_uom(self):
|
|
|
|
""" update factor_2nd_uom by quantity
|
|
|
|
"""
|
|
|
|
self.factor_2nd_uom = self.on_change_with_factor_2nd_uom()
|
|
|
|
|
|
|
|
@fields.depends('quantity', 'quantity_2nd_uom')
|
2023-01-12 22:37:20 +00:00
|
|
|
def on_change_with_factor_2nd_uom(self, name=None):
|
|
|
|
""" get factor from uom
|
|
|
|
"""
|
2023-01-14 23:36:02 +00:00
|
|
|
if (self.quantity is not None) and (self.quantity_2nd_uom is not None):
|
2023-01-12 22:37:20 +00:00
|
|
|
if self.quantity != Decimal('0.0'):
|
2023-01-14 23:36:02 +00:00
|
|
|
exp = Decimal(Decimal(1) / 10 ** uom_conversion_digits[1])
|
|
|
|
return (self.quantity_2nd_uom / self.quantity).quantize(exp)
|
2023-01-12 22:37:20 +00:00
|
|
|
|
2023-06-08 12:00:59 +00:00
|
|
|
@fields.depends(
|
|
|
|
'booktransf', '_parent_booktransf.quantity_uom', 'quantity_uom')
|
2023-01-12 22:37:20 +00:00
|
|
|
def on_change_with_quantity2nd(self, name=None):
|
|
|
|
""" uom of transfer-target
|
|
|
|
"""
|
|
|
|
if self.booktransf:
|
|
|
|
if self.quantity_uom:
|
|
|
|
if self.booktransf.quantity_uom:
|
|
|
|
if self.quantity_uom.id != \
|
2023-06-08 12:00:59 +00:00
|
|
|
self.booktransf.quantity_uom.id:
|
2023-01-12 22:37:20 +00:00
|
|
|
return self.booktransf.quantity_uom.id
|
|
|
|
|
|
|
|
@fields.depends('booktransf', '_parent_booktransf.quantity_digits')
|
|
|
|
def on_change_with_quantity2nd_digits(self, name=None):
|
|
|
|
""" uom of transfer-target
|
|
|
|
"""
|
|
|
|
if self.booktransf:
|
|
|
|
return self.booktransf.quantity_digits
|
|
|
|
else:
|
|
|
|
return 2
|
|
|
|
|
|
|
|
# end SecondUomMixin
|