www/includes/db_inventory_equipments.php

Thu, 12 Oct 2023 14:19:46 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 12 Oct 2023 14:19:46 +0200
changeset 849
16079aef4c4c
parent 842
897bf2a43253
permissions
-rw-r--r--

Version 0.3.44. Moved iSpindel Plato calculation from the php script to bmsd. This uses calibration data in the mon_ispindels table. Setup of this data will be done by the bmsapp applications. Default settings are stored in MySQL. From now on you don't need to store calibration data in the iSpindel.

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

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

if (isset($_POST['insert']) || isset($_POST['update'])) {
	if (isset($_POST['insert'])) {
		// INSERT COMMAND
		$sql  = "INSERT INTO `inventory_equipments` SET ";
	}
	if (isset($_POST['update'])) {
		// UPDATE COMMAND
		$sql  = "UPDATE `inventory_equipments` 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 .= "', boil_size='" . $_POST['boil_size'];
	$sql .= "', batch_size='" . $_POST['batch_size'];
	$sql .= "', tun_volume='" . $_POST['tun_volume'];
	$sql .= "', tun_weight='" . $_POST['tun_weight'];
	$sql .= "', tun_specific_heat='" .  $_POST['tun_specific_heat'];
	$sql .= "', tun_material='" . $_POST['tun_material'];
	$sql .= "', tun_height='" . floatval($_POST['tun_height']) / 100.0;
	$sql .= "', top_up_water='" . $_POST['top_up_water'];
	$sql .= "', trub_loss='" . $_POST['trub_loss'];
	$sql .= "', evap_rate='" . $_POST['evap_rate'];
	$sql .= "', boil_time='" . $_POST['boil_time'];
	($_POST['calc_boil_volume'] == 'true') ? $sql .= "', calc_boil_volume='1" : $sql .= "', calc_boil_volume='0";
	$sql .= "', top_up_kettle='" . $_POST['top_up_kettle'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']);
	$sql .= "', lauter_volume='" . $_POST['lauter_volume'];
	$sql .= "', lauter_height='" . floatval($_POST['lauter_height']) / 100.0;
	$sql .= "', lauter_deadspace='" . $_POST['lauter_deadspace'];
	$sql .= "', kettle_volume='" . $_POST['kettle_volume'];
	$sql .= "', kettle_height='" . floatval($_POST['kettle_height']) / 100.0;
	$sql .= "', mash_volume='" . $_POST['mash_volume'];
	$sql .= "', mash_max='" . $_POST['mash_max'];
	$sql .= "', efficiency='" . $_POST['efficiency'];
	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_equipment: ".$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. check if this record is in use.
	$sql = "SELECT name FROM `inventory_equipments` WHERE record='".$_POST['record']."';";
	$result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect));
	if (! $result) {
                syslog(LOG_NOTICE, "db_inventory_equipment: ".$sql." result: ".mysqli_error($connect));
                $response['error'] = true;
		$response['msg'] = "SQL fout: ".mysqli_error($connect);
		exit(json_encode($response));
	}
	if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$sql = "SELECT eq_name FROM products WHERE eq_name='" . $row['name'] . "';";
		$result2 = mysqli_query($connect, $sql);
		$ar = mysqli_affected_rows($connect);
		if ($ar > 0) {
			$response['error'] = true;
                	$response['msg'] = "Installatie wordt ".$ar." maal gebruikt.";
			exit(json_encode($response));
		}
	}
	// Record not used, delete it.
	$sql = "DELETE FROM `inventory_equipments` WHERE record='".$_POST['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_equipment: ".$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_equipments ORDER BY name";
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$equipments[] = array(
			'record' => $row['record'],
			'name' => $row['name'],
			'uuid' => $row['uuid'],
			'boil_size' => $row['boil_size'],
			'batch_size' => $row['batch_size'],
			'tun_volume' => $row['tun_volume'],
			'tun_weight' => $row['tun_weight'],
			'tun_specific_heat' => $row['tun_specific_heat'],
			'tun_material' => $row['tun_material'],
			'tun_height' => floatval($row['tun_height']) * 100.0,
			'top_up_water' => $row['top_up_water'],
			'trub_loss' => $row['trub_loss'],
			'evap_rate' => $row['evap_rate'],
			'boil_time' => $row['boil_time'],
			'calc_boil_volume' => $row['calc_boil_volume'],
			'top_up_kettle' => $row['top_up_kettle'],
			'notes' => $row['notes'],
			'lauter_volume' => $row['lauter_volume'],
			'lauter_height' => floatval($row['lauter_height']) * 100.0,
			'lauter_deadspace' => $row['lauter_deadspace'],
			'kettle_volume' => $row['kettle_volume'],
			'kettle_height' => floatval($row['kettle_height']) * 100.0,
			'mash_volume' => $row['mash_volume'],
			'mash_max' => $row['mash_max'],
			'efficiency' => $row['efficiency']
		);
	}
	header("Content-type: application/json");
	exit(json_encode($equipments));
}

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

?>

mercurial