Add Phase 12 server AI semantic accounting layer
This commit is contained in:
@@ -9,6 +9,7 @@ from app.modules.accounting.gstr2b_models import AccountingGSTR2BPurchase
|
||||
from app.modules.accounting.gstr2b_service import nature_lookup, review_purchase
|
||||
from app.modules.accounting.historical_learning_service import active_natures, ledger_mappings
|
||||
from app.modules.accounting.purchase_enrichment_models import AccountingPurchaseEnrichmentRecord
|
||||
from app.modules.accounting.ai_service import ai_assist_purchase as run_ai_assist_purchase, mark_review_outcome
|
||||
|
||||
VALID_STATUSES = {"all", "pending_analysis", "suggested", "review_required", "reviewed"}
|
||||
VALID_CONFIDENCE = {"all", "high", "medium", "low"}
|
||||
@@ -133,7 +134,17 @@ def review_one(db, *, tenant_id, client_id, purchase_id, final_nature_id, final_
|
||||
row=db.execute(select(AccountingGSTR2BPurchase).where(AccountingGSTR2BPurchase.id==purchase_id,AccountingGSTR2BPurchase.tenant_id==tenant_id,AccountingGSTR2BPurchase.client_id==client_id)).scalar_one_or_none()
|
||||
if not row: raise ValueError("Purchase record was not found.")
|
||||
_validate_choice(db,tenant_id=tenant_id,client_id=client_id,purchase=row,nature_id=final_nature_id,ledger_name=final_ledger_name)
|
||||
return review_purchase(db,row=row,final_nature_id=int(final_nature_id),final_ledger_name=(final_ledger_name or "").strip(),user_id=user_id)
|
||||
reviewed = review_purchase(db,row=row,final_nature_id=int(final_nature_id),final_ledger_name=(final_ledger_name or "").strip(),user_id=user_id)
|
||||
mark_review_outcome(
|
||||
db,
|
||||
tenant_id=tenant_id,
|
||||
source_type="gstr2b",
|
||||
source_record_id=reviewed.id,
|
||||
final_nature_id=reviewed.final_nature_id,
|
||||
final_ledger_name=reviewed.final_ledger_name,
|
||||
user_id=user_id,
|
||||
)
|
||||
return reviewed
|
||||
|
||||
|
||||
def bulk_confirm(db, *, tenant_id, client_id, purchase_ids, user_id):
|
||||
@@ -148,6 +159,26 @@ def bulk_confirm(db, *, tenant_id, client_id, purchase_ids, user_id):
|
||||
if row.review_status=="reviewed": continue
|
||||
if not row.suggested_nature_id or int(row.suggested_confidence or 0)<85: raise ValueError(f"Invoice {row.invoice_number} is not eligible for high-confidence bulk confirmation.")
|
||||
_validate_choice(db,tenant_id=tenant_id,client_id=client_id,purchase=row,nature_id=row.suggested_nature_id,ledger_name=row.suggested_ledger_name or "")
|
||||
review_purchase(db,row=row,final_nature_id=int(row.suggested_nature_id),final_ledger_name=row.suggested_ledger_name or "",user_id=user_id)
|
||||
reviewed = review_purchase(db,row=row,final_nature_id=int(row.suggested_nature_id),final_ledger_name=row.suggested_ledger_name or "",user_id=user_id)
|
||||
mark_review_outcome(
|
||||
db,
|
||||
tenant_id=tenant_id,
|
||||
source_type="gstr2b",
|
||||
source_record_id=reviewed.id,
|
||||
final_nature_id=reviewed.final_nature_id,
|
||||
final_ledger_name=reviewed.final_ledger_name,
|
||||
user_id=user_id,
|
||||
)
|
||||
completed+=1
|
||||
return completed
|
||||
|
||||
|
||||
def ai_assist_one(db, *, tenant_id, client_id, purchase_id, user_id):
|
||||
row = db.execute(select(AccountingGSTR2BPurchase).where(
|
||||
AccountingGSTR2BPurchase.id == int(purchase_id),
|
||||
AccountingGSTR2BPurchase.tenant_id == int(tenant_id),
|
||||
AccountingGSTR2BPurchase.client_id == int(client_id),
|
||||
)).scalar_one_or_none()
|
||||
if not row:
|
||||
raise ValueError("Purchase record was not found.")
|
||||
return run_ai_assist_purchase(db, row=row, user_id=user_id)
|
||||
|
||||
Reference in New Issue
Block a user