feat: discount coupons (front 8.3)
Gestão creates coupons in the Site tab: a code, a percentage or an amount per billed metre, validity, a minimum of metres, use limits in all and per customer, one product or all, and a note; they can be paused and deleted (kept for history). The customer types the code at checkout; the server checks it when the quote is made and again at approval, discounts only the eligible items after the bands (never freight or the treatment) and keeps the coupon on the order. Tiny gets the discount, the e-mail and receipt show it, the order panel carries a badge and Números counts orders, metres, discount and money per coupon. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,7 @@ from uuid import UUID, uuid4
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from psycopg.types.json import Jsonb
|
||||
|
||||
from .. import catalog, payments
|
||||
from .. import catalog, coupons, payments
|
||||
from ..core import db
|
||||
from ..core.auth import owner
|
||||
from ..core.models import QuoteRequest
|
||||
@@ -26,7 +26,11 @@ def create_quote(body: QuoteRequest, session_id=Depends(owner)):
|
||||
if abs(item.metres*100-item.production.height_cm) > Decimal('0.02'):
|
||||
raise HTTPException(422, 'A metragem não confere com a montagem enviada. Monte o pedido de novo.')
|
||||
require_delivery_available(body.freight.service)
|
||||
# The coupon code is stored normalized, so a quote waiting for review keeps
|
||||
# the code the operator will see (front 8.3).
|
||||
code = (body.coupon or '').strip().upper() or None
|
||||
draft = body.model_dump(mode='json', exclude={'request_key'})
|
||||
draft['coupon'] = code
|
||||
digest = hashlib.sha256(json.dumps(draft, sort_keys=True).encode()).hexdigest()
|
||||
accepted_at = datetime.now(timezone.utc).isoformat()
|
||||
for item in draft['items']:
|
||||
@@ -36,8 +40,9 @@ def create_quote(body: QuoteRequest, session_id=Depends(owner)):
|
||||
item['stroke_choice_at'] = accepted_at
|
||||
reason = quote_review.review_reason(draft)
|
||||
with db.connect() as c:
|
||||
if reason is not None:
|
||||
# Waits for review: check the delivery now, priced at approval.
|
||||
if reason is not None or code:
|
||||
# Waits for review: check the delivery now, priced at approval. A
|
||||
# coupon is checked on the same priced items (front 8.3).
|
||||
try:
|
||||
priced = []
|
||||
for item in body.items:
|
||||
@@ -57,10 +62,14 @@ def create_quote(body: QuoteRequest, session_id=Depends(owner)):
|
||||
if price_for_total != catalog.unit_cents(c, mode):
|
||||
banded[mode] = price_for_total
|
||||
apply_bands(priced, banded)
|
||||
freight.quote(body.freight.service, body.freight.postal_code,
|
||||
sum(Decimal(i['billed_metres']) for i in priced), sum(i['total_cents'] for i in priced))
|
||||
if reason is not None:
|
||||
freight.quote(body.freight.service, body.freight.postal_code,
|
||||
sum(Decimal(i['billed_metres']) for i in priced),
|
||||
sum(i['total_cents'] for i in priced))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(422, str(exc))
|
||||
if code:
|
||||
coupons.check(c, code, body.customer.cnpj, priced)
|
||||
rows = {}
|
||||
for item in body.items:
|
||||
for uid in item.uploads:
|
||||
|
||||
Reference in New Issue
Block a user