src/database/db_product.cpp

changeset 262
6bab9440aeb9
parent 256
c136bd15b084
child 270
a0f72d7ace1f
equal deleted inserted replaced
261:246893ad04a6 262:6bab9440aeb9
536 536
537 537
538 bool DB_product::save(Product *prod, QDialog *dialog) 538 bool DB_product::save(Product *prod, QDialog *dialog)
539 { 539 {
540 QSqlQuery query; 540 QSqlQuery query;
541 QString sql = "";
541 542
542 if (prod->record == -1) { 543 if (prod->record == -1) {
543 query.prepare("INSERT INTO products SET " 544 sql = "INSERT INTO products SET ";
544 "name=:name, code=:code, birth=:birth, stage=:stage, notes=:notes, log_brew=:log_brew, " 545 } else {
546 sql = "UPDATE products SET ";
547 }
548 sql.append("name=:name, code=:code, birth=:birth, stage=:stage, notes=:notes, log_brew=:log_brew, "
545 "log_fermentation=:log_fermentation, log_ispindel=:log_ispindel, log_co2pressure=:log_co2pressure, " 549 "log_fermentation=:log_fermentation, log_ispindel=:log_ispindel, log_co2pressure=:log_co2pressure, "
546 "inventory_reduced=:inventory_reduced, locked=:locked, " 550 "inventory_reduced=:inventory_reduced, locked=:locked, "
547 "eq_name=:eq_name, eq_boil_size=:eq_boil_size, eq_efficiency=:eq_efficiency, " 551 "eq_name=:eq_name, eq_boil_size=:eq_boil_size, eq_efficiency=:eq_efficiency, "
548 "eq_batch_size=:eq_batch_size, eq_tun_volume=:eq_tun_volume, eq_tun_weight=:eq_tun_weight, " 552 "eq_batch_size=:eq_batch_size, eq_tun_volume=:eq_tun_volume, eq_tun_weight=:eq_tun_weight, "
549 "eq_tun_specific_heat=:eq_tun_specific_heat, eq_tun_material=:eq_tun_material, eq_tun_height=:eq_tun_height, " 553 "eq_tun_specific_heat=:eq_tun_specific_heat, eq_tun_material=:eq_tun_material, eq_tun_height=:eq_tun_height, "
608 "prop1_type=:prop1_type, prop1_volume=:prop1_volume, prop2_type=:prop2_type, prop2_volume=:prop2_volume, " 612 "prop1_type=:prop1_type, prop1_volume=:prop1_volume, prop2_type=:prop2_type, prop2_volume=:prop2_volume, "
609 "prop3_type=:prop3_type, prop3_volume=:prop3_volume, prop4_type=:prop4_type, prop4_volume=:prop4_volume, " 613 "prop3_type=:prop3_type, prop3_volume=:prop3_volume, prop4_type=:prop4_type, prop4_volume=:prop4_volume, "
610 "divide_type=:divide_type, divide_size=:divide_size, divide_factor=:divide_factor, " 614 "divide_type=:divide_type, divide_size=:divide_size, divide_factor=:divide_factor, "
611 "divide_parts=:divide_parts, divide_part=:divide_part, " 615 "divide_parts=:divide_parts, divide_part=:divide_part, "
612 "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, " 616 "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, "
613 "json_yeasts=:json_yeasts, json_mashs=:json_mashs, uuid=:uuid"); 617 "json_yeasts=:json_yeasts, json_mashs=:json_mashs");
614 } else { 618
615 query.prepare("UPDATE products SET " 619 if (prod->record == -1) {
616 "name=:name, code=:code, birth=:birth, stage=:stage, notes=:notes, log_brew=:log_brew, " 620 sql.append(", uuid=:uuid");
617 "log_fermentation=:log_fermentation, log_ispindel=:log_ispindel, log_co2pressure=:log_co2pressure, " 621 } else {
618 "inventory_reduced=:inventory_reduced, locked=:locked, " 622 sql.append(" WHERE record = :recno");
619 "eq_name=:eq_name, eq_boil_size=:eq_boil_size, eq_efficiency=:eq_efficiency, " 623 }
620 "eq_batch_size=:eq_batch_size, eq_tun_volume=:eq_tun_volume, eq_tun_weight=:eq_tun_weight, " 624
621 "eq_tun_specific_heat=:eq_tun_specific_heat, eq_tun_material=:eq_tun_material, eq_tun_height=:eq_tun_height, " 625 query.prepare(sql);
622 "eq_top_up_water=:eq_top_up_water, eq_trub_chiller_loss=:eq_chiller_loss, eq_evap_rate=:eq_evap_rate, "
623 "eq_boil_time=:eq_boil_time, eq_calc_boil_volume=:eq_calcboil, eq_top_up_kettle=:eq_top_up_kettle, "
624 "eq_hop_utilization=:eq_hopfactor, eq_notes=:eq_notes, eq_lauter_volume=:eq_lauter_volume, "
625 "eq_lauter_height=:eq_lauter_height, eq_lauter_deadspace=:eq_lauter_deadspace, eq_kettle_volume=:eq_kettle_volume, "
626 "eq_kettle_height=:eq_kettle_height, eq_mash_volume=:eq_mash_volume, eq_mash_max=:eq_mash_max, "
627 "brew_date_start=:brew_date_start, brew_mash_ph=:brew_mash_ph, brew_mash_sg=:brew_mash_sg, "
628 "brew_mash_efficiency=:brew_mash_efficiency, brew_sparge_temperature=:brew_sparge_temperature, "
629 "brew_sparge_volume=:brew_sparge_volume, brew_sparge_est=:brew_sparge_est, brew_sparge_ph=:brew_sparge_ph, "
630 "brew_preboil_volume=:brew_preboil_volume, brew_preboil_sg=:brew_preboil_sg, brew_preboil_ph=:brew_preboil_ph, "
631 "brew_preboil_efficiency=:brew_preboil_efficiency, brew_aboil_volume=:brew_aboil_volume, "
632 "brew_aboil_sg=:brew_aboil_sg, brew_aboil_ph=:brew_aboil_ph, brew_aboil_efficiency=:brew_aboil_efficiency, "
633 "brew_cooling_method=:brew_cooling_method, brew_cooling_time=:brew_cooling_time, brew_cooling_to=:brew_cooling_to, "
634 "brew_whirlpool9=:brew_whirlpool9, brew_whirlpool7=:brew_whirlpool7, brew_whirlpool6=:brew_whirlpool6, "
635 "brew_whirlpool2=:brew_whirlpool2, brew_fermenter_volume=:brew_fermenter_volume, "
636 "brew_fermenter_extrawater=:brew_fermenter_extrawater, brew_fermenter_tcloss=:brew_fermenter_tcloss, "
637 "brew_aeration_time=:brew_aeration_time, brew_aeration_speed=:brew_aeration_speed, "
638 "brew_aeration_type=:brew_aeration_type, brew_fermenter_sg=:brew_fermenter_sg, brew_fermenter_ibu=:brew_fermenter_ibu, "
639 "brew_fermenter_color=:brew_fermenter_color, brew_date_end=:brew_date_end, "
640 "og=:og, fg=:fg, primary_start_temp=:primary_start_temp, primary_max_temp=:primary_max_temp, "
641 "primary_end_temp=:primary_end_temp, primary_end_sg=:primary_end_sg, primary_end_date=:primary_end_date, "
642 "secondary_temp=:secondary_temp, secondary_end_sg=:secondary_end_sg, secondary_end_date=:secondary_end_date, "
643 "tertiary_temp=:tertiary_temp, package_date=:package_date, package_volume=:package_volume, "
644 "package_infuse_amount=:package_infuse_amount, package_infuse_abv=:package_infuse_abv, "
645 "package_infuse_notes=:package_infuse_notes, package_abv=:package_abv, package_ph=:package_ph, "
646 "bottle_amount=:bottle_amount, bottle_carbonation=:bottle_carbonation, bottle_priming_sugar=:bottle_priming_sugar, "
647 "bottle_priming_amount=:bottle_priming_amount, bottle_priming_water=:bottle_priming_water, "
648 "bottle_carbonation_temp=:bottle_carbonation_temp, keg_amount=:keg_amount, keg_carbonation=:keg_carbonation, "
649 "keg_priming_sugar=:keg_priming_sugar, keg_priming_amount=:keg_priming_amount, keg_priming_water=:keg_priming_water, "
650 "keg_carbonation_temp=:keg_carbonation_temp, keg_forced_carb=:keg_forced_carb, keg_pressure=:keg_pressure, "
651 "taste_notes=:taste_notes, taste_rate=:taste_rate, taste_date=:taste_date, taste_color=:taste_color, "
652 "taste_transparency=:taste_transparency, taste_head=:taste_head, taste_aroma=:taste_aroma, "
653 "taste_taste=:taste_taste, taste_mouthfeel=:taste_mouthfeel, taste_aftertaste=:taste_aftertaste, "
654 "st_name=:st_name, st_letter=:st_letter, "
655 "st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, "
656 "st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, "
657 "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, "
658 "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, "
659 "type=:type, batch_size=:batch_size, boil_size=:boil_size, "
660 "boil_time=:boil_time, efficiency=:efficiency, est_og=:est_og, est_og3=:est_og3, est_fg=:est_fg, est_abv=:est_abv, "
661 "est_color=:est_color, color_method=:color_method, est_ibu=:est_ibu, ibu_method=:ibu_method, "
662 "est_carb=:est_carb, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, "
663 "sparge_volume=:sparge_volume, sparge_source=:sparge_source, sparge_acid_type=:sparge_acid_type, "
664 "sparge_acid_perc=:sparge_acid_perc, sparge_acid_amount=:sparge_acid_amount, mash_ph=:mash_ph, "
665 "mash_name=:mash_name, calc_acid=:calc_acid, "
666 "w1_name=:w1_name, w1_amount=:w1_amount, w1_calcium=:w1_calcium, w1_sulfate=:w1_sulfate, "
667 "w1_chloride=:w1_chloride, w1_sodium=:w1_sodium, w1_magnesium=:w1_magnesium, "
668 "w1_total_alkalinity=:w1_total_alkalinity, w1_ph=:w1_ph, w1_cost=:w1_cost, "
669 "w2_name=:w2_name, w2_amount=:w2_amount, w2_calcium=:w2_calcium, w2_sulfate=:w2_sulfate, "
670 "w2_chloride=:w2_chloride, w2_sodium=:w2_sodium, w2_magnesium=:w2_magnesium, "
671 "w2_total_alkalinity=:w2_total_alkalinity, w2_ph=:w2_ph, w2_cost=:w2_cost, "
672 "wg_amount=:wg_amount, wg_calcium=:wg_calcium, wg_sulfate=:wg_sulfate, "
673 "wg_chloride=:wg_chloride, wg_sodium=:wg_sodium, wg_magnesium=:wg_magnesium, "
674 "wg_total_alkalinity=:wg_total_alkalinity, wg_ph=:wg_ph, "
675 "wb_calcium=:wb_calcium, wb_sulfate=:wb_sulfate, wb_chloride=:wb_chloride, wb_sodium=:wb_sodium, "
676 "wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, "
677 "wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, "
678 "starter_enable=:starter_enable, starter_type=:starter_type, starter_sg=:starter_sg, "
679 "starter_viability=:starter_viability, yeast_prod_date=:yeast_prod_date, yeast_pitchrate=:yeast_pitchrate, "
680 "prop1_type=:prop1_type, prop1_volume=:prop1_volume, prop2_type=:prop2_type, prop2_volume=:prop2_volume, "
681 "prop3_type=:prop3_type, prop3_volume=:prop3_volume, prop4_type=:prop4_type, prop4_volume=:prop4_volume, "
682 "divide_type=:divide_type, divide_size=:divide_size, divide_factor=:divide_factor, "
683 "divide_parts=:divide_parts, divide_part=:divide_part, "
684 "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, "
685 "json_yeasts=:json_yeasts, json_mashs=:json_mashs WHERE record = :recno");
686 }
687
688 query.bindValue(":name", prod->name); 626 query.bindValue(":name", prod->name);
689 query.bindValue(":code", prod->code); 627 query.bindValue(":code", prod->code);
690 query.bindValue(":birth", prod->birth); 628 query.bindValue(":birth", prod->birth);
691 query.bindValue(":stage", prod->stage); 629 query.bindValue(":stage", prod->stage);
692 query.bindValue(":notes", prod->notes); 630 query.bindValue(":notes", prod->notes);
921 obj.insert("f_add_after_boil", prod->fermentables.at(i).f_add_after_boil ? 1:0); 859 obj.insert("f_add_after_boil", prod->fermentables.at(i).f_add_after_boil ? 1:0);
922 obj.insert("f_adjust_to_total_100", prod->fermentables.at(i).f_adjust_to_total_100 ? 1:0); 860 obj.insert("f_adjust_to_total_100", prod->fermentables.at(i).f_adjust_to_total_100 ? 1:0);
923 obj.insert("f_percentage", round(prod->fermentables.at(i).f_percentage * 10) / 10); 861 obj.insert("f_percentage", round(prod->fermentables.at(i).f_percentage * 10) / 10);
924 obj.insert("f_di_ph", round(prod->fermentables.at(i).f_di_ph * 100000) / 100000); 862 obj.insert("f_di_ph", round(prod->fermentables.at(i).f_di_ph * 100000) / 100000);
925 obj.insert("f_acid_to_ph_57", round(prod->fermentables.at(i).f_acid_to_ph_57 * 100000) / 100000); 863 obj.insert("f_acid_to_ph_57", round(prod->fermentables.at(i).f_acid_to_ph_57 * 100000) / 100000);
926 qDebug() << "fermentables_Json" << i << obj; 864 // qDebug() << "fermentables_Json" << i << obj;
927 array.append(obj); /* Append this object */ 865 array.append(obj); /* Append this object */
928 } 866 }
929 QJsonDocument doc; 867 QJsonDocument doc;
930 doc.setArray(array); 868 doc.setArray(array);
931 query.bindValue(":json_fermentables", doc.toJson(QJsonDocument::Compact)); 869 query.bindValue(":json_fermentables", doc.toJson(QJsonDocument::Compact));
951 obj.insert("h_humulene", round(prod->hops.at(i).h_humulene * 100) / 100); 889 obj.insert("h_humulene", round(prod->hops.at(i).h_humulene * 100) / 100);
952 obj.insert("h_caryophyllene", round(prod->hops.at(i).h_caryophyllene * 100) / 100); 890 obj.insert("h_caryophyllene", round(prod->hops.at(i).h_caryophyllene * 100) / 100);
953 obj.insert("h_cohumulone", round(prod->hops.at(i).h_cohumulone * 100) / 100); 891 obj.insert("h_cohumulone", round(prod->hops.at(i).h_cohumulone * 100) / 100);
954 obj.insert("h_myrcene", round(prod->hops.at(i).h_myrcene * 100) / 100); 892 obj.insert("h_myrcene", round(prod->hops.at(i).h_myrcene * 100) / 100);
955 obj.insert("h_total_oil", round(prod->hops.at(i).h_total_oil * 100) / 100); 893 obj.insert("h_total_oil", round(prod->hops.at(i).h_total_oil * 100) / 100);
956 qDebug() << "hops_Json" << i << obj; 894 // qDebug() << "hops_Json" << i << obj;
957 array.append(obj); /* Append this object */ 895 array.append(obj); /* Append this object */
958 } 896 }
959 QJsonDocument doc; 897 QJsonDocument doc;
960 doc.setArray(array); 898 doc.setArray(array);
961 query.bindValue(":json_hops", doc.toJson(QJsonDocument::Compact)); 899 query.bindValue(":json_hops", doc.toJson(QJsonDocument::Compact));
972 obj.insert("m_type", prod->miscs.at(i).m_type); 910 obj.insert("m_type", prod->miscs.at(i).m_type);
973 obj.insert("m_use_use", prod->miscs.at(i).m_use_use); 911 obj.insert("m_use_use", prod->miscs.at(i).m_use_use);
974 obj.insert("m_time", round(prod->miscs.at(i).m_time)); 912 obj.insert("m_time", round(prod->miscs.at(i).m_time));
975 obj.insert("m_amount_is_weight", prod->miscs.at(i).m_amount_is_weight ? 1:0); 913 obj.insert("m_amount_is_weight", prod->miscs.at(i).m_amount_is_weight ? 1:0);
976 obj.insert("m_cost", round(prod->miscs.at(i).m_cost * 10000) / 10000); 914 obj.insert("m_cost", round(prod->miscs.at(i).m_cost * 10000) / 10000);
977 qDebug() << "miscs_Json" << i << obj; 915 // qDebug() << "miscs_Json" << i << obj;
978 array.append(obj); /* Append this object */ 916 array.append(obj); /* Append this object */
979 } 917 }
980 QJsonDocument doc; 918 QJsonDocument doc;
981 doc.setArray(array); 919 doc.setArray(array);
982 query.bindValue(":json_miscs", doc.toJson(QJsonDocument::Compact)); 920 query.bindValue(":json_miscs", doc.toJson(QJsonDocument::Compact));
1012 obj.insert("y_gr_hl_lo", prod->yeasts.at(i).y_gr_hl_lo); 950 obj.insert("y_gr_hl_lo", prod->yeasts.at(i).y_gr_hl_lo);
1013 obj.insert("y_sg_lo", round(prod->yeasts.at(i).y_sg_lo * 1000) / 1000); 951 obj.insert("y_sg_lo", round(prod->yeasts.at(i).y_sg_lo * 1000) / 1000);
1014 obj.insert("y_gr_hl_hi", prod->yeasts.at(i).y_gr_hl_hi); 952 obj.insert("y_gr_hl_hi", prod->yeasts.at(i).y_gr_hl_hi);
1015 obj.insert("y_sg_hi", round(prod->yeasts.at(i).y_sg_hi * 1000) / 1000); 953 obj.insert("y_sg_hi", round(prod->yeasts.at(i).y_sg_hi * 1000) / 1000);
1016 obj.insert("y_cost", round(prod->yeasts.at(i).y_cost * 1000) / 1000); 954 obj.insert("y_cost", round(prod->yeasts.at(i).y_cost * 1000) / 1000);
1017 qDebug() << "yeasts_Json" << i << obj; 955 // qDebug() << "yeasts_Json" << i << obj;
1018 array.append(obj); /* Append this object */ 956 array.append(obj); /* Append this object */
1019 } 957 }
1020 QJsonDocument doc; 958 QJsonDocument doc;
1021 doc.setArray(array); 959 doc.setArray(array);
1022 query.bindValue(":json_yeasts", doc.toJson(QJsonDocument::Compact)); 960 query.bindValue(":json_yeasts", doc.toJson(QJsonDocument::Compact));
1038 obj.insert("ramp_time", round(prod->mashs.at(i).ramp_time * 100) / 100); 976 obj.insert("ramp_time", round(prod->mashs.at(i).ramp_time * 100) / 100);
1039 obj.insert("end_temp", round(prod->mashs.at(i).end_temp * 100) / 100); 977 obj.insert("end_temp", round(prod->mashs.at(i).end_temp * 100) / 100);
1040 obj.insert("step_wg_ratio", round(prod->mashs.at(i).step_wg_ratio * 100) / 100); 978 obj.insert("step_wg_ratio", round(prod->mashs.at(i).step_wg_ratio * 100) / 100);
1041 obj.insert("step_ph", round(prod->mashs.at(i).step_ph * 100) / 100); 979 obj.insert("step_ph", round(prod->mashs.at(i).step_ph * 100) / 100);
1042 obj.insert("step_sg", round(prod->mashs.at(i).step_sg * 10000) / 10000); 980 obj.insert("step_sg", round(prod->mashs.at(i).step_sg * 10000) / 10000);
1043 qDebug() << "mashs_Json" << i << obj; 981 // qDebug() << "mashs_Json" << i << obj;
1044 array.append(obj); /* Append this object */ 982 array.append(obj); /* Append this object */
1045 } 983 }
1046 QJsonDocument doc; 984 QJsonDocument doc;
1047 doc.setArray(array); 985 doc.setArray(array);
1048 query.bindValue(":json_mashs", doc.toJson(QJsonDocument::Compact)); 986 query.bindValue(":json_mashs", doc.toJson(QJsonDocument::Compact));
1052 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); 990 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
1053 } else { 991 } else {
1054 query.bindValue(":recno", prod->record); 992 query.bindValue(":recno", prod->record);
1055 } 993 }
1056 query.exec(); 994 query.exec();
1057 qDebug() << query.lastQuery();
1058 if (query.lastError().isValid()) { 995 if (query.lastError().isValid()) {
1059 qWarning() << "saveProduct" << query.lastError(); 996 qWarning() << "saveProduct" << query.lastError();
1060 QMessageBox::warning(dialog, QObject::tr("Database error"), 997 QMessageBox::warning(dialog, QObject::tr("Database error"),
1061 QObject::tr("MySQL error: %1\n%2\n%3") 998 QObject::tr("MySQL error: %1\n%2\n%3")
1062 .arg(query.lastError().nativeErrorCode()) 999 .arg(query.lastError().nativeErrorCode())
1063 .arg(query.lastError().driverText()) 1000 .arg(query.lastError().driverText())
1064 .arg(query.lastError().databaseText())); 1001 .arg(query.lastError().databaseText()));
1065 return false; 1002 return false;
1066 } else { 1003 }
1067 /* 1004
1068 * If this was a new product, find out what record number we 1005 /*
1069 * have got and set it. So when the user saves this record 1006 * If this was a new product, find out what record number we
1070 * again, it will be updated instead of inserting a new copy. 1007 * have got and set it. So when the user saves this record
1071 */ 1008 * again, it will be updated instead of inserting a new copy.
1072 if (prod->record < 0) { 1009 */
1073 QVariant id = query.lastInsertId(); 1010 if (prod->record < 0) {
1074 prod->record = id.toInt(); 1011 QVariant id = query.lastInsertId();
1075 qInfo() << "saveProduct Inserted record" << prod->record; 1012 prod->record = id.toInt();
1076 } else { 1013 qInfo() << "saveProduct Inserted record" << prod->record;
1077 qInfo() << "saveProduct Updated record" << prod->record; 1014 } else {
1078 } 1015 qInfo() << "saveProduct Updated record" << prod->record;
1079 } 1016 }
1080 return true; 1017 return true;
1081 } 1018 }
1082 1019
1083 1020

mercurial