www/includes/db_product.php

Fri, 20 Sep 2019 20:12:36 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 20 Sep 2019 20:12:36 +0200
changeset 500
8d53ad389204
parent 499
4f14a18b581e
child 515
9d771385a8a0
permissions
-rw-r--r--

Reworked the splitted batches, the data is now in a separate table and there is only one product record.

<?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'];
	} 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 .= "', 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']);
	$sql .= "', log_brew='" . $_POST['log_brew'];
	$sql .= "', log_fermentation='" . $_POST['log_fermentation'];
	$sql .= "', inventory_reduced='" . $_POST['inventory_reduced'];
	$sql .= "', locked='" . $_POST['locked'];
	// 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'];
	// brewdate
	if ($_POST['brew_date_start'] == '')
		$sql .= "', brew_date_start=NULL";
	else
		$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_mash_efficiency='" . $_POST['brew_mash_efficiency'];
	$sql .= "', brew_sparge_est='" . $_POST['brew_sparge_est'];
	$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_preboil_efficiency='" . $_POST['brew_preboil_efficiency'];
	$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_tcloss='" . $_POST['brew_fermenter_tcloss'];
	$sql .= "', brew_fermenter_sg='" . $_POST['brew_fermenter_sg'];
	$sql .= "', brew_fermenter_ibu='" . $_POST['brew_fermenter_ibu'];
	$sql .= "', brew_fermenter_color='" . $_POST['brew_fermenter_color'];
	if ($_POST['brew_date_end'] == '')
		$sql .= "', brew_date_end=NULL";
	else
		$sql .= "', brew_date_end='" . $_POST['brew_date_end'] . "'";
	$sql .=  ", og='" . $_POST['og'];
	$sql .= "', fg='" . $_POST['fg'];
	$sql .= "', primary_start_temp='" . $_POST['primary_start_temp'];
	$sql .= "', primary_max_temp='" . $_POST['primary_max_temp'];
	$sql .= "', primary_end_temp='" . $_POST['primary_end_temp'];
	if ($_POST['primary_end_date'] == '')
		$sql .= "', primary_end_date=NULL";
	else
		$sql .= "', primary_end_date='" . $_POST['primary_end_date'] ."'";
	$sql .=  ", primary_end_sg='" . $_POST['primary_end_sg'];
	$sql .= "', secondary_temp='" . $_POST['secondary_temp'];
	$sql .= "', secondary_end_sg='" . $_POST['secondary_end_sg'];
	if ($_POST['secondary_end_date'] == '')
		$sql .= "', secondary_end_date=NULL";
	else
		$sql .= "', secondary_end_date='" . $_POST['secondary_end_date'] ."'";
	$sql .=  ", tertiary_temp='" . $_POST['tertiary_temp'];
	if ($_POST['package_date'] == '')
		$sql .= "', package_date=NULL";
	else
		$sql .= "', package_date='" . $_POST['package_date'] ."'";
	$sql .=  ", package_volume='" . $_POST['package_volume'];
	$sql .= "', package_infuse_amount='" . $_POST['package_infuse_amount'];
	$sql .= "', package_infuse_abv='" . $_POST['package_infuse_abv'];
	$sql .= "', package_infuse_notes='" . mysqli_real_escape_string($connect, $_POST['package_infuse_notes']);
	$sql .= "', package_abv='" . $_POST['package_abv'];
	$sql .= "', package_ph='" . $_POST['package_ph'];
	$sql .= "', bottle_amount='" . $_POST['bottle_amount'];
	$sql .= "', bottle_carbonation='" . $_POST['bottle_carbonation'];
	$sql .= "', bottle_priming_water='" . $_POST['bottle_priming_water'];
	$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_water='" . $_POST['keg_priming_water'];
	$sql .= "', keg_priming_amount='" . $_POST['keg_priming_amount'];
	$sql .= "', keg_carbonation_temp='" . $_POST['keg_carbonation_temp'];
	$sql .= "', keg_forced_carb='" . $_POST['keg_forced_carb'];
	$sql .= "', keg_pressure='" . $_POST['keg_pressure'];
	$sql .= "', taste_notes='" . mysqli_real_escape_string($connect, $_POST['taste_notes']);
	$sql .= "', taste_rate='" . $_POST['taste_rate'];
	if ($_POST['taste_date'] == '')
		$sql .= "', taste_date=NULL";
	else
		$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'];
	$sql .= "', calc_acid='" . $_POST['calc_acid'];
	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 .= "', wg_amount='" . $_POST['wg_amount'];
        $sql .= "', wg_calcium='" . $_POST['wg_calcium'];
        $sql .= "', wg_sulfate='" . $_POST['wg_sulfate'];
        $sql .= "', wg_chloride='" . $_POST['wg_chloride'];
        $sql .= "', wg_sodium='" . $_POST['wg_sodium'];
        $sql .= "', wg_magnesium='" . $_POST['wg_magnesium'];
        $sql .= "', wg_total_alkalinity='" . $_POST['wg_total_alkalinity'];
        $sql .= "', wg_ph='" . $_POST['wg_ph'];
        $sql .= "', wb_calcium='" . $_POST['wb_calcium'];
        $sql .= "', wb_sulfate='" . $_POST['wb_sulfate'];
        $sql .= "', wb_chloride='" . $_POST['wb_chloride'];
        $sql .= "', wb_sodium='" . $_POST['wb_sodium'];
        $sql .= "', wb_magnesium='" . $_POST['wb_magnesium'];
        $sql .= "', wb_total_alkalinity='" . $_POST['wb_total_alkalinity'];
        $sql .= "', wb_ph='" . $_POST['wb_ph'];
	$sql .= "', wa_acid_name='" . $_POST['wa_acid_name'];
	$sql .= "', wa_acid_perc='" . $_POST['wa_acid_perc'];
	$sql .= "', wa_base_name='" . $_POST['wa_base_name'];
	if ($_POST['starter_enable'] == 0) {
		$sql .= "', starter_enable='0', starter_type='0', starter_sg='1.040', starter_viability='97";
		$sql .= "', prop1_type='0', prop1_volume='0', prop2_type='0', prop2_volume='0";
		$sql .= "', prop3_type='0', prop3_volume='0', prop4_type='0', prop4_volume='0";
	} else {
		$sql .= "', starter_enable='1";
		$sql .= "', starter_type='" . $_POST['starter_type'];
		$sql .= "', starter_sg='" . $_POST['starter_sg'];
		$sql .= "', starter_viability='" . $_POST['starter_viability'];
		$sql .= "', prop1_type='" . $_POST['prop1_type'];
		$sql .= "', prop1_volume='" . $_POST['prop1_volume'];
		$sql .= "', prop2_type='" . $_POST['prop2_type'];
		$sql .= "', prop2_volume='" . $_POST['prop2_volume'];
		$sql .= "', prop3_type='" . $_POST['prop3_type'];
		$sql .= "', prop3_volume='" . $_POST['prop3_volume'];
		$sql .= "', prop4_type='" . $_POST['prop4_type'];
		$sql .= "', prop4_volume='" . $_POST['prop4_volume'];
	}
	$sql .= "', divide_type='" . $_POST['divide_type'];
	$sql .= "', divide_size='" . $_POST['divide_size'];
	$sql .= "', divide_parts='" . $_POST['divide_parts'];
	//syslog(LOG_NOTICE, $sql);

	$fermentables = '[';
	$comma = FALSE;
	if (isset($_POST['fermentables'])) {
		$array = $_POST['fermentables'];
		// Sort the array
		$added = array();
		$amount = array();
		for ($i = 0; $i < count($array); $i++) {
			$added[] = $array[$i]['f_added'];
			$amount[] = $array[$i]['f_amount'];
		}
		array_multisort($added, SORT_ASC, SORT_NUMERIC, 
			$amount, SORT_DESC, SORT_NUMERIC,
			$array);
		// Write the sorted array.
		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 .= ']';
	//syslog(LOG_NOTICE, $fermentables);
	$sql .= "', json_fermentables='" . $fermentables;

	$hops = '[';
	$comma = FALSE;
	if (isset($_POST['hops'])) {
		$array = $_POST['hops'];
		// Sort the array
		$useat = array();
        	$time = array();
		$amount = array();
		for ($i = 0; $i < count($array); $i++) {
	  		$useat[] = $array[$i]['h_useat'];
	  		$time[] = $array[$i]['h_time'];
			$amount[] = $array[$i]['h_amount'];
		}
		array_multisort($useat, SORT_ASC, SORT_NUMERIC, 
				$time, SORT_DESC, SORT_NUMERIC,
				$amount, SORT_DESC, SORT_NUMERIC,
				$array);
		// Write the sorted array.
		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 .= ']';
	//syslog(LOG_NOTICE, 'hops: ' . $hops);
	$sql .= "', json_hops='" . $hops;

	$miscs = '[';
	$comma = FALSE;
	if (isset($_POST['miscs'])) {
		$array = $_POST['miscs'];
		// Sort the array
                $use = array();
                $type = array();
		$amount = array();
                for ($i = 0; $i < count($array); $i++) {
                        $use[] = $array[$i]['m_use_use'];
                        $type[] = $array[$i]['m_type'];
			$amount[] = $array[$i]['m_amount'];
                }
                array_multisort($use, SORT_ASC, SORT_NUMERIC,
                        $type, SORT_ASC, SORT_NUMERIC,
			$amount, SORT_DESC, SORT_NUMERIC,
                        $array);
                // Write the sorted array.
		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 .= ']';
	//syslog(LOG_NOTICE, 'miscs: ' . $miscs);
	$sql .= "', json_miscs='" . $miscs;

	$yeasts = '[';
	$comma = FALSE;
	if (isset($_POST['yeasts'])) {
		$array = $_POST['yeasts'];
		// Sort the array
                $use = array();
                $amount = array();
                for ($i = 0; $i < count($array); $i++) {
                        $use[] = $array[$i]['y_use'];
                        $amount[] = $array[$i]['y_amount'];
                }
                array_multisort($use, SORT_ASC, SORT_NUMERIC,
                        $amount, SORT_DESC, SORT_NUMERIC,
                        $array);
                // Write the sorted array.
		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_tolerance":' . $item['y_tolerance'];
			$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 .= ']';
	//syslog(LOG_NOTICE, $yeasts);
	$sql .= "', json_yeasts='" . $yeasts;

	$mashs = '[';
	$comma = FALSE;
	if (isset($_POST['mashs'])) {
		$array = $_POST['mashs'];
		// Sort the array
                $temp = array();
                for ($i = 0; $i < count($array); $i++) {
                        $temp[] = $array[$i]['step_temp'];
                }
                array_multisort($temp, SORT_ASC, SORT_NUMERIC, $array);
                // Write the sorted array.
		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 .= ']';
	//syslog(LOG_NOTICE, $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']);
			inventory_reduce();
		} else {
			$lastid = mysqli_insert_id($connect);
			syslog(LOG_NOTICE, "db_product: inserted record ".$lastid);
		}
	}
	echo $result;

} else if (isset($_POST['splitit'])) {
	/*
	 * Update split batch details
	 */
	$sql  = "UPDATE `products` SET ";
	$sql .=    "divide_type='" . $_POST['divide_type'];
        $sql .= "', divide_size='" . $_POST['divide_size'];
        $sql .= "', divide_parts='" . $_POST['divide_parts'];
	$sql .= "' WHERE record='" . $_POST['record'] . "';";
        syslog(LOG_NOTICE, $sql);

	$result = mysqli_query($connect, $sql);
        if (! $result) {
		syslog(LOG_NOTICE, "db_product: result: ".mysqli_error($connect));
        } else {
                syslog(LOG_NOTICE, "db_product: updated (divide) record ".$_POST['record']);
        }
        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,brew_date_start,package_date 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'],
				'brew_date' => substr($row['brew_date_start'], 0, 10),
				'package_date' => $row['package_date']
			);
		}
		header("Content-type: application/json");
		echo json_encode($brews, JSON_UNESCAPED_UNICODE);
		return;
	}

	if (isset($_GET['select']) && ($_GET['select'] == "archname")) {
		$query = "SELECT record,name,code,brew_date_start,st_name,og,fg,batch_size FROM products WHERE stage = '11' ORDER BY name,brew_date_start;";
		$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'],
				'date' => substr($row['brew_date_start'], 0, 10),
				'style' => $row['st_name'],
				'og' => $row['og'],
				'fg' => $row['fg'],
				'batch_size' => $row['batch_size']
			);
		}
		header("Content-type: application/json");
	        echo json_encode($brews, JSON_UNESCAPED_UNICODE);
	        return;
	}

	if (isset($_GET['select']) && ($_GET['select'] == "archcode")) {
		$query = "SELECT record,name,code,brew_date_start,st_name,og,fg,batch_size FROM products WHERE stage = '11' 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(
				'record' => $row['record'],
				'name' => $row['name'],
				'code' => $row['code'],
				'date' => substr($row['brew_date_start'], 0, 10),
				'style' => $row['st_name'],
				'og' => $row['og'],
				'fg' => $row['fg'],
				'batch_size' => $row['batch_size']
			);
		}
		header("Content-type: application/json");
		echo json_encode($brews, JSON_UNESCAPED_UNICODE);
		return;
	}

	if (isset($_GET['select']) && ($_GET['select'] == "archdate")) {
		$query = "SELECT record,name,code,brew_date_start,st_name,og,fg,batch_size FROM products WHERE stage = '11' ORDER BY brew_date_start;";
		$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'],
				'year' => substr($row['brew_date_start'], 0, 4),
				'date' => substr($row['brew_date_start'], 0, 10),
				'style' => $row['st_name'],
				'og' => $row['og'],
				'fg' => $row['fg'],
				'batch_size' => $row['batch_size']
			);
		}
		header("Content-type: application/json");
		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,json_yeasts FROM products WHERE ";
		$query .= "stage='1' OR stage='2' OR stage='3' OR stage='4' OR stage='5' OR stage='6' OR stage='7' ORDER BY code;";
		$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
			$yl = 0;
			$yh = 40;
			$yeasts = json_decode($row['json_yeasts'], true);
			for ($i = 0; $i < count($yeasts); $i++) {
				if ($yeasts[$i]['y_use'] == 0) { // Primary
					if (floatval($yeasts[$i]['y_min_temperature']) > $yl)
						$yl = floatval($yeasts[$i]['y_min_temperature']);
					if (floatval($yeasts[$i]['y_max_temperature']) < $yh)
						$yh = floatval($yeasts[$i]['y_max_temperature']);
				}
			}
			$brews[] = array(
				'code' => $row['code'],
				'name' => $row['name'],
				'uuid' => $row['uuid'],
				'stage' => $row['stage'],
				'yeast_lo' => $yl,
				'yeast_hi' => $yh
			);
		}
		header("Content-type: application/json");
		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'] . "';";
	} 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_mash_efficiency":' . floatval($row['brew_mash_efficiency']);
		$brew .= ',"brew_sparge_est":' . floatval($row['brew_sparge_est']);
		$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_preboil_efficiency":' . floatval($row['brew_preboil_efficiency']);
		$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_fermenter_tcloss":' . floatval($row['brew_fermenter_tcloss']);
		$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_fermenter_color":' . floatval($row['brew_fermenter_color']);
		$brew .= ',"brew_date_end":"' . $row['brew_date_end'];
		$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_sg":' . floatval($row['secondary_end_sg']);
		$brew .= ',"secondary_end_date":"' . $row['secondary_end_date'];
		$brew .= '","tertiary_temp":' . floatval($row['tertiary_temp']);
		$brew .= ',"package_date":"' . $row['package_date'];
		$brew .= '","package_volume":' . floatval($row['package_volume']);
		$brew .= ',"package_infuse_amount":' . floatval($row['package_infuse_amount']);
		$brew .= ',"package_infuse_abv":' . floatval($row['package_infuse_abv']);
		$brew .= ',"package_infuse_notes":"' . $row['package_infuse_notes'];
		$brew .= '","package_abv":' . floatval($row['package_abv']);
		$brew .= ',"package_ph":' . floatval($row['package_ph']);
		$brew .= ',"bottle_amount":' . floatval($row['bottle_amount']);
		$brew .= ',"bottle_carbonation":' . floatval($row['bottle_carbonation']);
		$brew .= ',"bottle_priming_water":' . floatval($row['bottle_priming_water']);
		$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_water":' . floatval($row['keg_priming_water']);
		$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":' . floatval($row['mash_ph']);
		$brew .= ',"mash_name":"' . str_replace($escapers, $replacements, $row['mash_name']);
		$brew .= '","calc_acid":' . $row['calc_acid'];
		$brew .= ',"w1_name":"' . str_replace($escapers, $replacements, $row['w1_name']);
		$brew .= '","w1_amount":' . floatval($row['w1_amount']);
		$brew .= ',"w1_calcium":' . floatval($row['w1_calcium']);
		$brew .= ',"w1_sulfate":' . floatval($row['w1_sulfate']);
		$brew .= ',"w1_chloride":' . floatval($row['w1_chloride']);
		$brew .= ',"w1_sodium":' . floatval($row['w1_sodium']);
		$brew .= ',"w1_magnesium":' . floatval($row['w1_magnesium']);
		$brew .= ',"w1_total_alkalinity":' . floatval($row['w1_total_alkalinity']);
		$brew .= ',"w1_ph":' . floatval($row['w1_ph']);
		$brew .= ',"w1_cost":' . floatval($row['w1_cost']);
		$brew .= ',"w2_name":"' . str_replace($escapers, $replacements, $row['w2_name']);
		$brew .= '","w2_amount":' . floatval($row['w2_amount']);
		$brew .= ',"w2_calcium":' . floatval($row['w2_calcium']);
		$brew .= ',"w2_sulfate":' . floatval($row['w2_sulfate']);
		$brew .= ',"w2_chloride":' . floatval($row['w2_chloride']);
		$brew .= ',"w2_sodium":' . floatval($row['w2_sodium']);
		$brew .= ',"w2_magnesium":' . floatval($row['w2_magnesium']);
		$brew .= ',"w2_total_alkalinity":' . floatval($row['w2_total_alkalinity']);
		$brew .= ',"w2_ph":' . floatval($row['w2_ph']);
		$brew .= ',"w2_cost":' . floatval($row['w2_cost']);
		$brew .= ',"wg_amount":' . floatval($row['wg_amount']);
                $brew .= ',"wg_calcium":' . floatval($row['wg_calcium']);
                $brew .= ',"wg_sulfate":' . floatval($row['wg_sulfate']);
                $brew .= ',"wg_chloride":' . floatval($row['wg_chloride']);
                $brew .= ',"wg_sodium":' . floatval($row['wg_sodium']);
                $brew .= ',"wg_magnesium":' . floatval($row['wg_magnesium']);
                $brew .= ',"wg_total_alkalinity":' . floatval($row['wg_total_alkalinity']);
                $brew .= ',"wg_ph":' . floatval($row['wg_ph']);
                $brew .= ',"wb_calcium":' . floatval($row['wb_calcium']);
                $brew .= ',"wb_sulfate":' . floatval($row['wb_sulfate']);
                $brew .= ',"wb_chloride":' . floatval($row['wb_chloride']);
                $brew .= ',"wb_sodium":' . floatval($row['wb_sodium']);
                $brew .= ',"wb_magnesium":' . floatval($row['wb_magnesium']);
                $brew .= ',"wb_total_alkalinity":' . floatval($row['wb_total_alkalinity']);
                $brew .= ',"wb_ph":' . floatval($row['wb_ph']);
		$brew .= ',"wa_acid_name":' . $row['wa_acid_name'];
		$brew .= ',"wa_acid_perc":' . $row['wa_acid_perc'];
		$brew .= ',"wa_base_name":' . $row['wa_base_name'];
		$brew .= ',"starter_enable":' . $row['starter_enable'];
		$brew .= ',"starter_type":' . $row['starter_type'];
		$brew .= ',"starter_sg":' . $row['starter_sg'];
		$brew .= ',"starter_viability":' . $row['starter_viability'];
		$brew .= ',"prop1_type":' . $row['prop1_type'];
		$brew .= ',"prop1_volume":' . $row['prop1_volume'];
		$brew .= ',"prop2_type":' . $row['prop2_type'];
		$brew .= ',"prop2_volume":' . $row['prop2_volume'];
		$brew .= ',"prop3_type":' . $row['prop3_type'];
		$brew .= ',"prop3_volume":' . $row['prop3_volume'];
		$brew .= ',"prop4_type":' . $row['prop4_type'];
		$brew .= ',"prop4_volume":' . $row['prop4_volume'];

		$brew .= ',"divide_type":' . $row['divide_type'];
		$brew .= ',"divide_size":' . floatval($row['divide_size']);
		$brew .= ',"divide_parts":' . $row['divide_parts'];

		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
				if (! isset($yeasts[$i]['y_tolerance']))
                                        $yeasts[$i]['y_tolerance'] = 0;
				$sql2 = "SELECT inventory,tolerance 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;
						if ($yeasts[$i]['y_tolerance'] == 0) {
							$yeasts[$i]['y_tolerance'] = floatval($obj->tolerance); // Upgrade alcohol tolerance
						}
					}
					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 .= ']';
	header("Content-type: application/json");
	echo $brews;
}



function reduce_fermentables($item) {

	global $connect;

	$amount = round($item['f_amount'] * 1000) / 1000;
        $sql2  = "UPDATE inventory_fermentables SET inventory = inventory - " . $amount;
        $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['f_name']);
        $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['f_origin']);
        $sql2 .= "' AND supplier='" . mysqli_real_escape_string($connect, $item['f_supplier']);
        $sql2 .= "' AND inventory >= " . $amount;
        $sql2 .= " LIMIT 1;";
        $result2 = mysqli_query($connect, $sql2);
        $ar = mysqli_affected_rows($connect);
        if ($ar == 1) {
         	syslog(LOG_NOTICE, "Reduced fermentable `".$item['f_name']."' from `".$item['f_supplier']."' with ".$amount." kg");
        } else if ($ar == 0) {
                $sql2  = "UPDATE inventory_fermentables SET inventory = 0";
                $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['f_name']);
                $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['f_origin']);
                $sql2 .= "' AND supplier='" . mysqli_real_escape_string($connect, $item['f_supplier']);
                $sql2 .= "' AND inventory < " . $amount;
                $sql2 .= " LIMIT 1;";
                $result2 = mysqli_query($connect, $sql2);
                $ar = mysqli_affected_rows($connect);
                if ($ar == 1) {
                        syslog(LOG_NOTICE, "Reduced fermentable `".$item['f_name']."' from `".$item['f_supplier']."' to 0 kg");
                } else if ($ar == 0) {
                        syslog(LOG_NOTICE, "Reduce fermentable `".$item['f_name']."' from `".$item['f_supplier']."' failed");
                }
        }
}



function reduce_hops($item) {

	global $connect;

	$amount = round($item['h_amount'] * 1000) / 1000;
        $sql2  = "UPDATE inventory_hops SET inventory = inventory - " . $amount;
        $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['h_name']);
        $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['h_origin']);
        $sql2 .= "' AND form=" . $item['h_form'];
        $sql2 .= " AND inventory >= " . $amount . " LIMIT 1;";
        $result2 = mysqli_query($connect, $sql2);
        $ar = mysqli_affected_rows($connect);
        if ($ar == 1) {
                syslog(LOG_NOTICE, "Reduced hop `".$item['h_name']."' from `".$item['h_origin']."' with ".$amount." kg");
        } else if ($ar == 0) {
                $sql2  = "UPDATE inventory_hops SET inventory = 0";
                $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['h_name']);
                $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['h_origin']);
                $sql2 .= "' AND form=" . $item['h_form'];
                $sql2 .= " AND inventory < " . $amount . " LIMIT 1;";
                $result2 = mysqli_query($connect, $sql2);
                $ar = mysqli_affected_rows($connect);
                if ($ar == 1) {
                        syslog(LOG_NOTICE, "Reduced hop `".$item['h_name']."' from `".$item['h_origin']."' to 0 kg");
                } else if ($ar == 0) {
                        syslog(LOG_NOTICE, "Reduce hop `".$item['h_name']."' from `".$item['h_origin']."' failed");
                }
        }
}



function reduce_miscs($item) {

	global $connect;

	$amount = round($item['m_amount'] * 100000) / 100000;
        $sql2  = "UPDATE inventory_miscs SET inventory = inventory - " . $amount;
        $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['m_name']);
        $sql2 .= "' AND inventory >= " . $amount . " LIMIT 1;";
        $result2 = mysqli_query($connect, $sql2);
        $ar = mysqli_affected_rows($connect);
        if ($ar == 1) {
                syslog(LOG_NOTICE, "Reduced misc `".$item['m_name']."' with ".$amount);
        } else if ($ar == 0) { 
                $sql2  = "UPDATE inventory_miscs SET inventory = 0";
                $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['m_name']);
                $sql2 .= "' AND inventory < " . $amount . " LIMIT 1;";
                $result2 = mysqli_query($connect, $sql2); 
                $ar = mysqli_affected_rows($connect);
                if ($ar == 1) {
                       syslog(LOG_NOTICE, "Reduced misc `".$item['m_name']."' to 0");
                } else if ($ar == 0) { 
                       syslog(LOG_NOTICE, "Reduce misc `".$item['m_name']."' failed");
                }
        }
}



function reduce_yeast($item) {

	global $connect;

	$amount = round($item['y_amount'] * 10000) / 10000;
	$sql2  = "UPDATE inventory_yeasts SET inventory = inventory - " . $amount;
        $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['y_name']);
        $sql2 .= "' AND laboratory='" . mysqli_real_escape_string($connect, $item['y_laboratory']);
        $sql2 .= "' AND product_id='" . mysqli_real_escape_string($connect, $item['y_product_id']);
        $sql2 .= "' AND form=" . $item['y_form'];
        $sql2 .= " AND inventory >= " . $amount . " LIMIT 1;";
        $result2 = mysqli_query($connect, $sql2);
        $ar = mysqli_affected_rows($connect);
        if ($ar == 1) {
		syslog(LOG_NOTICE, "Reduced yeast `".$item['y_product_id'].' '.$item['y_name']."' from `".$item['y_laboratory']."' with ".$amount);
        } else if ($ar == 0) {
                $sql2  = "UPDATE inventory_yeasts SET inventory = 0";
                $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['y_name']);
                $sql2 .= "' AND laboratory='" . mysqli_real_escape_string($connect, $item['y_laboratory']);
                $sql2 .= "' AND product_id='" . mysqli_real_escape_string($connect, $item['y_product_id']);
                $sql2 .= "' AND form=" . $item['y_form'];
                $sql2 .= " AND inventory < " . $amount . " LIMIT 1;";
                $result2 = mysqli_query($connect, $sql2);
                $ar = mysqli_affected_rows($connect);
                if ($ar == 1) {
                        syslog(LOG_NOTICE, "Reduced yeast `".$item['y_product_id'].' '.$item['y_name']."' from `".$item['y_laboratory']."' to 0");
                } else if ($ar == 0) {
                	syslog(LOG_NOTICE, "Reduce yeast `".$item['y_product_id'].' '.$item['y_name']."' from `".$item['y_laboratory']."' failed");
                }
        }
}



/*
 * Reduce inventory depending on the production stage.
 *  Stage 3+, after brew, reduce sugars(0-mash, 1-boil), hops(0-mash, 1-fwh, 2-boil, 3-aroma, 4-whirlpool), miscs(0-starter, 1-mash, 2-boil)
 *  Stage 4+, after primary, reduce sugars(2-fermention), yeasts(0-Primary), miscs(3-primary)
 *  Stage 5+, after secondary, reduce yeasts(1-Secondary)
 *  Stage 6+, after tertiary, reduce sugars(3-lagering), hops(5-dry-hop), yeasts(2-Tertiary), miscs(4-secondary)
 *  Stage 7+, after packaging, reduce sugars(4-bottle, 5-kegs), yeasts(3-Bottle), miscs(5-bottling)
 */
function inventory_reduce() {

    global $connect;

    $savethis = 0;
    $stage = $_POST['stage'];
    $inventory_reduced = $_POST['inventory_reduced'];
    if ($stage == $inventory_reduced) {
	return;
    }
    syslog(LOG_NOTICE, "inventory_reduce() stage: ".$stage." inventory_reduced: ".$inventory_reduced);

    /*
     * If the brew is done, reduce the used ingredients.
     */
    if (($stage >= 3) && ($inventory_reduced < 3)) {
	syslog(LOG_NOTICE, "Reduce brew inventory from " . $_POST['code'] . " " . $_POST['name']);

	if (isset($_POST['fermentables'])) {
            $array = $_POST['fermentables'];
            foreach($array as $key => $item) {
	    	if ($item['f_added'] <= 1) {	// Mash, Boil
		    reduce_fermentables($item);
	    	}
	    }
	}

	if (isset($_POST['hops'])) {
            $array = $_POST['hops'];
            foreach($array as $key => $item) {
	    	if ($item['h_useat'] <= 4) { // Mash, FWH, Boil, Flameout, Whirlpool
		    reduce_hops($item);
		}
	    }
	}

	if (isset($_POST['miscs'])) {
            $array = $_POST['miscs'];
            foreach($array as $key => $item) {
	    	if ($item['m_use_use'] <= 2) {	// Starter, Mash, Boil
		    reduce_miscs($item);
		}
	    }
	}

	if ($_POST['w1_name'] != '') {
            $sql2  = "UPDATE inventory_waters SET inventory = inventory - ".$_POST['w1_amount'];
            $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w1_name']);
            $sql2 .= "' AND unlimited_stock=0 AND inventory >= ".$_POST['w1_amount']." LIMIT 1;";
            $result2 = mysqli_query($connect, $sql2);
            $ar = mysqli_affected_rows($connect);
            if ($ar == 1) {
                syslog(LOG_NOTICE, "Reduced water `".$_POST['w1_name']."' with ".$_POST['w1_amount']." liter");
            } else if ($ar == 0) {
                $sql2  = "UPDATE inventory_waters SET inventory = 0";
                $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w1_name']);
                $sql2 .= "' AND unlimited_stock=0 AND inventory < ".$_POST['w1_amount']." LIMIT 1;";
                $result2 = mysqli_query($connect, $sql2);
                $ar = mysqli_affected_rows($connect);
                if ($ar == 1) {
                    syslog(LOG_NOTICE, "Reduced water `".$_POST['w1_name']."' to 0 liters");
                } else {
                    syslog(LOG_NOTICE, "Reduce water `".$_POST['w1_name']."' not reduced is maybe tapwater");
                }
            }
        }
	if ($_POST['w2_name'] != '') {
            $sql2  = "UPDATE inventory_waters SET inventory = inventory - ".$_POST['w2_amount'];
            $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w2_name']);
            $sql2 .= "' AND unlimited_stock=0 AND inventory >= ".$_POST['w2_amount']." LIMIT 1;";
            $result2 = mysqli_query($connect, $sql2);
            $ar = mysqli_affected_rows($connect);
            if ($ar == 1) {
                syslog(LOG_NOTICE, "Reduced water `".$_POST['w2_name']."' with ".$_POST['w2_amount']." liter");
            } else if ($ar == 0) {
                $sql2  = "UPDATE inventory_waters SET inventory = 0";
                $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w2_name']);
                $sql2 .= "' AND unlimited_stock=0 AND inventory < ".$_POST['w2_amount']." LIMIT 1;";
                $result2 = mysqli_query($connect, $sql2);
                $ar = mysqli_affected_rows($connect);
                if ($ar == 1) {
                    syslog(LOG_NOTICE, "Reduced water `".$_POST['w2_name']."' to 0 liters");
		} else {
                    syslog(LOG_NOTICE, "Reduce water `".$_POST['w2_name']."' failed");
                }
            }
        }

	$inventory_reduced = 3;
	$savethis = 1;
    }

    /*
     * After the Primary fermentation
     */
    if (($stage >= 4) && ($inventory_reduced < 4)) {
        syslog(LOG_NOTICE, "Reduce Primary inventory from " . $_POST['code'] . " " . $_POST['name']);

	if (isset($_POST['fermentables'])) {
            $array = $_POST['fermentables'];
            foreach($array as $key => $item) {
            	if ($item['f_added'] == 2) {    // Fermentation
		    reduce_fermentables($item);
		}
            }
        }

	if (isset($_POST['miscs'])) {
            $array = $_POST['miscs'];
            foreach($array as $key => $item) {
            	if ($item['m_use_use'] == 3) { // Fermentation
		    reduce_miscs($item);
		}
            }
        }

	if (isset($_POST['yeasts'])) {
            $array = $_POST['yeasts'];
            foreach($array as $key => $item) {
	    	if ($item['y_use'] == 0) {	// Primary
		    reduce_yeast($item);
		}
	    }
	}

        $inventory_reduced = 4;
	$savethis = 1;
    }


    /*
     * After the Seconday fermentation
     */
    if (($stage >= 5) && ($inventory_reduced < 5)) {
        syslog(LOG_NOTICE, "Reduce Secondary inventory from " . $_POST['code'] . " " . $_POST['name']);

	if (isset($_POST['yeasts'])) {
            $array = $_POST['yeasts'];
            foreach($array as $key => $item) {
                if ($item['y_use'] == 1) {      // Secondary
                    reduce_yeast($item);
                }
            }
        }

        $inventory_reduced = 5;
	$savethis = 1;
    }


    /*
     * After the Tertiary fermentation
     */
    if (($stage >= 6) && ($inventory_reduced < 6)) {
        syslog(LOG_NOTICE, "Reduce Tertiary inventory from " . $_POST['code'] . " " . $_POST['name']);

	if (isset($_POST['fermentables'])) {
            $array = $_POST['fermentables'];
            foreach($array as $key => $item) {
                if ($item['f_added'] == 3) {    // Lagering
                    reduce_fermentables($item);
                }
            }
        }

	if (isset($_POST['hops'])) {
            $array = $_POST['hops'];
            foreach($array as $key => $item) {
                if ($item['h_useat'] == 5) { // Dry hop
                    reduce_hops($item);
                }
            }
        }

	if (isset($_POST['yeasts'])) {
            $array = $_POST['yeasts'];
            foreach($array as $key => $item) {
                if ($item['y_use'] == 2) {      // Tertiary
                    reduce_yeast($item);
                }
            }
        }

	if (isset($_POST['miscs'])) {
            $array = $_POST['miscs'];
            foreach($array as $key => $item) {
                if ($item['m_use_use'] == 4) { // Secondary or Tertiary
                    reduce_miscs($item);
                }
            }
        }

        $inventory_reduced = 6;
	$savethis = 1;
    }

    /*
     * After packaging
     *  reduce sugars(4/5-bottle), yeasts(3-Bottle), miscs(5-bottling)
     */
    if (($stage >= 6) && ($inventory_reduced < 7)) {
        syslog(LOG_NOTICE, "Reduce Packaging inventory from " . $_POST['code'] . " " . $_POST['name']);

	if (isset($_POST['fermentables'])) {
            $array = $_POST['fermentables'];
            foreach($array as $key => $item) {
                if ($item['f_added'] >= 4) {    // Bottling or Kegging
                    reduce_fermentables($item);
                }
            }
        }

	if (isset($_POST['yeasts'])) {
            $array = $_POST['yeasts'];
            foreach($array as $key => $item) {
                if ($item['y_use'] == 3) {      // Bottle
                    reduce_yeast($item);
                }
            }
        }

	if (isset($_POST['miscs'])) {
            $array = $_POST['miscs'];
            foreach($array as $key => $item) {
                if ($item['m_use_use'] == 5) { // Bottle
                    reduce_miscs($item);
                }
            }
        }

	if ($stage < 7)
                $stage = 7;
        $inventory_reduced = $stage;
	$savethis = 1;
    }

    /*
     * Save only if something was reduced.
     */
    if ($savethis == 1) {
    	$sql2 = "UPDATE products SET stage=".$stage.", inventory_reduced=".$inventory_reduced." WHERE record='".$_POST['record']."';";
    	syslog(LOG_NOTICE, $sql2);
    	$result2 = mysqli_query($connect, $sql2);
    	$ar = mysqli_affected_rows($connect);
	if ($ar != 1) {
    	    syslog(LOG_NOTICE, $sql2." error, affected rows: ".$ar);
	}
    }
}


?>

mercurial