feat: a Tiny order can sell UV as the client's one SKU per 10 cm
All checks were successful
Build and deploy / Validate source (push) Successful in 15s
Build and deploy / Integration suite on a real stack (push) Successful in 3m26s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m7s

Dropstar sells UV only as DTFIMP.UV.010CM, per 10 cm. TINY_SKU10_<MODE>
names such a SKU: 2,30 m goes to Tiny as 23 of it at the price the customer
paid, with a rest that does not divide on a line of its own so the order
adds up to the cent. "Testar conexão" and tiny_probe conferir check it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-05 15:27:45 -03:00
parent 43d9a3c637
commit 172544b173
7 changed files with 92 additions and 7 deletions

View File

@@ -49,6 +49,10 @@ PRODUCT_SETTINGS = {'file': 'TINY_PRODUCT_TEXTIL_FOLHA', 'avulsa': 'TINY_PRODUCT
# the mode's product id.
SKU_SETTINGS = {'file': 'TINY_SKU_TEXTIL_FOLHA', 'avulsa': 'TINY_SKU_TEXTIL_AVULSA',
'uvfile': 'TINY_SKU_UV_FOLHA', 'uv': 'TINY_SKU_UV_AVULSA'}
# Or a single SKU per 10 cm, as the client sells UV ("DTFIMP.UV.010CM"):
# 2,30 m is 23 of it.
STEP_SETTINGS = {'file': 'TINY_SKU10_TEXTIL_FOLHA', 'avulsa': 'TINY_SKU10_TEXTIL_AVULSA',
'uvfile': 'TINY_SKU10_UV_FOLHA', 'uv': 'TINY_SKU10_UV_AVULSA'}
PRODUCTS = {'file': 'DTF Têxtil 57 cm · folha montada',
'avulsa': 'DTF Têxtil 57 cm · artes avulsas',
'uvfile': 'DTF UV 28,5 cm · folha montada',
@@ -101,18 +105,48 @@ def configured():
def required_settings():
"""The OAuth application, and a product id for each mode not sold by SKU."""
return ['TINY_CLIENT_ID', 'TINY_CLIENT_SECRET', 'TINY_REDIRECT_URI'] + [
setting for mode, setting in PRODUCT_SETTINGS.items() if not sku_prefix(mode)]
setting for mode, setting in PRODUCT_SETTINGS.items() if not (sku_prefix(mode) or step_sku(mode))]
def sku_prefix(mode):
return os.environ.get(SKU_SETTINGS[mode], '').strip()
def step_sku(mode):
return os.environ.get(STEP_SETTINGS[mode], '').strip()
def mode_codes(mode):
"""Every SKU code a mode sold by SKU uses, or [] for a mode sold by product id."""
if step_sku(mode):
return [step_sku(mode)]
return sku_codes(sku_prefix(mode)) if sku_prefix(mode) else []
def sku_codes(prefix):
"""The metre SKU and the nine of 10 to 90 cm, as the client's catalogue names them."""
return [f'{prefix}.100CM'] + [f'{prefix}.{cm:04d}CM' for cm in range(10, 100, 10)]
def tenths(item):
"""The billed length in 10 cm steps; the Site bills in them."""
billed = Decimal(item['billed_metres'])
steps = billed * 10
if steps != steps.to_integral_value():
raise TinyError(f'{billed} m is not a whole number of 10 cm steps')
return int(steps), int(billed * int(item['unit_cents']))
def step_lines(item, code):
"""(code, quantity, cents each) for a single 10 cm SKU, adding up to the
item's total; a rest that does not divide goes on one line of its own."""
steps, total = tenths(item)
each, rest = divmod(total, steps)
if not rest:
return [(code, steps, each)]
return [(code, steps - 1, each), (code, 1, each + rest)] if steps > 1 else [(code, 1, total)]
def sku_lines(item, prefix):
"""(code, quantity, cents each) adding up to the item: whole metres as the
metre SKU, the rest as one 10 cm SKU. The Site bills in 10 cm steps, so
@@ -316,11 +350,12 @@ def order_payload(payload, contact_id, today=None, product_id=None):
items = []
for item in order['items']:
note = PRODUCTS[item['mode']] + f" · nota {item['grade']}"
prefix = sku_prefix(item['mode'])
if prefix:
prefix, step = sku_prefix(item['mode']), step_sku(item['mode'])
if step or prefix:
if product_id is None:
raise TinyError(f"{SKU_SETTINGS[item['mode']]} needs the SKUs looked up in Tiny")
for index, (code, quantity, cents) in enumerate(sku_lines(item, prefix)):
lines = step_lines(item, step) if step else sku_lines(item, prefix)
for index, (code, quantity, cents) in enumerate(lines):
line = {'produto': {'id': product_id(code)}, 'quantidade': quantity, 'valorUnitario': money(cents)}
if index == 0:
line['infoAdicional'] = note + f" · {item['billed_metres']} m"
@@ -372,7 +407,7 @@ def configured_product(orders, mode):
def configured_skus(orders, mode):
"""Whether every SKU of a mode sold by SKU is an active product in Tiny."""
missing = []
for code in sku_codes(sku_prefix(mode)):
for code in mode_codes(mode):
try:
orders.product_id(code)
except TinyError:
@@ -400,7 +435,7 @@ def check(auth=None, transport=None, orders=None):
return 'ok'
reads = [('pedidos', lambda: listed('/pedidos')), ('contatos', lambda: listed('/contatos'))]
reads += [(PRODUCTS[mode], lambda mode=mode: configured_skus(orders, mode) if sku_prefix(mode)
reads += [(PRODUCTS[mode], lambda mode=mode: configured_skus(orders, mode) if mode_codes(mode)
else configured_product(orders, mode)[1]) for mode in PRODUCT_SETTINGS]
reads.append(('forma de envio de retirada', lambda: configured_pickup(orders)[1]))
results = {}

View File

@@ -67,7 +67,12 @@ def formas_envio(orders, termo, out):
def conferir(orders, out):
ok = True
for mode, setting in tiny.PRODUCT_SETTINGS.items():
prefix = tiny.sku_prefix(mode)
prefix, step = tiny.sku_prefix(mode), tiny.step_sku(mode)
if step:
result = tiny.configured_skus(orders, mode)
ok &= result == 'ok'
out(f'{tiny.STEP_SETTINGS[mode]} ({tiny.PRODUCTS[mode]}): {result} ({step} a cada 10 cm)')
continue
if prefix:
result = tiny.configured_skus(orders, mode)
ok &= result == 'ok'