wpi: Add shrink_contents_if_needed() method

This method can be used to shrink the number of lines on the customs
declaration, which unfortunately only supports 5 lines in the
WaPoInt API.
This commit is contained in:
Harald Welte 2021-03-07 14:32:18 +01:00
parent db0a0fb7ae
commit ebb0fce5d0
1 changed files with 34 additions and 0 deletions

View File

@ -214,6 +214,40 @@ class WarenpostInt(object):
ret['contentPieceOrigin'] = origin_cc
return ret
def shrink_contents_if_needed(self, contents):
"""Attempt to shrink the number of content lines below the permitted 5. We
intentionally ignore the country of origin and group all lines by the
HTS code. All lines sharing the same HTS code are merged to one,
hopefully this is sufficient to get the count below 5."""
if len(contents) < 5:
return contents
# group items by HTS code
by_hts = {}
for c in contents:
hts = c['contentPieceHsCode'].strip()
if hts in by_hts:
by_hts[hts].append(c)
else:
by_hts[hts] = [c]
if len(by_hts.keys()) > 5:
raise ValueError('More than 5 distinct HTS numbers(%u); cannot merge' % (len(by_hts.keys())))
out = []
# generate one aggregate item per HTS code
for k in by_hts:
total_grams = 0
total_value = 0
total_qty = 0
for c in by_hts[k]:
total_grams += c['contentPieceNetweight']
total_value += float(c['contentPieceValue'])
total_qty += c['contentPieceAmount']
aggregate = by_hts[k][0]
aggregate['contentPieceAmount'] = total_qty
aggregate['contentPieceValue'] = "%.2f" % (total_value)
aggregate['contentPieceNetweight'] = total_grams
out.append(aggregate)
return out
def build_item(self, product, sender, recipient, weight_grams, amount=0, currency='EUR',
shipment_nature='SALE_GOODS', customer_reference=None, contents=None):
"""Build an 'item' in the language of the WaPoInt API. Represents one shipment."""