Set recipe edit values readonly when the brewday is over. Add the inventory reduce script and update the checkbox. Active after packaging.

Wed, 13 Feb 2019 20:28:54 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 13 Feb 2019 20:28:54 +0100
changeset 273
19606adfea42
parent 272
0c2ea1c2b858
child 274
65ca75fc4af8

Set recipe edit values readonly when the brewday is over. Add the inventory reduce script and update the checkbox. Active after packaging.

www/js/prod_edit.js file | annotate | diff | comparison | revisions
www/prod_reduce.php file | annotate | diff | comparison | revisions
--- a/www/js/prod_edit.js	Mon Feb 11 22:56:58 2019 +0100
+++ b/www/js/prod_edit.js	Wed Feb 13 20:28:54 2019 +0100
@@ -1385,6 +1385,37 @@
 			$('#jqxTabs').jqxTabs('disableAt', 9);	// Fermentation tab
 		} else {
 			$('#jqxTabs').jqxTabs('enableAt', 9);
+			$("#batch_size").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#boil_size").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#boil_time").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#efficiency").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#est_og").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#type").jqxDropDownList({ disabled: true });
+			$("#styleSelect").jqxDropDownList({ disabled: true });
+			$("#color_method").jqxDropDownList({ disabled: true });
+			$("#ibu_method").jqxDropDownList({ disabled: true });
+			$("#mash_select").jqxDropDownList({ disabled: true });
+			$("#w1_name").jqxDropDownList({ disabled: true });
+			$("#w2_name").jqxDropDownList({ disabled: true });
+			$("#w2_amount").jqxNumberInput({ readOnly: true });
+			$("#pr_name").jqxDropDownList({ disabled: true });
+			$("#wa_cacl2").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#wa_caso4").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#wa_mgso4").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#wa_nacl").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#mash_ph").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#calc_acid").jqxCheckBox({ disabled: true });
+			$("#wa_base_name").jqxDropDownList({ disabled: true });
+			$("#wa_base").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#wa_acid_name").jqxDropDownList({ disabled: true });
+			$("#wa_acid").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#wa_acid_perc").jqxNumberInput({ spinButtons: false, readOnly: true, width: 70 });
+			$("#sparge_temp").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#sparge_volume").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#sparge_ph").jqxNumberInput({ spinButtons: false, readOnly: true, width: 90 });
+			$("#sparge_source").jqxDropDownList({ disabled: true });
+			$("#sparge_acid_type").jqxDropDownList({ disabled: true });
+			$("#sparge_acid_perc").jqxNumberInput({ spinButtons: false, readOnly: true, width: false });
 		}
 		if (dataRecord.stage < 5) { // At least secondary
 			$('#jqxTabs').jqxTabs('disableAt', 10);	// Packaging tab
@@ -2392,6 +2423,7 @@
 					template: "primary",
                                         source: fermentablelist,
                                         displayMember: "name",
+					disabled: (dataRecord.stage > 3),
                                         width: 150,
                                         height: 27,
                                         dropDownWidth: 500,
@@ -2446,14 +2478,14 @@
                                         }
                                 });
 
-                                $("#finstockbutton").jqxCheckBox({ theme: theme, height: 27 });
+                                $("#finstockbutton").jqxCheckBox({ theme: theme, height: 27, disabled: (dataRecord.stage > 3) });
                                 $("#finstockbutton").on('change', function (event) {
                                         fermentableinstock = event.args.checked;
                                         fermentablelist.dataBind();
                                 });
 
                                 // delete selected fermentable.
-                                $("#fdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150 });
+                                $("#fdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150, disabled: (dataRecord.stage > 3) });
                                 $("#fdeleterowbutton").on('click', function () {
                                         var selectedrowindex = $("#fermentableGrid").jqxGrid('getselectedrowindex');
                                         var rowscount = $("#fermentableGrid").jqxGrid('getdatainformation').rowscount;
@@ -2522,15 +2554,17 @@
 				{ text: 'Wijzig', datafield: 'Edit', columntype: 'button', width: 100, align: 'center', cellsrenderer: function () {
 					return "Wijzig";
 					}, buttonclick: function (row) {
-						fermentableRow = row;
-						fermentableData = $("#fermentableGrid").jqxGrid('getrowdata', fermentableRow);
-						$("#wf_name").val(fermentableData.f_name);
-						$("#wf_amount").val(fermentableData.f_amount);
-						$("#wf_percentage").val(fermentableData.f_percentage);
-						$("#wf_adjust_to_total_100").val(fermentableData.f_adjust_to_total_100);
-						$("#wf_added").val(fermentableData.f_added);
-						// show the popup window.
-						$("#popupFermentable").jqxWindow('open');
+						if (dataRecord.stage <= 3) {
+							fermentableRow = row;
+							fermentableData = $("#fermentableGrid").jqxGrid('getrowdata', fermentableRow);
+							$("#wf_name").val(fermentableData.f_name);
+							$("#wf_amount").val(fermentableData.f_amount);
+							$("#wf_percentage").val(fermentableData.f_percentage);
+							$("#wf_adjust_to_total_100").val(fermentableData.f_adjust_to_total_100);
+							$("#wf_added").val(fermentableData.f_added);
+							// show the popup window.
+							$("#popupFermentable").jqxWindow('open');
+						}
 					}
 				}
                         ]
@@ -2594,6 +2628,7 @@
                                         theme: theme,
 					template: "primary",
                                         source: hoplist,
+					disabled: (dataRecord.stage > 3),
                                         displayMember: "name",
                                         width: 150,
                                         height: 27,
@@ -2631,14 +2666,14 @@
 					$("#haddrowbutton").jqxDropDownList('clearSelection');
                                 });
 
-                                $("#hinstockbutton").jqxCheckBox({ theme: theme, height: 27 });
+                                $("#hinstockbutton").jqxCheckBox({ theme: theme, height: 27, disabled: (dataRecord.stage > 3) });
                                 $("#hinstockbutton").on('change', function (event) {
                                         hopinstock = event.args.checked;
                                         hoplist.dataBind();
                                 });
 
                                 // delete selected hop.
-                                $("#hdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150 });
+                                $("#hdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150, disabled: (dataRecord.stage > 3) });
                                 $("#hdeleterowbutton").on('click', function () {
                                         var selectedrowindex = $("#hopGrid").jqxGrid('getselectedrowindex');
                                         var rowscount = $("#hopGrid").jqxGrid('getdatainformation').rowscount;
@@ -2720,23 +2755,25 @@
 				{ text: 'Wijzig', datafield: 'Edit', columntype: 'button', width: 100, align: 'center', cellsrenderer: function () {
 					return "Wijzig";
 					}, buttonclick: function (row) {
-						hopRow = row;
-						hopData = $("#hopGrid").jqxGrid('getrowdata', hopRow);
-						$("#wh_name").val(hopData.h_name);
-						$("#wh_amount").val(hopData.h_amount * 1000);
-						var ibu = toIBU(hopData.h_useat, hopData.h_form, preboil_sg,
-							parseFloat($("#batch_size").jqxNumberInput('decimal')),
-							parseFloat(hopData.h_amount), parseFloat(hopData.h_time),
-							parseFloat(hopData.h_alpha), $("#ibu_method").val()
-						);
-						$("#wh_ibu").val(ibu);
-						if (hopData.h_useat == 5)       // Dry hop
-							$("#wh_time").val(hopData.h_time / 1440);
-						else
-							$("#wh_time").val(hopData.h_time);
-						$("#wh_useat").val(hopData.h_useat);
-						// show the popup window.
-						$("#popupHop").jqxWindow('open');
+						if (dataRecord.stage <= 3) {
+							hopRow = row;
+							hopData = $("#hopGrid").jqxGrid('getrowdata', hopRow);
+							$("#wh_name").val(hopData.h_name);
+							$("#wh_amount").val(hopData.h_amount * 1000);
+							var ibu = toIBU(hopData.h_useat, hopData.h_form, preboil_sg,
+								parseFloat($("#batch_size").jqxNumberInput('decimal')),
+								parseFloat(hopData.h_amount), parseFloat(hopData.h_time),
+								parseFloat(hopData.h_alpha), $("#ibu_method").val()
+							);
+							$("#wh_ibu").val(ibu);
+							if (hopData.h_useat == 5)       // Dry hop
+								$("#wh_time").val(hopData.h_time / 1440);
+							else
+								$("#wh_time").val(hopData.h_time);
+							$("#wh_useat").val(hopData.h_useat);
+							// show the popup window.
+							$("#popupHop").jqxWindow('open');
+						}
 					}
 				}
                         ]
@@ -2850,6 +2887,7 @@
                                         theme: theme,
 					template: "primary",
                                         source: misclist,
+					disabled: (dataRecord.stage > 3),
                                         displayMember: "name",
                                         width: 150,
                                         height: 27,
@@ -2872,13 +2910,13 @@
                                                 var commit = $("#miscGrid").jqxGrid('addrow', null, row);
                                         }
                                 });
-                                $("#minstockbutton").jqxCheckBox({ theme: theme, height: 27 });
+                                $("#minstockbutton").jqxCheckBox({ theme: theme, height: 27, disabled: (dataRecord.stage > 3) });
                                 $("#minstockbutton").on('change', function (event) {
                                         miscinstock = event.args.checked;
                                         misclist.dataBind();
                                 });
                                 // delete selected misc.
-                                $("#mdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150 });
+                                $("#mdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150, disabled: (dataRecord.stage > 3) });
                                 $("#mdeleterowbutton").on('click', function () {
                                         var selectedrowindex = $("#miscGrid").jqxGrid('getselectedrowindex');
                                         var rowscount = $("#miscGrid").jqxGrid('getdatainformation').rowscount;
@@ -2931,22 +2969,24 @@
 				{ text: 'Wijzig', datafield: 'Edit', columntype: 'button', width: 100, align: 'center', cellsrenderer: function () {
 					return "Wijzig";
 					}, buttonclick: function (row) {
-						miscRow = row;
-						miscData = $("#miscGrid").jqxGrid('getrowdata', miscRow);
-						if (miscData.m_amount_is_weight)
-							$("#wm_pmpt_amount").html("Gewicht gram:");
-						else
-							$("#wm_pmpt_amount").html("Volume ml:");
-						$("#wm_name").val(miscData.m_name);
-						$("#wm_amount").val(miscData.m_amount * 1000);
-						if ((miscData.m_use_use == 3) || (miscData.m_use_use == 4))     // Primary or Secondary
-							$("#wm_time").val(miscData.m_time / 1440);
-						else
-							$("#wm_time").val(miscData.m_time);
-						$("#wm_use_use").val(miscData.m_use_use);
-						// show the popup window.
-						if (miscData.m_type != 4)
-							$("#popupMisc").jqxWindow('open');
+						if (dataRecord.stage <= 3) {
+							miscRow = row;
+							miscData = $("#miscGrid").jqxGrid('getrowdata', miscRow);
+							if (miscData.m_amount_is_weight)
+								$("#wm_pmpt_amount").html("Gewicht gram:");
+							else
+								$("#wm_pmpt_amount").html("Volume ml:");
+							$("#wm_name").val(miscData.m_name);
+							$("#wm_amount").val(miscData.m_amount * 1000);
+							if ((miscData.m_use_use == 3) || (miscData.m_use_use == 4))     // Primary or Secondary
+								$("#wm_time").val(miscData.m_time / 1440);
+							else
+								$("#wm_time").val(miscData.m_time);
+							$("#wm_use_use").val(miscData.m_use_use);
+							// show the popup window.
+							if (miscData.m_type != 4)
+								$("#popupMisc").jqxWindow('open');
+						}
 					}
 				}
                         ]
@@ -3006,6 +3046,7 @@
                                         placeHolder: "Kies gist:",
                                         theme: theme,
                                         source: yeastlist,
+					disabled: (dataRecord.stage > 3),
 					template: "primary",
                                         displayMember: "name",
                                         width: 150,
@@ -3040,13 +3081,13 @@
                                         }
 					$("#yaddrowbutton").jqxDropDownList('clearSelection');
                                 });
-                                $("#yinstockbutton").jqxCheckBox({ theme: theme, height: 27 });
+                                $("#yinstockbutton").jqxCheckBox({ theme: theme, height: 27, disabled: (dataRecord.stage > 3) });
                                 $("#yinstockbutton").on('change', function (event) {
                                         yeastinstock = event.args.checked;
                                         yeastlist.dataBind();
                                 });
                                 // delete selected yeast.
-                                $("#ydeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150 });
+                                $("#ydeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150, disabled: (dataRecord.stage > 3) });
                                 $("#ydeleterowbutton").on('click', function () {
                                         var selectedrowindex = $("#yeastGrid").jqxGrid('getselectedrowindex');
                                         var rowscount = $("#yeastGrid").jqxGrid('getdatainformation').rowscount;
@@ -3102,27 +3143,29 @@
 				{ text: 'Wijzig', datafield: 'Edit', columntype: 'button', width: 100, align: 'center', cellsrenderer: function () {
 					return "Wijzig";
 					}, buttonclick: function (row) {
-						yeastRow = row;
-						yeastData = $("#yeastGrid").jqxGrid('getrowdata', yeastRow);
-						if (yeastData.y_form == 0) {
-							$("#wy_pmpt_amount").html("Pak(ken):");
-							$("#wy_amount").val(yeastData.y_amount);
-							$("#wy_amount").jqxNumberInput({ decimalDigits: 0 });
-						} else if (yeastData.y_form == 1) {
-							$("#wy_pmpt_amount").html("Gewicht gram:");
-							$("#wy_amount").val(yeastData.y_amount * 1000);
-							$("#wy_amount").jqxNumberInput({ decimalDigits: 1 });
-						} else {
-							$("#wy_pmpt_amount").html("Volume ml:");
-							$("#wy_amount").val(yeastData.y_amount * 1000);
-							$("#wy_amount").jqxNumberInput({ decimalDigits: 0 });
+						if (dataRecord.stage <= 3) {
+							yeastRow = row;
+							yeastData = $("#yeastGrid").jqxGrid('getrowdata', yeastRow);
+							if (yeastData.y_form == 0) {
+								$("#wy_pmpt_amount").html("Pak(ken):");
+								$("#wy_amount").val(yeastData.y_amount);
+								$("#wy_amount").jqxNumberInput({ decimalDigits: 0 });
+							} else if (yeastData.y_form == 1) {
+								$("#wy_pmpt_amount").html("Gewicht gram:");
+								$("#wy_amount").val(yeastData.y_amount * 1000);
+								$("#wy_amount").jqxNumberInput({ decimalDigits: 1 });
+							} else {
+								$("#wy_pmpt_amount").html("Volume ml:");
+								$("#wy_amount").val(yeastData.y_amount * 1000);
+								$("#wy_amount").jqxNumberInput({ decimalDigits: 0 });
+							}
+							$("#wy_name").val(yeastData.y_name);
+							$("#wy_laboratory").val(yeastData.y_laboratory);
+							$("#wy_product_id").val(yeastData.y_product_id);
+							$("#wy_use").val(yeastData.y_use);
+							// show the popup window.
+							$("#popupYeast").jqxWindow('open');
 						}
-						$("#wy_name").val(yeastData.y_name);
-						$("#wy_laboratory").val(yeastData.y_laboratory);
-						$("#wy_product_id").val(yeastData.y_product_id);
-						$("#wy_use").val(yeastData.y_use);
-						// show the popup window.
-						$("#popupYeast").jqxWindow('open');
 					}
 				}
                         ]
@@ -3188,13 +3231,13 @@
                                 toolbar.append(container);
                                 container.append('<input style="float: left; margin-left: 165px;" id="saddrowbutton" type="button" value="Nieuwe stap" />');
                                 container.append('<input style="float: left; margin-left: 565px;" id="sdeleterowbutton" type="button" value="Verwijder stap" />');
-                                $("#saddrowbutton").jqxButton({ template: "primary", theme: theme, height: 27, width: 150 });
+                                $("#saddrowbutton").jqxButton({ template: "primary", theme: theme, height: 27, width: 150, disabled: (dataRecord.stage > 3) });
                                 $("#saddrowbutton").on('click', function () {
                                         var datarow = generaterow();
                                         var commit = $("#mashGrid").jqxGrid('addrow', null, datarow);
                                 });
                                 // delete selected step.
-                                $("#sdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150 });
+                                $("#sdeleterowbutton").jqxButton({ template: "danger", theme: theme, height: 27, width: 150, disabled: (dataRecord.stage > 3) });
                                 $("#sdeleterowbutton").on('click', function () {
                                         var selectedrowindex = $("#mashGrid").jqxGrid('getselectedrowindex');
                                         var rowscount = $("#mashGrid").jqxGrid('getdatainformation').rowscount;
@@ -3224,24 +3267,26 @@
 				{ text: 'Wijzig', datafield: 'Edit', columntype: 'button', width: 100, align: 'center', cellsrenderer: function () {
 					return "Wijzig";
 					}, buttonclick: function (row) {
-						mashRow = row;
-						mashData = $("#mashGrid").jqxGrid('getrowdata', mashRow);
-						$("#wstep_name").val(mashData.step_name);
-						$("#wstep_type").val(mashData.step_type);
-						$("#wstep_infuse_amount").val(mashData.step_infuse_amount);
-						$("#wstep_temp").val(mashData.step_temp);
-						$("#wend_temp").val(mashData.end_temp);
-						$("#wstep_time").val(mashData.step_time);
-						$("#wramp_time").val(mashData.ramp_time);
-						if (mashData.step_type == 0) {
-							$("#wstep_infuse_amount").show();
-							$("#wstep_pmpt").show();
-						} else {
-							$("#wstep_infuse_amount").hide();
-							$("#wstep_pmpt").hide();
+						if (dataRecord.stage <= 3) {
+							mashRow = row;
+							mashData = $("#mashGrid").jqxGrid('getrowdata', mashRow);
+							$("#wstep_name").val(mashData.step_name);
+							$("#wstep_type").val(mashData.step_type);
+							$("#wstep_infuse_amount").val(mashData.step_infuse_amount);
+							$("#wstep_temp").val(mashData.step_temp);
+							$("#wend_temp").val(mashData.end_temp);
+							$("#wstep_time").val(mashData.step_time);
+							$("#wramp_time").val(mashData.ramp_time);
+							if (mashData.step_type == 0) {
+								$("#wstep_infuse_amount").show();
+								$("#wstep_pmpt").show();
+							} else {
+								$("#wstep_infuse_amount").hide();
+								$("#wstep_pmpt").hide();
+							}
+							// show the popup window.
+							$("#popupMash").jqxWindow('open');
 						}
-						// show the popup window.
-						$("#popupMash").jqxWindow('open');
 					}
 				}
                         ]
@@ -3260,11 +3305,26 @@
 	$("#stage").jqxInput({ theme: theme, width: 100, height: 23 });
 	$("#inventory_reduced").jqxCheckBox({ theme: theme, width: 120, height: 23 });
 	$('#inventory_reduced').on('checked', function (event) {
-		// Call a script to do the work and block this.
-		// Note that this script must set this flag too, so if the user doesn't Save it is still set.
-		// Call the script with the uuid.
-		// set dataRecord.inventory_reduced to 1
-		$("#inventory_reduced").jqxCheckBox({ disabled : true });
+		if (dataRecord.inventory_reduced == 0) {
+			saveRecord();
+			var data = "reduce=1&uuid="+dataRecord.uuid+"&record="+my_record;
+			var url = "prod_reduce.php";
+			$.ajax({
+				dataType: 'json',
+				url: url,
+				cache: false,
+				data: data,
+				type: "POST",
+				success: function (data, status, xhr) {
+					console.log("success");
+					window.location.href = my_return;
+				},
+				error: function(jqXHR, textStatus, errorThrown) {
+					console.log("error");
+					window.location.href = my_return;
+				}
+			});
+		}
 	});
 	$("#locked").jqxCheckBox({ theme: theme, width: 120, height: 23, disabled : true });
 	$('#locked').on('checked', function (event) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/prod_reduce.php	Wed Feb 13 20:28:54 2019 +0100
@@ -0,0 +1,211 @@
+<?php
+
+require_once('config.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" );
+
+if (isset($_POST['reduce']) && isset($_POST['uuid'])) {
+	$sql1 = "SELECT * FROM products WHERE uuid = '" . $_POST['uuid'] . "';";
+} else {
+	exit;
+}
+
+//syslog(LOG_NOTICE, $sql1);
+
+$result1 = mysqli_query($connect, $sql1) or die("SQL Error 1: " . mysqli_error($connect));
+while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
+
+	if ($row['stage'] < 6) {
+		syslog(LOG_NOTICE, "code: " . $row['code'] . " cannot reduce inventory, not yet packaged.");
+		exit;
+	}
+	if ($row['inventory_reduced']) {
+		syslog(LOG_NOTICE, "code: " . $row['code'] . " cannot reduce inventory, already reduced.");
+		exit;
+	}
+
+	syslog(LOG_NOTICE, "Start reducing inventory from ". $row['code'].' '.$row['name']);
+
+	$fermentables = json_decode($row['json_fermentables'], true);
+	for ($i = 0; $i < count($fermentables); $i++) {
+
+		$sql2  = "UPDATE inventory_fermentables SET inventory = inventory - " . $fermentables[$i]['f_amount'];
+		$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $fermentables[$i]['f_name']);
+	        $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $fermentables[$i]['f_origin']);
+		$sql2 .= "' AND supplier='" . mysqli_real_escape_string($connect, $fermentables[$i]['f_supplier']);
+	        $sql2 .= "' AND inventory >= " . $fermentables[$i]['f_amount'];
+		$sql2 .= " LIMIT 1;";
+//		syslog(LOG_NOTICE, $sql2);
+		$result2 = mysqli_query($connect, $sql2);
+		$ar = mysqli_affected_rows($connect);
+		if ($ar == 1) {
+			syslog(LOG_NOTICE, "Reduced fermentable `".$fermentables[$i]['f_name']."' from `".$fermentables[$i]['f_supplier']."' with ".$fermentables[$i]['f_amount']." kg");
+		} else if ($ar == 0) {
+			$sql2  = "UPDATE inventory_fermentables SET inventory = 0";
+			$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $fermentables[$i]['f_name']);
+		        $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $fermentables[$i]['f_origin']);
+			$sql2 .= "' AND supplier='" . mysqli_real_escape_string($connect, $fermentables[$i]['f_supplier']);
+		        $sql2 .= "' AND inventory < " . $fermentables[$i]['f_amount'];
+			$sql2 .= " LIMIT 1;";
+//			syslog(LOG_NOTICE, $sql2);
+			$result2 = mysqli_query($connect, $sql2);
+			$ar = mysqli_affected_rows($connect);
+			if ($ar == 1) {
+				syslog(LOG_NOTICE, "Reduced fermentable `".$fermentables[$i]['f_name']."' from `".$fermentables[$i]['f_supplier']."' to 0 kg");
+			} else if ($ar == 0) {
+				syslog(LOG_NOTICE, "Reduced fermentable `".$fermentables[$i]['f_name']."' from `".$fermentables[$i]['f_supplier']."' failed");
+			}
+//			syslog(LOG_NOTICE, "affected rows: ".$ar);
+		}
+	}
+
+	$hops = json_decode($row['json_hops'], true);
+	for ($i = 0; $i < count($hops); $i++) {
+
+		$sql2  = "UPDATE inventory_hops SET inventory = inventory - " . $hops[$i]['h_amount'];
+		$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $hops[$i]['h_name']);
+	        $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $hops[$i]['h_origin']);
+		$sql2 .= "' AND form=" . $hops[$i]['h_form'];
+		$sql2 .= " AND inventory >= " . $hops[$i]['h_amount'] . " LIMIT 1;";
+//		syslog(LOG_NOTICE, $sql2);
+		$result2 = mysqli_query($connect, $sql2);
+		$ar = mysqli_affected_rows($connect);
+		if ($ar == 1) {
+			syslog(LOG_NOTICE, "Reduced hop `".$hops[$i]['h_name']."' from `".$hops[$i]['h_origin']."' with ".$hops[$i]['h_amount']." kg");
+		} else if ($ar == 0) {
+			$sql2  = "UPDATE inventory_hops SET inventory = 0";
+			$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $hops[$i]['h_name']);
+		        $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $hops[$i]['h_origin']);
+			$sql2 .= "' AND form=" . $hops[$i]['h_form'];
+			$sql2 .= " AND inventory < " . $hops[$i]['h_amount'] . " LIMIT 1;";
+//			syslog(LOG_NOTICE, $sql2);
+			$result2 = mysqli_query($connect, $sql2);
+			$ar = mysqli_affected_rows($connect);
+			if ($ar == 1) {
+				syslog(LOG_NOTICE, "Reduced hop `".$hops[$i]['h_name']."' from `".$hops[$i]['h_origin']."' to 0 kg");
+			} else if ($ar == 0) {
+				syslog(LOG_NOTICE, "Reduced hop `".$hops[$i]['h_name']."' from `".$hops[$i]['h_origin']."' failed");
+			}
+		}
+	}
+
+	$miscs = json_decode($row['json_miscs'], true);
+	for ($i = 0; $i < count($miscs); $i++) {
+
+		$sql2  = "UPDATE inventory_miscs SET inventory = inventory - " . $miscs[$i]['m_amount'];
+		$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $miscs[$i]['m_name']);
+	        $sql2 .= "' AND inventory >= " . $miscs[$i]['m_amount'] . " LIMIT 1;";
+//		syslog(LOG_NOTICE, $sql2);
+		$result2 = mysqli_query($connect, $sql2);
+		$ar = mysqli_affected_rows($connect);
+		if ($ar == 1) {
+			syslog(LOG_NOTICE, "Reduced misc `".$miscs[$i]['m_name']."' with ".$miscs[$i]['m_amount']);
+		} else if ($ar == 0) {
+			$sql2  = "UPDATE inventory_miscs SET inventory = 0";
+			$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $miscs[$i]['m_name']);
+		        $sql2 .= "' AND inventory < " . $miscs[$i]['m_amount'] . " LIMIT 1;";
+//			syslog(LOG_NOTICE, $sql2);
+			$result2 = mysqli_query($connect, $sql2);
+			$ar = mysqli_affected_rows($connect);
+			if ($ar == 1) {
+				syslog(LOG_NOTICE, "Reduced misc `".$miscs[$i]['m_name']."' to 0");
+			} else if ($ar == 0) {
+				syslog(LOG_NOTICE, "Reduced misc `".$miscs[$i]['m_name']."' failed");
+			}
+		}
+	}
+
+	$yeasts = json_decode($row['json_yeasts'], true);
+	for ($i = 0; $i < count($yeasts); $i++) {
+
+		$sql2  = "UPDATE inventory_yeasts SET inventory = inventory - " . $yeasts[$i]['y_amount'];
+		$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $yeasts[$i]['y_name']);
+	        $sql2 .= "' AND laboratory='" . mysqli_real_escape_string($connect, $yeasts[$i]['y_laboratory']);
+		$sql2 .= "' AND product_id='" . mysqli_real_escape_string($connect, $yeasts[$i]['y_product_id']);
+	        $sql2 .= "' AND form=" . $yeasts[$i]['y_form'];
+		$sql2 .= " AND inventory >= " . $yeasts[$i]['y_amount'] . " LIMIT 1;";
+//		syslog(LOG_NOTICE, $sql2);
+		$result2 = mysqli_query($connect, $sql2);
+		$ar = mysqli_affected_rows($connect);
+		if ($ar == 1) {
+			syslog(LOG_NOTICE, "Reduced yeast `".$yeasts[$i]['y_product_id'].' '.$yeasts[$i]['y_name']."' from `".$yeasts[$i]['y_laboratory']."' with ".$yeasts[$i]['y_amount']);
+		} else if ($ar == 0) {
+			$sql2  = "UPDATE inventory_yeasts SET inventory = 0";
+			$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $yeasts[$i]['y_name']);
+		        $sql2 .= "' AND laboratory='" . mysqli_real_escape_string($connect, $yeasts[$i]['y_laboratory']);
+			$sql2 .= "' AND product_id='" . mysqli_real_escape_string($connect, $yeasts[$i]['y_product_id']);
+		        $sql2 .= "' AND form=" . $yeasts[$i]['y_form'];
+			$sql2 .= " AND inventory < " . $yeasts[$i]['y_amount'] . " LIMIT 1;";
+//			syslog(LOG_NOTICE, $sql2);
+			$result2 = mysqli_query($connect, $sql2);
+			$ar = mysqli_affected_rows($connect);
+			if ($ar == 1) {
+				syslog(LOG_NOTICE, "Reduced yeast `".$yeasts[$i]['y_product_id'].' '.$yeasts[$i]['y_name']."' from `".$yeasts[$i]['y_laboratory']."' to 0");
+			} else if ($ar == 0) {
+				syslog(LOG_NOTICE, "Reduced yeast `".$yeasts[$i]['y_product_id'].' '.$yeasts[$i]['y_name']."' from `".$yeasts[$i]['y_laboratory']."' failed");
+			}
+		}
+	}
+
+	if ($row['w1_name'] != '') {
+		$sql2  = "UPDATE inventory_waters SET inventory = inventory - ".$row['w1_amount'];
+		$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $row['w1_name']);
+		$sql2 .= "' AND unlimited_stock=0 AND inventory >= ".$row['w1_amount']." LIMIT 1;";
+//		syslog(LOG_NOTICE, $sql2);
+		$result2 = mysqli_query($connect, $sql2);
+		$ar = mysqli_affected_rows($connect);
+		if ($ar == 1) {
+			syslog(LOG_NOTICE, "Reduced water `".$row['w1_name']."' with ".$row['w1_amount']." liter");
+		} else if ($ar == 0) {
+			$sql2  = "UPDATE inventory_waters SET inventory = 0";
+			$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $row['w1_name']);
+			$sql2 .= "' AND unlimited_stock=0 AND inventory < ".$row['w1_amount']." LIMIT 1;";
+//			syslog(LOG_NOTICE, $sql2);
+			$result2 = mysqli_query($connect, $sql2);
+			$ar = mysqli_affected_rows($connect);
+			if ($ar == 1) {
+				syslog(LOG_NOTICE, "Reduced water `".$row['w1_name']."' to 0 liters");
+			} else {
+				syslog(LOG_NOTICE, "Reduced water `".$row['w1_name']."' failed or tapwater");
+			}
+		}
+	}
+	if ($row['w2_name'] != '') {
+		$sql2  = "UPDATE inventory_waters SET inventory = inventory - ".$row['w2_amount'];
+		$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $row['w2_name']);
+		$sql2 .= "' AND unlimited_stock=0 AND inventory >= ".$row['w2_amount']." LIMIT 1;";
+//		syslog(LOG_NOTICE, $sql2);
+		$result2 = mysqli_query($connect, $sql2);
+		$ar = mysqli_affected_rows($connect);
+		if ($ar == 1) {
+			syslog(LOG_NOTICE, "Reduced water `".$row['w2_name']."' with ".$row['w2_amount']." liter");
+		} else if ($ar == 0) {
+			$sql2  = "UPDATE inventory_waters SET inventory = 0";
+			$sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $row['w2_name']);
+			$sql2 .= "' AND unlimited_stock=0 AND inventory < ".$row['w2_amount']." LIMIT 1;";
+//			syslog(LOG_NOTICE, $sql2);
+			$result2 = mysqli_query($connect, $sql2);
+			$ar = mysqli_affected_rows($connect);
+			if ($ar == 1) {
+				syslog(LOG_NOTICE, "Reduced water `".$row['w2_name']."' to 0 liters");
+			} else {
+				syslog(LOG_NOTICE, "Reduced water `".$row['w2_name']."' failed or tapwater");
+			}
+		}
+	}
+	
+	syslog(LOG_NOTICE, "Finished reducing inventory from ". $row['code'].' '.$row['name']);
+	mysqli_free_result($result1);
+
+	$sql1 = "UPDATE products SET inventory_reduced=1 WHERE uuid = '" . $_POST['uuid'] . "';";
+	//syslog(LOG_NOTICE, $sql1);
+	$result1 = mysqli_query($connect, $sql1);
+	$ar = mysqli_affected_rows($connect);
+}
+
+exit(0);

mercurial