﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="49BFF3D9-FD4D-4F36-8BB9-B5356E864012" codekey="TrainingCostsPerUser" categoryCodekey="Trainings" name="Seminarkosten Teilnehmer" description="">
      <MetaData created="2011-01-18T16:30:10" createdBy="VIWIS" createdBy_user_id="3" modified="2014-04-01T13:19:05" modifiedBy="Administrator" />
      <ExecutionDetails format="KeyValueResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="D1E45C53-7336-4C86-864A-26FFF0FEDDA9" mandatorName="HUKeLearning" isStandard="False" isUsedByMenu="True" />
      <Parameters>
        <Parameter id="ca406f5d-c0f6-414b-97b2-1382f42684f0" isRequired="True" name="Benutzer" contextName="Benutzer" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a972a19c-7501-4956-ba07-18128e1037f7" isRequired="False" name="Seminartitel abhängig vom Benutzer" contextName="Seminartitel" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="4dfd4179-f2e4-40b8-b61a-7289f0e2bce7" isRequired="False" name="Seminartermin abhängig von Benutzer und Titel" contextName="Seminartermin abhängig von Benutzer und Titel" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a57b48c0-6f41-49a6-85a4-eadc5603a0cd" isRequired="False" name="Kostenart" contextName="Kostenart" defaultValue="" 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

        -- 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), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        amount
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=1
        ) * #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers) end) as 'CostTrainer',

        convert(decimal(18,2), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        amount
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.trainingEventCostType_id=2 and
        tblTrainingEventCosts.user_id is null
        ) * #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers) end) as 'CostRentAndTechnical',

        convert(decimal(18,2), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        SUM(amount)
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.trainingEventCostType_id=22 and
        tblTrainingEventCosts.user_id is null
        ) * #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers) end) as 'RoomCosts',

        convert(decimal(18,2), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        amount
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=3
        ) * #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers) end) as 'CostConference',

        convert(decimal(18,2), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        amount
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=4
        ) * #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers) end) as 'CostExam',

        convert(decimal(18,2), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        amount
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=5
        ) * #tempTrainingTitles.countUsers/#tempTrainingTitles.countAllUsers) end) as 'GeneralCostCancelation',

        convert(decimal(18,2), case when #tempTrainingTitles.countAllUsers = 0 then 0 else ((select
        amount
        from tblTrainingEventCosts
        where
        tblTrainingEventCosts.trainingEvent_id=#tempTrainingTitles.trainingEvent_id and
        tblTrainingEventCosts.user_id is null and
        tblTrainingEventCosts.trainingEventCostType_id=6
        ) * #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="ca406f5d-c0f6-414b-97b2-1382f42684f0" isSystem="True" name="Benutzer" reportParameterType_id="1f6b4472-1471-441e-a195-454faa229f22" queryParameterName="@user_id" />
    <Parameter id="a972a19c-7501-4956-ba07-18128e1037f7" isSystem="False" name="Seminartitel abhängig vom Benutzer" reportParameterType_id="f4b9f859-f0fe-41b4-9471-890cb2a439b3" queryParameterName="@trainingTitle_id" />
    <Parameter id="4dfd4179-f2e4-40b8-b61a-7289f0e2bce7" mandator_id="cf85efc9-150b-4eee-8c20-929a112b658c" isSystem="False" name="Seminartermin abhängig von Benutzer und Titel" reportParameterType_id="1f0b7d47-1ed0-4273-a531-1bd73c30e9ea" 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="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isSystem="True" name="Zeitraum" reportParameterType_id="abff13be-91c3-4ee1-93a3-7292f8e013ba" queryParameterName="@dateRange" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="1f6b4472-1471-441e-a195-454faa229f22" isSystem="True" name="User" datatype="User" dataValueField="" dataTextField="" />
    <ParameterType id="f4b9f859-f0fe-41b4-9471-890cb2a439b3" isSystem="False" name="Seminartitel abhängig vom Benutzer" datatype="GuidDDL" dataValueField="key" dataTextField="value">
      <query>
        select
        distinct(tblTrainingTitles.id) as 'key', tblItems.title as 'value'
        from
        tblTrainingTitles
        inner join tblTrainings on tblTrainings.trainingTitle_id=tblTrainingTitles.id
        inner join tblItems on tblTrainingTitles.id = tblItems.id
        inner join tblTrainingEventCosts on tblTrainingEventCosts.trainingEvent_id=tblTrainings.id
        where tblTrainings.mandator_id=@current_mandator_id and
              (tblTrainingEventCosts.user_id = @user_id or
               tblTrainingEventCosts.user_id is null and
               not exists(select * from tblUserTrainingStatus where tblUserTrainingStatus.training_id=tblTrainings.id)
              )
        order by 'value'
      </query>
      <Parameters>
        <Parameter id="ca406f5d-c0f6-414b-97b2-1382f42684f0" isRequired="True" name="Benutzer" contextName="Benutzer" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
    </ParameterType>
    <ParameterType id="1f0b7d47-1ed0-4273-a531-1bd73c30e9ea" isSystem="False" name="Seminartermin abhängig von Benutzer und Titel" datatype="GuidDDL" dataValueField="value" dataTextField="text">
      <query>
        SELECT distinct tblItems.id AS value,
        tblItems.title + ' [' + dbo.fn_FormatDateSpan(startDate, endDate) + ']' AS text
        FROM tblTrainings
        inner join tblItems on tblTrainings.id = tblItems.id
        inner join tblTrainingEventCosts on tblTrainingEventCosts.trainingEvent_id=tblTrainings.id
        WHERE tblTrainings.isClosed!=0
        AND tblTrainings.mandator_id=@current_mandator_id
        AND (@trainingTitle_id IS NULL OR trainingTitle_id = @trainingTitle_id)
        AND (tblTrainingEventCosts.user_id = @user_id or
        tblTrainingEventCosts.user_id is null and
        not exists(select * from tblUserTrainingStatus where tblUserTrainingStatus.training_id=tblTrainings.id)
        )
        ORDER BY text, value
      </query>
      <Parameters>
        <Parameter id="ca406f5d-c0f6-414b-97b2-1382f42684f0" isRequired="False" name="Benutzer" contextName="Benutzer" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a972a19c-7501-4956-ba07-18128e1037f7" isRequired="False" name="Seminartitel abhängig vom Benutzer" contextName="Seminartitel abhängig vom Benutzer" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </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="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
  </ParameterTypes>
</ReportsExport>