<?php
/*
Copyright (c) 2010, Till Brehm, projektfarm Gmbh
All rights reserved.
*/

require_once('../../lib/config.inc.php');
require_once('../../lib/app.inc.php');

//* Check permissions for module
//$app->auth->check_module_permissions('billing');

$invoice_id = intval($_GET['id']);

if($app->auth->is_admin()) {
	$invoice = $app->db->queryOneRecord("SELECT * FROM invoice WHERE invoice_id = $invoice_id");
} else {
	$group_rec = $app->db->queryOneRecord('SELECT client_id FROM sys_group WHERE groupid = '.$_SESSION['s']['user']['default_group']);
	$client_id = $group_rec['client_id'];
	$invoice = $app->db->queryOneRecord("SELECT * FROM invoice WHERE invoice_id = $invoice_id AND client_id = $client_id");
}

if(is_array($invoice) && $invoice['invoice_number'] != '') {
	
	$app->uses('billing_functions');
	$invoice_filename = $app->billing_functions->get_invoice_filename($invoice['invoice_number']);
	$path_parts = pathinfo($invoice_filename);

	if(is_file($invoice_filename)) {
		header('Content-type: application/pdf');
		header('Content-Disposition: attachment; filename="'.$path_parts['basename'].'"');
		readfile($invoice_filename);
	} else {
		die('No invoice PDF file found.');
	}
} else {
	die('Invoice does not exist or you do not have permissions to access this invoice.');
}



?>