This commit is contained in:
yumoqing 2026-04-14 11:21:38 +08:00
parent a833fa6790
commit afef7fb9fc

View File

@ -143,22 +143,11 @@ class Accounting:
def check_accounting_balance(self, legs): def check_accounting_balance(self, legs):
debt_balance = 0.0 debt_balance = 0.0
credit_balance = 0.0 credit_balance = 0.0
curacc = None
acc_balance = 0.00
for l in legs: for l in legs:
if l['accid'] != curacc:
curacc = l['accid']
acc_balance = l['acc'].balance
if l['acc_dir'] != l['balance_at']: if l['acc_dir'] != l['balance_at']:
acc_balance -= l['amount']
l['balance_amount'] = -l['amount'] l['balance_amount'] = -l['amount']
else: else:
acc_balance += l['amount']
l['balance_amount'] = l['amount'] l['balance_amount'] = l['amount']
if acc_balance < 0.000001:
e = AccountOverDraw(curacc, l['acc'].balance, l['amount'])
exception(f'{e},{legs=}')
raise e
if l['acc_dir'] == DEBT: if l['acc_dir'] == DEBT:
debt_balance += l['amount'] debt_balance += l['amount']
else: else:
@ -198,6 +187,8 @@ class Accounting:
async def leg_accounting(self, sor, leg): async def leg_accounting(self, sor, leg):
# print(f'leg_accounting(), {leg=}') # print(f'leg_accounting(), {leg=}')
if leg['amount'] < 0.00001:
return
accid = leg['accid'] accid = leg['accid']
sql = "select * from account where id=${accid}$ for update" sql = "select * from account where id=${accid}$ for update"
accounts = await sor.sqlExe(sql, {'accid': accid}) accounts = await sor.sqlExe(sql, {'accid': accid})
@ -206,8 +197,12 @@ class Accounting:
exception(f'{e}') exception(f'{e}')
raise e raise e
account = accounts[0] account = accounts[0]
if leg['amount'] < 0.00001: new_balance = account.balance + leg['balance_amount']
return if new_balance < -0.0000001:
e = AccountOverDraw(curacc, l['acc'].balance, l['amount'])
exception(f'{e},{legs=}')
raise e
subjects = await sor.R('subject', {'id': leg['subjectid']}) subjects = await sor.R('subject', {'id': leg['subjectid']})
if len(subjects) > 0: if len(subjects) > 0:
leg['subjectname'] = subjects[0].name leg['subjectname'] = subjects[0].name
@ -216,22 +211,19 @@ class Accounting:
where accountid=${accid}$ where accountid=${accid}$
and acc_date = ${curdate}$ for update""" and acc_date = ${curdate}$ for update"""
recs = await sor.sqlExe(sql, {'accid':accid, 'curdate':self.curdate}) recs = await sor.sqlExe(sql, {'accid':accid, 'curdate':self.curdate})
new_balance = 0.0
if len(recs) == 0: if len(recs) == 0:
r = recs[0] r = recs[0]
ns = { ns = {
'id':getID(), 'id':getID(),
'accountid':accid, 'accountid':accid,
'acc_date':self.curdate, 'acc_date':self.curdate,
'balance':account.balance + leg['balance_amount'] 'balance': new_balance
} }
await sor.C('acc_balance', ns.copy()) await sor.C('acc_balance', ns.copy())
new_balance = leg['balance_amount']
else: else:
ns = recs[0] ns = recs[0]
ns['balance'] += account.balance + leg['balance_amount'] ns['balance'] += new_balance
await sor.U('acc_balance', ns.copy()) await sor.U('acc_balance', ns.copy())
new_balance = ns['balance']
# summary = self.summary # summary = self.summary
ns = { ns = {
@ -269,13 +261,13 @@ where accountid=${accid}$
'acc_dir':leg['acc_dir'], 'acc_dir':leg['acc_dir'],
'summary':self.summary, 'summary':self.summary,
'amount':leg['amount'], 'amount':leg['amount'],
'balance':new_balance, 'balance': new_balance,
'acclogid':logid 'acclogid':logid
} }
await sor.C('acc_detail', ns.copy()) await sor.C('acc_detail', ns.copy())
await sor.U('account', { await sor.U('account', {
'id': accid, 'id': accid,
'max_detailno': account.max_detailno + 1, 'max_detailno': account.max_detailno + 1,
'balance': account.balance + leg['balance_amount'] 'balance': new_balance
}) })