<?xml version="1.0"?>

<report name="cash-report" pdf-renderer="TagesabschlussSchublade.xsl">
	<title>Tagesabschluss Schublade</title>

	<report-parameter name="TENANT" type="integer" hidden="true">
		<preset>@TENANT_NO</preset>
	</report-parameter>
	<report-parameter name="COMPANY" type="integer" hidden="true">
		<preset>@COMPANY_NO</preset>
	</report-parameter>
	<report-parameter name="DATE" type="date" label="Von Datum:">
		<preset>@TODAY</preset>
		<input type="datepicker" />
	</report-parameter>
	<report-parameter name="BUSINESSUNIT" type="integer">
		<label>Filiale:</label>
		<input type="selectlist">
			<source type="dataset" name="businessunits">
			</source>
		</input>
	</report-parameter>

	<report-result report-dataset="businessunits">
	</report-result>
		<report-result report-dataset="drawers">
	</report-result>
	
		<report-result report-dataset="pos_sessions">
	</report-result>
	<report-result report-dataset="pos_sessions_acct">
	</report-result>
<report-result report-dataset="count_sessions">
	</report-result>
	<report-result report-dataset="cash_start">
	</report-result>
	<report-result report-dataset="cash_end">
	</report-result>


	<report-dataset name="businessunits">
		<sql>
			select * from businessunits where tenant_no=@TENANT_NO and company_no=@COMPANY_NO and department_no=@DEPARTMENT_NO and businessunit_no=@BUSINESSUNIT_NO
		</sql>
	</report-dataset>


	<report-dataset name="drawers">
		<sql>
			select * from pos_drawers where tenant_no=@TENANT_NO and pos_cd=@POS_CD 
		</sql>
	</report-dataset>
	
	<report-dataset name="pos_sessions">
		<sql>
			select
			coalesce(sum(cash_in),0) t_cash_in,
			coalesce(sum(cash_out),0) t_cash_out,
			coalesce(sum(cash_sales_in-cash_sales_return_out),0) t_cash_sales_in,
			coalesce(sum(cash_purchase_out-cash_purchase_return_in),0) t_cash_purchase_out,
			coalesce(sum(cash_debt_out),0) t_cash_debt_out,
			coalesce(sum(cash_debt_in),0) t_cash_debt_in,
			coalesce(sum(ec_sales_in),0) t_ec_sales_in,
			coalesce(sum(cc_sales_in),0) t_cc_sales_in,
			coalesce(sum(transfer_sales_in),0) t_transfer_sales_in,
			coalesce(sum(transfer_purchase_out),0) t_transfer_purchase_out,
			coalesce(sum(creditnote_sales_in),0) t_creditnote_sales_in,
			coalesce(sum(creditnote_sales_out),0) t_creditnote_sales_out,
			coalesce(sum(discount_sales_out),0) t_discount_sales_out,
			coalesce(sum(voucher_sales_in),0) t_voucher_sales_in,
			coalesce(sum(voucher_out),0) t_voucher_out
			from pos_sessions 
			
			where tenant_no=@TENANT_NO
			and pos_cd=@POS_CD
			and drawer_no = @DRAWER_NO 
			and session_end_ts IS NOT NULL
			and cast (session_end_ts as DATE) = cast (@DATE as DATE)
			
		</sql>
	</report-dataset>
	
	
		<report-dataset name="pos_sessions_acct">
		<sql>
			select
			  coalesce(contra_acct_cd, pd.acct_cd) contra_acct_cd,
			  sum(case when payment_type = 1 THEN CASE WHEN sales_inv_id IS NULL THEN CASE WHEN purchase_inv_id IS NULL THEN CASE WHEN payment_direction = 1 THEN (coalesce(payment,0)-coalesce(payment_change,0)) ELSE 0 END ELSE 0 END ELSE 0 END ELSE 0 END) t_acct_cd_cash_in,
			  sum(case when payment_type = 1 THEN CASE WHEN sales_inv_id IS NULL THEN CASE WHEN purchase_inv_id IS NULL THEN CASE WHEN payment_direction = 2 THEN (coalesce(payment,0)-coalesce(payment_change,0)) ELSE 0 END ELSE 0 END ELSE 0 END ELSE 0 END) t_acct_cd_cash_out
			  
			
			from pos_payments pp 
			join pos_sessions ps 
			  ON pp.tenant_no = ps.tenant_no
			      AND pp.pos_cd = ps.pos_cd
			      AND pp.drawer_no = ps.drawer_no
			      AND pp.pos_session_id = ps.pos_session_id
			left join pos_drawers pd
			       ON pp.tenant_no = pd.tenant_no
			      AND pp.pos_cd = pd.pos_cd
			      AND pp.drawer_no = pd.drawer_no
				where pp.tenant_no=@TENANT_NO
			      and pp.pos_cd=@POS_CD
			      and pp.drawer_no = @DRAWER_NO 
			      and ps.session_end_ts IS NOT NULL
			      and cast (ps.session_end_ts as DATE) = cast (@DATE as DATE)
			  
			  
			GROUP BY  coalesce(pp.contra_acct_cd, pd.acct_cd) 
		</sql>
	</report-dataset>
	
	
	<report-dataset name="cash_start">
		<sql>
		
		SELECT ss.drawer_no, ss.session_start_ts, coalesce(ss.cash_start,0) t_cash_start
	      	from pos_sessions ss join 	
            	(	SELECT tenant_no , pos_cd , drawer_no  ,min(session_start_ts) m_start_ts 
            	      FROM pos_sessions  
            	      where tenant_no=@TENANT_NO and pos_cd=@POS_CD and drawer_no = @DRAWER_NO  and session_end_ts IS NOT NULL and cast
		                 	(session_end_ts as DATE) = cast (@DATE as DATE) 
			          GROUP by tenant_no , pos_cd , drawer_no  
			     )mps
			on mps.tenant_no = ss.tenant_no and mps.pos_cd = ss.pos_cd and mps.drawer_no = ss.drawer_no AND mps.m_start_ts = ss.session_start_ts
			
		
		</sql>
	</report-dataset>
	
	
	<report-dataset name="cash_end">
		<sql>
		
		SELECT ss.drawer_no, ss.session_end_ts,  coalesce(ss.cash_end ,0) t_cash_end
	      	from pos_sessions ss join 	
            	(	SELECT tenant_no , pos_cd , drawer_no  ,max(session_end_ts) m_end_ts 
            	      FROM pos_sessions  
            	      where tenant_no=@TENANT_NO and pos_cd=@POS_CD and drawer_no = @DRAWER_NO  and session_end_ts IS NOT NULL and cast
		                 	(session_end_ts as DATE) = cast (@DATE as DATE) 
			          GROUP by tenant_no , pos_cd , drawer_no  
			     )mps
			on mps.tenant_no = ss.tenant_no and mps.pos_cd = ss.pos_cd and mps.drawer_no = ss.drawer_no AND mps.m_end_ts = ss.session_end_ts
		
			
		</sql>
	</report-dataset>
	
		<report-dataset name="count_sessions">
		<sql>
		
		SELECT coalesce( sum(case when ss.session_end_ts is null then 0 else 1 end ),0) t_cnt_sessions_end from pos_sessions ss where ss.tenant_no=@TENANT_NO and ss.pos_cd=@POS_CD and drawer_no = @DRAWER_NO  and session_end_ts IS NOT NULL
			and cast (session_end_ts as DATE) = cast (@DATE as DATE)
		
			
		</sql>
	</report-dataset>


<report-dataset name="cash_startold">
		<sql>
		
		
			
		
			SELECT coalesce(sum(ss.cash_start),0) t_cash_start , COUNT(pos_session_id) t_cnt_sessions_start from pos_sessions ss  where ss.tenant_no=@TENANT_NO and ss.pos_cd=@POS_CD and drawer_no = @DRAWER_NO  and session_end_ts IS NOT NULL
			and cast (session_end_ts as DATE) = cast (@DATE as DATE)
		</sql>
	</report-dataset>

<report-dataset name="cash_endOld">
		<sql>
		
	
		
			SELECT coalesce(sum(case when ss.session_end_ts is null then 0 else ss.cash_end end),0)  t_cash_end, sum(case when ss.session_end_ts is null then 0 else 1 end ) t_cnt_sessions_end from pos_sessions ss where ss.tenant_no=@TENANT_NO and ss.pos_cd=@POS_CD and drawer_no = @DRAWER_NO  and session_end_ts IS NOT NULL
			and cast (session_end_ts as DATE) = cast (@DATE as DATE)
		</sql>
	</report-dataset>



</report>
