ns = params_kw.copy() userorgid = await get_userorgid() if not userorgid: return { "widgettype":"Error", "options":{ "title":"Authorization Error", "timeout":3, "cwidth":16, "cheight":9, "message":"Please login" } } ns['resellerid'] = userorgid ns['userorgid'] = userorgid debug(f'get_sales_ledger.dspy:{ns=}') if not ns.get('page'): ns['page'] = 1 if not ns.get('sort'): ns['sort'] = ["sale_date desc","created_at desc"] sql = '''select a.*, b.sub_reseller_id_text, c.supplier_id_text, d.agreement_id_text, e.contract_id_text, f.resellerid_text from (select * from sales_ledger where 1=1 [[filterstr]]) a left join (select id as sub_reseller_id, sub_reseller_name as sub_reseller_id_text from sub_resellers where 1 = 1) b on a.sub_reseller_id = b.sub_reseller_id left join (select id as supplier_id, supplier_name as supplier_id_text from suppliers where 1 = 1) c on a.supplier_id = c.supplier_id left join (select id as agreement_id, agreement_name as agreement_id_text from distribution_agreements where 1 = 1) d on a.agreement_id = d.agreement_id left join (select id as contract_id, contract_name as contract_id_text from supply_contracts where 1 = 1) e on a.contract_id = e.contract_id left join (select id as resellerid, orgname as resellerid_text from organization where 1 = 1) f on a.resellerid = f.resellerid''' filterjson = params_kw.get('data_filter') if filterjson and isinstance(filterjson, str): try: filterjson = json.loads(filterjson) except (json.JSONDecodeError, TypeError): filterjson = None fields_str=r'''[ { "name": "id", "title": "主键ID", "type": "str", "length": 32, "nullable": "no" }, { "name": "resellerid", "title": "所属分销商机构ID", "type": "str", "length": 32, "nullable": "no" }, { "name": "sub_reseller_id", "title": "二级分销商ID", "type": "str", "length": 32 }, { "name": "supplier_id", "title": "供应商ID", "type": "str", "length": 32 }, { "name": "agreement_id", "title": "分销协议ID", "type": "str", "length": 32 }, { "name": "contract_id", "title": "供销合同ID", "type": "str", "length": 32 }, { "name": "prodtypeid", "title": "产品分类ID", "type": "str", "length": 32 }, { "name": "productid", "title": "产品ID", "type": "str", "length": 32 }, { "name": "sale_date", "title": "销售日期", "type": "date", "nullable": "no" }, { "name": "quantity", "title": "销售数量", "type": "double", "length": 15, "dec": 2, "nullable": "no" }, { "name": "unit_price", "title": "销售单价", "type": "double", "length": 15, "dec": 4, "nullable": "no" }, { "name": "supply_discount", "title": "进货折扣", "type": "double", "length": 5, "dec": 4 }, { "name": "supply_amount", "title": "进货金额", "type": "double", "length": 15, "dec": 2 }, { "name": "distribution_discount", "title": "分销折扣", "type": "double", "length": 5, "dec": 4 }, { "name": "distribution_amount", "title": "分销金额", "type": "double", "length": 15, "dec": 2 }, { "name": "profit_amount", "title": "利润金额", "type": "double", "length": 15, "dec": 2 }, { "name": "settlement_status", "title": "结算状态", "type": "char", "length": 1, "nullable": "no", "default": "0" }, { "name": "remark", "title": "备注", "type": "text" }, { "name": "created_by", "title": "创建人", "type": "str", "length": 32 }, { "name": "created_at", "title": "创建时间", "type": "datetime", "nullable": "no" }, { "name": "updated_at", "title": "更新时间", "type": "datetime" } ]''' ori_fields = json.loads(fields_str) if not filterjson: fields = [ f['name'] for f in ori_fields ] filterjson = default_filterjson(fields, ns) # 确保 logined 过滤条件始终生效 if filterjson: if not isinstance(filterjson, dict) or 'AND' not in filterjson: filterjson = {'AND': [filterjson] if filterjson else []} filterjson['AND'].append({'field': 'resellerid', 'op': '=', 'var': '__logined_orgid__'}) ns['__logined_orgid__'] = userorgid filterdic = ns.copy() filterdic['filterstr'] = '' filterdic['userorgid'] = '${userorgid}$' filterdic['userid'] = '${userid}$' if filterjson: dbf = DBFilter(filterjson) conds = dbf.gen(ns) if conds: ns.update(dbf.consts) conds = f' and {conds}' filterdic['filterstr'] = conds ac = ArgsConvert('[[', ']]') vars = ac.findAllVariables(sql) NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' } filterdic.update(NameSpace) sql = ac.convert(sql, filterdic) debug(f'{sql=}') db = DBPools() dbname = get_module_dbname('supplychain') async with db.sqlorContext(dbname) as sor: r = await sor.sqlPaging(sql, ns) return r return { "total":0, "rows":[] }