src/EditProductTab6.cpp

changeset 198
904591820c3d
parent 197
6a5e5b3d0fcd
child 199
59b0bdbb2f9f
--- a/src/EditProductTab6.cpp	Thu May 05 20:43:45 2022 +0200
+++ b/src/EditProductTab6.cpp	Sat May 07 13:55:36 2022 +0200
@@ -139,7 +139,7 @@
     ui->est_og4Edit->setValue(product->est_og);
     ui->est_fg3Edit->setValue(product->est_fg);
     ui->est_abv2Edit->setValue(product->est_abv);
-    ui->productionEdit->setText(product->yeast_prod_date.toString("dd MMM yyyy"));
+    ui->productionEdit->setDate(product->yeast_prod_date);
     ui->conditionShow->setValue(product->starter_viability);
     ui->startersgEdit->setValue(product->starter_sg);
     ui->pitchrateEdit->setValue(product->yeast_pitchrate);
@@ -187,6 +187,8 @@
     if (product->yeasts.size() == 0)
 	return;		// No yeast in product.
 
+    calcViability();
+
     for (int i = 0; i < product->yeasts.size(); i++) {
 	if (product->yeasts.at(i).y_use == YEAST_USE_PRIMARY) {		// Primary
 	    if (product->yeasts.at(i).y_form == YEAST_FORMS_DRY) {
@@ -236,9 +238,9 @@
 		    ui->pitchrateEdit->setValue(product->yeast_pitchrate);
 		}
 
-		initcells = (product->yeasts.at(i).y_cells / 1000000) * product->yeasts.at(i).y_amount * 0.97;	// cells / ml.
+		initcells = (product->yeasts.at(i).y_cells / 1000000) * product->yeasts.at(i).y_amount * (product->starter_viability / 100.0);
 		if (product->yeasts.at(i).y_form == YEAST_FORMS_LIQUID)
-		    initcells = (product->yeasts.at(i).y_cells / 1000000000) * product->yeasts.at(i).y_amount * 0.97;	// 97% viability assumed.
+		    initcells = (product->yeasts.at(i).y_cells / 1000000000) * product->yeasts.at(i).y_amount * (product->starter_viability / 100.0);
 
 		needed = round(product->yeast_pitchrate * volume * plato * 10.0) / 10.0;
 		if (needed > initcells) {
@@ -613,8 +615,77 @@
 }
 
 
-void EditProduct::yeast_prod_date_clicked()
+void EditProduct::calcViability()
 {
+    double vpm = 1.00;
+    double max = 100;
+
+    for (int i = 0; i < product->yeasts.size(); i++) {
+	if (product->yeasts.at(i).y_use == 0) {
+	    if (product->yeasts.at(i).y_form == 0) {	// Liquid
+		vpm = 0.80;
+		max = 97;
+		if (product->yeasts.at(i).y_laboratory == "White Labs") { // PurePitch
+		    vpm = 0.95;
+		    max = 100;
+		}
+	    } else if (product->yeasts.at(i).y_form == 1) {	// dry
+		vpm = 0.998;
+		max = 100;
+	    } else if (product->yeasts.at(i).y_form == 6) {	// dried kveik
+		vpm = 0.92;
+		max = 100;
+	    } else {	// Slant, Culture, Frozen, Bottle
+		vpm = 0.99;
+		max = 97;
+	    }
+	}
+    }
+    qDebug() << "calcViability vpm:" << vpm << "max:" << max;
+
+    double base = max;
+
+    /*
+     * Calculate time days before today. If the date is cleared, 
+     * the result is 0 days. Dates in the future are ignored.
+     */
+    int timeDiff = product->yeast_prod_date.daysTo(QDate::currentDate());
+    if (timeDiff < 0)
+	timeDiff == 0;
+
+    double degrade = 1 - ((1 - vpm) / 30.41);	// viability degradation per day.
+    for (int i = 0; i < timeDiff; i++) {
+	base = base * degrade;
+    }
+    if (base > max)
+	base = max;
+    base = round(base);
+    product->starter_viability = base;
+    ui->conditionShow->setValue(product->starter_viability);
+    qDebug() << "age" << timeDiff << "degrade" << degrade << "base" << base ;
+}
+
+
+void EditProduct::yeast_prod_date_changed(QDate val)
+{
+    product->yeast_prod_date = ui->productionEdit->nullDate();
+    qDebug() << "yeast_prod_date_changed" << val << product->yeast_prod_date;
+    calcViability();
+    calcYeast();
+}
+
+
+void EditProduct::yeast_prod_date_clear()
+{
+    product->yeast_prod_date = QDate();
+    ui->productionEdit->setDate(QDate());
+}
+
+
+void EditProduct::yeast_prod_date_today()
+{
+    product->yeast_prod_date = QDate::currentDate();
+    ui->productionEdit->setDate(QDate::currentDate());
 }
 
 
@@ -701,8 +772,6 @@
 	is_changed();
     }
 
-    disconnect(typeEdit, nullptr, nullptr, nullptr);
-    disconnect(volEdit, nullptr, nullptr, nullptr);
     disconnect(buttonBox, nullptr, nullptr, nullptr);
 }
 

mercurial