With recipe import calculate the OG

Sun, 11 Nov 2018 17:53:33 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 11 Nov 2018 17:53:33 +0100
changeset 86
e977a505ea8c
parent 85
ca7a37586551
child 87
7f1d0abe5571

With recipe import calculate the OG

www/import/from_brouwhulp.php file | annotate | diff | comparison | revisions
www/rec_print.php file | annotate | diff | comparison | revisions
--- a/www/import/from_brouwhulp.php	Sun Nov 11 16:36:38 2018 +0100
+++ b/www/import/from_brouwhulp.php	Sun Nov 11 17:53:33 2018 +0100
@@ -449,6 +449,10 @@
 	$recipes= simplexml_load_file($brouwhulp . '/recipes.xml');
 
 	foreach ($recipes->RECIPE as $recipe) {
+		$f_sugars = 0;
+		$efficiency = 75;
+		$batch_size = 20;
+
 		$sql  = "INSERT INTO recipes SET name='" . mysqli_real_escape_string($db, $recipe->NAME);
 		if ($recipe->NOTES)
 			$sql .= "', notes='" . mysqli_real_escape_string($db, $recipe->NOTES);
@@ -459,21 +463,16 @@
 		else
 			$sql .= "', type='";
 		if ($recipe->BATCH_SIZE)
-			$sql .= "', batch_size='" . floatval($recipe->BATCH_SIZE);
-		else
-			$sql .= "', batch_size='20.0";
+			$batch_size = floatval($recipe->BATCH_SIZE);
+		$sql .= "', batch_size='" . $batch_size;
 		if ($recipe->BOIL_TIME)
 			$sql .= "', boil_time='" . floatval($recipe->BOIL_TIME);
 		else
 			$sql .= "', boil_time='90";
 		if ($recipe->EFFICIENCY)
-			$sql .= "', efficiency='" . floatval($recipe->EFFICIENCY);
-		else
-			$sql .= "', efficiency='75";
-		if ($recipe->EST_OG)
-			$sql .= "', est_og='" . floatval($recipe->EST_OG);
-		else
-			$sql .= "', est_og='1.052";
+			$efficiency = floatval($recipe->EFFICIENCY);
+		$sql .= "', efficiency='" . $efficiency;
+		/* Don't use $recipe->EST_OG but recalculate it */
 		if ($recipe->EST_FG)
 			$sql .= "', est_fg='" . floatval($recipe->EST_FG);
 		else
@@ -553,10 +552,12 @@
 				$fermentables .= '{"f_name":"' . mysqli_real_escape_string($db, $fermentable->NAME) . '"';
 				$fermentables .= ',"f_origin":"' . mysqli_real_escape_string($db, $fermentable->ORIGIN) . '"';
 				$fermentables .= ',"f_supplier":"' . mysqli_real_escape_string($db, $fermentable->SUPPLIER) . '"';
-				$fermentables .= ',"f_amount":' . floatval($fermentable->AMOUNT);
+				$famount = floatval($fermentable->AMOUNT);
+				$fermentables .= ',"f_amount":' . $famount;
 				$fermentables .= ',"f_cost":' . floatval($fermentable->COST);
 				$fermentables .= ',"f_type":"' . mysqli_real_escape_string($db, $fermentable->TYPE) . '"';
-				$fermentables .= ',"f_yield":' . floatval($fermentable->YIELD);
+				$fyield = floatval($fermentable->YIELD);
+				$fermentables .= ',"f_yield":' . $fyield;
 				if ($fermentable->COLOR) {
 					$srm = floatval($fermentable->COLOR);
 					$ebc = srm_to_ebc($srm);
@@ -567,9 +568,10 @@
 				else
 					$fermentables .= ',"f_coarse_fine_diff":0';
 				if ($fermentable->MOISTURE)
-					$fermentables .= ',"f_moisture":' . floatval($fermentable->MOISTURE);
+					$fmoisture = floatval($fermentable->MOISTURE);
 				else
-					$fermentables .= ',"f_moisture":0';
+					$fmoisture = 0;
+				$fermentables .= ',"f_moisture":' . $fmoisture;
 				if ($fermentable->DIASTATIC_POWER)
 				        $fermentables .= ',"f_diastatic_power":' . floatval($fermentable->DIASTATIC_POWER);
 				else
@@ -587,9 +589,10 @@
 				else
 					$fermentables .= ',"f_graintype":"Base"';
 				if ($fermentable->ADDED)
-					$fermentables .= ',"f_added":"' . mysqli_real_escape_string($db, $fermentable->ADDED) . '"';
+					$fadded = mysqli_real_escape_string($db, $fermentable->ADDED);
 				else
-					$fermentables .= ',"f_added":"Mash"';
+					$fadded = "Mash";
+				$fermentables .= ',"f_added":"'.$fadded.'"';
 				($fermentable->ADD_AFTER_BOIL== "TRUE") ? $fermentables .= ',"f_add_after_boil":true' : $fermentables .= ',"f_add_after_boil":false';
 				($fermentable->RECOMMEND_MASH== "TRUE") ? $fermentables .= ',"f_recommend_mash":true' : $fermentables .= ',"f_recommend_mash":false';
 				if ($fermentable->DISSOLVED_PROTEIN)
@@ -603,6 +606,11 @@
 				else
 					$fermentables .= ',"f_di_ph":5.6';
 				$fermentables .= "}";
+				/* Sugars */
+				$d = $famount * ($fyield / 100) * (1 - $fmoisture / 100);
+				if ($fadded == "Mash")
+					$d = floatval($efficiency) / 100 * $d;
+				$f_sugars += $d;
 			}
 
 			$fermentables .= ']';
@@ -812,6 +820,20 @@
 			}
 		}
 
+		/*
+		 * Added the calculated values
+		 *  OG, FG, color, IBU
+		 */
+		$plato = 100 * $f_sugars / $batch_size;
+		$sg = plato_to_sg($plato);
+		/* Average loops, HansH 5x. Brouwhulp 20x, about 10x is enough so keep 20. */
+		for ($i = 0; $i < 20; $i++) {
+			if ($sg > 0)
+				$plato = 100 * $f_sugars / ($batch_size * $sg);
+			$sg = plato_to_sg($plato);
+		}
+		$sql .= "', est_og='" . floatval($sg);
+
 		$sql .= "';";
 		if (! $result = mysqli_query($db, $sql)) {
 			printf("Error: %s\n", mysqli_error($db));
--- a/www/rec_print.php	Sun Nov 11 16:36:38 2018 +0100
+++ b/www/rec_print.php	Sun Nov 11 17:53:33 2018 +0100
@@ -142,7 +142,7 @@
 		$this->ProcessingTable=true;
 
 		$sugf = 0;
-		$tot = 0;
+//		$tot = 0;
 		$this->SetFont('Helvetica','',9);
 		$this->SetFillColor(250, 195, 65);
 		$arr = json_decode($row['json_fermentables'], true);
@@ -164,7 +164,7 @@
 			if ($added == "Mash")
 				$d = floatval($row['efficiency']) / 100 * $d;
 			$sugf += $d;
-			$tot += $amount;
+//			$tot += $amount;
 			//$plato = 100 * $d / $amount;
 			//$this->Cell(0,5,$tot.' sugf: '.$sugf.' d: '.$d.' pt: '.$plato.' moisture: '.$moisture,0,0,'L',false);
 			//$this->Ln();
@@ -182,7 +182,7 @@
 
 		$plato = 100 * $sugf / floatval($row['batch_size']);
 		$sg = plato_to_sg($plato);
-		/* Average loops, HansH 5x. Brouwhulp 20x, about 10x is enough so keep 20. */ 
+		/* Average loops, HansH 5x. Brouwhulp 20x, about 10x is enough so keep 20. */
 		for ($i = 0; $i < 20; $i++) {
 			if ($sg > 0)
 				$plato = 100 * $sugf / (floatval($row['batch_size']) * $sg);

mercurial