<?php

/* $Revision: 1.26 $ */

$PageSecurity = 2;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');

if(!isset($_GET['OrderNo']) && !isset($_POST['OrderNo'])){
        $title = _('Select a Purchase Order');
        include('includes/header.inc');
        echo '<div class="centre"><br><br><br>';
        prnMsg( _('Select a Purchase Order Number to Print before calling this page') , 'error');
        echo '<br><br><br><table class="table_index">
		<tr><td class="menu_group_item">
                <li><a href="'. $rootpath . '/PO_SelectOSPurchOrder.php?'.SID .'">' . _('Outstanding Purchase Orders') . '</a></li>
                <li><a href="'. $rootpath . '/PO_SelectPurchOrder.php?'. SID .'">' . _('Purchase Order Inquiry') . '</a></li>
                </td></tr></table></div><br><br><br>';
        include('includes/footer.inc');
        exit();

	echo '<div class="centre"><br><br><br>' . _('This page must be called with a purchase order number to print');
	echo '<br><a href="'. $rootpath . '/index.php?' . SID . '">' . _('Back to the menu') . '</a></div>';
	exit;
}

if (isset($_GET['OrderNo'])){
	$OrderNo = $_GET['OrderNo'];
} elseif (isset($_POST['OrderNo'])){
	$OrderNo = $_POST['OrderNo'];
}

$title = _('Print Purchase Order Number').' '. $OrderNo;

$sql='SELECT status
		FROM purchorders
		WHERE orderno='.$OrderNo;
$result=DB_query($sql, $db);
$myrow=DB_fetch_array($result);
$OrderStatus=$myrow['status'];

if ($OrderStatus!=_('Authorised') and $OrderStatus!=_('Printed')) {
	include('includes/header.inc');
	prnMsg( _('Purchase orders can only be printed once they have been authorised').'. '.
		_('This order is currently at a status of').' '.$OrderStatus,'warn');
	include('includes/footer.inc');
	exit;
}

$ViewingOnly = 0;
if (isset($_GET['ViewingOnly']) && $_GET['ViewingOnly']!='') {
	$ViewingOnly = $_GET['ViewingOnly'];
} elseif (isset($_POST['ViewingOnly']) && $_POST['ViewingOnly']!='') {
	$ViewingOnly = $_POST['ViewingOnly'];
}


if (isset($_POST['DoIt'])  AND ($_POST['PrintOrEmail']=='Print' || $ViewingOnly==1) ){
	$MakePDFThenDisplayIt = True;
} elseif (isset($_POST['DoIt']) AND $_POST['PrintOrEmail']=='Email' AND strlen($_POST['EmailTo'])>6){
	$MakePDFThenEmailIt = True;
}

if (isset($OrderNo) && $OrderNo != "" && $OrderNo > 0){
	//Check this up front. Note that the myrow recordset is carried into the actual make pdf section
	/*retrieve the order details from the database to print */
	$ErrMsg = _('There was a problem retrieving the purchase order header details for Order Number'). ' ' . $OrderNo .
			' ' . _('from the database');
	$sql = "SELECT
			purchorders.supplierno,
			suppliers.suppname,
			suppliers.address1,
			suppliers.address2,
			suppliers.address3,
			suppliers.address4,
			purchorders.comments,
			purchorders.orddate,
			purchorders.rate,
			purchorders.dateprinted,
			purchorders.deladd1,
			purchorders.deladd2,
			purchorders.deladd3,
			purchorders.deladd4,
			purchorders.deladd5,
			purchorders.deladd6,
			purchorders.deliverydate,
			purchorders.allowprint,
			purchorders.requisitionno,
			purchorders.initiator,
			purchorders.payment_terms,
			suppliers.currcode
		FROM purchorders INNER JOIN suppliers
			ON purchorders.supplierno = suppliers.supplierid
		WHERE purchorders.orderno = " . $OrderNo;
	$result=DB_query($sql,$db, $ErrMsg);

	if (DB_num_rows($result)==0){ /*There is ony one order header returned */

		$title = _('Print Purchase Order Error');
		include('includes/header.inc');
		echo '<div class="centre"><br><br><br>';
		prnMsg( _('Unable to Locate Purchase Order Number') . ' : ' . $OrderNo . ' ', 'error');
		echo '<br><br><br><table class="table_index">
			<tr><td class="menu_group_item">
	                <li><a href="'. $rootpath . '/PO_SelectOSPurchOrder.php?'.SID .'">' . _('Outstanding Purchase Orders') . '</a></li>
        	        <li><a href="'. $rootpath . '/PO_SelectPurchOrder.php?'. SID .'">' . _('Purchase Order Inquiry') . '</a></li>
                	</td></tr></table></div><br><br><br>';
		include('includes/footer.inc');
		exit();

	} elseif (DB_num_rows($result)==1){ /*There is only one order header returned */

	   $POHeader = DB_fetch_array($result);
	   if ($ViewingOnly==0) {
		   if ($POHeader['allowprint']==0){
			  $title = _('Purchase Order Already Printed');
			  include('includes/header.inc');
			  echo '<p>';
			  prnMsg( _('Purchase order number').' ' . $OrderNo . ' '.
				_('has previously been printed') . '. ' . _('It was printed on'). ' ' .
				ConvertSQLDate($POHeader['dateprinted']) . '<br>'.
				_('To re-print the order it must be modified to allow a reprint'). '<br>'.
				_('This check is there to ensure that duplicate purchase orders are not sent to the supplier	resulting in several deliveries of the same supplies'), 'warn');
           echo '<br><table class="table_index">
                <tr><td class="menu_group_item">
 					 <LI><a href="' . $rootpath . '/PO_PDFPurchOrder.php?' . SID . 'OrderNo=' . $OrderNo . '&ViewingOnly=1">'.
				_('Print This Order as a Copy'). '</a>
 				<LI><a href="' . $rootpath . '/PO_Header.php?' . SID . 'ModifyOrderNumber=' . $OrderNo . '">'.
				_('Modify the order to allow a real reprint'). '</a>' .
			  	'<LI><a href="'. $rootpath .'/PO_SelectPurchOrder.php?' . SID . '">'.
				_('Select another order'). '</a>'.
			  	'<LI><a href="' . $rootpath . '/index.php?' . SID . '">'. _('Back to the menu').'</a>';
			  echo '</BODY></HTML>';
			  include('includes/footer.inc');
			  exit;
		   }//AllowedToPrint
	   }//not ViewingOnly
	}// 1 valid record
}//if there is a valid order number

if (isset($MakePDFThenDisplayIt) or isset($MakePDFThenEmailIt)) {
	
	$PaperSize = 'A4';

	include('includes/PDFStarter.php');

	$pdf->addinfo('Title', _('Purchase Order') );
	$pdf->addinfo('Subject', _('Purchase Order Number').' ' . $OrderNo);

	$line_height=16;
	   /* Then there's an order to print and its not been printed already (or its been flagged for reprinting)
	   Now ... Has it got any line items */

	   $PageNumber = 1;
	   $ErrMsg = _('There was a problem retrieving the line details for order number') . ' ' . $OrderNo . ' ' .
			_('from the database');
	   $sql = "SELECT itemcode,
	   			deliverydate,
				qtyinvoiced,
				itemdescription,
				unitprice,
				unitname as units,
				quantityord,
				decimalplaces
			FROM purchorderdetails LEFT JOIN stockmaster
				ON purchorderdetails.itemcode=stockmaster.stockid
			LEFT JOIN unitsofmeasure
				ON purchorderdetails.uom=unitsofmeasure.unitid
			WHERE orderno =" . $OrderNo;
	   $result=DB_query($sql,$db);

	   if (DB_num_rows($result)>0){
	   /*Yes there are line items to start the ball rolling with a page header */

		include('includes/PO_PDFOrderPageHeader.inc');

		$YPos-=$line_height;

		$OrderTotal = 0;
		$SlNo=1;

		while ($POLine=DB_fetch_array($result)){

			$sql = "SELECT supplierdescription 
				FROM purchdata 
				WHERE stockid='" . $POLine['itemcode'] . "' 
				AND supplierno ='" . $POHeader['supplierno'] . "'";
			$SuppDescRslt = DB_query($sql,$db);
	
			$ItemDescription='';

			if (DB_error_no($db)==0){
				if (DB_num_rows($SuppDescRslt)==1){
					$SuppDescRow = DB_fetch_row($SuppDescRslt);
					if (strlen($SuppDescRow[0])>2){
						$ItemDescription = $SuppDescRow[0];
					}
				}
			}
			if (strlen($ItemDescription)<2){
				$ItemDescription = $POLine['itemdescription'];
			}

			$DisplayQty = number_format($POLine['quantityord'],$POLine['decimalplaces']);
			if ($_POST['ShowAmounts']=='Yes'){
				$DisplayPrice = number_format($POLine['unitprice'],2);
			} else {
				$DisplayPrice = "----";
			}
			$DisplayDelDate = ConvertSQLDate($POLine['deliverydate'],2);
			if ($_POST['ShowAmounts']=='Yes'){
				$DisplayLineTotal = number_format($POLine['unitprice']*$POLine['quantityord'],2);
			} else {
				$DisplayLineTotal = "----";
			}

			$OrderTotal += ($POLine['unitprice']*$POLine['quantityord']);
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1,$YPos+84,40,$FontSize-2,$SlNo, 'left');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40,$YPos+84,94,$FontSize-2,$POLine['itemcode'], 'left');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+165,$YPos+84,55,$FontSize-2,$DisplayQty, 'right');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+55+150+40,$YPos+84,37,$FontSize-2,$POLine['units'], 'left');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+55+150+40+45,$YPos+84,60,$FontSize-2,$DisplayDelDate, 'left');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+55+150+40+45+22,$YPos+84,80,$FontSize-2,$DisplayPrice, 'right');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+55+150+40+45+22+60,$YPos+84,85,$FontSize-2,$DisplayLineTotal, 'right');
			$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+55,$YPos+84,270,$FontSize-2,$ItemDescription, 'left');
			$SlNo++;
			if (strlen($LeftOvers)>1){
		
				$LeftOvers = $pdf->addTextWrap($Left_Margin+1+94,$YPos-$line_height,270,$FontSize,$LeftOvers, 'left');
				$YPos-=$line_height;
			}

			if ($YPos-$line_height <= $Bottom_Margin){
		        /* We reached the end of the page so finsih off the page and start a newy */
				$PageNumber++;
				include ('includes/PO_PDFOrderPageHeader.inc');
			} //end if need a new page headed up

			/*increment a line down for the next line item */
			$YPos -= $line_height;

		} //end while there are line items to print out

		if ($YPos-$line_height <= $Bottom_Margin){ // need to ensure space for totals
		        $PageNumber++;
			include ('includes/PO_PDFOrderPageHeader.inc');
		} //end if need a new page headed up


		if ($_POST['ShowAmounts']=='Yes'){
			$DisplayOrderTotal = number_format($OrderTotal,2);
		} else {
			$DisplayOrderTotal = "----";
		}
		//$pdf->line($Left_Margin, $YPos, $Page_Width-$Right_Margin, $YPos);
		//$YPos = $Bottom_Margin + $line_height;
		$pdf->addText(300,$YPos+65, 10, _('Order Total - excl tax'). ' ' . $POHeader['currcode']);
		$LeftOvers = $pdf->addTextWrap($Left_Margin+1+40+55+150+40+45+22+50,$YPos+65,95,10,$DisplayOrderTotal, 'right');
		

		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-70,130,9,'1.Product Specification', 'left');
		$pdf->line($Left_Margin, $YPos-50,  $Page_Width-$Right_Margin, $YPos-50);
		$pdf->line($Left_Margin+150, $YPos-90, $Left_Margin+150, $YPos-50); 

		$LeftOvers = $pdf->addText($Left_Margin+15,$YPos+285, $FontSize+2, 'To:');
		$LeftOvers = $pdf->addText($Left_Margin+15,$YPos+270, $FontSize-2, $POHeader['suppname']);
		$LeftOvers = $pdf->addText($Left_Margin+15,$YPos+265, $FontSize-2, $POHeader['address1']);
		$LeftOvers = $pdf->addText($Left_Margin+15,$YPos+260, $FontSize-2, $POHeader['address2']);
		$LeftOvers = $pdf->addText($Left_Margin+15,$YPos+255, $FontSize-2, $POHeader['address3']);
		$LeftOvers = $pdf->addText($Left_Margin+15,$YPos+250, $FontSize-2, $POHeader['address4']);

		//fine code
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-110,300,9,'2.Inspection/Test Reports', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+10,$YPos-90,500,8,'', 'right');
		$pdf->line($Left_Margin, $YPos-90,  $Page_Width-$Right_Margin, $YPos-90);
		$pdf->line($Left_Margin+150, $YPos-130, $Left_Margin+150, $YPos-90);

        $LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-155,500,9,'3.Prices & terms of Payments', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-155,500,8,$POHeader['payment_terms'], 'left');
		$pdf->line($Left_Margin+150, $YPos-180, $Left_Margin+150, $YPos-130);
		$pdf->line($Left_Margin, $YPos-130,  $Page_Width-$Right_Margin, $YPos-130);
		
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-200,500,9,'4.Shipment Details & Penalty', 'left');
		$pdf->line($Left_Margin+150, $YPos-200, $Left_Margin+150, $YPos-170);

		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-240,500,9,'5.Delivery Date', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-240,500,8, ConvertSQLDate($POHeader['deliverydate']), 'left');
		
		//$pdf->line($Left_Margin+150, $YPos-145, $Left_Margin+150, $YPos-165);
	
		$pdf->line($Left_Margin, $YPos-170, $Page_Width-$Right_Margin, $YPos-170);
		$pdf->line($Left_Margin, $YPos-255, $Page_Width-$Right_Margin, $YPos-255);
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-270,500,9,'6.Delivery At', 'left');//Delivery to
        $LeftOvers = $pdf->addTextWrap($Left_Margin+10,$YPos-190,500,8,'', 'right');
		$pdf->line($Left_Margin+150, $YPos-270, $Left_Margin+150, $YPos-210);
		
		$LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-270,500,8,$POHeader['deladd1'],',', 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+300,$YPos-270,500,8,$POHeader['deladd2'], 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+350,$YPos-270,500,8,$POHeader['deladd3'], 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-280,500,8, $POHeader['deladd4'], 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+300,$YPos-280,500,8, $POHeader['deladd5'], 'left');
	
		$pdf->line($Left_Margin, $YPos-215, $Page_Width-$Right_Margin, $YPos-215);
		$LeftOvers = $pdf->line($Left_Margin, $YPos-295, $Page_Width-$Right_Margin, $YPos-295);
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-310,500,9,'7.Packing Standard & Method', 'left');
		$pdf->line($Left_Margin+150, $YPos-270, $Left_Margin+150, $YPos-330);
		$pdf->line($Left_Margin+150, $YPos-195, $Left_Margin+150, $YPos-215);

		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-345,500,9,'8.Documents Required', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-355,500,7,'1. Signed & Stamped Invoice ', 'left');
		 $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-375,500,7,'2.All mould drawing models, mold flow analysis report ', 'left');
		 
		 $LeftOvers = $pdf->line($Left_Margin, $YPos-330, $Page_Width-$Right_Margin, $YPos-330);
		
         $LeftOvers = $pdf->addTextWrap($Left_Margin+420,$YPos-340,500,8,'time ', 'left');
		 $LeftOvers = $pdf->addTextWrap($Left_Margin+460,$YPos-340,500,8,'through ', 'left');
		 $LeftOvers = $pdf->line($Left_Margin+150, $YPos-345, $Page_Width-$Right_Margin, $YPos-345);
		 $LeftOvers = $pdf->line($Left_Margin+150, $YPos-365, $Page_Width-$Right_Margin, $YPos-365);

		 
		 $pdf->line($Left_Margin+150, $YPos-345, $Left_Margin+150, $YPos-400);
		 $pdf->line($Left_Margin+415, $YPos-330, $Left_Margin+415, $YPos-390);
		 $pdf->line($Left_Margin+458, $YPos-330, $Left_Margin+458, $YPos-390);
		 $pdf->line($Left_Margin, $YPos-450, $Page_Width-$Right_Margin, $YPos-450);

		$pdf->line($Left_Margin+150, $YPos-390, $Left_Margin+150, $YPos-450);
		$pdf->line($Left_Margin+330, $YPos-390, $Left_Margin+330, $YPos-450);
		
		 $pdf->line($Left_Margin, $YPos-390, $Page_Width-$Right_Margin, $YPos-390);
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-405,200,9,'9.Other Instruction/Terms', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-400,500,7,'CENTRAL EXERCISE REG. NUMBER ', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-410,200,6,'E.C.C NO. ', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-420,200,6,'CST NO. ', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+155,$YPos-430,200,6,'UPST NO ', 'left');
		
	    $LeftOvers = $pdf->addTextWrap($Left_Margin+340,$YPos-400,500,7,'AAA CD 461 KXM005', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+340,$YPos-410,200,6,'AAA CD 461 KXM005', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+340,$YPos-420,200,6,'ND 5058346 DT:-21-09-93', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+340,$YPos-430,200,6,'ND 0058078 DT:-21-09-93', 'left');
        $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos-420,200,8,'TIN NO. 09465900282', 'left');

		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-465,300,9,'Please confirm the other instruction of the above order', 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-475,100,9,'Best Regards', 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-510,100,9,'Approved By', 'left');//stats_comment
		$LeftOvers = $pdf->addTextWrap($Left_Margin+301,$YPos-465,300,9,'We here by accept the above order along with all the', 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+301,$YPos-475,100,9,'conditioned mentioned in', 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+301,$YPos-510,100,9,'Date:', 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+380,$YPos-510,100,9,'Signature and Rubber Stamp', 'left');

	   	 $pdf->line($Left_Margin+275, $YPos-515, $Left_Margin+275, $YPos-450);
	} /*end if there are order details to show on the order*/
    //} /* end of check to see that there was an order selected to print */

    //failed var to allow us to print if the email fails.
    $failed = false;
    if ($MakePDFThenDisplayIt){

    	$buf = $pdf->output();
    	$len = strlen($buf);
    	header('Content-type: application/pdf');
    	header('Content-Length: ' . $len);
    	header('Content-Disposition: inline; filename=PurchaseOrder.pdf');
    	header('Expires: 0');
    	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    	header('Pragma: public');

    	$pdf->stream();

    } else { /* must be MakingPDF to email it */

    	$pdfcode = $pdf->output();
	$fp = fopen( $_SESSION['reports_dir'] . '/PurchOrder.pdf','wb');
	fwrite ($fp, $pdfcode);
	fclose ($fp);

	include('includes/htmlMimeMail.php');

	$mail = new htmlMimeMail();
	$attachment = $mail->getFile($_SESSION['reports_dir'] . '/PurchOrder.pdf');
	$mail->setText( _('Please find herewith our purchase order number').' ' . $OrderNo);
	$mail->setSubject( _('Purchase Order Number').' ' . $OrderNo);
	$mail->addAttachment($attachment, 'PurchOrder.pdf', 'application/pdf');
	$mail->setFrom($_SESSION['CompanyRecord']['coyname'] . "<" . $_SESSION['CompanyRecord']['email'] .">");
	$result = $mail->send(array($_POST['EmailTo']));
	if ($result==1){
		$failed = false;
		echo '<p>';
		prnMsg( _('Purchase order'). ' ' . $OrderNo.' ' . _('has been emailed to') .' ' . $_POST['EmailTo'] . ' ' . _('as directed'), 'success');
	} else {
		$failed = true;
		echo '<p>';
		prnMsg( _('Emailing Purchase order'). ' ' . $OrderNo.' ' . _('to') .' ' . $_POST['EmailTo'] . ' ' . _('failed'), 'error');
	}

    }

	if ($ViewingOnly==0 && !$failed) {
		$commentsql='SELECT initiator,stat_comment FROM purchorders WHERE orderno='.$OrderNo;
		$commentresult=DB_query($commentsql,$db);
		$commentrow=DB_fetch_array($commentresult);
		$comment=$commentrow['stat_comment'];
		$emailsql='SELECT email FROM www_users WHERE userid="'.$commentrow['initiator'].'"';
		$emailresult=DB_query($emailsql, $db);
		$emailrow=DB_fetch_array($emailresult);
		$date = date($_SESSION['DefaultDateFormat']);
		$StatusComment=$date.' - Printed by <a href="mailto:'.$emailrow['email'].'">'.$_SESSION['UserID'].
			'</a><br>'.$comment;
		$sql = "UPDATE purchorders 
			SET allowprint=0, 
				dateprinted='" . Date('Y-m-d') . "',
				status='"._('Printed')."',
				stat_comment='".$StatusComment."' 
			WHERE purchorders.orderno=" .$OrderNo;
		$result = DB_query($sql,$db);
	}

} /* There was enough info to either print or email the purchase order */
 else { /*the user has just gone into the page need to ask the question whether to print the order or email it to the supplier */

	include ('includes/header.inc');
	echo '<form action="' . $_SERVER['PHP_SELF'] . '?' . SID . '" method=post>';

	if ($ViewingOnly==1){
		echo '<input type=hidden name="ViewingOnly" VALUE=1>';
	}
	echo '<br><br>';
	echo '<input type=hidden name="OrderNo" VALUE="'. $OrderNo. '">';
	echo '<table><tr><td>'. _('Print or Email the Order'). '</td><td>
		<select name="PrintOrEmail">';

	if (!isset($_POST['PrintOrEmail'])){
		$_POST['PrintOrEmail'] = 'Print';
	}

	if ($_POST['PrintOrEmail']=='Print'){
		echo '<option selected VALUE="Print">'. _('Print');
		echo '<option VALUE="Email">' . _('Email');
	} else {
		echo '<option VALUE="Print">'. _('Print');
		echo '<option selected VALUE="Email">'. _('Email');
	}
	echo '</select></td></tr>';

	echo '<tr><td>'. _('Show Amounts on the Order'). '</td><td>
		<select name="ShowAmounts">';
		
	if (!isset($_POST['ShowAmounts'])){
		$_POST['ShowAmounts'] = 'Yes';
	}

	if ($_POST['ShowAmounts']=='Yes'){
		echo '<option selected VALUE="Yes">'. _('Yes');
		echo '<option VALUE="No">' . _('No');
	} else {
		echo '<option VALUE="Yes">'. _('Yes');
		echo '<option selected VALUE="No">'. _('No');
	}
	
	echo '</select></td></tr>';
	if ($_POST['PrintOrEmail']=='Email'){
		$ErrMsg = _('There was a problem retrieving the contact details for the supplier');
		$SQL = "SELECT suppliercontacts.contact,
				suppliercontacts.email
			FROM suppliercontacts INNER JOIN purchorders
			ON suppliercontacts.supplierid=purchorders.supplierno
			WHERE purchorders.orderno=$OrderNo";
		$ContactsResult=DB_query($SQL,$db, $ErrMsg);

		if (DB_num_rows($ContactsResult)>0){
			echo '<tr><td>'. _('Email to') .':</td><td><select name="EmailTo">';
			while ($ContactDetails = DB_fetch_array($ContactsResult)){
				if (strlen($ContactDetails['email'])>2 AND strpos($ContactDetails['email'],'@')>0){
					if ($_POST['EmailTo']==$ContactDetails['email']){
						echo '<option selected VALUE="' . $ContactDetails['email'] . '">' . $ContactDetails['Contact'] . ' - ' . $ContactDetails['email'];
					} else {
						echo '<option VALUE="' . $ContactDetails['email'] . '">' . $ContactDetails['contact'] . ' - ' . $ContactDetails['email'];
					}
				}
			}
			echo '</select></td></tr></table>';
		} else {
			echo '</table><br>';
			prnMsg ( _('There are no contacts defined for the supplier of this order') . '. ' .
				_('You must first set up supplier contacts before emailing an order'), 'error');
			echo '<br>';
		}
	} else {
		echo '</table>';
	}
	echo '<br><div class="centre"><input type=submit name="DoIt" VALUE="' . _('OK') . '"></div>';
	echo '</form>';
	include('includes/footer.inc');
}
?>
