275 } else if (fermentables.isArray()) { |
275 } else if (fermentables.isArray()) { |
276 double percentcheck = 0; |
276 double percentcheck = 0; |
277 for (int i = 0; i < fermentables.array().size(); i++) { |
277 for (int i = 0; i < fermentables.array().size(); i++) { |
278 QJsonObject obj = fermentables.array().at(i).toObject(); |
278 QJsonObject obj = fermentables.array().at(i).toObject(); |
279 Fermentables f; |
279 Fermentables f; |
280 f.f_inventory = 0; |
280 f.inventory = 0; |
281 f.f_avail = false; |
281 f.avail = false; |
282 f.f_name = obj["f_name"].toString(); |
282 f.name = obj["f_name"].toString(); |
283 f.f_origin = obj["f_origin"].toString(); |
283 f.origin = obj["f_origin"].toString(); |
284 f.f_supplier = obj["f_supplier"].toString(); |
284 f.supplier = obj["f_supplier"].toString(); |
285 f.f_amount = obj["f_amount"].toDouble(); |
285 f.amount = obj["f_amount"].toDouble(); |
286 f.f_cost = obj["f_cost"].toDouble(); |
286 f.cost = obj["f_cost"].toDouble(); |
287 f.f_type = obj["f_type"].toInt(); |
287 f.type = obj["f_type"].toInt(); |
288 f.f_yield = obj["f_yield"].toDouble(); |
288 f.yield = obj["f_yield"].toDouble(); |
289 f.f_color = obj["f_color"].toDouble(); |
289 f.color = obj["f_color"].toDouble(); |
290 f.f_coarse_fine_diff = obj["f_coarse_fine_diff"].toDouble(); |
290 f.coarse_fine_diff = obj["f_coarse_fine_diff"].toDouble(); |
291 f.f_moisture = obj["f_moisture"].toDouble(); |
291 f.moisture = obj["f_moisture"].toDouble(); |
292 f.f_diastatic_power = obj["f_diastatic_power"].toDouble(); |
292 f.diastatic_power = obj["f_diastatic_power"].toDouble(); |
293 f.f_protein = obj["f_protein"].toDouble(); |
293 f.protein = obj["f_protein"].toDouble(); |
294 f.f_dissolved_protein = obj["f_dissolved_protein"].toDouble(); |
294 f.dissolved_protein = obj["f_dissolved_protein"].toDouble(); |
295 f.f_max_in_batch = obj["f_max_in_batch"].toDouble(); |
295 f.max_in_batch = obj["f_max_in_batch"].toDouble(); |
296 f.f_graintype = obj["f_graintype"].toInt(); |
296 f.graintype = obj["f_graintype"].toInt(); |
297 f.f_added = obj["f_added"].toInt(); |
297 f.added = obj["f_added"].toInt(); |
298 f.f_recommend_mash = obj["f_recommend_mash"].toInt() ? true:false; |
298 f.recommend_mash = obj["f_recommend_mash"].toInt() ? true:false; |
299 f.f_add_after_boil = obj["f_add_after_boil"].toInt() ? true:false; |
299 f.add_after_boil = obj["f_add_after_boil"].toInt() ? true:false; |
300 f.f_adjust_to_total_100 = obj["f_adjust_to_total_100"].toInt() ? true:false; |
300 f.adjust_to_total_100 = obj["f_adjust_to_total_100"].toInt() ? true:false; |
301 f.f_percentage = obj["f_percentage"].toDouble(); |
301 f.percentage = obj["f_percentage"].toDouble(); |
302 f.f_di_ph = obj["f_di_ph"].toDouble(); |
302 f.di_ph = obj["f_di_ph"].toDouble(); |
303 f.f_acid_to_ph_57 = obj["f_acid_to_ph_57"].toDouble(); |
303 f.acid_to_ph_57 = obj["f_acid_to_ph_57"].toDouble(); |
304 if (f.f_adjust_to_total_100) |
304 if (f.adjust_to_total_100) |
305 prod->fermentables_use100 = true; |
305 prod->fermentables_use100 = true; |
306 percentcheck += f.f_percentage; |
306 percentcheck += f.percentage; |
307 /* Check and update inventory */ |
307 /* Check and update inventory */ |
308 yquery.prepare("SELECT inventory FROM inventory_fermentables WHERE name=:name AND supplier=:supplier AND origin=:origin"); |
308 yquery.prepare("SELECT inventory FROM inventory_fermentables WHERE name=:name AND supplier=:supplier AND origin=:origin"); |
309 yquery.bindValue(":name", f.f_name); |
309 yquery.bindValue(":name", f.name); |
310 yquery.bindValue(":supplier", f.f_supplier); |
310 yquery.bindValue(":supplier", f.supplier); |
311 yquery.bindValue(":origin", f.f_origin); |
311 yquery.bindValue(":origin", f.origin); |
312 yquery.exec(); |
312 yquery.exec(); |
313 if (yquery.first()) { |
313 if (yquery.first()) { |
314 f.f_avail = true; |
314 f.avail = true; |
315 f.f_inventory = yquery.value(0).toDouble(); |
315 f.inventory = yquery.value(0).toDouble(); |
316 } |
316 } |
317 prod->fermentables.append(f); |
317 prod->fermentables.append(f); |
318 } |
318 } |
319 qDebug() << "fermentables" << prod->fermentables.size() << percentcheck; |
319 qDebug() << "fermentables" << prod->fermentables.size() << percentcheck; |
320 if (percentcheck == 0 && prod->fermentables.size()) { |
320 if (percentcheck == 0 && prod->fermentables.size()) { |
321 /* Missing percentages, fix it. */ |
321 /* Missing percentages, fix it. */ |
322 double total = 0; |
322 double total = 0; |
323 for (int i = 0; i < prod->fermentables.size(); i++) { |
323 for (int i = 0; i < prod->fermentables.size(); i++) { |
324 if (prod->fermentables.at(i).f_added < 4) |
324 if (prod->fermentables.at(i).added < 4) |
325 total += prod->fermentables.at(i).f_amount; |
325 total += prod->fermentables.at(i).amount; |
326 } |
326 } |
327 for (int i = 0; i < prod->fermentables.size(); i++) { |
327 for (int i = 0; i < prod->fermentables.size(); i++) { |
328 if (prod->fermentables.at(i).f_added < 4) |
328 if (prod->fermentables.at(i).added < 4) |
329 prod->fermentables[i].f_percentage = round((prod->fermentables.at(i).f_amount / total) * 10000.0) / 100.0; |
329 prod->fermentables[i].percentage = round((prod->fermentables.at(i).amount / total) * 10000.0) / 100.0; |
330 } |
330 } |
331 qInfo() << " fixed missing percentages"; |
331 qInfo() << " fixed missing percentages"; |
332 // is_changed(); |
332 // is_changed(); |
333 } |
333 } |
334 } |
334 } |
837 query.bindValue(":json_fermentables", "[]"); |
837 query.bindValue(":json_fermentables", "[]"); |
838 } else { |
838 } else { |
839 QJsonArray array; |
839 QJsonArray array; |
840 for (int i = 0; i < prod->fermentables.size(); i++) { |
840 for (int i = 0; i < prod->fermentables.size(); i++) { |
841 QJsonObject obj; |
841 QJsonObject obj; |
842 obj.insert("f_name", prod->fermentables.at(i).f_name); |
842 obj.insert("f_name", prod->fermentables.at(i).name); |
843 obj.insert("f_origin", prod->fermentables.at(i).f_origin); |
843 obj.insert("f_origin", prod->fermentables.at(i).origin); |
844 obj.insert("f_supplier", prod->fermentables.at(i).f_supplier); |
844 obj.insert("f_supplier", prod->fermentables.at(i).supplier); |
845 obj.insert("f_amount", round(prod->fermentables.at(i).f_amount * 100000) / 100000); |
845 obj.insert("f_amount", round(prod->fermentables.at(i).amount * 100000) / 100000); |
846 obj.insert("f_cost", round(prod->fermentables.at(i).f_cost * 1000) / 1000); |
846 obj.insert("f_cost", round(prod->fermentables.at(i).cost * 1000) / 1000); |
847 obj.insert("f_type", prod->fermentables.at(i).f_type); |
847 obj.insert("f_type", prod->fermentables.at(i).type); |
848 obj.insert("f_yield", round(prod->fermentables.at(i).f_yield * 10) / 10); |
848 obj.insert("f_yield", round(prod->fermentables.at(i).yield * 10) / 10); |
849 obj.insert("f_color", round(prod->fermentables.at(i).f_color * 10) / 10); |
849 obj.insert("f_color", round(prod->fermentables.at(i).color * 10) / 10); |
850 obj.insert("f_coarse_fine_diff", round(prod->fermentables.at(i).f_coarse_fine_diff * 10) / 10); |
850 obj.insert("f_coarse_fine_diff", round(prod->fermentables.at(i).coarse_fine_diff * 10) / 10); |
851 obj.insert("f_moisture", round(prod->fermentables.at(i).f_moisture * 10) / 10); |
851 obj.insert("f_moisture", round(prod->fermentables.at(i).moisture * 10) / 10); |
852 obj.insert("f_diastatic_power", round(prod->fermentables.at(i).f_diastatic_power * 100000) / 100000); |
852 obj.insert("f_diastatic_power", round(prod->fermentables.at(i).diastatic_power * 100000) / 100000); |
853 obj.insert("f_protein", round(prod->fermentables.at(i).f_protein * 10) / 10); |
853 obj.insert("f_protein", round(prod->fermentables.at(i).protein * 10) / 10); |
854 obj.insert("f_dissolved_protein", round(prod->fermentables.at(i).f_dissolved_protein * 10) / 10); |
854 obj.insert("f_dissolved_protein", round(prod->fermentables.at(i).dissolved_protein * 10) / 10); |
855 obj.insert("f_max_in_batch", prod->fermentables.at(i).f_max_in_batch); |
855 obj.insert("f_max_in_batch", prod->fermentables.at(i).max_in_batch); |
856 obj.insert("f_graintype", prod->fermentables.at(i).f_graintype); |
856 obj.insert("f_graintype", prod->fermentables.at(i).graintype); |
857 obj.insert("f_added", prod->fermentables.at(i).f_added); |
857 obj.insert("f_added", prod->fermentables.at(i).added); |
858 obj.insert("f_recommend_mash", prod->fermentables.at(i).f_recommend_mash ? 1:0); |
858 obj.insert("f_recommend_mash", prod->fermentables.at(i).recommend_mash ? 1:0); |
859 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).add_after_boil ? 1:0); |
860 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).adjust_to_total_100 ? 1:0); |
861 obj.insert("f_percentage", round(prod->fermentables.at(i).f_percentage * 10) / 10); |
861 obj.insert("f_percentage", round(prod->fermentables.at(i).percentage * 10) / 10); |
862 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).di_ph * 100000) / 100000); |
863 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).acid_to_ph_57 * 100000) / 100000); |
864 // qDebug() << "fermentables_Json" << i << obj; |
864 // qDebug() << "fermentables_Json" << i << obj; |
865 array.append(obj); /* Append this object */ |
865 array.append(obj); /* Append this object */ |
866 } |
866 } |
867 QJsonDocument doc; |
867 QJsonDocument doc; |
868 doc.setArray(array); |
868 doc.setArray(array); |