Added 'WholeSaleAmount' to Article, and refactored article internal functions

This commit is contained in:
Marcus Lindvall 2019-09-06 11:21:42 +02:00
parent cf7ed09049
commit a23e88ff2f

View file

@ -116,6 +116,7 @@ class Article(RawBaseModel):
'UnitListPrice',
# 'Extra1',
'WholeSaleUnit',
'WholeSaleAmount',
'ListPrice',
'Balance')
@ -130,33 +131,44 @@ class Article(RawBaseModel):
ArticleClass = relationship(ArticleClass, lazy='joined')
ArticleBalance = relationship(ArticleBalance)
ArticleUnit = relationship(ArticleUnit)
ArticleUnit = relationship(ArticleUnit, uselist=True)
def _get_alt_unit(self):
# Find matching alternative unit for amount per piece, or return default for the article.
spec_conv = self.ArtFsgForp if self.ArtFsgForp else None
def _get_standard_alt_unit(self):
for unit in self.ArticleUnit:
if unit.AltEnhetOrderStd == "1":
if unit.AltEnhetOrderStd == "1" and not spec_conv:
return unit
elif (spec_conv and (unit.AltEnhetOmrFaktor == spec_conv or
unit.ArticleAlternativeUnit.AltEnhetOmrFaktor == spec_conv)):
return unit
def get_unit_conv(self):
# Return conversion factor for the article's alternative unit
if self.ArtFsgForp:
return self.ArtFsgForp
unit = self._get_standard_alt_unit()
if unit and unit.AltEnhetOmrFaktor:
return unit.AltEnhetOmrFaktor
elif unit:
return unit.ArticleAlternativeUnit.AltEnhetOmrFaktor
unit = self._get_alt_unit()
if unit:
return (unit.AltEnhetOmrFaktor if unit.AltEnhetOmrFaktor
else unit.ArticleAlternativeUnit.AltEnhetOmrFaktor)
return 1
@hybrid_property
def WholeSaleUnit(self):
if self.Extra1:
# Description of alternative unit, or Extra1 if no alternative unit is in use.
unit = self._get_alt_unit()
if unit:
return unit.ArticleAlternativeUnit.AltEnhetBeskr
else:
return self.Extra1
unit = self._get_standard_alt_unit()
if unit and not self.ArtFsgForp:
return unit.ArticleAlternativeUnit.AltEnhetBeskr
@hybrid_property
def WholeSaleAmount(self):
# Amount of units in the alternative unit of the article, or 1 if none exist.
return self.get_unit_conv()
@hybrid_property
def ListPrice(self):
@ -176,7 +188,7 @@ class Article(RawBaseModel):
def _base_filters(self, obj):
return RawBaseModel._base_filters(
obj,
and_(obj.LagTyp == 0)
and_(obj.ItemStatusCode == 0)
)