<?php
ob_start();
include('includes/session.inc');
$Title = _('Create Invoice');
include('includes/SQL_CommonFunctions.inc');


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

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



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

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


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


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



$sql4 =  "SELECT *
			FROM monitoring_master_data
			WHERE active = '1'
			AND account_number = '" . $account. "'
		
			
			";
	$ErrMsg = _('This customer could not be added    because');
			$result4 = DB_query($sql4,$ErrMsg);
		
		

		
		
		
		
			
$myrow4 = DB_fetch_array($result4);
			
		//	$account =$myrow4['account_number'];
	      $firstname =$myrow4['first_name'];
		  $lastname =$myrow4['last_name'];
	  $businessname =$myrow4['business_name'];
	  $billcity =$myrow4['bill_city'];
	  $billaddr =$myrow4['bill_address'];
			
		$yeardate = date('Y',strtotime($trandate));
		$monthdate =date('M',strtotime($trandate));
			//$quotedate = 
			   
		

	$department = 'Monitoring';	



//get periodno 
	
	
	    $query22 = "SELECT 
						periodno,
		             lastdate_in_period
		                 
					  
		          FROM periods
				    WHERE YEAR(lastdate_in_period) = '" . $yeardate . "'
					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
				
        $result22=DB_query($query22);
        $myrow22=DB_fetch_array($result22);
      
				$PeriodNo = $myrow22['periodno'];
		     	$data = $myrow22['lastdate_in_period'];	
				
	$quotedate = date('d-m-Y',strtotime($trandate));


		
				
$SQLmaster = "SELECT *
	               
			FROM monitoring_billing
			WHERE id='" . $bill_id. "'
			";
	
			$resultmaster = DB_query($SQLmaster);
	
			$myrowmaster = DB_fetch_array($resultmaster) ;

$due_date = $myrowmaster['due_date'];
$readableduedate = date('d-m-Y',strtotime($due_date));
//$quotedateraw = $_POST['order_date'] ;
$servicesprovidedmonth= date("M-Y", strtotime($trandate));

 //$sqldate= FormatDateForSQL(DB_escape_string($_POST['order_date'] ));

	$userid = $_SESSION['UserID'];
		
				
$SQLuser = "SELECT email,realname
	               
			FROM www_users
			WHERE userid='" . $userid. "'
			";
	
			$resultuser = DB_query($SQLuser);
	
			$myrowuser = DB_fetch_array($resultuser) ;
		$emailm= $myrowuser['email'];
$realname= $myrowuser['realname'];


		
			$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', _('MOnitoring') );
$FontSize = 12;
$line_height = 12;// Recommended: $line_height = $x * $FontSize.


		include('includes/PDFInvoiceHeaderMonitoring.inc');


		
	$YPos -= $line_height;//
	
			
		
	
	$LeftOvers = $pdf->addText(130, $YPos+$FontSize, $FontSize,_('Monitoring Premises - For the month of : ').$servicesprovidedmonth);
	$LeftOvers = $pdf->addText(130, $YPos+$FontSize, $FontSize,_('__________________'));
				
//$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90,$YPos,85,$FontSize,$vepamount,'right');
$account = $myrow4['account_number'];
			
	//loop and display all premises
	
$sqlm =  "SELECT *
			FROM monitoring_premises
			WHERE account_number ='" . $myrow4['account_number'] . "'
			AND active = '1'
	        
			";
	$ErrMsg = _('This customer could not be added    because');
			$resultmonitoring11 = DB_query($sqlm,$ErrMsg);
				
			$count =0;
	
	$totalamount= 0;
				
		while ($myrowm = DB_fetch_array($resultmonitoring11)) {
			
			
			
    
			
			   $ListCount ++;
				

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

		

			

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



		$LeftOvers = $pdf->addText(130, $YPos+$FontSize, $FontSize,$myrowm['premises']);

		//$LeftOvers = $pdf->addText(145, $YPos+$FontSize, $FontSize, _(''));

	//$LeftOvers = $pdf->addTextWrap(450, $YPos,85,$FontSize,_(''),'right');

	//	$LeftOvers = $pdf->addTextWrap(510, $YPos,85,$FontSize,_(''),'right');

			
		if ($account = "3220SS" || $account = "2566SS"  || $account = "2504SS" || $account = "1286SS" ){
	    $LeftOvers = $pdf->addTextWrap(570, $YPos,85,$FontSize,$myrowm['cost_centre'],'right');

			}
			
			

				$LeftOvers = $pdf->addTextWrap(745, $YPos,85,$FontSize,$myrowm['monthly_rate'],'right');

				if ($myrowm['premises'] ==''){
					$LeftOvers = $pdf->addTextWrap(745, $YPos,85,$FontSize,$ovamount,'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/PDFInvoiceHeaderMonitoring.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/PDFInvoiceHeaderMonitoring.inc');
				
				
				
	} //end if need a new page headed up

		  
		  
		  
		$totalamount +=  $myrowm['monthly_rate'];
		  
	  $count++;
			
			
		
			
		
		}	
		

//above
	   

      //$order_total_tax = $order_total_tax1 + $order_total_tax2 + $order_total_tax3;

		

	   		

	//update status

//check amount in master records
	
	

		
		
	

  



				

		$FontSize = 10;

	



	$YPos -= $line_height;

	/*rule off for total */
	
	
	


			$pdf->line($Page_Width-$Right_Margin-810, $YPos+7,$Page_Width-$Right_Margin,$YPos+7);

	$pdf->line($Page_Width-$Right_Margin-810, $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, _('$').$ovamount, 'right');

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

$YPos -= $FontSize;

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

	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-9, 90, $FontSize, _('$').$vat, 'right');

	   

	$YPos -= $FontSize;



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

	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-13, 90, $FontSize, _('$').$total_vip, 'right');


	// Print salesorders.comments:
	$YPos -= $FontSize*2;
	$pdf->addText($XPos+250, $YPos+$FontSize-20, $FontSize, _('TERMS: PAYMENT DUE ON 20th OF THE FOLLOWING MONTH OF PURCHASE'),'centre');
	$pdf->addText($XPos+200, $YPos+$FontSize-35, $FontSize, _('OUR BANKS: Westpac Bank: A/C: 9804778075 | ANZ Bank: A/C: 11879362 | BSP Bank: A/C: 9737614'));
$pdf->addText($XPos+300, $YPos+$FontSize-45, $FontSize, _('BILL PAY AVAILABLE VIA ONLINE BANKING'));
	$pdf->addText($XPos+300, $YPos+$FontSize-55, $FontSize, _('Evergreen International Fiji LLC appreciates your business!'));
$pdf->addText($XPos+330, $YPos+$FontSize-70, $FontSize, _('Powered by E-Business ERP 2.0'));
	$pdf->addText($XPos, $YPos+$FontSize-75, $FontSize, _('Notes').':');

	$Width2 = $Page_Width-$Right_Margin-120;// Width to print salesorders.comments.

$LeftOvers =	$pdf->addTextWrap($XPos, $YPos+$FontSize-95,780,$FontSize, $myrow4['notes']);

	//$LeftOvers = trim($quoteterms);

		//$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

	//**********

	   if (mb_strlen($LeftOvers)>0) {

				$LeftOvers = $pdf->addTextWrap($XPos,$YPos+$FontSize-110,780,$FontSize,$LeftOvers);

				if (mb_strlen($LeftOvers)>0) {

						//$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

	//**********

					$LeftOvers = $pdf->addTextWrap($XPos,$YPos+$FontSize-120,780,$FontSize,$LeftOvers);

					/*If there is some of the InvText leftover after 3 lines 200 wide then it is not printed :( */

				}

			}

	//**********

	$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/PDFInvoiceHeaderMonitoring.inc');

		}

		$LeftOvers = $pdf->addTextWrap(40, $YPos, $Width2, $FontSize, $LeftOvers);

	}

//$pdf->addText($XPos+330, $YPos+$FontSize-250, $FontSize, _('Powered by E-Business ERP 2.0'));

 /*end if there are line details to show on the quotation*/

	$timenow = time();

	$modifieddatetime = date("Y-m-d h:i:sa", $timenow);

	

//debtor trans and debtor vat
	
		$fname = preg_replace('/[^A-Za-z0-9]/', ' ', $firstname);
	$lname = preg_replace('/[^A-Za-z0-9]/', ' ', $lastname);
	$Bname = preg_replace('/[^A-Za-z0-9]/', ' ', $businessname);




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

		$pdf->Output('evergreeninvoice_' . $InvoiceNo, 'I');
		
	
	$pdf->__destruct();
		
	
		
	
	

			
		
			
		
			
		

		
		
			    
		
	


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

?>
