<?xml version="1.0"?>

<report name="cash-report" pdf-renderer="Monatsbericht.xsl">
	<title>Tagesabschluss</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="revenue">
	</report-result>
	<report-result report-dataset="drawers">
	</report-result>
	<report-result report-dataset="notes">
	</report-result>
<report-transaction report-nm="pos_payment_statistics" report-transaction="de.timeglobe.pos.reporting.PosPaymentStatistics">
	</report-transaction>
	
<!-- 	<report-result report-dataset="pos_sessions">
	</report-result>
-->
<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 d
			join 	
            	(	SELECT tenant_no , pos_cd , drawer_no  
            	      FROM pos_payments  
            	      where tenant_no=@TENANT_NO and pos_cd=@POS_CD and cast (payment_ts as DATE) &gt;= cast (@DATE as DATE) 
			and cast (payment_ts as DATE) &lt;= cast (@TODATE as DATE)
			          GROUP by tenant_no , pos_cd , drawer_no  
			     )mps
			on mps.tenant_no = d.tenant_no and mps.pos_cd = d.pos_cd and mps.drawer_no = d.drawer_no
			where d.tenant_no=@TENANT_NO and d.pos_cd=@POS_CD
		</sql>
	</report-dataset>


	


	<report-dataset name="revenue">
		<sql>
			select
			 t.dayDt dayDt,
			 t.r_revenue r_revenue,
			 t.r_rebate r_rebate,
			 t.r_revenue_services r_revenue_services,
			 t.r_revenue_products r_revenue_products
			FROM
			(
			select
			(cast (h.sales_inv_ts as DATE)) dayDt,
			coalesce(sum(case when h.sales_inv_type=1 then p.position_gross_price else case when h.sales_inv_type=2 then -p.position_gross_price else 0 end  end),0) r_revenue,
			coalesce(sum(case when h.sales_inv_type=1 then (coalesce(p.applied_pos_rebate_price,0) + coalesce(p.applied_inv_rebate_price,0))  else case when h.sales_inv_type=2 then -(coalesce(p.applied_pos_rebate_price,0) + coalesce(p.applied_inv_rebate_price,0)) else 0 end end),0) r_rebate,
			coalesce(sum(case when coalesce(p.stockable, 0)=0 then case when h.sales_inv_type=1 then p.position_gross_price else  case when h.sales_inv_type=2 then -p.position_gross_price else   0  END   END ELSE 0 end),0) r_revenue_services,
			coalesce(sum(case when coalesce(p.stockable, 0)=1 then case when h.sales_inv_type=1 then p.position_gross_price else  case when h.sales_inv_type=2 then -p.position_gross_price else   0  END   END ELSE 0 end),0)  r_revenue_products
			from sales_inv_positions p
			join sales_invs h on h.tenant_no = p.tenant_no and h.pos_cd = p.pos_cd and h.sales_inv_id = p.sales_inv_id
			where h.tenant_no=@TENANT_NO 
			  AND h.pos_cd=@POS_CD  
			  AND cast (h.sales_inv_ts as DATE) &gt;= cast (@DATE as DATE) 
			  and cast (h.sales_inv_ts as DATE) &lt;= cast (@TODATE as DATE)
			
		
			GROUP BY cast (h.sales_inv_ts as DATE)
			) t
			ORDER BY dayDt ASC
		</sql>
	</report-dataset>

	<report-dataset name="notes">
		<sql>
			select
			(cast (h.sales_inv_ts as DATE)) dayDt,
			coalesce(sum(1),0) r_revenue,
			coalesce(sum(case when  h.cancel_for_sales_inv_id IS Not NULL then  1 else 0 end),0) r_canceled,
			coalesce(sum(case when  h.cancel_for_sales_inv_id IS Not NULL then total_gross_price  else 0 end),0) r_canceled_value,
			coalesce(sum(case when h.sales_inv_type=2  AND h.cancel_for_sales_inv_id IS  NULL then  1 else 0 end),0) r_credit_notes,
			coalesce(sum(case when h.sales_inv_type=2  AND h.cancel_for_sales_inv_id IS  NULL then total_gross_price  else 0 end),0) r_credit_notes_value,
			count(distinct(h.customer_no)) r_cnt_customer
			from sales_invs h
			where 
			      h.tenant_no=@TENANT_NO 
			  AND h.pos_cd=@POS_CD  
			  AND cast (h.sales_inv_ts as DATE) &gt;= cast (@DATE as DATE) 
			  and cast (h.sales_inv_ts as DATE) &lt;= cast (@TODATE as DATE)
			GROUP BY cast (h.sales_inv_ts as DATE)
		</sql>
	</report-dataset>

	

	

	<report-dataset name="pos_sessions">
		<sql>
			select
			(cast (session_end_ts as DATE)) endDt,
			drawer_no t_drawer_no,
			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 session_end_ts IS NOT NULL
			and cast (session_end_ts as DATE) &gt;= cast (@DATE as DATE) 
			and cast (session_end_ts as DATE) &lt;= cast (@TODATE as DATE)
			GROUP BY cast (session_end_ts as DATE),drawer_no
			
		</sql>
	</report-dataset>


</report>
