Drop the hard balance check in debit_reimburse so occupancy can exceed uploaded invoices; balance may go negative until later top-ups. Co-authored-by: Cursor <cursoragent@cursor.com>
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
"""发票额度流水汇总与退回幂等。"""
|
|
from decimal import Decimal
|
|
|
|
from app.services.bx.invoice_ledger import (
|
|
ENTRY_CREDIT_RELEASE,
|
|
ENTRY_CREDIT_UPLOAD,
|
|
ENTRY_DEBIT_DELETE,
|
|
ENTRY_DEBIT_REIMBURSE,
|
|
should_skip_release,
|
|
summarize_entries,
|
|
)
|
|
|
|
|
|
def test_summarize_upload_and_reimburse():
|
|
rows = [
|
|
{"entry_type": ENTRY_CREDIT_UPLOAD, "amount": Decimal("1000")},
|
|
{"entry_type": ENTRY_DEBIT_REIMBURSE, "amount": Decimal("300")},
|
|
]
|
|
s = summarize_entries(rows)
|
|
assert s["total_amount"] == Decimal("1000")
|
|
assert s["used_amount"] == Decimal("300")
|
|
assert s["balance"] == Decimal("700")
|
|
|
|
|
|
def test_summarize_delete_and_release():
|
|
rows = [
|
|
{"entry_type": ENTRY_CREDIT_UPLOAD, "amount": Decimal("500")},
|
|
{"entry_type": ENTRY_DEBIT_DELETE, "amount": Decimal("100")},
|
|
{"entry_type": ENTRY_DEBIT_REIMBURSE, "amount": Decimal("200")},
|
|
{"entry_type": ENTRY_CREDIT_RELEASE, "amount": Decimal("200")},
|
|
]
|
|
s = summarize_entries(rows)
|
|
assert s["total_amount"] == Decimal("400")
|
|
assert s["used_amount"] == Decimal("0")
|
|
assert s["balance"] == Decimal("400")
|
|
|
|
|
|
def test_release_idempotent_guard():
|
|
assert should_skip_release(has_debit=True, has_release=True) is True
|
|
assert should_skip_release(has_debit=True, has_release=False) is False
|
|
assert should_skip_release(has_debit=False, has_release=False) is True
|
|
|
|
|
|
def test_summarize_allows_negative_balance():
|
|
"""提交占用可超过已上传总额,余额可为负(补票后再回升)。"""
|
|
rows = [
|
|
{"entry_type": ENTRY_CREDIT_UPLOAD, "amount": Decimal("100")},
|
|
{"entry_type": ENTRY_DEBIT_REIMBURSE, "amount": Decimal("300")},
|
|
]
|
|
s = summarize_entries(rows)
|
|
assert s["total_amount"] == Decimal("100")
|
|
assert s["used_amount"] == Decimal("300")
|
|
assert s["balance"] == Decimal("-200")
|