diff --git a/accounting/accounting_config.py b/accounting/accounting_config.py index 2e08a62..5c7e69a 100644 --- a/accounting/accounting_config.py +++ b/accounting/accounting_config.py @@ -49,7 +49,7 @@ class Accounting: async def setup_all_accounting_legs(self): self.accounting_legs = [] debug(f'{self.callers=}') - for caller in self.callers: + for i, caller in ienumerate(self.callers): self.caller = caller self.curdate = caller.curdate self.realtimesettled = False @@ -64,18 +64,20 @@ class Accounting: self.own_salemode = None self.reseller_salemode = None self.variable = caller.variable - await self.setup_accounting_legs() + await self.setup_accounting_legs(i) legs = sorted( self.accounting_legs, key=lambda x: ( x.get('accounting_orgid','0'), - x.get('accid', '0'), + x.get('orgid'), + x.get('subjectid'), 0 if x.get('acc_dir', '0') == x.get('balance_at', '0') else 1 ) ) self.accounting_legs = legs + self.get_legs_account() - async def setup_accounting_legs(self): + async def setup_accounting_legs(self, pos): sor = self.sor action = self.action.split('_')[0] acfg = await get_accounting_config(self.sor) @@ -84,6 +86,7 @@ class Accounting: debug(f'{legs=}') rev = self.action.endswith('_REVERSE') for l in legs: + l['position'] = pos if rev: l['acc_dir'] = DEBT if l['accounting_dir'] == CREDIT else CREDIT else: @@ -110,17 +113,27 @@ class Accounting: orgid = await self.caller.get_orgid_by_trans_role(sor, l, l.orgtype) org1id = None if l.org1type is None else \ await self.caller.get_orgid_by_trans_role(sor, l, l.org1type) - acc = await get_account(sor, accounting_orgid, orgid, l.subjectid, org1id=org1id) - if acc is None: - debug(f'can not get accountid {accounting_orgid=}, {orgid=},{l.subjectid=}, {org1id=}, {l=}, {self.customerid=},{self.resellerid=},{self.providerid=}') - raise AccountIdNone(accounting_orgid, orgid, l['subjectid']) l['accounting_orgid'] = accounting_orgid l['orgid'] = orgid l['org1id'] = org1id + self.accounting_legs += legs + + def get_legs_account(self): + sor = self.sor + oldk = '' + acc = None + for l in self.accounting_legs: + k = f'{l.accounting_orgid}|{l.orgid}|{l.subjectid}|{l.org1id=org1id}' + if oldk != k: + acc = await get_account(sor, l.accounting_orgid, l.orgid, + l.subjectid, l.org1id=org1id, update=True) + if acc is None: + debug(f'can not get accountid {accounting_orgid=}, {orgid=},{l.subjectid=}, {org1id=}, {l=}, {self.customerid=},{self.resellerid=},{self.providerid=}') + raise AccountIdNone(accounting_orgid, orgid, l['subjectid']) + oldk = k l['accid'] = acc.id l['balance_at'] = acc.balance_at l['acc'] = acc - self.accounting_legs += legs def check_accounting_balance(self, legs): debt_balance = 0.0 @@ -136,7 +149,7 @@ class Accounting: else: acc_balance += l['amount'] if acc_balance < 0.000000: - e = AccountOverDraw(curacc, acc['balance'], leg['amount']) + e = AccountOverDraw(curacc, l['acc'].balance, leg['amount']) exception(f'{e},{legs=}') raise e l['new_balance'] = acc_balance diff --git a/accounting/getaccount.py b/accounting/getaccount.py index 14b4d7e..1bae40a 100644 --- a/accounting/getaccount.py +++ b/accounting/getaccount.py @@ -13,9 +13,7 @@ async def get_account(sor, accounting_orgid, orgid, subjectid, org1id=None, upda sql = """select a.*, -case when b.balance is null then 0.00 else b.balance end as balance -from account a left join acc_balance b - on a.id = b.accountid +from account a where a.subjectid = ${subjectid}$ and a.accounting_orgid = ${accounting_orgid}$ and @@ -38,48 +36,14 @@ where return None return recs[0] -async def get_account_by_subjectname(sor, accounting_orgid, orgid, subjectname, org1id=None): - ss = "a.org1id is NULL" - if org1id: - ss = "a.org1id = ${org1id}$" - - sql = """select * from account a, subject b - where - a.subjectid = b.id and - b.name = ${subjectname}$ and - a.accounting_orgid = ${accounting_orgid}$ and - a.orgid = ${orgid}$ and - """ + ss - recs = await sor.sqlExe(sql, { - "accounting_orgid":accounting_orgid, - "orgid":orgid, - "org1id":org1id, - "subjectname":subjectname - }); - if len(recs) == 0: - return None - for rec in recs: - if a.org1id == org1id: - return rec['id'] - return None - async def getAccountByName(sor, accounting_orgid, orgid, name, org1id): - sql = """select a.* from account a, subject b -where a.subjectid = b.id and - a.accounting_orgid = ${accounting_orgid}$ and - a.orgid = ${orgid}$ and - b.name = ${name}$""" + sql = """select a.* from subject a where a.name = ${name}$""" recs = await sor.sqlExe(sql, { - "accounting_orgid":accounting_orgid, - "orgid":orgid, "name":name }); if len(recs) == 0: return None - for rec in recs: - if rec.org1id == org1id: - return rec['id'] - return None + return get_account(sor, accounting_orgid, orgid, recs[0].id, org1id=org1id) async def getTransPayMode(): pass @@ -103,13 +67,13 @@ async def getCustomerBalance(sor, customerid): return balance async def getAccountBalance(sor, accounting_orgid, orgid, subjectname, org1id): - accid = await getAccountByName(sor, accounting_orgid, + acc = await getAccountByName(sor, accounting_orgid, orgid, subjectname,org1id) if accid is None: debug(f'accid is None, {accounting_orgid=}, {orgid=}, {subjectname=}') return None - return await getAccountBalanceByAccid(sor, accid) + return acc.balance async def get_account_total_amount(sor, accid, accounting_dir, from_date, to_date): sql = """select sun(amount) as amount from acc_detail @@ -133,30 +97,9 @@ where accountid =${accountid}$ async def getAccountBalanceByAccid(sor, accid): - balances = await sor.sqlExe("""select * from acc_balance where accountid=${accid}$ order by acc_date desc""", {'accid':accid}) + balances = await sor.sqlExe("""select * from account where id=${accid}$""", {'accid':accid}) if len(balances) == 0: debug(f'acc_balance is None, {accid=}') return 0 return balances[0]['balance'] -async def get_account_info(sor, accid): - sql = ''' -select b.orgname as accounting_org, -case when a.accounting_orgid = a.orgid then '本机构' - when c.org_type in ('0', '1') then '分销商' - when c.org_type = '2' then '供应商' - else '客户' end as acctype, -c.orgname, -d.name -from account a, organization b, organization c, subject d -where a.accounting_orgid = b.id - and a.orgid = c.id - and a.subjectid = d.id - and a.id = ${accid}$''' - recs = await sor.sqlExe(sql, {'accid':accid}) - if len(recs) == 0: - - return None - r = recs[0] - r['balance'] = await getAccountBalanceByAccid(sor, accid) - return r diff --git a/models/acc_balance.xlsx b/models/acc_balance.xlsx index 8b4cd3a..52b46e5 100644 Binary files a/models/acc_balance.xlsx and b/models/acc_balance.xlsx differ diff --git a/models/account.xlsx b/models/account.xlsx index 17aee05..40e912c 100644 Binary files a/models/account.xlsx and b/models/account.xlsx differ