SELECT id, name FROM items WHERE category = 'Coffee: Roasted' AND id IN (SELECT item FROM current_items) ORDER BY name
SELECT id, name FROM coffees WHERE quantity <> 0 ORDER BY name
0.0
0.0
0) {
for(var i = 0; i < navigationwindow.loggingWindow.scales.length; i++) {
var scale = navigationwindow.loggingWindow.scales[i];
var label = new DragLabel();
var weighButton = new QPushButton();
weighButton.text = "Weigh";
weighButton.clicked.connect(scale.weigh);
label.updateMeasurement = function(m, u) {
switch(unitBox.currentIndex) {
case 0:
this.text = Units.convertWeight(m, u, Units.Gram).toFixed(1);
break;
case 1:
this.text = Units.convertWeight(m, u, Units.Kilogram).toFixed(4);
break;
case 2:
this.text = Units.convertWeight(m, u, Units.Ounce).toFixed(3);
break;
case 3:
this.text = Units.convertWeight(m, u, Units.Pound).toFixed(4);
break;
}
};
scalesLayout.addWidget(label);
scalesLayout.addWidget(weighButton);
scale.newMeasurement.connect(function(m, u) {
label.updateMeasurement(m, u);
});
scale.weigh();
unitBox['currentIndexChanged(int)'].connect(scale.weigh);
}
}
var remainingStock = new Array();
var query = new QSqlQuery();
query.exec("SELECT id, quantity, (SELECT conversion FROM lb_bag_conversion WHERE item = id) FROM coffees WHERE quantity <> 0");
while(query.next()) {
remainingStock.push({id: query.value(0),
quantity: query.value(1),
conversion: query.value(2)});
}
query = query.invalidate();
var convertToPounds = function(w, u) {
switch(u) {
case "g":
return w * 0.0022;
case "oz":
return w * 0.0625;
case "Kg":
return w * 2.2;
}
return w;
};
var convertFromPounds = function(w, u) {
switch(u) {
case "g":
return w / 0.0022;
case "oz":
return w / 0.0625;
case "Kg":
return w / 2.2;
}
return w;
};
var specnotes = findChildObject(this, 'specnotes');
specnotes.readOnly = true;
var lossspec = findChildObject(this, 'lossspec');
var minfield = findChildObject(this, 'minroastweight');
var midfield = findChildObject(this, 'expectedroastweight');
var maxfield = findChildObject(this, 'maxroastweight');
var minloss = 0;
var maxloss = 0;
var expectloss = 0;
var updateGreenTable = function() {
var deleteRow = -1;
/* The combo box delegate updates user data before display data
and this code is executed before the model update is fully
complete. Rather than rely on this behavior continuing, we
check that the display value has also been updated and defer
row removal until both updates are complete.
*/
while((deleteRow = table.findData("delete", 0)) > -1) {
if(table.data(deleteRow, 0, 0) == "Delete") {
table.removeRow(table.findData("delete", 0));
} else {
break;
}
}
green.text = table.columnSum(1, 0);
table.resizeColumnToContents(0);
var gid = 0;
var r = 0;
while(gid >= 0)
{
gid = Number(table.data(r, 0, 32));
if(isNaN(gid))
{
gid = -1;
break;
}
var bagConversion = 1;
for(var i = 0; i < remainingStock.length; i++)
{
if(gid == Number(remainingStock[i].id))
{
var displayValue = Number(remainingStock[i].quantity);
bagConversion = Number(remainingStock[i].conversion);
if(!isNaN(Number(table.data(r, 1, 0))))
{
var change = Number(table.data(r, 1, 0));
switch(unitBox.currentIndex)
{
case 0:
change = convertToPounds(change, "g");
break;
case 1:
change = convertToPounds(change, "Kg");
break;
case 2:
change = convertToPounds(change, "oz");
break;
}
displayValue -= change;
}
var bagCount = (displayValue / bagConversion).toFixed(2);
switch(unitBox.currentIndex)
{
case 0:
displayValue = convertFromPounds(displayValue, "g");
break;
case 1:
displayValue = convertFromPounds(displayValue, "Kg");
break;
case 2:
displayValue = convertFromPounds(displayValue, "oz");
break;
}
displayValue = "" + Number(displayValue).toFixed(3) + " (" + Number(bagCount).toFixed(3) + " bags)";
if(table.data(r, 2, 0) != displayValue)
{
table.setData(r, 2, displayValue, 0);
table.setData(r, 2, displayValue, 2);
table.resizeColumnToContents(2);
}
}
}
r++;
}
if(parseFloat(green.text) > 0)
{
var expectedLossDesc = "";
if(lossspec.text.length > 0) {
if(minloss != expectloss) {
minfield.text = (-(parseFloat(green.text)) * (maxloss - 1)).toFixed(2);
maxfield.text = (-(parseFloat(green.text)) * (minloss - 1)).toFixed(2);
}
midfield.text = (-(parseFloat(green.text)) * (expectloss - 1)).toFixed(2);
}
if(parseFloat(roastwt.text) > 0)
{
lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
}
else
{
lossField.text = "100%";
}
}
};
model.dataChanged.connect(updateGreenTable);
unitBox['currentIndexChanged(int)'].connect(updateGreenTable);
roastwt.textChanged.connect(function() {
if(parseFloat(green.text) > 0)
{
if(parseFloat(roastwt.text) > 0)
{
lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
}
else
{
lossField.text = "100%";
}
}
});
var profilebutton = findChildObject(this, 'load');
profilebutton.setEnabled(false);
roasted['currentIndexChanged(int)'].connect(function() {
print("Roasted coffee item changed");
table.clear();
var query = new QSqlQuery();
var q = "SELECT EXISTS(SELECT 1 FROM item_files WHERE item = ";
q = q + roasted.currentData();
q = q + ")";
query.exec(q);
if(query.next())
{
if(query.value(0) == 'false')
{
profilebutton.setEnabled(false);
}
else
{
profilebutton.setEnabled(true);
}
}
else
{
profilebutton.setEnabled(false);
}
var title = "Typica - [*]New Batch (";
title = title + roasted.currentText;
title = title + ")";
print("Updating window title");
batch.windowTitle = title;
print("Checking green coffees");
q = "SELECT unroasted_id FROM roasting_log WHERE roasted_id = ";
q = q + roasted.currentData();
q = q + " AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = ";
q = q + roasted.currentData();
q = q + ")";
query.exec(q);
print("Updating green table");
if(query.next())
{
var unroasted_items = sqlToArray(query.value(0));
var names = [];
q = "SELECT name FROM items WHERE id = :id AND quantity <> 0";
query.prepare(q);
var allInStock = true;
for(var i = 0; i < unroasted_items.length; i++)
{
query.bind("id", unroasted_items[i]);
query.exec();
if(query.next())
{
names[i] = query.value(0);
}
else
{
allInStock = false;
}
}
if(allInStock)
{
for(var i = 0; i < unroasted_items.length; i++)
{
table.setData(i, 0, names[i], 0);
table.setData(i, 0, unroasted_items[i], 32);
}
}
}
print("About to query product spec");
query.prepare("SELECT loss, tolerance, notes FROM roasting_specification WHERE item = :id1 AND time = (SELECT max(time) FROM roasting_specification WHERE item = :id2)");
query.bind(":id1", roasted.currentData());
query.bind(":id2", roasted.currentData());
query.exec();
var lossSpecDescription = "";
print("About to read loss spec");
if(query.next()) {
if(query.value(0).length > 0) {
lossSpecDescription += (Number(query.value(0)) * 100).toFixed(2);
minloss = Number(query.value(0));
maxloss = Number(query.value(0));
expectloss = Number(query.value(0));
}
if(query.value(1).length > 0) {
lossSpecDescription += " +/- " + (Number(query.value(1)) * 100).toFixed(2);
minloss -= Number(query.value(1));
maxloss += Number(query.value(1));
}
if(lossSpecDescription.length > 0) {
lossSpecDescription += "%";
}
lossspec.text = lossSpecDescription;
specnotes.plainText = query.value(2);
} else {
lossspec.text = "";
specnotes.plainText = "";
}
print("Updated roast spec");
//roastestimate.text = "";
print("Cleared roast estimate");
query = query.invalidate();
print("About to redraw tag");
drawTag();
print("Tag redrawn");
});
var validateCapacity = function() {
if(checkCapacity == "true") {
if(convertToPounds(parseFloat(green.text), unitBox.currentText) > poundsCapacity) {
return false;
}
}
return true;
}
var duration = findChildObject(this, 'duration');
var timefield = findChildObject(this, 'time');
profilebutton.clicked.connect(function() {
var proceed = false;
if(validateCapacity()) {
proceed = true;
} else {
proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
}
if((proceed == true) && (timefield.text.length != 0)) {
proceed = displayWarning(TTR("batchWindow", "Batch Already Roasted"),
TTR("batchWindow", "Roasting data already exists for this batch. Roasting another batch from this window will overwrite existing data. Are you sure you want to do this?"));
}
if(proceed) {
doLoadProfile();
}
});
var doLoadProfile = function() {
batch.windowModified = true;
currentBatchInfo = batch;
query = new QSqlQuery();
var q = "SELECT files FROM item_files WHERE item = :item AND time = (SELECT max(time) FROM item_files WHERE item = :again)";
query.prepare(q);
query.bind(":item", Number(roasted.currentData()));
query.bind(":again", Number(roasted.currentData()));
query.exec();
var graph;
var log;
if(query.next())
{
var files = query.value(0);
files = files.replace("{", "(");
files = files.replace("}", ")");
q = "SELECT file, name FROM files WHERE id IN ";
q = q + files;
q = q + " AND type = 'profile'";
query.exec(q);
if(query.next())
{
var targetseries = -1;
var buffer = new QBuffer(query.value(0));
var pname = query.value(1);
var input = new XMLInput(buffer, 1);
graph = findChildObject(navigationwindow.loggingWindow, 'graph');
log = findChildObject(navigationwindow.loggingWindow, 'log');
log.clear();
graph.clear();
input.newTemperatureColumn.connect(log.setHeaderData);
input.newTemperatureColumn.connect(function(col, text) {
if(text == navigationwindow.loggingWindow.targetcolumnname)
{
targetseries = col;
}
});
input.newAnnotationColumn.connect(log.setHeaderData);
input.measure.connect(graph.newMeasurement);
input.measure.connect(log.newMeasurement);
input.measure.connect(function(data, series) {
if(series == targetseries)
{
targetDetector.newMeasurement(data);
}
});
var lc;
input.lastColumn.connect(function(c) {
lc = c;
QSettings.setValue("liveColumn", c + 1);
navigationwindow.loggingWindow.postLoadColumnSetup(c)
});
input.annotation.connect(function(note, tcol, ncol) {
for(var i = tcol; i < ncol; i++) {
log.newAnnotation(note, i, ncol);
}
});
}
}
query = query.invalidate();
navigationwindow.loggingWindow.windowTitle = "Typica - [*]" + pname;
navigationwindow.loggingWindow.raise();
navigationwindow.loggingWindow.activateWindow();
graph.updatesEnabled = false;
log.updatesEnabled = false;
input.input();
log.updatesEnabled = true;
graph.updatesEnabled = true;
log.newAnnotation(TTR("batchWindow", "End"), 1, lc);
}
var noprofilebutton = findChildObject(this, 'noprofile');
noprofilebutton.clicked.connect(function() {
var proceed = false;
if(validateCapacity()) {
proceed = true;
} else {
proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
}
if((proceed == true) && (timefield.text.length != 0)) {
proceed = displayWarning(TTR("batchWindow", "Batch Already Roasted"),
TTR("batchWindow", "Roasting data already exists for this batch. Roasting another batch from this window will overwrite existing data. Are you sure you want to do this?"));
}
if(proceed) {
doNoProfile();
}
});
var doNoProfile = function() {
batch.windowModified = true;
currentBatchInfo = batch;
navigationwindow.loggingWindow.raise();
navigationwindow.loggingWindow.activateWindow();
}
var submitbutton = findChildObject(this, 'submit');
var notes = findChildObject(this, 'annotation');
var approval = findChildObject(this, 'approval');
approval.checked = true;
var target = findChildObject(this, 'target');
var greenCheck = function() {
var itemArray = table.columnArray(0, 32).split("\\s*,\\s*");
var weightArray = table.columnArray(1, 0).split("\\s*,\\s*");
return (itemArray.length == weightArray.length) && (itemArray.length > 0);
}
var checkSubmitEnable = function () {
if(roasted.currentIndex > 0) {
if(timefield.text.length > 0) {
if(duration.text.length > 0) {
if(batch.tempData.length > 0) {
if(green.text.length > 0) {
if(greenCheck()) {
return true;
}
}
}
}
}
}
return false;
}
submitbutton.clicked.connect(function() {
var proceed = false;
if(validateCapacity()) {
proceed = true;
} else {
proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
}
if(proceed) {
if(checkSubmitEnable()) {
doSubmit();
} else {
displayError(TTR("batchWindow", "Incomplete Input"),
TTR("batchWindow", "Some required information is not available. Please check inputs and try again."));
}
}
});
var filenofield = findChildObject(this, 'filenofield');
this.endBatch = function() {
var q = "INSERT INTO files (id, name, type, note, file) VALUES(default, :name, 'profile', NULL, :data) RETURNING id";
var query = new QSqlQuery();
query.prepare(q);
query.bind(":name", timefield.text + " " + roasted.currentText);
query.bindFileData(":data", batch.tempData);
query.exec();
query.next();
filenofield.text = query.value(0);
var file = new QFile(batch.tempData);
file.remove();
drawTag();
}
var doSubmit = function() {
checkQuery = new QSqlQuery();
checkQuery.exec("SELECT 1 FROM machine WHERE id = " + selectedRoasterID);
if(!checkQuery.next())
{
checkQuery.prepare("INSERT INTO machine (id, name) VALUES(:id, :name)");
checkQuery.bind(":id", selectedRoasterID);
checkQuery.bind(":name", selectedRoasterName);
checkQuery.exec();
}
checkQuery = checkQuery.invalidate();
query = new QSqlQuery();
var fileno = Number(filenofield.text);
var q2 = "INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files) VALUES(:time, ";
q2 = q2 + table.columnArray(0, 32);
q2 = q2 + ", ";
for(var i = 0; table.data(i, 1, 0).value != ""; i++)
{
table.setData(i, 1, convertToPounds(parseFloat(table.data(i, 1, 0)), unitBox.currentText) ,32)
}
q2 = q2 + table.columnArray(1, 32);
q2 = q2 + ", ";
q2 = q2 + convertToPounds(parseFloat(green.text), unitBox.currentText);
q2 = q2 + ", ";
q2 = q2 + roasted.currentData();
q2 = q2 + ", ";
q2 = q2 + convertToPounds(parseFloat(roastwt.text), unitBox.currentText);
q2 = q2 + ", 'ROAST', :annotation, ";
q2 = q2 + selectedRoasterID;
q2 = q2 + ", :duration, :approval, NULL, NULL, NULL, NULL, '{";
q2 = q2 + fileno;
q2 = q2 + "}') RETURNING time";
query2 = new QSqlQuery();
query2.prepare(q2);
query2.bind(":time", timefield.text);
query2.bind(":annotation", notes.plainText);
query2.bind(":duration", duration.text);
query2.bind(":approval", approval.checked);
query2.exec();
if(!query2.next()) {
displayError(TTR("batchWindow", "Database Insert Failed"), TTR("batchWindow", "Failed to save batch to database. Please check inputs and connection and try again."));
query2 = query2.invalidate();
query = query.invalidate();
return;
}
query2 = query2.invalidate();
if(target.checked) {
var q3 = "INSERT INTO item_files (time, item, files) VALUES(:time, :item, '{";
q3 = q3 + fileno;
q3 = q3 + "}')";
query.prepare(q3);
query.bind(":time", timefield.text);
query.bind(":item", roasted.currentData());
query.exec();
}
query = query.invalidate();
batch.windowModified = false;
batch.close();
}
function drawTag() {
var buffer = new QBuffer;
buffer.open(3);
var output = new XmlWriter(buffer);
output.writeStartDocument("1.0");
output.writeDTD('');
output.writeStartElement("html");
output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
output.writeStartElement("head");
var styleFile = new QFile(QSettings.value("config") + "/Scripts/batchtag.css");
styleFile.open(1);
output.writeTextElement("style", styleFile.readToString());
styleFile.close();
output.writeStartElement("script");
scriptFile = new QFile(QSettings.value("config") + "/Scripts/qrcode.js");
scriptFile.open(1);
output.writeCDATA(scriptFile.readToString());
scriptFile.close();
output.writeEndElement();
output.writeEndElement();
output.writeStartElement("body");
output.writeStartElement("h1");
output.writeCharacters(roasted.currentText);
output.writeEndElement();
output.writeTextElement("span", "Roasted at: " + timefield.text);
output.writeTextElement("span", "On machine: " + machine.text);
output.writeTextElement("span", "Batch file: " + filenofield.text);
output.writeStartElement("div");
output.writeAttribute("id", "container");
output.writeEndElement();
output.writeStartElement("script");
var tag = {g: "Typica", m: Number(selectedRoasterID), v: 1};
if(timefield.text.length > 0) {
tag.t = timefield.text;
}
if(filenofield.text.length > 0) {
tag.f = Number(filenofield.text);
}
var scriptData = 'var width = document.getElementById("container").offsetWidth;';
scriptData += 'var qrcode = new QRCode({content: \'';
scriptData += JSON.stringify(tag);
scriptData += '\', width: width, height: width});';
scriptData += 'var svg = qrcode.svg();';
scriptData += 'document.getElementById("container").innerHTML = svg;';
output.writeCDATA(scriptData);
output.writeEndElement();
output.writeEndElement();
output.writeEndElement();
output.writeEndDocument();
batchTag.setContent(buffer);
buffer.close();
};
drawTag();
var printMenu = findChildObject(this, 'print');
printMenu.triggered.connect(function() {
batchTag.print();
});
var printers = findChildObject(this, 'printerlist');
printers.currentIndex = printers.findText(QSettings.value("script/batchtagprinter"));
printers['currentIndexChanged(int)'].connect(function() {
QSettings.setValue("script/batchtagprinter", printers.currentText);
});
var printbutton = findChildObject(this, 'printbutton');
printbutton.clicked.connect(function() {
batchTag.print(printers.currentText);
});
]]>