www/inv_instock.php

Tue, 31 Aug 2021 20:48:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 31 Aug 2021 20:48:37 +0200
changeset 774
92e1e8f175a2
parent 514
3c680d1dea35
permissions
-rw-r--r--

Split batch, adjust mash step volume. In the duplicated log_brew handle the missing values. In save product, round the mash step sg to 4 decimals. In prod_edit, ingredients are stored as strings, not arrays. This triggered a memory corruption that only happened in rare circumstances. Don't fix mash step fields in the javascript, it is already done during load from the database. Calculation of the mash volume is rounded to 6 decimals. Enter mash step Brix/Plato value, the SG result is rounded to 4 decimals.

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

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

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

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 CalcWidths($width, $align) {
		// Compute the widths of the columns
		$TableWidth=0;
		foreach($this->aCols as $i=>$col) {
			$w=$col['w'];
			if($w==-1)
				$w=$width/count($this->aCols);
			elseif(substr($w,-1)=='%')
				$w=$w/100*$width;
			$this->aCols[$i]['w']=$w;
			$TableWidth+=$w;
		}
		// Compute the abscissa of the table
		if($align=='C')
			$this->TableX=max(($this->w-$TableWidth)/2,0);
		elseif($align=='R')
			$this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
		else
			$this->TableX=$this->lMargin;
	}

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

	function TableFermentables($link,$prop) {
		global $fermentabletype;
		$this->AddCol( 26,'Type','L');
		$this->AddCol( 26,'Leverancier','L');
		$this->AddCol( 68,'Vergistbaar ingredient','L');
		$this->AddCol( 30,'Voorraad','R');
		$this->AddCol( 20, 'Prijs/kg', 'R');
		$this->AddCol( 20, 'Waarde', 'R');
		$prop['width']=$this->w-$this->lMargin-$this->rMargin;
		$prop['align']='L';
		$cMargin=$this->cMargin;
		$this->cMargin=$prop['padding'];
		$this->HeaderColor=$prop['HeaderColor'];
		$this->TotalColor=$prop['TotalColor'];
		$this->RowColors=array($prop['color1'],$prop['color2']);
		$this->CalcWidths($prop['width'],$prop['align']);
		$this->TableHeader();
		$this->SetFont('Helvetica','',9);
		$this->ColorIndex=0;
		$this->ProcessingTable=true;

		$sql = "SELECT type,name,supplier,inventory,cost FROM inventory_fermentables WHERE inventory > 0 ORDER BY type,supplier,name";
		$result = mysqli_query($link, $sql);
		$tot_fermentables = 0.0;

		while($row=mysqli_fetch_array($result)) {
			$value = $row['inventory'] * $row['cost'];
			$tot_fermentables += $value;

			$this->SetX($this->TableX);
			$ci=$this->ColorIndex;
			$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
			$this->Cell(26,5,$fermentabletype[$row['type']],0,0,'L',true);
			$this->Cell(26,5,iconv('UTF-8','windows-1252',$row['supplier']),0,0,'L',true);
			$this->Cell(68,5,iconv('UTF-8','windows-1252',$row['name']),0,0,'L',true);
			$this->Cell(30,5,sprintf("%10.3f kg",$row['inventory']),0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$row['cost']).EURO,0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$value).EURO,0,0,'R',true);
			$this->Ln();
			$this->ColorIndex=1-$ci;
		}

		$this->SetX($this->TableX);
		$this->SetFillColor($this->TotalColor[0],$this->TotalColor[1],$this->TotalColor[2]);
		$this->Cell(170,5,'Totaal',0,0,'L',true);
		$this->Cell(20,5,sprintf("%8.2f ",$tot_fermentables).EURO,0,0,'R',true);
		$this->Ln();

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

	function TableHops($link,$prop) {
		global $hopform;
		$this->AddCol( 30,'Land','L');
		$this->AddCol( 75,'Hoppen','L');
		$this->AddCol( 15,'Soort','L');
		$this->AddCol( 30,'Voorraad','R');
		$this->AddCol( 20, 'Prijs/kg', 'R');
		$this->AddCol( 20, 'Waarde', 'R');
		$prop['width']=$this->w-$this->lMargin-$this->rMargin;
		$prop['align']='L';
		$cMargin=$this->cMargin;
		$this->cMargin=$prop['padding'];
		$this->CalcWidths($prop['width'],$prop['align']);
		$this->TableHeader();
		$this->SetFont('Helvetica','',9);
		$this->ColorIndex=0;
		$this->ProcessingTable=true;

		$result = mysqli_query($link, "SELECT name,form,origin,inventory,cost FROM inventory_hops WHERE inventory > 0 ORDER BY origin,name");
		$tot_hops = 0.0;

		while($row=mysqli_fetch_array($result)) {
			$value = $row['inventory'] * $row['cost'];
			$tot_hops += $value;
			$stock = floatval($row['inventory']) * 1000.0;

			$this->SetX($this->TableX);
			$ci=$this->ColorIndex;
			$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
			$this->Cell(30,5,iconv('UTF-8','windows-1252',$row['origin']),0,0,'L',true);
			$this->Cell(75,5,iconv('UTF-8','windows-1252',$row['name']),0,0,'L',true);
			$this->Cell(15,5,$hopform[$row['form']],0,0,'L',true);
			$this->Cell(30,5,sprintf("%10.1f gr",$stock),0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$row['cost']).EURO,0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$value).EURO,0,0,'R',true);
			$this->Ln();
			$this->ColorIndex=1-$ci;
		}

		$this->SetX($this->TableX);
		$this->SetFillColor($this->TotalColor[0],$this->TotalColor[1],$this->TotalColor[2]);
		$this->Cell(170,5,'Totaal',0,0,'L',true);
		$this->Cell(20,5,sprintf("%8.2f ",$tot_hops).EURO,0,0,'R',true);
		$this->Ln();

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

	function TableYeasts($link,$prop) {
		$this->AddCol( 30,'Laboratorium','L');
		$this->AddCol( 20,'Product','L');
		$this->AddCol( 70,'Gist','L');
		$this->AddCol( 30,'Voorraad','R');
		$this->AddCol( 20, 'Prijs/kg', 'R');
		$this->AddCol( 20, 'Waarde', 'R');
		$prop['width']=$this->w-$this->lMargin-$this->rMargin;
		$prop['align']='L';
		$cMargin=$this->cMargin;
		$this->cMargin=$prop['padding'];
		$this->CalcWidths($prop['width'],$prop['align']);
		$this->TableHeader();
		$this->SetFont('Helvetica','',9);
		$this->ColorIndex=0;
		$this->ProcessingTable=true;

		$sql = "SELECT name,laboratory,product_id,form,inventory,cost FROM inventory_yeasts WHERE inventory > 0 ORDER BY laboratory,product_id";
		$result = mysqli_query($link, $sql);
		$tot_yeasts = 0.0;

		while($row=mysqli_fetch_array($result)) {
			$value = $row['inventory'] * $row['cost'];
			$tot_yeasts += $value;
			if ($row['form'] == 0)
				$stock = floatval($row['inventory']);
			else
				$stock = floatval($row['inventory']) * 1000.0;
			$form = array( 'pak', 'gr', 'ml', 'ml', 'ml', 'ml' );

			$this->SetX($this->TableX);
			$ci=$this->ColorIndex;
			$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
			$this->Cell(30,5,iconv('UTF-8','windows-1252',$row['laboratory']),0,0,'L',true);
			$this->Cell(20,5,iconv('UTF-8','windows-1252',$row['product_id']),0,0,'L',true);
			$this->Cell(70,5,iconv('UTF-8','windows-1252',$row['name']),0,0,'L',true);
			$this->Cell(30,5,sprintf("%10.1f ",$stock).$form[$row['form']],0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$row['cost']).EURO,0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$value).EURO,0,0,'R',true);
			$this->Ln();
			$this->ColorIndex=1-$ci;
		}

		$this->SetX($this->TableX);
		$this->SetFillColor($this->TotalColor[0],$this->TotalColor[1],$this->TotalColor[2]);
		$this->Cell(170,5,'Totaal',0,0,'L',true);
		$this->Cell(20,5,sprintf("%8.2f ",$tot_yeasts).EURO,0,0,'R',true);
		$this->Ln();

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

	function TableMiscs($link,$prop) {
		global $misctype;
		$this->AddCol( 30,'Type','L');
		$this->AddCol( 90,'Ingredient','L');
		$this->AddCol( 30,'Voorraad','R');
		$this->AddCol( 20, 'Prijs/kg', 'R');
		$this->AddCol( 20, 'Waarde', 'R');
		$prop['width']=$this->w-$this->lMargin-$this->rMargin;
		$prop['align']='L';
		$cMargin=$this->cMargin;
		$this->cMargin=$prop['padding'];
		$this->CalcWidths($prop['width'],$prop['align']);
		$this->TableHeader();
		$this->SetFont('Helvetica','',9);
		$this->ColorIndex=0;
		$this->ProcessingTable=true;

		$result = mysqli_query($link, "SELECT name,type,amount_is_weight,inventory,cost FROM inventory_miscs WHERE inventory > 0 ORDER BY type,name");
		$tot_miscs = 0.0;

		while($row=mysqli_fetch_array($result)) {
			$value = $row['inventory'] * $row['cost'];
			$tot_miscs += $value;
			$stock = floatval($row['inventory']) * 1000.0;
			($row['amount_is_weight']) ? $amount = "gr" : $amount = "ml";

			$this->SetX($this->TableX);
			$ci=$this->ColorIndex;
			$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
			$this->Cell(30,5,$misctype[$row['type']],0,0,'L',true);
			$this->Cell(90,5,iconv('UTF-8','windows-1252',$row['name']),0,0,'L',true);
			$this->Cell(30,5,sprintf("%10.1f ",$stock).$amount,0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$row['cost']).EURO,0,0,'R',true);
			$this->Cell(20,5,sprintf("%8.2f ",$value).EURO,0,0,'R',true);
			$this->Ln();
			$this->ColorIndex=1-$ci;
		}

		$this->SetX($this->TableX);
		$this->SetFillColor($this->TotalColor[0],$this->TotalColor[1],$this->TotalColor[2]);
		$this->Cell(170,5,'Totaal',0,0,'L',true);
		$this->Cell(20,5,sprintf("%8.2f ",$tot_miscs).EURO,0,0,'R',true);
		$this->Ln();

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



class PDF extends PDF_MySQL_Table {
	function Header() {
		$this->Image('images/logo.png',10,6,30);
		// Title
		$this->SetFont('Helvetica','',18);
		$this->Cell(0,10,'Inventaris',0,1,'C');
		$this->Ln(20);
		// Ensure table header is printed
		parent::Header();
	}
}


$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->TableFermentables($link,$prop);
$pdf->Ln(10);
$pdf->TableHops($link,$prop);
$pdf->AddPage();
$pdf->TableYeasts($link,$prop);
$pdf->Ln(10);
$pdf->TableMiscs($link,$prop);
$pdf->Output();

mercurial