src/EditProductTab6.cpp

changeset 483
eb0a5f132ea9
parent 482
e13763ec829f
child 484
ebf7ef31da35
--- a/src/EditProductTab6.cpp	Tue Jan 31 16:24:11 2023 +0100
+++ b/src/EditProductTab6.cpp	Thu Feb 02 15:23:18 2023 +0100
@@ -590,7 +590,12 @@
 
     for (int i = 0; i < product->yeasts.size(); i++) {
 	if (product->yeasts.at(i).use == YEAST_USE_PRIMARY) {
-	    if (product->yeasts.at(i).form == YEAST_FORMS_LIQUID) {
+	    qDebug() << "calcViability()" << i << product->yeasts.at(i).yp_uuid << product->yeasts.at(i).yp_package;
+	    if (! product->yeasts.at(i).yp_uuid.isNull() && (product->yeasts.at(i).yp_uuid.length() == 36)) {
+		qDebug() << "  valid package vpm:" << product->yeasts.at(i).yp_viability << product->yeasts.at(i).yp_max;
+		vpm = product->yeasts.at(i).yp_viability;
+		max = product->yeasts.at(i).yp_max;
+	    } else if (product->yeasts.at(i).form == YEAST_FORMS_LIQUID) {	// Fallback to hardcoded values.
 		vpm = 0.80;
 		max = 97;
 		if (product->yeasts.at(i).laboratory == "White Labs") {	// PurePitch
@@ -954,9 +959,79 @@
 }
 
 
+void EditProduct::yeast_load_packages(QString laboratory, int form)
+{
+    QSqlQuery query;
+
+    /*
+     * Clear package and rebuild package select table.
+     */
+    this->ypackageEdit->setCurrentIndex(-1);
+    this->ypackageEdit->clear();
+    this->ypackageEdit->addItem("");    // Start with empty value
+    query.prepare("SELECT * FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form AND valid='1' ORDER BY laboratory,package");
+    query.bindValue(":laboratory", laboratory);
+    query.bindValue(":form", form);
+    qDebug() << "  search" << laboratory << form;
+    query.exec();
+    while (query.next()) {
+        this->ypackageEdit->addItem(query.value("laboratory").toString()+" - "+query.value("package").toString());
+        qDebug() << "  add package" << query.value("laboratory").toString() << query.value("package").toString();
+    }
+}
+
+
+void EditProduct::yeast_package_changed(int val)
+{
+    QSqlQuery query;
+    int index = 0;
+
+#ifdef DEBUG_YEAST
+    qDebug() << "yeast_package_changed()" << product->yeasts_row << val;
+#endif
+
+    /*
+     * Scan the packages for result number 'val'
+     */
+    query.prepare("SELECT * FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form AND valid='1' ORDER BY laboratory,package");
+    query.bindValue(":laboratory", product->yeasts.at(product->yeasts_row).laboratory);
+    query.bindValue(":form", product->yeasts.at(product->yeasts_row).form);
+    qDebug() << "  search" << product->yeasts.at(product->yeasts_row).laboratory << product->yeasts.at(product->yeasts_row).form;
+    query.exec();
+    while (query.next()) {
+	index++;
+	if (index == val) {
+	    qDebug() << "  result" << index << query.value("package").toString();
+	    product->yeasts[product->yeasts_row].yp_uuid = query.value("uuid").toString();
+	    product->yeasts[product->yeasts_row].yp_package = query.value("package").toString();
+	    product->yeasts[product->yeasts_row].yp_cells = query.value("cells").toDouble();
+	    product->yeasts[product->yeasts_row].yp_viability = query.value("viability").toDouble();
+	    product->yeasts[product->yeasts_row].yp_max = query.value("max").toInt();
+	    product->yeasts[product->yeasts_row].yp_size = query.value("size").toDouble();
+	    is_changed();
+	    return;
+	}
+    }
+    if (! product->yeasts[product->yeasts_row].yp_uuid.isNull()) {
+	/*
+	 * Clear package
+	 */
+	product->yeasts[product->yeasts_row].yp_uuid = QString();
+	product->yeasts[product->yeasts_row].yp_package = QString();
+	product->yeasts[product->yeasts_row].yp_cells = product->yeasts[product->yeasts_row].cells;
+	product->yeasts[product->yeasts_row].yp_viability = 0.99;
+	product->yeasts[product->yeasts_row].yp_max = 100;
+	product->yeasts[product->yeasts_row].yp_size = 0.01;
+	is_changed();
+	qDebug() << "  cleared old yp_xxx data";
+    }
+    qDebug() << "  result not found";
+}
+
+
 void EditProduct::yeast_select_changed(int val)
 {
-    QSqlQuery query, query1;
+    QSqlQuery query;
     bool instock = yinstockEdit->isChecked();
     QString w;
     QTableWidgetItem *item;
@@ -1052,18 +1127,7 @@
     /*
      * Clear package and rebuild package select table.
      */
-    this->ypackageEdit->setCurrentIndex(-1);
-    this->ypackageEdit->clear();
-    this->ypackageEdit->addItem("");	// Start with empty value
-    query1.prepare("SELECT * FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form AND valid='1' ORDER BY laboratory,package");
-    query1.bindValue(":laboratory", query.value("laboratory").toString());
-    query1.bindValue(":form", query.value("form").toInt());
-    qDebug() << "  search" << query.value("laboratory").toString() << query.value("form").toInt();
-    query1.exec();
-    while (query1.next()) {
-	this->ypackageEdit->addItem(query1.value("laboratory").toString()+" - "+query1.value("package").toString());
-	qDebug() << "  add package" << query1.value("laboratory").toString() << query1.value("package").toString();
-    }
+    yeast_load_packages(query.value("laboratory").toString(), query.value("form").toInt());
 
     ui->yeastsTable->setItem(product->yeasts_row, 0, new QTableWidgetItem(product->yeasts.at(product->yeasts_row).name));
     ui->yeastsTable->setItem(product->yeasts_row, 1, new QTableWidgetItem(product->yeasts.at(product->yeasts_row).laboratory));
@@ -1298,8 +1362,25 @@
     useatEdit->setCurrentIndex(product->yeasts.at(product->yeasts_row).use);
 
     yeast_instock_changed(true);
+    yeast_load_packages(product->yeasts.at(product->yeasts_row).laboratory, product->yeasts.at(product->yeasts_row).form);
+
+    int index = -1;
+    if (! product->yeasts.at(product->yeasts_row).yp_uuid.isNull() && (product->yeasts.at(product->yeasts_row).yp_uuid.length() == 36)) {
+	query.prepare("SELECT * FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form AND valid='1' ORDER BY laboratory,package");
+	query.bindValue(":laboratory", product->yeasts.at(product->yeasts_row).laboratory);
+	query.bindValue(":form", product->yeasts.at(product->yeasts_row).form);
+	qDebug() << "  search" << product->yeasts.at(product->yeasts_row).laboratory << product->yeasts.at(product->yeasts_row).form;
+	query.exec();
+	while (query.next()) {
+	    index++;
+	    if (query.value("uuid").toString() == product->yeasts.at(product->yeasts_row).yp_uuid)
+		break;
+	}
+	qDebug() << "  result" << index << (query.value("uuid").toString() == product->yeasts.at(product->yeasts_row).yp_uuid);
+    }
 
     connect(yselectEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditProduct::yeast_select_changed);
+    connect(ypackageEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditProduct::yeast_package_changed);
     connect(yamountEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditProduct::yeast_amount_changed);
     connect(useatEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditProduct::yeast_useat_changed);
     connect(yinstockEdit, &QCheckBox::stateChanged, this, &EditProduct::yeast_instock_changed);
@@ -1318,6 +1399,7 @@
     }
 
     disconnect(yselectEdit, nullptr, nullptr, nullptr);
+    disconnect(ypackageEdit, nullptr, nullptr, nullptr);
     disconnect(yamountEdit, nullptr, nullptr, nullptr);
     disconnect(useatEdit, nullptr, nullptr, nullptr);
     disconnect(yinstockEdit, nullptr, nullptr, nullptr);

mercurial