from .const import * from appPublic.log import debug from sqlor.dbpools import DBPools async def get_parent_orgid(sor, orgid): sql = """select a.id from organization a, organization b where b.parentid = a.id and b.id = ${orgid}$""" recs = await sor.sqlExe(sql, {'orgid':orgid}) if len(recs) == 0: return None return recs[0]['id'] async def get_offer_orgid(sor, bid_orgid, providerid, productid, curdate): sql = """select a.offer_orgid from saleprotocol a, product_salemode b where a.id = b.protocolid and a.bid_orgid = ${bid_orgid}$ and b.providerid = ${providerid}$ and b.productid in (${productid}$, '*') and a.start_date <= ${curdate}$ and a.end_date > ${curdate}$ """ recs = await sor.sqlExe(sql, { 'bid_orgid':bid_orgid, 'providerid':providerid, 'productid':productid, 'curdate':curdate }) if len(recs) == 0: return None rec = recs[0] return rec['offer_orgid'] async def get_offer_orgs(sor, bid_orgid, providerid, productid, curdate): offer_orgid = await get_offer_orgid(sor, bid_orgid, providerid, productid, curdate) if offer_orgid is None or offer_orgid == providerid: return [] myids = [offer_orgid] orgs = await get_offer_orgs(sor, offer_orgid, providerid, productid, curdate) return orgs + myids async def get_ancestor_orgs(sor, orgid): id = await get_parent_orgid(sor, orgid) if not id: return [] ret = await get_ancestor_orgs(sor, id) return ret + [id] async def get_orgtypes(sor, orgid): sql = "select orgtypeid from orgtypes where orgid = ${orgid}$" recs = await sor.sqlExe(sql, {'orgid':orgid}) return [r.orgtypeid for r in recs] async def get_accounting_nodes(sor, customerid): """ gt all accounting organization for transactes customer orgid """ mytypes = await get_orgtypes(sor, customerid) is_c = False for t in mytypes: if t in ['customer', 'agencycustomer', 'personalcustomer']: is_c = True break if not is_c: debug(f'{customerid=} is not a customer organzition') return [] sql = """select a.id from organization a, organization b where b.parentid = a.id and b.id = ${customerid}$ """ recs = await sor.sqlExe(sql, {'customerid':customerid}) if len(recs) == 0: debug(f'{customerid=} not found is organziation table') return ['0'] return [] ret = await get_ancestor_orgs(sor, recs[0]['id']) ret.append(recs[0]['id']) return ret