From b3ecba590bcd2def9686ff7ceed19f7d85eabbd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Wed, 18 Nov 2026 12:24:21 -0300 Subject: [PATCH] fix: customer credit reaches Tiny as CREDITO PRE-PAGO, with its own marcador beside SITE DTF (#42) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Marcus, 2026-11-18: "Pago fora do site › Crédito do cliente" maps to the forma de recebimento CREDITO PRE-PAGO (987762591), and the order also gets the marcador CREDITO PRE-PAGO. CORREÇÃO DTF, refused by Tiny for #42, is for correction orders only. A retry adds only the marcadores the order is missing. The outbox computes the forma when it delivers, so #42's queued sale goes through on its next retry after the deploy. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/tiny.py | 37 +++++++++++++++++++++++++------------ tests/test_tiny.py | 19 ++++++++++++++++--- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/tiny.py b/app/tiny.py index 4312597..3ce5d97 100644 --- a/app/tiny.py +++ b/app/tiny.py @@ -137,13 +137,14 @@ PICKUP_EXACT = 'retirar pessoalmente' # Dropstar's own ids, read from its store orders with app.tiny_probe exemplo # (2026-10-19). The app may not list formas de recebimento, so these are not # looked up: Pix and credit card, as the store's orders are paid, and the -# DropStar carrier its pickup orders name. Dinheiro and, for the customer's -# credit, Dropstar's own "CORREÇÃO DTF" were sent by Marcus on 2026-10-27, for -# the orders the team registers as paid outside the site (D-35). "Boleto" is +# DropStar carrier its pickup orders name. Dinheiro was sent by Marcus on +# 2026-10-27, for the orders the team registers as paid outside the site +# (D-35). The customer's credit is "CREDITO PRE-PAGO" (Marcus, 2026-11-18); +# "CORREÇÃO DTF", used for it before, is for correction orders only. "Boleto" is # for a montador Venda order (front 7a.3); no id is known yet, and while # TINY_RECEIPT_BOLETO is empty the order goes without a forma de recebimento. _BOLETO = os.environ.get('TINY_RECEIPT_BOLETO', '').strip() -RECEIPT_METHODS = {'pix': 919478952, 'card': 977460722, 'cash': 919478944, 'credit': 977493654, +RECEIPT_METHODS = {'pix': 919478952, 'card': 977460722, 'cash': 919478944, 'credit': 987762591, **({'faturado': int(_BOLETO)} if _BOLETO.isdigit() else {})} PICKUP_CARRIER = 761565838 # Dropstar's answers: the financial category "DTF SITE" (RECEITAS, id sent on @@ -157,6 +158,15 @@ CATEGORY = 986932924 # the rules document (D-21) keeps the two channels apart in the financial. CATEGORIES = {'dropflow': 987032506} TAG = 'SITE DTF' +# An order paid with the customer's credit also carries this marcador +# (Marcus, 2026-11-18), beside SITE DTF. +CREDIT_TAG = 'CREDITO PRE-PAGO' + + +def order_tags(payload): + """The marcadores an order gets in Tiny.""" + paid = payload.get('payment') or {} + return [TAG] + ([CREDIT_TAG] if paid.get('method') == 'credit' else []) # Freight is the sender's (CIF), as on the store's orders. FREIGHT_PAID_BY = 'R' # The forma de envio each delivery service uses, by name; a Correios service @@ -864,15 +874,17 @@ class TinyOrders: return detail return None - def tag(self, tiny_id, known=False): - """The SITE DTF marcador on the order, once. A retry that finds the - order already created checks first, since POST adds another.""" + def tag(self, tiny_id, tags, known=False): + """The order's marcadores (order_tags), once each. A retry that finds + the order already created checks first, since POST adds another.""" + missing = list(tags) if known: have = self.request('GET', f'/pedidos/{tiny_id}/marcadores') have = have if isinstance(have, list) else have.get('itens') or [] - if any(str(m.get('descricao', '')).strip().upper() == TAG for m in have): - return - self.request('POST', f'/pedidos/{tiny_id}/marcadores', json=[{'descricao': TAG}]) + names = {str(m.get('descricao', '')).strip().upper() for m in have} + missing = [tag for tag in tags if tag not in names] + if missing: + self.request('POST', f'/pedidos/{tiny_id}/marcadores', json=[{'descricao': tag} for tag in missing]) def set_situacao(self, tiny_id, situacao): self.request('PUT', f'/pedidos/{tiny_id}/situacao', json={'situacao': situacao}) @@ -911,8 +923,9 @@ class TinyOrders: tiny_id, situacao = created.get('id'), ABERTA # Tagged before it is approved, so a failure here is retried while the order is still open. if situacao == ABERTA: - self.tag(tiny_id, known=bool(existing)) - receipt['marcador'] = TAG + tags = order_tags(payload) + self.tag(tiny_id, tags, known=bool(existing)) + receipt['marcador'] = ', '.join(tags) if status_updates() and situacao == ABERTA: self.set_situacao(tiny_id, APROVADA) receipt['situacao'] = SITUACOES[APROVADA] diff --git a/tests/test_tiny.py b/tests/test_tiny.py index 79288ba..5c0e7d7 100644 --- a/tests/test_tiny.py +++ b/tests/test_tiny.py @@ -455,13 +455,14 @@ class TinyStatusTests(FakeTinyCase): 1, thursday, shipping=self.tiny.shipping) self.assertEqual(pix['pagamento']['formaRecebimento'], {'id': RECEIPT_METHODS['pix']}) self.assertEqual(pix['pagamento']['categoria'], {'id': CATEGORY}) - # Dinheiro and the customer's credit (CORREÇÃO DTF), Marcus's ids of 2026-10-27. - self.assertEqual((RECEIPT_METHODS['cash'], RECEIPT_METHODS['credit']), (919478944, 977493654)) + # Dinheiro (Marcus, 2026-10-27) and the customer's credit, CREDITO + # PRE-PAGO (Marcus, 2026-11-18; no longer CORREÇÃO DTF). + self.assertEqual((RECEIPT_METHODS['cash'], RECEIPT_METHODS['credit']), (919478944, 987762591)) # A Dropflow order goes in DTF DROPFLOW, not DTF SITE (D-21). batch = order_payload({**PAID, 'origin': 'dropflow', 'payment': {'method': 'credit', 'outside': True}}, 1, thursday, shipping=self.tiny.shipping) self.assertEqual(batch['pagamento']['categoria'], {'id': 987032506}) - self.assertEqual(batch['pagamento']['formaRecebimento'], {'id': 977493654}) + self.assertEqual(batch['pagamento']['formaRecebimento'], {'id': 987762591}) self.assertTrue(batch['observacoesInternas'].startswith('Dropflow DTF · pago fora do site (crédito do cliente) · ')) def test_the_order_is_tagged_kanbandtf_once(self): @@ -474,6 +475,18 @@ class TinyStatusTests(FakeTinyCase): self.assertEqual(len(self.orders), 1) self.assertEqual(self.tags[tiny_id], [{'descricao': 'SITE DTF'}]) + def test_customer_credit_also_carries_the_credito_pre_pago_marcador(self): + credit = {**PAID, 'payment': {'method': 'credit', 'outside': True}} + first = self.tiny.deliver('k1', credit) + self.assertEqual(first['marcador'], 'SITE DTF, CREDITO PRE-PAGO') + tiny_id = self.orders[0]['id'] + self.assertEqual(self.orders[0]['pagamento']['formaRecebimento'], {'id': 987762591}) + self.assertEqual(self.tags[tiny_id], [{'descricao': 'SITE DTF'}, {'descricao': 'CREDITO PRE-PAGO'}]) + # A retry adds only what is missing. + self.tags[tiny_id] = [{'descricao': 'SITE DTF'}] + self.tiny.deliver('k1', credit) + self.assertEqual(self.tags[tiny_id], [{'descricao': 'SITE DTF'}, {'descricao': 'CREDITO PRE-PAGO'}]) + def test_a_failed_tag_is_retried_without_a_second_order(self): self.refuse_tag = 1 with self.assertRaises(TinyError):