Add Phase 12 server AI semantic accounting layer
This commit is contained in:
@@ -11,7 +11,7 @@ from app.modules.accounting.historical_learning_service import active_natures
|
||||
from app.modules.accounting.ledger_learning_service import available_tally_guids
|
||||
from app.modules.accounting.purchase_review_service import (
|
||||
review_queue, review_counts, return_periods, nature_maps_for_rows,
|
||||
explanations_for_rows, mappings_by_nature, review_one, bulk_confirm,
|
||||
explanations_for_rows, mappings_by_nature, review_one, bulk_confirm, ai_assist_one,
|
||||
)
|
||||
from app.modules.accounting.ui import _find_visible_client, _require_partner, _visible_clients
|
||||
from app.modules.core.rbac.deps import get_user_permissions, get_user_roles
|
||||
@@ -90,3 +90,52 @@ async def bulk_confirm_rows(request:Request):
|
||||
except Exception as exc:
|
||||
db.rollback(); return _redirect(client_id,filters,error=str(exc))
|
||||
finally:db.close()
|
||||
|
||||
|
||||
@router.post("/purchase/{purchase_id}/ai-assist")
|
||||
def ai_assist_purchase_row(
|
||||
request: Request,
|
||||
purchase_id: int,
|
||||
client_id: int = Form(...),
|
||||
tally_guid: str = Form(""),
|
||||
status: str = Form("all"),
|
||||
confidence: str = Form("all"),
|
||||
source: str = Form("all"),
|
||||
supplier: str = Form(""),
|
||||
return_period: str = Form(""),
|
||||
page: int = Form(1),
|
||||
per_page: int = Form(25),
|
||||
csrf_token: str = Form(...),
|
||||
):
|
||||
validate_csrf(request, csrf_token)
|
||||
db = CommonSessionLocal()
|
||||
filters = {
|
||||
"tally_guid": tally_guid, "status": status, "confidence": confidence,
|
||||
"source": source, "supplier": supplier, "return_period": return_period,
|
||||
"page": page, "per_page": per_page,
|
||||
}
|
||||
try:
|
||||
user, response = _require_partner(request, db, "accounting.learning.manage")
|
||||
if response:
|
||||
return response
|
||||
client, _, scope = _find_visible_client(db, request, user, client_id)
|
||||
if not client:
|
||||
from app.core.http_responses import ui_access_denied
|
||||
return ui_access_denied()
|
||||
decision = ai_assist_one(
|
||||
db,
|
||||
tenant_id=scope.tenant_id,
|
||||
client_id=client.id,
|
||||
purchase_id=purchase_id,
|
||||
user_id=user.id,
|
||||
)
|
||||
return _redirect(
|
||||
client.id,
|
||||
filters,
|
||||
message=f"AI semantic fallback completed at {decision.combined_confidence}% combined confidence.",
|
||||
)
|
||||
except Exception as exc:
|
||||
db.rollback()
|
||||
return _redirect(client_id, filters, error=str(exc))
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user