www/includes/db_inventory_yeasts.php

Thu, 10 Feb 2022 22:15:10 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 10 Feb 2022 22:15:10 +0100
changeset 785
aa79acfdf8a9
parent 767
08c0343b622b
permissions
-rw-r--r--

Added uuid field in inventory equipments, fermentables, hops, miscs, suppliers, water and yeasts. Added uuid field in profiles mash, styles and water.

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");
require($_SERVER['DOCUMENT_ROOT']."/version.php");
require($_SERVER['DOCUMENT_ROOT']."/includes/constants.php");

#Connect to the database
$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $connect) {
	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
mysqli_set_charset($connect, "utf8" );

$response = array(
   'error' => false,
   'msg' => 'Ok',
);

// get data and store in a json array
if (isset($_POST['insert']) || isset($_POST['update'])) {
	if (isset($_POST['insert'])) {
		$sql  = "INSERT INTO `inventory_yeasts` SET ";
	}
	if (isset($_POST['update'])) {
		$sql  = "UPDATE `inventory_yeasts` SET ";
	}

	if (isset($_POST['uuid']) && (strlen($_POST['uuid']) == 36)) {
                $sql .= "uuid='" . $_POST['uuid'];
        } else {
                $uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid'));
                $sql .= "uuid='" . $uuid;
        }
	$sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']);
	$sql .= "', type='" . array_search($_POST['type'], $yeasttype);
	$sql .= "', form='" . array_search($_POST['form'], $yeastform);
	$sql .= "', laboratory='" . mysqli_real_escape_string($connect, $_POST['laboratory']);
	$sql .= "', product_id='" . mysqli_real_escape_string($connect, $_POST['product_id']);
	$sql .= "', short_desc='" . mysqli_real_escape_string($connect, $_POST['short_desc']);
	$sql .= "', min_temperature='" . $_POST['min_temperature'];
	$sql .= "', max_temperature='" . $_POST['max_temperature'];
	$sql .= "', flocculation='" . $_POST['flocculation'];
	$sql .= "', attenuation='" . $_POST['attenuation'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']);
	$sql .= "', best_for='" . mysqli_real_escape_string($connect, $_POST['best_for']);
	$sql .= "', max_reuse='" . $_POST['max_reuse'];
	$sql .= "', cells='" . floatval($_POST['cells']) * 1000000000.0;
	$sql .= "', tolerance='" . $_POST['tolerance'];
	($_POST['sta1'] == 'true') ? $sql .= "', sta1='1" : $sql .= "', sta1='0";
	($_POST['bacteria'] == 'true') ? $sql .= "', bacteria='1" : $sql .= "', bacteria='0";
	($_POST['harvest_top'] == 'true') ? $sql .= "', harvest_top='1" : $sql .= "', harvest_top='0";
	$sql .= "', harvest_time='" . $_POST['harvest_time'];
	$sql .= "', pitch_temperature='" . floatval($_POST['pitch_temperature']);
	($_POST['pofpos'] == 'true') ? $sql .= "', pofpos='1" : $sql .= "', pofpos='0";
	$sql .= "', zymocide='" . $_POST['zymocide'];
	$sql .= "', gr_hl_lo='" . $_POST['gr_hl_lo'];
	$sql .= "', sg_lo='" . floatval($_POST['sg_lo']);
	$sql .= "', gr_hl_hi='" . $_POST['gr_hl_hi'];
	$sql .= "', sg_hi='" . floatval($_POST['sg_hi']);
	$sql .= "', inventory='" . $_POST['inventory'];
	$sql .= "', cost='" . $_POST['cost'] . "'";
	if ($_POST['production_date'] == '')
		$sql .= ", production_date=NULL";
	else
		$sql .= ", production_date='" . $_POST['production_date'] . "'";
	if ($_POST['tht_date'] == '')
		$sql .= ", tht_date=NULL";
	else
		$sql .= ", tht_date='" . $_POST['tht_date'] . "'";
	if (isset($_POST['insert'])) {
		$sql .= ";";
	}
	if (isset($_POST['update'])) {
		$sql .= " WHERE record='" . $_POST['record'] . "';";
	}
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_yeasts: ".$sql." result: ".mysqli_error($connect));
                $response['error'] = true;
                $response['msg'] = "SQL fout: ".mysqli_error($connect);
        }
        exit(json_encode($response));

} else if (isset($_POST['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `inventory_yeasts` WHERE record='".$_POST['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_yeasts: ".$sql." result: ".mysqli_error($connect));
                $response['error'] = true;
                $response['msg'] = "SQL fout: ".mysqli_error($connect);
        }
        exit(json_encode($response));

} else {
	// SELECT COMMAND
	$query = "SELECT * FROM inventory_yeasts ORDER BY laboratory,product_id,name";
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$yeasts[] = array(
			'record' => $row['record'],
			'name' => $row['name'],
			'uuid' => $row['uuid'],
			'type' => $yeasttype[$row['type']],
			'form' => $yeastform[$row['form']],
			'laboratory' => $row['laboratory'],
			'product_id' => $row['product_id'],
			'short_desc' => $row['short_desc'],
			'min_temperature' => $row['min_temperature'],
			'max_temperature' => $row['max_temperature'],
			'flocculation' => $row['flocculation'],
			'attenuation' => $row['attenuation'],
			'notes' => $row['notes'],
			'best_for' => $row['best_for'],
			'max_reuse' => $row['max_reuse'],
			'inventory' => $row['inventory'],
			'cost' => $row['cost'],
			'production_date' => $row['production_date'],
			'tht_date' => $row['tht_date'],
			'cells' => floatval($row['cells']) / 1000000000.0,
			'tolerance' => $row['tolerance'],
			'sta1' => $row['sta1'],
			'bacteria' => $row['bacteria'],
			'harvest_top' => $row['harvest_top'],
			'harvest_time' => $row['harvest_time'],
			'pitch_temperature' => floatval($row['pitch_temperature']),
			'pofpos' => $row['pofpos'],
			'zymocide' => $row['zymocide'],
			'gr_hl_lo' => $row['gr_hl_lo'],
			'sg_lo' => floatval($row['sg_lo']),
			'gr_hl_hi' => $row['gr_hl_hi'],
			'sg_hi' => floatval($row['sg_hi'])
		);
	}
	header("Content-type: application/json");
	exit(json_encode($yeasts));
}

syslog(LOG_NOTICE, "db_inventory_yeasts: missing arguments");
$response['error'] = true;
$response['msg'] = "missing arguments";
echo json_encode($response);

?>

mercurial