<?php 
#for bidp 14de05 0.0.1 updated 15de05, 26je06
session_start();


#echo '<p>post=';
#print_r($_POST);


#@_OUTLINE_PART_1_
#constants
$key1="bidp";
$data_base="$key1";
$table1='certs3';
$include="test.php"; #change to test.php for easyphp
$connect="pear_connect"; #change to bidp_connect for easyphp, pear_connect elsewhere
$filesubdir="/php/$key1/";
#$homefile="http://localhost/";
$homefile="https://bidp.bw";
$_SESSION['homefile']=$homefile;
$thisfile=$homefile.$_SERVER[PHP_SELF];
#echo '<p>thisfile=' .$thisfile; #ok
$_SESSION['thisfile']=$thisfile;
$headwords=strtoupper($key1)." certificate of making good defects: ";
#constants


include "$include";
include 'allhere.php';
require 'DB.php'; #pear only


#import from userin or posted data
if ($_POST['start']=='')
	{$start=1;}

#$today=date("Y-m-d");
	
#------------------------------------------
if ($_POST["start"]==2) #in second pass, check data entered in first pass
{
	#echo '<p>ref=' .$_POST['jobrefn']; #ok
	if ($_POST['jobrefn']>'')
	{
		#print '<p>ref=' .$_POST['jobrefn'].' test'; #ok
		$jobref=$_POST["jobrefn"];
		#$jobref=mysql_real_escape_string($jobref); #inop
		$_SESSION['jobref']=$jobref;
		#print '<p>ref1=' .$jobref.' here!'; #ok
		$jobrefn=$_POST["jobrefn"];
		$jobrefn=mysql_real_escape_string($jobrefn);
	}
	$start=2;
	#echo '<p>start=' .$start; #ok
}
elseif ($_POST['start']==4)
	{$start=4;}


#------------------------------------------
	
#------------------------------------------
if ($start>1) #add securing entered text 21oc15
{
	if ($_POST['jobref']=='')
		{$jobref=$_SESSION['jobref'];}
	$architect=$_POST['architect'];
	$archaddress=$_POST['archaddress'];
	$employer=$_POST['employer'];
	$empaddress=$_POST['empaddress'];
	$contractor=$_POST['contractor'];
	$conaddress=$_POST['conaddress'];
	$theworks=$_POST['theworks'];
	$situated=$_POST['situated'];
	$contractdt=$_POST['contractdt'];
##		$reference=$_POST[10]; #is picked up elsewhere?
	$ccemp=$_POST['ccemp'];
	$cccon=$_POST['cccon'];
	$ccarc=$_POST['ccarc'];
	$ccqs=$_POST['ccqs'];
	$ccse=$_POST['ccse'];
	$ccee=$_POST['ccee'];
	$ccme=$_POST['ccme'];
	$cccow=$_POST['cccow'];
}
#------------------------------------------	

#------------------------------------------
#set up headings
if ($start==1)
	{$title = "$headwords define reference and scope";}
elseif ($start==2)
{
	if ($practype=='ispart')
	{
		$title = $headwords.' enter details (partial completion)';
		$clauseref='16';
	}
	else 
	{
		$practype='isall';
		$title = $headwords.' enter details (all works)';
		$clauseref='15';
	}
}
elseif ($start>2)
	{$title='';}
#set up headings
#=============================================


#=============================================
#first pass to do initial solicit data only if start=1
if ($start==1)
{
	?>
	<head>
	<STYLE>
	<!--
	TH { color: #008000; font-family: "Arial", "Helvetica"; font-size: 9pt; text-align: left }
	TD { color: #008000; font-family: "Arial", "Helvetica"; font-size: 9pt }
	H2 { font-family: "Arial", sans-serif }
	P { font-family: "Arial", "Helvetica"; font-size: 9pt }
	H3 { font-family: "Arial", sans-serif }
	H5 { font-family: "Arial", sans-serif }
	-->
	</STYLE>
	</head>
	<?php
	print "<h3>$title</h3>";
	print '<p>Created 14de05'; #; Updated on 26je06</p>
	echo '<p>'.date("l jS F Y").'</p>';
		
	#first pass form to indicate project reference and extent of works covered (fll or partial)
	$defaults=array('mgdtype'=>'isdone'); #default is unconditional
	print "<form action=\"$thisfile\" method=\"post\">";
	print ("<p>Please enter the project reference and select scope of certificate,\n");
	print ("then select Submit.\n</p>\n");
	print ("<table>\n");
	print "<tr bgcolor=\"cccccc\"><th align=\"left\">Project reference</th><th></th><th></th></tr>";

	print ("<tr>");
	print ("<td>New project: </td>");
	print "<td>";
	input_text('jobrefn',$_POST,$jobrefn); #set limit to entry length
	#need a test here whether 'new' reference exists in table, & feedback if it does
	print ("</td><td>(Limit 9 characters presently)</td></tr>");
	print "<tr><td></td></tr>";
	print "<tr bgcolor=\"cccccc\"><th align=\"left\">Certificate scope</th><th></th><th></th></tr>";
	print ("<tr>");
	print "<td>Unconditional or schedule/value of remaining defects?";
	print "</td>";
	print "<td>";
	input_radiocheck('radio','mgdtype',$defaults,'isdone');
	print " Unconditional (default)<br>";
	input_radiocheck('radio','mgdtype',$defaults,'isfaulty');
	print " Schedule/value of remaining defects";
	print ("</td></tr>");
	print ("</table>");
	print ("<input type=\"submit\" name=\"button\" value=\"Submit\">");
	print ("<input type=\"hidden\" name=\"start\" value=\"2\">"); #sets start level for next pass
	print "</form>";
	
	echo '<hr>';
	print("<p><a href=$homefile><font color=\"#999966\"><b>Home</b></font></a>");
	print("<p><a href=\"http://www.bidp.bw\"><font color=\"#999966\"><b>BIDP web page</b></font></a>");	

	echo $madehere;
	
} #end start=1
#end of first pass to do initial solicit_data
#=============================================


#=============================================
#second pass main data entry for this form
if ($start==2)
{
	?>
	<head><STYLE>
	<!--
	TH { color: #008000; font-family: "Arial", "Helvetica"; font-size: 9pt; text-align: left }
	TD { color: #008000; font-family: "Arial", "Helvetica"; font-size: 9pt }
	H2 { font-family: "Arial", sans-serif }
	P { font-family: "Arial", "Helvetica"; font-size: 9pt }
	H3 { font-family: "Arial", sans-serif }
	H5 { font-family: "Arial", sans-serif }
	-->
	</STYLE></head>
	<?php
	
	print "<h3>$title</h3>";
	echo '<p>'.date("l jS F Y").'</p>';
#	print $jobref.' check'; prints ok
	$reference=$jobref;
#	print $reference.' check'; #prints ok
	$mgdtype=$_POST['mgdtype']; #26je06
	if ($mgdtype=='isdone')
		{$certtype=' unconditional ';}
	else
		{$certtype=' conditional ';}
#	print $mgdtype; #ok
	print "<form action=\"$thisfile\" method=\"post\">";
	print ("<p>Please enter the $certtype certificate details,");
	print "then select Submit."; 
	#(Do not use apostrophes in the text yet, as the form will break)</p>"); #no longer an issue as not using sql 1ap19
	print ("<table>");
	
	print '<tr><td align="right">';
	print "Project reference: </td><td><b>$jobref</b>";
	print '</td></tr><tr><td align="right">';
	print 'Architect/Issuer firm: </td><td>';
	print "<INPUT type=\"text\" name=\"architect\" value=\"$architect\" maxlength=\"99\"></td></td><td><td align=\"right\">";
	print "Employer copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"ccemp\" value=\"$ccemp\">";
	print "</td></tr>";
#	input_text('architect', $architect, $architect);
	print '<tr><td align="right">';
	print "Architect address: </td><td>";
	print "<INPUT type=\"text\" name=\"archaddress\" value=\"$archaddress\" maxlength=\"99\"><td align=\"right\">ie Box, Town</td><td align=\"right\">";
	print "Contractor copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"cccon\" value=\"$cccon\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Employer: </td><td>";
	print "<INPUT type=\"text\" name=\"employer\" value=\"$employer\" maxlength=\"99\"><td align=\"right\"></td><td align=\"right\">";
	print "Architect copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"ccarc\" value=\"$ccarc\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Employer address: </td><td>";
	print "<INPUT type=\"text\" name=\"empaddress\" value=\"$empaddress\" maxlength=\"99\"><td align=\"right\"></td><td align=\"right\">";
	print "QS copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"ccqs\" value=\"$ccqs\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Contractor: </td><td>";
	print "<INPUT type=\"text\" name=\"contractor\" value=\"$contractor\" maxlength=\"99\"><td align=\"right\"></td><td align=\"right\">";
	print "SE copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"ccse\" value=\"$ccse\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Contractor address: </td><td>";
	print "<INPUT type=\"text\" name=\"conaddress\" value=\"$conaddress\" maxlength=\"99\"><td align=\"right\"></td><td align=\"right\">";
	print "EE copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"ccee\" value=\"$ccee\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "the Works: </td><td>";
	print "<INPUT type=\"text\" name=\"theworks\" value=\"$theworks\" maxlength=\"99\"><td align=\"right\"></td><td align=\"right\">";
	print "ME copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"ccme\" value=\"$ccme\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Situated: </td><td>";
	print "<INPUT type=\"text\" name=\"situated\" value=\"$situated\" maxlength=\"99\"><td align=\"right\"></td><td align=\"right\">";
	print "CoW copies:";
	print "</td><td>";
	print "<INPUT type=\"text\" name=\"cccow\" value=\"$cccow\">";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Contract dated: </td><td>";
	print "<INPUT type=\"text\" name=\"contractdt\" value=\"$contractdt\" size=\"12\" maxlength=\"10\"></td><td>yyyy-mm-dd</td><td>";
	print '</td></tr>';
	print '<tr><td align="right">';
	print "Certificate date: </td><td>";
	print "<INPUT type=\"text\" name=\"certdate\" value=\"$today\" size=\"12\" maxlength=\"10\">";
	print '</td><td>yyyy-mm-dd</td></tr>';
	if ($mgdtype=='isfaulty')
	{
		print '<tr><td align="right">';
		print "Schedule of defects dated: </td>yyyy-mm-dd<td>";
		print "<INPUT type=\"text\" name=\"scheddate\" size=\"12\" maxlength=\"10\">";
		print '</td><td></td></tr>';
		print '<tr><td align="right">';
		print "In the sum of P: </td><td>";
		print "<INPUT type=\"textarea\" name=\"schedsum\">";
		print '</td></tr>';
		print '<tr><td>';
		print ("<input type=\"hidden\" name=\"mgdtype\" value=\"isfaulty\">\n");
		print '</td></tr>';
	}
	else
		{echo '<input type="hidden" name="mgdtype" value="isdone">';}
	$defaults2=array('output'=>'ispdf');
#	print '<tr><td align="right">';
#	print 'Select output format: ';
#	print '</td><td>';
#	input_radiocheck('radio','output',$defaults2,'ispdf');
#	print " PDF (default)<br>";
#	input_radiocheck('radio','output',$defaults2,'ishtm');
#	print " HTML ";
#	print ("</td></tr>");
#	print $jobref.', '.$practype;
	print ("</table>\n");
	print ("<input type=\"submit\" name=\"button\" value=\"Submit\">\n");
#	print $jobrefn.' >>>';
	#if ($jobref>'')
	#{
	#	print ("<input type=\"hidden\" name=\"start\" value=\"3\">\n"); #sets start level for next pass (save)	
	#}
	#else
	#{
	print ("<input type=\"hidden\" name=\"start\" value=\"4\">\n");
#		html_end (); #sets start level for next pass (pdf)
	#}
	print ("<input type=\"hidden\" name=\"jobref\" value=\"$jobref\">\n"); #carry jobref forward
#	print $jobref.' here!! '.$start.' '.$jobrefn;
	print "</form>\n";
	
	echo '<hr>';
	print("<p><a href=$homefile><font color=\"#999966\"><b>Home</b></font></a>");	
	echo $madehere;
	
}
#end part 2 main data entry for this form
#=============================================


#=============================================
#produce pdf
if ($start==4)
{
#	print $jobref.' here!'.$_POST['contractdt'].$_POST['certdate'];
#	print $start;
#	print $certdate.' pqr';
#	echo '<p>Displaying PDF next: please print from the PDF display';
#	print '<p>'.$start.' here!';
#	print '<p>'.$start.' again';
	include 'pdml.php';
	
	?>
	<pdml>
	<body>
	<font face=arial size=10>
	<cell left=0 width=100% height=1500%></cell><br height=1500%>
	<img left=163mm top=35mm width=21mm height=26.3mm src=bidplog5.jpg>
	<cell width=42mm align=right>Issued by:</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php 
	$architect=$_POST['architect'];
	print $architect; ?></cell>
	<cell width=5mm> </cell>
	<cell width=22mm align=left>Certificate of</cell><br height=150%>
	<cell width=42mm align=right>address:</cell>
	<cell width=5mm > </cell>
	<cell width=100mm align=left><?php 
	$archaddress=$_POST['archaddress'];
	print $archaddress; ?></cell>
	<cell width=5mm> </cell>
	<font face=arial size=14>
	<cell width=22mm align=left><b>Making Good</b></cell><br height=150%>
	<font face=arial size=10>
	<cell width=152mm> </cell>
	<font face=arial size=14>
	<cell width=22mm><b>Defects</b></cell><br height=150%>
	<font face=arial size=10>
	<cell width=42mm align=right>Employer</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php 
	$employer=$_POST['employer'];
	print $employer; ?></cell>
	<cell width=5mm> </cell>
	<cell width=22mm></cell><br height=150%>
	<cell width=42mm align=right>address</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php 
	$empaddress=$_POST['empaddress'];
	print $empaddress; ?></cell>
	<cell width=27mm align=left> </cell><br height=150%>
	<cell width=174mm> </cell><br height=150%>
	<cell width=42mm align=right>Contractor</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php 
	$contractor=$_POST['contractor'];
	print $contractor; ?></cell>
	<cell width=27mm align=left></cell><br height=150%>
	<cell width=42mm align=right>address</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php 
	$conaddress=$_POST['conaddress'];
	print $conaddress; ?></cell>
	<cell width=27mm align=left></cell><br height=150%>
	<cell width=174mm> </cell><br height=150%>
	<cell width=42mm align=right>the Works</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php
	$theworks=$_POST['theworks'];
 	print $theworks; ?></cell>
	<cell width=27mm align=left></cell><br height=150%>
	<cell width=42mm align=right>situated at</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php 
	$situated=$_POST['situated'];
	print $situated; ?></cell>
	<cell width=27mm align=left></cell><br height=150%>
	<cell width=174mm> </cell><br height=150%>
	<cell width=42mm align=right>Contract dated</cell>
	<cell width=5mm> </cell>
	<cell width=100mm align=left><?php
#	$tdate='1970-01-01';
	$contractdt=$_POST['contractdt'];
	$sdate=mktime(0,0,0,substr($contractdt,5,2),substr($contractdt,8,2),substr($contractdt,
0,4));
#	print $contractdt.' here!';
#	print substr($contractdt,0,4);
	print date('jS F, Y',$sdate); ?></cell>
	<cell width=5mm> </cell>
	<cell width=22mm align=left></cell><br height=150%>
	
	<div left=57mm width=100mm>In accordance with clause <?php
	$mgdtype=$_POST[mgdtype];
	if ($mgdtype=='isfaulty')
	{
	 	print '15'; #clause ref
	}
	else
	{
	 	print '15(4)'; #clause reference
	}
	?> of the BIDP standard form of building contract, I/we certify that all outstanding items, and all defects, shrinkages and other faults which appeared during the defects liability period, were completed and made good to our satisfaction on <?php
	$certdate=$_POST['certdate'];
	$cdate=mktime(0,0,0,substr($certdate,5,2),substr($certdate,8,2),substr($certdate,0,4));
	print '<b>'.date("jS F, Y, ",$cdate).'</b>';
	if ($mgdtype=='isfaulty')
	{
		$scheddate=$_POST[scheddate];
		$schedsum=$_POST[schedsum];
		$sdate=mktime(0,0,0,substr($scheddate,5,2),substr($scheddate,8,2),substr($scheddate,0,4));
		echo 'subject to the schedule of defects agreed between employer and contractor dated '.date("jS F, Y",$sdate).' valuing the defects still outstanding at final inspection in the sum of P'.$schedsum.', ';
	} 
	?>and I/we declare that the residue of the retention monies deducted under previous certificates is to be released.</div><br /><br /><br />
	<cell width=174mm top=155mm> </cell><br height=150%>
	<font face=arial size=14>
	<div left=57mm width=100mm></div></font><br height=190%>
	<font face=arial size=10>
	<?php

		print '<cell width=47mm align=right></cell>';
		print '<cell width=100mm align=right></cell>';
		print '<cell width=27mm></cell><br height=150%>';
		print '<cell width=47mm></cell>';
		print '<cell width=100mm align=right></cell>';
		print '<cell width=5mm> </cell>';
		print '<cell width=22mm align=left></cell><br height=150%>';
		print '<cell width=47mm></cell>';
		print '<cell width=100mm align=right></cell>';
		print '<cell width=5mm align=left></cell>';
		print '<cell width=22mm align=left></cell><br height=150%>';
		
	?>
	<cell width=174mm height=450%> </cell><br height=450%>
	<font face=arial size=7>
	<cell width=42mm align=right>To be signed by or for</cell>
	<font face=arial size=9>
	<cell width=5mm> </cell>
	<cell width=100mm align=left height=150%>Signed: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . </cell>
	<cell width=27mm> </cell><br height=100%>
	<font face=arial size=7>
	<cell width=42mm align=right>the issuer named above</cell>
	<cell width=147></cell><br height=100%>
	<font face=arial size=9>
	<cell width=47mm></cell>
	<cell width=100mm align=left><?php
#	print "on ".date('jS F, Y',$cdate); ?></cell>
	<cell width=27mm align=left></cell><br height=150%>
	<font face=arial size=7>
	<cell width=47mm></cell>
	<cell width=100mm top=-55mm align=left>BIDP working demo; Licence expires 30th November, 2026</cell>
	<cell width=27mm> </cell><br height=100%>
	<cell width=47mm></cell>
	<cell width=100mm top=-50mm align=left>(c) copyright Botswana Institute of Development Professions, 2005-2019</cell>
	<cell width=27mm> </cell><br height=100%>
	<cell width=42mm align=right top=-44mm>Distribution</cell>
	<cell width=5mm> </cell>
	<font face=arial size=9>
	<cell width=5mm border=1 align=left height=115%><?php  
	$ccemp=$_POST['ccemp'];
	if ($ccemp>0) {print $ccemp;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$cccon=$_POST['cccon'];
	if ($cccon>0) {print $cccon;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$ccarc=$_POST['ccarc']; #fix26je06
	if ($ccarc>0) {print $ccarc;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$ccqs=$_POST['ccqs'];
	if ($ccqs>0) {print $ccqs;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$ccse=$_POST['ccse'];
	if ($ccse>0) {print $ccse;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$ccee=$_POST['ccee'];
	if ($ccee>0) {print $ccee;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$ccme=$_POST['ccme'];
	if ($ccme>0) {print $ccme;} ?></cell>
	<cell width=3mm height=150%></cell>
	<cell width=5mm border=1 align=left height=115%><?php  
	$cccow=$_POST['cccow'];
	if ($cccow>0) {print $cccow;} ?></cell>
	<font face=arial size=7>
	<?php #insert distribution list ?> 
	
	<rotate angle="90" center="0mm,0mm">
	<div top=56mm left=-268.5mm width=25mm height=5mm align=right>
	<font face=arial size=7>Employer</div>
	<div top=64mm left=-270mm width=25mm height=5mm align=right>Contractor</div>
	<div top=72mm left=-268mm width=25mm height=5mm>Architect</div>
	<div top=80mm left=-277.5mm width=25mm height=5mm >
	Quantity surveyor</div>
	<div top=88mm left=-279mm width=25mm height=5mm >Structural engineer</div>
	<div top=96mm left=-278.5mm width=25mm height=5mm align=right>Electrical engineer</div>
	<div top=104mm left=-281mm width=25mm height=5mm align=right>Mechanical engineer</div>
	<div top=112mm left=-274mm width=25mm height=5mm align=right>Clerk of works</font></div>
	
	</body>
	</pdml>
	<?php
}
#function print pdf to screen
#=============================================


?>