0.0
0 ORDER BY name]]>
SELECT id, name FROM items WHERE category = 'Coffee: Roasted' AND id IN (SELECT item FROM current_items) ORDER BY name
0.0
30
0) {
pluginContext.table.newAnnotation(currentnote.text, 1, 2);
}
currentnote.text = "";
var t = QTime();
t = t.fromString(currenttime.time, "hh:mm:ss");
t = t.addSecs(30);
currenttime.time = t;
currenttemperature.text = "";
});
currenttemperature.returnPressed.connect(addmeasurement.clicked);
currentnote.returnPressed.connect(addmeasurement.clicked);
var v1 = findChildObject(this, 'ms');
v1.triggered.connect(pluginContext.table.LOD_ms);
var v2 = findChildObject(this, '1s');
v2.triggered.connect(pluginContext.table.LOD_1s);
var v3 = findChildObject(this, '5s');
v3.triggered.connect(pluginContext.table.LOD_5s);
var v4 = findChildObject(this, '10s');
v4.triggered.connect(pluginContext.table.LOD_10s);
var v5 = findChildObject(this, '15s');
v5.triggered.connect(pluginContext.table.LOD_15s);
var v6 = findChildObject(this, '30s');
v6.triggered.connect(pluginContext.table.LOD_30s);
var v7 = findChildObject(this, '1m');
v7.triggered.connect(pluginContext.table.LOD_1m);
var clear = findChildObject(this, 'clear');
clear.triggered.connect(pluginContext.table.clear);
clear.triggered.connect(pluginContext.graph.clear);
clear.triggered.connect(function() {
currenttime.time = QTime(0, 0, 0, 0);
currenttemperature.text = "";
currentnote.text = "";
pluginContext.table.clearOutputColumns();
pluginContext.table.addOutputTemperatureColumn(1);
pluginContext.table.addOutputAnnotationColumn(2);
});
var sampleGreenName = findChildObject(this, 'sampleGreenName');
var sampleGreenWeight = findChildObject(this, 'sampleGreenWeight');
var productionGreenTable = findChildObject(this, 'productionGreenTable');
var greenModel = productionGreenTable.model();
var greenTotal = 0.0;
var updateGreenTable = function() {
var deleteRow = -1;
while((deleteRow = productionGreenTable.findData("delete", 0)) > -1) {
if(productionGreenTable.data(deleteRow, 0, 0) == "Delete") {
productionGreenTable.removeRow(productionGreenTable.findData("delete", 0));
} else {
break;
}
}
greenTotal = productionGreenTable.columnSum(1, 0);
productionGreenTable.resizeColumnToContents(0);
};
greenModel.dataChanged.connect(updateGreenTable);
var validateInputs = function() {
if(batchType.currentIndex == 0) {
/* Sample batch */
if(sampleGreenName.text.length == 0) {
tabs.setCurrentIndex(0);
displayError(TTR("manualLogEntry", "Data Entry Error"),
TTR("manualLogEntry", "Please enter a green coffee name."));
return false;
}
if(Number(sampleGreenWeight.text) <= 0 || isNaN(sampleGreenWeight.text)) {
tabs.setCurrentIndex(0);
displayError(TTR("manualLogEntry", "Data Entry Error"),
TTR("manualLogEntry", "Green coffee weight must be a number greater than 0."));
return false;
}
} else {
/* Production batch */
var itemArray = productionGreenTable.columnArray(0, 32).split("\\s*,\\s*");
var weightArray = productionGreenTable.columnArray(1, 0).split("\\s*,\\s*");
if((itemArray.length != weightArray.length) || (itemArray.length == 0)) {
tabs.setCurrentIndex(0);
displayError(TTR("manualLogEntry", "Data Entry Error"),
TTR("manualLogEntry", "Please check that at least one green coffee has been selected and each green coffee has a valid weight"));
return false;
}
if(Number(greenTotal) <= 0) {
tabs.setCurrentIndex(0);
displayError(TTR("manualLogEntry", "DataEntryError"),
TTR("manualLogEntry", "Total green coffee weight must be a number greater than 0."));
return false;
}
if(roastedItem.currentIndex == 0) {
tabs.setCurrentIndex(0);
displayError(TTR("manualLogEntry", "DataEntryError"),
TTR("manualLogEntry", "Please select a roasted coffee item."));
return false;
}
}
return true;
};
var roastDataExists = function() {
return (pluginContext.table.rowCount() > 0);
}
var roastTime = findChildObject(this, 'roastTime');
var attributes = findChildObject(this, 'attributes');
var sampleGreenArrivalDate = findChildObject(this, 'sampleGreenArrivalDate');
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 roastedWeight = findChildObject(this, 'roastedWeight');
var notes = findChildObject(this, 'notes');
var roastDuration = findChildObject(this, 'roastDuration');
var doSubmit = function() {
var fileID = -1;
var query = new QSqlQuery();
if(roastDataExists()) {
var buffer = new QBuffer;
buffer.open(3);
pluginContext.table.saveXML(buffer);
buffer.open(3); /* saveXML closes the buffer */
var q = "INSERT INTO files (id, name, type, note, file) VALUES (default, :name, 'profile', NULL, :data) RETURNING id";
query.prepare(q);
query.bind(":name", roastTime.text + " Manual Entry");
query.bind(":data", buffer.readToString());
query.exec();
query.next();
fileID = query.value(0);
buffer.close();
}
var rootIndex = machineModel.index(machineSelector.currentIndex, 0);
var selectedRoasterName = machineModel.data(rootIndex, 0);
var machineReference = machineModel.referenceElement(machineModel.data(rootIndex, 32));
var selectedRoasterID = machineReference.databaseid;
query.exec("SELECT 1 FROM machine WHERE id = " + selectedRoasterID);
if(!query.next()) {
query.prepare("INSERT INTO machine (id, name) VALUES (:id, :name)");
query.bind(":id", selectedRoasterID);
query.bind(":name", selectedRoasterName);
query.exec();
}
if(batchType.currentIndex == 0) {
/* Sample roast */
var attnames = sqlToArray(attributes.columnArray(0, 0));
for(var i = 0; i < attnames.length; i++) {
var attname = attnames[i];
if(attname[0] == '{') {
attname = attname.substr(1);
}
if(attname[0] == ' ') {
attname = attname.substr(1);
}
if(attname[attname.length - 1] == '}') {
attname = attname.substr(0, attname.length - 1);
}
if(attname.length == 0) {
break;
}
query.prepare("SELECT id FROM item_attributes WHERE name = :name");
query.bind(":name", attname);
query.exec();
if(query.next()) {
attributes.setData(i, 0, query.value(0), 32);
} else {
query.prepare("INSERT INTO item_attributes(id, name) VALUES (DEFAULT, :name) RETURNING id");
query.bind(":name", attname);
query.exec();
query.next();
attributes.setData(i, 0, query.value(0), 32);
}
}
query.prepare("INSERT INTO coffee_sample_items(id, name, reference, unit, quantity, category, arrival, vendor, attribute_ids, attribute_values) VALUES (DEFAULT, :name, NULL, 'lb', 0, 'Coffee: Green Sample', :arrival, :vendor, :attrids, :attrvals) RETURNING id");
query.bind(":name", sampleGreenName.text);
query.bind(":arrival", sampleGreenArrivalDate.date);
query.bind(":attrids", attributes.bindableColumnArray(0, 32));
query.bind(":attrvals", attributes.bindableQuotedColumnArray(1, 0));
query.exec();
query.next();
var greenId = query.value(0);
query.prepare("INSERT INTO items (id, name, reference, unit, quantity, category) VALUES (DEFAULT, :name, NULL, 'lb', 0, 'Coffee: Roasted Sample') RETURNING id");
query.bind(":name", sampleGreenName.text + " Roasted Sample");
query.exec();
query.next();
var roastedId = query.value(0);
query.prepare("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, person) VALUES (:time, :unroastedids, :greens, :green, :roastedid, :roasted, 'SAMPLEROAST', :note, :machine, :duration, TRUE, NULL, NULL, NULL, NULL, :files, :user)");
query.bind(":time", roastTime.text);
query.bind(":unroastedids", "{" + greenId + "}");
query.bind(":greens", "{" + convertToPounds(parseFloat(sampleGreenWeight.text), sampleGreenUnit.currentText) + "}");
query.bind(":green", convertToPounds(parseFloat(sampleGreenWeight.text), sampleGreenUnit.currentText));
query.bind("roastedid", Number(roastedId));
query.bind("roasted", convertToPounds(parseFloat(roastedWeight.text), sampleGreenUnit.currentText));
query.bind(":note", notes.plainText);
query.bind(":machine", Number(selectedRoasterID));
query.bind(":duration", roastDuration.text);
if(fileID > 0) {
query.bind(":files", "{" + fileID + "}");
} else {
query.bind(":file", "{}");
}
query.bind(":user", Application.currentTypicaUser());
query.exec();
} else {
var q = "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, person) VALUES (:time, ";
q += productionGreenTable.columnArray(0, 32);
q += ", ";
var greenSum = 0.0;
for(var i = 0; i < productionGreenTable.data(i, 1, 0).value != ""; i++) {
var greenWt = convertToPounds(parseFloat(productionGreenTable.data(i, 1, 0)), productionGreenUnit.currentText);
productionGreenTable.setData(i, 1, greenWt, 32);
greenSum += greenWt;
}
q += productionGreenTable.columnArray(1, 32);
q += ", ";
q += greenSum;
q += ", ";
q += roastedItem.currentData();
q += ", ";
q += convertToPounds(parseFloat(roastedWeight.text), productionGreenUnit.currentText);
q += ", 'ROAST', :annotation, ";
q += selectedRoasterID;
q += ", :duration, TRUE, NULL, NULL, NULL, NULL, '{";
if(fileID > 0) {
q += fileID;
}
q += "}', :user)";
query.prepare(q);
query.bind(":time", roastTime.text);
query.bind(":annotation", notes.plainText);
query.bind(":duration", roastDuration.text);
query.bind(":user", Application.currentTypicaUser());
query.exec();
}
query = query.invalidate();
window.close();
}
var submit = findChildObject(this, 'submit');
submit.clicked.connect(function() {
if(validateInputs()) {
doSubmit();
}
});
]]>