﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="C78C1442-CB00-430F-80C4-4CEB98D05CCA" codekey="TrainingCosts" categoryCodekey="Trainings" name="Seminarkosten" description="">
      <MetaData created="2010-12-14T16:20:23" createdBy="VIWIS" createdBy_user_id="3" modified="2014-04-01T13:17:15" modifiedBy="Administrator" />
      <ExecutionDetails format="KeyValueResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="AdminAnzahlNutzerProThema" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="D1E45C53-7336-4C86-864A-26FFF0FEDDA9" mandatorName="HUKeLearning" 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="fb59ff9d-0092-49b6-a953-99817b57ed18" isRequired="False" name="Seminartitel abhängig von Zielgruppen" contextName="Seminartitel" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="65a4a62e-6f63-4fc1-aabd-08b089df1514" 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,
        Travel_Price decimal (18,2),
        Travel_Others 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.Travel_Price) as 'Travel_Price',
        sum(v_TrainingEventCostsPerUser.Travel_Others) as 'Travel_Others'

        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.Travel_Price) as 'Travel_Price',
        sum(v_TrainingEventCostsPerUser.Travel_Others) as 'Travel_Others'

        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.Travel_Price) as 'Travel_Price',
        sum(v_TrainingEventCostsPerUser.Travel_Others) as 'Travel_Others'

        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 v_Users.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',

        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=4) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'CostExam',

        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=5) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'GeneralCostCancelation',

        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=6) *
        (case when #tempTrainingTitles.countAllUsers = 0 then 1 else #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers end)
        ) as 'CostOthers'

        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 CostExam=0 where CostExam is null
        update #tempResults set GeneralCostCancelation=0 where GeneralCostCancelation is null
        update #tempResults set CostOthers=0 where CostOthers is null

        update #tempResults set Travel_Price=0 where Travel_Price is null
        update #tempResults set Travel_Others=0 where Travel_Others is null

        -- select * from #tempResults

        select
        sum(CostTrainer) as 'CostTrainer',
        sum(CostRentAndTechnical) as 'CostRentAndTechnical',
        sum(RoomCosts) as 'RoomCosts',
        sum(CostConference) as 'CostConference',
        sum(CostExam) as 'CostExam',
        sum(GeneralCostCancelation) as 'GeneralCostCancelation',
        sum(CostOthers) as 'CostOthers',

        sum(Travel_Price) as 'Travel_Price',
        sum(Travel_Others) as 'Travel_Others',

        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 CostExam=0 where CostExam is null
        update #tempResults2 set GeneralCostCancelation=0 where GeneralCostCancelation is null
        update #tempResults2 set CostOthers=0 where CostOthers is null

        update #tempResults2 set Travel_Price=0 where Travel_Price is null
        update #tempResults2 set Travel_Others=0 where Travel_Others 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

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=4) )
        begin
        update #tempResults2 set CostExam=0 where CostExam is null
        update #tempResults2 set CostExam = CostExam + (select amount from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=4)
        end

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=5) )
        begin
        update #tempResults2 set GeneralCostCancelation=0 where GeneralCostCancelation is null
        update #tempResults2 set GeneralCostCancelation = GeneralCostCancelation + (select amount from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=5)
        end

        if ( exists(select * from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=6) )
        begin
        update #tempResults2 set CostOthers=0 where CostOthers is null
        update #tempResults2 set CostOthers = CostOthers + (select amount from #costsOfEventdsWithNoTrainigs where trainingEventCostType_id=6)
        end

        drop table #costsOfEventdsWithNoTrainigs
        end

        -- select * from #tempResults2

        select
        convert(nvarchar, GETDATE(), 104) as 'ReportDate',
        CostTrainer + CostRentAndTechnical + RoomCosts + CostExam + GeneralCostCancelation + CostOthers + Travel_Price + Travel_Others as 'SumAll',
        CostTrainer + CostRentAndTechnical + RoomCosts + CostExam + GeneralCostCancelation + CostOthers as 'SumGeneral',
        CostTrainer,
        CostRentAndTechnical,
        RoomCosts,
        CostConference,
        CostExam,
        GeneralCostCancelation,
        CostOthers,
        Travel_Price + Travel_Others as 'SumDirect',
        Travel_Price,
        Travel_Others
        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,
        CostExam = CostExam / @totalUsers,
        GeneralCostCancelation = GeneralCostCancelation / @totalUsers,
        CostOthers = CostOthers / @totalUsers,

        SumDirect = SumDirect / @totalUsers,
        Travel_Price = Travel_Price / @totalUsers,
        Travel_Others = Travel_Others / @totalUsers
        else
        update #tempResult3 set
        SumAll = 0,

        SumGeneral = 0,
        CostTrainer  = 0,
        CostRentAndTechnical  = 0,
        RoomCosts = 0,
        CostConference = 0,
        CostExam = 0,
        GeneralCostCancelation = 0,
        CostOthers = 0,

        SumDirect = 0,
        Travel_Price = 0,
        Travel_Others = 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,
        CostExam = CostExam / @totalTrainingDays,
        GeneralCostCancelation = GeneralCostCancelation / @totalTrainingDays,
        CostOthers = CostOthers / @totalTrainingDays,

        SumDirect = SumDirect / @totalTrainingDays,
        Travel_Price = Travel_Price / @totalTrainingDays,
        Travel_Others = Travel_Others / @totalTrainingDays
        else
        update #tempResult3 set
        SumAll = 0,

        SumGeneral = 0,
        CostTrainer  = 0,
        CostRentAndTechnical  = 0,
        RoomCosts = 0,
        CostConference = 0,
        CostExam = 0,
        GeneralCostCancelation = 0,
        CostOthers = 0,

        SumDirect = 0,
        Travel_Price = 0,
        Travel_Others = 0
        end

        if  ( @costType_id=1 )
        begin
        -- Allgemeine Kosten
        update #tempResult3 set
        SumAll = null,

        SumDirect = null,
        Travel_Price = null,
        Travel_Others = 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,
        CostExam = null,
        GeneralCostCancelation = null,
        CostOthers = null
        end

        /*
        else -- if (@costType_id=0)
        begin
        -- Gesamtkosten
        update #tempResult3 set

        SumDirect = null,
        Travel_Price = null,
        Travel_Others = null,

        SumGeneral = null,
        CostTrainer  = null,
        CostRentAndTechnical  = null,
        RoomCosts = null,
        CostConference = null,
        CostExam = null,
        GeneralCostCancelation = null,
        CostOthers = null
        end
        */

        select
        ReportDate as 'Auswertung vom',
        SumAll as 'Gesamtkosten',
        SumGeneral as 'Allgemeine Kosten',
        CostTrainer as 'Trainerkosten',
        CostRentAndTechnical as 'Seminar-/Tagungskosten',
        RoomCosts as 'Raumkosten',
        CostConference as 'Tagungspauschale',
        CostExam as 'Prüfungskosten',
        GeneralCostCancelation as 'Stornokosten',
        CostOthers as 'Sonstige Kosten',

        SumDirect as 'Direkte Kosten',
        Travel_Price as 'Kilometergeld',
        Travel_Others as 'Sonstige Fahrbelege'

        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',

        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_7}', 'style="width:150px; border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_7}', '', 'Euro', 7

        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', 8

        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', 9

        insert into #$formating_0
        select 'class="AdminGeneral"', 'colspan=4', '&amp;nbsp;', '', '', '', '', '', '', 10

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="font-weight:bold"', '{key_10}', 'colspan="2" style="border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_10}', '', 'Euro', '', '', 11

        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', 12

        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', 13


        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="9ae252e1-18b4-4b71-982c-e27b2d3a5287" 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="fb59ff9d-0092-49b6-a953-99817b57ed18" isSystem="False" name="Seminartitel abhängig von Zielgruppen" reportParameterType_id="5455aebb-4726-4628-abcc-f3f36127f1d4" queryParameterName="@trainingTitle_id" />
    <Parameter id="65a4a62e-6f63-4fc1-aabd-08b089df1514" isSystem="False" name="Seminartermin" reportParameterType_id="05619e2e-36e2-4f4b-91ae-71696196b42d" 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" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" isSystem="True" name="TargetGroup" datatype="TargetGroup" dataValueField="" dataTextField="" />
    <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="5455aebb-4726-4628-abcc-f3f36127f1d4" isSystem="False" name="Seminartitel abhängig von Zielgruppe" datatype="GuidDDL" dataValueField="" dataTextField="">
      <query>
        SELECT distinct(tblItems.id) AS value, 
               tblItems.title AS text 
          FROM tblTrainingTitles 
              INNER JOIN tblItems ON tblItems.id = tblTrainingTitles.id
              INNER JOIN tblTrainings on tblTrainings.trainingTitle_id=tblTrainingTitles.id
              INNER JOIN tblTrainingEventCosts on tblTrainingEventCosts.trainingEvent_id=tblTrainings.id
        WHERE tblItems.mandator_id = @current_mandator_id
            AND (@targetGroup_id IS NULL OR EXISTS (SELECT *
                FROM tblEduOffersTargetGroups,
                tblEduOffersModules,
                tblModuleItems
                WHERE tblEduOffersTargetGroups.targetGroup_id = @targetGroup_id
                AND tblEduOffersModules.eduOffer_id = tblEduOffersTargetGroups.eduOffer_id
                AND tblModuleItems.module_id = tblEduOffersModules.module_id
                AND tblTrainingTitles.id = tblModuleItems.item_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="05619e2e-36e2-4f4b-91ae-71696196b42d" isSystem="False" name="Seminartermine pro Seminartitel" datatype="GuidDDL" dataValueField="" dataTextField="">
      <query>
        SELECT distinct(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
             INNER JOIN tblTrainingEventCosts on tblTrainingEventCosts.trainingEvent_id=tblTrainings.id
        WHERE (@trainingTitle_id IS NULL OR trainingTitle_id = @trainingTitle_id)
              AND tblTrainings.mandator_id=@current_mandator_id
              AND tblTrainings.isClosed!=0
        ORDER BY text, value
      </query>
      <Parameters>
        <Parameter id="fb59ff9d-0092-49b6-a953-99817b57ed18" isRequired="False" name="Seminartitel abhängig von Zielgruppen" 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="" />
  </ParameterTypes>
</ReportsExport>