From a23e88ff2f17d6d3c580946bdcc7f830d4e5964e Mon Sep 17 00:00:00 2001 From: Marcus Lindvall Date: Fri, 6 Sep 2019 11:21:42 +0200 Subject: [PATCH] Added 'WholeSaleAmount' to Article, and refactored article internal functions --- pyjeeves/models/raw.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/pyjeeves/models/raw.py b/pyjeeves/models/raw.py index 33675d1..177297f 100644 --- a/pyjeeves/models/raw.py +++ b/pyjeeves/models/raw.py @@ -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) )