﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="3fbd3243-01aa-49c9-a286-5b50cbce166f" codekey="TrainingCosts" categoryCodekey="Trainings" name="Seminarkosten (Multimandator)" description="">
      <MetaData created="2012-01-13T12:20:23" createdBy="VIWIS" createdBy_user_id="3" modified="2015-03-18T12:30:29" modifiedBy="Gabi Wappler" />
      <ExecutionDetails format="KeyValueResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="AdminAnzahlNutzerProThema" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="cf85efc9-150b-4eee-8c20-929a112b658c" mandatorName="ADAC" isStandard="False" isUsedByMenu="True" />
      <Parameters>
        <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isRequired="False" name="Zielgruppe" contextName="Zielgruppe" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="aeee15e5-5f10-4de2-8255-05effbce4991" isRequired="False" name="Geschäftsstelle" contextName="Bereich" defaultValue="" renderHint="DropDownList" disableParameter="DontDisable" />
        <Parameter id="ea936f9e-6001-4252-a186-1f47854eeab6" isRequired="False" name="Seminartitel abhängig von Zielgruppen multimandator" contextName="Seminartitel" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="c67f946f-ad8b-4183-a649-ea8e826d8e10" isRequired="False" name="Seminartermin" contextName="Seminartermin" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a57b48c0-6f41-49a6-85a4-eadc5603a0cd" isRequired="True" name="Kostenart" contextName="Kostenart" defaultValue="0" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="03fe41ae-e014-4f1c-9a2c-04c1905b7319" isRequired="True" name="Kosten" contextName="Kosten" defaultValue="0" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isRequired="False" name="Zeitraum" contextName="Zeitraum" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="90" />
      </Roles>
      <command>
        -- This report works for Seminarkosten as well as for Seminarkosten Teilnehmer

        -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        -- MandatorSpecificVersion (make sure to keep in sync): ../TrainingCosts.xml
        -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        -- Because the paramters for both reports are different, you have to
        -- used different predefined paramters sets. Uncomment the correct set below

        -- Seminarkosten:

        declare @user_id int
        -- will always be null for "Seminarkosten" and further is the switch to distiguish between the two
        -- report type
        set @user_id = null

        -- Variables used for debugging "Seminarkosten"
        /*
        declare @current_mandator_id uniqueidentifier
        select @current_mandator_id=id from tblMandators where name='viwis'

        declare @targetGroup_id int
        set @targetGroup_id = null

        declare @division nvarchar(128)
        set @division='alle'

        declare @trainingTitle_id uniqueidentifier
        set @trainingTitle_id = null

        declare @training_id uniqueidentifier
        set @training_id = null

        declare @costType_id int
        set @costType_id = null

        declare @cost_id int
        set @cost_id = null

        declare @dateRange_start datetime
        set @dateRange_start = '2010-03-01'

        declare @dateRange_end datetime
        set @dateRange_end = '2010-03-01'
        */

        /*
        ---------------------------
        -- Seminarkosten Teilnehmer
        ---------------------------

        declare @targetGroup_id int
        set @targetGroup_id = null

        declare @division nvarchar(128)
        set @division='alle'

        declare @cost_id int
        set @cost_id = 0

        -- Variables used for debugging "Seminarkosten Teilnehmer"

        declare @user_id int
        set @user_id =2

        declare @current_mandator_id uniqueidentifier
        select @current_mandator_id=id from tblMandators where name='viwis'

        declare @trainingTitle_id uniqueidentifier
        set @trainingTitle_id = null

        declare @training_id uniqueidentifier
        set @training_id = null

        declare @costType_id int
        set @costType_id = null

        declare @dateRange_start datetime
        set @dateRange_start = '2010-03-01'

        declare @dateRange_end datetime
        set @dateRange_end = '2011-03-01'
        */
        -- The report without debugging information starts here

        if ( @division is null ) set @division=''
        if ( @division='alle' ) set @division=null

        declare @trainingEvent_id uniqueidentifier
        set @trainingEvent_id = @training_id

        if (@dateRange_start is not null)
        set @dateRange_start = cast(convert(NVARCHAR(32), @dateRange_start, 112) as datetime)

        if ( @dateRange_end is not null )
        begin
        set @dateRange_end  = cast(convert(NVARCHAR(32), @dateRange_end, 112) as datetime)
        set @dateRange_end = dateadd(day, 1, @dateRange_end)
        set @dateRange_end = dateadd(second, -1, @dateRange_end)
        end

        begin transaction

        -- create a list of trainingTitle_ids, trainingEvent_ids which must be taken into account
        create table #tempTrainingTitles
        (
        trainingTitle_id uniqueidentifier,
        trainingEvent_id uniqueidentifier,
        trainingTimeInDays int,
        countUsers int,
        countAllUsers int,
        CostExpensesFull decimal(18,2),
        CostExpensesPartial decimal(18,2),
        CostTravelFull decimal(18,2),
        CostTravelPartial decimal(18,2),
        CostTravelNoTax decimal(18,2),
        CostConferenceUser1 decimal(18,2),
        CostConferenceUser2 decimal(18,2),
        CostSeminar decimal(18,2),
        CostCancelation decimal (18,2)
        )

        if ( @user_id is not null )
        begin
        -- Seminarkosten Teilnehmer
        insert into #tempTrainingTitles
        select
        distinct(tblTrainings.trainingTitle_id) as 'trainingTitle_id',
        tblTrainings.id as 'trainingEvent_id',
        0 as trainingTimeInDays,
        count(case when tblUserTrainingStatus.actionclass_id&gt;=70 then tblUserTrainingStatus.user_id else null end) as 'countUsers',
        0 as 'countAllUsers',
        sum(v_TrainingEventCostsPerUser.CostExpensesFull) as 'CostExpensesFull',
        sum(v_TrainingEventCostsPerUser.CostExpensesPartial) as 'CostExpensesPartial',
        sum(v_TrainingEventCostsPerUser.CostTravelFull) as 'CostTravelFull',
        sum(v_TrainingEventCostsPerUser.CostTravelPartial) as 'CostTravelPartial',
        sum(v_TrainingEventCostsPerUser.CostTravelNoTax) as 'CostTravelNoTax',
        sum(v_TrainingEventCostsPerUser.CostConferenceUser1) as 'CostConferenceUser1',
        sum(v_TrainingEventCostsPerUser.CostConferenceUser2) as 'CostConferenceUser2',
        sum(v_TrainingEventCostsPerUser.CostSeminar) as 'CostSeminar',
        sum(v_TrainingEventCostsPerUser.CostCancelation) as 'CostCancelation'

        from tblUserTrainingStatus
        inner join tblTrainings on tblTrainings.id=tblUserTrainingStatus.training_id

        inner join v_TrainingEventCostsPerUser on v_TrainingEventCostsPerUser.user_id=tblUserTrainingStatus.user_id and v_TrainingEventCostsPerUser.trainingEvent_id=tblTrainings.id

        where
        tblTrainings.isClosed!=0
        and tblUserTrainingStatus.user_id=@user_id
        and (@trainingTitle_id is null or tblTrainings.trainingTitle_id=@trainingTitle_id)
        and (@trainingEvent_id is null or tblTrainings.id=@trainingEvent_id)
        and (@dateRange_start is null or tblTrainings.startDate&gt;=@dateRange_start)
        and (@dateRange_end is null or tblTrainings.endDate&lt;=@dateRange_end)

        group by tblTrainings.trainingTitle_id, tblTrainings.id

        end
        else
        begin
        -- Seminarkosten
        if ( @targetGroup_id is not null )
        begin
        -- only for users which are in the given target group

        insert into #tempTrainingTitles
        select
        distinct(tblTrainings.trainingTitle_id) as 'trainingTitle_id',
        tblTrainings.id as 'trainingEvent_id',
        0 as trainingTimeInDays,
        count(case when tblUserTrainingStatus.actionclass_id&gt;=70 then tblUserTrainingStatus.user_id else null end) as 'countUsers',
        0 as 'countAllUsers',
        sum(v_TrainingEventCostsPerUser.CostExpensesFull) as 'CostExpensesFull',
        sum(v_TrainingEventCostsPerUser.CostExpensesPartial) as 'CostExpensesPartial',
        sum(v_TrainingEventCostsPerUser.CostTravelFull) as 'CostTravelFull',
        sum(v_TrainingEventCostsPerUser.CostTravelPartial) as 'CostTravelPartial',
        sum(v_TrainingEventCostsPerUser.CostTravelNoTax) as 'CostTravelNoTax',
        sum(v_TrainingEventCostsPerUser.CostConferenceUser1) as 'CostConferenceUser1',
        sum(v_TrainingEventCostsPerUser.CostConferenceUser2) as 'CostConferenceUser2',
        sum(v_TrainingEventCostsPerUser.CostSeminar) as 'CostSeminar',
        sum(v_TrainingEventCostsPerUser.CostCancelation) as 'CostCancelation'

        from tblUserTrainingStatus
        inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
        inner join tblUsersTargetGroups on tblUsersTargetGroups.UserCn=tblUserTrainingStatus.user_id -- and tblUserTrainingStatus.actionclass_id=6
        inner join tblTrainings on tblTrainings.id=tblUserTrainingStatus.training_id

        inner join v_TrainingEventCostsPerUser on v_TrainingEventCostsPerUser.user_id=tblUserTrainingStatus.user_id and v_TrainingEventCostsPerUser.trainingEvent_id=tblTrainings.id

        where
        tblTrainings.isClosed!=0
        and tblUsersTargetGroups.targetGroup_id=@targetGroup_id
        and (@division is null or v_Users.division=@division or (@division='' and v_Users.division is null))
        and (@trainingTitle_id is null or tblTrainings.trainingTitle_id=@trainingTitle_id)
        and (@trainingEvent_id is null or tblTrainings.id=@trainingEvent_id)
        and (@dateRange_start is null or tblTrainings.startDate&gt;=@dateRange_start)
        and (@dateRange_end is null or tblTrainings.endDate&lt;=@dateRange_end)

        group by tblTrainings.trainingTitle_id, tblTrainings.id
        end
        else
        begin
        -- for all users of a the current mandator

        insert into #tempTrainingTitles
        select
        distinct(tblTrainings.trainingTitle_id) as 'trainingTitle_id',
        tblTrainings.id as 'trainingEvent_id',
        0 as trainingTimeInDays,
        count(case when tblUserTrainingStatus.actionclass_id&gt;=70 then tblUserTrainingStatus.user_id else null end) as 'countUsers',
        0 as 'countAllUsers',
        sum(v_TrainingEventCostsPerUser.CostExpensesFull) as 'CostExpensesFull',
        sum(v_TrainingEventCostsPerUser.CostExpensesPartial) as 'CostExpensesPartial',
        sum(v_TrainingEventCostsPerUser.CostTravelFull) as 'CostTravelFull',
        sum(v_TrainingEventCostsPerUser.CostTravelPartial) as 'CostTravelPartial',
        sum(v_TrainingEventCostsPerUser.CostTravelNoTax) as 'CostTravelNoTax',
        sum(v_TrainingEventCostsPerUser.CostConferenceUser1) as 'CostConferenceUser1',
        sum(v_TrainingEventCostsPerUser.CostConferenceUser2) as 'CostConferenceUser2',
        sum(v_TrainingEventCostsPerUser.CostSeminar) as 'CostSeminar',
        sum(v_TrainingEventCostsPerUser.CostCancelation) as 'CostCancelation'

        from tblUserTrainingStatus
        inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
        inner join tblTrainings on tblTrainings.id=tblUserTrainingStatus.training_id
        inner join v_TrainingEventCostsPerUser on v_TrainingEventCostsPerUser.user_id=tblUserTrainingStatus.user_id and v_TrainingEventCostsPerUser.trainingEvent_id=tblTrainings.id

        where
        tblTrainings.isClosed!=0
        and tblTrainings.mandator_id=@current_mandator_id
        and (@division is null or v_Users.division=@division or (@division='' and v_Users.division is null))
        and (@trainingTitle_id is null or tblTrainings.trainingTitle_id=@trainingTitle_id)
        and (@trainingEvent_id is null or tblTrainings.id=@trainingEvent_id)
        and (@dateRange_start is null or tblTrainings.startDate&gt;=@dateRange_start)
        and (@dateRange_end is null or tblTrainings.endDate&lt;=@dateRange_end)

        group by tblTrainings.trainingTitle_id, tblTrainings.id
        end
        end

        update #tempTrainingTitles
        set
        #tempTrainingTitles.countAllUsers=(select COUNT(*) from tblUserTrainingStatus where tblUserTrainingStatus.training_id=#tempTrainingTitles.trainingEvent_id  and tblUserTrainingStatus.actionclass_id&gt;=70),
        #tempTrainingTitles.trainingTimeInDays = (select datediff(day, tblTrainings.startDate, tblTrainings.endDate)+1 from tblTrainings where tblTrainings.id=#tempTrainingTitles.trainingEvent_id)

        -- select * from #tempTrainingTitles

        select #tempTrainingTitles.*,

        convert(decimal(18,2),
        (select amount from tblTrainingEventCosts where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=1) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'CostTrainer',

        convert(decimal(18,2),
        (select amount from tblTrainingEventCosts where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=2) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'CostRentAndTechnical',

        convert(decimal(18,2),
        (select SUM(amount) from tblTrainingEventCosts where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=22) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'RoomCosts',

        convert(decimal(18,2),
        (select amount from tblTrainingEventCosts where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=3) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'CostConference'

        into #tempResults
        from #tempTrainingTitles

        drop table #tempTrainingTitles

        update #tempResults set CostTrainer=0 where CostTrainer is null
        update #tempResults set CostRentAndTechnical=0 where CostRentAndTechnical is null
        update #tempResults set RoomCosts=0 where RoomCosts is null
        update #tempResults set CostConference=0 where CostConference is null

        update #tempResults set CostExpensesFull=0 where CostExpensesFull is null
        update #tempResults set CostExpensesPartial=0 where CostExpensesPartial is null
        update #tempResults set CostTravelFull=0 where CostTravelFull is null
        update #tempResults set CostTravelPartial=0 where CostTravelPartial is null
        update #tempResults set CostTravelNoTax=0 where CostTravelNoTax is null
        update #tempResults set CostConferenceUser1=0 where CostConferenceUser1 is null
        update #tempResults set CostConferenceUser2=0 where CostConferenceUser2 is null
        update #tempResults set CostSeminar=0 where CostSeminar is null
        update #tempResults set CostCancelation=0 where CostCancelation is null

        -- select * from #tempResults

        select
        sum(CostTrainer) as 'CostTrainer',
        sum(CostRentAndTechnical) as 'CostRentAndTechnical',
        sum(RoomCosts) as 'RoomCosts',
        sum(CostConference) as 'CostConference',

        sum(CostExpensesFull) as 'CostExpensesFull',
        sum(CostExpensesPartial) as 'CostExpensesPartial',
        sum(CostTravelFull) as 'CostTravelFull',
        sum(CostTravelPartial) as 'CostTravelPartial',
        sum(CostTravelNoTax) as 'CostTravelNoTax',
        sum(CostConferenceUser1) as 'CostConferenceUser1',
        sum(CostConferenceUser2) as 'CostConferenceUser2',
        sum(CostSeminar) as 'CostSeminar',
        sum(CostCancelation) as 'CostCancelation',
        sum(trainingTimeInDays) as 'TotalTrainingDays',
        sum(countUsers) as 'TotalUsers'
        into #tempResults2
        from #tempResults

        drop table #tempResults

        -- set null values to zero, helps a lot in upcomming calculations

        update #tempResults2 set CostTrainer=0 where CostTrainer is null
        update #tempResults2 set CostRentAndTechnical=0 where CostRentAndTechnical is null
        update #tempResults2 set RoomCosts=0 where RoomCosts is null
        update #tempResults2 set CostConference=0 where CostConference is null

        update #tempResults2 set CostExpensesFull=0 where CostExpensesFull is null
        update #tempResults2 set CostExpensesPartial=0 where CostExpensesPartial is null
        update #tempResults2 set CostTravelFull=0 where CostTravelFull is null
        update #tempResults2 set CostTravelPartial=0 where CostTravelPartial is null
        update #tempResults2 set CostTravelNoTax=0 where CostTravelNoTax is null
        update #tempResults2 set CostConferenceUser1=0 where CostConferenceUser1 is null
        update #tempResults2 set CostConferenceUser2=0 where CostConferenceUser2 is null
        update #tempResults2 set CostSeminar=0 where CostSeminar is null
        update #tempResults2 set CostCancelation=0 where CostCancelation is null
        update #tempResults2 set TotalTrainingDays=0 where TotalTrainingDays is null
        update #tempResults2 set TotalUsers=0 where TotalUsers is null

        declare @totalTrainingDays int
        declare @totalUsers int

        select
        @totalTrainingDays=totalTrainingDays,
        @totalUsers = totalUsers
        from
        #tempResults2

        if ( @targetGroup_id is null )
        begin
        -- add the costs for trainings which don't have any user
        -- only meaningfull if there is no target group

        select tblTrainingEventCosts.trainingEventCostType_id, sum(tblTrainingEventCosts.amount) as amount
        into #costsOfEventdsWithNoTrainigs
        from tblTrainings
        inner join tblTrainingEventCosts on tblTrainingEventCosts.trainingEvent_id=tblTrainings.id and tblTrainingEventCosts.user_id is null
        where
        tblTrainings.mandator_id=@current_mandator_id
        and (@trainingTitle_id is null or tblTrainings.trainingTitle_id=@trainingTitle_id)
        and (@trainingEvent_id is null or tblTrainings.id=@trainingEvent_id)
        and (@dateRange_start is null or tblTrainings.startDate&gt;=@dateRange_start)
        and (@dateRange_end is null or tblTrainings.endDate&lt;=@dateRange_end)
        and not exists(select * from tblUserTrainingStatus where tblUserTrainingStatus.training_id=tblTrainings.id)
        group by tblTrainingEventCosts.trainingEventCostType_id

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=1) )
        begin
        update #tempResults2 set CostTrainer=0 where CostTrainer is null
        update #tempResults2 set CostTrainer = CostTrainer + (select amount from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=1)
        end

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=2) )
        begin
        update #tempResults2 set CostRentAndTechnical=0 where CostRentAndTechnical is null
        update #tempResults2 set CostRentAndTechnical = CostRentAndTechnical + (select amount from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=2)
        end

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=22) )
        begin
        update #tempResults2 set RoomCosts=0 where RoomCosts is null
        update #tempResults2 set RoomCosts = RoomCosts + (select SUM(amount) from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=22)
        end

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=3) )
        begin
        update #tempResults2 set CostConference=0 where CostConference is null
        update #tempResults2 set CostConference = CostConference + (select amount from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=3)
        end

        drop table #costsOfEventdsWithNoTrainigs
        end

        -- select * from #tempResults2

        select
        convert(nvarchar, GETDATE(), 104) as 'ReportDate',
        CostTrainer + CostRentAndTechnical + RoomCosts + CostConference + CostExpensesFull + CostExpensesPartial + CostTravelFull + CostTravelPartial + CostTravelNoTax + CostConferenceUser1 + CostConferenceUser2 + CostSeminar + CostCancelation as 'SumAll',
        CostTrainer + CostRentAndTechnical + RoomCosts + CostConference as 'SumGeneral',
        CostTrainer,
        CostRentAndTechnical,
        RoomCosts,
        CostConference,
        CostExpensesFull + CostExpensesPartial + CostTravelFull + CostTravelPartial + CostTravelNoTax + CostConferenceUser1 + CostConferenceUser2 + CostSeminar + CostCancelation as 'SumDirect',
        CostExpensesFull,
        CostExpensesPartial,
        CostTravelFull,
        CostTravelPartial,
        CostTravelNoTax,
        CostConferenceUser1,
        CostConferenceUser2,
        CostSeminar,
        CostCancelation
        into
        #tempResult3
        from
        #tempResults2

        drop table #tempResults2

        -- in case we don't have any data this looks much better
        update #tempResult3 set SumAll=0 where SumAll is null
        update #tempResult3 set SumGeneral=0 where SumGeneral is null
        update #tempResult3 set SumDirect=0 where SumDirect is null

        if ( @cost_id=1 )
        begin
        -- Pro Kopf
        if ( @totalUsers&gt;0 )
        update #tempResult3 set
        SumAll = SumAll / @totalUsers,

        SumGeneral = SumGeneral / @totalUsers,
        CostTrainer  = CostTrainer / @totalUsers,
        CostRentAndTechnical  = CostRentAndTechnical / @totalUsers,
        RoomCosts = RoomCosts / @totalUsers,
        CostConference = CostConference / @totalUsers,

        SumDirect = SumDirect / @totalUsers,
        CostExpensesFull = CostExpensesFull / @totalUsers,
        CostExpensesPartial = CostExpensesPartial / @totalUsers,
        CostTravelFull = CostTravelFull / @totalUsers,
        CostTravelPartial = CostTravelPartial / @totalUsers,
        CostTravelNoTax = CostTravelNoTax / @totalUsers,
        CostConferenceUser1 = CostConferenceUser1 / @totalUsers,
        CostConferenceUser2 = CostConferenceUser2 / @totalUsers,
        CostSeminar = CostSeminar / @totalUsers,
        CostCancelation = CostCancelation / @totalUsers
        else
        update #tempResult3 set
        SumAll = 0,

        SumGeneral = 0,
        CostTrainer  = 0,
        CostRentAndTechnical  = 0,
        RoomCosts = 0,
        CostConference = 0,

        SumDirect = 0,
        CostExpensesFull = 0,
        CostExpensesPartial = 0,
        CostTravelFull = 0,
        CostTravelPartial = 0,
        CostTravelNoTax = 0,
        CostConferenceUser1 = 0,
        CostConferenceUser2 = 0,
        CostSeminar = 0,
        CostCancelation = 0
        end
        else if (@cost_id=2 )
        begin
        -- Pro Seminartag
        if ( @totalTrainingDays&gt;0 )
        update #tempResult3 set
        SumAll = SumAll / @totalTrainingDays,

        SumGeneral = SumGeneral / @totalTrainingDays,
        CostTrainer  = CostTrainer / @totalTrainingDays,
        CostRentAndTechnical  = CostRentAndTechnical / @totalTrainingDays,
        RoomCosts = RoomCosts / @totalTrainingDays,
        CostConference = CostConference / @totalTrainingDays,

        SumDirect = SumDirect / @totalTrainingDays,
        CostExpensesFull = CostExpensesFull / @totalTrainingDays,
        CostExpensesPartial = CostExpensesPartial / @totalTrainingDays,
        CostTravelFull = CostTravelFull / @totalTrainingDays,
        CostTravelPartial = CostTravelPartial / @totalTrainingDays,
        CostTravelNoTax = CostTravelNoTax / @totalTrainingDays,
        CostConferenceUser1 = CostConferenceUser1 / @totalTrainingDays,
        CostConferenceUser2 = CostConferenceUser2 / @totalTrainingDays,
        CostSeminar = CostSeminar / @totalTrainingDays,
        CostCancelation = CostCancelation / @totalTrainingDays
        else
        update #tempResult3 set
        SumAll = 0,

        SumGeneral = 0,
        CostTrainer  = 0,
        CostRentAndTechnical  = 0,
        RoomCosts = 0,
        CostConference = 0,

        SumDirect = 0,
        CostExpensesFull = 0,
        CostExpensesPartial = 0,
        CostTravelFull = 0,
        CostTravelPartial = 0,
        CostTravelNoTax = 0,
        CostConferenceUser1 = 0,
        CostConferenceUser2 = 0,
        CostSeminar = 0,
        CostCancelation = 0
        end

        if  ( @costType_id=1 )
        begin
        -- Allgemeine Kosten
        update #tempResult3 set
        SumAll = null,

        SumDirect = null,
        CostExpensesFull = null,
        CostExpensesPartial = null,
        CostTravelFull = null,
        CostTravelPartial = null,
        CostTravelNoTax = null,
        CostConferenceUser1 = null,
        CostConferenceUser2 = null,
        CostSeminar = null,
        CostCancelation = null
        end
        else if ( @costType_id=2 )
        begin
        -- Direkte Kosten
        update #tempResult3 set
        SumAll = null,

        SumGeneral = null,
        CostTrainer  = null,
        CostRentAndTechnical  = null,
        RoomCosts = null,
        CostConference = null
        end

        /*
        else -- if (@costType_id=0)
        begin
        -- Gesamtkosten
        update #tempResult3 set

        SumDirect = null,
        CostExpensesFull = null,
        CostExpensesPartial = null,
        CostTravelFull = null,
        CostTravelPartial = null,
        CostTravelNoTax = null,
        CostConferenceUser1 = null,
        CostConferenceUser2 = null,
        CostSeminar = null,
        CostCancelation = null,

        SumGeneral = null,
        CostTrainer  = null,
        CostRentAndTechnical  = null,
        RoomCosts = null,
        CostConference = null
        end
        */

        select
        ReportDate as 'Auswertung vom',
        SumAll as 'Gesamtkosten',
        SumGeneral as 'Allgemeine Kosten',
        CostTrainer as 'Trainerkosten',
        CostRentAndTechnical as 'Raummiete/Technik',
        RoomCosts as 'Raumkosten',
        CostConference as 'Tagungspauschale',

        SumDirect as 'Direkte Kosten',
        CostExpensesFull as 'ÜN-Kosten (19%)',
        CostExpensesPartial as 'ÜN-Kosten (7%)',
        CostTravelFull as 'RK-Kosten (19%)',
        CostTravelPartial as 'RK-Kosten (7%)',
        CostTravelNoTax as 'RK-Kosten (ohne Steuer)',
        CostConferenceUser1 as 'Tagungspauschale_User1',
        CostConferenceUser2 as 'Tagungspauschale_User2',
        CostSeminar as 'Seminarkosten',
        CostCancelation as 'Stornokosten'
        from
        #tempResult3

        drop table #tempResult3

        rollback transaction

        -- Formatting starts here


        create table #$formating_0
        (
        tr nvarchar(128),
        col0f  nvarchar(128),
        col0v nvarchar(128),
        col1f  nvarchar(128),
        col1v nvarchar(128),
        col2f  nvarchar(128),
        col2v nvarchar(128),
        col3f  nvarchar(128),
        col3v nvarchar(128),
        sortOrder int
        )

        insert into #$formating_0
        select 'format info', 'cellspacing="4"', '', '', '', '', '', '', '', -1

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '{key_0}', 'colspan="2" style="border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_0}', '', '&amp;nbsp', '', '', 0

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="font-weight:bold"', '{key_1}', 'colspan="2" style="border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_1}', '', 'Euro', '', '', 1

        insert into #$formating_0
        select 'class="AdminGeneral"', 'colspan=4', '&amp;nbsp;', '', '', '', '', '', '', 2

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="font-weight:bold"', '{key_2}', 'colspan="2" style="border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_2}', '', 'Euro', '', '', 3

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_3}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_3}', '', 'Euro', 4

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_4}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_4}', '', 'Euro', 5

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_5}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_5}', '', 'Euro', 6

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_6}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_6}', '', 'Euro', 7

        insert into #$formating_0
        select 'class="AdminGeneral"', 'colspan=4', '&amp;nbsp;', '', '', '', '', '', '', 8

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="font-weight:bold"', '{key_7}', 'colspan="2" style="border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_7}', '', 'Euro', '', '', 9

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_8}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_8}', '', 'Euro', 10

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_9}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_9}', '', 'Euro', 11

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_10}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_10}', '', 'Euro', 12

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_11}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_11}', '', 'Euro', 13

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_12}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_12}', '', 'Euro', 14

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', 'Tagungspauschale 1', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_13}', '', 'Euro', 15

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', 'Tagungspauschale 2', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_14}', '', 'Euro', 16

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_15}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_15}', '', 'Euro', 17

        insert into #$formating_0
        select 'class="AdminGeneral"', '', '&amp;nbsp;', 'style="width:150px"', '{key_16}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_16}', '', 'Euro', 18

        select * from #$formating_0 order by sortOrder
        drop table #$formating_0
      </command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isSystem="True" name="Zielgruppe" reportParameterType_id="4123c2c2-d408-476a-82bf-ca77f9ecf944" queryParameterName="@targetGroup_id" />
    <Parameter id="aeee15e5-5f10-4de2-8255-05effbce4991" isSystem="False" name="Geschäftsstelle" reportParameterType_id="ce7d19c1-b072-4a3a-b335-1d4cc2b940db" queryParameterName="@division" />
    <Parameter id="ea936f9e-6001-4252-a186-1f47854eeab6" isSystem="False" name="Seminartitel abhängig von Zielgruppen multimandator" reportParameterType_id="13f8c60d-7acd-4599-b31d-f996199e311c" queryParameterName="@trainingTitle_id" />
    <Parameter id="c67f946f-ad8b-4183-a649-ea8e826d8e10" isSystem="False" name="Seminartermin" reportParameterType_id="f22db1dd-65f9-4b99-a45e-ec63268de8d6" queryParameterName="@training_id" />
    <Parameter id="a57b48c0-6f41-49a6-85a4-eadc5603a0cd" isSystem="False" name="Kostenart" reportParameterType_id="a5255b20-40a0-4cbd-a664-0926299c512f" queryParameterName="@costType_id" />
    <Parameter id="03fe41ae-e014-4f1c-9a2c-04c1905b7319" isSystem="False" name="Kosten" reportParameterType_id="6036b338-5f89-4d19-b3b6-a7d247a480ca" queryParameterName="@cost_id" />
    <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isSystem="True" name="Zeitraum" reportParameterType_id="abff13be-91c3-4ee1-93a3-7292f8e013ba" queryParameterName="@dateRange" />
    <Parameter id="8eacbb69-0b54-4785-9223-3343af2b7ae6" isSystem="True" name="Seminartitel" reportParameterType_id="c4fa1006-96a9-4b85-bf22-6a480289e5f3" queryParameterName="@trainingTitle_id" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="4123c2c2-d408-476a-82bf-ca77f9ecf944" isSystem="False" name="TargetGroup" datatype="IntegerDDL" dataValueField="id" dataTextField="title">
      <query>
        SELECT id, title
        FROM tblTargetGroups
        WHERE(mandator_id = @current_mandator_id)
        AND (EXISTS(SELECT * FROM v_Users WHERE intUserCn = @current_user_id AND securityId = 100)
        OR EXISTS(SELECT * FROM tblTargetGroupManagers WHERE tblTargetGroupManagers.user_id = @current_user_id
                  AND tblTargetGroupManagers.targetGroup_id = tblTargetGroups.id)
        OR EXISTS(SELECT *
                  FROM tblRolesFunctions
                  JOIN tblRoles ON tblRoles.id = tblRolesFunctions.role_id
                  JOIN tblFunctions ON tblFunctions.id = tblRolesFunctions.function_id
                  JOIN v_Users ON v_Users.intUserCn = @current_user_id
                  WHERE tblFunctions.shortcut = 'FcnIsAuthorizedForAllTGs' AND tblRoles.id = v_Users.SecurityID))
        AND defaultforlibrary = 0
        ORDER BY title
      </query>
    </ParameterType>
    <ParameterType id="ce7d19c1-b072-4a3a-b335-1d4cc2b940db" isSystem="False" name="Division" datatype="StringDDL" dataValueField="division" dataTextField="division">
      <query>select distinct(division) as division from v_Users where mandator_id=@current_mandator_id and division is not null</query>
    </ParameterType>
    <ParameterType id="13f8c60d-7acd-4599-b31d-f996199e311c" isSystem="False" name="Seminartitel abhängig von Zielgruppe multimandator" datatype="GuidDDL" dataValueField="value" dataTextField="text">
      <query>SELECT tblTrainingTitles.id AS value, title AS text 
FROM tblTrainingTitles 
join tblItems on tblItems.id = tblTrainingTitles.id
WHERE exists (select * from tblTrainingTitlesMandatorsNeededSeats where mandator_id = @current_mandator_id and trainingTitle_id = tblTrainingTitles.id) 
AND (@targetGroup_id IS NULL OR EXISTS (SELECT * FROM tblEduOffersTargetGroups, tblEduOffersModules, tblModuleItems, tblTrainings 
WHERE tblEduOffersTargetGroups.targetGroup_id = @targetGroup_id AND tblEduOffersModules.eduOffer_id = tblEduOffersTargetGroups.eduOffer_id 
AND tblModuleItems.module_id = tblEduOffersModules.module_id AND tblTrainings.id = tblModuleItems.item_id AND tblTrainingTitles.id = tblTrainings.trainingTitle_id)) 
ORDER BY text
</query>
      <Parameters>
        <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isRequired="True" name="Zielgruppe" contextName="Zielgruppe" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
    </ParameterType>
    <ParameterType id="f22db1dd-65f9-4b99-a45e-ec63268de8d6" isSystem="False" name="Seminartermine pro Seminartitel" datatype="GuidDDL" dataValueField="" dataTextField="">
      <query>
        SELECT tblItems.id AS value,
        tblItems.title + ' [' + dbo.fn_FormatDateSpan(tblTrainings.startDate, tblTrainings.endDate) + ']' AS text
        FROM tblTrainings INNER JOIN tblItems ON tblItems.id = tblTrainings.id
        WHERE @trainingTitle_id IS NULL
        OR trainingTitle_id = @trainingTitle_id
        ORDER BY title
      </query>
      <Parameters>
        <Parameter id="8eacbb69-0b54-4785-9223-3343af2b7ae6" isRequired="False" name="Seminartitel" contextName="Seminartitel" defaultValue="" renderHint="Undefined" disableParameter="DisableUntilSet" />
      </Parameters>
    </ParameterType>
    <ParameterType id="a5255b20-40a0-4cbd-a664-0926299c512f" isSystem="False" name="Cost type" datatype="IntegerDDL" dataValueField="costType_id" dataTextField="costtype_Name">
      <query>
        create table #tempCostType
        (
        costType_id int,
        costType_Name nvarchar(64)
        )

        insert into #tempCostType (costType_id, costType_Name) values (0, 'Gesamtkosten')
        insert into #tempCostType (costType_id, costType_Name) values (1, 'Allgemeine Kosten')
        insert into #tempCostType (costType_id, costType_Name) values (2, 'Direktkosten')

        select * from #tempCostType
        drop table #tempCostType
      </query>
    </ParameterType>
    <ParameterType id="6036b338-5f89-4d19-b3b6-a7d247a480ca" isSystem="False" name="Costs" datatype="IntegerDDL" dataValueField="cost_id" dataTextField="cost_Name">
      <query>
        create table #tempCost
        (
        cost_id int,
        cost_Name nvarchar(64)
        )

        insert into #tempCost (cost_id, cost_Name) values (0, 'Gesamt')
        insert into #tempCost (cost_id, cost_Name) values (1, 'Pro Kopf')
        insert into #tempCost (cost_id, cost_Name) values (2, 'Pro Seminartag')

        select * from #tempCost
        drop table #tempCost
      </query>
    </ParameterType>
    <ParameterType id="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
    <ParameterType id="c4fa1006-96a9-4b85-bf22-6a480289e5f3" isSystem="False" name="Seminartitel" datatype="GuidDDL" dataValueField="" dataTextField="">
      <query>SELECT tblItems.id AS value, 
                    tblItems.title AS text 
               FROM tblTrainingTitles 
                    INNER JOIN tblItems ON tblItems.id = tblTrainingTitles.id
              WHERE tblTrainingTitles.mandator_id = @current_mandator_id ORDER BY text</query>
    </ParameterType>
  </ParameterTypes>
</ReportsExport>