#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#                                                                     #
# THIS MODULE CONTAINS VARIOUS FUNCTIONS USED BY DODO.                #
# These functions allow DNA reception to return prepared DNA to       #
# Fingerprinters in the form of a rack of eppendorfs                  #
#                                                                     #
#######################################################################
package Quality;
use strict;

sub new
{
my $pkg = shift;
my $setup = {};
$setup->{Contents} = [];
bless $setup,$pkg;
}



sub Initialiseobject
{
my $pkg = shift;
# pull out speciesname and chromosome name of all clones that have been actioned.

$pkg->{ListSQL} = "	SELECT 	UNIQUE CD.SPECIESNAME, CD.CHROMOSOME 
			FROM 	CHROMOSOMEDICT CD, CLONE C, GLYSTOCK G, DNAPREP DP, 
				DNAPREP_STATUS DS, CLONE_CULTURE CC
			WHERE	CD.ID_DICT 		= C.CHROMOSOME
			AND	C.CLONENAME		= G.CLONENAME
			AND	G.ID_GLYSTOCK 		= CC.ID_GLYSTOCK
			AND	CC.ID_CULTURE 		= DP.ID_CULTURE
			AND	DP.ID_PREP 		= DS.ID_PREP					
			AND	DS.STATUS 		= 2
			AND	DS.ISCURRENT		= 1";

$pkg->{ResultSQL} ="	SELECT	C.CLONENAME, 
				PD.DESCRIPTION, 
				DP.ID_PREP, 
				P.EMAIL,
				CC.ID_REQUEST,
				TO_CHAR(ST.STATUSDATE,'dd-MM-YY') SD,  
				DP.COMMENTS,
				CP.PRIORITY - 1, 
				CD.CHROMOSOME,
				DP.PREP_NUMBER
			FROM	DNAPREP DP, 		DNAPREP_STATUS DS,	DNAPREP_STATUS ST,
				CLONE C, 		CHROMOSOMEDICT CD, 	PERSON P,	
				TEAM_PERSON_ROLE T, 	CLONE_CULTURE CC, 	DNAPREPTYPEDICT PD,
				CLONE_PRIORITY CP
			WHERE	P.ID_PERSON		= T.ID_PERSON
			AND	T.ID_ROLE		= DP.OPERATOR
			AND	PD.ID_DICT		= DP.PREP_TYPE
			AND	CP.IS_CURRENT		= 1
			AND	CP.CLONENAME		= DP.CLONENAME
			AND	CC.ID_CULTURE		= DP.ID_CULTURE
			AND	ST.STATUS		= 1
			AND	ST.ID_PREP		= DP.ID_PREP
			AND	CD.CHROMOSOME 		LIKE 'VAR1'
			AND	CD.SPECIESNAME 		LIKE 'VAR2' 
			AND	CD.ID_DICT		= C.CHROMOSOME
			AND	C.CLONENAME 		= DP.CLONENAME
			AND	DP.ID_PREP		= DS.ID_PREP
			AND	DS.STATUS 		= 2	
			AND	DS.ISCURRENT		= 1
			ORDER BY DP.PREP_TYPE,SD,C.CLONENAME, DP.PREP_NUMBER";

$pkg->{Button1Label}	= 'QC Check';
$pkg->{Duplicatecheck} 	= 2;

$pkg->{Columnheaders} = "Clone";				
while(length($pkg->{Columnheaders}) < 17)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}
	
$pkg->{Columnheaders} = "$pkg->{Columnheaders} | Prep";
while(length($pkg->{Columnheaders}) < 24)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}


$pkg->{Columnheaders} = "$pkg->{Columnheaders} | By";
while(length($pkg->{Columnheaders}) < 32)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}
	

$pkg->{Columnheaders} = "$pkg->{Columnheaders} | Start Date";
while(length($pkg->{Columnheaders}) < 46)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}
	
$pkg->{Columnheaders} = "$pkg->{Columnheaders} | Method";
while(length($pkg->{Columnheaders}) < 68)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}

$pkg->{Columnheaders} = "$pkg->{Columnheaders} | Plate Name";
while(length($pkg->{Columnheaders}) < 92)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}

$pkg->{Columnheaders} = "$pkg->{Columnheaders} | Chr";
while(length($pkg->{Columnheaders}) < 99)
	{$pkg->{Columnheaders} = "$pkg->{Columnheaders} ";}


	
$pkg->{Columnheaders} = "$pkg->{Columnheaders}|";
# [a,b] a = content array index, b = width of field to display -1
# Internal column headings = sum b + 2 end one + 3
$pkg->{Column} = [[0,15,'Clone'],[9,5,'Prep'],[3,6,'By'],[5,12,'Start Date'],[1,20,'Method'],[6,22,'Plate Name'],[8,4,'Chromosome']];					

				
				
undef $pkg->{Selection};

$pkg->Addtobrowse();
undef @{$pkg->{Contents}};
$pkg->Refresh();
$pkg->{Title}->configure(-text => "Operator : $pkg->{Who}\tRole Type : $pkg->{Roletype}\t\tSEND DNAprep To QC (Finger printing) ");

}


sub Killprep
{
my $pkg = shift;
my $sql;
my @multisql;
my $radio;
my $ref;

$pkg->radiocheck(\$radio,'Failed Gell Check','Selected By Mistake');
$pkg->{Mainwin}->waitVariable(\$radio);
if($radio !~ /\D/){return;}
if($radio =~ /^Failed Gell Check/)
	{$radio = 3;}			
else
	{$radio = 1;}


		
foreach (@{$pkg->{Contents}})
{

	if($radio == 1)
	{
	# Preps Haven't been made yet
	$sql = "DELETE FROM 	DNAPREP_STATUS 
		WHERE 		(ID_PREP 	= $_->[2])	AND
				(STATUS		=	2)	AND
				(ISCURRENT	=	1)	";
	push @multisql,$sql;
	
	
	# roll back to previous status entry, I.E ACTIONED
	$sql = "UPDATE 	DNAPREP_STATUS 
		SET 	ISCURRENT = 1 
		WHERE 	(STATUS 	= 1)	AND
			(ISCURRENT	= 0)	AND
			(ID_PREP  	= $_->[2])";
	
	push @multisql,$sql;
	
	
	$pkg->MultiWritedb(@multisql);
	undef @multisql;
	}
	else
	{
	# Check to see how many completed preps on request 
	$sql = "SELECT 	COUNT(DNAPREP.ID_PREP)
		FROM 	DNAPREP, DNAPREP_STATUS, CLONE_CULTURE, DNAREQUEST_STATUS
		WHERE 	(DNAPREP_STATUS.STATUS		= 7)				AND
			(DNAPREP_STATUS.ISCURRENT	= 1)				AND
			(DNAPREP.ID_PREP 		= DNAPREP_STATUS.ID_PREP)	AND
			(CLONE_CULTURE.ID_CULTURE	= DNAPREP.ID_CULTURE)		AND
			(DNAREQUEST_STATUS.ID_REQUEST	= CLONE_CULTURE.ID_REQUEST)	AND
			(DNAREQUEST_STATUS.STATUS	= 2)				AND
			(DNAREQUEST_STATUS.ISCURRENT	= 1)				AND
			(DNAREQUEST_STATUS.ID_REQUEST	= $_->[4])";

	$ref = $pkg->Querydb($sql);
	
	my $completed = $ref->[0][0];
	
	# Check to see how many live preps on request Don't count succesfully completed
	$sql = "SELECT 	COUNT(DNAPREP.ID_PREP)
		FROM 	DNAPREP, DNAPREP_STATUS, CLONE_CULTURE, DNAREQUEST_STATUS
		WHERE 	(DNAPREP_STATUS.STATUS		< 7)				AND
			(DNAPREP_STATUS.ISCURRENT	= 1)				AND
			(DNAPREP.ID_PREP 		= DNAPREP_STATUS.ID_PREP)	AND
			(CLONE_CULTURE.ID_CULTURE	= DNAPREP.ID_CULTURE)		AND
			(DNAREQUEST_STATUS.ID_REQUEST	= CLONE_CULTURE.ID_REQUEST)	AND
			(DNAREQUEST_STATUS.STATUS	= 2)				AND
			(DNAREQUEST_STATUS.ISCURRENT	= 1)				AND
			(DNAREQUEST_STATUS.ID_REQUEST	= $_->[4])";

	$ref = $pkg->Querydb($sql);
	
	
	$sql = "UPDATE 	DNAPREP_STATUS 
		SET 	ISCURRENT = 0 
		WHERE 	ISCURRENT = 1 AND ID_PREP = $_->[2]";
	
	push @multisql,$sql;
	# failed in dnaprep
	$sql = "INSERT INTO 	DNAPREP_STATUS	(ID_PREP, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
		VALUES				($_->[2] ,9,SYSDATE,1, $$, 'DODO','$pkg->{Who}')";
				
	push @multisql,$sql;
	
	
	if($ref->[0][0] == 1)
	{
		$sql = "UPDATE 	DNAREQUEST_STATUS 
			SET 	ISCURRENT = 0 
			WHERE 	ISCURRENT = 1 AND ID_REQUEST = $_->[4]";
		
		push @multisql,$sql;

	if($completed == 0)
	{
		$sql = "INSERT INTO 	DNAREQUEST_STATUS	(ID_REQUEST, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
			VALUES					($_->[4] , 3,SYSDATE,1, $$, 'DODO','$pkg->{Who}')";
	
		push @multisql,$sql;
	}
	else
	{# Complete request
		$sql = "INSERT INTO 	DNAREQUEST_STATUS	(ID_REQUEST, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
			VALUES					($_->[4] , 4,SYSDATE,1, $$, 'DODO','$pkg->{Who}')";
	
		push @multisql,$sql;
	}
	}
	}# else on fail reason
	
	eval{$pkg->MultiWritedb(@multisql);
	undef @multisql;
	};if($@){print STDERR "Function	: QUALITY::Killprep\nRequest ID	: $_->[4]\nPrep ID	: $_->[2]\n\n$@\n";}
	}# for loop

undef @{$pkg->{Contents}};
$pkg->Refresh();
$pkg->Addtobrowse();
$pkg->Addtoresults();

}


sub Action {
my $pkg = shift;
my $sql;
my @multisql;
my $ref;
my $i;
my $radio;

$pkg->radiocheck(\$radio,'Assign To Plate','Send For Library Making','Return To Finger Printers','Put On Hold');
$pkg->{Mainwin}->waitVariable(\$radio);
if($radio !~ /\D/){return;}
if($radio =~ /^Assign To Plate/)
{	
	
	my $d;
	($i, $d) = $pkg->Getplateid("Enter description of gell check rack");
	if((!defined $i)||(!defined $d))
		{return;}		
	# Go through selection
	CLONE: foreach (@{$pkg->{Contents}})
	{		
		
	while($i > 96)
	{# To allow additions to a rack of eppendorfs after initial creation
		($i, $d) = $pkg->Getplateid("Rack Full ! Enter New Rack Description");
		
		if((!defined $i)||(!defined $d))
		{ 	last CLONE;
		}
	}
	
	my $c = "$d\-$i";
	if($d eq 'CLEAR'){$c = '';}
	
	$sql = "UPDATE 	DNAPREP 
		SET 	COMMENTS 	= '$c'
		WHERE	ID_PREP		= $_->[2]";
		
	push @multisql,$sql;
	
	#May need if users insist on blanks fro on going preps
	#$sql = "SELECT 	COUNT(DNAPREP.ID_PREP)
	#	FROM 	DNAPREP, DNAPREP_STATUS, CLONE_CULTURE, DNAREQUEST_STATUS
	#	WHERE 	(DNAPREP_STATUS.STATUS		< 2)				AND
	#		(DNAPREP_STATUS.ISCURRENT	= 1)				AND
	#		(DNAPREP.ID_PREP 		= DNAPREP_STATUS.ID_PREP)	AND
	#		(CLONE_CULTURE.ID_CULTURE	= DNAPREP.ID_CULTURE)		AND
	#		(DNAREQUEST_STATUS.ID_REQUEST	= CLONE_CULTURE.ID_REQUEST)	AND
	#		(DNAREQUEST_STATUS.STATUS	= 2)				AND
	#		(DNAREQUEST_STATUS.ISCURRENT	= 1)				AND
	#		(DNAREQUEST_STATUS.ID_REQUEST	= $_->[4])";

	#$ref = $pkg->Querydb($sql);
	
	#$i = $i + $ref->[0][0];
	
	
	eval{
	$pkg->MultiWritedb(@multisql);
	undef @multisql;
	};if($@){print STDERR "Function	: QC Check::Action\nRequest ID	: $_->[4]\nPrep ID	: $_->[2]\n\n$@\n";}
	$i++;
	
	}

}
elsif($radio =~ /^Return To Finger Printers/)
{

foreach (@{$pkg->{Contents}})
{		
eval{	
	$sql = "UPDATE 	DNAPREP_STATUS
		SET 	ISCURRENT 	= 0
		WHERE	ISCURRENT 	= 1	AND
			ID_PREP 	= $_->[2]";
			
	push @multisql,$sql ;
	
	$sql = "INSERT INTO DNAPREP_STATUS	(ID_PREP, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
		VALUES				($_->[2] ,3,SYSDATE,1, $$, 'DODO','$pkg->{Who}')";
					
	push @multisql,$sql;
	
	
	$pkg->MultiWritedb(@multisql);
	undef @multisql;
	};if($@){print STDERR "Function	: QUALITY::Action\nRequest ID	: $_->[4]\nPrep ID	: $_->[2]\n\n$@\n";}

$i++;
}

$pkg->Projectstatus(6);
}
elsif($radio =~ /Put On Hold/)
{

foreach (@{$pkg->{Contents}})
{
# Now Update Prep Status
$sql = "UPDATE 	DNAPREP_STATUS
		SET 	ISCURRENT 	= 0
		WHERE	ISCURRENT 	= 1	AND
			ID_PREP 	= $_->[2]";
			
push @multisql,$sql ;
	
$sql = "INSERT INTO DNAPREP_STATUS	(ID_PREP, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
	VALUES				($_->[2] ,11,SYSDATE,1, $$, 'DODO','$pkg->{Who}')";
					
push @multisql,$sql;
	
$pkg->MultiWritedb(@multisql);
undef @multisql;
}



$pkg->Projectstatus(26,1);
}
else
{
my @mail;
# send preps to Subcloning
eval {
 $pkg->Requestdigest();


my @preps;
my @requests;

foreach (@{$pkg->{Contents}})
{
	my $number;
	my $plate;

	($plate, $number) = split /-/,$_->[6];

	$plate =~ s/PLATE//g;

	$plate = $plate/1;
	my $h = 0;
	foreach my $m (@mail)
	{
		if($m == $plate){$h = 1;last;}
	}
	
	if($h == 0){push @mail, $plate;}
	
	push @preps,$_->[2];
}

foreach(@preps)
{
$sql = "SELECT 	COUNT(DR.ID_REQUEST)
	FROM 	DNAPREP DP, DNAPREP_STATUS DS, CLONE_CULTURE CC, 
		DNAREQUEST_STATUS DR
	WHERE 	DS.STATUS	< 7
	AND	DS.ISCURRENT	= 1
	AND	DS.ID_PREP    	= DP.ID_PREP 
	AND	NOT DP.ID_PREP 	= $_
	AND	DP.ID_CULTURE 	= CC.ID_CULTURE
	AND	CC.ID_REQUEST 	= DR.ID_REQUEST
	AND	DR.STATUS	= 2
	AND	DR.ISCURRENT	= 1
	AND	DR.ID_REQUEST =(SELECT	C.ID_REQUEST
				FROM	DNAPREP D, CLONE_CULTURE C
				WHERE	C.ID_CULTURE	= D.ID_CULTURE
				AND	D.ID_PREP	= $_
				AND	ROWNUM		= 1)";
				

	my $ref = $pkg->Querydb($sql);
	if($ref->[0][0] == 0)
	{# Record preps were no other preps on request detected, for completion.
		push @requests, $_;
	}

}



# Now Update Prep Status

$sql = "UPDATE 	DNAPREP_STATUS
	SET 	ISCURRENT 	= 0
	WHERE	ISCURRENT 	= 1	
	AND	ID_PREP 	= ?";



$pkg->Boundwrite($sql, \@preps);

$sql = "INSERT INTO DNAPREP_STATUS	(ID_PREP, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
	VALUES				(? ,7,SYSDATE,1, $$, 'DODO','$pkg->{Who}')";
	
$pkg->Boundwrite($sql, \@preps);




$sql = "UPDATE 	DNAREQUEST_STATUS 
	SET 	ISCURRENT 	= 0 
	WHERE 	ISCURRENT 	= 1 
	AND 	ID_REQUEST  = (	SELECT	C.ID_REQUEST
				FROM	DNAPREP D, CLONE_CULTURE C
				WHERE	C.ID_CULTURE	= D.ID_CULTURE
				AND	D.ID_PREP	= ?
				AND	ROWNUM		= 1)";
	
$pkg->Boundwrite($sql, \@requests);

	
$sql = "INSERT INTO 	DNAREQUEST_STATUS	(ID_REQUEST, STATUS, STATUSDATE, ISCURRENT, SESSIONID, PROGRAM, OPERATOR)
	SELECT	C.ID_REQUEST,4,SYSDATE,1, $$, 'DODO','$pkg->{Who}'
	FROM	DNAPREP D, CLONE_CULTURE C
	WHERE	C.ID_CULTURE	= D.ID_CULTURE
	AND	D.ID_PREP	= ?
	AND	ROWNUM		= 1";		
	
$pkg->Boundwrite($sql, \@requests);



};if($@){print STDERR "Function	: QUALITY::Action\nRequest ID	: $_->[4]\nPrep ID	: $_->[2]\n\n$@\n";}

#Cleared For library Making
$pkg->Projectstatus(31);


if($mail[0])
{
eval{ 

open (MAIL,"|/usr/bin/mail rs2");
print MAIL "From: DODO                <>\n";
print MAIL "To: rs2\n";
print MAIL "Subject: Preps Arriving from DNA Reception\n\n";
print MAIL "This is an Automated Mail Message from Oracle.\n\n";
print MAIL "********************************************************\n";
print MAIL " THESE PREPS HAVE COME DIRECTLY FROM DNA RECEPTION THEY\n";
print MAIL "         HAVE NOT BEEN REFINGERPRINTED / FISHED\n";
print MAIL "********************************************************\n";
print MAIL "The following Plates will shortly be arriving from DNA Reception : $pkg->{Who}\n";
foreach(@mail){print MAIL "PLATE $_\n";}
print MAIL "A Text FILE for each of these files can be found in\n\n /nfs/digest/imdata/human plates \n\n";
close(MAIL);

foreach(@mail){
open (FHDL, ">/nfs/digest/imdata/human_plates/plate$_.csv") or die "Cannot Access the image Directory\n";

$sql = "SELECT D.CLONENAME||'-'||D.PREP_NUMBER||','||TO_CHAR(M.STATUSDATE,'DD.MM.YY')||','||SUBSTR(D.COMMENTS,7,INSTR(D.COMMENTS,'-')-7)||','||upper(R.MAPROW)||R.MAPCOL Info
	FROM	DNAPREP D, DNAPREP_STATUS DS, DNAPREP_STATUS M, ROWCOL_MAP R
	WHERE	R.ID_MAP	= SUBSTR(D.COMMENTS,INSTR(D.COMMENTS,'-')+1)
	AND	M.STATUS	= 1
	AND	M.ID_PREP	= DS.ID_PREP
	AND	DS.STATUS	= 7
	AND	DS.ID_PREP	= D.ID_PREP
	AND	D.COMMENTS 	LIKE '%$_-%'
	ORDER BY R.ID_MAP";

$ref = $pkg->Querydb($sql);

foreach my $p (@{$ref})
{
	print FHDL "$p->[0]\n";
}

close FHDL;
}
};if($@){
		open (MAIL,"|/usr/bin/mail bsw");
		print MAIL "From: DODO  ERROR              <>\n";
		print MAIL "To: bsw\n";
		print MAIL "Subject: Error Writing plates\n\n";
		print MAIL "DODO Had trouble writing out QC plates to Digests\n\n";
		close(MAIL);
	}
}



}
undef @{$pkg->{Contents}};
$pkg->Refresh();
$pkg->Addtobrowse();
$pkg->Addtoresults();



}







sub Lock
{
my $pkg = shift;
my $sql;


if($pkg->{dbh}){$pkg->{dbh}->commit;}

$sql = "	SELECT 	DNAPREP_REQUEST.ID_REQUEST 
		FROM 	DNAPREP_REQUEST, DNAREQUEST_STATUS, GLYSTOCK, CLONE_CULTURE, DNAPREP, DNAPREP_STATUS
		WHERE	(DNAPREP_STATUS.ID_PREP		= DNAPREP.ID_PREP)		AND
			(DNAPREP.ID_CULTURE		= CLONE_CULTURE.ID_CULTURE)	AND
			(CLONE_CULTURE.ID_GLYSTOCK	= GLYSTOCK.ID_GLYSTOCK)		AND
			(GLYSTOCK.ID_GLYSTOCK		= DNAPREP_REQUEST.ID_GLYSTOCK)	AND
			(DNAREQUEST_STATUS.ID_REQUEST	= DNAPREP_REQUEST.ID_REQUEST)	AND
			(";

$sql = "	SELECT 	DNAPREP.ID_PREP
		FROM	DNAPREP, DNAPREP_STATUS
		WHERE	DNAPREP_STATUS.ID_PREP = DNAPREP.ID_PREP	AND
			(";



			
foreach(@{$pkg->{Data}})
{
	$sql = "$sql\nDNAPREP.ID_PREP 		= $_->[2] OR";
}
$sql = substr($sql,0,length($sql)-2);
$sql = "$sql)\nFOR UPDATE NOWAIT";

return $sql;

}

sub Getplateid
{
my ($pkg, $title) = @_;
my $d;
my $p;
my $sql;
my $ref;
my $c = 0;

$pkg->InputBox($title, \$d);
$pkg->{Mainwin}->waitVariable(\$d);

if(($d !~ /\D/)&&($d == 0))
	{return;}


$d = substr($d,0,15);
$d = uc($d);
$d =~ s/-/_/g;

$sql = "SELECT  DNAPREP.COMMENTS
	FROM	DNAPREP 
	WHERE	DNAPREP.COMMENTS LIKE '$d%'";

$ref = $pkg->Querydb($sql);

foreach (@{$ref})
{
my $t;
($t,$t) = split /-/,$_->[0];
if($t > $c){$c = $t;}
}
$c++;
return ($c, $d);

}

sub Requestdigest {
my $pkg = shift;
my $sql;
my $ref;
my $person;
my $confirm;
my $mail;

$sql = "SELECT ID_PERSON FROM PERSON WHERE EMAIL = '$pkg->{Who}'";
$ref = $pkg->Querydb($sql);
$person = $ref->[0][0];

$confirm = Database->new();
$confirm->Initialisedb();
$confirm->Userdb('image');
$confirm->Connect_To_DB();
#my $PrepsDone;

my @multisql;
foreach (@{$pkg->{Contents}})
{		
my $priority = 0;
my $sample;


	$sample = substr($_->[5],3);
	$sample =~ s/\-/\./g;
	$sample = "$sample/$_->[9]";

	if($_->[7] > 0){$priority = 1}
	
##########################################
#Made check to avoid duplicate inserts
#if(index($PrepsDone,$_->[2]) > -1){next;}
#$PrepsDone = $PrepsDone."$_->[2],";	
##########################################

foreach my $enzyme ('EcoRI','HindIII','BamHI')
{
	# Trigger on rdrequest for clonename on id_dnaprep
	$sql	= "	INSERT INTO RDREQUEST 	(ID_RDREQUEST, CLONENAME, REQUESTED_BY, REQUEST_DATE, IS_FASTTRACK, ID_DNAPREP, SAMPLE_REF)
			VALUES			(SEQ_RDREQUEST.NEXTVAL, 'A',$person, SYSDATE, $priority, $_->[2], '$sample')";
	
	push @multisql,$sql;
	
	
	$sql	= "	INSERT INTO RDREQUEST_STATUS 	(ID_RDREQUEST, STATUS, STATUSDATE, ISCURRENT, PROGRAM, OPERATOR)
			VALUES				(SEQ_RDREQUEST.CURRVAL, 1, SYSDATE, 1, 'DODO','$pkg->{Who}')";
	
	push @multisql,$sql;
		
	$sql	= "	INSERT INTO RDREQUEST_ENZYME 	(ID_RDREQUEST, ENZYMENAME)
			VALUES				(SEQ_RDREQUEST.CURRVAL,'$enzyme')";
	
	push @multisql,$sql;

	
}
	$confirm->MultiWritedb(@multisql);
	undef @multisql;
	
}

$confirm->{dbh}->disconnect;

}

1;
