Merge main into label
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -65,8 +65,8 @@ def cursor_pair(first, second):
|
||||
return first is not None
|
||||
|
||||
|
||||
MODES = {'textil': {'file', 'avulsa'}, 'uv': {'uvfile', 'uv'},
|
||||
'folha': {'file', 'uvfile'}, 'avulsa': {'avulsa', 'uv'}}
|
||||
MODES = {'textil': {'file', 'avulsa', 'premium'}, 'uv': {'uvfile', 'uv'},
|
||||
'folha': {'file', 'uvfile', 'premium'}, 'avulsa': {'avulsa', 'uv'}}
|
||||
|
||||
|
||||
def like(text):
|
||||
|
||||
@@ -133,6 +133,22 @@ def tiny_status(c=None):
|
||||
return {'configured': True, 'orders_enabled': os.environ.get('TINY_ADAPTER') == 'tiny',
|
||||
**tiny.TinyAuth().status(c)}
|
||||
|
||||
@router.get('/api/operator/tiny/contacts')
|
||||
def tiny_contacts(q: str = Query(min_length=1, max_length=120), user=Depends(can('assemble'))):
|
||||
"""The Tiny contacts a Venda customer is picked from (Fase 2, front
|
||||
7a.3): by name, or by CPF/CNPJ when only digits were typed. The fake
|
||||
adapter answers with two fixed contacts on the local stack and in CI."""
|
||||
if os.environ.get('TINY_ADAPTER') != 'tiny':
|
||||
from ...adapters import FakeTiny
|
||||
return FakeTiny().search_contacts(q)
|
||||
if not tiny.configured():
|
||||
raise HTTPException(409, 'O Tiny não está conectado.')
|
||||
try:
|
||||
return tiny.search_contacts(tiny.TinyOrders(), q)
|
||||
except tiny.TinyNotConnected:
|
||||
raise HTTPException(409, 'O Tiny não está conectado.')
|
||||
|
||||
|
||||
@router.post('/api/operator/tiny/connect')
|
||||
def tiny_connect(user=Depends(can('integrations'))):
|
||||
"""Start the one-time authorisation of this system in the client's Tiny."""
|
||||
|
||||
@@ -4,7 +4,7 @@ from decimal import Decimal
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
||||
from ... import machines, unpaid
|
||||
from ... import machines, payments, unpaid
|
||||
from ...core import db
|
||||
from ...core.auth import operator
|
||||
from .common import ORIGINS, REAL_ORDER, TREATED, person
|
||||
@@ -13,6 +13,11 @@ from .rework import CUSTOMER_CAUSE, HOUSE_CAUSES, REWORK_GOAL_PERCENT
|
||||
router = APIRouter()
|
||||
|
||||
LOCAL_DAY = "(o.created_at AT TIME ZONE 'America/Sao_Paulo')::date"
|
||||
# A montador "Interno" or "faturado" order records the table price, not money
|
||||
# received (Marcus, 2026-11-10): the cash figures leave it out and Números
|
||||
# shows it in its own block.
|
||||
NOT_CASH = ('COALESCE(o.payment->>\'method\',\'\') NOT IN '
|
||||
f"('{payments.INTERNAL_METHOD}','{payments.INVOICED_METHOD}')")
|
||||
|
||||
|
||||
def waited(order, start, end):
|
||||
@@ -57,7 +62,8 @@ def production_stats(days: int | None = Query(default=None, ge=7, le=365), start
|
||||
limit_rows = c.execute('SELECT state,hours,responsible FROM dtf_local.stage_limits').fetchall()
|
||||
limits = {row['state']: {'hours': float(row['hours']), 'responsible': row['responsible']}
|
||||
for row in limit_rows}
|
||||
total = c.execute(f'''SELECT count(*) AS orders, COALESCE(sum((o.snapshot->>'total_cents')::bigint),0) AS cents,
|
||||
total = c.execute(f'''SELECT count(*) AS orders,
|
||||
COALESCE(sum((o.snapshot->>'total_cents')::bigint) FILTER (WHERE {NOT_CASH}),0) AS cents,
|
||||
COALESCE(sum({metres}),0) AS metres,
|
||||
count(*) FILTER (WHERE EXISTS (SELECT 1 FROM dtf_local.movements m WHERE m.order_id=o.id
|
||||
AND m.to_state='cor' AND NOT m.back)) AS corrected
|
||||
@@ -131,20 +137,29 @@ def production_stats(days: int | None = Query(default=None, ge=7, le=365), start
|
||||
# Each way in apart (D-21): what came from where, and how long it took
|
||||
# from payment to Finalizado, Aguardando contato left out as above.
|
||||
origins = {row['origin']: row for row in c.execute(f'''SELECT o.origin, count(*) AS orders,
|
||||
COALESCE(sum((o.snapshot->>'total_cents')::bigint),0) AS cents, COALESCE(sum({metres}),0) AS metres,
|
||||
COALESCE(sum((o.snapshot->>'total_cents')::bigint) FILTER (WHERE {NOT_CASH}),0) AS cents,
|
||||
COALESCE(sum({metres}),0) AS metres,
|
||||
count(f.at) AS finished, percentile_cont(0.5) WITHIN GROUP (ORDER BY extract(epoch FROM
|
||||
f.at - o.created_at) - {waited('o.id', 'o.created_at', 'f.at')}) / 3600 AS median_hours
|
||||
FROM dtf_local.orders o
|
||||
LEFT JOIN LATERAL (SELECT min(created_at) AS at FROM dtf_local.movements m
|
||||
WHERE m.order_id=o.id AND m.to_state='fin') f ON true
|
||||
WHERE {REAL_ORDER} AND {within} GROUP BY o.origin''', period).fetchall()}
|
||||
# The table price of the montador's Interno and Venda orders, counted
|
||||
# apart from the money received above (front 7a.3).
|
||||
price_table = {row['method']: row for row in c.execute(f'''SELECT o.payment->>'method' AS method,
|
||||
count(*) AS orders, COALESCE(sum({metres}),0) AS metres,
|
||||
COALESCE(sum((o.snapshot->>'total_cents')::bigint),0) AS cents
|
||||
FROM dtf_local.orders o WHERE {REAL_ORDER} AND {within}
|
||||
AND o.payment->>'method' IN (%s,%s) GROUP BY 1''',
|
||||
period + (payments.INTERNAL_METHOD, payments.INVOICED_METHOD)).fetchall()}
|
||||
# Coupons used by the period's paid orders (front 8.3): what each code
|
||||
# brought in orders, billed metres, discount and money received.
|
||||
coupons_used = c.execute(f'''SELECT o.snapshot->'coupon'->>'id' AS id,
|
||||
o.snapshot->'coupon'->>'code' AS code, count(*) AS orders,
|
||||
COALESCE(sum({metres}),0) AS metres,
|
||||
COALESCE(sum((o.snapshot->'coupon'->>'discount_cents')::bigint),0) AS discount_cents,
|
||||
COALESCE(sum((o.snapshot->>'total_cents')::bigint),0) AS cents
|
||||
COALESCE(sum((o.snapshot->>'total_cents')::bigint) FILTER (WHERE {NOT_CASH}),0) AS cents
|
||||
FROM dtf_local.orders o
|
||||
WHERE {REAL_ORDER} AND {within} AND o.snapshot->'coupon'->>'id' IS NOT NULL
|
||||
GROUP BY 1,2 ORDER BY orders DESC, code''', period).fetchall()
|
||||
@@ -224,6 +239,11 @@ def production_stats(days: int | None = Query(default=None, ge=7, le=365), start
|
||||
'median_hours': hours(origins[name]['median_hours']) if name in origins else None}
|
||||
for name in ORIGINS],
|
||||
'unpaid': unpaid_figures,
|
||||
'price_table': [{'method': method,
|
||||
'orders': price_table[method]['orders'] if method in price_table else 0,
|
||||
'metres': float(price_table[method]['metres']) if method in price_table else 0.0,
|
||||
'cents': int(price_table[method]['cents']) if method in price_table else 0}
|
||||
for method in (payments.INVOICED_METHOD, payments.INTERNAL_METHOD)],
|
||||
'lead': {'orders': lead['orders'], 'median_hours': hours(lead['median_hours'])},
|
||||
'stage_limits': limits, 'late': late,
|
||||
'stages': {row['state']: {'orders': row['orders'], 'passages': row['passages'],
|
||||
|
||||
Reference in New Issue
Block a user