R02، بخش تراکنشی: اتمیک‌ماندن نهایی‌سازی مالی افتتاحیه #68

Merged
morrning merged 1 commit from Seyyed/Seyyed_arc:codex/team-base-38-opening-cancel-lifecycle into master 2026-11-19 14:54:15 +03:30
2 changed files with 109 additions and 4 deletions

View file

@ -553,6 +553,7 @@ def post_opening_balance(
user_id: int,
fiscal_year_id: Optional[int],
) -> Dict[str, Any]:
"""نهایی‌سازی در تراکنش فراخواننده؛ commit بر عهدهٔ caller است."""
fy_id, _, _ = _ensure_fiscal_year(db, business_id, fiscal_year_id)
existing = _find_existing_ob_document(db, business_id, fy_id)
if not existing:
@ -565,7 +566,7 @@ def post_opening_balance(
"extra_info": {**(existing.extra_info or {}), "posted": True, "posted_by": int(user_id)},
}
repo = DocumentRepository(db)
updated = repo.update_document(existing.id, payload)
updated = repo.update_document(existing.id, payload, auto_commit=False)
if not updated:
raise ApiError("POST_FAILED", "نهایی‌سازی تراز افتتاحیه ناموفق بود", http_status=500)
return repo.get_document_details(updated.id) or {}
@ -578,7 +579,7 @@ def unpost_opening_balance(
fiscal_year_id: Optional[int],
request: Any = None,
) -> Dict[str, Any]:
"""لغو نهایی‌سازی؛ فقط وقتی مجاز است که جز سند افتتاحیه، سند حسابداری دیگری در همان سال مالی نباشد."""
"""لغو نهایی‌سازی بدون commit؛ فقط وقتی در همان سال سند دیگری جز افتتاحیه نباشد."""
fy_id, _, _ = _ensure_fiscal_year(db, business_id, fiscal_year_id)
existing = _find_existing_ob_document(db, business_id, fy_id)
if not existing:
@ -612,7 +613,7 @@ def unpost_opening_balance(
extra.pop("posted_by", None)
repo = DocumentRepository(db)
updated = repo.update_document(existing.id, {"extra_info": extra})
updated = repo.update_document(existing.id, {"extra_info": extra}, auto_commit=False)
if not updated:
raise ApiError("UNPOST_FAILED", "لغو نهایی‌سازی تراز افتتاحیه ناموفق بود", http_status=500)
@ -632,7 +633,6 @@ def unpost_opening_balance(
after_data={"posted": False},
request=request,
)
db.commit()
except Exception:
pass

View file

@ -0,0 +1,105 @@
"""Financial finalization owns no commit; these tests do not claim a Variant warehouse lifecycle."""
from datetime import date
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy.schema import CreateTable
from adapters.db import models
from adapters.db.models.activity_log import ActivityLog
from adapters.db.models.business import BusinessField, BusinessType
from adapters.db.models.product_variant import ProductVariant
from app.core.responses import ApiError
from app.services.opening_balance_service import post_opening_balance, unpost_opening_balance
DAY = date(2026, 11, 11)
@pytest.fixture
def opening_db():
engine = create_engine("sqlite://")
tables = (models.Business, models.Currency, models.FiscalYear, models.User, models.Account,
models.Product, ProductVariant, models.Document, models.DocumentLine, ActivityLog)
with engine.begin() as connection:
for model in tables:
connection.execute(CreateTable(model.__table__, include_foreign_key_constraints=[]))
with Session(engine, autoflush=False, expire_on_commit=False) as db:
db.add_all([
models.Business(id=1, name="Test", owner_id=1, default_currency_id=1,
business_type=BusinessType.SHOP, business_field=BusinessField.TRADING),
models.Currency(id=1, name="Rial", title="Rial", code="IRR", symbol="IRR"),
models.FiscalYear(id=1, business_id=1, title="FY", start_date=DAY, end_date=DAY, is_last=True),
models.User(id=1, first_name="Test", last_name="Owner", password_hash="fixture", referral_code="OBTX"),
models.Account(id=1, business_id=1, code="INV", name="Inventory", account_type="asset"),
models.Account(id=2, business_id=1, code="EQUITY", name="Equity", account_type="equity"),
models.Product(id=1, business_id=1, code="P", name="P", variant_mode="inventory", main_unit="each"),
ProductVariant(id=1, business_id=1, product_id=1, combination_key="1"),
models.Document(id=1, business_id=1, fiscal_year_id=1, currency_id=1, created_by_user_id=1,
code="OPENING", document_type="opening_balance", document_date=DAY,
is_proforma=False, extra_info={"posted": False}),
models.DocumentLine(document_id=1, product_id=1, quantity=10, debit=0, credit=0,
extra_info={"warehouse_id": 1, "movement": "in", "allocations": [{"variant_id": 1, "quantity": 10}]}),
models.DocumentLine(document_id=1, account_id=1, debit=1000, credit=0),
models.DocumentLine(document_id=1, account_id=2, debit=0, credit=1000),
])
db.commit()
yield db
engine.dispose()
def lines(db):
return [(row.id, row.document_id, row.quantity, row.debit, row.credit, row.extra_info)
for row in db.query(models.DocumentLine).order_by(models.DocumentLine.id)]
@pytest.mark.parametrize("initial,operation", [(False, post_opening_balance), (True, unpost_opening_balance)])
def test_caller_failure_rolls_back_finalization_and_activity(opening_db, initial, operation):
db = opening_db
db.get(models.Document, 1).extra_info = {"posted": initial}
db.commit()
before = lines(db)
try:
operation(db, 1, 1, 1)
assert db.get(models.Document, 1).extra_info["posted"] is not initial
raise RuntimeError("late caller failure")
except RuntimeError:
db.rollback()
with Session(db.bind) as fresh:
assert fresh.get(models.Document, 1).extra_info["posted"] is initial
assert lines(fresh) == before
assert fresh.query(ActivityLog).count() == 0
@pytest.mark.parametrize("initial,operation", [(False, post_opening_balance), (True, unpost_opening_balance)])
def test_root_commit_persists_and_repeated_operation_is_idempotent(opening_db, initial, operation):
db = opening_db
db.get(models.Document, 1).extra_info = {"posted": initial}
db.commit()
before = lines(db)
for _ in range(2):
operation(db, 1, 1, 1)
db.commit()
with Session(db.bind) as fresh:
assert fresh.get(models.Document, 1).extra_info["posted"] is not initial
assert lines(fresh) == before
assert fresh.query(ActivityLog).count() == (1 if initial else 0)
def test_same_year_financial_guard_still_rejects_unpost_without_changes(opening_db):
db = opening_db
db.get(models.Document, 1).extra_info = {"posted": True}
db.add(models.Document(id=2, business_id=1, fiscal_year_id=1, currency_id=1, created_by_user_id=1,
code="OTHER", document_type="accounting_document", document_date=DAY, is_proforma=False))
db.add_all([models.DocumentLine(document_id=2, account_id=1, debit=20, credit=0),
models.DocumentLine(document_id=2, account_id=2, debit=0, credit=20)])
db.commit()
before = lines(db)
with pytest.raises(ApiError) as error:
unpost_opening_balance(db, 1, 1, 1)
assert (error.value.code, error.value.status_code) == ("OPENING_BALANCE_UNPOST_BLOCKED", 409)
db.rollback()
with Session(db.bind) as fresh:
assert fresh.get(models.Document, 1).extra_info["posted"] is True
assert lines(fresh) == before