supplychain/wwwroot/supplychain_accounting_list/get_supplychain_accounting.dspy

230 lines
5.9 KiB
Plaintext

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_supplychain_accounting.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.supply_contract_id_text, c.distribution_agreement_id_text, d.sub_distributor_id_text, e.supplier_id_text, f.prodtypeid_text, g.productid_text, h.resellerid_text
from (select * from supplychain_accounting where 1=1 [[filterstr]]) a left join (select id as supply_contract_id,
contract_name as supply_contract_id_text from supply_contracts where 1 = 1) b on a.supply_contract_id = b.supply_contract_id left join (select id as distribution_agreement_id,
agreement_name as distribution_agreement_id_text from distribution_agreements where 1 = 1) c on a.distribution_agreement_id = c.distribution_agreement_id left join (select id as sub_distributor_id,
sub_dist_name as sub_distributor_id_text from sub_distributors where 1 = 1) d on a.sub_distributor_id = d.sub_distributor_id left join (select id as supplier_id,
supplier_name as supplier_id_text from suppliers where 1 = 1) e on a.supplier_id = e.supplier_id left join (select id as prodtypeid,
type_name as prodtypeid_text from product_types where 1 = 1) f on a.prodtypeid = f.prodtypeid left join (select id as productid,
product_name as productid_text from products where 1 = 1) g on a.productid = g.productid left join (select id as resellerid,
orgname as resellerid_text from organization where 1 = 1) h on a.resellerid = h.resellerid'''
filterjson = params_kw.get('data_filter')
fields_str=r'''[
{
"name": "id",
"title": "主键ID",
"type": "str",
"length": 32,
"nullable": "no"
},
{
"name": "resellerid",
"title": "所属主分销商机构ID",
"type": "str",
"length": 32,
"nullable": "no"
},
{
"name": "supply_contract_id",
"title": "供销合同ID",
"type": "str",
"length": 32
},
{
"name": "supply_contract_item_id",
"title": "供销合同产品明细ID",
"type": "str",
"length": 32
},
{
"name": "distribution_agreement_id",
"title": "分销协议ID",
"type": "str",
"length": 32
},
{
"name": "distribution_agreement_item_id",
"title": "分销协议产品明细ID",
"type": "str",
"length": 32
},
{
"name": "sub_distributor_id",
"title": "二级分销商ID",
"type": "str",
"length": 32
},
{
"name": "supplier_id",
"title": "供应商ID",
"type": "str",
"length": 32
},
{
"name": "prodtypeid",
"title": "产品分类ID",
"type": "str",
"length": 32
},
{
"name": "productid",
"title": "产品ID",
"type": "str",
"length": 32
},
{
"name": "quantity",
"title": "数量",
"type": "double",
"length": 15,
"dec": 4,
"nullable": "no",
"default": "0"
},
{
"name": "unit_price",
"title": "销售单价",
"type": "double",
"length": 15,
"dec": 4,
"nullable": "no",
"default": "0"
},
{
"name": "supply_discount",
"title": "进货折扣",
"type": "double",
"length": 5,
"dec": 4
},
{
"name": "supply_amount",
"title": "进货金额(应付供应商)",
"type": "double",
"length": 15,
"dec": 2,
"nullable": "no",
"default": "0"
},
{
"name": "dist_discount",
"title": "分销折扣",
"type": "double",
"length": 5,
"dec": 4
},
{
"name": "dist_amount",
"title": "分销金额(二级分销商应付)",
"type": "double",
"length": 15,
"dec": 2,
"nullable": "no",
"default": "0"
},
{
"name": "profit_amount",
"title": "利润金额",
"type": "double",
"length": 15,
"dec": 2,
"nullable": "no",
"default": "0"
},
{
"name": "sale_date",
"title": "销售日期",
"type": "date",
"nullable": "no"
},
{
"name": "source_type",
"title": "来源类型",
"type": "char",
"length": 1,
"default": "1"
},
{
"name": "source_id",
"title": "来源记录ID",
"type": "str",
"length": 32
},
{
"name": "remark",
"title": "备注",
"type": "text"
},
{
"name": "created_by",
"title": "创建人",
"type": "str",
"length": 32
},
{
"name": "created_at",
"title": "创建时间",
"type": "datetime",
"nullable": "no"
}
]'''
ori_fields = json.loads(fields_str)
if not filterjson:
fields = [ f['name'] for f in ori_fields ]
filterjson = default_filterjson(fields, ns)
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":[]
}