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