Allow editing orders in the order card
Cashiers can edit recipient, TTN, notes, goods and total in the order
modal and save via PATCH /orders/{id}. Edited orders get edited_at and
are no longer overwritten by CRM sync. Editing is blocked once a receipt
exists; changing the TTN resets Nova Poshta tracking fields.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,13 +2,16 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from decimal import Decimal
|
||||
from decimal import ROUND_HALF_UP, Decimal
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db.models.order import Order
|
||||
from app.schemas.orders import OrderUpdateIn
|
||||
from app.services.crm.client import CrmClient
|
||||
from app.services.nova_poshta.client import NovaPoshtaClient
|
||||
|
||||
@@ -17,6 +20,19 @@ _CRM_STATUS = "APPROVED"
|
||||
# NP отклоняет запросы с более чем 100 накладными за раз (см. np_client.py).
|
||||
_NP_BATCH_SIZE = 100
|
||||
|
||||
# Поля, которые кассир может править в карточке заказа (кроме goods/total).
|
||||
_EDITABLE_FIELDS = (
|
||||
"recipient_name",
|
||||
"recipient_phone",
|
||||
"recipient_email",
|
||||
"waybill_number",
|
||||
"notes",
|
||||
)
|
||||
|
||||
|
||||
class OrderEditError(Exception):
|
||||
"""Заказ нельзя сохранить — сообщение показывается кассиру."""
|
||||
|
||||
|
||||
def _to_kopecks(amount: str) -> int:
|
||||
return int((Decimal(amount) * 100).to_integral_value())
|
||||
@@ -31,7 +47,8 @@ async def sync_orders_from_crm(session: AsyncSession, crm: CrmClient) -> None:
|
||||
|
||||
Уже скрытые (`is_deleted`) заказы не восстанавливаются и не перезаписываются
|
||||
— иначе кнопка «Удалить» переставала бы работать при следующем открытии
|
||||
дашборда, т.к. CRM продолжает возвращать эти заказы как есть.
|
||||
дашборда, т.к. CRM продолжает возвращать эти заказы как есть. По той же
|
||||
причине не трогаются заказы, отредактированные вручную (`edited_at`).
|
||||
"""
|
||||
crm_orders = await crm.get_orders(status=_CRM_STATUS)
|
||||
if not crm_orders:
|
||||
@@ -45,7 +62,7 @@ async def sync_orders_from_crm(session: AsyncSession, crm: CrmClient) -> None:
|
||||
for crm_order in crm_orders:
|
||||
local = existing_by_id.get(crm_order.id)
|
||||
if local is not None:
|
||||
if local.is_deleted:
|
||||
if local.is_deleted or local.edited_at is not None:
|
||||
continue
|
||||
else:
|
||||
local = Order(id=crm_order.id)
|
||||
@@ -119,3 +136,92 @@ async def delete_order(session: AsyncSession, order_id: str) -> Order | None:
|
||||
order.is_deleted = True
|
||||
order.deleted_at = datetime.now(UTC)
|
||||
return order
|
||||
|
||||
|
||||
def _good_key(good: dict[str, Any]) -> tuple[Any, ...]:
|
||||
"""Позиция без учёта формата строк: CRM пишет "1", форма — "1.000"."""
|
||||
return (
|
||||
good["id"],
|
||||
good.get("sku") or "",
|
||||
good["name"],
|
||||
_to_kopecks(good["price"]),
|
||||
Decimal(good["quantity"]),
|
||||
_to_kopecks(good["amount"]) if good.get("amount") else None,
|
||||
)
|
||||
|
||||
|
||||
def _build_goods(data: OrderUpdateIn) -> tuple[list[dict[str, Any]], int]:
|
||||
"""Позиции формы → снимок `orders.goods` в формате CRM и их сумма в копейках."""
|
||||
goods: list[dict[str, Any]] = []
|
||||
total = 0
|
||||
for good in data.goods:
|
||||
price = int(good.price * 100)
|
||||
discount = int(good.discount_amount * 100)
|
||||
gross = int((price * good.quantity).to_integral_value(ROUND_HALF_UP))
|
||||
net = gross - discount
|
||||
if net < 0:
|
||||
raise OrderEditError(f"Товар «{good.name}»: скидка больше суммы строки")
|
||||
goods.append(
|
||||
{
|
||||
"id": good.id or f"new-{uuid.uuid4().hex[:8]}",
|
||||
"sku": good.sku,
|
||||
"name": good.name,
|
||||
"price": f"{good.price:.2f}",
|
||||
"quantity": f"{good.quantity:.3f}",
|
||||
"discount_amount": f"{good.discount_amount:.2f}",
|
||||
"discount_percent": None,
|
||||
"amount": f"{net / 100:.2f}",
|
||||
}
|
||||
)
|
||||
total += net
|
||||
return goods, total
|
||||
|
||||
|
||||
async def update_order(
|
||||
session: AsyncSession, order_id: str, data: OrderUpdateIn
|
||||
) -> tuple[Order, list[str]] | None:
|
||||
"""Сохраняет ручные правки заказа. Возвращает заказ и список изменённых полей.
|
||||
|
||||
Править можно только заказ без чека: у заказа с чеком (в т.ч. `pending`)
|
||||
данные уже ушли или уходят в Checkbox. Строка блокируется, чтобы
|
||||
параллельный запрос на создание чека не прочитал заказ посреди правки.
|
||||
"""
|
||||
order = await session.scalar(select(Order).where(Order.id == order_id).with_for_update())
|
||||
if order is None or order.is_deleted:
|
||||
return None
|
||||
if order.receipt_created_at is not None:
|
||||
raise OrderEditError("По заказу уже создан чек — редактирование недоступно")
|
||||
|
||||
goods, goods_total = _build_goods(data)
|
||||
total = _to_kopecks(str(data.total_amount))
|
||||
if total > goods_total:
|
||||
raise OrderEditError(
|
||||
f"Сумма заказа {total / 100:.2f} ₴ больше суммы товаров {goods_total / 100:.2f} ₴"
|
||||
)
|
||||
|
||||
changed = [
|
||||
field for field in _EDITABLE_FIELDS if getattr(order, field) != getattr(data, field)
|
||||
]
|
||||
goods_changed = [_good_key(good) for good in order.goods] != [_good_key(good) for good in goods]
|
||||
if goods_changed:
|
||||
changed.append("goods")
|
||||
if order.total_amount_kopecks != total:
|
||||
changed.append("total_amount")
|
||||
if not changed:
|
||||
return order, changed
|
||||
|
||||
if "waybill_number" in changed:
|
||||
# Статус старой ТТН к новой не относится — worker опросит новую за минуту.
|
||||
order.np_status = None
|
||||
order.np_status_code = None
|
||||
order.np_cod_amount_kopecks = None
|
||||
order.np_payment_status = None
|
||||
|
||||
for field in _EDITABLE_FIELDS:
|
||||
setattr(order, field, getattr(data, field))
|
||||
if goods_changed:
|
||||
# Иначе оставляем снимок CRM как есть — форматы строк у CRM свои ("1" vs "1.000").
|
||||
order.goods = goods
|
||||
order.total_amount_kopecks = total
|
||||
order.edited_at = datetime.now(UTC)
|
||||
return order, changed
|
||||
|
||||
Reference in New Issue
Block a user