www/includes/db_product.php

Mon, 04 Feb 2019 15:21:49 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 04 Feb 2019 15:21:49 +0100
changeset 240
a85c1d4c4d12
parent 238
8e7384587a10
child 241
e95d2886f49f
permissions
-rw-r--r--

Depending on the product stage, more or less fields are saved in the database. Added begin of auto stage increase. Added missing inventory columns in the fermentables grid. New product wizzard now works with the indexed values. Product print works with indexed values and prepared for different print modules depending on the product stage.

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

$escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
$replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
$rescapers = array("'");
$rreplacements = array("\\'");
$disallowed = array('visibleindex','uniqueid','boundindex','uid','h_weight','m_weight');

if (isset($_POST['insert']) || isset($_POST['update'])) {
	if (isset($_POST['insert'])) {
		$sql  = "INSERT INTO `products` SET ";
	}
	if (isset($_POST['update'])) {
		$sql  = "UPDATE `products` SET ";
	}

	$stage = $_POST['stage'];

	// Basic settings
	if (isset($_POST['uuid'])) {
		$sql .= "uuid='" . $_POST['uuid'];
		syslog(LOG_NOTICE, 'Keep uuid ');
	} else {
		$uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid'));
		$sql .= "uuid='" . $uuid;
		syslog(LOG_NOTICE, 'New uuid ');
	}
	$sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']);
	$sql .= "', code='" . mysqli_real_escape_string($connect, $_POST['code']);
	$sql .= "', birth='" . $_POST['birth'];
	$sql .= "', stage='" . $_POST['stage'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']);
	($_POST['log_brew'] == 'true') ? $sql .= "', log_brew='1" : $sql .= "', log_brew='0";
	($_POST['log_fermentation'] == 'true') ? $sql .= "', log_fermentation='1" : $sql .= "', log_fermentation='0";
	($_POST['inventory_reduced'] == 'true') ? $sql .= "', inventory_reduced='1" : $sql .= "', inventory_reduced='0";
	($_POST['locked'] == 'true') ? $sql .= "', locked='1" : $sql .= "', locked='0";
	// Equipment
	$sql .= "', eq_name='" . mysqli_real_escape_string($connect, $_POST['eq_name']);
	$sql .= "', eq_boil_size='" . $_POST['eq_boil_size'];
	$sql .= "', eq_batch_size='" . $_POST['eq_batch_size'];
	$sql .= "', eq_tun_volume='" . $_POST['eq_tun_volume'];
	$sql .= "', eq_tun_weight='" . $_POST['eq_tun_weight'];
	$sql .= "', eq_tun_specific_heat='" . $_POST['eq_tun_specific_heat'];
	$sql .= "', eq_tun_material='" . $_POST['eq_tun_material'];
	$sql .= "', eq_tun_height='" . $_POST['eq_tun_height'];
	$sql .= "', eq_top_up_water='" . $_POST['eq_top_up_water'];
	$sql .= "', eq_trub_chiller_loss='" . $_POST['eq_trub_chiller_loss'];
	$sql .= "', eq_evap_rate='" . $_POST['eq_evap_rate'];
	$sql .= "', eq_boil_time='" . $_POST['eq_boil_time'];
	$sql .= "', eq_calc_boil_volume='" . $_POST['eq_calc_boil_volume'];
	$sql .= "', eq_top_up_kettle='" . $_POST['eq_top_up_kettle'];
	$sql .= "', eq_hop_utilization='" . $_POST['eq_hop_utilization'];
	$sql .= "', eq_notes='" . mysqli_real_escape_string($connect, $_POST['eq_notes']);
	$sql .= "', eq_lauter_volume='" . $_POST['eq_lauter_volume'];
	$sql .= "', eq_lauter_height='" . $_POST['eq_lauter_height'];
	$sql .= "', eq_lauter_deadspace='" . $_POST['eq_lauter_deadspace'];
	$sql .= "', eq_kettle_volume='" . $_POST['eq_kettle_volume'];
	$sql .= "', eq_kettle_height='" . $_POST['eq_kettle_height'];
	$sql .= "', eq_mash_volume='" . $_POST['eq_mash_volume'];
	$sql .= "', eq_mash_max='" . $_POST['eq_mash_max'];
	$sql .= "', eq_efficiency='" . $_POST['eq_efficiency'];

	if ($stage > 1) {
		$sql .= "', brew_date_start='" . $_POST['brew_date_start'];
		$sql .= "', brew_mash_ph='" . $_POST['brew_mash_ph'];
		$sql .= "', brew_mash_sg='" . $_POST['brew_mash_sg'];
		$sql .= "', brew_sparge_temperature='" . $_POST['brew_sparge_temperature'];
		$sql .= "', brew_sparge_volume='" . $_POST['brew_sparge_volume'];
		$sql .= "', brew_sparge_ph='" . $_POST['brew_sparge_ph'];
		$sql .= "', brew_preboil_volume='" . $_POST['brew_preboil_volume'];
		$sql .= "', brew_preboil_sg='" . $_POST['brew_preboil_sg'];
		$sql .= "', brew_preboil_ph='" . $_POST['brew_preboil_ph'];
		$sql .= "', brew_aboil_volume='" . $_POST['brew_aboil_volume'];
		$sql .= "', brew_aboil_sg='" . $_POST['brew_aboil_sg'];
		$sql .= "', brew_aboil_ph='" . $_POST['brew_aboil_ph'];
		$sql .= "', brew_aboil_efficiency='" . $_POST['brew_aboil_efficiency'];
		$sql .= "', brew_cooling_method='" . $_POST['brew_cooling_method'];
		$sql .= "', brew_cooling_time='" . $_POST['brew_cooling_time'];
		$sql .= "', brew_cooling_to='" . $_POST['brew_cooling_to'];
		$sql .= "', brew_whirlpool9='" . $_POST['brew_whirlpool9'];
		$sql .= "', brew_whirlpool7='" . $_POST['brew_whirlpool7'];
		$sql .= "', brew_whirlpool6='" . $_POST['brew_whirlpool6'];
		$sql .= "', brew_whirlpool2='" . $_POST['brew_whirlpool2'];
		$sql .= "', brew_aeration_time='" . $_POST['brew_aeration_time'];
		$sql .= "', brew_aeration_speed='" . $_POST['brew_aeration_speed'];
		$sql .= "', brew_aeration_type='" . $_POST['brew_aeration_type'];
		$sql .= "', brew_fermenter_volume='" . $_POST['brew_fermenter_volume'];
		$sql .= "', brew_fermenter_extrawater='" . $_POST['brew_fermenter_extrawater'];
		$sql .= "', brew_fermenter_sg='" . $_POST['brew_fermenter_sg'];
		$sql .= "', brew_fermenter_ibu='" . $_POST['brew_fermenter_ibu'];
		$sql .= "', brew_date_end='" . $_POST['brew_date_end'];
		($_POST['brew_log_available'] == 'true') ? $sql .= "', brew_log_available='1" : $sql .= "', brew_log_available='0";
	}
	$sql .= "', og='" . $_POST['og'];
	$sql .= "', fg='" . $_POST['fg'];

	if ($stage > 2) {
		$sql .= "', primary_start_temp='" . $_POST['primary_start_temp'];
		$sql .= "', primary_max_temp='" . $_POST['primary_max_temp'];
		$sql .= "', primary_end_temp='" . $_POST['primary_end_temp'];
		$sql .= "', primary_end_sg='" . $_POST['primary_end_sg'];
		$sql .= "', primary_end_date='" . $_POST['primary_end_date'];
	}

	if ($stage > 3) {
		$sql .= "', secondary_temp='" . $_POST['secondary_temp'];
		$sql .= "', secondary_end_date='" . $_POST['secondary_end_date'];
		$sql .= "', tertiary_temp='" . $_POST['tertiary_temp'];
	}

	if ($stage > 4) {
		$sql .= "', package_date='" . $_POST['package_date'];
		$sql .= "', bottle_amount='" . $_POST['bottle_amount'];
		$sql .= "', bottle_carbonation='" . $_POST['bottle_carbonation'];
		$sql .= "', bottle_priming_sugar='" . $_POST['bottle_priming_sugar'];
		$sql .= "', bottle_priming_amount='" . $_POST['bottle_priming_amount'];
		$sql .= "', bottle_carbonation_temp='" . $_POST['bottle_carbonation_temp'];
		$sql .= "', keg_amount='" . $_POST['keg_amount'];
		$sql .= "', keg_carbonation='" . $_POST['keg_carbonation'];
		$sql .= "', keg_priming_sugar='" . $_POST['keg_priming_sugar'];
		$sql .= "', keg_priming_amount='" . $_POST['keg_priming_amount'];
		$sql .= "', keg_carbonation_temp='" . $_POST['keg_carbonation_temp'];
		($_POST['keg_forced_carb'] == 'true') ? $sql .= "', keg_forced_carb='1" : $sql .= "', keg_forced_carb='0";
		$sql .= "', keg_pressure='" . $_POST['keg_pressure'];
	}

	if ($stage > 8) {
		$sql .= "', taste_notes='" . mysqli_real_escape_string($connect, $_POST['taste_notes']);
		$sql .= "', taste_rate='" . $_POST['taste_rate'];
		$sql .= "', taste_date='" . $_POST['taste_date'];
		$sql .= "', taste_color='" . mysqli_real_escape_string($connect, $_POST['taste_color']);
		$sql .= "', taste_transparency='" . mysqli_real_escape_string($connect, $_POST['taste_transparency']);
		$sql .= "', taste_head='" . mysqli_real_escape_string($connect, $_POST['taste_head']);
		$sql .= "', taste_aroma='" . mysqli_real_escape_string($connect, $_POST['taste_aroma']);
		$sql .= "', taste_taste='" . mysqli_real_escape_string($connect, $_POST['taste_taste']);
		$sql .= "', taste_mouthfeel='" . mysqli_real_escape_string($connect, $_POST['taste_mouthfeel']);
		$sql .= "', taste_aftertaste='" . mysqli_real_escape_string($connect, $_POST['taste_aftertaste']);
	}

	/*
	 * Recipe part
	 */
	$sql .= "', st_name='" . mysqli_real_escape_string($connect, $_POST['st_name']);
	$sql .= "', st_letter='" . mysqli_real_escape_string($connect, $_POST['st_letter']);
	$sql .= "', st_guide='" . mysqli_real_escape_string($connect, $_POST['st_guide']);
	$sql .= "', st_type='" . $_POST['st_type'];
	$sql .= "', st_category='" . mysqli_real_escape_string($connect, $_POST['st_category']);
	$sql .= "', st_category_number='" . $_POST['st_category_number'];
	$sql .= "', st_og_min='" . $_POST['st_og_min'];
	$sql .= "', st_og_max='" . $_POST['st_og_max'];
	$sql .= "', st_fg_min='" . $_POST['st_fg_min'];
	$sql .= "', st_fg_max='" . $_POST['st_fg_max'];
	$sql .= "', st_ibu_min='" . $_POST['st_ibu_min'];
	$sql .= "', st_ibu_max='" . $_POST['st_ibu_max'];
	$sql .= "', st_color_min='" . $_POST['st_color_min'];
	$sql .= "', st_color_max='" . $_POST['st_color_max'];
	$sql .= "', st_carb_min='" . $_POST['st_carb_min'];
	$sql .= "', st_carb_max='" . $_POST['st_carb_max'];
	$sql .= "', st_abv_min='" . $_POST['st_abv_min'];
	$sql .= "', st_abv_max='" . $_POST['st_abv_max'];
	$sql .= "', type='" . $_POST['type'];
	$sql .= "', batch_size='" . $_POST['batch_size'];
	$sql .= "', boil_size='" . $_POST['boil_size'];
	$sql .= "', boil_time='" . $_POST['boil_time'];
	$sql .= "', efficiency='" . $_POST['efficiency'];
	$sql .= "', est_og='" . $_POST['est_og'];
	$sql .= "', est_fg='" . $_POST['est_fg'];
	$sql .= "', est_abv='" . $_POST['est_abv'];
	$sql .= "', est_carb='" . $_POST['est_carb'];
	$sql .= "', est_color='" . $_POST['est_color'];
	$sql .= "', color_method='" . $_POST['color_method'];
	$sql .= "', est_ibu='" . $_POST['est_ibu'];
	$sql .= "', ibu_method='" . $_POST['ibu_method'];
	$sql .= "', sparge_temp='" . $_POST['sparge_temp'];
	$sql .= "', sparge_ph='" . $_POST['sparge_ph'];
	$sql .= "', sparge_volume='" . $_POST['sparge_volume'];
	$sql .= "', sparge_source='" . $_POST['sparge_source'];
	$sql .= "', sparge_acid_type='" . $_POST['sparge_acid_type'];
	$sql .= "', sparge_acid_perc='" . $_POST['sparge_acid_perc'];
	$sql .= "', sparge_acid_amount='" . $_POST['sparge_acid_amount'];
	$sql .= "', mash_ph='" . $_POST['mash_ph'];
	$sql .= "', mash_name='" . $_POST['mash_name'];
	($_POST['calc_acid'] == 'true') ? $sql .= "', calc_acid='1" : $sql .= "', calc_acid='0";
	if (isset($_POST['w1_name'])) {
		$sql .= "', w1_name='" . mysqli_real_escape_string($connect, $_POST['w1_name']);
		$sql .= "', w1_amount='" . $_POST['w1_amount'];
		$sql .= "', w1_calcium='" . $_POST['w1_calcium'];
		$sql .= "', w1_sulfate='" . $_POST['w1_sulfate'];
		$sql .= "', w1_chloride='" . $_POST['w1_chloride'];
		$sql .= "', w1_sodium='" . $_POST['w1_sodium'];
		$sql .= "', w1_magnesium='" . $_POST['w1_magnesium'];
		$sql .= "', w1_total_alkalinity='" . $_POST['w1_total_alkalinity'];
		$sql .= "', w1_ph='" . $_POST['w1_ph'];
		$sql .= "', w1_cost='" . $_POST['w1_cost'];
	}
	if (isset($_POST['w2_name'])) {
		$sql .= "', w2_name='" . mysqli_real_escape_string($connect, $_POST['w2_name']);
		$sql .= "', w2_amount='" . $_POST['w2_amount'];
		$sql .= "', w2_calcium='" . $_POST['w2_calcium'];
		$sql .= "', w2_sulfate='" . $_POST['w2_sulfate'];
		$sql .= "', w2_chloride='" . $_POST['w2_chloride'];
		$sql .= "', w2_sodium='" . $_POST['w2_sodium'];
		$sql .= "', w2_magnesium='" . $_POST['w2_magnesium'];
		$sql .= "', w2_total_alkalinity='" . $_POST['w2_total_alkalinity'];
		$sql .= "', w2_ph='" . $_POST['w2_ph'];
		$sql .= "', w2_cost='" . $_POST['w2_cost'];
	}
	$sql .= "', wa_acid_name='" . $_POST['wa_acid_name'];
	$sql .= "', wa_acid_perc='" . $_POST['wa_acid_perc'];
	$sql .= "', wa_base_name='" . $_POST['wa_base_name'];
	syslog(LOG_NOTICE, $sql);

	$fermentables = '[';
	$comma = FALSE;
	if (isset($_POST['fermentables'])) {
		$array = $_POST['fermentables'];
		foreach($array as $key => $item){
			/*
			 * Manual encode to json.
			 */
			if ($comma)
				$fermentables .= ',';
			$comma = TRUE;
			$fermentable  = '{"f_name":"' . str_replace($rescapers,$rreplacements,$item['f_name']);
			$fermentable .= '","f_origin":"' . str_replace($rescapers,$rreplacements,$item['f_origin']);
			$fermentable .= '","f_supplier":"' . str_replace($rescapers,$rreplacements,$item['f_supplier']);
			$fermentable .= '","f_amount":' . $item['f_amount'];
			$fermentable .= ',"f_cost":' . $item['f_cost'];
			$fermentable .= ',"f_type":' . $item['f_type'];
			$fermentable .= ',"f_yield":' . $item['f_yield'];
			$fermentable .= ',"f_color":' . $item['f_color'];
			$fermentable .= ',"f_coarse_fine_diff":' . $item['f_coarse_fine_diff'];
			$fermentable .= ',"f_moisture":' . $item['f_moisture'];
			$fermentable .= ',"f_diastatic_power":' . $item['f_diastatic_power'];
			$fermentable .= ',"f_protein":' . $item['f_protein'];
			$fermentable .= ',"f_dissolved_protein":' . $item['f_dissolved_protein'];
			$fermentable .= ',"f_max_in_batch":' . $item['f_max_in_batch'];
			$fermentable .= ',"f_graintype":' . $item['f_graintype'];
			$fermentable .= ',"f_added":' . $item['f_added'];
			$fermentable .= ',"f_recommend_mash":' . $item['f_recommend_mash'];
			$fermentable .= ',"f_add_after_boil":' . $item['f_add_after_boil'];
			$fermentable .= ',"f_adjust_to_total_100":' . $item['f_adjust_to_total_100'];
			$fermentable .= ',"f_percentage":' . $item['f_percentage'];
			$fermentable .= ',"f_di_ph":' . $item['f_di_ph'];
			$fermentable .= ',"f_acid_to_ph_57":' . $item['f_acid_to_ph_57'] . '}';
			syslog(LOG_NOTICE, $fermentable);
			$fermentables .= $fermentable;
		}
	}
	$fermentables .= ']';
	$sql .= "', json_fermentables='" . $fermentables;

	$hops = '[';
	$comma = FALSE;
	if (isset($_POST['hops'])) {
		$array = $_POST['hops'];
		foreach($array as $key => $item){
			if ($comma)
				$hops .= ',';
			$comma = TRUE;
			$hop  = '{"h_name":"' . str_replace($rescapers,$rreplacements,$item['h_name']);
			$hop .= '","h_origin":"' . str_replace($rescapers,$rreplacements,$item['h_origin']);
			$hop .= '","h_amount":' . $item['h_amount'];
			$hop .= ',"h_cost":' . $item['h_cost'];
			$hop .= ',"h_type":' . $item['h_type'];
			$hop .= ',"h_form":' . $item['h_form'];
			$hop .= ',"h_useat":' . $item['h_useat'];
			$hop .= ',"h_time":' . $item['h_time'];
			$hop .= ',"h_alpha":' . $item['h_alpha'];
			$hop .= ',"h_beta":' . $item['h_beta'];
			$hop .= ',"h_hsi":' . $item['h_hsi'];
			$hop .= ',"h_humulene":' . $item['h_humulene'];
			$hop .= ',"h_caryophyllene":' . $item['h_caryophyllene'];
			$hop .= ',"h_cohumulone":' . $item['h_cohumulone'];
			$hop .= ',"h_myrcene":' . $item['h_myrcene'];
			$hop .= ',"h_total_oil":' . $item['h_total_oil'] . '}';
			syslog(LOG_NOTICE, $hop);
			$hops .= $hop;
		}
	}
	$hops .= ']';
	$sql .= "', json_hops='" . $hops;

	$miscs = '[';
	$comma = FALSE;
	if (isset($_POST['miscs'])) {
		$array = $_POST['miscs'];
		foreach($array as $key => $item){
			if ($comma)
				$miscs .= ',';
			$comma = TRUE;
			$misc  = '{"m_name":"' . str_replace($rescapers,$rreplacements,$item['m_name']);
			$misc .= '","m_amount":' . $item['m_amount'];
			$misc .= ',"m_type":' . $item['m_type'];
			$misc .= ',"m_use_use":' . $item['m_use_use'];
			$misc .= ',"m_time":' . $item['m_time'];
			$misc .= ',"m_amount_is_weight":' . $item['m_amount_is_weight'];
			$misc .= ',"m_cost":' . $item['m_cost'] . '}';
			syslog(LOG_NOTICE, $misc);
			$miscs .= $misc;
		}
	}
	$miscs .= ']';
	$sql .= "', json_miscs='" . $miscs;

	$yeasts = '[';
	$comma = FALSE;
	if (isset($_POST['yeasts'])) {
		$array = $_POST['yeasts'];
		foreach($array as $key => $item){
			if ($comma)
				$yeasts .= ',';
			$comma = TRUE;
			$yeast  = '{"y_name":"' . str_replace($rescapers,$rreplacements,$item['y_name']);
			$yeast .= '","y_laboratory":"' . str_replace($rescapers,$rreplacements,$item['y_laboratory']);
			$yeast .= '","y_product_id":"' . str_replace($rescapers,$rreplacements,$item['y_product_id']);
			$yeast .= '","y_amount":' . $item['y_amount'];
			$yeast .= ',"y_type":' . $item['y_type'];
			$yeast .= ',"y_form":' . $item['y_form'];
			$yeast .= ',"y_min_temperature":' . $item['y_min_temperature'];
			$yeast .= ',"y_max_temperature":' . $item['y_max_temperature'];
			$yeast .= ',"y_flocculation":' . $item['y_flocculation'];
			$yeast .= ',"y_attenuation":' . $item['y_attenuation'];
			$yeast .= ',"y_cells":' . $item['y_cells'];
			$yeast .= ',"y_inventory":' . $item['y_inventory'];
			$yeast .= ',"y_use":' . $item['y_use'];
			$yeast .= ',"y_cost":' . $item['y_cost'] . '}';
			syslog(LOG_NOTICE, $yeast);
			$yeasts .= $yeast;
		}
	}
	$yeasts .= ']';
	$sql .= "', json_yeasts='" . $yeasts;

	$mashs = '[';
	$comma = FALSE;
	if (isset($_POST['mashs'])) {
		$array = $_POST['mashs'];
		foreach($array as $key => $item){
			if ($comma)
				$mashs .= ',';
			$comma = TRUE;
			$mash  = '{"step_name":"' . str_replace($rescapers,$rreplacements,$item['step_name']);
			$mash .= '","step_type":' . $item['step_type'];
			if (isset($item['step_infuse_amount']) && $item['step_infuse_amount'] != "")
				$mash .= ',"step_infuse_amount":' . $item['step_infuse_amount'];
			else
				$mash .= ',"step_infuse_amount":0';
			$mash .= ',"step_temp":' . $item['step_temp'];
			$mash .= ',"step_time":' . $item['step_time'];
			$mash .= ',"ramp_time":' . $item['ramp_time'];
			$mash .= ',"end_temp":' . $item['end_temp'] . '}';
			syslog(LOG_NOTICE, $mash);
			$mashs .= $mash;
		}
	}
	$mashs .= ']';
	$sql .= "', json_mashs='" . $mashs;

	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_product: result: ".mysqli_error($connect));
	} else {
		if (isset($_POST['update'])) {
			syslog(LOG_NOTICE, "db_product: updated record ".$_POST['record']);
		} else {
			$lastid = mysqli_insert_id($connect);
			syslog(LOG_NOTICE, "db_product: inserted record ".$lastid);
		}
	}
	echo $result;

} else if (isset($_POST['delete'])) {
	/*
	 * DELETE command.
	 */
	$sql = "DELETE FROM `products` WHERE uuid='".$_POST['uuid']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_product: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_product: deleted product uuid ".$_POST['uuid']);
	}
	echo $result;

} else {
	/*
	 * SELECT, produce a list of products that are not yet Closed.
	 */
	if (isset($_GET['select']) && ($_GET['select'] == "inprod")) {
		$query = "SELECT record,name,code,birth,stage FROM products WHERE stage != '11' ORDER BY birth,code;";
		$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
			$brews[] = array(
				'record' => $row['record'],
				'name' => $row['name'],
				'code' => $row['code'],
				'birth' => $row['birth'],
				'stage' => $row['stage']
			);
		}
		echo json_encode($brews, JSON_UNESCAPED_UNICODE);
		return;
	}

	/*
	 * SELECT, produce a list of products that can be fermented.
	 */
	if (isset($_GET['select']) && ($_GET['select'] == "ferment")) {
		$query  = "SELECT code,name,uuid,stage FROM products WHERE ";
		$query .= "stage='1' OR stage='2' OR stage='3' OR stage='4' OR stage='5' OR stage='6' ORDER BY code;";
		$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
			$brews[] = array(
				'code' => $row['code'],
				'name' => $row['name'],
				'uuid' => $row['uuid'],
				'stage' => $row['stage']
			);
		}
		echo json_encode($brews, JSON_UNESCAPED_UNICODE);
		return;
	}

	/*
	 * Default, select all or a given record.
	 */
	if (isset($_GET['record'])) {
		$query  = "SELECT * FROM products WHERE record='" . $_GET['record'] . "';";
		syslog(LOG_NOTICE, "get products record " . $_GET['record']);
	} else {
		$query = "SELECT * FROM products ORDER BY birth,code;";
	}
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	$brews = '[';
	$comma = FALSE;
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		// Manual encode to JSON.
		if ($comma)
			$brews .= ',';
		$comma = TRUE;
		$brew  = '{"record":' . $row['record'];
		$brew .= ',"uuid":"' . str_replace($escapers, $replacements, $row['uuid']);
		$brew .= '","name":"' . str_replace($escapers, $replacements, $row['name']);
		$brew .= '","code":"' . str_replace($escapers, $replacements, $row['code']);
		$brew .= '","birth":"' . str_replace($escapers, $replacements, $row['birth']);
		$brew .= '","stage":' . $row['stage'];
		$brew .= ',"notes":"' . str_replace($escapers, $replacements, $row['notes']);
		$brew .= '","log_brew":' . $row['log_brew'];
		$brew .= ',"log_fermentation":' . $row['log_fermentation'];
		$brew .= ',"inventory_reduced":' . $row['inventory_reduced'];
		$brew .= ',"locked":' . $row['locked'];
		$brew .= ',"eq_name":"' . str_replace($escapers, $replacements, $row['eq_name']);
		$brew .= '","eq_notes":"' . str_replace($escapers, $replacements, $row['eq_notes']);
		$brew .= '","eq_boil_size":' . floatval($row['eq_boil_size']);
		$brew .= ',"eq_batch_size":' . floatval($row['eq_batch_size']);
		$brew .= ',"eq_tun_volume":' . floatval($row['eq_tun_volume']);
		$brew .= ',"eq_tun_weight":' . floatval($row['eq_tun_weight']);
		$brew .= ',"eq_tun_specific_heat":' . floatval($row['eq_tun_specific_heat']);
		$brew .= ',"eq_tun_material":' . $row['eq_tun_material'];
		$brew .= ',"eq_tun_height":' . floatval($row['eq_tun_height']);
		$brew .= ',"eq_top_up_water":' . floatval($row['eq_top_up_water']);
		$brew .= ',"eq_trub_chiller_loss":' . floatval($row['eq_trub_chiller_loss']);
		$brew .= ',"eq_evap_rate":' . floatval($row['eq_evap_rate']);
		$brew .= ',"eq_boil_time":' . floatval($row['eq_boil_time']);
		$brew .= ',"eq_calc_boil_volume":' . $row['eq_calc_boil_volume'];
		$brew .= ',"eq_calc_boil_volume":' . floatval($row['eq_calc_boil_volume']);
		$brew .= ',"eq_top_up_kettle":' . floatval($row['eq_top_up_kettle']);
		$brew .= ',"eq_hop_utilization":' . floatval($row['eq_hop_utilization']);
		$brew .= ',"eq_lauter_volume":' . floatval($row['eq_lauter_volume']);
		$brew .= ',"eq_lauter_height":' . floatval($row['eq_lauter_height']);
		$brew .= ',"eq_lauter_deadspace":' . floatval($row['eq_lauter_deadspace']);
		$brew .= ',"eq_kettle_volume":' . floatval($row['eq_kettle_volume']);
		$brew .= ',"eq_kettle_height":' . floatval($row['eq_kettle_height']);
		$brew .= ',"eq_mash_volume":' . floatval($row['eq_mash_volume']);
		$brew .= ',"eq_mash_max":' . floatval($row['eq_mash_max']);
		$brew .= ',"eq_efficiency":' . floatval($row['eq_efficiency']);
		$brew .= ',"eq_top_up_water":' . floatval($row['eq_top_up_water']);
		$brew .= ',"brew_date_start":"' . $row['brew_date_start'];
		$brew .= '","brew_mash_ph":' . floatval($row['brew_mash_ph']);
		$brew .= ',"brew_mash_sg":' . floatval($row['brew_mash_sg']);
		$brew .= ',"brew_sparge_temperature":' . floatval($row['brew_sparge_temperature']);
		$brew .= ',"brew_sparge_volume":' . floatval($row['brew_sparge_volume']);
		$brew .= ',"brew_sparge_ph":' . floatval($row['brew_sparge_ph']);
		$brew .= ',"brew_preboil_volume":' . floatval($row['brew_preboil_volume']);
		$brew .= ',"brew_preboil_sg":' . floatval($row['brew_preboil_sg']);
		$brew .= ',"brew_preboil_ph":' . floatval($row['brew_preboil_ph']);
		$brew .= ',"brew_aboil_volume":' . floatval($row['brew_aboil_volume']);
		$brew .= ',"brew_aboil_sg":' . floatval($row['brew_aboil_sg']);
		$brew .= ',"brew_aboil_ph":' . floatval($row['brew_aboil_ph']);
		$brew .= ',"brew_aboil_efficiency":' . floatval($row['brew_aboil_efficiency']);
		$brew .= ',"brew_cooling_method":' . $row['brew_cooling_method'];
		$brew .= ',"brew_cooling_time":' . floatval($row['brew_cooling_time']);
		$brew .= ',"brew_cooling_to":' . floatval($row['brew_cooling_to']);
		$brew .= ',"brew_whirlpool9":' . floatval($row['brew_whirlpool9']);
		$brew .= ',"brew_whirlpool7":' . floatval($row['brew_whirlpool7']);
		$brew .= ',"brew_whirlpool6":' . floatval($row['brew_whirlpool6']);
		$brew .= ',"brew_whirlpool2":' . floatval($row['brew_whirlpool2']);
		$brew .= ',"brew_fermenter_volume":' . floatval($row['brew_fermenter_volume']);
		$brew .= ',"brew_fermenter_extrawater":' . floatval($row['brew_fermenter_extrawater']);
		$brew .= ',"brew_aeration_time":' . floatval($row['brew_aeration_time']);
		$brew .= ',"brew_aeration_speed":' . floatval($row['brew_aeration_speed']);
		$brew .= ',"brew_aeration_type":' . $row['brew_aeration_type'];
		$brew .= ',"brew_fermenter_sg":' . floatval($row['brew_fermenter_sg']);
		$brew .= ',"brew_fermenter_ibu":' . floatval($row['brew_fermenter_ibu']);
		$brew .= ',"brew_date_end":"' . $row['brew_date_end'];
		$brew .= '","brew_log_available":' . $row['brew_log_available'];
		$brew .= ',"og":' . floatval($row['og']);
		$brew .= ',"fg":' . floatval($row['fg']);
		$brew .= ',"primary_start_temp":' . floatval($row['primary_start_temp']);
		$brew .= ',"primary_max_temp":' . floatval($row['primary_max_temp']);
		$brew .= ',"primary_end_temp":' . floatval($row['primary_end_temp']);
		$brew .= ',"primary_end_sg":' . floatval($row['primary_end_sg']);
		$brew .= ',"primary_end_date":"' . $row['primary_end_date'];
		$brew .= '","secondary_temp":' . floatval($row['secondary_temp']);
		$brew .= ',"secondary_end_date":"' . $row['secondary_end_date'];
		$brew .= '","tertiary_temp":' . floatval($row['tertiary_temp']);
		$brew .= ',"package_date":"' . $row['package_date'];
		$brew .= '","bottle_amount":' . floatval($row['bottle_amount']);
		$brew .= ',"bottle_carbonation":' . floatval($row['bottle_carbonation']);
		$brew .= ',"bottle_priming_sugar":' . $row['bottle_priming_sugar'];
		$brew .= ',"bottle_priming_amount":' . floatval($row['bottle_priming_amount']);
		$brew .= ',"bottle_carbonation_temp":' . floatval($row['bottle_carbonation_temp']);
		$brew .= ',"keg_amount":' . floatval($row['keg_amount']);
		$brew .= ',"keg_carbonation":' . floatval($row['keg_carbonation']);
		$brew .= ',"keg_priming_sugar":' . $row['keg_priming_sugar'];
		$brew .= ',"keg_priming_amount":' . floatval($row['keg_priming_amount']);
		$brew .= ',"keg_carbonation_temp":' . floatval($row['keg_carbonation_temp']);
		$brew .= ',"keg_forced_carb":' . floatval($row['keg_forced_carb']);
		$brew .= ',"keg_pressure":' . floatval($row['keg_pressure']);
		$brew .= ',"taste_notes":"' . str_replace($escapers, $replacements, $row['taste_notes']);
		$brew .= '","taste_rate":' . floatval($row['taste_rate']);
		$brew .= ',"taste_date":"' . str_replace($escapers, $replacements, $row['taste_date']);
		$brew .= '","taste_color":"' . str_replace($escapers, $replacements, $row['taste_color']);
		$brew .= '","taste_transparency":"' . str_replace($escapers, $replacements, $row['taste_transparency']);
		$brew .= '","taste_head":"' . str_replace($escapers, $replacements, $row['taste_head']);
		$brew .= '","taste_aroma":"' . str_replace($escapers, $replacements, $row['taste_aroma']);
		$brew .= '","taste_taste":"' . str_replace($escapers, $replacements, $row['taste_taste']);
		$brew .= '","taste_mouthfeel":"' . str_replace($escapers, $replacements, $row['taste_mouthfeel']);
		$brew .= '","taste_aftertaste":"' . str_replace($escapers, $replacements, $row['taste_aftertaste']);
		$brew .= '","st_guide":"' . str_replace($escapers, $replacements, $row['st_guide']);
		$brew .= '","st_letter":"' . str_replace($escapers, $replacements, $row['st_letter']);
		$brew .= '","st_name":"'  . str_replace($escapers, $replacements, $row['st_name']);
		$brew .= '","st_type":' . $row['st_type'];
		$brew .= ',"st_category":"' . str_replace($escapers, $replacements, $row['st_category']);
		$brew .= '","st_category_number":' . floatval($row['st_category_number']);
		$brew .= ',"st_og_min":' . floatval($row['st_og_min']);
		$brew .= ',"st_og_max":' . floatval($row['st_og_max']);
		$brew .= ',"st_fg_min":' . floatval($row['st_fg_min']);
		$brew .= ',"st_fg_max":' . floatval($row['st_fg_max']);
		$brew .= ',"st_ibu_min":' . floatval($row['st_ibu_min']);
		$brew .= ',"st_ibu_max":' . floatval($row['st_ibu_max']);
		$brew .= ',"st_color_min":' . floatval($row['st_color_min']);
		$brew .= ',"st_color_max":' . floatval($row['st_color_max']);
		$brew .= ',"st_carb_min":' . floatval($row['st_carb_min']);
		$brew .= ',"st_carb_max":' . floatval($row['st_carb_max']);
		$brew .= ',"st_abv_min":' . floatval($row['st_abv_min']);
		$brew .= ',"st_abv_max":' . floatval($row['st_abv_max']);
		$brew .= ',"type":' . $row['type'];
		$brew .= ',"batch_size":' . floatval($row['batch_size']);
		$brew .= ',"boil_size":' . floatval($row['boil_size']);
		$brew .= ',"boil_time":' . floatval($row['boil_time']);
		$brew .= ',"efficiency":' . floatval($row['efficiency']);
		$brew .= ',"est_og":' . floatval($row['est_og']);
		$brew .= ',"est_fg":' . floatval($row['est_fg']);
		$brew .= ',"est_abv":' . floatval($row['est_abv']);
		$brew .= ',"est_carb":' . floatval($row['est_carb']);
		$brew .= ',"est_color":' . floatval($row['est_color']);
		$brew .= ',"color_method":' . $row['color_method'];
		$brew .= ',"est_ibu":' . floatval($row['est_ibu']);
		$brew .= ',"ibu_method":' . $row['ibu_method'];
		$brew .= ',"sparge_temp":' . floatval($row['sparge_temp']);
		$brew .= ',"sparge_ph":' . floatval($row['sparge_ph']);
		$brew .= ',"sparge_volume":' . floatval($row['sparge_volume']);
		$brew .= ',"sparge_source":' . $row['sparge_source'];
		$brew .= ',"sparge_acid_type":' . $row['sparge_acid_type'];
		$brew .= ',"sparge_acid_perc":' . floatval($row['sparge_acid_perc']);
		$brew .= ',"sparge_acid_amount":' . floatval($row['sparge_acid_amount']);
		$brew .= ',"mash_ph":' . $row['mash_ph'];
		$brew .= ',"mash_name":"' . $row['mash_name'];
		$brew .= '","calc_acid":' . $row['calc_acid'];
		$brew .= ',"w1_name":"' . str_replace($escapers, $replacements, $row['w1_name']);
		$brew .= '","w1_amount":' . $row['w1_amount'];
		$brew .= ',"w1_calcium":' . $row['w1_calcium'];
		$brew .= ',"w1_sulfate":' . $row['w1_sulfate'];
		$brew .= ',"w1_chloride":' . $row['w1_chloride'];
		$brew .= ',"w1_sodium":' . $row['w1_sodium'];
		$brew .= ',"w1_magnesium":' . $row['w1_magnesium'];
		$brew .= ',"w1_total_alkalinity":' . $row['w1_total_alkalinity'];
		$brew .= ',"w1_ph":' . $row['w1_ph'];
		$brew .= ',"w1_cost":' . $row['w1_cost'];
		$brew .= ',"w2_name":"' . str_replace($escapers, $replacements, $row['w2_name']);
		$brew .= '","w2_amount":' . $row['w2_amount'];
		$brew .= ',"w2_calcium":' . $row['w2_calcium'];
		$brew .= ',"w2_sulfate":' . $row['w2_sulfate'];
		$brew .= ',"w2_chloride":' . $row['w2_chloride'];
		$brew .= ',"w2_sodium":' . $row['w2_sodium'];
		$brew .= ',"w2_magnesium":' . $row['w2_magnesium'];
		$brew .= ',"w2_total_alkalinity":' . $row['w2_total_alkalinity'];
		$brew .= ',"w2_ph":' . $row['w2_ph'];
		$brew .= ',"w2_cost":' . $row['w2_cost'];
		$brew .= ',"wa_acid_name":' . $row['wa_acid_name'];
		$brew .= ',"wa_acid_perc":' . $row['wa_acid_perc'];
		$brew .= ',"wa_base_name":' . $row['wa_base_name'];
		if (isset($_GET['record'])) {
			// Append stock information.
			$fermentables = json_decode($row['json_fermentables'], true);
			for ($i = 0; $i < count($fermentables); $i++) {
				$fermentables[$i]['f_inventory'] = 0;   // Not in stock
				$fermentables[$i]['f_avail'] = 0;       // Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_fermentables ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $fermentables[$i]['f_name'])."' AND";
				$sql2 .= " supplier='".str_replace($rescapers, $rreplacements, $fermentables[$i]['f_supplier'])."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$fermentables[$i]['f_inventory'] = floatval($obj->inventory);
						$fermentables[$i]['f_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$brew .= ',"fermentables":' . json_encode($fermentables, JSON_UNESCAPED_UNICODE);

			$hops = json_decode($row['json_hops'], true);
			for ($i = 0; $i < count($hops); $i++) {
				$hops[$i]['h_inventory'] = 0;   // Not in stock
				$hops[$i]['h_avail'] = 0;       // Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_hops ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $hops[$i]['h_name'])."' AND";
				$sql2 .= " origin='".str_replace($rescapers, $rreplacements, $hops[$i]['h_origin'])."' AND";
				$sql2 .= " form='".$hops[$i]['h_form']."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$hops[$i]['h_inventory'] = floatval($obj->inventory);
						$hops[$i]['h_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$brew .= ',"hops":' . json_encode($hops, JSON_UNESCAPED_UNICODE);

			$miscs = json_decode($row['json_miscs'], true);
			for ($i = 0; $i < count($miscs); $i++) {
				$miscs[$i]['m_inventory'] = 0;   // Not in stock
				$miscs[$i]['m_avail'] = 0;       // Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_miscs ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $miscs[$i]['m_name'])."' AND";
				$sql2 .= " type='".$miscs[$i]['m_type']."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$miscs[$i]['m_inventory'] = floatval($obj->inventory);
						$miscs[$i]['m_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$brew .= ',"miscs":' . json_encode($miscs, JSON_UNESCAPED_UNICODE);

			$yeasts = json_decode($row['json_yeasts'], true);
			for ($i = 0; $i < count($yeasts); $i++) {
				$yeasts[$i]['y_inventory'] = 0;   // Not in stock
				$yeasts[$i]['y_avail'] = 0;       // Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_yeasts ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_name'])."' AND";
				$sql2 .= " form='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_form'])."' AND";
				$sql2 .= " laboratory='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_laboratory'])."' AND";
				$sql2 .= " product_id='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_product_id'])."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$yeasts[$i]['y_inventory'] = floatval($obj->inventory);
						$yeasts[$i]['y_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$brew .= ',"yeasts":' . json_encode($yeasts, JSON_UNESCAPED_UNICODE);
		} else {
			// Just leave it
			$brew .= ',"fermentables":' . $row['json_fermentables'];
			$brew .= ',"hops":' . $row['json_hops'];
			$brew .= ',"miscs":' . $row['json_miscs'];
			$brew .= ',"yeasts":' . $row['json_yeasts'];
		}
		$brew .= ',"mashs":' . $row['json_mashs'];
		$brew .= '}';
//		syslog(LOG_NOTICE, $brew);
		$brews .= $brew;
	}
	$brews .= ']';
	if (isset($_GET['record']))
		syslog(LOG_NOTICE, "ready to sent");
	header("Content-type: application/json");
	echo $brews;
}
?>

mercurial