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


	

	
	
if (isset($_GET['dispatchid'])) {
		$_SESSION['dispatchid'] = stripslashes($_GET['dispatchid']);
	}
	$stockrequestno= $_SESSION['dispatchid'];
if (isset($_GET['loccode'])) {
		$_SESSION['loccode'] = stripslashes($_GET['loccode']);
	}



$SQLstockrequest = "SELECT *
	               
			FROM stockrequest
			WHERE dispatchid='" . $stockrequestno. "'
			";
	
			$resultstockrequest = DB_query($SQLstockrequest);

	$myrowstockrequest= DB_fetch_array($resultstockrequest);

$costglcode = $myrowstockrequest['cost_glcode'];
//$datetimeR = $myrowstockrequest['datetime_received'];



	$loccode= $myrowstockrequest['loccode'];
if (isset($_GET['departmentid'])) {
		$_SESSION['departmentid'] = stripslashes($_GET['departmentid']);
	}
	$departmentid= $myrowstockrequest['departmentid'];
if (isset($_GET['despatchdate'])) {
		$_SESSION['despatchdate'] = stripslashes($_GET['despatchdate']);
	}
	$despatchdate= $myrowstockrequest['despatchdate'];


$date = date('d-m-Y',strtotime($despatchdate));
if (isset($_GET['jobno'])) {
		$_SESSION['jobno'] = stripslashes($_GET['jobno']);
	}
	$jobno= $myrowstockrequest['jobno'];
if (isset($_GET['authorised'])) {
		$_SESSION['authorised'] = stripslashes($_GET['authorised']);
	}
	$authorised= $_SESSION['authorised'];
if (isset($_GET['authorized_by'])) {
		$_SESSION['authorized_by'] = stripslashes($_GET['authorized_by']);
	}
	$authorized_by= $myrowstockrequest['authorized_by'];
if (isset($_GET['narrative'])) {
		$_SESSION['narrative'] = stripslashes($_GET['narrative']);
	}
	$narrative= $myrowstockrequest['narrative'];
if (isset($_GET['initiator'])) {
		$_SESSION['initiator'] = stripslashes($_GET['initiator']);
	}
	$initiator= $myrowstockrequest['initiator'];

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

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

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

if (isset($_GET['client'])) {
		$_SESSION['client'] = stripslashes($_GET['client']);
	}
	$client= $myrowstockrequest['client'];
if (isset($_GET['account'])) {
		$_SESSION['account'] = stripslashes($_GET['account']);
	}
	$account= $myrowstockrequest['account'];

if (isset($_GET['employee'])) {
		$_SESSION['employee'] = stripslashes($_GET['employee']);
	}
	$employee= $_SESSION['employee'];
if (isset($_GET['datetime'])) {
		$_SESSION['datetime'] = stripslashes($_GET['datetime']);
	}
	$datetime= $myrowstockrequest['datetime'];

$timestamp = strtotime($datetime);
$fijitime = date("d-m-Y h:i:sa", $timestamp);

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


	$datetimeR=$myrowstockrequest['datetime_received'];


if ($datetimeR =='0000-00-00 00:00:00'){
	
	$fijitimeR =' ';
}else{
	
$timestampR = strtotime($datetimeR);
$fijitimeR = date("d-m-Y h:i:sa", $timestampR);	
	
}


//first check whether this costing is approved or not

//Print header PDFStockRequestheader.inc
$SQL77 = "SELECT 	departmentname
							 
			FROM department
			WHERE departmentid='" . $departmentid . "'
			";
	
	$result77 = DB_query($SQL77);
	
	$myrow77 = DB_fetch_array($result77) ;

$department = $myrow77['departmentname'];


$SQL771 = "SELECT 	name
							 
			FROM debtorsmaster
			WHERE debtorno='" . $client . "'
			";
	
	$result771 = DB_query($SQL771);
	
	$myrow771 = DB_fetch_array($result771) ;

$customername = $myrow771['name'];
$SQL7710 = "SELECT 	first_name, last_name
							 
			FROM employeemaster
			WHERE employeeid='" . $employee . "'
			";
	
	$result7710 = DB_query($SQL7710);
	
	$myrow7710 = DB_fetch_array($result7710) ;

$firstname = $myrow7710['first_name'];
$lastname = $myrow7710['last_name'];

//Print header PDFStockRequestheader.inc
$SQLjobs = "SELECT 	*
							 
			FROM projectdetails
			WHERE projectid='" . $jobno . "'
			";
	
	$resultjobs = DB_query($SQLjobs);
	
	$myrowjobs = DB_fetch_array($resultjobs) ;

$wip = $myrowjobs['wip_expense'];

$projectname = $myrowjobs['project_name'];


$SQLP = "SELECT 	accountname
							 
			FROM chartmaster
			WHERE accountcode ='" . $producttype . "'
			";
	
	$resultP= DB_query($SQLP);
	
	$myrowP = DB_fetch_array($resultP) ;

$accountname = $myrowP['accountname'];


	//fire contract fiji wide
if ($producttype == '7100500'){
		
		
$old_sales_code = '103';
$old_gl_code = 'G610';
}
	
	//7102400 Security Guard Contract Income

if ($producttype == '7102400'){
		
		
	 $old_sales_code = '602';
     $old_gl_code = 'G70021';
										
}
	   if ($producttype == '7102400'){
		
		
		$old_sales_code = '601';
		$old_gl_code = 'G70011';
	   }
	
	//security mobile income
			   if ($producttype == '7102401'){
		
		
		$old_sales_code = '604';
       $old_gl_code = 'G70022';
								
		
	}
	   if ($producttype == '7102401'){
		
		
	$old_sales_code = '603';
    $old_gl_code = 'G70012';
										

		
	}
	  if ($producttype == '7102401'){
		
		
		$old_sales_code = '609';
      $old_gl_code = 'G70031';
										
				
	}
	
		   if ($producttype == '7102400'){
		
		
		 $old_sales_code = '609';
        $old_gl_code = 'G70031';
							
		
	}
	
	
//Fire Equipment contract
	 if ($producttype == '7102301'){
		
		
	$old_sales_code = '104';
    $old_gl_code = 'G610';
					
		
	}
	
	//Fire sprinkler contract
	
	 if ($producttype == '7102300'){
		
		 $old_sales_code = '107';
                                        	$old_gl_code = 'G610';
					
		
	}

	//Fire Prepaid suva
	
	 if ($producttype == '7101005'){
		
		
		 $old_sales_code = '111';
                                        	$old_gl_code = 'G61101';
					
	}
	
	
	//FE Sales
	 if ($producttype == '7100900'){
		
		
		$old_sales_code = '202';
                                        	$old_gl_code = 'W7020102';
							
		
	}
	 if ($producttype == '7100900'){
		
		
		 $old_sales_code = '302';
                                        	$old_gl_code = 'W7020202';
							
		
	}
   if ($producttype == '7100900'){
		
		
		 $old_sales_code = '402';
                                        	$old_gl_code = 'W7020302';
						
	}
	
	// FE servicing
	   if ($producttype == '7101003'){
		
		
		$old_sales_code = '210';
                                        	$old_gl_code = 'W7020106';
						
		
	}
	   if ($producttype == '7101003'){
		
		
		$old_sales_code = '310';
                                        	$old_gl_code = 'W7020206';
						
		
	}
		  if ($producttype == '7101003'){
		
	$old_sales_code = '410';
    $old_gl_code = 'W7020306';
					
		
	}
	
	//fire alarm small jobs
	
	  if ($producttype == '7100600'){
		
		
		 $old_sales_code = '207';
                                        	$old_gl_code = 'W7060103';
								
		
	}
	   if ($producttype == '7100600'){
		
		$old_sales_code = '307';
                                        	$old_gl_code = 'W7060203';
					
		
	}
		//7102200 Fire Sprinkler Small Jobs
	   if ($producttype == '7102200'){
		
		
		$old_sales_code = '212';
        $old_gl_code = 'W7060107';
										
		
	}
	   if ($producttype == '7102200'){
		
	 $old_sales_code = '312';
                                        	$old_gl_code = 'W7060207';
						
	}
		 //7100700  Annuals Survey  fee
	
	
	//7101001 Fire alarm monthly testing

	//7100300 Brigade Sales
	   if ($producttype == '7100300'){
		
		
	 $old_sales_code = '201';
                                        	$old_gl_code = 'W7020101';
							
	}
	   if ($producttype == '7100300'){
		
		$old_sales_code = '301';
                                        	$old_gl_code = 'W7020201';
							
		
	}
		  if ($producttype == '7100300'){
		
		$old_sales_code = '401';
                                        	$old_gl_code = 'W7020301';
								
		
	}
	
	
	
	//7101006 Fire Pump Biannual Servicing
	
	
//7100603 Special Hazard R102 Servicing
   if ($producttype == '7100603'){
		
		
		$old_sales_code = '301';
                                        	$old_gl_code = 'W7020201';
									
	}
	
	//7101001 Fire alarm weekly testing

	   if ($producttype == '7101001'){
		
		
		 $old_sales_code = '205';
                                        	$old_gl_code = 'W7040103';
								
		
	}
	   if ($producttype == '7101001'){
		
		
	$old_sales_code = '305';
                                        	$old_gl_code = 'W7040203';
									
	}
	   if ($producttype == '7101001'){
		
		
		 $old_sales_code = '405';
                                        	$old_gl_code = 'W7040303';
								
	}
	
	//7100700  Annuals Survey  fee
	   if ($producttype == '7100700'){
		
		
		 $old_sales_code = '204';
                                        	$old_gl_code = 'W7030103';
							
		
	}
	   if ($producttype == '7100700'){
		
		
		 $old_sales_code = '304';
                                        	$old_gl_code = 'W7040203';
									
		
	}
	
	
	
	//7101000	Fire Training Sales
	
		   if ($producttype == '7101000'){
		
	$old_sales_code = '206';
                                        	$old_gl_code = 'W7020401';
									
		
	}
	   if ($producttype == '7101000'){
		
		$old_sales_code = '306';
                                        	$old_gl_code = 'W7020402';
								
		
	}
	
	//7100200 Installs Access System Small Jobs Income
			   if ($producttype == '7100200'){
		
		
		$old_sales_code = '704';
                                        	$old_gl_code = 'S70004';
								
		
	}
	   if ($producttype == '7100200'){
		
		
	$old_sales_code = '734';
                                        	$old_gl_code = 'S700204';
								
	}
	   if ($producttype == '7100200'){
		
		
		 $old_sales_code = '703';
                                        	$old_gl_code = 'S700301';
								
		
	}
	
	//access system contract 7100100
	
		   if ($producttype == '7100100'){
		
		
	$old_sales_code = '600';
                                        	$old_gl_code = 'G610';
								
		
	}
	   if ($producttype == '7100200'){
		
		
		$old_sales_code = '700';
                                        	$old_gl_code = 'G610';
										
	}
	//cctv contract 7100400
	   if ($producttype == '7100400'){
		
		
	$old_sales_code = '600';
                                        	$old_gl_code = 'G610';
								
	}
	   if ($producttype == '7100400'){
		
		
		$old_sales_code = '700';
                                        	$old_gl_code = 'G610';
							
		
	}

	//cctv small jobs
			   if ($producttype == '7100401'){
		
		
		$old_sales_code = '704';
                                        	$old_gl_code = 'S70004';
									
		
	}
	   if ($producttype == '7100401'){
		
		
		$old_sales_code = '734';
                                        	$old_gl_code = 'S700204';
							
		
	}
	
	//7100602	Installs Alarm Small Jobs
	
	 if ($producttype == '7100602'){
		
		
	 $old_sales_code = '704';
                                        	$old_gl_code = 'S70004';
									
		
	}
	   if ($producttype == '7100602'){
		
		
	$old_sales_code = '734';
                                        	$old_gl_code = 'S700204';
								
		
	}
	
	//7100601	Installs Alarm Contract
	  if ($producttype == '7100601'){
		
		
	$old_sales_code = '600';
                                        	$old_gl_code = 'G610';
								
		
	}
	   if ($producttype == '7100601'){
		
		 $old_sales_code = '700';
                                        	$old_gl_code = 'G610';
								
		
	}
	
	
	//7100201	Installs Prepaid Incomen G61101

		  if ($producttype == '7100201'){
		
		
	 $old_sales_code = '801';
                                        	$old_gl_code = 'G61101';
					
	}
	   if ($producttype == '7100201'){
		
		$old_sales_code = '621';
                                        	$old_gl_code = 'G61101';
								
	}
	
	// 7100202 installs alarm testing
	  if ($producttype == '7100202'){
		
		
		 $old_sales_code = '701';
                                        	$old_gl_code = 'S70002';
							
		
	}
	  
	
	//monitoring
	
	  if ($producttype == '7101800' || $producttype == '7102000' ){
		
		
		$old_sales_code = '803';
                                        	$old_gl_code = 'S70005';
							
	}
	
	//cit
	
	  if ($producttype == '7102600'){
		
		$old_sales_code = '900';
                                        	$old_gl_code = 'G70050';
								
		
	}
	

$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', _('Stock Request Form') );
$pdf->addInfo('Subject', _('Stock Request') );
$FontSize = 12;
$line_height = 12;// Recommended: $line_height = $x * $FontSize.
		
		include('includes/PDFStockRequestheader.inc');


//print items

$SQL = "SELECT *
	               
			FROM stockrequestitems
			WHERE dispatchid='" . $stockrequestno. "'
			";
	
			$result7979 = DB_query($SQL);
	
$grandtotal = 0;
			while ($myrow7979 = DB_fetch_array($result7979)) { 

				
				//get stock name and cost value
				
				$SQL3 = "SELECT description,materialcost
	               
			FROM stockmaster
			WHERE stockid='" . $myrow7979['stockid']. "'
			";
	
			$result3= DB_query($SQL3);
				$myrow3 = DB_fetch_array($result3);
				
				  $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/PDFStockRequestheader.inc');
		} //end if need a new page headed up

			
								//$myrow6 = DB_fetch_array($result6) ;
				
				
				//write to pdf
				
				$total = $myrow7979['quantity'] * $myrow3['materialcost'] ;
			    
			 
			   $grandtotal += $total;
				
				$costprice = locale_number_format($myrow3['materialcost'],2);
				
				$roundtotal= locale_number_format($total,2);

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

		$LeftOvers = $pdf->addText($Left_Margin, $YPos+$FontSize, $FontSize,$myrow7979['stockid']);
		$LeftOvers = $pdf->addText(145, $YPos+$FontSize, $FontSize, $myrow3['description']);
	
		$LeftOvers = $pdf->addTextWrap(420, $YPos,85,$FontSize,$myrow7979['quantity'],'right');
					$LeftOvers = $pdf->addTextWrap(500, $YPos,85,$FontSize,$myrow7979['qtydelivered'],'right');
		
				$LeftOvers = $pdf->addTextWrap(600, $YPos,85,$FontSize,$myrow7979['uom'],'right');
				$LeftOvers = $pdf->addTextWrap(650, $YPos,85,$FontSize,$costprice,'right');
		$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, $roundtotal,'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/PDFStockRequestheader.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/PDFStockRequestheader.inc');
	} //end if need a new page headed up

				
				
				
				
		}	
		

		
			
		
		
		$FontSize = 10;
		

			
	
	
	$grand = locale_number_format($grandtotal,2);
		
			$DisplayFreight = 0;
			
			
			
	$YPos -= $line_height;
	/*rule off for total */
			$pdf->line($Page_Width-$Right_Margin-222, $YPos+7,$Page_Width-$Right_Margin,$YPos+7);
$YPos -= $FontSize;
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90-655, $YPos, 655, $FontSize, _('GRAND TOTAL'), 'right');
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, _('$').$grand, 'right');
	$YPos -= $FontSize;





	// Print salesorders.comments:
	$YPos -= $FontSize*3;
	$pdf->addText($XPos+550, $YPos+$FontSize, $FontSize, _('Stock Received By:'));
    $pdf->addText($XPos+650, $YPos+$FontSize, $FontSize, $employee.$firstname.$lastname);
$pdf->line($Page_Width-$Right_Margin-222, $YPos+$FontSize*-1,$Page_Width-$Right_Margin,$YPos+$FontSize*-1);


	$pdf->addText($XPos+550, $YPos+$FontSize*-2, $FontSize, _('Authorized By:'));
    $pdf->addText($XPos+650, $YPos+$FontSize*-2, $FontSize, $authorized_by);
$pdf->line($Page_Width-$Right_Margin-222, $YPos+$FontSize*-3.5,$Page_Width-$Right_Margin,$YPos+$FontSize*-3.5);

$pdf->addText($XPos+550, $YPos+$FontSize*-4, $FontSize, _('Authorized Time:'));
    $pdf->addText($XPos+650, $YPos+$FontSize*-4, $FontSize, $datetime);
$pdf->line($Page_Width-$Right_Margin-222, $YPos+$FontSize*-3.5,$Page_Width-$Right_Margin,$YPos+$FontSize*-3.5);


$pdf->addText($XPos+550, $YPos+$FontSize*-6, $FontSize, _('Receivers Signature:'));

$pdf->line($Page_Width-$Right_Margin-222, $YPos+$FontSize*-3.5,$Page_Width-$Right_Margin,$YPos+$FontSize*-3.5);

$pdf->addText($XPos+550, $YPos+$FontSize*-8, $FontSize, _('Date Received'));
  $pdf->addText($XPos+650, $YPos+$FontSize*-8, $FontSize, $datetimeR);


	$pdf->addText($XPos, $YPos+$FontSize-45, $FontSize, _('Comments').':');
	$Width2 = $Page_Width-$Right_Margin-120;// Width to print salesorders.comments.
$LeftOvers =	$pdf->addText($XPos, $YPos+$FontSize-55, $FontSize, $narrative);
	//$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/PDFStockRequestheader.inc');
		}
		$LeftOvers = $pdf->addTextWrap(40, $YPos, $Width2, $FontSize, $LeftOvers);
	}


	
	

ob_end_clean();
	 //its not an email just print the invoice to PDF
		$pdf->OutputD('stockrequest_' . $stockrequestno. '.pdf');

		
		
	
	$pdf->__destruct();
		
	

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

?>
