www/inv_instock.php

Tue, 06 Nov 2018 22:55:55 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 06 Nov 2018 22:55:55 +0100
changeset 77
a9f8de2d7b2b
parent 33
2ee6ad5d6f14
child 78
f35a38e8c16f
permissions
-rw-r--r--

Fixed most charset problems. Added fpdf library. Added inventory pdf creation.

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

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) {
		$this->AddCol(100,'Vergistbaar ingredient','L');
		$this->AddCol( 28,'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;

		$result = mysqli_query($link, "SELECT name,inventory,cost FROM inventory_fermentables WHERE inventory > 0");
		$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(100,5,$row['name'],0,0,'L',true);
			$this->Cell(28,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(148,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) {
		$this->AddCol(100,'Hoppen','L');
		$this->AddCol( 28,'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");
		$tot_hops = 0.0;

		while($row=mysqli_fetch_array($result)) {
			$value = $row['inventory'] * $row['cost'];
			$tot_hops += $value;
			$hop = $row['form'] . " " . $row['origin'] . " - " . $row['name'];
			$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(100,5,$hop,0,0,'L',true);
			$this->Cell(28,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(148,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( 28,'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,laboratory,product_id,form,inventory,cost FROM inventory_yeasts WHERE inventory > 0");
		$tot_yeasts = 0.0;

		while($row=mysqli_fetch_array($result)) {
			$value = $row['inventory'] * $row['cost'];
			$tot_yeasts += $value;
			$stock = floatval($row['inventory']) * 1000.0;
			($row['form'] == 'Dry') ? $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,$row['laboratory'],0,0,'L',true);
			$this->Cell(20,5,$row['product_id'],0,0,'L',true);
			$this->Cell(70,5,$row['name'],0,0,'L',true);
			$this->Cell(28,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(168,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) {
		$this->AddCol( 30,'Type','L');
		$this->AddCol( 70,'Ingredient','L');
		$this->AddCol( 28,'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");
		$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,$row['type'],0,0,'L',true);
			$this->Cell(70,5,$row['name'],0,0,'L',true);
			$this->Cell(28,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(168,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() {
		// Title
		$this->SetFont('Helvetica','',18);
		$this->Cell(0,6,'Inventaris',0,1,'C');
		$this->Ln(10);
		// 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