<?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['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'];

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

$_POST['lpo'] = $myrow['lpo'];

$SQLac = "SELECT *
	               
			FROM debtortrans
			WHERE transno ='" . $InvoiceNo. "'
			";
	
			$resultac = DB_query($SQLac);
	
			$myrowac = DB_fetch_array($resultac) ;
$accountnumber = $myrowac['account_number'];

$account = $accountnumber;
$_POST['account'] = $accountnumber;
$_POST['lpo'] = $myrowac['lpo'];

$sql4011 =  "SELECT billing_name, sub_billing_name
			FROM debtoraccount
			
			WHERE account_number = '" . $accountnumber. "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result4011 = DB_query($sql4011,$ErrMsg);
	$myrow4011 = DB_fetch_array($result4011) ;

		$billingname = $myrow4011['billing_name'];

$subbillingname = $myrow4011['sub_billing_name'];


$SQL = "SELECT *
	               
			FROM debtortrans
			WHERE transno='" . $InvoiceNo. "'
			";
	
			$result = DB_query($SQL);
	
			$myrow1000 = DB_fetch_array($result) ;

$quotedated = date('Y-m-d', strtotime($myrow1000['trandate']));
		$quotedate = date('d-m-Y',strtotime($myrow1000['trandate']));
$yeardate = date('Y',strtotime($myrow1000['trandate']));
$monthdate =date('M',strtotime($myrow1000['trandate']));
$departmentid =$myrow1000['department'];
$bill_id = $myrow1000['monitoring_id'];

if ($departmentid == '11'){
	
	
$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 salesorderdetails
			WHERE orderno ='" . $myrow['orderno'] . "'
		
	        
			";
	$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['stock_name']);

		//$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['westpac_costcenter'],'right');

			}
			
			

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

				if ($myrowm['stock_name'] ==''){
					$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($invtext) >200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($invtext) >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->OutputD('evergreeninvoice_' . $InvoiceNo. '.pdf');
	
	$pdf->__destruct();
		
	
}
else {



$retention = $myrow1000['retention'];
$cashcustomeradd = $myrow1000['cash_customer_address'];
$cashcustomer = $myrow1000['cash_customer_name'];
$debtorno = $myrow1000['debtorno'];
$contactperson = $myrow1000['contact_person'];
	$invtext = $myrow1000['invtext'];
	
		$sql4 =  "SELECT projectid,
	               project_type,
				   project_name,
				   retention_rate,
				  
				   customer,
				   branch,
				
				   contact_number,
				   required_date,
				   start_date,
				 
				   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) ;

//look for department id
	$sql40 =  "SELECT departmentname
			FROM department
			
			WHERE departmentid = '" . $myrow1000['department']. "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result40 = DB_query($sql40,$ErrMsg);
	$myrowd =DB_fetch_array($result40);
		$department = $myrowd['departmentname'];


//$quotedate = $myrow['orddate'];
	$myrow40 = DB_fetch_array($result40) ;

		$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= $myrow['preparedby'];
		
//get debtor headoffice details:
		$SQL100 = "SELECT 	name,
							address1,
						     address2,
				   			 address3,
							 address4,
							 address5,
							 address6,
							 paymentterms
							 
			FROM debtorsmaster
			WHERE debtorno='" . $debtorno . "'
			";
	 
	$result100 = DB_query($SQL100);
	
	$myrow100 = DB_fetch_array($result100) ;

		$debtorname = $myrow100['name'];
		

	

 
 
if($debtorname == 'CASH SALE'){
	
	 $billingname = $cashcustomer;
	
}

$address1 = $myrow100['address1'];
		$address2 = $myrow100['address2'];
		$address3 = $myrow100['address3'];
		$address4 = $myrow100['address4'];
		$address5 = $myrow100['address5'];
		$address6=  $myrow100['address6'];
		
		
		$SQL1002 = "SELECT 	*
							 
			FROM paymentterms
			WHERE termsindicator='" . $myrow100['paymentterms'] . "'
			";
	
	$result1002 = DB_query($SQL1002);
	
	$myrow1002 = DB_fetch_array($result1002) ;
		
		

$readableduedate = CalcDueDate(ConvertSQLDate($myrow1000['trandate']), $myrow1002['dayinfollowingmonth'], $myrow1002['daysbeforedue']);
		
		//now get other customer details from custbranch tables
		
				$SQL2 = "SELECT brname,
								braddress1,
								braddress2,
								braddress3,
								phoneno,
								contactname,
								email
				   
			FROM custbranch
			WHERE branchcode='" . $myrow['branchcode']. "'
			";
	
	$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();

	     
			//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'];	
	


	if ($debtorno == '165' && $departmentid =='11') {
	$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/PDFInvoiceHeaderAHC.inc');



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

		
		$LeftOvers = $pdf->addText(245, $YPos+$FontSize, $FontSize,_('TO PROVIDE SECURITY SERVICES FOR THE AUSTRALIAN HIGH COMMISSION SUVA'));
$LeftOvers = $pdf->addText(290, $YPos, $FontSize,_('(37 PRINCES ROAD) AND NOMINATED RESIDENCES, SUVA, FIJI'));
		
			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/PDFInvoiceHeaderAHC.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);
				
				
				$tax = ($stup * 0.15);
				
				$stovm = ($stup + $tax );
				
				$qty =locale_number_format($myrow1['quantity'],2);
				
if ($stovm == '0'){
	
	$stovm = '';
	$tax = '';
	$myrow1['uom'] = '';
	$myrow1['tax_code']='';
	$stup='';
	$qty ='';
}
		
				
				if ($myrow1['tax_code'] =='Vat'){
					
					$myrow1['tax_code'] ='VAT';
				}
	
			
			$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(600, $YPos,85,$FontSize,$stup,'right');
		$LeftOvers = $pdf->addTextWrap(650, $YPos,85,$FontSize,$tax,'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/PDFInvoiceHeaderAHC.inc');
			}
			$LeftOvers = $pdf->addTextWrap(145, $YPos, $Width2, $FontSize2, $LeftOvers);
		}
			
			if ((mb_strlen($invtext) >200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($invtext) >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/PDFInvoiceHeaderAHC.inc');
	} //end if need a new page headed up

		}	
		

		
			
		
		
		$FontSize = 10;
	$finalamount = ($myrow1000['ovamount'] + $myrow1000['ovgst'] - $myrow1000['retention']) ;

$vat = locale_number_format($myrow1000['ovgst'],2);

	$YPos -= $line_height;
/*rule off for total */
			$pdf->line($Page_Width-$Right_Margin-810, $YPos+7,$Page_Width-$Right_Margin,$YPos+7);
	

	$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($myrow1000['ovamount'],2), 'right');


	$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-11, 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, _('$').locale_number_format($finalamount,2), 'right');

		
		//line
$pdf->line($Page_Width-$Right_Margin-810, $YPos+$FontSize-25,$Page_Width-$Right_Margin,$YPos+$FontSize-25);
	// Print salesorders.comments:
	$YPos -= $FontSize*2;
				if (($YPos-$line_height <= 75)
			OR ($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/PDFInvoiceHeaderAHC.inc');
	} //end if need a new page headed up

		
	$pdf->addText($XPos+250, $YPos+$FontSize-25, $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-140;// Width to print salesorders.comments.
    $LeftOvers = $pdf->addTextWrap($XPos, $YPos+$FontSize-95,780, $FontSize, $myrow1000['invtext']);
	        if (mb_strlen($LeftOvers)>0) {
				$LeftOvers = $pdf->addTextWrap($XPos,$YPos+$FontSize-110,780,$FontSize,$LeftOvers);
				if (mb_strlen($LeftOvers)>0) {
					$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 :( */
				}
			}
         
//$pdf->addText($XPos, $YPos+$FontSize-120, $FontSize, _('Test'). $LeftOvers);
	//$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/PDFInvoiceHeaderAHC.inc');
		}
		$LeftOvers = $pdf->addTextWrap($XPos, $YPos, $Width2, $FontSize, $LeftOvers);
	}


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

		//$pdf->Output('evergreeninvoice_' . $InvoiceNo, 'I');
		$pdf->OutputD('evergreeninvoice_' . $InvoiceNo. '.pdf');
	
	$pdf->__destruct();
			
		
	}
	else {
$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/PDFInvoiceHeader.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/PDFInvoiceHeader.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);
				
				$qty =locale_number_format($myrow1['quantity'],2);
				
if ($stovm == '0'){
	
	$stovm = '';
	$myrow1['uom'] = '';
	$myrow1['tax_code']='';
	$stup='';
	$qty ='';
}
		
				
				if ($myrow1['tax_code'] =='Vat'){
					
					$myrow1['tax_code'] ='VAT';
				}
	
			
			$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(470, $YPos,85,$FontSize,$myrow1['tax_code'],'right');
		$LeftOvers = $pdf->addTextWrap(520, $YPos,85,$FontSize,$qty,'right');
				$LeftOvers = $pdf->addTextWrap(600, $YPos,85,$FontSize,$myrow1['uom'],'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/PDFInvoiceHeader.inc');
			}
			$LeftOvers = $pdf->addTextWrap(145, $YPos, $Width2, $FontSize2, $LeftOvers);
		}
			
			if ((mb_strlen($invtext) > 200 AND $YPos-$line_height <= 75)
			OR (mb_strlen($invtext) >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/PDFInvoiceHeader.inc');
	} //end if need a new page headed up

		}	
		

		
			
		
		
		$FontSize = 10;
	$finalamount = ($myrow1000['ovamount'] + $myrow1000['ovgst'] - $myrow1000['retention']) ;

$vat = locale_number_format($myrow1000['ovgst'],2);

	$YPos -= $line_height;
/*rule off for total */
			$pdf->line($Page_Width-$Right_Margin-810, $YPos+7,$Page_Width-$Right_Margin,$YPos+7);


	$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($myrow1000['ovamount'],2), '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, _('-$').locale_number_format($myrow1000['retention'],2), 'right');
	$YPos -= $FontSize;

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

	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90-655, $YPos-12, 655, $FontSize, _('Total'),'right');
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-13, 90, $FontSize, _('$').locale_number_format($finalamount,2), 'right');

				//line
$pdf->line($Page_Width-$Right_Margin-810, $YPos+$FontSize-25,$Page_Width-$Right_Margin,$YPos+$FontSize-25);
		
	// Print salesorders.comments:| BRED: 043581800012
	$YPos -= $FontSize*2;
		if (($YPos-$line_height <= 75)
			OR ($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/PDFInvoiceHeader.inc');
		}
	$pdf->addText($XPos+250, $YPos+$FontSize-25, $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 | HFC Bank: A/C: 100118984 '));
$pdf->addText($XPos+300, $YPos+$FontSize-45, $FontSize, _('BRED Bank: 043581800012 | 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-140;// Width to print salesorders.comments.
    $LeftOvers = $pdf->addTextWrap($XPos, $YPos+$FontSize-95,780, $FontSize, $myrow1000['invtext']);
	        if (mb_strlen($LeftOvers)>0) {
				$LeftOvers = $pdf->addTextWrap($XPos,$YPos+$FontSize-110,780,$FontSize,$LeftOvers);
				if (mb_strlen($LeftOvers)>0) {
					$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 :( */
				}
			}
         
//$pdf->addText($XPos, $YPos+$FontSize-120, $FontSize, _('Test'). $LeftOvers);
	//$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/PDFInvoiceHeader.inc');
		}
		$LeftOvers = $pdf->addTextWrap($XPos, $YPos, $Width2, $FontSize, $LeftOvers);
	}


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

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

			
		
			
		
			
		

		
		
			    
		
	


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

?>
