www/rec_print.php

Mon, 12 Nov 2018 11:32:16 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 12 Nov 2018 11:32:16 +0100
changeset 88
0a39cbdcf085
parent 87
7f1d0abe5571
child 89
082c23b26055
permissions
-rw-r--r--

Added ABV clculation.

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


$link = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME);
if (! $link) {
	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
if (! mysqli_set_charset($link, "utf8" )) {
	echo "error";
	return 1;
}

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);

$total_fermentables = 0.0;
$cost_fermentables = 0.0;
$total_hops = 0.0;
$cost_hops = 0.0;
$cost_yeasts = 0.0;
$cost_miscs = 0.0;
$pSugar = 0;
$pCara = 0;
$svg = 77;


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

	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(255,150,100);
		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) {
		/* 2 Columns */
		$vul = $this->w - $this->rMargin - $this->lMargin - 170;
		$cMargin=$this->cMargin;
		$this->cMargin=2;
		$this->TableX=$this->lMargin;
		$this->SetFont('Helvetica','B',9);
		$this->SetX($this->TableX);
		$this->SetFillColor(255,150,100);
		$this->Cell(0, 5, 'Recept overzicht',0,0,'C',true);
		$this->Ln();

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor(210,245,255);

		$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(10);

		$this->cMargin=$cMargin;
	}

	function TableFermentables($row) {
		global $cost_fermentables;
		global $total_fermentables;
		global $pSugar;
		global $pCara;
		$vul = $this->w - $this->rMargin - $this->lMargin - 132;
		$this->AddCol($vul,'Grondstof','L');
		$this->AddCol(30,'Leverancier','L');
		$this->AddCol(15,'EBC','R');
		$this->AddCol(17,'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=2;
		$this->TableX=$this->lMargin;
		$this->TableHeader();
		$this->ProcessingTable=true;

		$sugf = 0;
		$this->SetFont('Helvetica','',9);
		$this->SetFillColor(250, 195, 65);
		$arr = json_decode($row['json_fermentables'], true);
		foreach($arr as $item) { //foreach element in $arr
			$name     = iconv('UTF-8','windows-1252',$item['f_name']);
			$supplier = iconv('UTF-8','windows-1252',$item['f_supplier']);
			$added    = iconv('UTF-8','windows-1252',$item['f_added']);
			$type     = iconv('UTF-8','windows-1252',$item['f_type']);
			$graintype     = iconv('UTF-8','windows-1252',$item['f_graintype']);
			$amount   = floatval($item['f_amount']);
			$costkg   = floatval($item['f_cost']);
			$yield    = floatval($item['f_yield']);
			$moisture = floatval($item['f_moisture']);
			$color    = floatval($item['f_color']);
			$percent  = floatval($item['f_percentage']);

			if ($type == "Sugar")
				$pSugar += $percent;
			if ($graintype == "Crystal")
				$pCara += $percent;

			$cost = $amount * $costkg;
			$cost_fermentables += $cost;
			$total_fermentables += $amount;
			/* Calculate the amount of sugars */
			$d = $amount * ($yield / 100) * (1 - $moisture / 100);
			if ($added == "Mash")
				$d = floatval($row['efficiency']) / 100 * $d;
			$sugf += $d;

			$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(17,5,added_type_names($added),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();
		}

		$row['est_og'] = estimate_og($sugf, floatval($row['batch_size']));
		$this->SetFillColor(210,245,255);
		$this->Cell($vul+62,5,'',0,0,'L',false);
		$this->Cell(20,5,sprintf("%8.3f",$total_fermentables),0,0,'R',true);
		$this->Cell(30,5,'',0,0,'L',false);
		$this->Cell(20,5,sprintf("%8.3f",$cost_fermentables).EURO,0,0,'R',true);
		$this->Ln(10);
		$this->ProcessingTable=false;
		$this->cMargin=$cMargin;
		$this->aCols=array();
	}

	function TableHops($row) {
		global $total_hops;
		global $cost_hops;
		$vul = $this->w - $this->rMargin - $this->lMargin - 135;
		$this->AddCol($vul,'Hop','L');
		$this->AddCol(35,'Land','L');
		$this->AddCol(15,'Soort','L');
		$this->AddCol(20,'Moment','L');
		$this->AddCol(15,'Alpha','R');
		$this->AddCol(12,'IBU','R');
		$this->AddCol(18,'Gram','R');
		$this->AddCol(20,'Prijs','R');

		$cMargin=$this->cMargin;
		$this->cMargin=2;
		$this->TableX=$this->lMargin;
		$this->TableHeader();
		$this->ProcessingTable=true;

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor(100, 250, 65);

		$arr = json_decode($row['json_hops'], true);
		foreach($arr as $item) { //foreach element in $arr
			$name     = iconv('UTF-8','windows-1252',$item['h_name']);
			$origin   = iconv('UTF-8','windows-1252',$item['h_origin']);
			$useat    = iconv('UTF-8','windows-1252',$item['h_useat']);
			$form     = iconv('UTF-8','windows-1252',$item['h_form']);
			$amount   = floatval($item['h_amount']) * 1000;
			$mass     = $amount;
			$costkg   = floatval($item['h_cost']);
			$time     = floatval($item['h_time']);
			$alpha    = floatval($item['h_alpha']);

			$cost = ($amount * $costkg) / 1000;
			$cost_hops += $cost;
			$total_hops += $amount;

			$fmoment  = 1.0;
			$moment = hop_useat_names($useat);
			if (($useat == "Dry Hop") || ($useat == "Dry hop")) {
				$fmoment = 0.0;
			} else if ($useat == "Whirlpool") {
				$fmoment = 0.0;
			} else if ($useat == "Mash") {
				$fmoment = 0.7;		// Brouwhulp
			} else if (($useat == "First Wort") || ($useat == "First wort")) {
				$fmoment = 1.1;		// Brouwhulp, Louis, Ozzie
			} else if ($useat == "Aroma") {
				$fmoment = 0.0;		// Brouwhulp 1.0 redeneert vlamuit.
			} else if ($useat == "Boil") {
				$fmoment  = 1.0;
				$moment   = "Kook@".$time;
			}

			$pfactor  = 1.0;
			if ($form == "Pellet") {
				$pfactor = 1.1;
			}

			/* Tinseth only for now */
			$AddedAlphaAcids = (($alpha / 100) * $mass * 1000) / floatval($row['batch_size']);
			$Bigness_factor = 1.65 * pow( 0.000125, floatval($row['est_og']) - 1);
			$BoilTime_factor = ((1 - exp(-0.04 * $time)) / 4.15);
			$utiisation = $Bigness_factor * $BoilTime_factor;
			$ibu = (round($utiisation * $AddedAlphaAcids * $fmoment * $pfactor * 10) / 10.0);

			$this->Cell($vul,5,$name,0,0,'L',true);
			$this->Cell(35,5,$origin,0,0,'L',true);
			$this->Cell(15,5,hop_form_names($form),0,0,'L',true);
			$this->Cell(20,5,$moment,0,0,'L',true);
			$this->Cell(15,5,sprintf("%.1f%%",$alpha),0,0,'R',true);
			$this->Cell(12,5,sprintf("%.1f",$ibu),0,0,'R',true);
			$this->Cell(18,5,sprintf("%.1f",$amount),0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.3f",$cost).EURO,0,0,'R',true);
			$this->Ln();
		}

		$this->SetFillColor(210,245,255);
		$this->Cell($vul+97,5,'',0,0,'L',false);
		$this->Cell(18,5,sprintf("%.1f",$total_hops),0,0,'R',true);
		$this->Cell(20,5,sprintf("%8.3f",$cost_hops).EURO,0,0,'R',true);
		$this->Ln(10);
		$this->ProcessingTable=false;
		$this->cMargin=$cMargin;
		$this->aCols=array();
	}

	function TableYeasts($row) {
		global $cost_yeasts;
		global $svg;
		$vul = $this->w - $this->rMargin - $this->lMargin - 140;
		$this->AddCol(27,'Laboratorium','L');
		$this->AddCol(18,'Product','L');
		$this->AddCol($vul,'Gist','L');
		$this->AddCol(20,'Gebruik','L');
		$this->AddCol(15,'Attn','R');
		$this->AddCol(22,'Vorm','L');
		$this->AddCol(18,'Gram','R');
		$this->AddCol(20,'Prijs','R');

		$cMargin=$this->cMargin;
		$this->cMargin=2;
		$this->TableX=$this->lMargin;
		$this->TableHeader();
		$this->ProcessingTable=true;

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor(175, 175, 255);

		$arr = json_decode($row['json_yeasts'], true);
		foreach($arr as $item) { //foreach element in $arr
			$name       = iconv('UTF-8','windows-1252',$item['y_name']);
			$laboratory = iconv('UTF-8','windows-1252',$item['y_laboratory']);
			$product_id = iconv('UTF-8','windows-1252',$item['y_product_id']);
			$form       = iconv('UTF-8','windows-1252',$item['y_form']);
			$use        = iconv('UTF-8','windows-1252',$item['y_use']);
			$amount     = floatval($item['y_amount']) * 1000;
			$costg      = floatval($item['y_cost']);
			$attenuation = floatval($item['y_attenuation']);
			$cost        = $amount * $costg;
			$cost_yeasts += $cost;

			if ($use == "Primary")
				$svg = $attenuation;

			$this->Cell(27,5,$laboratory,0,0,'L',true);
			$this->Cell(18,5,$product_id,0,0,'L',true);
			$this->Cell($vul,5,$name,0,0,'L',true);
			$this->Cell(20,5,yeast_use_names($use),0,0,'L',true);
			$this->Cell(15,5,sprintf("%.1f%%",$attenuation),0,0,'R',true);
			$this->Cell(22,5,yeast_form_names($form),0,0,'L',true);
			$this->Cell(18,5,sprintf("%.1f",$amount),0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.3f",$cost).EURO,0,0,'R',true);
			$this->Ln();
		}

		$this->SetFillColor(210,245,255);
		$this->Cell($vul+120,5,'',0,0,'L',false);
		$this->Cell(20,5,sprintf("%8.3f",$cost_yeasts).EURO,0,0,'R',true);
		$this->Ln(10);
		$this->ProcessingTable=false;
		$this->cMargin=$cMargin;
		$this->aCols=array();
	}

	function TableMiscs($row) {
		global $cost_miscs;
		$vul = $this->w - $this->rMargin - $this->lMargin - 95;
		$this->AddCol($vul,'Naam','L');
		$this->AddCol(30,'Soort','L');
		$this->AddCol(25,'Gebruik','L');
		$this->AddCol(20,'Hoeveel','R');
		$this->AddCol(20,'Prijs','R');

		$cMargin=$this->cMargin;
		$this->cMargin=2;
		$this->TableX=$this->lMargin;
		$this->TableHeader();
		$this->ProcessingTable=true;

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor(175, 175, 255);

		$arr = json_decode($row['json_miscs'], true);
		foreach($arr as $item) {
			$name       = iconv('UTF-8','windows-1252',$item['m_name']);
			$type       = iconv('UTF-8','windows-1252',$item['m_type']);
			$use_use    = iconv('UTF-8','windows-1252',$item['m_use_use']);
			$amount     = floatval($item['m_amount']) * 1000;
			$costg      = floatval($item['m_cost']) / 1000;
			$time       = floatval($item['m_time']);
			$aiw        = $item['m_amount_is_weight'];
			$cost       = $amount * $costg;
			$cost_miscs += $cost;

			if ($type == "Water agent")
				$this->SetFillColor(240,140,130);
			else if ($type == "Fining")
				$this->SetFillColor(95,180,25);
			else if (($type == "Spice") || ($type == "Herb") || ($type == "Flavor") || ($type == "Yeast nutrient"))
				$this->SetFillColor(240,250,65);
			else
				$this->SetFillColor(210,245,255);

			if ($use_use == "Boil")
				$gebruik = sprintf("%s %d min",misc_use_names($use_use),$time);
			else
				$gebruik = misc_use_names($use_use);

			$hoeveel = sprintf("%.1f %s",$amount,$aiw ? "gr":"ml");
			$this->Cell($vul,5,$name,0,0,'L',true);
			$this->Cell(30,5,misc_type_names($type),0,0,'L',true);
			$this->Cell(25,5,$gebruik,0,0,'L',true);
			$this->Cell(20,5,$hoeveel,0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.3f",$cost).EURO,0,0,'R',true);
			$this->Ln();
		}

		$this->SetFillColor(210,245,255);
		$this->Cell($vul+75,5,'',0,0,'L',false);
		$this->Cell(20,5,sprintf("%8.3f",$cost_miscs).EURO,0,0,'R',true);
		$this->Ln(10);

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

	// Watercolor $this->SetFillColor(120,255,250);

	function TableSummary($row) {
		global $pSugar;
		global $pCara;
		global $svg;
		/* 2 Columns */
		$vul = $this->w - $this->rMargin - $this->lMargin - 170;
		$cMargin=$this->cMargin;
		$this->cMargin=2;
		$this->TableX=$this->lMargin;
		$this->SetFont('Helvetica','B',9);
		$this->SetX($this->TableX);
		$this->SetFillColor(255,150,100);
		$this->Cell(0, 5, 'Recept samenvatting',0,0,'C',true);
		$this->Ln();

		$this->SetFont('Helvetica','',9);
		$this->SetFillColor(210,245,255);

		$row['est_fg'] = estimate_fg($pSugar, $pCara, 0, 0, 0, $svg, $row['est_og']);

		$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();

		$row['est_abv'] = abvol($row['est_og'], $row['est_fg']);
		$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);
		$beercolor = ebc_to_color($row['est_color']);
		$this->SetFillColor($beercolor[0],$beercolor[1],$beercolor[2]);
		$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->SetFillColor(210,245,255);
		$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(10);
		// Kostprijs per liter, calorieren
		$this->cMargin=$cMargin;
	}
}



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();
$pdf->TableGlobal($row);
$pdf->TableFermentables($row);
$pdf->TableHops($row);
$pdf->TableYeasts($row);
$pdf->TableMiscs($row);
$pdf->TableSummary($row);
if (strlen($row['notes'])) {
	$pdf->SetFillColor(255,150,100);
	$pdf->Cell(0, 5, 'Recept opmerkingen',0,0,'C',true);
	$pdf->Ln();
	$pdf->SetFillColor(210,245,255);
	$pdf->MultiCell(0,4,$row['notes'],0,'L',true);
}
// Disclaimer toevoegen dat dit alleen recept ontwikkeling is. Uitkomsten zoals IBU zijn een berekende schatting.
// Footer sectie met berekende/geschatte uitkomsten zoals ABV, kostprijs per liter enz. 
$pdf->Output();

mercurial