<?xml version="1.0" encoding="utf-8" ?>
<select>
	<selectstr>
		SELECT
		u_mattress.mattressid,
		mattressname,
		deptid,
		mattresstypeid,
		mattresscode,
		mattress_width,
		mattress_length,
		mattress_height,
		packtype,
		packqty,
		woodpallettype,
		total_hr_cost,
		total_material_cost ,
		fees_dscrp,
		total_fees_cost,
		taxrate,
		commissionrate,
		commission,
		fob,
		profitrate,
		profitrate_point,
		butao_point,
		chaizhuang_point,
		haimian_point,
		dannum_rate,
		ISNULL(dannum_type, 1) AS dannum_type,
		hrcost,
		biandaicost,
		zhizao_amt,
		guanli_rate,
		discount,
		extras_cost,
		dept_profitrate,
		dept_profitrate_rangli,
		moneyrate,
		mattressrelcode,
		flag,
		auditingrep,
		auditingdate,
		createtime,
		createby,
		total_cost,
		nottax_factory_cost,
		nottax_dept_cost,
		taxes,
		dept_cost,
		foreign_cost,
		foreign_cost_bz,
		nottax_factory_cost  / ( dept_profitrate + dept_profitrate_rangli / 100  ) as dijia_cost,
		xd_flag,
		xd_auditingrep,
		xd_auditingdate,
		qr_flag,
		qr_auditingrep,
		qr_auditingdate,
		js1_flag ,
		erp_mtrlcode,
		erp_cb_updatetime,
		if_bcp_type,
		if_zhedie_type,
		if_w_butao,
		biandai_qty,
		other_rate,
		if_moneyrate,
		if_m_chai,
		if_z_chai,
		if_d_chai,
		if_n_butao,
		if_m_wbutao_way,
		s_cover_qty,
		z_cover_qty,
		x_cover_qty,
		s_m_cover_qty,
		z_m_cover_qty,
		x_m_cover_qty,
		cabinet_type,
		area,
		diameter,
		cubage,
		parentid,
		vv_mattress.child_count,
		 dannum_cost1,
		 dannum_cost2,
		 dannum_cost3,
		 dannum_cost4

		From u_mattress
		LEFT JOIN (
			SELECT
				um.mattressid,
				COUNT(um2.mattressid) AS child_count
			FROM
				u_mattress um
			LEFT JOIN
				u_mattress um2 ON um.mattressid = um2.parentid
			GROUP BY um.mattressid
		) AS vv_mattress on vv_mattress.mattressid = u_mattress.mattressid
	</selectstr>
	<where>
		<when notnull="@arg_mattressid">
			u_mattress.mattressid = @arg_mattressid
		</when>
		<when notnull="@arg_mattresstypeid">
			u_mattress.mattresstypeid = @arg_mattresstypeid
		</when>
		<when notnull="@arg_deptid">
			u_mattress.deptid = @arg_deptid
		</when>
		<when $user_outrepstr!="-1">
			u_mattress.createby in ($$user_outrepstr$$)
		</when>
		<when notnull="@arg_qr_flag">
			u_mattress.qr_flag = @arg_qr_flag
		</when>
		<when notnull="@arg_flag">
			u_mattress.flag = @arg_flag
		</when>
		<when notnull="@arg_xd_flag">
			u_mattress.xd_flag = @arg_xd_flag
		</when>
		<when notnull="@arg_cp_flag">
			u_mattress.js1_flag = @arg_cp_flag
		</when>
		<when>
			u_mattress.bcptypeid = 0
		</when>
		<when notnull="@arg_search">
			EXISTS (SELECT 1 FROM dbo.SC_SPLIT(@arg_search, ',') A WHERE A.Item <> '' AND (u_mattress.mattresscode LIKE '%'+LTRIM(RTRIM(A.Item))+'%' OR u_mattress.mattressrelcode like '%'+LTRIM(RTRIM(A.Item))+'%' OR u_mattress.mattressname like '%'+LTRIM(RTRIM(A.Item))+'%'))
		</when>
		<when notnull="@arg_erp_mtrlcode">
			EXISTS (SELECT 1 FROM dbo.SC_SPLIT(@arg_erp_mtrlcode, ',') A WHERE A.Item <> '' AND (u_mattress.erp_mtrlcode LIKE '%'+LTRIM(RTRIM(A.Item))+'%'))
		</when>
	</where>
	<orderstr>
		mattressid DESC
	</orderstr>
	<displayfields>
		<field field="mattresscode">报价唯一码</field>
		<field field="xd_flag" datatype="checkbox">下单</field>
		<field field="js1_flag" datatype="checkbox">产品补充审核</field>
		<field field="flag" datatype="checkbox">财务审核</field>
		<field field="deptid" mapper="" ddd="ddd_mattress_dept_choose" align="left">部门</field>
		<field field="mattressrelcode">床垫编码</field>
		<field field="mattressname">床垫名称</field>
		<field field="mattresstypeid" mapper="" ddd="ddd_mattress_type_choose">床垫类别</field>
		<field field="erp_mtrlcode">L1物料编码</field>
		<field field="mattress_width" datatype="integer">床垫宽/CM</field>
		<field field="mattress_length" datatype="integer">床垫长/CM</field>
		<field field="mattress_height" datatype="integer">床垫高/CM</field>
		<field field="total_cost" datatype="number">总成本</field>
		<field field="nottax_factory_cost" datatype="number">不含税出厂价</field>
		<field field="nottax_dept_cost" datatype="number">部门不含税价</field>
		<field field="taxes" datatype="number">税金</field>
		<field field="dept_cost" datatype="number">部门含税价</field>
		<field field="dannum_cost1" datatype="number">散单金额</field>
		<field field="dannum_cost2" datatype="number">标准金额</field>
		<field field="dannum_cost3" datatype="number">大单金额</field>
		<field field="dannum_cost4" datatype="number">小单金额</field>
		<field field="foreign_cost" datatype="number">外币价</field>
		<field field="packtype">包装方式</field>
		<field field="packqty" datatype="number">压包数量</field>
		<field field="woodpallettype" datatype="checkbox">木托方式</field>
		<field field="dijia_cost" datatype="number">底价</field>
		<field field="yongjin" datatype="number" compute="dijia_cost / (1 - (commission - 1 )) - dijia_cost">佣金</field>
		<field field="taxrate" datatype="number">税率</field>
		<field field="fob" datatype="number">FOB</field>
		<field field="moneyrate" mapper="u_mattress.if_moneyrate">币种</field>
		<field field="createby">登记人</field>
		<field field="createtime" datatype="datetime">登记时间</field>
		<field field="auditingrep">审核人</field>
		<field field="auditingdate" datatype="datetime">审核时间</field>
		<field field="mattressid">Mattressid</field>
		<field field="xd_auditingrep">下单人</field>
		<field field="xd_auditingdate" datatype="datetime">下单时间</field>
		<field field="qr_auditingrep">更新人</field>
		<field field="qr_auditingdate" datatype="datetime">最新更新日期</field>
		<field field="erp_cb_updatetime" datatype="datetime">erp成本更新时间</field>
		<field field="dept_profitrate_rangli" datatype="number">部门让利点数</field>
		<field field="fees_dscrp">额外费用说明</field>
		<field field="commission">佣金点数</field>
		<field field="other_rate">额外点数</field>
		<field field="extras_cost">额外费用</field>
		<field field="if_bcp_type" datatype="checkbox">半成品</field>
		<field field="if_w_butao" datatype="checkbox">顶布裥棉</field>
		<field field="biandai_qty" datatype="number">边带数</field>
		<field field="if_m_chai" datatype="checkbox">面拆</field>
		<field field="if_n_butao" datatype="checkbox">内布套</field>
		<field field="s_cover_qty" datatype="number">内布套上覆</field>
		<field field="z_cover_qty" datatype="number">内布套侧覆</field>
		<field field="x_cover_qty" datatype="number">内布套下覆</field>
		<field field="if_m_wbutao_way" datatype="checkbox">面料外布套做法</field>
		<field field="s_m_cover_qty" datatype="number">面层向大侧覆盖</field>
		<field field="z_m_cover_qty" datatype="number">大侧向底层覆盖</field>
		<field field="x_m_cover_qty" datatype="number">底层向大侧覆盖</field>
	</displayfields>
</select>
