www/includes/db_inventory_equipments.php

Sun, 04 Jul 2021 12:16:25 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 04 Jul 2021 12:16:25 +0200
changeset 766
86289b0c4a27
parent 715
8fb922c00a2d
child 767
08c0343b622b
permissions
-rw-r--r--

Version 0.3.38. Better error handling in inventory equipments. Check if equipment is in use before delete.

<?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 ";
	}
	$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_chiller_loss='" . $_POST['trub_chiller_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 .= "', hop_utilization='" . $_POST['hop_utilization'];
	$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($link);
	}
	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($link);
		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($link);
	}
	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'],
			'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_chiller_loss' => $row['trub_chiller_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'],
			'hop_utilization' => $row['hop_utilization'],
			'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