roxen.lists.roxen.general

Subject Author Date
Appendix (generate_pdf.php) to: Fwd:_[roxen.com_#8493]_En_fråga_angående_PHP_i_roxen Magnus Andersson <snichme[at]gmail[dot]com> 26-11-2006
<?php
require('fpdf.php');

/* <Settings> */

$fontFamily = "Times";
$fontSize = 11;

$rowHeight = 5;

$momsSats = 0.2;  // 0.06, 0.12 0.2, 0.25,   



/* </settings> */

$f = isset($_GET['f']) ? $_GET['f'] : 1;

$myinfo[1] = array(
	"faktnr" => $_POST['nr'],
	"facDate" => $_POST['facDate'],
	"lastDate" => $_POST['expireDate'],
	"payment" => $_POST['payment'],
	"refer" => $_POST['refer'],
	"customer" => array(
		"name" => $_POST['name'],
		"adr" => $_POST['adress'],
		"city" => $_POST['postnr']." ".$_POST['city']
	),
	"car" => array(
		"rentalDate" => $_POST['rentalDate'], 
		"length" => $_POST['length'], //I kilometer
		"what" => "",
		"costperMile" => $_POST['cost'],
	)
);

$myinfo[2] = array(
	"faktnr" => 2006002,
	"facDate" => "2006-09-27",
	"lastDate" => "2006-10-12",
	"payment" => "15 dagar netto",
	"refer" => "Magnus Andersson",
	"customer" => array(
		"name" => "Magnus Andersson",
		"adr" => "Fridtungatan 19",
		"city" => "582 12 Linköping"
	),
	"car" => array(
		"rentalDate" => "2006-09-03", 
		"length" => 154, //I kilometer
		"what" => "Sektionsverksamhet",
		"costperMile" => 15,
	)
);
$what = array(15 => "Sektionsverksamhet", 20 => "Privat sektionsaktiv", 25 =>
"Privat medlem d-sektionen", 30 => "");
if(isset($_GET['generate'])) {
	$info = $myinfo[1];
	$info['car']['what'] = $what[$info['car']['costperMile']];
}
else
	die("Det saknas information för att kunna generera PDF filen!!");

/* Avrundar körd sträcka och returnerar det i mil. */
function round2mil($km) {
	$t = $km/10;
	return ceil($t);
}

$mil = round2mil($info['car']['length']);
$total_cost = round2mil($info['car']['length']) * $info['car']['costperMile'];
$moms = round($total_cost * 0.2, 0);



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

 * DONT ALTER ANYTHING UNDER THIS POINT
 * ÄNDRA INGET UNDER DETTA   

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

class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;

function PDF($orientation='P',$unit='mm',$format='A4')
{
	//Call parent constructor
	$this->FPDF($orientation,$unit,$format);
	//Initialization
	$this->B=0;
	$this->I=0;
	$this->U=0;
	$this->HREF='';
}

function WriteHTML($html)
{
	//HTML parser
	$html=str_replace("\n",' ',$html);
	$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
	foreach($a as $i=>$e)
	{
		if($i%2==0)
		{
			//Text
			if($this->HREF)
				$this->PutLink($this->HREF,$e);
			else
				$this->Write(5,$e);
		}
		else
		{
			//Tag
			if($e=='/')
				$this->CloseTag(strtoupper(substr($e,1)));
			else
			{
				//Extract attributes
				$a2=explode(' ',$e);
				$tag=strtoupper(array_shift($a2));
				$attr=array();
				foreach($a2 as $v)
					if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
						$attr[strtoupper($a3[1])]=$a3[2];
				$this->OpenTag($tag,$attr);
			}
		}
	}
}

function OpenTag($tag,$attr)
{
	//Opening tag
	if($tag=='B' or $tag=='I' or $tag=='U')
		$this->SetStyle($tag,true);
	if($tag=='A')
		$this->HREF=$attr['HREF'];
	if($tag=='BR')
		$this->Ln(5);
}

function CloseTag($tag)
{
	//Closing tag
	if($tag=='B' or $tag=='I' or $tag=='U')
		$this->SetStyle($tag,false);
	if($tag=='A')
		$this->HREF='';
}

function SetStyle($tag,$enable)
{
	//Modify style and select corresponding font
	$this->$tag+=($enable ? 1 : -1);
	$style='';
	foreach(array('B','I','U') as $s)
		if($this->$s>0)
			$style.=$s;
	$this->SetFont('',$style);
}

function PutLink($URL,$txt)
{
	//Put a hyperlink
	$this->SetTextColor(0,0,255);
	$this->SetStyle('U',true);
	$this->Write(5,$txt,$URL);
	$this->SetStyle('U',false);
	$this->SetTextColor(0);
}


}
$pdf=new PDF();

$pdf->SetAuthor("Webbutskottet D-sektionen");
$pdf->SetCreator("Magnus Andersson");
$pdf->SetTitle("Faktura Lån utav bil");

//First page
$pdf->AddPage();
$pdf->SetFont($fontFamily,'B',20);
$pdf->SetLeftMargin(15);

$pdf->Image('dit.jpg',10, 10,'','40','jpg','http://www.d.lintek.liu.se/');
$pdf->SetFont($fontFamily,'B',16);
$pdf->Text(120, 16, "Datateknologsektionen");

$pdf->SetFont($fontFamily,'B',12);
$pdf->Text(120, 30, "Faktura");

$pdf->SetFont($fontFamily,'', $fontSize);
$pdf->Text(120, 36, "Fakturanummer:");
$pdf->Text(155, 36, $info['faktnr']);

$pdf->Text(120, 42, "Fakturadatum:");
$pdf->Text(155, 42, $info['facDate']);

$pdf->Text(120, 48, "Förfallodatum:");
$pdf->Text(155, 48, $info['lastDate']);

$pdf->Text(120, 54, "Betalnigsvillkor:");
$pdf->Text(155, 54, $info['payment']);

$pdf->SetFont($fontFamily,'I', $fontSize);
$pdf->Text(120, 60, "Dröjsmålsränta debiteras enligt räntelagen");


$pdf->SetFont($fontFamily,'B', $fontSize);
$pdf->Text(20, 80, "Vår referens");
$pdf->Text(70, 80, "Er referens");
$pdf->Text(120, 80, "Kund");

$pdf->SetFont($fontFamily,'', $fontSize);
$pdf->Text(20, 85, "Eddie Kaltea");
$pdf->Text(70, 85, $info['refer']);
$pdf->Text(120, 85, $info['customer']['name']);
$pdf->Text(120, 90, $info['customer']['adr']);
$pdf->Text(120, 95, $info['customer']['city']);


/*
Creating the table
*/
$pdf->setY(100);
$pdf->setX(20);


$pdf->SetDrawColor(0,0,0); //Set the color of the border
$pdf->SetLineWidth(.3);	   //Set the width of the border


//Header
$pdf->SetFillColor(220,220,220); 
$pdf->SetFont('','B');
$pdf->Cell(90, 7, "Specifikation", 1, 0, 'L', 1);
$pdf->Cell(70, 7, "Belopp (inkl. moms)", 1, 0, 'R', 1);
$pdf->Ln();

//Color and font restoration
$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('');

/*

Här är frågan om man ska loopa ut det eller om det kommer vara dessa rader och
inget mera.
Här ska det dynamiska innehållet in oxå!!!

Kolla upp om det ska kanske dit flera rader!!!!!!!

*/

//Data
$data = array(
	array("Hyra för sektionsbil ".$info['car']['rentalDate'], ""),
	array("Sträcka:	".$info['car']['length']." km (".$mil." mil)", ""),
	array("Taxa:	".$info['car']['costperMile']." kr/mil ".($info['car']['what'] ?
"(".$info['car']['what'].")" : ""), ""),
	array("Total kostnad", $total_cost.",00 kr"),
	array("", ""),
	array("Obs: Fakturering sker per påbörjad mil enligt avtal", "")
);
$fill=0;
foreach($data as $row) {
	//$fill = $row[1] ? 1 : 0; //Uncomment this is you want the line with a price
to be highlighted
	$pdf->setX(20);

	$pdf->Cell(90, $rowHeight+1,$row[0],'LR',0,'L',$fill);
	$pdf->Cell(70, $rowHeight+1,$row[1],'LR',0,'R',$fill);
	$pdf->Ln();
	//$fill=!$fill; //uncomment this if you want to have the rows colored every
other time.
}

$pdf->setX(20);
$pdf->SetFillColor(220,220,220); 
$pdf->SetFont('','B');
$pdf->Cell(90, $rowHeight, "Öresavrundning", 'TLR', 0, 'L', 1);
$pdf->Cell(70, $rowHeight, "0,00 kr", 'TLR', 0, 'R', 1);
$pdf->Ln();
$pdf->setX(20);
$pdf->Cell(90, $rowHeight, "Moms (25%)", 'LR', 0, 'L', 1);
$pdf->Cell(70, $rowHeight, $moms.",00 kr", 'LR', 0, 'R', 1);
$pdf->Ln();
$pdf->setX(20);
$pdf->Cell(90, $rowHeight, "Summa", 'LR', 0, 'L', 1);
$pdf->Cell(70, $rowHeight, $total_cost.",00 kr", 'LR', 0, 'R', 1);
$pdf->Ln();
$pdf->setX(20);
$pdf->Cell(160,0,'','T');

$pdf->Line(20, 255, 180, 255);
$pdf->Ln();


$pdf->SetY(-42);
$pdf->setX(20);

$pdf->SetFont('','B');
$pdf->Cell(60, $rowHeight, "Postadress", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "Organisationsnummer", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "Telefonnr", 0, 0, 'L', 0);
$pdf->Ln();

$pdf->SetFont('');
$pdf->setX(20);
$pdf->Cell(60, $rowHeight, "Datateknologsektionen", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "822002-1409", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "013-31 07 78", 0, 0, 'L', 0);
$pdf->Ln();
$pdf->setX(20);
$pdf->Cell(60, $rowHeight, "Kårallen, Universitetet", 0, 0, 'L', 0);
$pdf->SetFont('', 'B');
$pdf->Cell(60, $rowHeight, "BankGiro-nr", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "Hemsida", 0, 0, 'L', 0);
$pdf->SetFont('');
$pdf->Ln();
$pdf->setX(20);
$pdf->Cell(60, $rowHeight, "581 83 Linköping", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "5644-8251", 0, 0, 'L', 0);
$pdf->Cell(60, $rowHeight, "www.d.lintek.liu.se", 0, 0, 'L', 0);


/* Print out the whole PDF document.*/
$pdf->Output();
?>