forked from hesabix/arc
«معتمد آسا» 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>
236 lines
9.1 KiB
Python
Executable file
236 lines
9.1 KiB
Python
Executable file
"""
|
|
سرویس استعلام خودکار وضعیت فاکتورهای مالیاتی
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Dict, Any, List
|
|
from datetime import datetime, timedelta
|
|
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import and_
|
|
|
|
from adapters.db.models.document import Document
|
|
from adapters.db.models.tax_setting import TaxSetting
|
|
from app.services.motamed_asa_provider_service import MOTAMED_ASA_PROVIDER
|
|
from app.services.tax_submission_service import inquire_tax_status
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def auto_inquire_pending_invoices(
|
|
db: Session,
|
|
business_id: int | None = None,
|
|
max_invoices: int = 100,
|
|
hours_ago: int = 24,
|
|
) -> Dict[str, Any]:
|
|
"""
|
|
استعلام خودکار وضعیت فاکتورهای pending که بیش از hours_ago ساعت در pending هستند
|
|
|
|
Args:
|
|
db: Database session
|
|
business_id: شناسه کسبوکار (اگر None باشد، همه کسبوکارها)
|
|
max_invoices: حداکثر تعداد فاکتور برای استعلام
|
|
hours_ago: فاکتورهایی که بیش از این ساعت در pending هستند
|
|
|
|
Returns:
|
|
نتیجه استعلام
|
|
"""
|
|
cutoff_time = datetime.utcnow() - timedelta(hours=hours_ago)
|
|
|
|
# ساخت query برای فاکتورهای pending
|
|
query = db.query(Document).filter(
|
|
Document.extra_info.isnot(None)
|
|
)
|
|
|
|
# فیلتر بر اساس business_id
|
|
if business_id:
|
|
query = query.filter(Document.business_id == business_id)
|
|
|
|
# فیلتر فاکتورهای pending
|
|
# استفاده از JSON_EXTRACT یا LIKE برای بررسی extra_info
|
|
from sqlalchemy import func
|
|
|
|
# دریافت تمام فاکتورها و فیلتر در Python (بهتر از JSON query در MySQL)
|
|
all_docs = query.all()
|
|
pending_docs: List[Document] = []
|
|
|
|
for doc in all_docs:
|
|
extra = doc.extra_info or {}
|
|
if not isinstance(extra, dict):
|
|
continue
|
|
|
|
tax_status = extra.get("tax_status", "").strip() if isinstance(extra.get("tax_status"), str) else extra.get("tax_status")
|
|
tax_workspace = bool(extra.get("tax_workspace"))
|
|
tracking_code = extra.get("tax_tracking_code")
|
|
|
|
# فقط فاکتورهای pending با tracking code
|
|
if (
|
|
tax_workspace and
|
|
tax_status == "pending" and
|
|
tracking_code and
|
|
isinstance(tracking_code, str) and
|
|
tracking_code.strip()
|
|
):
|
|
# بررسی زمان آخرین ارسال
|
|
last_send_at = extra.get("tax_last_send_at")
|
|
if last_send_at:
|
|
try:
|
|
if isinstance(last_send_at, str):
|
|
send_time = datetime.fromisoformat(last_send_at.replace('Z', '+00:00'))
|
|
else:
|
|
send_time = last_send_at
|
|
|
|
if send_time < cutoff_time:
|
|
pending_docs.append(doc)
|
|
except Exception:
|
|
# اگر زمان نامعتبر بود، اضافه میکنیم
|
|
pending_docs.append(doc)
|
|
else:
|
|
# اگر زمان ارسال نداشت، اضافه میکنیم
|
|
pending_docs.append(doc)
|
|
|
|
if not pending_docs:
|
|
return {
|
|
"success": True,
|
|
"total_checked": 0,
|
|
"total_inquired": 0,
|
|
"results": [],
|
|
}
|
|
|
|
# محدود کردن تعداد
|
|
pending_docs = pending_docs[:max_invoices]
|
|
|
|
# گروهبندی بر اساس business_id
|
|
by_business: Dict[int, List[Document]] = {}
|
|
for doc in pending_docs:
|
|
if doc.business_id not in by_business:
|
|
by_business[doc.business_id] = []
|
|
by_business[doc.business_id].append(doc)
|
|
|
|
total_inquired = 0
|
|
all_results: List[Dict[str, Any]] = []
|
|
|
|
# استعلام برای هر کسبوکار
|
|
from app.core.moadian_plugin_dependency import check_moadian_plugin_active
|
|
|
|
for bid, docs in by_business.items():
|
|
try:
|
|
if not check_moadian_plugin_active(db, bid):
|
|
continue
|
|
# دریافت تنظیمات مالیاتی
|
|
tax_setting = (
|
|
db.query(TaxSetting)
|
|
.filter(TaxSetting.business_id == bid)
|
|
.first()
|
|
)
|
|
|
|
if not tax_setting:
|
|
logger.warning(f"Tax settings not found for business {bid}")
|
|
continue
|
|
|
|
# «معتمد آسا» invoices are inquired from ASA, not from the tax system with the business's own key.
|
|
asa_docs = [
|
|
doc for doc in docs
|
|
if (doc.extra_info or {}).get("tax_submission_provider") == MOTAMED_ASA_PROVIDER
|
|
]
|
|
if asa_docs:
|
|
asa_ids = {int(doc.id) for doc in asa_docs}
|
|
docs = [doc for doc in docs if int(doc.id) not in asa_ids]
|
|
try:
|
|
response = inquire_tax_status(db, bid, invoice_ids=sorted(asa_ids))
|
|
db.commit()
|
|
total_inquired += len(response.get("results") or [])
|
|
for doc in asa_docs:
|
|
extra = doc.extra_info or {}
|
|
all_results.append({
|
|
"invoice_id": doc.id,
|
|
"business_id": bid,
|
|
"tracking_code": extra.get("tax_tracking_code"),
|
|
"status": extra.get("tax_status"),
|
|
})
|
|
except Exception as e:
|
|
logger.error(f"Error inquiring motamed_asa status for business {bid}: {e}", exc_info=True)
|
|
db.rollback()
|
|
|
|
# جمعآوری tracking codes
|
|
tracking_codes: List[str] = []
|
|
doc_by_tracking: Dict[str, Document] = {}
|
|
|
|
for doc in docs:
|
|
code = (doc.extra_info or {}).get("tax_tracking_code")
|
|
if code and isinstance(code, str) and code.strip():
|
|
tracking_codes.append(code.strip())
|
|
doc_by_tracking[code.strip()] = doc
|
|
|
|
if not tracking_codes:
|
|
continue
|
|
|
|
# استعلام وضعیت
|
|
try:
|
|
from app.core.settings import get_settings
|
|
from app.integrations.moadian.client import MoadianClient
|
|
|
|
client = MoadianClient(settings=get_settings(), tax_setting=tax_setting)
|
|
try:
|
|
response = client.inquire_status(tracking_codes)
|
|
results = response.get("results") or []
|
|
|
|
# بهروزرسانی وضعیت فاکتورها
|
|
now = datetime.utcnow().isoformat()
|
|
for item in results:
|
|
reference = item.get("reference_number") or item.get("tracking_code")
|
|
if not reference:
|
|
continue
|
|
|
|
doc = doc_by_tracking.get(str(reference))
|
|
if not doc:
|
|
continue
|
|
|
|
extra = dict(doc.extra_info or {})
|
|
from app.services.tax_submission_service import _map_inquiry_status
|
|
mapped_status = _map_inquiry_status(item.get("status"))
|
|
if mapped_status:
|
|
extra["tax_status"] = mapped_status
|
|
if item.get("error_message"):
|
|
extra["tax_error_message"] = item.get("error_message")
|
|
elif item.get("status") not in ("failed", "error"):
|
|
extra.pop("tax_error_message", None)
|
|
extra["tax_last_inquiry_at"] = now
|
|
doc.extra_info = extra
|
|
db.add(doc)
|
|
|
|
all_results.append({
|
|
"invoice_id": doc.id,
|
|
"business_id": bid,
|
|
"tracking_code": reference,
|
|
"status": mapped_status or item.get("status"),
|
|
})
|
|
|
|
total_inquired += len(results)
|
|
db.commit()
|
|
|
|
finally:
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error inquiring status for business {bid}: {e}", exc_info=True)
|
|
db.rollback()
|
|
continue
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing business {bid}: {e}", exc_info=True)
|
|
continue
|
|
|
|
return {
|
|
"success": True,
|
|
"total_checked": len(pending_docs),
|
|
"total_inquired": total_inquired,
|
|
"results": all_results,
|
|
"completed_at": datetime.utcnow().isoformat(),
|
|
}
|
|
|
|
|
|
|
|
|