Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixAPI/adapters/api/v1/admin/tax_providers.py
Motamed ASA fa730b6ab0 feat(tax): add Motamed ASA as a tax submission provider
«معتمد آسا» becomes a third, optional sending method beside «ارسال مستقیم»
and «همراه فن‌آوا». It follows the Fanava provider pattern and reuses
tax_provider_credentials, business_tax_provider_accounts and
tax_settings.submission_provider: no new table and no migration. Direct and
Fanava behaviour is unchanged; the default stays moadian_direct.

Connection (informed consent):
- The tax settings page shows what is shared with Motamed ASA and why, with
  links to its terms and to https://asatsp.ir/privacy#partner-software, and
  asks for explicit consent; consent user and time are stored as for Fanava.
- The owner signs up on Motamed ASA's own page (tax-system one-time code);
  Hesabix collects no identity documents. The business API key is handed to
  Hesabix once, after the owner agrees there; a business that already has an
  ASA API v2 key can enter its client id/secret instead.
- The business private key and memory id are not needed: the trusted
  company signs and sends.

Sending (ASA TSP API v2, https://api-v2.asatsp.ir):
- original: POST /api/invoice/send with a stable internalId per document;
  a rejected or unsent invoice is edited in place (editInvoice);
- correction: editInvoice with confirmCorrection;
- cancellation: cancelInvoice; marked cancelled only once accepted;
- sales return: ins=4 whose Body is the rows remaining after the return
  (RC_IITP.IS_V7.9 table 1), read from the chain's accepted invoice;
- timeouts are reconciled by internalId and never re-sent blindly;
- ASA issues the tax id; Hesabix stores it from the answer.

Inquiry and the tax workspace:
- inquire_tax_status, the auto-inquiry loop and the workspace health check
  read Motamed ASA invoices from ASA (inquiryInternalIds).
- The کارپوشه status (buyer reaction) and the Article 6 status (عدول /
  عدم عدول) of the document's own invoice are kept and shown as two new
  workspace columns (tax_workspace_status, tax_article6_status). A buyer
  rejection is shown there and no longer marks a registered invoice failed.
- «پرداخت‌ها و ثبت در کارپوشه» on a sent Motamed ASA invoice opens its
  payment table: every row with its registration status, and new rows
  (method, date/time, amount with VAT, trace and terminal numbers)
  registered through ASA's registerPayment; locked rows stay read-only and a
  lost answer reads the rows back instead of sending them again.

Admin: «اتصال معتمد آسا» under system settings › external services holds
the partner client id/secret Motamed ASA issues for Hesabix.

Tests: tests/test_motamed_asa_{client,invoice_payload,provider_service}.py

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 02:01:02 +03:00

99 lines
3.3 KiB
Python

from __future__ import annotations
from typing import Any, Dict
from fastapi import APIRouter, Body, Depends, Request
from sqlalchemy.orm import Session
from adapters.db.session import get_db
from app.core.auth_dependency import AuthContext, get_current_user
from app.core.responses import ApiError, success_response
from app.services.tax_provider_service import (
get_provider_credential,
save_provider_credential,
serialize_provider_credential,
test_provider_connection,
)
from app.services.motamed_asa_provider_service import (
get_motamed_asa_credential,
save_motamed_asa_credential,
serialize_motamed_asa_credential,
test_motamed_asa_connection,
)
router = APIRouter(prefix="/admin/tax-providers", tags=["مدیریت ارائه‌دهندگان مالیاتی"])
def _require_admin(ctx: AuthContext) -> None:
if not (ctx.is_superadmin() or ctx.has_app_permission("system_settings")):
raise ApiError("FORBIDDEN", "فقط مدیر سیستم می‌تواند ارائه‌دهندگان مالیاتی را مدیریت کند.", http_status=403)
@router.get("/fanava")
def get_fanava_settings(
request: Request,
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
_require_admin(ctx)
return success_response(serialize_provider_credential(get_provider_credential(db)), request)
@router.put("/fanava")
def put_fanava_settings(
request: Request,
payload: Dict[str, Any] = Body(...),
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
_require_admin(ctx)
credential = save_provider_credential(db, payload)
db.commit()
db.refresh(credential)
return success_response(serialize_provider_credential(credential), request, "تنظیم فن‌آوا ذخیره شد.")
@router.post("/fanava/test-connection")
def test_fanava_settings(
request: Request,
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
_require_admin(ctx)
data = test_provider_connection(db)
return success_response(data, request, "اتصال خواندنی فن‌آوا موفق بود.")
@router.get("/motamed-asa")
def get_motamed_asa_settings(
request: Request,
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
_require_admin(ctx)
return success_response(serialize_motamed_asa_credential(get_motamed_asa_credential(db)), request)
@router.put("/motamed-asa")
def put_motamed_asa_settings(
request: Request,
payload: Dict[str, Any] = Body(...),
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
_require_admin(ctx)
credential = save_motamed_asa_credential(db, payload)
db.commit()
db.refresh(credential)
return success_response(serialize_motamed_asa_credential(credential), request, "تنظیم معتمد آسا ذخیره شد.")
@router.post("/motamed-asa/test-connection")
def test_motamed_asa_settings(
request: Request,
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
_require_admin(ctx)
data = test_motamed_asa_connection(db)
return success_response(data, request, "اتصال حسابیکس به معتمد آسا برقرار است.")