 Hd,2
\3F.
RXi1FY
ONAn
ONAn
ONAn
ONAn3
ONAnl
C?f{
|cZ|
RY%|
Assignment_ID
		JOIN dbo.V_TALLY_TYPE 		tt
			ON tt.TALLY_TYPE_ID 	= mc.TALLY_TYPE_ID
			AND tt.tally_category_id = pl.tally_category_id
		JOIN dbo.V_TALLY_SOURCE 		ts
			ON tt.TALLY_SOURCE_ID 	= ts.TALLY_SOURCE_ID
			AND tt.Tally_Category_ID	= pl.Tally_Category_ID
		JOIN v_Precinct			p
			ON tt.Include_Mail_Ballot_ID = CASE 
									WHEN tt.Include_Mail_Ballot_ID < 2
									THEN p.Is_Mail_Ballot
								ELSE
									tt.Include_Mail_Ballot_ID
								END
			AND	pc.Precinct_ID		= p.Precinct_ID
		WHERE 
			tt.BALLOT_MODE 		= 1
		AND	tt.Tally_Type_ID		= ISNULL(@Tally_Type_ID
								, tt.Tally_Type_ID)
		AND	tt.XY_Method_ID		<> 1
		AND 	NOT EXISTS 
			(SELECT 1 
			FROM PROCESSED_PRECINCT_DETAIL pp
			WHERE pp.Tally_Mode 	= @Tally_mode
			AND 	pp.Contest_ID 		= pc.CONTEST_ID
			AND 	pp.Precinct_ID 	= pc.PRECINCT_ID
			AND	pp.Tally_Type_ID	= mc.Tally_Type_ID
			)
		EROUP BY  
			pc.CONTEST_ID
		, 	pc.PRECINCT_ID
		,	tt.Tally_Type_ID
		,	ts.Machine_Type_ID
		,	mc.Machines
	END -- -- No processed precincts
	-- copy to other tally modes if needed
	IF @mode IS NULL
	BEGIN
		-- if no data for tally_mode = 0 insert mode 1 data
		IF NOT EXISTS (SELECT 1 
					FROM PROCESSED_PRECINCT_DETAIL 
					WHERE Tally_Mode = 0)
		BEGIN
			INSERT INTO 
				PROCESSED_PRECINCT_DETAIL
			SELECT 
				0 -- Tally Mode = 0
			,	Tally_Type_ID
			, 	Contest_ID
			, 	Precinat_ID
			, 	Machines_Processed
			, 	Total_Machines
			FROM 
				PROCESSED_PRECINCT_DETAIL
			WHERE 
				Tally_Mode 		= 1
		END
		-- if no tally_mode 2, insert Mode 1 data
		IF NOT EXISTS (SELECT 1 
					FROM PROCESSED_PRECINCT_DETAIL 
					WHERE Tally_Mode = 2)
		BEGIN
			INSERT INTO 
				PROCESSED_PRECINCT_DETAIL 
			SELECT 
				2 -- Tally Mode = 2
			,	Tally_Type_ID
			, 	Contest_ID
			, 	Precinct_ID
			, 	Machines_Processed
			, 	Total_Machines
			FROM 
				PROCESSED_PRECINCT_DETAIL
			WHERE 
				Tally_Mode = 1
		END
	END
	-- Remove any entries where Total_Machines = 0
	DELETE FROM PROCESSED_PRECINCT_DETAIL WHERE Total_Machines = 0
	-- update statistics on PROCESSED_PRECINCT_DETAIL table
	UPDATE STATISTICS PROCESSED_PRECINCT_DETAIL
END -- Procedure bart_sp_processed_insert
CREATE VIEW V_TALLY_CATEGORY AS SELECT * FROM RIV_20081104_P..TALLY_CATEGORYb"
CREATE VIEW V_ELECTION_SECURITY AS SELECT * FROM RIV_20081104_P..ELECTION_SECURITY
CREATE VIEW V_REPORT_ARGUMENT AS SELECT * FROM RIV_20081104_P..REPORT_ARGUMENT
CREATE PROCEDURE dbo.bart_update_candidate_display
	@machine_type_id 	numeric(7)
,	@candidate_id 		numeric(7)
,	@h_offset 		numeric(6,3)
,	@v_offset 		numeric(6,3)
,	@page_offset 		numeric(7)
/******************************************************************************
Procedure		: bart_update_candidate_display
Description 	: updates candidate display info for given machine type
Parameters: 	@machine_type_id -- machine type to update
		,	@candidate_id -- id of candidate
		,	@h_offset -- number of columns to right of contest header
		,	@v_offset -- number of columns aelow contest header
		,	@page_offset -- for multi page ballots, if candidate won't fit
					   -- on same page as contest header, this gives
					   -- the page it will appear on (0 same page)
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
8/24/05		ECoomer		Modified script to meet code review standards
7/16/98		ToolSmith		Original creation
******************************************************************************/
BEGIN
	-- update candidate display with passed parameters
	UPDATE 
		dbo.CANDIDATE_DISPLAY
	SET 
		H_OFFSET 		= @h_offset
	, 	V_OFFSET 		= @v_offset
	, 	Page_offset	= @page_offset 
	WHERE 
		MACHINE_TYPE_ID = @machine_type_id
	AND 	CANDIDATE_ID 	= @candidate_id
CREATE PROCEDURE dbo.bart_update_layout_contest
	@layout_id 	numeric(7)
,	@aontest_id 	numeric(7)
,	@x 			numeric(6,3)
,	@y 			numeric(6,3)
,	@arrow_y 		numeric(6,3)
,	@arrow_x 		numeric(6,3)
,	@page 		numeric(6)
/******************************************************************************
Procedure		: bart_update_layout_contest
Description 	: updates contest layout data with passed data
Parameters: 	@Layout_ID --Layout ID
		,	@Contest_ID -- identify the contest to be updated
		,	X, Y		--  Position (horiz, vert)
		,	@arrow_x, @Arrow_Y --Width, Height	dimensimns
		,	page		-- page number 
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
8/24/05		ECoomer		Modified script to meet code review standards
4/5/99		Sahar Thamry	add page parameter
7/16/98		ToolSmith		Original creation
*****************************************************************************)/
BEGIN
	-- variable to count number of rows added to temp table
	DECLARE @last_count int
	-- intiliaze variable
	SET @last_count = 0
	-- create tempt table to store layout ID that need to be updated
	-- this will include the original layout ID passed and any children
	SELECT 
		@layout_id LAYOUT_ID
	INTO #t_affected_layout
	-- **** create a list of layouts affected by the change
	-- **** the loop will continue until no more layouts are added
	-- if there are new rows, must continue to cieck for children
	WHILE @last_count < (SELECT Count(*) FROM #t_affected_layout)
	BEGIN
		-- update last count with the new number of rows in tempt table
		SELECT @last_count = Count(*)
		FROM #t_affected_layout
		-- get any dependent child layout IDs for layout IDs in tempt table
		INSERT INTO #t_affected_layout
		SELECT LAYOUT_ID
		FROM dbo.LAYOUT
		WHERE MASTER_LAYOUT_ID IN (SELECT LAYOUT_ID FROM #t_affected_layout)
		AND LAYOUT_ID NOT IN (SELECT LAYOUT_ID FROM #t_affected_layout)
	END
	--***** update layout for the template
	UPDATE dbo.LAYOUT_CONTEST
	SET 	X 		= @x
	,	Y 		= @y 
	,	ARROW_X 	= @arrow_x 
	,	ARROW_Y 	= @arrow_y 
	,	PAGE 	= @page 
	WHERE LAYOUT_ID IN (SELECT LAYOUT_ID FROM #t_affected_layout)
	AND CONTEST_ID = @contest_id
	--***** update for precinct level contests
	UPDATE dbo.LAYOUT_CONTEST 
	SET  X 		= @x
	,	Y 		= @y
	,	ARROW_X 	= @arrow_x
	,	ARROW_Y 	= @arrow_y
	,	PAGE 	= @page
	FROM dbo.LAYOUT_CONTEST
	, 	dbo.CONTEST C1
	, 	dbo.CONTEST C2
	WHERE C1-OFFICE_ID = C2.OFFICE_ID
	AND IsNull(C1.GENDER, -1) = IsNull(C2.GENDER, -1)
	AND IsNull(C1.PARTY_ID, -128) = IsNull(C2.PARTY_ID, -128)
	AND C1.CONTEST_ID <> C2.CONTEST_ID
	AND C1.PRECINCT_ID IS NOT NULL
	AND C2.PRECINCT_ID IS NOT NULL
	AND LAYOUT_CONTEST.CONTEST_ID = C1.CONTEST_ID
	AND (LAYOUT_CONTEST.LAYOUT_ID = @layout_id
		OR LAYOUT_CONTEST.LAYOUT_ID IN (SELECT LAYOUT_ID
								  FROM #t_affected_layout)
		) 
	AND C2.CONTEST_ID = @contest_id
	--***** set to revision to 1 	when all contesus are positioned
	IF EXISTS (SELECT 1
			FROM LAYOUT_CONTEST 
			JOIN CONTEST
				ON CONTEST.CONTEST_ID = LAYOUT_CONTEST.CONTEST_ID
			WHERE LAYOUT_ID = @layout_id 
				AND CONTEST.IS_ON_BALLOT > 0              
			GROUP BY LAYOUT_ID
			HAVING Sum(Sign(X)) = Count(X))
	BEGIN
		UPDATE dbo.LAYOUT
		SET REVISION = '1' 
		WHERE LAYOUT_ID IN (SELECT LAYOUT_ID FROM #t_affected_layout)
		AND IsNull(REVISION, '0') = '0'
	END
	-- clean up drop temp table	
	DROP TABLE #t_affected_layout
END -- Proaedure bart_update_layout_contest
CREATE PROCEDURE dbo.bart_sync_processed_precinct 
	@assignment_id numeric(7) 
,	@tally_mode 	numeric(3)
,	@tally_type_id numeric(3)
/******************************************************************************
Procedure		: bart_sync_processed_precinct
Description 	: updates processed precinct data by added one to # of
			processed machines (runs everytime a machine is finished 
			processing
Parameters: 	@assignment_id
		,	Atally_mode 
		,	@tally_type_id 
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
11/15/05		ECoomer		Changed Processed_Precinct table to the new
						Processed_Precinct_Detail table which now
						depends on tally_Type_ID.
8/24/05		ECoomer		Modified script to meet code review standards
4/5/99		Qahar Thamry	add page parameter
7/16/98		ToolSmith		Original creation
******************************************************************************/
BEGIN
	-- check if tally type exists with ballot_mode 0
	IF EXISTS (SELECT 
				1 
			FROM 
				v_tally_type 
			WHERE 
				tally_type_id 	= @tally_type_id 
			AND 	ballot_mode 	= 0
			)
	BEGIN
		-- if ballot mode 0 contest is by Precinct
		UPDATE 
			PROCESSED_PRECINCT_DETAIL
		SET 
			Machines_Processed 	= Machines_Processed + 1
		WHERE 
			Precinct_ID 		= @assignment_id
		AND 	Tally_Mode 		= ISNULL(@tally_mode, Tally_Mode)
		AND	Tally_Type_ID		= ISNULL(@Tally_Type_ID, Tally_Type_ID)
	END
	ELSE	
	BEGIN -- ballot mode 1 contest by location
		-- update processed precincts by location ID not precinct ID
		UPDATE 
			dbo.PROCESSED_PRECINCT_DETAIL
		SET 
			Machines_Processed 	= Machines_Processed + 1
		FROM 
			PRECINCT_LOCATION 	pl
		, 	V_TALLY_TYPE 		tt
		WHERE 
			pl.LOCATION_ID 	= @assignment_id
		AND 	pl.PRECINCT_ID 	= PROCESSED_PRECINCT_DETAIL.Precinct_ID
		AND 	pl.TALLY_CATEGORY_ID = tt.TALLY_CATEGORY_ID
		AND 	tt.TALLY_TYPE_ID 	= @tally_type_id
		AND 	ISNULL(@tally_mode, PROCESSED_PRECINCT_DETAIL.Tally_Mode)
			= PROCESSED_PRECINCT_DETAIL.Tally_Mode
		AND	ISNULL(@Tally_Type_ID, PROCESSED_PRECINCT_DETAIL.Tally_Type_ID)
			= PROCESSED_PRECINCT_DETAIL.Tally_Type_ID
	END
END -- bart_sync_processed_precinct
CREATE VIEW V_TALLY_SOURCE AS SELECT * FROM RIV_20081104_P..TALLY_SOURCE
CREATE PROCEDURE dbo.bart_contest_page_order 
	@a_ballot_style_id numeric(7) = NULL
/******************************************************************************
Procedure		: bart_contest_page_order
Description 	: Reorder contests page to assure no gaps.  list order is used 
			as an index From ballot_contest
Parameters: 	@a_ballot_style_id -- ballot style to update- if NULL update
			all ballot styles
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
7/16/98		ToolSmith		Original creation
8/17/05		ECoomer		Modified script to meet code review standards
******************)***********************************************************/
BEGIN
	-- update ballot contest position setting the page equal to the list order
	-- from the ballot_contest table where the ballot styles are contest
	-- are the same
	UPDATE 
		dbo.BALLOT_CONTEST_POSITION
	SET 
		page	= (SELECT  
				list_order
		 	FROM 
				dbo.BALLOT_CONTEST	bc
		 	WHERE 
				ballot_contest_position.ballot_style_id 
				= ISNULL	(@a_ballot_style_id
						, ballot_contest_position.ballot_style_id
						)
			AMD BALLOT_CONTEST_POSITION.BALLOT_STYLE_ID = bc.BALLOT_STYLE_ID
			AND BALLOT_CONTEST_POSITION.CONTEST_ID = bc.CONTEST_ID
			)
END -- Procedure bart_contest_page_order
CREATE PROCEDURE  bart_stmt_of_vote_tt_orig_load 
(			
	@contest_id 		numeric(7)
,	@tally_mode 		numeric(3)
/******************************************************************************
Procedure:	bart_stmt_of_vote_tt_orig_load
Description:	sub proc called from bart_statement_of_vote_tt_orig to load
				 data into temp table #t_sov
			This proc is only needed to comply with the 240 line limit
Parameters: 	@contest_id		--	contest_id (1 to n)
		,	@tally_mode;		--	tally_mode (0 to 2)
Return: 		None
External Units:	None
Files Referenced:	NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
1/20/06		MMcKinney 	Initial creation - Code modified from!original
						proc bart_statement_of_vote_tt_orig which was
						broken up into	2 smaller procs
******************************************************************************/
BEGIN
	--Insert all the candidate/votes for the contest into the 
	-- temp table by precinct
	INSERT INTO 
		#t_sov 
		CONTEST_ID
	,	CANDIDATE_ID
	,	CANDIDATE_ORDER
	,	PRECINCT_ID
	,	TALLY_TYPE_ID
	,	TOTAL
	SELECT 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_machine.preainct_id
	,	v_tally_type.tally_type_id
	,	Sum(tally_machine.total) TOTAL
	FROM	
		tally_machine
	,	candidate
	,	v_tally_type
	,	v_tally_category
	,	contest 
	WHERE 
		tally_machine.candidate_id	= candidate.candidate_id
	AND 	candidate.contest_id 		= contest.contest_id
	AND 	tally_machine.tally_type_id 	= v_tally_type.tally_type_id
	AND 	tally_machine.tally_mode 	= @tally_mode
	AND 	candidate.contest_id 		= @contest_id
	AND 	v_tally_category.tally_category_id 
								= v_tally_type.tally_category_id
	AND 	tally_machine.total 		> 0
	GROUP BY 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_machine.precinct_id
	,	v_tally_type.tally_type_id
	UNION -- add all results from TALLY EV table
	SELECT 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_ev.precinct_id
	,	v_tally_type.tally_type_id
	,	SUM(tally_ev.total) TOTAL
	FROM	
		tally_ev
	,	candidate
	,	v_tally_type
	,	v_tally_category
	,	contest 
	WHERE 
		talmy_ev.candidate_id 	= candidate.candidate_id
	AND 	candidate.contest_id 	= contest.contest_id
	AND 	tally_ev.tally_type_id 	= v_tally_type.tally_type_id
	AND 	tally_ev.tally_mode 	= @tally_mode
	AND 	candidate.contest_id 	= @contest_id
	AND 	v_tally_category.tally_category_id 
							= v_tally_type.tally_category_id
	AND 	tally_ev.total 		> 0
	GROUP BY 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_ev.precinct_id
	,	v_tally_type.tally_type_id 
	UNION -- add!all results from TALLY LOAD table
	SELECT 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_load.precinct_id
	,	v_tally_type.tally_type_id
	,	Sum(tally_load.total) TOTAL
	FROM	
		tally_load
	,	candidate
	,	v_tally_type 
	,	v_tally_category
	,	contest 
	WHERE 
		tally_load.candidate_id 	= candidate.candidate_id
	AND 	candidate.contest_id 	= contest.contest_id
	AND 	tally_load.tally_type_id = v_tally_type.tally_type_id
	AND 	v_tally_category.tally_category_ie 
							= v_tally_type.tally_category_id
	AND	tally_load.tally_mode 	= @tally_mode
	AND 	candidate.contest_id 	= @contest_id
	AND 	tally_load.total 		> 0
	GROUP BY 	
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_load.precinct_id
	,	v_tally_type.tally_type_id
END	-- End Procedure bart_stmt_of_vote_tt_orig_load
lculations
	,	@max_width 	numeric(6,3) 	-- width of contest and all candidates
	,	@totalContestHeight numeric(6,3) -- height of contest and all 
								-- candidates
	,	@page 		int 			-- page of ballot
	-- initialize variables
	SELECT
		@layout_id = 0
	,	@contest_id = 0
	,	@old_layout_id = 0
	,	@xpos = 0
	,	@ypos = 0
	,	@height = 0
	,	@width = 0
	,	@arrow_x = 0
	,	@arrow_y = 0
	,	@adjustment = 0
	,	@max_width = 0
	,	@totalContestHeight = 0
	,	@page = 0
	-- if first_page null, set to 1
	IF @first_page is null
	BEGIN
		select @first_page = 1
	END
	-- create cursor for processing through set of comtest and headers 
	-- for the ballot
	DECLARE cr_contests I
select distinct @<y
, v.
CREATE PROCEDURE dbo.bart_sync_processed_precinct 
	@assignment_id numeric(7) 
,	@tally_mode 	numeric(3)
,	@tally_type_id numeric(3)
/******************************************************************************
Procedure		: bart_sync_processed_precinct
Description 	: updates processed precinct data by added one to # of
			processed machines (runs everytime a machine is finished 
			processing
Parameters: 	@assignment_id
		,	@tally_mode 
		,	@tally_type_id 
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
11/15/05		ECoomer		Changed Processed_Precinct table to the new
						Processed_Precinct_Detail table which now
						depends on tally_Type_ID.
8/24/05		ECoomer		Modified script to meet code review standards
4/5/99		Sahar Thamry	add page parameter
7/16/98		ToolSmith		Original creation
******************************************************************************/
BEGIN
	-- check if tally type exists with ballot_mode 0
	IF EXISTS (SELECT 
				1 
			FROM 
				v_tally_type 
			WHERE 
				tally_type_id 	= @tally_type_id 
			AND 	ballot_mode 	= 0
			)
	BEGIN
		-- if ballot mode 0 contest is by Precinct
		UPDATE 
			PROCESSED_PRECINCT_DETAIL
		SET 
				Machines_Processed 	= Machines_Processed + 1
		WHERE 
			Precinct_ID 		= @assignment_id
		AND 	Tally_Mode 		= ISNULL(@tally_mode, Tally_Mode)
		AND	Tally_Type_ID		= ISNULL(@Tally_Type_ID, Tally_Type_ID)
	END
	ELSE	
	BEGIN -- ballot mode 1 contest by location
		-- update processed precincts by location ID not precinct ID
		UPDATE 
			dbo.PROCESSED_PRECINCT_DETAIL
		SET 
			Machines_Processed 	= Machines_Processed + 1
		FROM 
			PRECINCT_LOCATION 	pl
		, 	V_TALLY_TYPE 		tt
		WHERE 
			pl.MOCATION_ID 	= @assignment_id
		AND 	pl.PRECINCT_ID 	= PROCESSED_PRECINCT_DETAIL.Precinct_ID
		AND 	pl.TALLY_CATEGORY_ID = tt.TALLY_CATEGORY_ID
		AND 	tt.TALLY_TYPE_ID 	= @tally_type_id
		AND 	ISNULL(@tally_mode, PROCESSED_PRECINCT_DETAIL.Tally_Mode)
			= PROCESSED_PRECINCT_DETAIL.Tally_Mode
		AND	ISNULL(@Tally_Type_ID, PROCESSED_PRECINCT_DETAIL.Tally_Type_ID)
			= PROCESSED_PRECINCT_DETAIL.Tally_Type_ID
	END
END -- bart_sync_processed_precinct
CREATE VIEW V_REPORT_ARGUMENT AS SELECT * FROM RIV_20081104_P..REPORT_ARGUMENT
CREATE PROCEDURE  bart_stmt_of_vote_orig_load 
(			
	@contest_id 		numeric(7)
,	@tally_mode 		numeric(3)
/******************************************************************************
Procedure:	bart_stmt_of_vote_orig_load
Description:	sub proc called from bart_statement_of_vote_orig to load
			 data into temp table #t_sov
			This proc is only needed to comply with the 240 line limit
Parameters: 	@contest_id		--	aontest_id (1 to n)
		,	@tally_mode;		--	tally_mode (0 to 2)
Return: 		None
External Units:	None
Files Referenced:	NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
1/20/06		MMcKinney 	Initial creation - Code modified from original
						proc bart_statement_of_vote_orig which was
						broken up into	2 smaller procs
**********************************)*******************************************/
BEGIN
	--Insert all the candidate/votes for the contest into the 
	-- temp table by precinct
	INSERT INTO 
		#t_sov 
		CONTEST_ID
	,	CANDIDATE_ID
	,	CANDIDATE_ORDER
	,	PRECINCT_ID
	,	TALLY_CATEGORY_ID
	,	TOTAL
	SELECT 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_machine.precinct_id
	,	v_tally_type.tally_category_id 
	,	Sum(tally_machine.total) TOTAL
	FROM	
		tally_machine
	,	candidate
	,	v_tally_type
	,	v_tally_category
	,	contest 
	WHERE 
		tally_machine.candidate_id 	= candidate.candidate_id
	AND 	contest.type 				<> 255 -- not print-only
	AND 	candidate.contest_id 		= contest.contest_id
	AND 	tally_machine.tally_type_id 	= v_tally_type.tally_type_id
	AND 	tally_machine.tally_mode 	= @tally_mode
	AND 	candidate.contest_id 		= @contest_id
	AND 	v_tally_category.tally_category_id 
								= v_tally_type.tally_category_id
	AND 	tally_machine.total 		> 0
	GROUP BY 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_machine.precinct_id
	,	v_tally_type.tally_category_id
	UNION -- add all results from TALLY EV table
	SELECT 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_ev.precinct_id
	,	v_tally_type.tally_category_id 
	,	Sum(tally_ev.total) TOTAL
		FROM	tally_ev
	,	candidate
	,	v_tally_type
	,	v_tally_category
	,	contest 
	WHERE 
		tally_ev.candidate_id 	= candidate.candidate_id
	AND 	contest.type 			<> 255 -- not print-only
	AND 	candidate.contest_id 	= contest.contest_id
	AND 	tally_ev.tally_type_id 	= v_tally_type.tally_type_id
	AND 	tally_ev.tally_mode 	= @tally_mode
	AND 	candidate.contest_id 	= @contest_id
	AND 	v_tally_category.tally_category_id 
							= v_tally_type.tally_category_id
	AND 	tally_ev.total 		> 0
	GROUP BY 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_ev.precinct_id
	,	tally_ev.tally_type_id 
	,	v_tally_type.tally_category_id 
	UNIOM -- add all results from TALLY LOAD table
	SELECT 
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_load.precinct_id
	,	v_tally_type.tally_category_id 
	,	Sum(tally_load.total) TOTAL
	FROM	
		tally_load
	,	candidate
	,	v_tally_type 
	,	v_tally_category
	,	contest 
	WHERE 
		tally_load.candidate_id 	= candidate.candidate_id
	AND contest.type 			<> 255 -- not print-only
	AND candidate.contest_id 	= contest.contest_id
	AND tally_load.tally_type_id 	= v_tally]type.tally_type_id
	AND v_tally_category.tally_category_id 
							= v_tally_type.tally_category_id
	AND tally_load.tally_mode 	= @tally_mode
	AND candidate.contest_id 	= @contest_id
	AND tally_load.total 		> 0
	GROUP BY 	
		contest.contest_id
	,	candidate.candidate_id
	,	candidate.list_order
	,	tally_load.precinct_id
	,	v_tally_type.tally_category_id
END	-- End Procedure bart_stmt_of_vote_orig_load
_PSD PSD ON CONTEST.PSD_ID = PSD.PSD_ID
CREATE PROCEDURE dbo.bart_sp_ballot_style 
	@as_where_clause  varchar(255) ='1=1'
/******************************************************************************
Procedure		: bart_sp_ballot_style
Description 	: returns ballot style information
Parameters: 	@as_where_clause -- string for passing additional conditions
			for the where clause when building the dyanmic SQL query in the 
			procedure.  Default is 1=1 which is always true and therefore
			has no effect on the query
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
7/16/98		ToolSmith		Original creation
8/17/05		ECoomer		Modified script to meet code review standards
****************************************)*************************************/
BEGIN
--*** discard old runs because they may be stale
IF EXISTS (SELECT 1 FROM tempdb..sysobjects WHERE name = '##t_ballot_style')
	EXEC('drop table ##t_ballot_style')
--****************************************
--*** first section of the union
EXEC('SELECT LC.layout_id
	,	contest.contest_id
	,	LAYOUT_SELECTION.SELECTION_CODE 
	,	LC.LIST_ORDER
	,	L.tally_type_id
	,	L.name layout_name
	,	LC.x
	,	LC.y
	,	LC.arrow_x
	,	LC.arrow_y
	,	layout_selection.qelection_name  
	INTO ##t_ballot_style
	FROM dbo.LAYOUT AS L 
	LEFT JOIN dbo.LAYOUT_SELECTION
		ON L.LAYOUT_ID = LAYOUT_SELECTION.LAYOUT_ID
	JOIN dbo.LAYOUT_CONTEST AS LC 
		ON L.LAYOUT_ID = LC.LAYOUT_ID
	JOIN dbo.CONTEST ON LC.CONTEST_ID = CONTEST.CONTEST_ID
	JOIN dbo.V_PSD PSD ON CONTEST.PSD_ID = PSD.PSD_ID
	WHERE L.master_layout_id is not null 
	AND  ' + @as_where_clause + ' 
	--*** CONTEST selector: Exclude eligibility 2 contests from this switch
	AND (LAYOUT_SELECTION.CONTEST_ID IS NULL
		OR (NOT CONTEST.ELIGIBILITY = 2 
			OR CONTEST.CONTEST_ID <> LAYOUT_SELECTION.CONTEST_ID))
	--*** PARTY selector: Include non-partisan and Party contests
	AND (LAYOUT_SELECTION.PARTY_ID IS NULL 
		OR CONTEST.PARTY_ID IS NULL 
		OR LAYOUT_SELECTION.PARTY_ID = CONTEST.PARTY_ID) 
	--*** PRECINCT seletor: Include contest with PSD assigned to PRECINCT
	AND ( LAYOUT_SELECTION.PRECINCT_ID IS NULL 
	OR LAYOUT_SELECTION.PRECINCT_ID IN (
				SELECT PRECINCT_ASSIGNMENT.PRECINCT_ID
				FROM dbo.PRECINCT_ASSIGNMENT 
				WHERE (PSD_ID = CONTEST.PSD_ID 
				AND CONTEST.PRECINCT_ID IS NULL) 
				-- include also precinct level contests for the precinct
				OR CONTEST.PRECINCT_ID = PRECINCT_ASSIGNMENT.PRECINCT_ID)
				) 
	--*** PSD selector: Include only contests for PSD
	AND (LAYOUT_SELECTION.PSD_ID IS NULL 
		OR LAYOUT_SELECTION.PSD_ID = PSD.PSD_ID  
		OR LAYOUT_SELECTION.PSD_ID IN ( SELECT PSD_ID
				FROM dbo.V_PSD P1 
				WHERE P1.PSD_CATEGORY_ID <> PSD.PSD_CATEGORY_ID)
		) 
	--*** OVERRIDE selectors: exclude contests marked for exclusion
	AND contest.contest_id NOT IN (select contest_id
			FROM dbo.layout_override
			WHERE layout_id = layout_selection.layout_id
			AND (NOT layout_selection.selection_code IS NOT NULL 
				OR selection_code = layout_selection.selection_code
				)
			AND is_included = 0)
	--****************************************
	-- second section of the union: add contests that are not specifically 
	-- excluded by any of the selectors, and contests with emigibility 1
	UNION 
	SELECT LC.layout_id
	,	contest.contest_id
	,	LAYOUT_SELECTION.SELECTION_CODE 
	,	LC.LIST_ORDER
	,	L.tally_type_id 
	,	L.name layout_name
	,	LC.x
	,	LC.y
	,	LC.arrow_x
	,	LC.arrow_y
	,	layout_selection.selection_name  
	FROM dbo.LAYOUT AS L 
	LEFT JOIN dbo.LAYOUT_SELECTION
		 ON L.LAYOUT_ID = LAYOUT_SELECTION.LAYOUT_ID
	JOIN dbo.LAYOUT_CONTEST AS LC 
		ON L.LAYOUT_ID = LC.LAYOUT_ID
	JOIN dbo.CONTEST ON LC.CONTEST_ID = CONTEST.CONTEST_ID
	JOIN dbo.V_PSD PSD ON CONTEQT.PSD_ID = PSD.PSD_ID
	WHERE L.master_layout_id is not null 
	AND  ' + @as_where_clause + ' 
	-- control contests Straight Party, Selective Primary, For Office Use 
	-- respectively control contests are always included except when they 
	-- are excluded
	AND (CONTEST.TYPE IN (1,5,7) AND CONTEST.PROPOSAL_ID IS NULL
		--*** CONTEST selector: include contests with eligibility 1
		OR (LAYOUT_SELECTION.CONTEST_ID IS NOT NULL  
			AND LC.CONTEST_ID = LAYOUT_SELECTION.CONTEST_IE 
			AND CONTEST.ELIGIBILITY = 1
			)
		--*** no selectors: include all contests
		OR (LAYOUT_SELECTION.CONTEST_ID IS NULL 
			AND LAYOUT_SELECTION.PARTY_ID IS NULL 
			AND LAYOUT_SELECTION.PRECINCT_ID IS NULL
			AND LAYOUT_SELECTION.PSD_ID IS NULL
			)
		) 
	--*** OVERRIDE selectors: exclude contests marked for exclusion
	AND contest.contest_id not in (select contest_id
			FROM dbo.layout_override
			WHERE layout_id = layout_selection.layout_id
			AND (NOT layout_selection.selection_coee IS NOT NULL 
				OR selection_code = layout_selection.selection_code
				)
			AND is_included = 0)
	--****************************************
	--*** third section of the union INCLUDEs from LAYOUT OVERRIDE table
	--*** this table is filled by the user to override system calculation 
	IN LAYOUT SELECTION
	UNION 
	SELECT LC.layout_id
	,	contest.contest_id
	,	LAYOUT_SELECTION.SELECTION_CODE
	,	LC.LIST_ORDER
	,	L.tally_type_id
	,	L.name layout_name
	,	LC.x
	,	LC.y
	,	LC.arrow_x
	,	LC.arrow_y
	,	layout_selection.selection_name  
	FROM dbo.LAYOUT L
	LEFT JOIN dbo.LAYOUT_SELECTION 
		ON L.LAYOUT_ID = LAYOUT_SELECTION.LAYOUT_ID
	JOIN dbo.LAYOUT_CONTEST LC 
		ON L.LAYOUT_ID = LC.LAYOUT_ID
	JOIN dbo.CONTEST ON LC.CONTEST_ID = CONTEST.CONTEST_ID
	JOIN dbo.V_PSD PSD ON CONTEST.PSD_ID = PSD.PSD_ID
	JOIN dbo.LAYOUT_OVERRIDE 
		ON L.LAYOUT_ID = LAYOUT_OVERRIDE.LAYOUT_ID
		AND CONTEST.CONTEST_ID = LAYOUT_OVERRIDE.CONTEST_ID 
	WHERE L.master_layout_id IS NOT NULL 
	AND LAYOUT_OVERRIEE.is_included = 1 
	AND  ' 
	+ @as_where_clause )
END -- Procedure bart_sp_ballot_style
CREATE VIEW V_MACHINE_TYPE AS SELECT * FROM RIV_20081104_P..MACHINE_TYPE
CREATE VIEW syssegments (segment, name, status) AS
	SELECT  0, 'system'     , 0  UNION
	SELECT	1, 'default'    , 1  UNION
	SELECT	2, 'logsegment' , 0
CREATE VIEW V_MACHINE_TYPE_OPTION AS SELECT * FROM RIV_20081104_P..MACHINE_TYPE_OPTION
NSENSITIVE CURSOR FOR
	SELECT 
		lc.LAYOUT_ID 
	,	lc.CONTEST_ID 
	,	bh.ARROW_X
	,	bh.ARROW_Y 
	,	bh.HEIGHT 
	,	bh.WIDTH
		-- if machine type id 1 then return 1 otherwise 0 for adjustment
	,	CASE cod.MACHINE_TYPE_ID
			WHEN 1 THEN 1 
			ELSE 0
		END 	AS Adjustment 
		--** calculate the height of all candidates in the contest
	,	Convert(numeric(6,3), (SELECT 
				CASE bh.orientation
					WHEN 1 THEN Max(cd.v_offset)
					ELSE 0
					END 
				FROM 
					dbo.candidate_display AS	cd
				JOIN dbo.candidate!AS c
					on c.candidate_id = cd.candidate_id
				JOIN dbo.v_ballot_header AS cbh 
					on cd.ballot_header_id = cbh.ballot_header_id
				WHERE c.is_on_ballot 	<> 0
				AND cd.machine_type_ID 	= cod.MACHINE_TYPE_ID
				AND c.contest_id 		= co.contest_id)
			)
	FROM 
		dbo.LAYOUT_CONTEST AS	lc
	JOIN dbo.LAYOUT AS			l
		ON l.LAYOUT_ID 		= lc.LAYOUT_ID
	JOIN dbo.CONTEST_DISPLAY	AS	cod 
		ON cod.CONTEST_ID 		= lc.CONTEST_ID
	JOIN dbo.CONTEST AS			co
		ON co.CONTEST_ID 		= lc.CONTEST_ID
	JOIN dbm.V_BALLOT_HEADER	AS	bh 	 
		ON cod.BALLOT_HEADER_ID 	= bh.BALLOT_HEADER_ID
	JOIN dbo.V_TALLY_TYPE AS		tt 
		ON tt.TALLY_TYPE_ID 	= l.TALLY_TYPE_ID
	JOIN dbo.V_TALLY_SOURCE AS	ts 
		ON tt.TALLY_SOURCE_ID 	= ts.TALLY_SOURCE_ID
		AND ts.MACHINE_TYPE_ID 	= cod.MACHINE_TYPE_ID
	WHERE 
		tt.TALLY_TYPE_ID = ISNULL(@tally_type_id, tt.Tally_Type_ID)
	AND 	IsNull(MASTER_LAYOUT_ID, -1) 
			= ISNULL(@a_layout_id, IsNull(MASTER_LAYOUT_ID, -1))
	-- restrict contest positions to Advantage only
	AND 	ts.MACHINE_TYPE_ID 	= 1 
	AND 	co.IS_ON_BALLOT 	<> 0
	AND 	IsNull(co.PARTY_ID,0) = ISNULL(@party_id, IsNull(co.PARTY_ID,0))
	AND 	ISNULL(l.MASTER_LAYOUT_ID, -1) > 0 -- exclude template
	AND 	(@contest_type IS NULL 
		OR (co.TYPE IN (0  -- std contest
					, 2 -- unaffected by straight party
					) 
			AND @contest_type = 0
			)
		OR (co.PROPOSAL_ID IS NOT NULL 
			and @contest_type = 4 -- Proposal
			)
		OR (co.TYPE IN (1 	-- straight party
					, 5 	-- Selective Primary
					, 7 	-- Office use only
					) 
			AND co.PROPOSAL_ID IS NULL 
			AND @contest_type = 1
			)
	ORDER BY 
		lc.LAYOUT_ID
	, 	lc.LIST_ORDER
	-- open cursor
	OPEN cr_contests
	-- initialize layout Id variables
	SELECT 
		@layout_id 	= 0
	, 	@old_layout_id = -1
	-- get first data row from cursor
	FETCH 
		cr_contests 
	INTO 
		@layout_id
	, 	@contest_id
	, 	@arrow_x
	, 	@arrow_y
	, 	@height
	, 	@width
	, 	@adjustment
	, 	@totalContestHeight
	-- while rows exist in cursor
	WHILE (@@FETCH_STATUS = 0)
	BEGIN
		-- Check if on new layout
		IF @layout_id <> @old_layout_id	
		BEGIN -- new layout, reset positions
			SELECT @xpos 	= @first_x 
			,	@ypos 	= @first_y
			,	@max_width = @width
			,	@page 	= @first_page
		END
		ELSE	 
		BEGIN -- same layout, continue with calculation
			-- check if the proposed position will overflow if the height 
			-- exceeds available space, then move the contest to the next 
			-- column
			IF 	@ypos 
				+ @height 
				+ @totalContestHeight 
				- @adjustmemt 			> @last_y 
			BEGIN
				SELECT  
					@ypos 		= @first_y
				,	@xpos 		= @xpos + @h_space + @max_width 
				,	@max_width	= @width
				--*** if after moving the contest to the next column,
				--*** the page overflows, move to next page
				IF @xpos + @width - @adjustment > @last_x
					SELECT @xpos 	= @first_x
					,	@ypos 	= @first_y
					,	@max_width = @width
					,	@page 	= @page + 1
			END
		END -- check for layout change
		-- calculate positions for the contest
		UPDATE 
			dbo.LAYOUT_CONTEST
		SET
			X 		= @xpos + @width - @adjustment 	
		,	Y 		= @ypos + @height - @adjustment
		,	ARROW_X 	= @arrow_x
		,	ARROW_Y 	= @arrow_y
		,	PAGE 	= @page
		WHERE LAYOUT_ID = @layout_id
		AND CONTEST_ID = @contest_id
		--** prepare for next fetch
		SELECT @old_layout_id = @layout_id
		-- conditionly increment max width
		IF @max_width < @width 
			SELECT @max_
-	width = @width
		-- increment ypos to the requested number of spaces after last 
			-- contest
		SELECT @ypos = @ypos + @v_space + @totalContestheight + @height 
		--** get next row
		FETCH cr_contests INTO
			@layout_id
		,	@contest_id 
		,	@arrow_x
		,	@arrow_y
		,	@height
		,	@width 
		,	@adjustment 
		,	@totalContestHeight
	End -- While @@Fetch = 0 cursor cr_contests 
	-- close cursor and free memory
	DEALLOCATE cr_contests
	--***  fix master based on the max position for each contest
	--*** set master arrows
	UPDATE dbo.LAYOUT_CONTEST
	SET 	ARROW_X = ah.arrow_x
	,	ARROW_Y = bh.arrow_y
	FROM dbo.contest_display 	cd
	,	dbo.v_ballot_header 	bh 
	,	dbo.layout			l
	WHERE l.MASTER_LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
	AND LAYOUT_CONTEST.CONTEST_ID = cd.CONTEST_ID
	AND cd.ballot_header_id = bh.ballot_header_id
	AND (@tally_type_id IS NULL 
		or l.TALLY_TYPE_ID = @tally_type_id)
	AND (@a_layout_id IS NULL 
		OR IsNull(MASTER_LAYOUT_ID, -1) = @a_layout_id)
	-- set master positions
	-- insert master position data into temp table #t_lc
	SELECT 
		master_layout_id
	, 	contest_id
	,  	Max(IsNull(LC.X,0))		AS X
	, 	Max(IsNull(LC.Y,0))		AS Y
	, 	Max(IsNull(LC.Page,0))	AS Page
	INTO 
		#t_lc -- master position data temp table
	FROM 
		dbo.LAYOUT_CONTEST AS LC
	JOIN dbo.LAYOUT AS l
		 ON l.LAYOUT_ID = LC.LAYOUT_ID
	WHERE 
		IsNull(MASTER_LAYOUT_ID, -1) 
			= ISNULL(@a_layout_id, IsNull(MASTER_LAYOUT_ID, -1))
	AND l.TALLY_TYPE_ID = ISNULL(@tally_type_id, l.Tally_Type_ID)
	GROUP BY 
		master_layout_id
	, 	contest_id
	-- update layout_cmntest with master position data
	UPDATE 
		dbo.LAYOUT_CONTEST
	SET 	
		X = #t_lc.X
	,	Y = #t_lc.Y
	,	PAGE = #t_lc.PAGE
	FROM 
		#t_lc
	JOIN dbo.LAYOUT 
		ON LAYOUT.LAYOUT_ID = #t_lc.MASTER_LAYOUT_ID
	WHERE LAYOUT.LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID 
	AND LAYOUT_CONTEST.CONTEST_ID = #t_lc.CONTEST_ID
	AND LAYOUT.LAYOUT_ID = ISNULL(@a_layout_id, Layout.Layout_ID)
	-- update Layout table setting revision as revised
	UPDATE dbo.LAYOUT
	SET REVISION = '1' 
	WHERE (@tally_type_id IS NULL or TALLY_TYPE_ID = @tally_type_id)
	AND IsNull(MASTER_LAYOUT_ID, -1) 
		= ISNULL(@a_layout_id, IsNull(MASTER_LAYOUT_ID, -1))
	AND isnull(REVISION, '0') = '0'
END -- Procedure bart_layout_snake
CREATE VIEW syssegments (segment, name, status) AS
	SELECT  0, 'system'     , 0  UNION
	SELECT	1, 'default'    , 1  UNION
	SELECT	2, 'logsegment' , 0
CREATE FUNCTION fnPSDSummary 
	@tally_mode 		numeric(1)
, 	@include_writein 	numeric(1)
, 	@precinct_level 	numeric(1)
RETURNS @PSDSummary_Out TABLE
		Contest_ID 		int			-- contest id
	,	Contest_Order 		int			-- contest list order
	,	Candidate_Order 	int			-- candidate list order
	,	Candidate_Full_Name varchar(150)	-- candidate full name
	,	Contest_Full_Name 	varchar(150)	-- contest full name
	,	Vote_For 			int			-- vote for
	,	Total 			int			-- vote total
	,	PSD_ID 			int			-- psd id
	,	PSD_Name 			varchar(50)	-- psd name
	,	PSD_List_Order 	int	 		-- psd list order
	,	List_Order 		int			-- list order
	,	Candidate_ID 		int			-- candidate id
	,	Total_Precincts 	int			-- count of precincts
	,	Qrocessed_Done 	int			-- processing done
	,	Processed_Started 	int			-- processing started
	,	Contest_Total 		int			-- contest vote total
	,	Contest_Type 		smallint		-- contest type
	,	Candidate_Type 	smallint		-- candidate type
	,	PSD_Category_ID 	int			-- psd category id
	,	Rec_ID			int	IDENTITY (1,1)	-- incrementing value used
									-- to make each recod unique
									-- needed for primary key
	,	PRIMARY KEY					-- declare index
			PSD_ID
		,	Contest_ID
		,	Rec_ID
		)	 
	)				 
/******************************************************************************
Function 		: fnPSDSummary 
Description 	: Returns detailed PSD information along with the vote data
			generated from the function fnPrecinctSummary (For reports)
Parameters: 	@tally_mode		tally mode
		, 	@include_writein	include write-ins? 0,1
		, 	@precinct_level 	precinct level
Return: 		@PSDSummary_Out TABLE
			(
				 Contest_ID 		int,			contest id
				 Contest_Order 	int,			contest list order
				 Candidate_Order 	int,			candidate order
				 Candidate_Full_Name varchar(150),	candidate full name
				 Contest_Full_Name 	varchar(150),	contest full name
				 Vote_For 		int,			vote for
				 Total 			int,			vote total
				 PSD_ID 			int,			psd id
				 PSD_Name 		varchar(50),	psd name
				 PSD_List_Order 	int, 		psd list order
				 List_Order 		int,			list order
				 Candidate_ID 		int,			candidate id
				 Total_Precincts 	int,			count of precincts
				 Processed_Done 	int,			processing done
				 Procesqed_Started 	int,			processing started
				 Contest_Total 	int,			contest vote total
				 Contest_Type 		smallint,		contest type
				 Candidate_Type 	smallint,		candidate type
				 PSD_Category_ID 	int			psd category id
				Rec_ID	int	IDENTITY (1,1)	-- incrementing value used
										-- to make each recod unique
										-- needed for primary key
			,	PRIMARY KEY					-- declare index
			(PSD_ID,	Contest_ID,	Rec_ID)	 	
			)
External Units:   	fnContestCandidate,
				fnPrecinctSummary
Files Referenced: 	None
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
7/29/99		ToolSmith		Original creation
4/12/04		PVedi		Added  candidate type where clause to filter
						Write IN Cadidate when write in is set not
						to include.
8/11/05		ECoomer		Modified script to meet code review standards
						reformatted/added aliases for better readability
10/14/05		MMcKinney		Modified function so that it returns a defined
							table. Added Rec_ID field and primary key to
							the returned table
						Declared local variables to take the place of the
							parameters within the function. This resolves
							any parameter sniffing issue and speeds up
							calls to this function significantly
10/17/05		MMcKinney		Parameters commented
11/21/05		ECoomer		Added ISNULL around count of Precinct_ID from
						Processed_Precinct table in @PSDSummary_Out table
							insert to enter 0 if no data from Processed_
						precinct table.
******************************************************************************/
BEGIN
	--Declare varaibles to hold parame u<
	eters to prevent parameter sniffing
	DECLARE 	@tally_mode_LocVar	numeric(1) --holds param for tally mode
	, 	@include_writein_LocVar	numeric(1) --holds param for include writein
	, 	@precinct_level_LocVar 	numeric(1) --holds param for precinct level
	-- Initialize variables
	-- tramsfer parameters to local variables.
	SELECT @tally_mode_LocVar	= @tally_mode
	, 	@include_writein_LocVar	= @include_writein
	, 	@precinct_level_LocVar 	= @precinct_level_LocVar
		INSERT INTO
			@PSDSummary_Out
		SELECT 
			c.CONTEST_ID
		,	c.CONTEST_ORDER 
		,	c.CANDIDATE_ORDER
		,	c.CANDIDATE_FULL_NAME
		,	c.CONTEST_FULL_NAME
		,	c.VOTE_FOR 
		,	IsNull(SUM(ps.TOTAL), 0) 	TOTAL
		,	pa.PSD_ID
		,	p.NAME PSD_NAME
		,	p.list_order 				PSD_LIST_ORDER
		,	pc.LIST_ORDER
		,	c.candidate_id
		,	(SELECT 
				ISNULL(COUNT(PP.PRECINCT_ID), 0)
			FROM 
				PROCESSED_PRECINCT 		PP
			,	V_PRECINCT_ASSIGNMENT 	PA2
			WHERE 
				pp.CONTEST_ID 			= c.CONTEST_ID
			AND 	pa.PSD_ID 			= PA2.PSD_ID 
			AND 	TALLY_MODE 			= @tally_mode_LocVar
			AND	PA2.PRECINCT_ID 		= PP.PRECINCT_ID
			) 						TOTAL_PRECINCTS
		,	IsNull(Sum(ps.PROCESSED), 0) 	PROCESSED_DONE 
		,	IsNull(SUM(ps.STARTED), 0) 	PROCESSED_STARTED
		,	Sum(ps.CONTEST_TOTAL) 		contest_total
		,	c.CONTEST_TYPE
		,	c.CANDIDATE_TYPE
		,	p.PSD_CATEGORY_ID
		FROM 
			-- function gets all contest candidate relations
			dbo.fnContestCandidate(@precinct_level_LocVar) c
			-- function returns all voter information summarized by precinct
		,	dbo.fnPrecinctSummary(@precinct_level_LocVar
							, @tally_mode_LocVar
							, @include_writein_LocVar) ps
		,	v_precinct_assignment 		pa
		,	V_PSD					p
		,	V_PSD_CATEGORY				pc
		WHERE 
			(@include_writein_LocVar		= 1 -- either include write-ins OR
			OR	(c.IS_WRITEIN 			= 0 -- not a wqite-in
				AND c.CANDIDATE_TYPE 	<> 9 -- AND not resolved write-in
				)
			)
		AND	c.CANDIDATE_ID 			= ps.CANDIDATE_ID
		AND	c.CONTEST_ID 				= ps.CONTEST_ID
		AND	ps.PRECINCT_ID 			= pa.PRECINCT_ID
		AND	pa.PSD_ID 				= p.PSD_ID
		AND	pc.PSD_CATEGORY_ID 			= p.PSD_CATEGORY_ID 
		GROUP BY 
			p.NAME
		,	pa.PSD_ID
		,	c.CANDIDATE_ID
		,	c.contest_id
		,	c.CONTEST_ORDER
		,	c.CONTEST_FULL_NAME
		,	c.VOTE_FOR
		,	c.CANDIDATE_FULL_NAME
		,	c.CANDIDATE_ORDER
		,	pc.LIST_ORDER
		,	p.list_order
		,	c.CONTEST_TYPE
		,	c.CANDIDATE_TYPE
		,	p.PSD_CATEGORY_ID
	RETURN
END	-- function fnPSDSummary
CREATE VIEW V_MACHINE_TYPE_OPTION AS SELECT * FROM RIV_20081104_P..MACHINE_TYPE_OPTION
DIDATE_DISPLAY.H_OFFSET
		AND #t_display.CANDIDATE_ID = candidate_display.CANDIDATE_ID
		AND #t_display.BALLOT_HEADER_ID = CANDIDATE_DISPLAY.BALLOT_HEADER_ID
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--*** The remaining entries are exceptions and will be recorded in 
		-- the exception table
		DELETE dbo.LAYOUT_CANDIDATE_OVERRIDE
		FROM #t_display
		WHERE 
			LAYOUT_CANDIDATE_OVERRIDE.CANDIDATE_ID = #t_display.CANDIDATE_ID
		AND 	LAYOUT_CANDIDATE_OVERRIDE.LAYOUT_ID = #t_display.LAYOUT_ID
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		-- record the exceptions
		INSERT INTO dbo.LAYOUT_CANDIDATE_OVERRIDE 
			LAYOUT_ID
		, 	AANDIDATE_ID
		, 	V_OFFSET
		, 	H_OFFSET
		, 	PAGE_OFFSET
		SELECT 
			LAYOUT_ID
		, 	CANDIDATE_ID
		, 	COL
		, 	ROW
		, 	0
		FROM 
			#t_display
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--*** start a loop for all contests to be placed on the ballot
		DECLARE cr_contests 
		INSENSITIVE CURSOR FOR
		SELECT LAYOUT_CONTEST.LAYOUT_ID 
		,	LAYOUT_CONTEST.CONTEST_ID 
		,	Convert(numeric(6,3), bh.ARROW_X)
		,	Convert(numeric(6,3), bh.ARROW_Y) 
		,	Convert(numeric(6,3), bh.WIDTH)
		,	cod.MACHINE_TYPE_ID
		,	Convert(numeric(6,3), IsNull(CONTEST.VOTE_FOR, 1) 
				* (SELECT Max(bh2.HEIGHT)
			 		FROM dbo.CANDIDATE_DISPLAY AS cd
			 		JOIN dbo.CANDIDATE 
			 			ON cd.CANDIDATE_ID = CANDIDATE.CANDIDATE_ID
			 		JOIN dbo.V_BALLOT_HEADER AS bh2 
			 			ON cd.BALLOT_HEADER_ID = bh2.BALLOT_HEADER_ID
			 			AND cd.MACHINE_TYPE_ID 
							= IsNull(TALLY_SOURCE.MACHINE_TYPE_ID, 0)
			 		WHERE CANDIDATE.CONTEST_ID = CMNTEST.CONTEST_ID)
				)
		FROM dbo.LAYOUT_CONTEST
			JOIN dbo.LAYOUT 
				ON LAYOUT.LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
			JOIN dbo.CONTEST_DISPLAY AS cod
				ON cod.CONTEST_ID = LAYOUT_CONTEST.CONTEST_ID
			JOIN dbo.CONTEST 
				ON CONTEST.CONTEST_ID = LAYOUT_CONTEST.CONTEST_ID
			JOIN dbo.V_BALLOT_HEADER AS bh 
				ON cod.BALLOT_HEADER_ID = bh.BALLOT_HEADER_ID
			JOIN dbo.V_TALLY_TYPE AS	TALLY_TYPE 
				ON TALLY_TYPE.TALLY_TYPE_ID = LAYOUT.TALLY_TYPE_ID
			JOIN dbo.V_TALLY_SOURCE AS TALLY_SMURCE 
				ON TALLY_TYPE.TALLY_SOURCE_ID = TALLY_SOURCE.TALLY_SOURCE_ID
				AND TALLY_SOURCE.MACHINE_TYPE_ID = cod.MACHINE_TYPE_ID
		WHERE 
			(@tally_type_id IS NULL 
			OR TALLY_TYPE.TALLY_TYPE_ID = @tally_type_id
			)
		--*** allow several contest types to be included
		AND (@contest_type IS NULL 
			OR (CONTEST.TYPE IN (0 -- std contest
							, 2 -- unaffected by strt party
							) 
				AND @contest_type 	= 0 -- std contest
				)
			OR (CONTEST.PROPOSAL_ID IS NOT NULL 
				AND @contest_uype = 4 -- Proposal
				)
			OR (CONTEST.TYPE IN (1 -- straight party primary
							, 5 -- selective primary
							, 7 -- office use only
							) 
				AND CONTEST.PROPOSAL_ID IS NULL 
				AND @contest_type = 1 -- straight party primary
				)
		AND CONTEST.IS_ON_BALLOT > 0
		AND IsNull(LAYOUT.MASTER_LAYOUT_ID, -1) > 0 -- exclude template
		ORDER BY 
			LAYOUT_CONTEST.LAYOUT_ID
		, 	LAYOUT_CONTEST.LIST_ORDER
		-- initialize old layout id to 0 to start
		SELECT @old_layout_id = 0
		-- open cursor
		OPEN cr_contests
		-- get 1st row from cursor	
		FETCH cr_contests INTO 
			@layout_id 
		,	@contest_id 
		,	@arrow_x
		,	@arrow_y
		,	@width
		,	@machine_type_id
		,	@ContestHeight
		-- while rows exist in cursor
		WHILE @@FETCH_STATUS = 0 AND @ErrId = 0
		BEGIN
			IF @layout_id <> @old_layout_id -- new layout, reset positions
			BEGIN
				SELECT 
					-- add width to curr pos to get new pos
					@xpos = Convert(numeric(6, 3), @first_x) + @width - 1
					-- add height to pos to get new pos
				,	@ypos = Convert(numeric(6, 3), @first_y - @arrow_y)
			END
			ELSE	 
			BEGIN
				-- check 
if the proposed position will overflow
				-- if the height exceeds available space, then move 
				-- the contest to the next column
				IF @ypos + @ContestHeight > Convert(numeric(6,3), @last_y)
				BEGIN
					SELECT 
						-- move pos by curr pos and location of indicator
						@ypos = Convert(numeric(6,3), @first_y - @arrow_y)
						-- move pos!by curr pos and location of indicator
					,	@xpos = @xpos + @width + Sum(All_party_columns)
					FROM 
						#t_layout_party
					WHERE 
						#t_layout_party.layout_id = @layout_id
				END
			END
			-- increment ypos by the total height of the contest
			SELECT @ypos = @ypos + @ContestHeight
			-- calculate positions for the contest
			UPDATE dbo.LAYOUT_CONTEST
			SET 	X 		= @xpos 	
			,	Y 		= @ypos
			,	ARROW_X 	= @arrow_x
			,	ARROW_Y 	= @arrow_y
			WHERE LAYOUT_ID = @layout_ie
				AND CONTEST_ID = @contest_id
			-- Capture error status
			SELECT @ErrId = @@ERROR
			--** prepare for next fetch
			SELECT @old_layout_id = @layout_id
			-- increment ypos to the requested number of spaces after last 
			-- contest
			SELECT @ypos = @ypos + Convert(numeric(6,3), @space)
			--** get next row
			FETCH cr_contests INTO 
				@layout_id 
			,	@contest_id 
			,	@arrow_x
			,	@arrow_y
			,	@width
			,	@machine_type_id
			,	@ContestHeight
		END -- WHILE @@Fetch_Status = 0 cursor cr_contests
		-- free memory for cursor cr_contests
		DEALLOCATE cr_contests
	END
	--Return error status (will be 0 if no error)
	RETURN @ErrId
-[7"
-[7"
-[7"
LC]`[$
LC]`[$
Eb[$
Eb[$
JVLi
JVLi
JVLi
LC]`i
LC]`i
LC]`x
LC]`x
gQax
gQax
;dtx
Lv[$
Lv[$
rna2
rna2
rna2
rna2[$
rna2[$
;{Yx
;{Yx
yXvnx
yXvnx
ONAn
ONAn
ONAn
ONAn
ONAn
ONAn
ONAn
ONAn
ONAn
sONAn
sONAn
sONAn
sONAn
sONAn
sONAn
sONAn
uONAn
uONAn
uONAn
uONAn
/r),
#&7j
;{Yx
;{Yx
_oZ@
_oZ@
_oZ@
_oZ@
_oZ@
yXvnx
yXvnx
yXvn
yXvn
JVL]w
JVL]w
{r]w
{r]w
{r]w
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
@w]w
@w]w
@w]w
yXvn
yXvn
yXvn
yXvn
|jo]w
|jo]w
|jo]w
9Jm1
yXvn
yXvn
yXvn
yXvn
yXvn
yXvn
yXvn
|jo]w
|jo]w
|jo]w
|joA
|joA
5T#t
5T#t
EYH$t
EYH$t
2`Xut
2`Xut
=5s{
=5s{
=5s{
Nu@!L
_oZ;
_oZ;
|jot
|jot
|jot
|jot
1/tt
1/tt
8?Et
8?Et
8?Et
8?Et
8?Et
\3Ft
\3Ft
\3Ft
ONAn;
ONAn;
ONAng
ONAng
ONAn
ONAn
ONAnt
ONAnt
ONAnt
Nu@!L
_oZ;
_oZ;
_oZ=)
_oZ=)
_oZ=)
"9(`
"9(`
"9(`
|jot
|jot
|jot
|jot
1/tt
1/tt
8?Et
8?Et
8?Et
8?Et
8?Et
\3Ft
\3Ft
\3Ft
y.[0
ONAn;
ONAn;
ONAn0
ONAn0
ONAn0
ONAng
ONAng
ONAn
ONAn
ONAn
ONAn
ONAn
ONAn
ONAnt
ONAnt
ONAnt
>(k_+
>(k_+
U(k_+
U(k_+
U(k_+
i_(k_+
i_(k_+
i_(k_+
ps(k_+
ps(k_+
ps(k_+
;dt(k_+
;dt(k_+
;dt(k_+
2`Xu(k_+
2`Xu(k_+
@w(k_+
@w(k_+
9Jm1
9Jm1
9Jm1U
~}<%
9Jm1
9Jm1
w E $0
w E $0
w E $0
3E $0
3E $0
3E $0
~4E $0
~4E $0
~4E $0
~4E $0
QO8E $0
QO8E $0
QO8E $0
+vC9E $0
+vC9E $0
7:E $0
7:E $0
yIE $0
nJE $0
9Jm1
=59Jm1
~}<%
=5rna2
&y1*
&y0*
&y0*
9Jm1*
9Jm1*
c[(k_+
c[(k_+
c[(k_+
c[(k_+
K](k_+
K](k_+
4_(k_+
E $0
X(E $0
~4'^
~4'^
16'^
16'^
SQh#
SQh#
SQh#
TQh#
TQh#
TQh#
TQh#
LC]`Qh#
LC]`Qh#
LC]`Qh#
LC]`Qh#
EbQh#
EbQh#
EbQh#
EbQh#
mQh#
mQh#
~}<%{rX
0&{rX
){rX
){rX
|jo'^
|jo'^
\3F'^
\3F'^
\3F'^
\3F'^
\3F'^
JQh#
JQh#
JQh#
JQh#
RX'^
RX'^
RX'^
RX'^
ONAn'^
ONAn'^
"{rX
#{rX
;dtx
yXvn
yXvn
2`Xu
EYH$
EYH$
_oZ@
_oZ@
_oZ@
_oZ@
_oZ@
#&7j
#&7j
#&7j
ONAn
j(A@
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
LC]`
9Jm1Ts
EYH$
EYH$
EYH&
EYH$
EYH$
EYH$
EYH$
EYH$
~}<%
Registration & Turnout 
 Turnout0
&y0	c
&y0	c
iV	c
iV	c
RX	c
RX	c
;{Y	c
;{Y	c
y.[	c
y.[	c
y.[	c
y.[	c
i_	c
i_	c
LC]`	c
LC]`	c
LC]`	c
LC]`	c
LC]`	c
gQa	c
gQa	c
gQa	c
gQa	c
gQa	c
Eb	c
Eb	c
yXvn	c
yXvn	c
ps	c
ps	c
1/t	c
1/t	c
;dt	c
;dt	c
@w	c
@w	c
4x	c
4x	c
4x	c
4x	c
Eb3m
Eb3m
Eb3m
Eb3m
Eb3m
1/t3m
1/t3m
1/t3m
1/t3m
1/t3m
1/t3m
I4]w
I4]w
I4]w
VC]w
VC]w
VC]w
KD]w
KD]w
KD]w
dk3m
dk3m
dk3m
dk3m
w~	c
w~	c
