www/includes/db_inventory_fermentables.php

Wed, 30 Jan 2019 16:40:23 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 30 Jan 2019 16:40:23 +0100
changeset 221
a8aabb63fbcc
parent 195
2ac491548d8d
child 296
69fadd1aded2
permissions
-rw-r--r--

Added two missing fields in getfermentablesources.php. When getting a single recipe from the database, update the fermentables json data with available inventory and ingredient. Added inventory column in the fermentable grid. Added ingredient selection in the fermentable popup window to replace ingredient.

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");
require($_SERVER['DOCUMENT_ROOT']."/version.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" );

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

	$sql .= "name='" . mysqli_real_escape_string($connect, $_POST['name']);
	$sql .= "', type='" . $_POST['type'];
	$sql .= "', yield='" . $_POST['yield'];
	$sql .= "', color='" . $_POST['color'];
	($_POST['add_after_boil'] == 'true') ? $sql .= "', add_after_boil='1" : $sql .= "', add_after_boil='0";
	$sql .= "', origin='" . mysqli_real_escape_string($connect, $_POST['origin']);
	$sql .= "', supplier='" . mysqli_real_escape_string($connect, $_POST['supplier']);
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']);
	$sql .= "', coarse_fine_diff='" . $_POST['coarse_fine_diff'];
	$sql .= "', moisture='" . $_POST['moisture'];
	$sql .= "', diastatic_power='" . $_POST['diastatic_power'];
	$sql .= "', protein='" . $_POST['protein'];
	$sql .= "', dissolved_protein='" . $_POST['dissolved_protein'];
	$sql .= "', max_in_batch='" . $_POST['max_in_batch'];
	($_POST['recommend_mash'] == 'true') ? $sql .= "', recommend_mash='1" : $sql .= "', recommend_mash='0";
	$sql .= "', added='" . $_POST['added'];
	($_POST['always_on_stock'] == 'true') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
	$sql .= "', di_ph='" . $_POST['di_ph'];
	$sql .= "', acid_to_ph_57='" . $_POST['acid_to_ph_57'];
	$sql .= "', graintype='" . $_POST['graintype'];
	$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'] . "';";
	}
	syslog(LOG_NOTICE, $sql);

	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_fermentables: ".$sql." result: ".mysqli_error($connect));
	} else {
		if (isset($_POST['update'])) {
			syslog(LOG_NOTICE, "db_inventory_fermentables: updated record ".$_POST['record']);
		} else {
			$lastid = mysqli_insert_id($connect);
			syslog(LOG_NOTICE, "db_inventory_fermentables: inserted record ".$lastid);
		}
	}
	echo $result;

} else if (isset($_POST['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `inventory_fermentables` WHERE record='".$_POST['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_fermentables: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_fermentables: deleted record ".$_POST['record']);
	}
	echo $result;

} else {
	// SELECT COMMAND
	$query = "SELECT * FROM inventory_fermentables ORDER BY supplier,name";
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$fermentables[] = array(
			'record' => $row['record'],
			'name' => $row['name'],
			'type' => $row['type'],
			'yield' => $row['yield'],
			'color' => $row['color'],
			'add_after_boil' => $row['add_after_boil'],
			'origin' => $row['origin'],
			'supplier' => $row['supplier'],
			'notes' => $row['notes'],
			'coarse_fine_diff' => $row['coarse_fine_diff'],
			'moisture' => $row['moisture'],
			'diastatic_power' => $row['diastatic_power'],
			'protein' => $row['protein'],
			'dissolved_protein' => $row['dissolved_protein'],
			'max_in_batch' => $row['max_in_batch'],
			'recommend_mash' => $row['recommend_mash'],
			'added' => $row['added'],
			'always_on_stock' => $row['always_on_stock'],
			'di_ph' => $row['di_ph'],
			'acid_to_ph_57' => $row['acid_to_ph_57'],
			'graintype' => $row['graintype'],
			'inventory' => $row['inventory'],
			'cost' => $row['cost'],
			'production_date' => $row['production_date'],
			'tht_date' => $row['tht_date']
		);
	}
	echo json_encode($fermentables);
}
?>

mercurial