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

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

	$userid = $_SESSION['UserID'];

	// check if the user has set a default bank account for invoices, if not leave it blank


$SQL = "SELECT *
	               
			FROM overseas_purchase_orders
			WHERE id='" . $opono. "'
			";
	
			$result = DB_query($SQL);
	
			$myrow = DB_fetch_array($result) ;


$glcodesingle = $myrow['gl_code'];

$quotedated = date('Y-m-d', strtotime($myrow['entered_date']));
		$quotedate = date('d-m-Y',strtotime($myrow['entered_date']));
$yeardate = date('Y',strtotime($myrow['entered_date']));
$monthdate =date('M',strtotime($myrow['entered_date']));
//$departmentid =$myrow['department'];

$attention =$myrow['attention'];
$deliveryaddress = $myrow['delivery_address']; 
$currency =$myrow['currency'];
$quoteno = $myrow['quote_no'];
//look for department id
	$sql40 =  "SELECT departmentname
			FROM department
			
			WHERE departmentid = '" . $departmentid . "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result40 = DB_query($sql40,$ErrMsg);
	$myrowd =DB_fetch_array($result40);
		//$department = $myrowd['departmentname'];
$departmentsingle = $myrowd['departmentname'];

//get gl and department in multiline

			



//$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 	supplierid,
							suppname,
							address1,
						     address2,
				   			 address3,
							 address4,
							 address5,
							 address6,
							 paymentterms
							 
			FROM suppliers
			WHERE supplierid ='" . $myrow['supplier_name'] . "'
			";
	 
	$result100 = DB_query($SQL100);
	
	$myrow100 = DB_fetch_array($result100) ;

		$suppname = $myrow100['suppname'];
		
$supplierid = $myrow100['supplierid'];
	



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


$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', _('Overseas Purchase Order') );
$pdf->addInfo('Subject', _('PO') . ' ' . $_GET['opono']);
$FontSize = 12;
$line_height = 12;// Recommended: $line_height = $x * $FontSize.
		
		include('includes/PDFOPOHeader.inc');



		
			$SQL1 = "SELECT *
			FROM overseas_purchase_orders_details 
			WHERE id='" . $opono . "'
			";
	
			$result1 = DB_query($SQL1);
$totalamount =0;
			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/PDFOPOHeader.inc');
		} //end if need a new page headed up

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

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

		$LeftOvers = $pdf->addText($Left_Margin, $YPos+$FontSize, $FontSize,$myrow1['erp_stockid']);
		$LeftOvers = $pdf->addText(145, $YPos+$FontSize, $FontSize, $myrow1['supplier_stockid']);
	   
				$LeftOvers = $pdf->addTextWrap(245, $YPos,500,$FontSize,$myrow1['description'],'left');
		$LeftOvers = $pdf->addTextWrap(540, $YPos,50,$FontSize,$qty,'left');
				$LeftOvers = $pdf->addTextWrap(600, $YPos,70,$FontSize,$stup,'left');
				$LeftOvers = $pdf->addTextWrap(640, $YPos,85,$FontSize,$myrow1['gl_code'],'left');
		$LeftOvers = $pdf->addTextWrap(700, $YPos,85,$FontSize,$myrow1['shipment_mode'],'left');
		$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, $stovm,'right');
			
			
				$totalamount = $totalamount + $ovamount;
			// 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/PDFOPOHeader.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/PDFOPOHeader.inc');
	} //end if need a new page headed up

				
				
				
		}	
		

		
			
		$totalamount1 =locale_number_format($totalamount,2);
		
		$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'),'right');
	$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos-7, 90, $FontSize,_('$').$totalamount1, '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($$totalamount1,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;
	//$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+310, $YPos+$FontSize-80, $FontSize, _('Kindly acknowledge once this purchase order is received accurately.'));
	$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, $myrow['notes']);
	        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-200,780, $FontSize, _('Authorised  By: Shaukh Khan - Head of Finance'));

//$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/PDFOPOHeader.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->OutputD('OverseasPurchaseOrder_Evergreen_' . $opono. '.pdf');
		
//	$pdf->OutputD($_SESSION['DatabaseName'] . '_PurchaseOrder_' . $OrderNo . '_' . date('Y-m-d') . '.pdf');
	$pdf->__destruct();
		
	
	
	
	

			
		
			
		
			
		

		
		
			    
		
	


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

?>
