Wed, 15 Jun 2022 10:54:04 +0200
Version 0.2.10 with updated translations and menu's.
/** * db_product.cpp is part of bmsapp. * * bmsapp is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * bmsapp is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "global.h" #include "db_product.h" bool DB_product::load(Product *prod, QDialog *dialog, int recno) { QSqlQuery query, yquery; query.prepare("SELECT * FROM products WHERE record = :recno"); query.bindValue(":recno", recno); query.exec(); if (! query.first()) { qWarning() << "loadProduct seek error record" << recno; QMessageBox::warning(dialog, QObject::tr("Database error"), QObject::tr("MySQL error: record %1 not found").arg(recno)); dialog->done(QDialog::Rejected); // At this stage, this doesn't work because the dialog is not yet visible. return false; } QSqlRecord rec = query.record(); for (int i = 0; i < rec.count(); i++) qDebug() << i << rec.fieldName(i) << query.value(i); prod->record = query.value("record").toInt(); prod->uuid = query.value("uuid").toString(); prod->name = query.value("name").toString(); prod->code = query.value("code").toString(); prod->birth = query.value("birth").toDate(); prod->stage = query.value("stage").toInt(); prod->notes = query.value("notes").toString(); prod->log_brew = query.value("log_brew").toInt() ? true:false; prod->log_fermentation = query.value("log_fermentation").toInt() ? true:false; prod->log_ispindel = query.value("log_ispindel").toInt() ? true:false; prod->log_co2pressure = query.value("log_co2pressure").toInt() ? true:false; prod->inventory_reduced = query.value("inventory_reduced").toInt(); prod->locked = query.value("locked").toInt() ? true:false; prod->eq_name = query.value("eq_name").toString(); prod->eq_notes = query.value("eq_notes").toString(); prod->eq_boil_size = query.value("eq_boil_size").toDouble(); prod->eq_batch_size = query.value("eq_batch_size").toDouble(); prod->eq_tun_volume = query.value("eq_tun_volume").toDouble(); prod->eq_tun_weight = query.value("eq_tun_weight").toDouble(); prod->eq_tun_specific_heat = query.value("eq_tun_specific_heat").toDouble(); prod->eq_tun_material = query.value("eq_tun_material").toInt(); prod->eq_tun_height = query.value("eq_tun_height").toDouble(); prod->eq_top_up_water = query.value("eq_top_up_water").toDouble(); prod->eq_trub_chiller_loss = query.value("eq_trub_chiller_loss").toDouble(); prod->eq_evap_rate = query.value("eq_evap_rate").toDouble(); prod->eq_boil_time = query.value("eq_boil_time").toDouble(); prod->eq_calc_boil_volume = query.value("eq_calc_boil_volume").toInt() ? true:false; prod->eq_top_up_kettle = query.value("eq_top_up_kettle").toDouble(); prod->eq_hop_utilization = query.value("eq_hop_utilization").toDouble(); prod->eq_lauter_volume = query.value("eq_lauter_volume").toDouble(); prod->eq_lauter_height = query.value("eq_lauter_height").toDouble(); prod->eq_lauter_deadspace = query.value("eq_lauter_deadspace").toDouble(); prod->eq_kettle_volume = query.value("eq_kettle_volume").toDouble(); prod->eq_kettle_height = query.value("eq_kettle_height").toDouble(); prod->eq_mash_volume = query.value("eq_mash_volume").toDouble(); prod->eq_mash_max = query.value("eq_mash_max").toDouble(); prod->eq_efficiency = query.value("eq_efficiency").toDouble(); prod->brew_date_start = query.value("brew_date_start").toDateTime(); prod->brew_mash_ph = query.value("brew_mash_ph").toDouble(); prod->brew_mash_sg = query.value("brew_mash_sg").toDouble(); prod->brew_mash_efficiency = query.value("brew_mash_efficiency").toDouble(); prod->brew_sparge_temperature = query.value("brew_sparge_temperature").toDouble(); prod->brew_sparge_volume = query.value("brew_sparge_volume").toDouble(); prod->brew_sparge_est = query.value("brew_sparge_est").toDouble(); prod->brew_sparge_ph = query.value("brew_sparge_ph").toDouble(); prod->brew_preboil_volume = query.value("brew_preboil_volume").toDouble(); prod->brew_preboil_sg = query.value("brew_preboil_sg").toDouble(); prod->brew_preboil_ph = query.value("brew_preboil_ph").toDouble(); prod->brew_preboil_efficiency = query.value("brew_preboil_efficiency").toDouble(); prod->brew_aboil_volume = query.value("brew_aboil_volume").toDouble(); prod->brew_aboil_sg = query.value("brew_aboil_sg").toDouble(); prod->brew_aboil_ph = query.value("brew_aboil_ph").toDouble(); prod->brew_aboil_efficiency = query.value("brew_aboil_efficiency").toDouble(); prod->brew_cooling_method = query.value("brew_cooling_method").toInt(); prod->brew_cooling_time = query.value("brew_cooling_time").toDouble(); prod->brew_cooling_to = query.value("brew_cooling_to").toDouble(); prod->brew_whirlpool9 = query.value("brew_whirlpool9").toDouble(); prod->brew_whirlpool7 = query.value("brew_whirlpool7").toDouble(); prod->brew_whirlpool6 = query.value("brew_whirlpool6").toDouble(); prod->brew_whirlpool2 = query.value("brew_whirlpool2").toDouble(); prod->brew_fermenter_volume = query.value("brew_fermenter_volume").toDouble(); prod->brew_fermenter_extrawater = query.value("brew_fermenter_extrawater").toDouble(); prod->brew_fermenter_tcloss = query.value("brew_fermenter_tcloss").toDouble(); prod->brew_aeration_time = query.value("brew_aeration_time").toDouble(); prod->brew_aeration_speed = query.value("brew_aeration_speed").toDouble(); prod->brew_aeration_type = query.value("brew_aeration_type").toInt(); prod->brew_fermenter_sg = query.value("brew_fermenter_sg").toDouble(); prod->brew_fermenter_ibu = query.value("brew_fermenter_ibu").toDouble(); prod->brew_fermenter_color = query.value("brew_fermenter_color").toDouble(); prod->brew_date_end = query.value("brew_date_end").toDateTime(); prod->og = query.value("og").toDouble(); prod->fg = query.value("fg").toDouble(); prod->primary_start_temp = query.value("primary_start_temp").toDouble(); prod->primary_max_temp = query.value("primary_max_temp").toDouble(); prod->primary_end_temp = query.value("primary_end_temp").toDouble(); prod->primary_end_sg = query.value("primary_end_sg").toDouble(); prod->primary_end_date = query.value("primary_end_date").toDate(); prod->secondary_temp = query.value("secondary_temp").toDouble(); prod->secondary_end_sg = query.value("secondary_end_sg").toDouble(); prod->secondary_end_date = query.value("secondary_end_date").toDate(); prod->tertiary_temp = query.value("tertiary_temp").toDouble(); prod->package_date = query.value("package_date").toDate(); prod->package_volume = query.value("package_volume").toDouble(); prod->package_infuse_amount = query.value("package_infuse_amount").toDouble(); prod->package_infuse_abv = query.value("package_infuse_abv").toDouble(); prod->package_infuse_notes = query.value("package_infuse_notes").toString(); prod->package_abv = query.value("package_abv").toDouble(); prod->package_ph = query.value("package_ph").toDouble(); prod->bottle_amount = query.value("bottle_amount").toDouble(); prod->bottle_carbonation = query.value("bottle_carbonation").toDouble(); prod->bottle_priming_sugar = query.value("bottle_priming_sugar").toInt(); prod->bottle_priming_amount = query.value("bottle_priming_amount").toDouble(); prod->bottle_priming_water = query.value("bottle_priming_water").toDouble(); prod->bottle_carbonation_temp = query.value("bottle_carbonation_temp").toDouble(); prod->keg_amount = query.value("keg_amount").toDouble(); prod->keg_carbonation = query.value("keg_carbonation").toDouble(); prod->keg_priming_sugar = query.value("keg_priming_sugar").toInt(); prod->keg_priming_amount = query.value("keg_priming_amount").toDouble(); prod->keg_priming_water = query.value("keg_priming_water").toDouble(); prod->keg_carbonation_temp = query.value("keg_carbonation_temp").toDouble(); prod->keg_forced_carb = query.value("keg_forced_carb").toInt() ? true:false; prod->keg_pressure = query.value("keg_pressure").toDouble(); prod->taste_notes = query.value("taste_notes").toString(); prod->taste_rate = query.value("taste_rate").toDouble(); prod->taste_date = query.value("taste_date").toDate(); prod->taste_color = query.value("taste_color").toString(); prod->taste_transparency = query.value("taste_transparency").toString(); prod->taste_head = query.value("taste_head").toString(); prod->taste_aroma = query.value("taste_aroma").toString(); prod->taste_taste = query.value("taste_taste").toString(); prod->taste_mouthfeel = query.value("taste_mouthfeel").toString(); prod->taste_aftertaste = query.value("taste_aftertaste").toString(); prod->st_name = query.value("st_name").toString(); prod->st_letter = query.value("st_letter").toString(); prod->st_guide = query.value("st_guide").toString(); prod->st_category = query.value("st_category").toString(); prod->st_category_number = query.value("st_category_number").toInt(); prod->st_type = query.value("st_type").toInt(); prod->st_og_min = query.value("st_og_min").toDouble(); prod->st_og_max = query.value("st_og_max").toDouble(); prod->st_fg_min = query.value("st_fg_min").toDouble(); prod->st_fg_max = query.value("st_fg_max").toDouble(); prod->st_ibu_min = query.value("st_ibu_min").toDouble(); prod->st_ibu_max = query.value("st_ibu_max").toDouble(); prod->st_color_min = query.value("st_color_min").toDouble(); prod->st_color_max = query.value("st_color_max").toDouble(); prod->st_carb_min = query.value("st_carb_min").toDouble(); prod->st_carb_max = query.value("st_carb_max").toDouble(); prod->st_abv_min = query.value("st_abv_min").toDouble(); prod->st_abv_max = query.value("st_abv_max").toDouble(); prod->type = query.value("type").toInt(); prod->batch_size = query.value("batch_size").toDouble(); prod->boil_size = query.value("boil_size").toDouble(); prod->boil_time = query.value("boil_time").toDouble(); prod->efficiency = query.value("efficiency").toDouble(); prod->est_og = query.value("est_og").toDouble(); prod->est_og3 = query.value("est_og3").toDouble(); prod->est_fg = query.value("est_fg").toDouble(); prod->est_abv = query.value("est_abv").toDouble(); prod->est_color = query.value("est_color").toDouble(); prod->color_method = query.value("color_method").toInt(); prod->est_ibu = query.value("est_ibu").toDouble(); prod->ibu_method = query.value("ibu_method").toInt(); prod->est_carb = query.value("est_carb").toDouble(); prod->sparge_temp = query.value("sparge_temp").toDouble(); prod->sparge_ph = query.value("sparge_ph").toDouble(); prod->sparge_volume = query.value("sparge_volume").toDouble(); prod->sparge_source = query.value("sparge_source").toInt(); prod->sparge_acid_type = query.value("sparge_acid_type").toInt(); prod->sparge_acid_perc = query.value("sparge_acid_perc").toDouble(); prod->sparge_acid_amount = query.value("sparge_acid_amount").toDouble(); prod->mash_ph = query.value("mash_ph").toDouble(); prod->mash_name = query.value("mash_name").toString(); prod->calc_acid = query.value("calc_acid").toInt() ? true:false; prod->w1_name = query.value("w1_name").toString(); prod->w1_amount = query.value("w1_amount").toDouble(); prod->w1_calcium = query.value("w1_calcium").toDouble(); prod->w1_sulfate = query.value("w1_sulfate").toDouble(); prod->w1_chloride = query.value("w1_chloride").toDouble(); prod->w1_sodium = query.value("w1_sodium").toDouble(); prod->w1_magnesium = query.value("w1_magnesium").toDouble(); prod->w1_total_alkalinity = query.value("w1_total_alkalinity").toDouble(); prod->w1_ph = query.value("w1_ph").toDouble(); prod->w1_cost = query.value("w1_cost").toDouble(); prod->w2_name = query.value("w2_name").toString(); prod->w2_amount = query.value("w2_amount").toDouble(); prod->w2_calcium = query.value("w2_calcium").toDouble(); prod->w2_sulfate = query.value("w2_sulfate").toDouble(); prod->w2_chloride = query.value("w2_chloride").toDouble(); prod->w2_sodium = query.value("w2_sodium").toDouble(); prod->w2_magnesium = query.value("w2_magnesium").toDouble(); prod->w2_total_alkalinity = query.value("w2_total_alkalinity").toDouble(); prod->w2_ph = query.value("w2_ph").toDouble(); prod->w2_cost = query.value("w2_cost").toDouble(); prod->wg_amount = query.value("wg_amount").toDouble(); prod->wg_calcium = query.value("wg_calcium").toDouble(); prod->wg_sulfate = query.value("wg_sulfate").toDouble(); prod->wg_chloride = query.value("wg_chloride").toDouble(); prod->wg_sodium = query.value("wg_sodium").toDouble(); prod->wg_magnesium = query.value("wg_magnesium").toDouble(); prod->wg_total_alkalinity = query.value("wg_total_alkalinity").toDouble(); prod->wg_ph = query.value("wg_ph").toDouble(); prod->wb_calcium = query.value("wb_calcium").toDouble(); prod->wb_sulfate = query.value("wb_sulfate").toDouble(); prod->wb_chloride = query.value("wb_chloride").toDouble(); prod->wb_sodium = query.value("wb_sodium").toDouble(); prod->wb_magnesium = query.value("wb_magnesium").toDouble(); prod->wb_total_alkalinity = query.value("wb_total_alkalinity").toDouble(); prod->wb_ph = query.value("wb_ph").toDouble(); prod->wa_acid_name = query.value("wa_acid_name").toInt(); prod->wa_acid_perc = query.value("wa_acid_perc").toDouble(); prod->wa_base_name = query.value("wa_base_name").toInt(); prod->starter_enable = query.value("starter_enable").toInt() ? true:false; prod->starter_type = query.value("starter_type").toInt(); prod->starter_sg = query.value("starter_sg").toDouble(); prod->starter_viability = query.value("starter_viability").toInt(); if (query.value("yeast_prod_date").toString().length() == 10) prod->yeast_prod_date = query.value("yeast_prod_date").toDate(); else prod->yeast_prod_date = QDate(); prod->yeast_pitchrate = query.value("yeast_pitchrate").toDouble(); prod->prop_type[0] = query.value("prop1_type").toInt(); prod->prop_volume[0] = query.value("prop1_volume").toDouble(); prod->prop_type[1] = query.value("prop2_type").toInt(); prod->prop_volume[1] = query.value("prop2_volume").toDouble(); prod->prop_type[2] = query.value("prop3_type").toInt(); prod->prop_volume[2] = query.value("prop3_volume").toDouble(); prod->prop_type[3] = query.value("prop4_type").toInt(); prod->prop_volume[3] = query.value("prop4_volume").toDouble(); prod->divide_type = query.value("divide_type").toInt(); prod->divide_size = query.value("divide_size").toDouble(); prod->divide_factor = query.value("divide_factor").toDouble(); prod->divide_parts = query.value("divide_parts").toInt(); prod->divide_part = query.value("divide_part").toInt(); QJsonParseError parseError; const auto& f_json = query.value("json_fermentables").toString(); if (!f_json.trimmed().isEmpty()) { const auto& formattedJson = QString("%1").arg(f_json); QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; } else if (fermentables.isArray()) { double percentcheck = 0; for (int i = 0; i < fermentables.array().size(); i++) { QJsonObject obj = fermentables.array().at(i).toObject(); Fermentables f; f.inventory = 0; f.avail = false; f.name = obj["f_name"].toString(); f.origin = obj["f_origin"].toString(); f.supplier = obj["f_supplier"].toString(); f.amount = obj["f_amount"].toDouble(); f.cost = obj["f_cost"].toDouble(); f.type = obj["f_type"].toInt(); f.yield = obj["f_yield"].toDouble(); f.color = obj["f_color"].toDouble(); f.coarse_fine_diff = obj["f_coarse_fine_diff"].toDouble(); f.moisture = obj["f_moisture"].toDouble(); f.diastatic_power = obj["f_diastatic_power"].toDouble(); f.protein = obj["f_protein"].toDouble(); f.dissolved_protein = obj["f_dissolved_protein"].toDouble(); f.max_in_batch = obj["f_max_in_batch"].toDouble(); f.graintype = obj["f_graintype"].toInt(); f.added = obj["f_added"].toInt(); f.recommend_mash = obj["f_recommend_mash"].toInt() ? true:false; f.add_after_boil = obj["f_add_after_boil"].toInt() ? true:false; f.adjust_to_total_100 = obj["f_adjust_to_total_100"].toInt() ? true:false; f.percentage = obj["f_percentage"].toDouble(); f.di_ph = obj["f_di_ph"].toDouble(); f.acid_to_ph_57 = obj["f_acid_to_ph_57"].toDouble(); if (f.adjust_to_total_100) prod->fermentables_use100 = true; percentcheck += f.percentage; /* Check and update inventory */ yquery.prepare("SELECT inventory FROM inventory_fermentables WHERE name=:name AND supplier=:supplier AND origin=:origin"); yquery.bindValue(":name", f.name); yquery.bindValue(":supplier", f.supplier); yquery.bindValue(":origin", f.origin); yquery.exec(); if (yquery.first()) { f.avail = true; f.inventory = yquery.value(0).toDouble(); } prod->fermentables.append(f); } qDebug() << "fermentables" << prod->fermentables.size() << percentcheck; if (percentcheck == 0 && prod->fermentables.size()) { /* Missing percentages, fix it. */ double total = 0; for (int i = 0; i < prod->fermentables.size(); i++) { if (prod->fermentables.at(i).added < 4) total += prod->fermentables.at(i).amount; } for (int i = 0; i < prod->fermentables.size(); i++) { if (prod->fermentables.at(i).added < 4) prod->fermentables[i].percentage = round((prod->fermentables.at(i).amount / total) * 10000.0) / 100.0; } qInfo() << " fixed missing percentages"; // is_changed(); } } } else { qDebug() << "empty fermentables"; } const auto& h_json = query.value("json_hops").toString(); if (!h_json.trimmed().isEmpty()) { const auto& formattedJson = QString("%1").arg(h_json); QJsonDocument hops = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset; } else if (hops.isArray()) { for (int i = 0; i < hops.array().size(); i++) { QJsonObject obj = hops.array().at(i).toObject(); Hops h; h.avail = false; h.inventory = 0; h.name = obj["h_name"].toString(); h.origin = obj["h_origin"].toString(); h.amount = obj["h_amount"].toDouble(); h.cost = obj["h_cost"].toDouble(); h.type = obj["h_type"].toInt(); h.form = obj["h_form"].toInt(); h.useat = obj["h_useat"].toInt(); h.time = obj["h_time"].toInt(); h.alpha = obj["h_alpha"].toDouble(); h.beta = obj["h_beta"].toDouble(); h.hsi = obj["h_hsi"].toDouble(); h.humulene = obj["h_humulene"].toDouble(); h.caryophyllene = obj["h_caryophyllene"].toDouble(); h.cohumulone = obj["h_cohumulone"].toDouble(); h.myrcene = obj["h_myrcene"].toDouble(); h.total_oil = obj["h_total_oil"].toDouble(); /* Check and update inventory */ yquery.prepare("SELECT inventory FROM inventory_hops WHERE name=:name AND origin=:origin"); yquery.bindValue(":name", h.name); yquery.bindValue(":origin", h.origin); yquery.exec(); if (yquery.first()) { h.avail = true; h.inventory = yquery.value(0).toDouble(); } prod->hops.append(h); } qDebug() << "hops" << prod->hops.size(); } } else { qDebug() << "empty hops"; } const auto& json = query.value("json_miscs").toString(); if (!json.trimmed().isEmpty()) { const auto& formattedJson = QString("%1").arg(json); QJsonDocument miscs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset; } else if (miscs.isArray()) { for (int i = 0; i < miscs.array().size(); i++) { QJsonObject obj = miscs.array().at(i).toObject(); Miscs m; m.avail = false; m.inventory = 0; m.name = obj["name"].toString(); m.amount = obj["amount"].toDouble(); m.type = obj["type"].toInt(); m.use_use = obj["use_use"].toInt(); m.time = obj["time"].toDouble(); m.amount_is_weight = obj["amount_is_weight"].toInt() ? true:false; m.cost = obj["cost"].toDouble(); /* Check and update inventory */ yquery.prepare("SELECT inventory FROM inventory_miscs WHERE name=:name"); yquery.bindValue(":name", m.name); yquery.exec(); if (yquery.first()) { m.avail = true; m.inventory = yquery.value(0).toDouble(); } prod->miscs.append(m); } qDebug() << "miscs" << prod->miscs.size(); } } else { qDebug() << "empty miscs"; } const auto& y_json = query.value("json_yeasts").toString(); if (!y_json.trimmed().isEmpty()) { const auto& formattedJson = QString("%1").arg(y_json); QJsonDocument yeasts = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; } else if (yeasts.isArray()) { for (int i = 0; i < yeasts.array().size(); i++) { QJsonObject obj = yeasts.array().at(i).toObject(); Yeasts y; /* First some defaults for newer fields. */ y.tolerance = y.harvest_time = y.pitch_temperature = y.zymocide = 0; y.avail = y.sta1 = y.bacteria = y.harvest_top = y.pofpos = false; y.gr_hl_lo = 50; y.sg_lo = 1.04; y.gr_hl_hi = 80; y.sg_hi = 1.08; /* Now get what we have got */ y.name = obj["y_name"].toString(); y.laboratory = obj["y_laboratory"].toString(); y.product_id = obj["y_product_id"].toString(); y.amount = obj["y_amount"].toDouble(); y.type = obj["y_type"].toInt(); y.form = obj["y_form"].toInt(); y.min_temperature = obj["y_min_temperature"].toDouble(); y.max_temperature = obj["y_max_temperature"].toDouble(); y.flocculation = obj["y_flocculation"].toInt(); y.attenuation = obj["y_attenuation"].toDouble(); y.cells = obj["y_cells"].toDouble(); y.tolerance = obj["y_tolerance"].toDouble(); y.inventory = obj["y_inventory"].toDouble(); y.use = obj["y_use"].toInt(); y.sta1 = obj["y_sta1"].toInt() ? true:false; y.bacteria = obj["y_bacteria"].toInt() ? true:false; y.harvest_top = obj["y_harvest_top"].toInt() ? true:false; y.harvest_time = obj["y_harvest_time"].toInt(); y.pitch_temperature = obj["y_pitch_temperature"].toDouble(); y.pofpos = obj["y_pofpos"].toInt() ? true:false; y.zymocide = obj["y_zymocide"].toInt(); y.gr_hl_lo = obj["y_gr_hl_lo"].toInt(); y.sg_lo = obj["y_sg_lo"].toDouble(); y.gr_hl_hi = obj["y_gr_hl_hi"].toInt(); y.sg_hi = obj["y_sg_hi"].toDouble(); y.cost = obj["y_cost"].toDouble(); /* * Possible data upgrade needed. */ yquery.prepare("SELECT tolerance,cells,sta1,bacteria,harvest_top,harvest_time,pitch_temperature," "pofpos,zymocide,gr_hl_lo,sg_lo,gr_hl_hi,sg_hi,inventory " "FROM inventory_yeasts WHERE name=:name AND laboratory=:laboratory AND product_id=:product_id"); yquery.bindValue(":name", y.name); yquery.bindValue(":laboratory", y.laboratory); yquery.bindValue(":product_id", y.product_id); yquery.exec(); if (yquery.first()) { if (y.tolerance == 0 || y.cells == 0) { y.tolerance = yquery.value(0).toDouble(); y.cells = yquery.value(1).toDouble(); } y.sta1 = yquery.value(2).toInt() ? true:false; y.bacteria = yquery.value(3).toInt() ? true:false; y.harvest_top = yquery.value(4).toInt() ? true:false; y.harvest_time = yquery.value(5).toInt(); y.pitch_temperature = yquery.value(6).toDouble(); y.pofpos = yquery.value(7).toInt() ? true:false; y.zymocide = yquery.value(8).toInt(); y.gr_hl_lo = yquery.value(9).toInt(); y.sg_lo = yquery.value(10).toDouble(); y.gr_hl_hi = yquery.value(11).toInt(); y.sg_hi = yquery.value(12).toDouble(); y.inventory = yquery.value(13).toDouble(); y.avail = true; } else { qDebug() << y.name << y.product_id << "not found for upgrade"; } prod->yeasts.append(y); } qDebug() << "yeasts" << prod->yeasts.size(); } } else { qDebug() << "empty yeasts"; } const auto& ma_json = query.value("json_mashs").toString().trimmed(); if (!ma_json.trimmed().isEmpty()) { const auto& formattedJson = QString("%1").arg(ma_json); QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError) { qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; } else if (mashs.isArray()) { for (int i = 0; i < mashs.array().size(); i++) { QJsonObject obj = mashs.array().at(i).toObject(); MashSteps m; m.step_name = obj["step_name"].toString(); m.step_type = obj["step_type"].toInt(); m.step_volume = obj["step_volume"].toDouble(); m.step_infuse_amount = obj["step_infuse_amount"].toDouble(); m.step_infuse_temp = obj["step_infuse_temp"].toDouble(); m.step_temp = obj["step_temp"].toDouble(); m.step_time = obj["step_time"].toDouble(); m.ramp_time = obj["ramp_time"].toDouble(); m.end_temp = obj["end_temp"].toDouble(); m.step_wg_ratio = obj["step_wg_ratio"].toDouble(); m.step_ph = obj["step_ph"].toDouble(); m.step_sg = obj["step_sg"].toDouble(); prod->mashs.append(m); } } } else { qDebug() << "empty mashs"; } qDebug() << "mashs" << prod->mashs.size(); return true; } bool DB_product::save(Product *prod, QDialog *dialog) { QSqlQuery query; QString sql = ""; if (prod->record == -1) { sql = "INSERT INTO products SET "; } else { sql = "UPDATE products SET "; } sql.append("name=:name, code=:code, birth=:birth, stage=:stage, notes=:notes, log_brew=:log_brew, " "log_fermentation=:log_fermentation, log_ispindel=:log_ispindel, log_co2pressure=:log_co2pressure, " "inventory_reduced=:inventory_reduced, locked=:locked, " "eq_name=:eq_name, eq_boil_size=:eq_boil_size, eq_efficiency=:eq_efficiency, " "eq_batch_size=:eq_batch_size, eq_tun_volume=:eq_tun_volume, eq_tun_weight=:eq_tun_weight, " "eq_tun_specific_heat=:eq_tun_specific_heat, eq_tun_material=:eq_tun_material, eq_tun_height=:eq_tun_height, " "eq_top_up_water=:eq_top_up_water, eq_trub_chiller_loss=:eq_chiller_loss, eq_evap_rate=:eq_evap_rate, " "eq_boil_time=:eq_boil_time, eq_calc_boil_volume=:eq_calcboil, eq_top_up_kettle=:eq_top_up_kettle, " "eq_hop_utilization=:eq_hopfactor, eq_notes=:eq_notes, eq_lauter_volume=:eq_lauter_volume, " "eq_lauter_height=:eq_lauter_height, eq_lauter_deadspace=:eq_lauter_deadspace, eq_kettle_volume=:eq_kettle_volume, " "eq_kettle_height=:eq_kettle_height, eq_mash_volume=:eq_mash_volume, eq_mash_max=:eq_mash_max, " "brew_date_start=:brew_date_start, brew_mash_ph=:brew_mash_ph, brew_mash_sg=:brew_mash_sg, " "brew_mash_efficiency=:brew_mash_efficiency, brew_sparge_temperature=:brew_sparge_temperature, " "brew_sparge_volume=:brew_sparge_volume, brew_sparge_est=:brew_sparge_est, brew_sparge_ph=:brew_sparge_ph, " "brew_preboil_volume=:brew_preboil_volume, brew_preboil_sg=:brew_preboil_sg, brew_preboil_ph=:brew_preboil_ph, " "brew_preboil_efficiency=:brew_preboil_efficiency, brew_aboil_volume=:brew_aboil_volume, " "brew_aboil_sg=:brew_aboil_sg, brew_aboil_ph=:brew_aboil_ph, brew_aboil_efficiency=:brew_aboil_efficiency, " "brew_cooling_method=:brew_cooling_method, brew_cooling_time=:brew_cooling_time, brew_cooling_to=:brew_cooling_to, " "brew_whirlpool9=:brew_whirlpool9, brew_whirlpool7=:brew_whirlpool7, brew_whirlpool6=:brew_whirlpool6, " "brew_whirlpool2=:brew_whirlpool2, brew_fermenter_volume=:brew_fermenter_volume, " "brew_fermenter_extrawater=:brew_fermenter_extrawater, brew_fermenter_tcloss=:brew_fermenter_tcloss, " "brew_aeration_time=:brew_aeration_time, brew_aeration_speed=:brew_aeration_speed, " "brew_aeration_type=:brew_aeration_type, brew_fermenter_sg=:brew_fermenter_sg, brew_fermenter_ibu=:brew_fermenter_ibu, " "brew_fermenter_color=:brew_fermenter_color, brew_date_end=:brew_date_end, " "og=:og, fg=:fg, primary_start_temp=:primary_start_temp, primary_max_temp=:primary_max_temp, " "primary_end_temp=:primary_end_temp, primary_end_sg=:primary_end_sg, primary_end_date=:primary_end_date, " "secondary_temp=:secondary_temp, secondary_end_sg=:secondary_end_sg, secondary_end_date=:secondary_end_date, " "tertiary_temp=:tertiary_temp, package_date=:package_date, package_volume=:package_volume, " "package_infuse_amount=:package_infuse_amount, package_infuse_abv=:package_infuse_abv, " "package_infuse_notes=:package_infuse_notes, package_abv=:package_abv, package_ph=:package_ph, " "bottle_amount=:bottle_amount, bottle_carbonation=:bottle_carbonation, bottle_priming_sugar=:bottle_priming_sugar, " "bottle_priming_amount=:bottle_priming_amount, bottle_priming_water=:bottle_priming_water, " "bottle_carbonation_temp=:bottle_carbonation_temp, keg_amount=:keg_amount, keg_carbonation=:keg_carbonation, " "keg_priming_sugar=:keg_priming_sugar, keg_priming_amount=:keg_priming_amount, keg_priming_water=:keg_priming_water, " "keg_carbonation_temp=:keg_carbonation_temp, keg_forced_carb=:keg_forced_carb, keg_pressure=:keg_pressure, " "taste_notes=:taste_notes, taste_rate=:taste_rate, taste_date=:taste_date, taste_color=:taste_color, " "taste_transparency=:taste_transparency, taste_head=:taste_head, taste_aroma=:taste_aroma, " "taste_taste=:taste_taste, taste_mouthfeel=:taste_mouthfeel, taste_aftertaste=:taste_aftertaste, " "st_name=:st_name, st_letter=:st_letter, " "st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, " "st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, " "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, " "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, " "type=:type, batch_size=:batch_size, boil_size=:boil_size, " "boil_time=:boil_time, efficiency=:efficiency, est_og=:est_og, est_og3=:est_og3, est_fg=:est_fg, est_abv=:est_abv, " "est_color=:est_color, color_method=:color_method, est_ibu=:est_ibu, ibu_method=:ibu_method, " "est_carb=:est_carb, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, " "sparge_volume=:sparge_volume, sparge_source=:sparge_source, sparge_acid_type=:sparge_acid_type, " "sparge_acid_perc=:sparge_acid_perc, sparge_acid_amount=:sparge_acid_amount, mash_ph=:mash_ph, " "mash_name=:mash_name, calc_acid=:calc_acid, " "w1_name=:w1_name, w1_amount=:w1_amount, w1_calcium=:w1_calcium, w1_sulfate=:w1_sulfate, " "w1_chloride=:w1_chloride, w1_sodium=:w1_sodium, w1_magnesium=:w1_magnesium, " "w1_total_alkalinity=:w1_total_alkalinity, w1_ph=:w1_ph, w1_cost=:w1_cost, " "w2_name=:w2_name, w2_amount=:w2_amount, w2_calcium=:w2_calcium, w2_sulfate=:w2_sulfate, " "w2_chloride=:w2_chloride, w2_sodium=:w2_sodium, w2_magnesium=:w2_magnesium, " "w2_total_alkalinity=:w2_total_alkalinity, w2_ph=:w2_ph, w2_cost=:w2_cost, " "wg_amount=:wg_amount, wg_calcium=:wg_calcium, wg_sulfate=:wg_sulfate, " "wg_chloride=:wg_chloride, wg_sodium=:wg_sodium, wg_magnesium=:wg_magnesium, " "wg_total_alkalinity=:wg_total_alkalinity, wg_ph=:wg_ph, " "wb_calcium=:wb_calcium, wb_sulfate=:wb_sulfate, wb_chloride=:wb_chloride, wb_sodium=:wb_sodium, " "wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, " "wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, " "starter_enable=:starter_enable, starter_type=:starter_type, starter_sg=:starter_sg, " "starter_viability=:starter_viability, yeast_prod_date=:yeast_prod_date, yeast_pitchrate=:yeast_pitchrate, " "prop1_type=:prop1_type, prop1_volume=:prop1_volume, prop2_type=:prop2_type, prop2_volume=:prop2_volume, " "prop3_type=:prop3_type, prop3_volume=:prop3_volume, prop4_type=:prop4_type, prop4_volume=:prop4_volume, " "divide_type=:divide_type, divide_size=:divide_size, divide_factor=:divide_factor, " "divide_parts=:divide_parts, divide_part=:divide_part, " "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, " "json_yeasts=:json_yeasts, json_mashs=:json_mashs"); if (prod->record == -1) { sql.append(", uuid=:uuid"); } else { sql.append(" WHERE record = :recno"); } query.prepare(sql); query.bindValue(":name", prod->name); query.bindValue(":code", prod->code); query.bindValue(":birth", prod->birth); query.bindValue(":stage", prod->stage); query.bindValue(":notes", prod->notes); query.bindValue(":log_brew", prod->log_brew ? 1:0); query.bindValue(":log_fermentation", prod->log_fermentation ? 1:0); query.bindValue(":log_ispindel", prod->log_ispindel ? 1:0); query.bindValue(":log_co2pressure", prod->log_co2pressure ? 1:0); query.bindValue(":inventory_reduced", prod->inventory_reduced); query.bindValue(":locked", prod->locked ? 1:0); query.bindValue(":eq_name", prod->eq_name); query.bindValue(":eq_boil_size", round(prod->eq_boil_size * 10) / 10); query.bindValue(":eq_batch_size", round(prod->eq_batch_size * 100) / 100); query.bindValue(":eq_tun_volume", round(prod->eq_tun_volume * 10) / 10); query.bindValue(":eq_tun_weight", round(prod->eq_tun_weight * 10) / 10); query.bindValue(":eq_tun_specific_heat", round(prod->eq_tun_specific_heat * 1000) / 1000); query.bindValue(":eq_tun_material", prod->eq_tun_material); query.bindValue(":eq_tun_height", round(prod->eq_tun_height * 1000) / 1000); query.bindValue(":eq_top_up_water", round(prod->eq_top_up_water * 10) / 10); query.bindValue(":eq_chiller_loss", round(prod->eq_trub_chiller_loss * 10) / 10); query.bindValue(":eq_evap_rate", round(prod->eq_evap_rate * 100) / 100); query.bindValue(":eq_boil_time", round(prod->eq_boil_time)); query.bindValue(":eq_calcboil", prod->eq_calc_boil_volume ? 1:0); query.bindValue(":eq_top_up_kettle", round(prod->eq_top_up_kettle * 10) / 10); query.bindValue(":eq_hopfactor", round(prod->eq_hop_utilization)); query.bindValue(":eq_notes", prod->eq_notes); query.bindValue(":eq_lauter_volume", round(prod->eq_lauter_volume * 10) / 10); query.bindValue(":eq_lauter_height", round(prod->eq_lauter_height * 1000) / 1000); query.bindValue(":eq_lauter_deadspace", round(prod->eq_lauter_deadspace * 10) / 10); query.bindValue(":eq_kettle_volume", round(prod->eq_kettle_volume * 10) / 10); query.bindValue(":eq_kettle_height", round(prod->eq_kettle_height * 1000) / 1000); query.bindValue(":eq_mash_volume", round(prod->eq_mash_volume * 10) / 10); query.bindValue(":eq_mash_max", round(prod->eq_mash_max * 10) / 10); query.bindValue(":eq_efficiency", round(prod->eq_efficiency * 10) / 10); query.bindValue(":brew_date_start", prod->brew_date_start); query.bindValue(":brew_mash_ph", round(prod->brew_mash_ph * 100) / 100); query.bindValue(":brew_mash_sg", round(prod->brew_mash_sg * 1000) / 1000); query.bindValue(":brew_mash_efficiency", round(prod->brew_mash_efficiency * 10) / 10); query.bindValue(":brew_sparge_temperature", round(prod->brew_sparge_temperature * 10) / 10); query.bindValue(":brew_sparge_volume", round(prod->brew_sparge_volume * 10) / 10); query.bindValue(":brew_sparge_est", round(prod->brew_sparge_est * 10) / 10); query.bindValue(":brew_sparge_ph", round(prod->brew_sparge_ph * 100) / 100); query.bindValue(":brew_preboil_volume", round(prod->brew_preboil_volume * 10) / 10); query.bindValue(":brew_preboil_sg", round(prod->brew_preboil_sg * 1000) / 1000); query.bindValue(":brew_preboil_ph", round(prod->brew_preboil_ph * 100) / 100); query.bindValue(":brew_preboil_efficiency", round(prod->brew_preboil_efficiency * 10) / 10); query.bindValue(":brew_aboil_volume", round(prod->brew_aboil_volume * 10) / 10); query.bindValue(":brew_aboil_sg", round(prod->brew_aboil_sg * 1000) / 1000); query.bindValue(":brew_aboil_ph", round(prod->brew_aboil_ph * 100) / 100); query.bindValue(":brew_aboil_efficiency", round(prod->brew_aboil_efficiency * 10) / 10); query.bindValue(":brew_cooling_method", prod->brew_cooling_method); query.bindValue(":brew_cooling_time", round(prod->brew_cooling_time * 10) / 10); query.bindValue(":brew_cooling_to", round(prod->brew_cooling_to * 10) / 10); query.bindValue(":brew_whirlpool9", round(prod->brew_whirlpool9)); query.bindValue(":brew_whirlpool7", round(prod->brew_whirlpool7)); query.bindValue(":brew_whirlpool6", round(prod->brew_whirlpool6)); query.bindValue(":brew_whirlpool2", round(prod->brew_whirlpool2)); query.bindValue(":brew_fermenter_volume", round(prod->brew_fermenter_volume * 10) / 10); query.bindValue(":brew_fermenter_extrawater", round(prod->brew_fermenter_extrawater * 10) / 10); query.bindValue(":brew_fermenter_tcloss", round(prod->brew_fermenter_tcloss * 10) / 10); query.bindValue(":brew_aeration_time", round(prod->brew_aeration_time)); query.bindValue(":brew_aeration_speed", round(prod->brew_aeration_speed * 10) / 10); query.bindValue(":brew_aeration_type", prod->brew_aeration_type); query.bindValue(":brew_fermenter_sg", round(prod->brew_fermenter_sg * 1000) / 1000); query.bindValue(":brew_fermenter_ibu", round(prod->brew_fermenter_ibu * 10) / 10); query.bindValue(":brew_fermenter_color", round(prod->brew_fermenter_color * 10) / 10); query.bindValue(":brew_date_end", prod->brew_date_end); query.bindValue(":og", round(prod->og * 1000) / 1000); query.bindValue(":fg", round(prod->fg * 1000) / 1000); query.bindValue(":primary_start_temp", round(prod->primary_start_temp * 10) / 10); query.bindValue(":primary_max_temp", round(prod->primary_max_temp * 10) / 10); query.bindValue(":primary_end_temp", round(prod->primary_end_temp * 10) / 10); query.bindValue(":primary_end_sg", round(prod->primary_end_sg * 1000) / 1000); query.bindValue(":primary_end_date", prod->primary_end_date); query.bindValue(":secondary_temp", round(prod->secondary_temp * 10) / 10); query.bindValue(":secondary_end_sg", round(prod->secondary_end_sg * 1000) / 1000); query.bindValue(":secondary_end_date", prod->secondary_end_date); query.bindValue(":tertiary_temp", round(prod->tertiary_temp * 10) / 10); query.bindValue(":package_date", prod->package_date); query.bindValue(":package_volume", round(prod->package_volume * 10) / 10); query.bindValue(":package_infuse_amount", round(prod->package_infuse_amount * 1000) / 1000); query.bindValue(":package_infuse_abv", round(prod->package_infuse_abv * 100) / 100); query.bindValue(":package_infuse_notes", prod->package_infuse_notes); query.bindValue(":package_abv", round(prod->package_abv * 100) / 100); query.bindValue(":package_ph", round(prod->package_ph * 100) / 100); query.bindValue(":bottle_amount", round(prod->bottle_amount * 10) / 10); query.bindValue(":bottle_carbonation", round(prod->bottle_carbonation * 100) / 100); query.bindValue(":bottle_priming_sugar", prod->bottle_priming_sugar); query.bindValue(":bottle_priming_amount", round(prod->bottle_priming_amount * 10) / 10); query.bindValue(":bottle_priming_water", round(prod->bottle_priming_water * 1000) / 1000); query.bindValue(":bottle_carbonation_temp", round(prod->bottle_carbonation_temp * 10) / 10); query.bindValue(":keg_amount", round(prod->keg_amount * 10) / 10); query.bindValue(":keg_carbonation", round(prod->keg_carbonation * 100) / 100); query.bindValue(":keg_priming_sugar", prod->keg_priming_sugar); query.bindValue(":keg_priming_amount", round(prod->keg_priming_amount * 10) / 10); query.bindValue(":keg_priming_water", round(prod->keg_priming_water * 1000) / 1000); query.bindValue(":keg_carbonation_temp", round(prod->keg_carbonation_temp * 10) / 10); query.bindValue(":keg_forced_carb", prod->keg_forced_carb ? 1:0); query.bindValue(":keg_pressure", round(prod->keg_pressure * 10) / 10); query.bindValue(":taste_notes", prod->taste_notes); query.bindValue(":taste_rate", round(prod->taste_rate * 10) / 10); query.bindValue(":taste_date", prod->taste_date); query.bindValue(":taste_color", prod->taste_color); query.bindValue(":taste_transparency", prod->taste_transparency); query.bindValue(":taste_head", prod->taste_head); query.bindValue(":taste_aroma", prod->taste_aroma); query.bindValue(":taste_taste", prod->taste_taste); query.bindValue(":taste_mouthfeel", prod->taste_mouthfeel); query.bindValue(":taste_aftertaste", prod->taste_aftertaste); query.bindValue(":st_name", prod->st_name); query.bindValue(":st_letter", prod->st_letter); query.bindValue(":st_guide", prod->st_guide); query.bindValue(":st_category", prod->st_category); query.bindValue(":st_catnr", prod->st_category_number); query.bindValue(":st_type", prod->st_type); query.bindValue(":st_og_min", round(prod->st_og_min * 1000) / 1000); query.bindValue(":st_og_max", round(prod->st_og_max * 1000) / 1000); query.bindValue(":st_fg_min", round(prod->st_fg_min * 1000) / 1000); query.bindValue(":st_fg_max", round(prod->st_fg_max * 1000) / 1000); query.bindValue(":st_ibu_min", round(prod->st_ibu_min * 10) / 10); query.bindValue(":st_ibu_max", round(prod->st_ibu_max * 10) / 10); query.bindValue(":st_color_min", round(prod->st_color_min * 10) / 10); query.bindValue(":st_color_max", round(prod->st_color_max * 10) / 10); query.bindValue(":st_carb_min", round(prod->st_carb_min * 10) / 10); query.bindValue(":st_carb_max", round(prod->st_carb_max * 10) / 10); query.bindValue(":st_abv_min", round(prod->st_abv_min * 10) / 10); query.bindValue(":st_abv_max", round(prod->st_abv_max * 10) / 10); query.bindValue(":type", prod->type); query.bindValue(":batch_size", round(prod->batch_size * 10) / 10); query.bindValue(":boil_size", round(prod->boil_size * 10) / 10); query.bindValue(":boil_time", round(prod->boil_time * 10) / 10); query.bindValue(":efficiency", round(prod->efficiency * 10) / 10); query.bindValue(":est_og", round(prod->est_og * 1000) / 1000); query.bindValue(":est_og3", round(prod->est_og3 * 1000) / 1000); query.bindValue(":est_fg", round(prod->est_fg * 1000) / 1000); query.bindValue(":est_abv", round(prod->est_abv * 10) / 10); query.bindValue(":est_color", round(prod->est_color * 10) / 10); query.bindValue(":color_method", prod->color_method); query.bindValue(":est_ibu", round(prod->est_ibu * 10) / 10); query.bindValue(":ibu_method", prod->ibu_method); query.bindValue(":est_carb", round(prod->est_carb * 10) / 10); query.bindValue(":sparge_temp", round(prod->sparge_temp * 10) / 10); query.bindValue(":sparge_ph", round(prod->sparge_ph * 100) / 100); query.bindValue(":sparge_volume", round(prod->sparge_volume * 10) / 10); query.bindValue(":sparge_source", prod->sparge_source); query.bindValue(":sparge_acid_type", prod->sparge_acid_type); query.bindValue(":sparge_acid_perc", round(prod->sparge_acid_perc * 10) / 10); query.bindValue(":sparge_acid_amount", round(prod->sparge_acid_amount * 100000) / 100000); query.bindValue(":mash_ph", round(prod->mash_ph * 100) / 100); query.bindValue(":mash_name", prod->mash_name); query.bindValue(":calc_acid", prod->calc_acid ?1:0); query.bindValue(":w1_name", prod->w1_name); query.bindValue(":w1_amount", round(prod->w1_amount * 10) / 10); query.bindValue(":w1_calcium", round(prod->w1_calcium * 100000) / 100000); query.bindValue(":w1_sulfate", round(prod->w1_sulfate * 100000) / 100000); query.bindValue(":w1_chloride", round(prod->w1_chloride * 100000) / 100000); query.bindValue(":w1_sodium", round(prod->w1_sodium * 100000) / 100000); query.bindValue(":w1_magnesium", round(prod->w1_magnesium * 100000) / 100000); query.bindValue(":w1_total_alkalinity", round(prod->w1_total_alkalinity * 100000) / 100000); query.bindValue(":w1_ph", round(prod->w1_ph * 100) / 100); query.bindValue(":w1_cost", round(prod->w1_cost * 100) / 100); query.bindValue(":w2_name", prod->w2_name); query.bindValue(":w2_amount", round(prod->w2_amount * 10) / 10); query.bindValue(":w2_calcium", round(prod->w2_calcium * 100000) / 100000); query.bindValue(":w2_sulfate", round(prod->w2_sulfate * 100000) / 100000); query.bindValue(":w2_chloride", round(prod->w2_chloride * 100000) / 100000); query.bindValue(":w2_sodium", round(prod->w2_sodium * 100000) / 100000); query.bindValue(":w2_magnesium", round(prod->w2_magnesium * 100000) / 100000); query.bindValue(":w2_total_alkalinity", round(prod->w2_total_alkalinity * 100000) / 100000); query.bindValue(":w2_ph", round(prod->w2_ph * 100) / 100); query.bindValue(":w2_cost", round(prod->w2_cost * 100) / 100); query.bindValue(":wg_amount", round(prod->wg_amount * 10) / 10); query.bindValue(":wg_calcium", round(prod->wg_calcium * 100000) / 100000); query.bindValue(":wg_sulfate", round(prod->wg_sulfate * 100000) / 100000); query.bindValue(":wg_chloride", round(prod->wg_chloride * 100000) / 100000); query.bindValue(":wg_sodium", round(prod->wg_sodium * 100000) / 100000); query.bindValue(":wg_magnesium", round(prod->wg_magnesium * 100000) / 100000); query.bindValue(":wg_total_alkalinity", round(prod->wg_total_alkalinity * 100000) / 100000); query.bindValue(":wg_ph", round(prod->wg_ph * 100) / 100); query.bindValue(":wb_calcium", round(prod->wb_calcium * 100000) / 100000); query.bindValue(":wb_sulfate", round(prod->wb_sulfate * 100000) / 100000); query.bindValue(":wb_chloride", round(prod->wb_chloride * 100000) / 100000); query.bindValue(":wb_sodium", round(prod->wb_sodium * 100000) / 100000); query.bindValue(":wb_magnesium", round(prod->wb_magnesium * 100000) / 100000); query.bindValue(":wb_total_alkalinity", round(prod->wb_total_alkalinity * 100000) / 100000); query.bindValue(":wb_ph", round(prod->wb_ph * 100) / 100); query.bindValue(":wa_acid_name", prod->wa_acid_name); query.bindValue(":wa_acid_perc", round(prod->wa_acid_perc * 10) / 10); query.bindValue(":wa_base_name", prod->wa_base_name); query.bindValue(":starter_enable", prod->starter_enable ? 1:0); query.bindValue(":starter_type", prod->starter_type); query.bindValue(":starter_sg", round(prod->starter_sg * 1000) / 1000); query.bindValue(":starter_viability", prod->starter_viability); query.bindValue(":yeast_prod_date", prod->yeast_prod_date); query.bindValue(":yeast_pitchrate", round(prod->yeast_pitchrate * 1000) / 1000); query.bindValue(":prop1_type", prod->prop_type[0]); query.bindValue(":prop1_volume", round(prod->prop_volume[0] * 1000) / 1000); query.bindValue(":prop2_type", prod->prop_type[1]); query.bindValue(":prop2_volume", round(prod->prop_volume[1] * 1000) / 1000); query.bindValue(":prop3_type", prod->prop_type[2]); query.bindValue(":prop3_volume", round(prod->prop_volume[2] * 1000) / 1000); query.bindValue(":prop4_type", prod->prop_type[3]); query.bindValue(":prop4_volume", round(prod->prop_volume[3] * 1000) / 1000); query.bindValue(":divide_type", prod->divide_type); query.bindValue(":divide_size", round(prod->divide_size * 10) / 10); query.bindValue(":divide_factor", round(prod->divide_factor * 10000) / 10000); query.bindValue(":divide_parts", prod->divide_parts); query.bindValue(":divide_part", prod->divide_part); if (prod->fermentables.size() == 0) { query.bindValue(":json_fermentables", "[]"); } else { QJsonArray array; for (int i = 0; i < prod->fermentables.size(); i++) { QJsonObject obj; obj.insert("f_name", prod->fermentables.at(i).name); obj.insert("f_origin", prod->fermentables.at(i).origin); obj.insert("f_supplier", prod->fermentables.at(i).supplier); obj.insert("f_amount", round(prod->fermentables.at(i).amount * 100000) / 100000); obj.insert("f_cost", round(prod->fermentables.at(i).cost * 1000) / 1000); obj.insert("f_type", prod->fermentables.at(i).type); obj.insert("f_yield", round(prod->fermentables.at(i).yield * 10) / 10); obj.insert("f_color", round(prod->fermentables.at(i).color * 10) / 10); obj.insert("f_coarse_fine_diff", round(prod->fermentables.at(i).coarse_fine_diff * 10) / 10); obj.insert("f_moisture", round(prod->fermentables.at(i).moisture * 10) / 10); obj.insert("f_diastatic_power", round(prod->fermentables.at(i).diastatic_power * 100000) / 100000); obj.insert("f_protein", round(prod->fermentables.at(i).protein * 10) / 10); obj.insert("f_dissolved_protein", round(prod->fermentables.at(i).dissolved_protein * 10) / 10); obj.insert("f_max_in_batch", prod->fermentables.at(i).max_in_batch); obj.insert("f_graintype", prod->fermentables.at(i).graintype); obj.insert("f_added", prod->fermentables.at(i).added); obj.insert("f_recommend_mash", prod->fermentables.at(i).recommend_mash ? 1:0); obj.insert("f_add_after_boil", prod->fermentables.at(i).add_after_boil ? 1:0); obj.insert("f_adjust_to_total_100", prod->fermentables.at(i).adjust_to_total_100 ? 1:0); obj.insert("f_percentage", round(prod->fermentables.at(i).percentage * 10) / 10); obj.insert("f_di_ph", round(prod->fermentables.at(i).di_ph * 100000) / 100000); obj.insert("f_acid_to_ph_57", round(prod->fermentables.at(i).acid_to_ph_57 * 100000) / 100000); // qDebug() << "fermentables_Json" << i << obj; array.append(obj); /* Append this object */ } QJsonDocument doc; doc.setArray(array); query.bindValue(":json_fermentables", doc.toJson(QJsonDocument::Compact)); } if (prod->hops.size() == 0) { query.bindValue(":json_hops", "[]"); } else { QJsonArray array; for (int i = 0; i < prod->hops.size(); i++) { QJsonObject obj; obj.insert("h_name", prod->hops.at(i).name); obj.insert("h_origin", prod->hops.at(i).origin); obj.insert("h_amount", round(prod->hops.at(i).amount * 100000) / 100000); obj.insert("h_cost", round(prod->hops.at(i).cost * 100) / 100); obj.insert("h_type", prod->hops.at(i).type); obj.insert("h_form", prod->hops.at(i).form); obj.insert("h_useat", prod->hops.at(i).useat); obj.insert("h_time", round(prod->hops.at(i).time)); obj.insert("h_alpha", round(prod->hops.at(i).alpha * 100) / 100); obj.insert("h_beta", round(prod->hops.at(i).beta * 100) / 100); obj.insert("h_hsi", round(prod->hops.at(i).hsi * 100) / 100); obj.insert("h_humulene", round(prod->hops.at(i).humulene * 100) / 100); obj.insert("h_caryophyllene", round(prod->hops.at(i).caryophyllene * 100) / 100); obj.insert("h_cohumulone", round(prod->hops.at(i).cohumulone * 100) / 100); obj.insert("h_myrcene", round(prod->hops.at(i).myrcene * 100) / 100); obj.insert("h_total_oil", round(prod->hops.at(i).total_oil * 100) / 100); // qDebug() << "hops_Json" << i << obj; array.append(obj); /* Append this object */ } QJsonDocument doc; doc.setArray(array); query.bindValue(":json_hops", doc.toJson(QJsonDocument::Compact)); } if (prod->miscs.size() == 0) { query.bindValue(":json_miscs", "[]"); } else { QJsonArray array; for (int i = 0; i < prod->miscs.size(); i++) { QJsonObject obj; obj.insert("name", prod->miscs.at(i).name); obj.insert("amount", round(prod->miscs.at(i).amount * 100000) / 100000); obj.insert("type", prod->miscs.at(i).type); obj.insert("use_use", prod->miscs.at(i).use_use); obj.insert("time", round(prod->miscs.at(i).time)); obj.insert("amount_is_weight", prod->miscs.at(i).amount_is_weight ? 1:0); obj.insert("cost", round(prod->miscs.at(i).cost * 10000) / 10000); // qDebug() << "miscs_Json" << i << obj; array.append(obj); /* Append this object */ } QJsonDocument doc; doc.setArray(array); query.bindValue(":json_miscs", doc.toJson(QJsonDocument::Compact)); } if (prod->yeasts.size() == 0) { query.bindValue(":json_yeasts", "[]"); } else { QJsonArray array; for (int i = 0; i < prod->yeasts.size(); i++) { QJsonObject obj; obj.insert("y_name", prod->yeasts.at(i).name); obj.insert("y_laboratory", prod->yeasts.at(i).laboratory); obj.insert("y_product_id", prod->yeasts.at(i).product_id); obj.insert("y_amount", round(prod->yeasts.at(i).amount * 100000) / 100000); obj.insert("y_type", prod->yeasts.at(i).type); obj.insert("y_form", prod->yeasts.at(i).form); obj.insert("y_min_temperature", round(prod->yeasts.at(i).min_temperature * 10) / 10); obj.insert("y_max_temperature", round(prod->yeasts.at(i).max_temperature * 10) / 10); obj.insert("y_flocculation", prod->yeasts.at(i).flocculation); obj.insert("y_attenuation", round(prod->yeasts.at(i).attenuation * 10) / 10); obj.insert("y_cells", prod->yeasts.at(i).cells); obj.insert("y_tolerance", round(prod->yeasts.at(i).tolerance * 10) / 10); obj.insert("y_inventory", round(prod->yeasts.at(i).inventory * 10000) / 10000); obj.insert("y_use", prod->yeasts.at(i).use); obj.insert("y_sta1", prod->yeasts.at(i).sta1 ? 1:0); obj.insert("y_bacteria", prod->yeasts.at(i).bacteria ? 1:0); obj.insert("y_harvest_top", prod->yeasts.at(i).harvest_top ? 1:0); obj.insert("y_harvest_time", prod->yeasts.at(i).harvest_time); obj.insert("y_pitch_temperature", round(prod->yeasts.at(i).pitch_temperature * 10) / 10); obj.insert("y_pofpos", prod->yeasts.at(i).pofpos ? 1:0); obj.insert("y_zymocide", prod->yeasts.at(i).zymocide); obj.insert("y_gr_hl_lo", prod->yeasts.at(i).gr_hl_lo); obj.insert("y_sg_lo", round(prod->yeasts.at(i).sg_lo * 1000) / 1000); obj.insert("y_gr_hl_hi", prod->yeasts.at(i).gr_hl_hi); obj.insert("y_sg_hi", round(prod->yeasts.at(i).sg_hi * 1000) / 1000); obj.insert("y_cost", round(prod->yeasts.at(i).cost * 1000) / 1000); // qDebug() << "yeasts_Json" << i << obj; array.append(obj); /* Append this object */ } QJsonDocument doc; doc.setArray(array); query.bindValue(":json_yeasts", doc.toJson(QJsonDocument::Compact)); } if (prod->mashs.size() == 0) { query.bindValue(":json_mashs", "[]"); } else { QJsonArray array; for (int i = 0; i < prod->mashs.size(); i++) { QJsonObject obj; obj.insert("step_name", prod->mashs.at(i).step_name); obj.insert("step_type", prod->mashs.at(i).step_type); obj.insert("step_volume", round(prod->mashs.at(i).step_volume * 100) / 100); obj.insert("step_infuse_amount", round(prod->mashs.at(i).step_infuse_amount * 100) / 100); obj.insert("step_infuse_temp", round(prod->mashs.at(i).step_infuse_temp * 100) / 100); obj.insert("step_temp", round(prod->mashs.at(i).step_temp * 100) / 100); obj.insert("step_time", round(prod->mashs.at(i).step_time * 100) / 100); obj.insert("ramp_time", round(prod->mashs.at(i).ramp_time * 100) / 100); obj.insert("end_temp", round(prod->mashs.at(i).end_temp * 100) / 100); obj.insert("step_wg_ratio", round(prod->mashs.at(i).step_wg_ratio * 100) / 100); obj.insert("step_ph", round(prod->mashs.at(i).step_ph * 100) / 100); obj.insert("step_sg", round(prod->mashs.at(i).step_sg * 10000) / 10000); // qDebug() << "mashs_Json" << i << obj; array.append(obj); /* Append this object */ } QJsonDocument doc; doc.setArray(array); query.bindValue(":json_mashs", doc.toJson(QJsonDocument::Compact)); } if (prod->record == -1) { query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); } else { query.bindValue(":recno", prod->record); } query.exec(); if (query.lastError().isValid()) { qWarning() << "saveProduct" << query.lastError(); QMessageBox::warning(dialog, QObject::tr("Database error"), QObject::tr("MySQL error: %1\n%2\n%3") .arg(query.lastError().nativeErrorCode()) .arg(query.lastError().driverText()) .arg(query.lastError().databaseText())); return false; } /* * If this was a new product, find out what record number we * have got and set it. So when the user saves this record * again, it will be updated instead of inserting a new copy. */ if (prod->record < 0) { QVariant id = query.lastInsertId(); prod->record = id.toInt(); qInfo() << "saveProduct Inserted record" << prod->record; } else { qInfo() << "saveProduct Updated record" << prod->record; } return true; } bool DB_product::dele(QDialog *dialog, int recno) { QSqlQuery query; query.prepare("DELETE FROM products WHERE record = :recno"); query.bindValue(":recno", recno); query.exec(); if (query.lastError().isValid()) { qWarning() << "deleteProduct" << query.lastError(); QMessageBox::warning(dialog, QObject::tr("Database error"), QObject::tr("MySQL error: %1\n%2\n%3") .arg(query.lastError().nativeErrorCode()) .arg(query.lastError().driverText()) .arg(query.lastError().databaseText())); return false; } qInfo() << "deleteProduct Deleted" << recno; return true; }