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

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

	$userid = $_SESSION['UserID'];

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


$SQL = "SELECT *
	               
			FROM supptrans
			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['trandate']));
$yeardate = date('Y',strtotime($myrow['entered_date']));
$monthdate =date('M',strtotime($myrow['entered_date']));
//$departmentid =$myrow['department'];
$supplierinvoicepaid = explode(", ", $myrow['suppinv_paid']);


$paymentamount =$myrow['ovamount']+ $myrow['ovgst'];
$paymenttype =$myrow['payment_method'];

$sql40b =  "SELECT *
			FROM bankaccounts
			
			WHERE oldglcode= '" . $myrow['bank'] . "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result40b = DB_query($sql40b,$ErrMsg);
	$myrowdb =DB_fetch_array($result40b);
$bankname = $myrowdb['bank_name'];


$deliveryaddress = $myrow['delivery_address']; 
$currency =$myrow['currency'];
$quoteno = $myrow['quote_no'];
$bankglcode =$myrow['bank'];
//get gl and department in multiline

			


		$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,
							 taxref,
							 paymentterms
							 
			FROM suppliers
			WHERE supplierid ='" . $myrow['supplierno'] . "'
			";
	 
	$result100 = DB_query($SQL100);
	
	$myrow100 = DB_fetch_array($result100) ;

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

$tin = $myrow100['taxref'];

$address1 = $myrow100['address1'];
		$address2 = $myrow100['address2'];
		$address3 = $myrow100['address3'];
		$address4 = $myrow100['address4'];
		$address5 = $myrow100['address5'];
		$address6=  $myrow100['address6'];
		
		

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


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


$ListCount ++;
				

		$YPos -= $line_height;// In
	$LeftOvers = $pdf->addTextWrap(200, $YPos,100,$FontSize,_('Creditors Payment GL:'),'left');
		
			$SQL1 = "SELECT *
			FROM gltrans
			WHERE typeno='" . $paymentno . "'
			AND type = 22
			";
	
			$result1 = DB_query($SQL1);
$totalamount =0;
			while ($myrow1 = DB_fetch_array($result1)) { 
		
		
		//todays date.This is to show today quote was generated
	
			$SQL1gl = "SELECT name
			FROM oldglcode
			WHERE oldglcode='" . $myrow1['account']. "'
			";
	
			$result1gl = DB_query($SQL1gl);
		$myrow1gl = DB_fetch_array($result1gl);
		
		//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.
				

	
			
			
						
			
	
				
	
								//$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['narrative'],'left');
		$LeftOvers = $pdf->addTextWrap(245, $YPos,100,$FontSize,$myrow1gl['name'],'left');
			//$LeftOvers = $pdf->addTextWrap(600, $YPos,70,$FontSize,$myrow1gl['name'],'left');
				$LeftOvers = $pdf->addTextWrap(640, $YPos,85,$FontSize,$myrow1['account'],'left');
		//$LeftOvers = $pdf->addTextWrap(700, $YPos,85,$FontSize,$myrow1['shipment_mode'],'left');
		$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, locale_number_format($myrow1['amount'],2),'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 = 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/PDFPaymentVoucherHeader.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/PDFPaymentVoucherHeader.inc');
	} //end if need a new page headed up

				
				
				
		}	
		$ListCount ++;
				

		$YPos -= $line_height;// In
$pdf->SetFont('Freeserif', 'B', 10,' ' , 'false');
	$LeftOvers = $pdf->addTextWrap(200, $YPos,250,$FontSize,_('Below All Creditors Invoice GL that was paid from above:'),'left');
$ListCount ++;
$YPos -= $line_height;
// Iterate over the array and display each record
foreach ($supplierinvoicepaid as $record) {
	
	//get suppreference
	
		$SQL111 = "SELECT trandate,suppreference,lpo_no
			FROM supptrans
			WHERE transno='" . $record . "'
			AND type = 20
			
			";
	
			$result111 = DB_query($SQL111);
	$myrow111 = DB_fetch_array($result111);
	//get 1
	
	$pdf->SetFont('Freeserif', 'B', 10,' ' , 'false');
				$LeftOvers = $pdf->addTextWrap(200, $YPos,750,$FontSize,_('Supp Invoice Reference: ').$myrow111['suppreference']._(' ERP Inv NO:  ').$record._(' ERP Inv Date:  ').$myrow111['trandate']._(' LPO:  ').$myrow111['lpo_no'],'left');
						
			$pdf->SetFont('Freeserif', '', 10,' ' , 'false');
			  // $ListCount ++;
	
	$SQL1 = "SELECT account, SUM(amount) AS total_amount
			FROM gltrans
			WHERE typeno='" . $record . "'
			AND type = 20
			GROUP BY account
			";
	
			$result1 = DB_query($SQL1);
$totalamount =0;
			while ($myrow1 = DB_fetch_array($result1)) { 
		
		
		//todays date.This is to show today quote was generated
	
			$SQL1gl = "SELECT name
			FROM oldglcode
			WHERE oldglcode='" . $myrow1['account']. "'
			";
	
			$result1gl = DB_query($SQL1gl);
		$myrow1gl = DB_fetch_array($result1gl);
		
		//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.
				

	
			
			
								//$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['narrative'],'left');
		$LeftOvers = $pdf->addTextWrap(245, $YPos,100,$FontSize,$myrow1gl['name'],'left');
			//$LeftOvers = $pdf->addTextWrap(600, $YPos,70,$FontSize,$myrow1gl['name'],'left');
				$LeftOvers = $pdf->addTextWrap(640, $YPos,85,$FontSize,$myrow1['account'],'left');
		//$LeftOvers = $pdf->addTextWrap(700, $YPos,85,$FontSize,$myrow1['shipment_mode'],'left');
		$LeftOvers = $pdf->addTextWrap($Page_Width-$Right_Margin-90, $YPos, 90, $FontSize, locale_number_format($myrow1['total_amount'],2),'right');
			
			
				$totalamount = $totalamount + $ovamount;
			
		//**********
		$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/PDFPaymentVoucherHeader.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/PDFPaymentVoucherHeader.inc');
	} //end if need a new page headed up

				
				
				
		}	
	
	
			   $ListCount ++;
				

		$YPos -= $line_height;// Increment a line down for the next line item.
				
	
}
	
			
		$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,_('$').-$paymentamount, '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, $YPos+$FontSize-65, $FontSize, _('Notes').':');

	$Width2 = $Page_Width-$Right_Margin-140;// Width to print salesorders.comments.
    $LeftOvers = $pdf->addTextWrap($XPos, $YPos+$FontSize-85,780, $FontSize, $myrow['suppreference']);
	        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/PDFPaymentVoucherHeader.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('CreditorsPaymentVoucher_Evergreen_' . $paymentno. '.pdf');
		
//	$pdf->OutputD($_SESSION['DatabaseName'] . '_PurchaseOrder_' . $OrderNo . '_' . date('Y-m-d') . '.pdf');
	$pdf->__destruct();
		
	
	
	
	

			
		
			
		
			
		

		
		
			    
		
	


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

?>
