Production:->Cost of Green Coffee for Roasted Coffee Print '); output.writeStartElement("html"); output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml"); output.writeStartElement("head"); output.writeTextElement("title", "Cost of Green Coffee for Roasted Coffee"); output.writeEndElement(); output.writeStartElement("body"); output.writeTextElement("h1", "Cost of Green Coffee for Roasted Coffee"); var query = new QSqlQuery(); query.exec("SELECT item, (SELECT name FROM items WHERE id = item) AS name FROM current_items ORDER BY name"); var roastedItems = new Array(); var roastedNames = new Array(); while(query.next()) { roastedItems.push(query.value(0)); roastedNames.push(query.value(1)); } var recipes = new Array(); var recipeQuantities = new Array(); query.prepare("SELECT unroasted_id, unroasted_quantity FROM roasting_log WHERE roasted_id = :item AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = :item2)"); for(var i = 0; i < roastedItems.length; i++) { query.bind("item", roastedItems[i]); query.bind("item2", roastedItems[i]); query.exec(); if(query.next()) { recipes.push(query.value(0)); recipeQuantities.push(query.value(1)); } else { print("Error 1"); } } query.prepare("SELECT 1 / (-1 * (min((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity) - 1)), 1 / (-1 * (max((unroasted_total_quantity - roasted_quantity)/unroasted_total_quantity) - 1)), 1 / (-1 * (avg((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity) - 1)) FROM roasting_log WHERE roasted_id = :ri AND unroasted_id = :gi AND unroasted_total_quantity > 0 AND roasted_quantity > 0 AND approval = true"); var mins = new Array(); var maxes = new Array(); var means = new Array(); for(var i = 0; i < roastedItems.length; i++) { query.bind(":ri", Number(roastedItems[i])); query.bind(":gi", recipes[i]); query.exec(); if(query.next()) { mins.push(query.value(0)); maxes.push(query.value(1)); means.push(query.value(2)); } else { print("Error 2"); } } var proportionalCosts = new Array(); query.prepare("SELECT cost * :proportion FROM purchase WHERE item = :id"); for(var i = 0; i < roastedItems.length; i++) { greens = sqlToArray(recipes[i]); weights = sqlToArray(recipeQuantities[i]); proportions = new Array(); quantitySum = weights.reduce(function(p, c) { return p + c; }); for(var j = 0; j < weights.length; j++) { proportions.push(weights[j]/quantitySum); } partialSum = 0; for(var j = 0; j < greens.length; j++) { query.bind(":proportion", proportions[j]); query.bind(":id", greens[j]); query.exec(); if(query.next()) { partialSum += query.value(0); } else { print("Error 3"); } } proportionalCosts.push(partialSum); } var minCosts = new Array(); var maxCosts = new Array(); var meanCosts = new Array(); for(var i = 0; i < roastedItems.length; i++) { minCosts.push(proportionalCosts[i] * mins[i]); maxCosts.push(proportionalCosts[i] * maxes[i]); meanCosts.push(proportionalCosts[i] * means[i]); } output.writeStartElement("table"); output.writeAttribute("rules", "groups"); output.writeAttribute("cellpadding", "3px"); output.writeStartElement("thead"); output.writeStartElement("tr"); output.writeTextElement("th", "Coffee"); output.writeTextElement("th", "Minimum Cost"); output.writeTextElement("th", "Maximum Cost"); output.writeTextElement("th", "Mean Cost"); output.writeEndElement(); output.writeEndElement(); output.writeStartElement("tbody"); for(var i = 0; i < roastedItems.length; i++) { output.writeStartElement("tr"); output.writeTextElement("td", roastedNames[i]); output.writeTextElement("td", Number(minCosts[i]).toFixed(2)); output.writeTextElement("td", Number(maxCosts[i]).toFixed(2)); output.writeTextElement("td", Number(meanCosts[i]).toFixed(2)); output.writeEndElement(); } output.writeEndElement(); output.writeEndElement(); output.writeEndElement(); output.writeEndElement(); output.writeEndDocument(); report.setContent(buffer); buffer.close(); }; refresh(); ]]>