#!/usr/bin/perl -w
use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost','parcdemog','parc9904')or die "Couldn't connect to database: " . DBI->errstr;
##Start Uploading
use File::Basename; 
$CGI::POST_MAX = 1024 * 5000; 
my $safe_filename_characters = "a-zA-Z0-9_.-"; 
my $upload_dir = "/var/www/vhosts/arabdemog.com/httpdocs/cf/images"; 
my $query = new CGI; 
my $filename = $query->param("photo"); 
my $visitCode=param('visitCode');
##End Uploading
sub escapedStr{
$out="";
$str=$_[0];
$len=length($str);
for ($i=0;$i<$len;$i++)
{
	$checkChar=substr($str,$i,1);
	if ($checkChar eq "'" || $checkChar eq "\"")
	{
		$out=$out."\\'";
	}
	else
	{
		$out=$out.$checkChar;
	}
}
return $out;
}
print <<blk;
content-type: text/html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
blk

my $country = param("conCode");
my $city = param("cityCode");
my $storeType   = param("typeCode");
my $sDate=param("datum1");	
my @arsdate=split('-',$sDate);
$sDate=$arsdate[2]."-".$arsdate[1]."-".$arsdate[0];
my $tilt=param("tilt");	
my $location= param("locCode");
##
my $strQuery="SELECT max(convert(right(questNo,4),SIGNED))+1 FROM cf_entry";
$sthM=$dbh->prepare($strQuery);
$sthM->execute;

while(@row = $sthM->fetchrow_array ) 
	{ 
		$intL= length("@row[0]");
		if ($intL == 1) 
		{
			$quno="0000@row[0]";
		}
		elsif ($intL == 2) 
		{
			$quno="000@row[0]";
		}
		elsif ($intL == 3) 
		{
			$quno="00@row[0]";
		}
		elsif ($intL == 4) 
		{
			$quno="0@row[0]";
		}
		elsif ($intL == 5) 
		{
			$quno="@row[0]";
		}
		#$intL= 0-length("0000@row[0]")+1;
		#$questNo="$location".substr("0000@row[0]",$intL,5);
		$questNo="$location".$quno;
	}
##
my $ath=param("ath");
my $atm=param("atm") - 1;
if (param("atap") == 1)
{
	$atampm='AM';
}
elsif (param("atap")== 2)
{
	$atampm='PM';
}
my $aTime=$ath.":".$atm." ".$atampm;
my $shID=param("shID");	
my $Nationality=param("nat");	
my $Gender=param("gender");	
my $Age=param("age");
my $dth=param("dth");	
my $dtm=param("dtm") - 1;
if (param("dtap") == 1)
{
	$dtampm='AM';
}
elsif (param("dtap")== 2)
{
	$dtampm='PM';
}
my $dTime = $dth.":".$dtm." ".$dtampm;
my $stName=param("stName");
my $stGender=param("stGender");	
my $receipt = escapedStr(param("receipt"));	
my $amount=param("amount");	
my $DOW=param("DOW");	

my $q1=param("q1");
my $c1=escapedStr(param("c1"));	
my $q2=param("q2");	
my $c2=escapedStr(param("c2"));	
my $q3=param("q3");	
my $c3=escapedStr(param("c3"));	
   
my $q4=param("q4");	
my $c4=escapedStr(param("c4"));	
my $q5=param("q5");	
my $c5=escapedStr(param("c5"));	
my $q6=param("q6");	
my $c6=escapedStr(param("c6"));	
my $q7=param("q7");	
my $c7=escapedStr(param("c7"));	
my $q8=param("q8");	
my $c8=escapedStr(param("c8"));	
my $q9=param("q9");	
my $c9=escapedStr(param("c9"));	
my $q10=param("q10");	
my $c10=escapedStr(param("c10"));
my $observation=escapedStr(param("c11"));
   
my $lang = param("lang");	

if ($q1 eq "") { $q1=-1; }
if ($q2 eq "") { $q2=-1; }
if ($q3 eq "") { $q3=-1; }
if ($q4 eq "") { $q4=-1; }
if ($q5 eq "") { $q5=-1; }
if ($q6 eq "") { $q6=-1; }
if ($q7 eq "") { $q7=-1; }
if ($q8 eq "") { $q8=-1; }
if ($q9 eq "") { $q9=-1; }
if ($q10 eq "") { $q10=-1; }
##Start Uploading
if ( !$filename ) 
{ 
print "There was a problem uploading the receipt (try a smaller file)."; 
exit; 
}
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
$filename = $questNo . lc($extension); $filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g; 
if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
{ 
$filename = $1; 
} 
else 
{ 
die "Filename contains invalid characters"; 
} 

my $upload_filehandle = $query->upload("photo"); 
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die " Can't write to  $upload_dir/$filename $!"; 
binmode UPLOADFILE; 
while ( <$upload_filehandle> ) 
{ 
print UPLOADFILE; 
} 
close UPLOADFILE; 
##End Uploading

@gettheip=split(/\./,$ENV{'REMOTE_ADDR'});
my $eip="$gettheip[0].$gettheip[1].$gettheip[2].$gettheip[3]";
	my $strsql="insert into cf_entry (country,city,storeType,sDate,tilt,location,aTime,shID,Nationality,Gender,Age,dTime,stName,stGender,receipt,amount,DOW,questNo,q1,c1,q2,c2,q3,c3,q4,c4,q5,c5,q6,c6,q7,c7,q8,c8,q9,c9,q10,c10,observation,edate,userip,lang,receiptName,isApproved) values
	($country,$city,$storeType,'$sDate','$tilt','$location','$aTime',$shID,$Nationality,$Gender,$Age,'$dTime','$stName',$stGender,'$receipt',$amount,$DOW,'$questNo',$q1,'$c1',$q2,'$c2',$q3,'$c3',$q4,'$c4',$q5,'$c5',$q6,'$c6',$q7,'$c7',$q8,'$c8',$q9,'$c9',$q10,'$c10','$observation',now(),'$eip','$lang','$filename','F')";
	my $rv=$dbh->do($strsql);
	if ($rv==1) 
	{
		$msg="Updation Successful.";
		my $rv1=$dbh->do("update cf_visitDetail set hasVisited='T' where visitCode='$visitCode'");
		my $rv2=$dbh->do("update cf_visitPlan set hasVisited='T' where planCode in (select planCode from cf_visitDetail where visitCode='$visitCode')");
	}
	else
	{
		$msg="Unable to Update";
	}
	$dbh->disconnect;
	

print<<blk1;
<tableborder="1"cellpadding="0"cellspacing="0"width="100%"bordercolor="#000080"height="359">
<tr>
<tdwidth="100%"height="357">
<palign="center"><fontface="Verdana"color="#000080"size="5"><b>$msg</b></font></td>
</tr>
</table>
</body>

</html>
blk1
