<?xml version="1.0"?>

<report name="cash-report" pdf-renderer="TagesabschlussSchubladeAlle.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_acct">
	</report-result>

<report-result report-dataset="simple_accounts">
	</report-result>
<report-transaction report-nm="pos_payment_statistics" report-transaction="de.timeglobe.pos.reporting.PosPaymentDrawerStatistics">
	</report-transaction>
<report-result report-dataset="departmentCurrency">
	</report-result>

    <report-dataset name="departmentCurrency">
		<sql>
			select * from currencies c join departments d on c.tenant_no = d.tenant_no AND c.currency_cd = d.currency_cd  where d.tenant_no=@TENANT_NO and d.company_no=@COMPANY_NO and d.department_no =@DEPARTMENT_NO 
		</sql>
	</report-dataset>
	<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="simple_accounts">
		<sql>
			select * from simple_accounts where tenant_no=@TENANT_NO and company_no=@COMPANY_NO AND department_no=@DEPARTMENT_NO
		</sql>
	</report-dataset>

	
	
		<report-dataset name="pos_sessions_acct">
		<sql>
			select
			pp.drawer_no,
			  coalesce(contra_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(cast(payment as decimal(16,2)),0)-coalesce(cast(payment_change as decimal(16,2)),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(cast(payment as decimal(16,2)),0)-coalesce(cast(payment_change as decimal(16,2)),0)) ELSE 0 END ELSE 0 END ELSE 0 END ELSE 0 END) t_acct_cd_cash_out
			  
			
			from pos_payments pp 
			
			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 cast (pp.payment_ts as DATE) = cast (@DATE as DATE)
			  
			  
			GROUP BY  pp.drawer_no, coalesce(pp.contra_acct_cd, '-') 
		</sql>
	</report-dataset>
	
	

</report>
