from sqlor.dbpools import DBPools from .const import * from appPublic.uniqueID import getID from appPublic.registerfunction import RegisterFunction, rfexe from appPublic.log import debug, exception from datetime import datetime from accounting.getaccount import get_account async def openAccount(sor, accounting_orgid, orgid, account_config, org1id=None): acc = await get_account(sor, accounting_orgid, orgid, account_config['subjectid'], org1id=org1id) if acc: debug(f'{acc=} opened') return ns = { 'id':getID(), 'accounting_orgid':accounting_orgid, 'orgid':orgid, 'subjectid':account_config['subjectid'], 'balance_at':account_config['balance_side'], 'max_detailno':0 } if org1id: ns['org1id'] = org1id; debug(f'openAccount(), {ns=}') await sor.C('account', ns.copy()) debug(f'{ns=} opened') async def openPartyAccounts(sor, accounting_orgid, orgid, party_type, org1id=None, party1_type=None): addon_cond = " and a.party1type is NULL " ns = {'partytype':party_type} if party1_type: addon_cond = " and a.party1type = ${party1type}$ " ns['party1type'] = party1_type sql = """select a.*, b.name as subjectname,b.balance_side from account_config a, subject b where a.subjectid = b.id """ \ + addon_cond + """ and a.partytype=${partytype}$ """ recs = await sor.sqlExe(sql, ns) # print(f'select account_config {recs=}', party_type) debug(f'{sql=}, {accounting_orgid=}, {orgid=}, {party_type=}') for r in recs: debug(f'{r=}') await openAccount(sor, accounting_orgid, orgid, r, org1id=org1id) async def openResellerAccounts(sor, accounting_orgid, orgid): return await openPartyAccounts(sor, accounting_orgid, orgid, PARTY_RESELLER) async def openCustomerAccounts(sor, accounting_orgid, orgid): return await openPartyAccounts(sor, accounting_orgid, orgid, PARTY_CUSTOMER) async def openOwnerAccounts(sor, accounting_orgid): orgid = accounting_orgid return await openPartyAccounts(sor, accounting_orgid, orgid, PARTY_OWNER) async def openProviderAccounts(sor, accounting_orgid, orgid): return await openPartyAccounts(sor, accounting_orgid, orgid, PARTY_PROVIDER) async def openAllCustomerAccounts(sor, accounting_orgid): rf = RegisterFunction() f = rf.get('get_customers_by_orgid') if f is None: exception(f'get_customers_by_orgid function not registed, {accounting_orgid=}') raise Exception('get_customers_by_orgid not registed') recs = await f(sor, accounting_orgid) print(f'{recs=}') for r in recs: await openCustomerAccounts(sor, accounting_orgid, r['id']) async def openAllResellerAccounts(sor, accounting_orgid): rf = RegisterFunction() f = rf.get('get_resellers_by_orgid') if f is None: exception(f'get_resellers_by_orgid function not registed, {accounting_orgid=}') raise Exception('get_resellers_by_orgid not registed') recs = await f(sor, accounting_orgid) print(f'{recs=}') for r in recs: await openResellerAccounts(sor, accounting_orgid, r['id']) async def openAllProviderAccounts(sor, accounting_orgid): rf = RegisterFunction() f = rf.get('get_providers_by_orgid') if f is None: exception(f'get_providers_by_orgid function not registed, {accounting_orgid=}') raise Exception('get_providers_by_orgid not registed') recs = await f(sor, accounting_orgid) print(f'{recs=}') for r in recs: await openProviderAccounts(sor, accounting_orgid, r['id']) async def openRetailRelationshipAccounts(sor, accounting_orgid, providerid, resellerid): await openPartyAccounts(sor, accounting_orgid, providerid, PARTY_PROVIDER, org1id=resellerid, party1_type=PARTY_RESELLER) await openPartyAccounts(sor, accounting_orgid, resellerid, PARTY_RESELLER, org1id=providerid, party1_type=PARTY_PROVIDER)