Files
lux_fiscal/backend/alembic/versions/0005_cash_registers_and_receipts.py
T
lauadminandClaude Opus 5.5 518a99197f Add Checkbox ETTN receipts for Nova Poshta COD waybills
Cashier creates an ETTN receipt in Checkbox bound to the TTN with payment
control; Checkbox fiscalizes it itself when the parcel is paid for.

- cash_registers (Fernet-encrypted license key / PIN) and receipts tables
- Checkbox HTTP client + stub (ETTN does not work on test registers)
- two-phase create via ARQ job, timeout reconciliation, cron status polling
- /receipts and /cash-registers API, audit records
- dashboard: per-order and bulk create, prepayment, cancel; cash registers page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 23:39:00 +03:00

115 lines
4.3 KiB
Python

"""Кассы Checkbox и ЕТТН-чеки
Revision ID: 0005
Revises: 0004
Create Date: 2026-09-23
"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "0005"
down_revision: str | None = "0004"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
receipt_status = postgresql.ENUM(
"pending",
"created",
"done",
"returned",
"receipt_error",
"cancelled",
"failed",
name="receipt_status",
create_type=False,
)
def upgrade() -> None:
receipt_status.create(op.get_bind(), checkfirst=True)
op.create_table(
"cash_registers",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("fiscal_number", sa.String(length=64), nullable=True),
sa.Column("license_key_enc", sa.String(length=512), nullable=False),
sa.Column("cashier_pin_enc", sa.String(length=512), nullable=False),
sa.Column("tax_codes", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("is_default", sa.Boolean(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.PrimaryKeyConstraint("id", name=op.f("pk_cash_registers")),
)
op.create_index(
"uq_cash_registers_default",
"cash_registers",
["is_default"],
unique=True,
postgresql_where=sa.text("is_default"),
)
op.create_table(
"receipts",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("order_id", sa.String(length=32), nullable=False),
sa.Column("cash_register_id", sa.Uuid(), nullable=False),
sa.Column("created_by_id", sa.Uuid(), nullable=True),
sa.Column("waybill_number", sa.String(length=64), nullable=False),
sa.Column("total_kopecks", sa.BigInteger(), nullable=False),
sa.Column("prepayment_kopecks", sa.BigInteger(), nullable=False),
sa.Column("cod_kopecks", sa.BigInteger(), nullable=False),
sa.Column("status", receipt_status, nullable=False),
sa.Column("checkbox_ettn_id", sa.String(length=64), nullable=True),
sa.Column("checkbox_status", sa.String(length=32), nullable=True),
sa.Column("checkbox_receipt_id", sa.String(length=64), nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("request_body", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("last_checked_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.ForeignKeyConstraint(
["order_id"],
["orders.id"],
name=op.f("fk_receipts_order_id_orders"),
ondelete="RESTRICT",
),
sa.ForeignKeyConstraint(
["cash_register_id"],
["cash_registers.id"],
name=op.f("fk_receipts_cash_register_id_cash_registers"),
ondelete="RESTRICT",
),
sa.ForeignKeyConstraint(
["created_by_id"],
["users.id"],
name=op.f("fk_receipts_created_by_id_users"),
ondelete="SET NULL",
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_receipts")),
)
op.create_index(op.f("ix_receipts_order_id"), "receipts", ["order_id"])
op.create_index(op.f("ix_receipts_cash_register_id"), "receipts", ["cash_register_id"])
op.create_index(op.f("ix_receipts_status"), "receipts", ["status"])
op.create_index(op.f("ix_receipts_checkbox_ettn_id"), "receipts", ["checkbox_ettn_id"])
op.create_index(
"uq_receipts_active_order",
"receipts",
["order_id"],
unique=True,
postgresql_where=sa.text("status NOT IN ('cancelled', 'failed')"),
)
def downgrade() -> None:
op.drop_table("receipts")
op.drop_table("cash_registers")
receipt_status.drop(op.get_bind(), checkfirst=True)