www/js/rec_edit.js

changeset 406
0ad967f2d6ee
parent 396
804800d8e885
child 428
a5d4467c9201
--- a/www/js/rec_edit.js	Mon Jun 10 20:04:12 2019 +0200
+++ b/www/js/rec_edit.js	Wed Jun 12 16:41:38 2019 +0200
@@ -157,6 +157,33 @@
 		$("#sparge_acid_perc").jqxNumberInput({ spinButtons: rw, readOnly: ro, width: w100 });
 	};
 
+	function calcPercentages() {
+
+		console.log("calcPercentages()");
+		var rowscount = $("#fermentableGrid").jqxGrid('getdatainformation').rowscount;
+		if (rowscount > 1) {
+			var tw = 0;
+			for (i = 0; i < rowscount; i++) {
+				var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
+				if (rowdata.f_added < 4)
+					tw += Math.round(rowdata.f_amount * 1000) / 1000;
+			};
+			tw = Math.round(tw * 1000) / 1000;
+
+			for (i = 0; i < rowscount; i++) {
+				var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
+				if (rowdata.f_added < 4) {
+					var percentage = Math.round(rowdata.f_amount / tw * 1000) / 10.0;
+					$("#fermentableGrid").jqxGrid('setcellvalue', i, "f_percentage", percentage);
+				} else {
+					$("#fermentableGrid").jqxGrid('setcellvalue', i, "f_percentage", 0);
+				}
+			};
+		} else {
+			$("#fermentableGrid").jqxGrid('setcellvalue', 0, "f_percentage", 100);
+		}
+	}
+
 	function calcFermentables() {
 		console.log("calcFermentables()");
 		sugarsf = 0;		// fermentable sugars mash + boil
@@ -200,9 +227,9 @@
 			var row = rows[i];
 			if (row.f_adjust_to_total_100)
 				my_100 = true;
-			if (row.f_type == 1)		// Sugar
+			if (row.f_type == 1 && row.f_added < 4)		// Sugar
 				psugar += row.f_percentage;
-			if (row.f_graintype == 2)	// Crystal
+			if (row.f_graintype == 2 && row.f_added < 4)	// Crystal
 				pcara += row.f_percentage;
 			var d = row.f_amount * (row.f_yield / 100) * (1 - row.f_moisture / 100);
 			if (row.f_added == 0) {		// Mash
@@ -216,7 +243,7 @@
 			}
 			if (row.f_added == 0 || row.f_added == 1)	// Mash or Boil
 				sugarsf += d;
-			if (row.f_added == 2 || row.f_added == 3) {
+			if (row.f_added == 2 || row.f_added == 3) {	// Fermentation or lagering
 				var x = (row.f_yield / 100) * (1 - row.f_moisture / 100);
 				addedS += row.f_amount * x;
 				addedmass += row.f_amount;
@@ -225,9 +252,11 @@
 			if (row.f_added == 0 && (row.f_type == 0 || row.f_type == 4) && row.f_color < 50) { // Mash and Grain/Adjunct and Color < 50
 				lintner += row.f_diastatic_power * row.f_amount;
 			}
-			colort += row.f_amount * ebc_to_srm(row.f_color);
-			colorh += row.f_amount * row.f_color * get_kt(row.f_color);
-			colorn += (row.f_percentage / 100) * row.f_color;	// For 8.6 Pt wort.
+			if (row.f_added < 4) {
+				colort += row.f_amount * ebc_to_srm(row.f_color);
+				colorh += row.f_amount * row.f_color * get_kt(row.f_color);
+				colorn += (row.f_percentage / 100) * row.f_color;	// For 8.6 Pt wort.
+			}
 		}
 		$("#ferm_lintner").val(Math.round(parseFloat(lintner / mashkg)));
 		console.log("lintner:"+lintner+" kg:"+mashkg);
@@ -1196,20 +1225,24 @@
 
 		for (var i = 0; i < rowscount; i++) {
 			var row = $("#fermentableGrid").jqxGrid('getrowdata', i);
-			var d = row.f_percentage / 100 * (row.f_yield / 100) * (1 - row.f_moisture / 100);
-			if (row.f_added == 0)	// Mash
-				d = efficiency / 100 * d;
-			tot += d;
+			if (row.f_added < 4) {
+				var d = row.f_percentage / 100 * (row.f_yield / 100) * (1 - row.f_moisture / 100);
+				if (row.f_added == 0)	// Mash
+					d = efficiency / 100 * d;
+				tot += d;
+			}
 		}
 		var	totmass = 0;
 		if (tot)
-			totmass = sug / tot;
+			totmass = Math.round((sug / tot) * 1000) / 1000;
 
 		if (totmass) {
 			for (i = 0; i < rowscount; i++) {
 				var row = $("#fermentableGrid").jqxGrid('getrowdata', i);
-				var amount = row.f_percentage / 100 * totmass;
-				$("#fermentableGrid").jqxGrid('setcellvalue', i, "f_amount", amount);
+				if (row.f_added < 4) {
+					var amount = Math.round(row.f_percentage * 10 * totmass) / 1000;
+					$("#fermentableGrid").jqxGrid('setcellvalue', i, "f_amount", amount);
+				}
 			}
 		}
 	};
@@ -1809,15 +1842,15 @@
 				{ name: 'f_avail', type: 'int' }
 			],
 			addrow: function (rowid, rowdata, position, commit) {
-				console.log("fermentable addrow "+rowid);
+				//console.log("fermentable addrow "+rowid);
 				commit(true);
 			},
 			deleterow: function (rowid, commit) {
-				console.log("fermentable deleterow "+rowid);
+				//console.log("fermentable deleterow "+rowid);
 				commit(true);
 			},
 			updaterow: function (rowid, rowdata, commit) {
-				console.log("fermentable updaterow "+rowid);
+				//console.log("fermentable updaterow "+rowid);
 				commit(true);
 			}
 		};
@@ -1926,16 +1959,7 @@
 								}
 							}
 						} else {
-							var tw = 0;
-							for (i = 0; i < rowscount; i++) {
-								var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
-								tw += rowdata.f_amount;
-							};
-							for (i = 0; i < rowscount; i++) {
-								var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
-								var percentage = Math.round(rowdata.f_amount / tw * 1000) / 10.0;
-								$("#fermentableGrid").jqxGrid('setcellvalue', i, "f_percentage", percentage);
-							};
+							calcPercentages();
 						}
 					} else {
 						$("#fermentableGrid").jqxGrid('setcellvalue', 0, "f_percentage", 100);
@@ -2965,7 +2989,7 @@
 			f_avail: fermentableData.f_avail
 		};
 		$("#fermentableGrid").jqxGrid('updaterow', rowID, row);
-		// Recalc percentages
+		calcPercentages();
 		calcFermentables();
 		calcIBUs();
 		calcMash();
@@ -3023,25 +3047,7 @@
 		$("#fermentableGrid").jqxGrid('setcellvalue', fermentableRow, 'f_amount', event.args.value);
 		fermentableData.f_amount = event.args.value;
 		if (! to_100) {
-			// Recalculate percentages
-			console.log("adjust percentages");
-			var rowscount = $("#fermentableGrid").jqxGrid('getdatainformation').rowscount;
-			if (rowscount > 1) {
-				var tw = 0;
-				for (i = 0; i < rowscount; i++) {
-					var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
-					tw += rowdata.f_amount;
-				};
-				for (i = 0; i < rowscount; i++) {
-					var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
-					var percentage = Math.round(rowdata.f_amount / tw * 1000) / 10.0;
-					$("#fermentableGrid").jqxGrid('setcellvalue', i, "f_percentage", percentage);
-		//			if (i == fermentableRow) // Will crash the script.
-		//				$("#wf_percentage").val(percentage);
-				};
-			} else {
-				$("#fermentableGrid").jqxGrid('setcellvalue', 0, "f_percentage", 100);
-			}
+			calcPercentages();
 			calcFermentables();
 			calcMash();
 		};
@@ -3062,13 +3068,15 @@
 				var tw = 0;     // total weight
 				for (i = 0; i < rowscount; i++) {
 					var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
-					tw += rowdata.f_amount;
+					if (rowdata.f_added < 4)
+						tw += Math.round(rowdata.f_amount * 1000) / 1000;
 				}
+				tw = Math.round(tw * 1000) / 1000;
 				if (to_100) {
 					// Adjust this row and the 100% row.
-					var damount = tw * diff / 100;
+					var damount = Math.round(tw * diff *10) / 1000;
 					var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', fermentableRow);
-					var namount = rowdata.f_amount + damount;
+					var namount = Math.round((rowdata.f_amount + damount) * 1000) / 1000;					
 					$("#fermentableGrid").jqxGrid('setcellvalue', fermentableRow, 'f_amount', namount);
 					$("#wf_amount").val(namount);
 					$("#fermentableGrid").jqxGrid('setcellvalue', fermentableRow, 'f_percentage', rowdata.f_percentage + diff);
@@ -3087,16 +3095,20 @@
 					var nw = tw * diff / 100;
 					for (i = 0; i < rowscount; i++) {
 						var rowdata = $("#fermentableGrid").jqxGrid('getrowdata', i);
-						if (i == fermentableRow) {
-							var namount = rowdata.f_amount + nw;
-							$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_amount', namount);
-		//					$("#wf_amount").val(namount); // Will crash the script.
-							$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_percentage', newvalue);
+						if (rowdata.f_added < 4) {
+							if (i == fermentableRow) {
+								var namount = Math.round((rowdata.f_amount + nw) * 1000) / 1000;
+								$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_amount', namount);
+		//						$("#wf_amount").val(namount); // Will crash the script.
+								$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_percentage', newvalue);
+							} else {
+								var namount = Math.round((rowdata.f_amount - (nw / (rowscount - 1))) * 1000) / 1000;
+								var newperc = Math.round((namount / tw) * 1000) / 10.0;
+								$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_amount', namount);
+								$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_percentage', newperc);
+							}
 						} else {
-							var namount = rowdata.f_amount - (nw / (rowscount - 1));
-							var newperc = Math.round((namount / tw) * 1000) / 10.0;
-							$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_amount', namount);
-							$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_percentage', newperc);
+							$("#fermentableGrid").jqxGrid('setcellvalue', i, 'f_percentage', 0);
 						}
 					}
 					calcFermentables();

mercurial