IF NOT EXISTS (select * from sys.objects where  name = 'APP_CCHEQ_MV_MAIN_PRINTREPORT')
EXEC dbo.sp_executesql @statement = N'
CREATE FUNCTION [dbo].[APP_CCHEQ_MV_MAIN_PRINTREPORT] (@PROTOCOL_PK BIGINT, @lang VARCHAR(3) = ''DE'')  
RETURNS @ausdruck_input TABLE(AUSDRUCKBEZEICHNUNG VARCHAR(1024),VORGANGSNUMMER BIGINT NULL, MVANUMMER VARCHAR(32) NULL, STANDORT VARCHAR(512) NULL, KENNZEICHEN VARCHAR(512) NULL, KMSTAND INT NULL, FAHRZEUGTYP VARCHAR(1024) NULL,
TANKINHALT_RESERVE BIT NULL, TANKINHALT_1_4 BIT NULL, TANKINHALT_2_4 BIT NULL, TANKINHALT_3_4 BIT NULL, TANKINHALT_4_4 BIT NULL, 
VORSCHAEDEN BIT NULL,NAVIGATION_OHNE BIT NULL, NAVIGATION_STECKMODUL BIT NULL, NAVIGATION_COMAND BIT NULL, BEREIFUNG_WINTER BIT NULL, BEREIFUNG_SOMMER BIT NULL, 
BEREIFUNG_GANZJAHR BIT NULL, SIGNATURE_DATA VARCHAR(2048) NULL, CLIENT_NAME VARCHAR(512) NULL, CLIENT_SIGNATURE IMAGE NULL, EMPLOYEE_NAME VARCHAR(512) NULL, SIGNATUREEMPLOYEE IMAGE NULL, THIRD_NAME VARCHAR(512) NULL, SIGNATURETHIRD IMAGE NULL, CLIENT_STREET VARCHAR(512) NULL, CLIENT_TOWN VARCHAR(512) NULL, CLIENT_ZIPCODE VARCHAR(512) NULL, CLIENT_NUMBER BIGINT NULL, VIN VARCHAR(512) NULL, ENTERPRISE_LOGO IMAGE NULL, PARKPLATZ VARCHAR(512) NULL
)  
BEGIN  --Author José. Created on Mai 24th 2019 Aufgabe 1105961
	IF(@lang NOT IN(''DE'',''US'',''NL'',''ES'', ''RU'')) SET @lang = ''DE''
	--SET @PROTOCOL_PK  = 1
	DECLARE @dateformat VARCHAR(32)= dbo.GET_DATE_FORMAT(@lang) 
	DECLARE @protocol_date VARCHAR(32) = ''''
	DECLARE @AUSDRUCKBEZEICHNUNG VARCHAR(1024) =  ''Kein Übergabeprotokoll'', @VERTRAGSNUMMER BIGINT = 0, @MVANUMMER VARCHAR(32) = '''', 
	@KUNDENNUMMER BIGINT = NULL, @OFFENODERCHECKIN VARCHAR(1) = '''', @STANDORT VARCHAR(128)  = '''', @KMSTAND BIGINT = 0, @TANK BIGINT = 0,
	@FAHRZEUGTYP VARCHAR(128) = '''', @TANKINHALT_RESERVE BIT = 0, @TANKINHALT_1_4 BIT  = 0, @TANKINHALT_2_4 BIT = 0, 
	@TANKINHALT_3_4 BIT =  0, @TANKINHALT_4_4 BIT = 0, @VORSCHAEDEN BIT  = 0, @SIGNATURE_DATA VARCHAR(2048)  = ''Standort, ''+dbo.getFormatDateFromString(CURRENT_TIMESTAMP, ''dd.mm.YYYY'')+'', '', --@CLIENT_SIGNATURE IMAGE = 0x89504e470d0a1a0a0000000d494844520000000a0000000a08060000008d32cfbd0000000473424954080808087c0864880000001849444154189563fcffffff7f06220013318a4615524f2100699a04108a8d0b680000000049454e44ae426082
    @SIGNATURECUSTOMER_DOC_ID BIGINT = NULL, @KENNZEICHEN VARCHAR(512) = '''', @SIGNATURETHIRD_DOC_ID BIGINT = NULL, @SIGNATUREEMPLOYEE_DOC_ID BIGINT = NULL,
	@NAVIGATION_OHNE BIT = 0, @NAVIGATION_STECKMODUL BIT  = 0, @NAVIGATION_COMAND BIT  = 0, @BEREIFUNG_WINTER BIT  = 0, @BEREIFUNG_SOMMER BIT  = 0, 
	@BEREIFUNG_GANZJAHR BIT  = 0, @CLIENT_NAME VARCHAR(512) = '''', @CLIENT_NUMBER BIGINT = NULL, @CLIENT_STREET VARCHAR(512) = '''', @CLIENT_TOWN VARCHAR(512) = '''', @CLIENT_ZIPCODE VARCHAR(512) = '''', @EMPLOYEE_NAME VARCHAR(512) = ''Mitarbeiter'', @THIRD_NAME VARCHAR(512) = '''',
	@VIN VARCHAR(512) = '''', @PARKPLATZ VARCHAR(512) = ''''

	DECLARE @empty_img VARBINARY(MAX) = 0x89504e470d0a1a0a0000000d494844520000000a0000000a08060000008d32cfbd0000000473424954080808087c0864880000001849444154189563fcffffff7f06220013318a4615524f2100699a04108a8d0b680000000049454e44ae426082
	DECLARE @mandant_logo VARBINARY(MAX) = ISNULL((SELECT TOP 1 ISNULL(Logo,@empty_img) FROM dbo.MAND_GLOBAL),@empty_img)
    

	--SET @AUSDRUCKBEZEICHNUNG = ISNULL((SELECT ISNULL(NAME,@AUSDRUCKBEZEICHNUNG) FROM CSAPP_PROTOCOL_TYPES WHERE PK = 1),@AUSDRUCKBEZEICHNUNG)	
	SET @AUSDRUCKBEZEICHNUNG = ISNULL((SELECT ISNULL(NAME,@AUSDRUCKBEZEICHNUNG) FROM CSAPP_PROTOCOL_TYPES WHERE PK = (select FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''PROTOCOLTYPE'')),@AUSDRUCKBEZEICHNUNG)	
	IF(@AUSDRUCKBEZEICHNUNG = ''Kein Übergabeprotokoll'' OR dbo.IND_BERESA_CSAPP_PROTOKOLL(@PROTOCOL_PK) = 0)
		BEGIN
		    INSERT INTO @ausdruck_input 
				SELECT @AUSDRUCKBEZEICHNUNG,0,@MVANUMMER,'''','''',0,'''',0,0,0,0,0,0,0,0,0,0,0,0,'' Minden ''+ dbo.getFormatDateFromString(CURRENT_TIMESTAMP, ''dd.mm.YYYY'')+'' kein Kunden, '', '''',@empty_img, '''',@empty_img, '''',@empty_img, @CLIENT_STREET, @CLIENT_TOWN, @CLIENT_ZIPCODE, @CLIENT_NUMBER, @VIN, @mandant_logo, @PARKPLATZ
			RETURN
		END
	---until this point there is a contract saved in the database 
		IF EXISTS (SELECT *  FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''RESERVATION'' AND FIELD_VALUE <> '''')
			BEGIN
				SELECT @VERTRAGSNUMMER = CAST(ISNULL(FIELD_VALUE,0) AS INT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''RESERVATION''
			END
		ELSE
		IF EXISTS (SELECT *  FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''REFERENZ'' AND FIELD_VALUE <> '''')
			BEGIN
				SELECT @VERTRAGSNUMMER = CAST(ISNULL(FIELD_VALUE,0) AS INT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''REFERENZ''
			END

		SELECT @MVANUMMER =  ISNULL(FIELD_VALUE,0) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''MVANUMBER''
		SELECT @VIN =  ISNULL(FIELD_VALUE,0) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''VIN''	
		SELECT @STANDORT = ISNULL(FIELD_VALUE,0) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''STANDORT''

		IF (@MVANUMMER = 0)
			SELECT @MVANUMMER =  ISNULL((SELECT MVANUMMER FROM dbo.AUTOS WHERE FAHRGESTELLNUMMER = @VIN),'''')
		
		IF (dbo.EXISTS_MVANUMMER(@MVANUMMER) = 1)
			BEGIN
			   -- SELECT @STANDORT =ISNULL((SELECT name FROM dbo.stammt WHERE station = ISNULL((SELECT Standort FROM dbo.AUTOS WHERE MVANUMMER = @MVANUMMER),'''')),'''')
			    SELECT @FAHRZEUGTYP =  ISNULL((SELECT FAHRZEUG FROM dbo.AUTOS WHERE MVANUMMER = @MVANUMMER),'''')  
			    SELECT @KENNZEICHEN =  ISNULL((SELECT KENNZEICHEN FROM dbo.AUTOS WHERE MVANUMMER = @MVANUMMER),'''')				
			    SELECT @VIN =  ISNULL((SELECT FAHRGESTELLNUMMER FROM dbo.AUTOS WHERE MVANUMMER = @MVANUMMER),'''')				
			END
		
		IF EXISTS(SELECT * FROM CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''KM'')
			BEGIN	
				SELECT @KMSTAND = CAST(ISNULL(FIELD_VALUE,0) AS INT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''KM''
			END
		
		SELECT @TANK = CAST(ISNULL(FIELD_VALUE,0) AS INT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''TANK''
		SELECT @KUNDENNUMMER = CAST(ISNULL(FIELD_VALUE,0) AS INT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''CUSTOMER''
		SELECT @THIRD_NAME = ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''ATTENDER''),'''')
		--@CLIENT_NAME VARCHAR(512) = '''', @CLIENT_STREET VARCHAR(512) = '''', @CLIENT_TOWN VARCHAR(512) = '''', @CLIENT_ZIPCODE
		SELECT @CLIENT_NAME = ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''NAME''),'''')
		SELECT @CLIENT_STREET = ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''STREET''),'''')
		SELECT @CLIENT_TOWN = ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''TOWN''),'''')
		SELECT @CLIENT_ZIPCODE = ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''CODE''),'''')
		SELECT @protocol_date = (ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''DATE''),''''))
		SELECT @PARKPLATZ = (ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''PARKPLATZ''),''''))
		
		
		
		IF(ISDATE(@protocol_date)=1)
			SET @protocol_date = dbo.getFormatDateFromString(@protocol_date, ''dd.mm.YYYY'')
		IF (ISDATE(@protocol_date)=0)
			BEGIN
				SET @protocol_date = REPLACE(LTRIM(RTRIM(@protocol_date)),'' '',''T'')
				IF(ISDATE(@protocol_date)=1)
					SET @protocol_date = dbo.getFormatDateFromString(@protocol_date, ''dd.mm.YYYY'')
				ELSE 
					SET @protocol_date = dbo.getFormatDateFromString(CURRENT_TIMESTAMP, ''dd.mm.YYYY'')
					--SET @protocol_date = @protocol_date-- dbo.getFormatDateFromString(CURRENT_TIMESTAMP, ''dd.mm.YYYY'')
			END
			 

		SELECT @SIGNATURE_DATA  = ISNULL((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''STANDORT''),''Standort'')+'', ''+@protocol_date+'', ''
		IF(@CLIENT_ZIPCODE <> '''')
			SET @CLIENT_TOWN = @CLIENT_ZIPCODE+ '', '' +@CLIENT_TOWN
		DECLARE @EMPLOYEE_NUMBER INT
		SELECT @EMPLOYEE_NUMBER = FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''EMPLOYEE''
	    SELECT @SIGNATURE_DATA  = ISNULL((SELECT name FROM dbo.stammt WHERE station = @STANDORT),'''')+'', ''+@protocol_date+'', ''
		
		IF(@TANK IN (0,1)) SET @TANKINHALT_RESERVE = 1
		IF(@TANK IN (2,3)) SET @TANKINHALT_1_4 = 1
		IF(@TANK IN (4,5)) SET @TANKINHALT_2_4 = 1
		IF(@TANK IN (6,7)) SET @TANKINHALT_3_4 = 1
		IF(@TANK IN (8)) SET @TANKINHALT_4_4 = 1
		SET @BEREIFUNG_WINTER = 1
		SET @NAVIGATION_COMAND = 1
		--SET  @SIGNATURE_DATA += 
		--SET @CLIENT_NAME = SUBSTRING(dbo.VTR_GET_KUNDENNAME(@KUNDENNUMMER),1,50) 
		SET @EMPLOYEE_NAME = ISNULL((SELECT [Name] FROM dbo.MITARBEITER WHERE PK = CAST(@EMPLOYEE_NUMBER AS INT)),'''')
		IF LTRIM(RTRIM(@EMPLOYEE_NAME)) = ''''
		BEGIN
			SET @EMPLOYEE_NAME = ISNULL((SELECT Benutzer FROM dbo.MITARBEITER WHERE PK = CAST(@EMPLOYEE_NUMBER AS INT)),'''') 
		END	

		IF(ISNUMERIC((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''SIGNATURECUSTOMER''))=1)
			SELECT @SIGNATURECUSTOMER_DOC_ID = CAST(FIELD_VALUE AS BIGINT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''SIGNATURECUSTOMER''
		IF(ISNUMERIC((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''SIGNATUREEMPLOYEE''))=1)
			SELECT @SIGNATUREEMPLOYEE_DOC_ID =  CAST(FIELD_VALUE AS BIGINT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''SIGNATUREEMPLOYEE''
		IF(ISNUMERIC((SELECT FIELD_VALUE FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''SIGNATURETHIRD''))=1)
			SELECT @SIGNATURETHIRD_DOC_ID =  CAST(FIELD_VALUE AS BIGINT) FROM dbo.CSAPP_PROTOCOL WHERE PROTOCOL_PK = @PROTOCOL_PK AND FIELD_NAME = ''SIGNATURETHIRD''		
		INSERT INTO @ausdruck_input 
			SELECT @AUSDRUCKBEZEICHNUNG,@VERTRAGSNUMMER,@MVANUMMER,@STANDORT,@KENNZEICHEN,@KMSTAND,@FAHRZEUGTYP,@TANKINHALT_RESERVE, @TANKINHALT_1_4, @TANKINHALT_2_4, @TANKINHALT_3_4, @TANKINHALT_4_4, @VORSCHAEDEN, @NAVIGATION_OHNE, @NAVIGATION_STECKMODUL, @NAVIGATION_COMAND, @BEREIFUNG_WINTER, @BEREIFUNG_SOMMER, @BEREIFUNG_GANZJAHR, @SIGNATURE_DATA, @CLIENT_NAME, @empty_img, @EMPLOYEE_NAME, @empty_img, @THIRD_NAME, @empty_img, @CLIENT_STREET, @CLIENT_TOWN, @CLIENT_ZIPCODE, @KUNDENNUMMER, @VIN, @mandant_logo, @PARKPLATZ 
		IF ISNUMERIC(@SIGNATURECUSTOMER_DOC_ID) = 1 AND ISNULL((SELECT TOP 1 DATALENGTH(Beleg)  FROM dbo.DOCS_DOCS WHERE PK = @SIGNATURECUSTOMER_DOC_ID),0) > 0
			BEGIN
			    UPDATE @ausdruck_input SET CLIENT_SIGNATURE = (SELECT TOP 1 Beleg  FROM dbo.DOCS_DOCS WHERE PK = @SIGNATURECUSTOMER_DOC_ID) 
			END
		IF ISNUMERIC(@SIGNATUREEMPLOYEE_DOC_ID) = 1 AND ISNULL((SELECT TOP 1 DATALENGTH(Beleg)  FROM dbo.DOCS_DOCS WHERE PK = @SIGNATUREEMPLOYEE_DOC_ID),0) > 0
			BEGIN
			    UPDATE @ausdruck_input SET SIGNATUREEMPLOYEE = (SELECT TOP 1 Beleg  FROM dbo.DOCS_DOCS WHERE PK = @SIGNATUREEMPLOYEE_DOC_ID) 
			END
		IF ISNUMERIC(@SIGNATURETHIRD_DOC_ID) = 1 AND ISNULL((SELECT TOP 1 DATALENGTH(Beleg)  FROM dbo.DOCS_DOCS WHERE PK = @SIGNATURETHIRD_DOC_ID),0) > 0
			BEGIN
			    UPDATE @ausdruck_input SET SIGNATURETHIRD = (SELECT TOP 1 Beleg  FROM dbo.DOCS_DOCS WHERE PK = @SIGNATURETHIRD_DOC_ID) 
			END
			
		RETURN 
		
  RETURN
END'



IF NOT EXISTS (select * from sys.objects where  name = 'CCHEQ_AUSDRUCK_FOTOS')
EXEC dbo.sp_executesql @statement = N'
CREATE FUNCTION [dbo].[CCHEQ_AUSDRUCK_FOTOS] (@PROTOCOL_ID int, @type VARCHAR(32)=''damage'', @foto_modulo INT = 4)
RETURNS @output TABLE(ID BIGINT IDENTITY(1,1) UNIQUE, Bild IMAGE NULL, Bildtitle VARCHAR(512) NULL, Bildview VARCHAR(512) NULL)  
BEGIN -- Author José February 06th. 2019
		DECLARE @max INT = 10 
		DECLARE @counter INT = 1 
		DECLARE @ausdruck_coci_photo_counter INT  
		DECLARE @ausdruck_coci_photo_rest INT   
		IF @type NOT IN (''damage'',''state'') SET @type = ''damage'' 
		DECLARE @images TABLE(
			Bild IMAGE NULL,
			Bildview varchar(255),
			Bildtitle varchar(512),
			PK bigint
			)
	   DECLARE @empty_img VARBINARY(MAX) = 0x89504e470d0a1a0a0000000d494844520000000a0000000a08060000008d32cfbd0000000473424954080808087c0864880000001849444154189563fcffffff7f06220013318a4615524f2100699a04108a8d0b680000000049454e44ae426082
 
	    IF(@type = ''damage'')
			BEGIN 
				declare @BILDTITEL varchar(255) = ''''
				IF EXISTS(SELECT bild, c.TEXT, ''Schaden '' + CAST(p.Schadenno AS VARCHAR(10)) + '' - '' + p.BEGRUENDUNG FROM dbo.bilder d,  schaeden p, CSAPP_CAMERA_VIEWS c  WHERE d.PKschaeden = p.PK AND p.FILDO_ID = @PROTOCOL_ID AND d.PKview = c.PK)					    	
				INSERT INTO @images(Bild, Bildview, Bildtitle, PK) 		
				SELECT bild, c.TEXT, ''Schaden '' + CAST(p.Schadenno AS VARCHAR(10)) + '' - '' + p.BEGRUENDUNG, d.PK FROM dbo.bilder d,  schaeden p, CSAPP_CAMERA_VIEWS c  WHERE d.PKschaeden = p.PK AND p.FILDO_ID = @PROTOCOL_ID AND d.PKview = c.PK  order by p.schadenno, d.PK					    	
				ELSE
				INSERT INTO @images(Bild, Bildtitle, PK) 		
				SELECT bild, ''Schaden '' + CAST(p.Schadenno AS VARCHAR(10)) + '' - '' + p.BEGRUENDUNG, d.PK FROM dbo.bilder d,  schaeden p  WHERE d.PKschaeden = p.PK AND p.FILDO_ID = @PROTOCOL_ID  order by p.schadenno, d.PK					    	

			WHILE exists (SELECT TOP 1 * from @images)
			BEGIN
			SET	@BILDTITEL = ''''
			IF NOT EXISTS (SELECT  Bildtitle FROM @output where Bildtitle = (SELECT TOP 1 Bildtitle from @images order by Bildtitle, PK) )
				BEGIN
					SET @BILDTITEL = (SELECT TOP 1 Bildtitle from @images order by Bildtitle, PK)
				END

				INSERT INTO @output(Bild, Bildview, Bildtitle)
				SELECT TOP 1 Bild, Bildview, @BILDTITEL FROM @images order by Bildtitle, PK;
				WITH bild AS (SELECT TOP 1 * from @images order by Bildtitle, PK) delete from bild
				SET @counter += 1
				
			END		
		END
		IF(@type = ''state'')
			BEGIN
				IF EXISTS(SELECT Beleg, c.TEXT FROM dbo.DOCS_DOCS d, CSAPP_ZUSTANDSBILDER_REL_PROTOKOLL p, CSAPP_CAMERA_VIEWS c   WHERE d.PK = p.BILD_PK AND p.PROTOKOLL_PK = @PROTOCOL_ID AND p.CAMERAVIEW_PK = c.PK	)
				INSERT INTO @images(Bild, Bildview) 		
					SELECT Beleg, c.TEXT FROM dbo.DOCS_DOCS d, CSAPP_ZUSTANDSBILDER_REL_PROTOKOLL p, CSAPP_CAMERA_VIEWS c   WHERE d.PK = p.BILD_PK AND p.PROTOKOLL_PK = @PROTOCOL_ID AND p.CAMERAVIEW_PK = c.PK					   
			ELSE
				INSERT INTO @images(Bild) 		
					SELECT Beleg FROM dbo.DOCS_DOCS d, CSAPP_ZUSTANDSBILDER_REL_PROTOKOLL p  WHERE d.PK = p.BILD_PK AND p.PROTOKOLL_PK = @PROTOCOL_ID					   

			WHILE exists (SELECT TOP 1 * from @images)
			BEGIN
				INSERT INTO @output(Bild, Bildview)
				SELECT TOP 1 Bild, Bildview FROM @images;
				WITH bild AS (SELECT TOP 1 * from @images) delete from bild
				SET @counter += 1
			END	
		END
		SET @ausdruck_coci_photo_rest = 0
		SET @ausdruck_coci_photo_counter = 0
		if (exists (SELECT ID FROM @output))
			SET @ausdruck_coci_photo_counter = ISNULL((SELECT count(ID) FROM @output),0)
 
				
		IF(@ausdruck_coci_photo_counter = 0 )
			BEGIN
				SET @ausdruck_coci_photo_rest = @foto_modulo
				GOTO fulfillEmptyImages
			END 
		IF(@ausdruck_coci_photo_counter > 0 )  
			BEGIN
				--SET @ausdruck_coci_photo_rest  =((@ausdruck_coci_photo_counter % @foto_modulo) * @foto_modulo) - @ausdruck_coci_photo_counter
				SET @ausdruck_coci_photo_rest = dbo.CCHEQ_AUSDRUCK_FOTO_GET_REST(@ausdruck_coci_photo_counter,@foto_modulo) 
				--CCHEQ_AUSDRUCK_FOTO_GET_REST
				GOTO fulfillEmptyImages
			END

		 
			 
	 fulfillEmptyImages:
		 BEGIN  
			IF (@ausdruck_coci_photo_rest <> 0)
				BEGIN
					IF @ausdruck_coci_photo_rest < 0 SET @ausdruck_coci_photo_rest = @ausdruck_coci_photo_rest*-1              
					INSERT INTO @output(Bild, Bildview, Bildtitle) 
						SELECT TOP (@ausdruck_coci_photo_rest) @empty_img, '''', ''''
							FROM dbo.bilder 						
				END
 	
		 END

	RETURN  
END'

