www/rec_print.php

Wed, 07 Nov 2018 22:52:39 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 07 Nov 2018 22:52:39 +0100
changeset 80
75b9227fb98c
child 83
85521c6e0022
permissions
-rw-r--r--

Added beginning of recipe print.

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/global.inc.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/fpdf/fpdf.php');

define('EURO', chr(128) );

$link = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME);
//mysqli_set_charset($link, "utf8" );
setlocale ( LC_ALL, 'nl_NL.UTF-8');
$record = $_GET["record"];
$result = mysqli_query($link, "SELECT * FROM recipes WHERE record='".$record."'");
$row = mysqli_fetch_array($result);


class PDF_MySQL_Table extends FPDF
{
	protected $ProcessingTable=false;
	protected $aCols=array();
	protected $TableX;
	protected $HeaderColor;
	protected $RowColors;
	protected $TotalColor;
	protected $ColorIndex;

	function Header() {
		// Print the table header if necessary
		if ($this->ProcessingTable)
			$this->TableHeader();
	}

	function TableHeader() {
		$this->SetFont('Helvetica','B',9);
		$this->SetX($this->TableX);
		$this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
		foreach($this->aCols as $col)
			$this->Cell($col['w'],5,$col['c'],0,0,$col['a'],true);
		$this->Ln();
	}

	function AddCol($width=-1, $caption='', $align='L') {
		$this->aCols[]=array('c'=>$caption,'w'=>$width,'a'=>$align);
	}

	function TableGlobal($row,$prop) {
		/* 2 Columns */
		$vul = $this->w - $this->rMargin - $this->lMargin - 170;
		$cMargin=$this->cMargin;
		$this->cMargin=$prop['padding'];
		$this->TableX=$this->lMargin;
		$this->HeaderColor=$prop['HeaderColor'];
		$this->TotalColor=$prop['TotalColor'];
		$this->RowColors=array($prop['color1'],$prop['color2']);
		$this->SetFont('Helvetica','B',11);
		$this->SetX($this->TableX);
		$this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
		$this->Cell(0, 6, 'Recept overzicht',0,0,'C',true);
		$this->Ln();

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor($this->RowColors[0][0],$this->RowColors[0][1],$this->RowColors[0][2]);

		$this->SetX($this->TableX);
		$this->Cell(35,5,'Type',0,0,'L',true);
		$this->Cell(25,5,$row['type'],0,0,'L',true);
		$this->Cell(25,5,'',0,0,'L',true);
		$this->Cell($vul,5,'',0,0,'L',false);
		$this->Cell(35,5,'Batch grootte',0,0,'L',true);
		$this->Cell(25,5,$row['batch_size'].' liter',0,0,'L',true);
		$this->Cell(25,5,'',0,0,'L',true);
		$this->Ln();

		$this->SetX($this->TableX);
		$this->Cell(35,5,'Kooktijd',0,0,'L',true);
		$this->Cell(25,5,$row['boil_time'].' minuten',0,0,'L',true);
		$this->Cell(25,5,'',0,0,'L',true);
		$this->Cell($vul,5,'',0,0,'L',false);
		$this->Cell(35,5,'Brouwzaal rendement',0,0,'L',true);
		$this->Cell(25,5,$row['efficiency'].' %',0,0,'L',true);
		$this->Cell(25,5,'',0,0,'L',true);
		$this->Ln();

		$this->SetX($this->TableX);
		$this->Cell(35,5,'Start SG',0,0,'L',true);
		$this->Cell(25,5,sprintf("%.3f",$row['est_og']),0,0,'L',true);
		$this->Cell(25,5,'('.sprintf("%.3f",$row['st_og_min']).' - '.sprintf("%.3f",$row['st_og_max']).')',0,0,'L',true);
		$this->Cell($vul,5,'',0,0,'L',false);
		$this->Cell(35,5,'Eind SG',0,0,'L',true);
		$this->Cell(25,5,sprintf("%.3f",$row['est_fg']),0,0,'L',true);
		$this->Cell(25,5,'('.sprintf("%.3f",$row['st_fg_min']).' - '.sprintf("%.3f",$row['st_fg_max']).')',0,0,'L',true);
		$this->Ln();

		$this->SetX($this->TableX);
		$this->Cell(35,5,'Geschat alcohol',0,0,'L',true);
		$this->Cell(25,5,sprintf("%.1f",$row['est_abv']).'%',0,0,'L',true);
		$this->Cell(25,5,'('.sprintf("%.1f",$row['st_abv_min']).' - '.sprintf("%.1f",$row['st_abv_max']).'%)',0,0,'L',true);
		$this->Cell($vul,5,'',0,0,'L',false);
		$this->Cell(35,5,'CO2',0,0,'L',true);
		$this->Cell(25,5,sprintf("%.1f",$row['est_carb']).'%',0,0,'L',true);
		$this->Cell(25,5,'('.sprintf("%.1f",$row['st_carb_min']).' - '.sprintf("%.1f",$row['st_carb_max']).'%)',0,0,'L',true);
		$this->Ln();

		$this->SetX($this->TableX);
		$this->Cell(35,5,'Kleur ('.$row['color_method'].')',0,0,'L',true);
		$this->Cell(25,5,$row['est_color'].' EBC',0,0,'L',true);
		$this->Cell(25,5,'('.$row['st_color_min'].' - '.$row['st_color_max'].' EBC)',0,0,'L',true);
		$this->Cell($vul,5,'',0,0,'L',false);
		$this->Cell(35,5,'Bitterheid ('.$row['ibu_method'].')',0,0,'L',true);
		$this->Cell(25,5,sprintf("%.0f",$row['est_ibu']).' IBU',0,0,'L',true);
		$this->Cell(25,5,'('.$row['st_ibu_min'].' - '.$row['st_ibu_max'].' IBU)',0,0,'L',true);
		$this->Ln();

		$this->cMargin=$cMargin;
	}

	function TableFermentables($row,$prop) {
		 $vul = $this->w - $this->rMargin - $this->lMargin - 130;
		$this->AddCol($vul,'Grondstof','L');
		$this->AddCol( 30,'Leverancier','L');
		$this->AddCol( 15,'EBC','R');
		$this->AddCol( 15,'Moment','L');
		$this->AddCol( 20,'Kg','R');
		$this->AddCol( 15,'Procent','R');
		$this->AddCol( 15,'Opbr.','R');
		$this->AddCol( 20,'Prijs','R');

		$cMargin=$this->cMargin;
		$this->cMargin=$prop['padding'];
		$this->TableX=$this->lMargin;
		$this->TableHeader();
		$this->ColorIndex=0;
		$this->ProcessingTable=true;

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor($this->RowColors[0][0],$this->RowColors[0][1],$this->RowColors[0][2]);

		$arr = json_decode($row['json_fermentables'], true);
		foreach($arr as $item) { //foreach element in $arr
			$name     = $item['f_name'];
			$supplier = $item['f_supplier'];
			$amount   = floatval($item['f_amount']);
			$costkg   = floatval($item['f_cost']);
			$yield    = floatval($item['f_yield']);
			$color    = floatval($item['f_color']);
			$percent  = floatval($item['f_percentage']);
			if ($item['f_added'] == "Boil")
				$add = "Koken";
			else if ($item['f_added'] == "Mash")
				$add = "Maisch";
			else
				$add = $item['f_added'];
			$cost = $amount * $costkg;
			$this->Cell($vul,5,$name,0,0,'L',true);
			$this->Cell(30,5,$supplier,0,0,'L',true);
			$this->Cell(15,5,sprintf("%.0f",$color),0,0,'R',true);
			$this->Cell(15,5,$add,0,0,'L',true);
			$this->Cell(20,5,sprintf("%8.3f",$amount),0,0,'R',true);
			$this->Cell(15,5,sprintf("%.1f%%",$percent),0,0,'R',true);
			$this->Cell(15,5,sprintf("%.1f%%",$yield),0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.3f",$cost).EURO,0,0,'R',true);
			$this->Ln();
		}

		$this->ProcessingTable=false;
		$this->cMargin=$cMargin;
		$this->aCols=array();
	}
}



class PDF extends PDF_MySQL_Table {
	function Header() {
		global $row;
		$this->Image('images/logo.png',10,6,30);
		// Title
		$this->SetFont('Helvetica','B',18);
		$this->SetX(45);
		$this->Cell(0,8,$row['name'],1,1,'L');
		$this->Ln(20);
		// Ensure table header is printed
		parent::Header();
	}
}


/*
 * Generate PDF from recipe data
 */
$pdf = new PDF();
$pdf->AddPage();
$prop = array('HeaderColor'=>array(255,150,100), 'color1'=>array(210,245,255),
	                'color2'=>array(255,255,210), 'TotalColor'=>array(255,150,100), 'padding'=>2);
$pdf->TableGlobal($row,$prop);
$pdf->Ln(10);
$pdf->TableFermentables($row,$prop);

//$pdf->SetFont('Helvetica','',10);
//$pdf->Cell(0,10,'Receptje '.$record,0,1,'C');

$pdf->Output();

mercurial