<?php
ob_start();
include('includes/session.inc');
$Title = _('Re-Print Quote');
include('includes/SQL_CommonFunctions.inc');


	

	
	
if (isset($_GET['orderno'])) {
		$_SESSION['orderno'] = stripslashes($_GET['orderno']);
	}
	$orderno= $_SESSION['orderno'];


//first check whether this costing is approved or not


$SQL = "SELECT *
	               
			FROM salesorders
			WHERE orderno='" . $orderno. "'
			";
	
			$result = DB_query($SQL);
	
			$myrow = DB_fetch_array($result) ;

	$quotedated = $myrow['quotedate'];
		$quotedate = date('d-m-Y',$quotedated);
$yeardate = date('Y',$quotedated);
$monthdate =date('m',$quotedated);

		$sql4 =  "SELECT projectid,
	               project_type,
				   project_name,
				   retention_rate,
				   department,
				   customer,
				   branch,
				   account_number,
				   contact_number,
				   required_date,
				   start_date,
				   sales_gl,
				   cogs_gl,
				   comments
			FROM projectdetails
			
			WHERE projectid = '" . $myrow['job_no']. "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result4 = DB_query($sql4,$ErrMsg);
	
		
	$myrow4 = DB_fetch_array($result4) ;
$SQL20 = "SELECT pcid,notes
								
				   
			FROM projectcosting
			WHERE projectid='" . $myrow['job_no']. "'
			";
	
	$result20 = DB_query($SQL20);
	
	$myrow20 = DB_fetch_array($result20) ;
//look for department id
	$sql40 =  "SELECT departmentid
			FROM department
			
			WHERE departmentname = '" . $myrow4['department']. "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result40 = DB_query($sql40,$ErrMsg);
	
		
	$myrow40 = DB_fetch_array($result40) ;

		$userid = $_SESSION['UserID'];
		
$SQLuser = "SELECT email,realname,phone
	               
			FROM www_users
			WHERE userid='" . $userid. "'
			";
	
			$resultuser = DB_query($SQLuser);
	
			$myrowuser = DB_fetch_array($resultuser) ;
		$emailm= $myrowuser['email'];
$realname= $myrowuser['realname'];
	$userphone= $myrowuser['phone'];
		
//get debtor headoffice details:
		$SQL100 = "SELECT 	name,
							address1,
						     address2,
				   			 address3,
							 address4,
							 address5,
							 address6
							 
			FROM debtorsmaster
			WHERE debtorno='" . $myrow['debtorno'] . "'
			";
	
	$result100 = DB_query($SQL100);
	
	$myrow100 = DB_fetch_array($result100) ;
		$debtorname = $myrow100['name'];
		
		$address1 = $myrow100['address1'];
		$address2 = $myrow100['address2'];
		$address3 = $myrow100['address3'];
		$address4 = $myrow100['address4'];
		$address5 = $myrow100['address5'];
		$address6=  $myrow100['address6'];
		
		
		
		
		//now get other customer details from custbranch tables
		
				$SQL2 = "SELECT brname,
								braddress1,
								braddress2,
								braddress3,
								phoneno,
								contactname,
								email
				   
			FROM custbranch
			WHERE branchcode='" . $myrow4['branch']. "'
			";
	
	$result2 = DB_query($SQL2);
	
	$myrow2 = DB_fetch_array($result2) ;
		
		$branchname = $myrow2['brname'];
		$branchad1 = $myrow2['braddress1'];
		$branchad2 = $myrow2['braddress2'];
		$branchad3 = $myrow2['braddress3'];
		$branchad4 = $myrow2['braddress3'];
		$branchad5 = $myrow2['braddress3'];
		$branchad6 = $myrow2['braddress3'];
		$phoneno = $myrow2['phoneno'];
		$contactname = $myrow2['contactname'];
		$email = $myrow2['email'];
		
		
		
	$username = $_SESSION['UsersRealName'];
		
		$userid = $_SESSION['userid'];	
			$dash = '-';
		
		
		//insert in salesorders to create new sales order/quote. The system can only add quotation and cant update it.
		//$Result = DB_Txn_Begin();

	   
	
$PaperSize = 'A4_Landscape';// PDFStarter.php: $Page_Width=842; $Page_Height=595; $Top_Margin=30; $Bottom_Margin=30; $Left_Margin=40; $Right_Margin=30;
include('includes/PDFStarter.php');
$pdf->addInfo('Title', _('Customer Invoice') );
$pdf->addInfo('Subject', _('Invoice') . ' ' . $_GET['orderno']);
$FontSize = 12;
$line_height = 12;// Recommended: $line_height = $x * $FontSize.
		
		include('includes/PDFQuotationPageHeader2.inc');



		
			$SQL1 = "SELECT *
			FROM salesorderdetails 
			WHERE orderno='" . $orderno . "'
			";
	
			$result1 = DB_query($SQL1);

			while ($myrow1 = DB_fetch_array($result1)) { 
		
		
		//todays date.This is to show today quote was generated
	
		
		
		//start digging to get projectcosting details. Key field is pcid. After which you use this to determine line items from projectcostingdetail and projectcostingdetaillabor tables. Once this values have been determined insert in salesorderdetails and also write to pdf.
		
		
	
			
			   $ListCount ++;
				

		$YPos -= $line_height;// Increment a line down for the next line item.
				

		if ((mb_strlen($myrow1['narrative']) >200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($myrow1['narrative']) >1 AND $YPos-$line_height <= 62)
			OR $YPos-$line_height <= 50){
		/* We reached the end of the page so finsih off the page and start a newy */
			include('includes/PDFQuotationPageHeader2.inc');
		} //end if need a new page headed up

			
			
						
			
	
				
	
								//$myrow6 = DB_fetch_array($result6) ;
				
				
				//write to pdf
				
				$ovamount = $myrow1['unitprice'] * $myrow1['quantity'];
			    
			 
			
				
				$stup = locale_number_format($myrow1['unitprice'],2);
				
				$stovm = locale_number_format($ovamount,2);

	
			
			$FontSize = 10;// Font size for the line item.

		$LeftOvers = $pdf->addText($Left_Margin, $YPos+$FontSize, $FontSize,$myrow1['stkcode']);
		$LeftOvers = $pdf->addText(145, $YPos+$FontSize, $FontSize, $myrow1['stock_name']);
	
		$LeftOvers = $pdf->addTextWrap(585, $YPos,85,$FontSize,$myrow1['quantity'],'right');
		$LeftOvers = $pdf->addTextWrap(650, $YPos,85,$FontSize,$stup,'right');
		$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, $stovm,'right');
			
			
			// Prints salesorderdetails.narrative:
		$FontSize2 = $FontSize*0.8;// Font size to print salesorderdetails.narrative.
		$Width2 = $Page_Width-$Right_Margin-145;// Width to print salesorderdetails.narrative.
		$LeftOvers = trim($myrow1['narrative']);
		//**********
		$LeftOvers = str_replace('\n', ' ', $LeftOvers);// Replaces line feed character.
		$LeftOvers = str_replace('\r', '', $LeftOvers);// Delete carriage return character
		$LeftOvers = str_replace('\t', '', $LeftOvers);// Delete tabulator character
			
			while (mb_strlen($LeftOvers) > 1) {
			$YPos -= $FontSize2;
			if ($YPos < ($Bottom_Margin)) {// Begins new page.
				include('includes/PDFQuotationPageHeader2.inc');
			}
			$LeftOvers = $pdf->addTextWrap(145, $YPos, $Width2, $FontSize2, $LeftOvers);
		}
			
			if ((mb_strlen($quoteterms) >200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($quoteterms) >1 AND $YPos-$line_height <= 62)
			OR $YPos-$line_height <= 50){
		/* We reached the end of the page so finish off the page and start a newy */
			include('includes/PDFQuotationPageHeader2.inc');
	} //end if need a new page headed up

		}	
		

		
			
		
		
		$FontSize = 10;
		
			

$totalafterretention = $myrow['amount'] ;

			$vat = $totalafterretention * 0.09;
			
			$totalvip = $totalafterretention+$vat;
	
	       //2 decimal point outpu
	
	$vatfinal = locale_number_format($vat,2);
				$displaytotalfinal= locale_number_format($totalvip,2);
			$DisplayFreight = 0;
			
			
			
	$YPos -= $line_height;
/*rule off for total */
			$pdf->line($Page_Width-$Right_Margin-773, $YPos+7,$Page_Width-$Right_Margin,$YPos+7);
	$pdf->line($Page_Width-$Right_Margin-773, $YPos+5,$Page_Width-$Right_Margin,$YPos+5);

	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90-655, $YPos-6, 655, $FontSize, _('Total VEP'),'right');
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-7, 90, $FontSize, locale_number_format($myrow['amount'],2), 'right');

$YPos -= $FontSize;
	


	$YPos -= $FontSize;
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90-655, $YPos-8, 655, $FontSize, _('VAT'), 'right');
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-9, 90, $FontSize, $vatfinal, 'right');
	$YPos -= $FontSize;

	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90-655, $YPos-10, 655, $FontSize, _('Total VIP'),'right');
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-11, 90, $FontSize, $displaytotalfinal, 'right');

	// Print salesorders.comments:
	$YPos -= $FontSize*2;
			$pdf->addText($XPos+300, $YPos+$FontSize, $FontSize, _('TERMS: Price is valid till 30 Days from the quote date.'),'centre');
	$pdf->addText($XPos+300, $YPos+$FontSize-15, $FontSize, _('Wormald Fire & Security looking forward in serving you'));
	$pdf->addText($XPos, $YPos+$FontSize-45, 15, _('Notes').':');
	$Width2 = $Page_Width-$Right_Margin-120;// Width to print salesorders.comments.
$LeftOvers =	$pdf->addText($XPos, $YPos+$FontSize-65, $FontSize, $myrow20['notes']);
	//$LeftOvers = trim($quoteterms);
	//**********
	$LeftOvers = str_replace('\n', ' ', $LeftOvers);// Replaces line feed character.
	$LeftOvers = str_replace('\r', '', $LeftOvers);// Delete carriage return character
	$LeftOvers = str_replace('\t', '', $LeftOvers);// Delete tabulator character
	//**********
	while(mb_strlen($LeftOvers) > 1) {
		$YPos -= $FontSize;
		if ($YPos < ($Bottom_Margin)) {// Begins new page.
			include('includes/PDFQuotationPageHeader2.inc');
		}
		$LeftOvers = $pdf->addTextWrap(40, $YPos, $Width2, $FontSize, $LeftOvers);
	}

 
ob_end_clean();
	 //its not an email just print the invoice to PDF

//$email = customer's email and $emailm is users email.

		include ('includes/htmlMimeMail.php');
//require_once ('mail/PHPMailer.php');

		$FileName =  'evergreenquote_'  . $orderno . '.pdf';
		$pdf->Output($FileName,'F');
	$mail = new htmlMimeMail();
      $mail->setCRLF("\r\n");
$mail->setHeadCharset('ISO-8859-1');
		$Attachment = $mail->getFile($FileName);
		$mail->setText(_('Please find attached quotation number:') . ' ' . $orderno);
		$mail->SetSubject(' Quotation-' . $orderno);
		$mail->addAttachment($Attachment, $FileName, 'application/pdf');


			
	
			$mail->setFrom($realname .'-'. $emailm . '>');
		$result = $mail->send(array($email));

		
//mail($email,'Quotation','attached is the Quotation',$emailm);

		$Title = _('Emailing') . ' Quotation: ' . _(' ') . ' ' . $orderno;
		include('includes/header.inc');
		echo '<p> Quotation:' . ' ' . $orderno . ' ' . _('has been emailed to customer') . ' ' .$debtorname.' '.'email:'. $email;
		include('includes/footer.inc');
		exit;

	


//include('includes/footer.inc'); 

?>
