src/EditProductTab1.cpp

changeset 305
35ce719998e1
parent 301
fe6346211b5b
child 344
9ffac162000c
--- a/src/EditProductTab1.cpp	Tue Jun 21 16:16:43 2022 +0200
+++ b/src/EditProductTab1.cpp	Thu Jun 23 16:50:23 2022 +0200
@@ -608,3 +608,375 @@
 }
 
 
+void EditProduct::reduce_fermentable(int i)
+{
+    QSqlQuery query;
+
+    query.prepare("SELECT record,inventory FROM inventory_fermentables WHERE name=:name AND origin=:origin AND supplier=:supplier LIMIT 1");
+    query.bindValue(":name", product->fermentables.at(i).name);
+    query.bindValue(":origin", product->fermentables.at(i).origin);
+    query.bindValue(":supplier", product->fermentables.at(i).supplier);
+    query.exec();
+    if (query.first()) {
+	int record = query.value("record").toInt();
+	double inventory = query.value("inventory").toDouble() - product->fermentables.at(i).amount;
+	if (inventory < 0)
+	    inventory = 0;
+	else
+	    inventory = round(inventory * 100000) / 100000;
+	query.prepare("UPDATE inventory_fermentables SET inventory=:inventory WHERE record=:record");
+	query.bindValue(":inventory", inventory );
+	query.bindValue(":record", record);
+	query.exec();
+	if (query.lastError().isValid()) {
+	    qWarning() << "UPDATE inventory_fermentables" << query.lastError();
+	} else {
+	    qInfo() << "  Reduced" << product->fermentables.at(i).name << "to" << inventory;
+	}
+    } else {
+	qWarning() << "  Fermentable" << product->fermentables.at(i).name << "not found";
+    }
+}
+
+
+void EditProduct::reduce_hop(int i)
+{
+    QSqlQuery query;
+
+    query.prepare("SELECT record,inventory FROM inventory_hops WHERE name=:name AND origin=:origin AND form=:form LIMIT 1");
+    query.bindValue(":name", product->hops.at(i).name);
+    query.bindValue(":origin", product->hops.at(i).origin);
+    query.bindValue(":form", product->hops.at(i).form);
+    query.exec();
+    if (query.first()) {
+        int record = query.value("record").toInt();
+        double inventory = query.value("inventory").toDouble() - product->hops.at(i).amount;
+        if (inventory < 0)
+            inventory = 0;
+        else
+            inventory = round(inventory * 100000) / 100000;
+        query.prepare("UPDATE inventory_hops SET inventory=:inventory WHERE record=:record");
+        query.bindValue(":inventory", inventory );
+        query.bindValue(":record", record);
+        query.exec();
+        if (query.lastError().isValid()) {
+            qWarning() << "UPDATE inventory_hops" << query.lastError();
+        } else {
+            qInfo() << "  Reduced" << product->hops.at(i).name << "to" << inventory;
+        }
+    } else {
+        qWarning() << "  Hop" << product->hops.at(i).name << "not found";
+    }
+}
+
+
+void EditProduct::reduce_misc(int i)
+{
+    QSqlQuery query;
+
+    query.prepare("SELECT record,inventory FROM inventory_miscs WHERE name=:name LIMIT 1");
+    query.bindValue(":name", product->miscs.at(i).name);
+    query.exec();
+    if (query.first()) {
+        int record = query.value("record").toInt();
+        double inventory = query.value("inventory").toDouble() - product->miscs.at(i).amount;
+        if (inventory < 0)
+            inventory = 0;
+        else
+            inventory = round(inventory * 100000) / 100000;
+        query.prepare("UPDATE inventory_miscs SET inventory=:inventory WHERE record=:record");
+        query.bindValue(":inventory", inventory );
+        query.bindValue(":record", record);
+        query.exec();
+        if (query.lastError().isValid()) {
+            qWarning() << "UPDATE inventory_miscs" << query.lastError();
+        } else {
+            qInfo() << "  Reduced" << product->miscs.at(i).name << "to" << inventory;
+        }
+    } else {
+        qWarning() << "  Misc" << product->miscs.at(i).name << "not found";
+    }
+}
+
+
+void EditProduct::reduce_yeast(int i)
+{
+    QSqlQuery query;
+
+    query.prepare("SELECT record,inventory FROM inventory_yeasts WHERE name=:name AND laboratory=:laboratory AND product_id=:product_id AND form=:form LIMIT 1");
+    query.bindValue(":name", product->yeasts.at(i).name);
+    query.bindValue(":laboratory", product->yeasts.at(i).laboratory);
+    query.bindValue(":product_id", product->yeasts.at(i).product_id);
+    query.bindValue(":form", product->yeasts.at(i).form);
+    query.exec();
+    if (query.first()) {
+        int record = query.value("record").toInt();
+        double inventory = query.value("inventory").toDouble() - product->yeasts.at(i).amount;
+        if (inventory < 0)
+            inventory = 0;
+        else
+            inventory = round(inventory * 100000) / 100000;
+        query.prepare("UPDATE inventory_yeasts SET inventory=:inventory WHERE record=:record");
+        query.bindValue(":inventory", inventory );
+        query.bindValue(":record", record);
+        query.exec();
+        if (query.lastError().isValid()) {
+            qWarning() << "UPDATE inventory_yeasts" << query.lastError();
+        } else {
+            qInfo() << "  Reduced" << product->yeasts.at(i).name << "to" << inventory;
+        }
+    } else {
+        qWarning() << "  Yeast" << product->yeasts.at(i).name << "not found";
+    }
+}
+
+
+/*
+ * Reduce inventory depending on the production stage.
+ *  Stage PROD_STAGE_BREW+, after brew, reduce sugars(0-mash, 1-boil), hops(0-mash, 1-fwh, 2-boil, 3-aroma, 4-whirlpool), miscs(0-starter, 1-mash, 2-boil)
+ *  Stage PROD_STAGE_PRIMARY+, after primary, reduce sugars(2-fermention), yeasts(0-Primary), miscs(3-primary)
+ *  Stage PROD_STAGE_SECONDARY+, after secondary, reduce yeasts(1-Secondary)
+ *  Stage PROD_STAGE_TERTIARY+, after tertiary, reduce sugars(3-lagering), hops(5-dry-hop), yeasts(2-Tertiary), miscs(4-secondary)
+ *  Stage PROD_STAGE_PACKAGE+, after packaging, reduce sugars(4-bottle, 5-kegs), hops( Extract ), yeasts(3-Bottle), miscs(5-bottling)
+ */
+void EditProduct::inventory_reduce()
+{
+    bool savethis = false;
+    QSqlQuery query;
+
+    if (product->stage == product->inventory_reduced)
+	return;
+
+    /*
+     * Nothing to reduce yet, but just set inventory reduced.
+     */
+    if ((product->stage >= PROD_STAGE_WAIT) && (product->inventory_reduced < PROD_STAGE_WAIT)) {
+	product->inventory_reduced = PROD_STAGE_WAIT;
+	savethis = true;
+    }
+    if ((product->stage >= PROD_STAGE_BREW) && (product->inventory_reduced < PROD_STAGE_BREW)) {
+	product->inventory_reduced = PROD_STAGE_BREW;
+	savethis = true;
+    }
+
+    /*
+     * If the brew is done, reduce the used ingredients.
+     */
+    if ((product->stage >= PROD_STAGE_PRIMARY) && (product->inventory_reduced < PROD_STAGE_PRIMARY)) {
+	qInfo() << "Reduce brew inventory from" << product->code << product->name;
+
+	if (product->fermentables.size()) {
+	    for (int i = 0; i < product->fermentables.size(); i++) {
+		if (product->fermentables.at(i).added <= FERMENTABLE_ADDED_BOIL) {	/* Mash or boil */
+		    reduce_fermentable(i);
+		}
+	    }
+	}
+	if (product->hops.size()) {
+	    for (int i = 0; i < product->hops.size(); i++) {
+		if (product->hops.at(i).useat <= HOP_USEAT_WHIRLPOOL) {	/* Mash, FWH, Boil, Flameout, Whirlpool */
+		    reduce_hop(i);
+		}
+	    }
+	}
+	if (product->miscs.size()) {
+	    for (int i = 0; i < product->miscs.size(); i++) {
+		if (product->miscs.at(i).use_use <= MISC_USES_BOIL) {	/* Starter, Mash, Boil */
+		    reduce_misc(i);
+		}
+	    }
+	}
+	if ((product->w1_name != "") && (product->w1_amount > 0)) {
+	    query.prepare("SELECT record,inventory FROM inventory_waters WHERE name=:name AND unlimited_stock=0 LIMIT 1");
+	    query.bindValue(":name", product->w1_name);
+	    query.exec();
+	    if (query.first()) {
+		int record = query.value("record").toInt();
+		double inventory = query.value("inventory").toDouble() - product->w1_amount;
+	        if (inventory < 0)
+        	    inventory = 0;
+        	else
+        	    inventory = round(inventory * 100) / 100;
+        	query.prepare("UPDATE inventory_waters SET inventory=:inventory WHERE record=:record");
+        	query.bindValue(":inventory", inventory );
+        	query.bindValue(":record", record);
+        	query.exec();
+        	if (query.lastError().isValid()) {
+        	    qWarning() << "UPDATE inventory_waters" << query.lastError();
+        	} else {
+        	    qInfo() << "  Reduced" << product->w1_name << "to" << inventory;
+        	}
+	    }
+	}
+	if ((product->w2_name != "") && (product->w2_amount > 0)) {
+	    query.prepare("SELECT record,inventory FROM inventory_waters WHERE name=:name AND unlimited_stock=0 LIMIT 1");
+            query.bindValue(":name", product->w1_name);
+            query.exec();
+            if (query.first()) {
+                int record = query.value("record").toInt();
+                double inventory = query.value("inventory").toDouble() - product->w1_amount;
+                if (inventory < 0)
+                    inventory = 0;
+                else
+                    inventory = round(inventory * 100) / 100;
+                query.prepare("UPDATE inventory_waters SET inventory=:inventory WHERE record=:record");
+                query.bindValue(":inventory", inventory );
+                query.bindValue(":record", record);
+                query.exec();
+                if (query.lastError().isValid()) {
+                    qWarning() << "UPDATE inventory_waters" << query.lastError();
+                } else {
+                    qInfo() << "  Reduced" << product->w1_name << "to" << inventory;
+                }
+            }
+	}
+	product->inventory_reduced = PROD_STAGE_PRIMARY;
+        savethis = true;
+    }
+
+    /*
+     * After the Primary fermentation
+     */
+    if ((product->stage >= PROD_STAGE_SECONDARY) && (product->inventory_reduced < PROD_STAGE_SECONDARY)) {
+	qInfo() << "Reduce primary inventory from" << product->code << product->name;
+
+	if (product->fermentables.size()) {
+            for (int i = 0; i < product->fermentables.size(); i++) {
+                if (product->fermentables.at(i).added == FERMENTABLE_ADDED_FERMENTATION) {
+                    reduce_fermentable(i);
+                }
+            }
+        }
+	if (product->miscs.size()) {
+            for (int i = 0; i < product->miscs.size(); i++) {
+                if (product->miscs.at(i).use_use == MISC_USES_PRIMARY) {
+                    reduce_misc(i);
+                }
+            }
+        }
+	if (product->yeasts.size()) {
+            for (int i = 0; i < product->yeasts.size(); i++) {
+                if (product->yeasts.at(i).use == YEAST_USE_PRIMARY) {
+                    reduce_yeast(i);
+                }
+            }
+        }
+	product->inventory_reduced = PROD_STAGE_SECONDARY;
+        savethis = true;
+    }
+
+    /*
+     * After the Seconday fermentation
+     */
+    if ((product->stage >= PROD_STAGE_TERTIARY) && (product->inventory_reduced < PROD_STAGE_TERTIARY)) {
+	qInfo() << "Reduce secondary inventory from" << product->code << product->name;
+
+	if (product->yeasts.size()) {
+            for (int i = 0; i < product->yeasts.size(); i++) {
+                if (product->yeasts.at(i).use == YEAST_USE_SECONDARY) {
+                    reduce_yeast(i);
+                }
+            }
+        }
+	product->inventory_reduced = PROD_STAGE_TERTIARY;
+        savethis = true;
+    }
+
+    /*
+     * After the Tertiary fermentation
+     */
+    if ((product->stage >= PROD_STAGE_PACKAGE) && (product->inventory_reduced < PROD_STAGE_PACKAGE)) {
+	qInfo() << "Reduce tertiary inventory from" << product->code << product->name;
+
+	if (product->fermentables.size()) {
+            for (int i = 0; i < product->fermentables.size(); i++) {
+                if (product->fermentables.at(i).added == FERMENTABLE_ADDED_LAGERING) {
+                    reduce_fermentable(i);
+                }
+            }
+        }
+	if (product->hops.size()) {
+            for (int i = 0; i < product->hops.size(); i++) {
+                if (product->hops.at(i).useat == HOP_USEAT_DRY_HOP) {
+                    reduce_hop(i);
+                }
+            }
+        }
+	if (product->miscs.size()) {
+            for (int i = 0; i < product->miscs.size(); i++) {
+                if (product->miscs.at(i).use_use == MISC_USES_SECONDARY) {
+                    reduce_misc(i);
+                }
+            }
+        }
+        if (product->yeasts.size()) {
+            for (int i = 0; i < product->yeasts.size(); i++) {
+                if (product->yeasts.at(i).use == YEAST_USE_TERTIARY) {
+                    reduce_yeast(i);
+                }
+            }
+        }
+
+	product->inventory_reduced = PROD_STAGE_PACKAGE;
+        savethis = true;
+    }
+
+    /*
+     * After packaging
+     *  reduce sugars(4/5-bottle), yeasts(3-Bottle), miscs(5-bottling)
+     */
+    if ((product->stage >= PROD_STAGE_CARBONATION) && (product->inventory_reduced < PROD_STAGE_CARBONATION)) {
+	qInfo() << "Reduce package inventory from" << product->code << product->name;
+
+	if (product->fermentables.size()) {
+            for (int i = 0; i < product->fermentables.size(); i++) {
+                if (product->fermentables.at(i).added >= FERMENTABLE_ADDED_BOTTLE) {
+                    reduce_fermentable(i);
+                }
+            }
+        }
+	if (product->yeasts.size()) {
+            for (int i = 0; i < product->yeasts.size(); i++) {
+                if (product->yeasts.at(i).use == YEAST_USE_BOTTLE) {
+                    reduce_yeast(i);
+                }
+            }
+        }
+	if (product->hops.size()) {
+            for (int i = 0; i < product->hops.size(); i++) {
+                if (product->hops.at(i).useat == HOP_USEAT_BOTTLING) {
+                    reduce_hop(i);
+                }
+            }
+        }
+	if (product->miscs.size()) {
+            for (int i = 0; i < product->miscs.size(); i++) {
+                if (product->miscs.at(i).use_use == MISC_USES_BOTTLING) {
+                    reduce_misc(i);
+                }
+            }
+        }
+	product->inventory_reduced = PROD_STAGE_CARBONATION;
+        savethis = true;
+    }
+
+    if ((product->stage >= PROD_STAGE_MATURE) && (product->inventory_reduced < PROD_STAGE_MATURE)) {
+	product->inventory_reduced = product->stage;
+	savethis = true;
+    }
+
+    /*
+     * Update the inventory_reduced state.
+     */
+    if (savethis) {
+	query.prepare("UPDATE products SET inventory_reduced=:inventory_reduced WHERE record=:record");
+	query.bindValue(":record", product->record);
+	query.bindValue(":inventory_reduced", product->inventory_reduced);
+	query.exec();
+    	if (query.lastError().isValid()) {
+	    qWarning() << "UPDATE products" << query.lastError();
+	}
+    }
+}
+
+

mercurial