diff --git a/app/tiny.py b/app/tiny.py index 9530e5c..a72037f 100644 --- a/app/tiny.py +++ b/app/tiny.py @@ -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 = {} diff --git a/app/tiny_probe.py b/app/tiny_probe.py index 4463ea1..4349d3e 100644 --- a/app/tiny_probe.py +++ b/app/tiny_probe.py @@ -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' diff --git a/compose.local.yaml b/compose.local.yaml index eaab0d5..66c0382 100644 --- a/compose.local.yaml +++ b/compose.local.yaml @@ -62,6 +62,10 @@ x-app: &app TINY_SKU_TEXTIL_AVULSA: ${TINY_SKU_TEXTIL_AVULSA:-} TINY_SKU_UV_FOLHA: ${TINY_SKU_UV_FOLHA:-} TINY_SKU_UV_AVULSA: ${TINY_SKU_UV_AVULSA:-} + TINY_SKU10_TEXTIL_FOLHA: ${TINY_SKU10_TEXTIL_FOLHA:-} + TINY_SKU10_TEXTIL_AVULSA: ${TINY_SKU10_TEXTIL_AVULSA:-} + TINY_SKU10_UV_FOLHA: ${TINY_SKU10_UV_FOLHA:-} + TINY_SKU10_UV_AVULSA: ${TINY_SKU10_UV_AVULSA:-} TINY_ECOMMERCE_ID: ${TINY_ECOMMERCE_ID:-} WHATSAPP_ADAPTER: fake STORAGE_ADAPTER: s3-local diff --git a/docker-compose.yml b/docker-compose.yml index 86b5241..fcb4970 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,6 +76,10 @@ x-app-environment: &app-environment TINY_SKU_TEXTIL_AVULSA: ${TINY_SKU_TEXTIL_AVULSA:-} TINY_SKU_UV_FOLHA: ${TINY_SKU_UV_FOLHA:-} TINY_SKU_UV_AVULSA: ${TINY_SKU_UV_AVULSA:-} + TINY_SKU10_TEXTIL_FOLHA: ${TINY_SKU10_TEXTIL_FOLHA:-} + TINY_SKU10_TEXTIL_AVULSA: ${TINY_SKU10_TEXTIL_AVULSA:-} + TINY_SKU10_UV_FOLHA: ${TINY_SKU10_UV_FOLHA:-} + TINY_SKU10_UV_AVULSA: ${TINY_SKU10_UV_AVULSA:-} # The client's "retirar pessoalmente" forma de envio id, on pickup orders. TINY_FORMA_ENVIO_RETIRADA: ${TINY_FORMA_ENVIO_RETIRADA:-} # Sets "Aprovada" on paid orders and "Pronto para envio" on finished pickup diff --git a/docs/LOCAL_SETUP.md b/docs/LOCAL_SETUP.md index a132e2d..f97fd76 100644 --- a/docs/LOCAL_SETUP.md +++ b/docs/LOCAL_SETUP.md @@ -314,6 +314,8 @@ TINY_SKU_TEXTIL_FOLHA=DTFIMP.TX # optional, instead of the product id: TINY_SKU_TEXTIL_AVULSA= # same, for the other modes, once their SKUs are confirmed TINY_SKU_UV_FOLHA= TINY_SKU_UV_AVULSA= +TINY_SKU10_UV_FOLHA=DTFIMP.UV.010CM # or one SKU per 10 cm: 2,30 m = 23 of it (the client's UV) +TINY_SKU10_UV_AVULSA=DTFIMP.UV.010CM ``` With the client ID and secret set, the Kanban header shows **Conectar Tiny**. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index dc4158a..0e1a384 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -365,6 +365,9 @@ From the report already sent. These are dated promises, not backlog. `DTFIMP.UV.010CM` seen) and artes avulsas stay on a product id per metre until their SKUs are confirmed. Fake transport only; whether v3's `/produtos` filters by `codigo` is checked by `conferir` on the account. + **UV (2026-10-05):** the client sells UV only as `DTFIMP.UV.010CM`, per 10 cm. + `TINY_SKU10_` names such a SKU: 2,30 m goes as 23 of it, at the price + paid (a rest that does not divide gets a line of its own). **Customer notices through Tiny (2026-09-25, Week 3 head start):** the client already sends WhatsApp notices from Tiny's order situação (Tiny webhook -> the `api-tiny-n8n` middleware, which reads the order through API v2 -> n8n diff --git a/tests/test_tiny.py b/tests/test_tiny.py index 2751c81..e601408 100644 --- a/tests/test_tiny.py +++ b/tests/test_tiny.py @@ -304,6 +304,38 @@ class TinySkuTests(FakeTinyCase): self.assertEqual(self.orders, []) +UV = {**PAID, 'order': {**PAID['order'], 'items': [ + {'mode': 'uvfile', 'grade': 100, 'billed_metres': '2.3', 'unit_cents': 6990, 'total_cents': 16077}]}} + + +@mock.patch.dict(os.environ, {**PRODUCTS, 'TINY_SKU10_UV_FOLHA': 'DTFIMP.UV.010CM'}) +class TinyStepSkuTests(FakeTinyCase): + """UV sold as one SKU per 10 cm, the only UV SKU the client has.""" + + def setUp(self): + super().setUp() + self.products['300'] = {'id': 300, 'sku': 'DTFIMP.UV.010CM', 'descricao': 'UV 10 cm', 'situacao': 'A'} + + def test_the_length_in_ten_centimetre_steps_at_the_price_paid(self): + self.tiny.deliver('k1', UV) + lines = [(i['produto']['id'], i['quantidade'], i['valorUnitario']) for i in self.orders[0]['itens']] + self.assertEqual(lines, [(300, 23, 6.99)]) # 2,30 m at R$ 69,90 = R$ 160,77 + + def test_a_rest_that_does_not_divide_gets_its_own_line(self): + from app.tiny import step_lines + lines = step_lines({'billed_metres': '1.3', 'unit_cents': 1495}, 'C') + self.assertEqual(lines, [('C', 12, 149), ('C', 1, 155)]) + self.assertEqual(sum(q * c for _, q, c in lines), 1943) + + def test_settings_and_check_know_the_step_sku(self): + from app.tiny import check, required_settings + self.assertNotIn('TINY_PRODUCT_UV_FOLHA', required_settings()) + self.assertEqual(check(orders=self.tiny)['DTF UV 28,5 cm · folha montada'], 'ok') + del self.products['300'] + self.tiny.skus.clear() + self.assertIn('DTFIMP.UV.010CM', check(orders=self.tiny)['DTF UV 28,5 cm · folha montada']) + + @mock.patch.dict(os.environ, PRODUCTS) class TinyProbeTests(FakeTinyCase): """The supervised console tool run against the fake Tiny."""