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
>'{roasted}')::numeric), (data#>>'{green_weight}')::numeric FROM scheduled_roasts WHERE machine IS NULL");
batches.setHeaderData(0, TTR("schedule", "ID"));
batches.setHeaderData(1, TTR("schedule", "Roasted Coffee"));
batches.setHeaderData(2, TTR("schedule", "Green Weight"));
batches.alternatingRowColors = true;
batches.selectionMode = 1;
batches.selectionBehavior = 1;
batches.editTriggers = 0;
var model = table.model();
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;
};
batches.selectEntry.connect(function(id) {
saved = true;
pages.setCurrentIndex(0);
editingstack.setCurrentIndex(1);
batch.text = id;
var query = new QSqlQuery();
query.prepare("SELECT data FROM scheduled_roasts WHERE id = :id");
query.bind(":id", id);
query.exec();
if(query.next()) {
var d = JSON.parse(query.value(0));
if(d.roasted) {
roasted.currentIndex = roasted.findData(d.roasted);
}
if(d.greens) {
query.prepare("SELECT name FROM items WHERE id = :id");
for(var i = 0; i < d.greens.length; i++) {
query.bind(":id", d.greens[i].id);
query.exec();
if(query.next()) {
table.setData(i, 0, query.value(0), 0);
table.setData(i, 0, d.greens[i].id, 32);
switch(unitBox.currentIndex) {
case 0:
table.setData(i, 1, convertFromPounds(d.greens[i].weight, "g"), 0);
break;
case 1:
table.setData(i, 1, convertFromPounds(d.greens[i].weight, "Kg"), 0);
break;
case 2:
table.setData(i, 1, convertFromPounds(d.greens[i].weight, "oz"), 0);
break;
default:
table.setData(i, 1, d.greens[i].weight, 0);
break;
}
}
}
}
if(d.filters) {
for(var i = 0; i < d.filters.length; i++) {
filters.setData(i, 0, d.filters[i].key, 0);
filters.setData(i, 1, d.filters[i].value, 0);
}
}
}
query = query.invalidate();
drawTag();
deletebutton.enabled = true;
});
var tabs = findChildObject(this, 'tabs');
tabs.addTab(TTR("schedule", "Coffee"));
tabs.addTab(TTR("schedule", "Filters"));
tabs.addTab(TTR("schedule", "Batch Tag"));
tabs.currentChanged.connect(function(index) {
pages.setCurrentIndex(index);
});
var unitBox = findChildObject(this, 'unit');
unitBox.addItem("g");
unitBox.addItem("Kg");
unitBox.addItem("oz");
unitBox.addItem("lb");
unitBox.currentIndex = QSettings.value("script/batch_unit", unitBox.findText("lb"));
unitBox['currentIndexChanged(int)'].connect(function() {
QSettings.setValue("script/batch_unit", unitBox.currentIndex);
});
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 updateGreenTable = function() {
var deleteRow = -1;
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++;
}
drawTag();
};
model.dataChanged.connect(updateGreenTable);
unitBox['currentIndexChanged(int)'].connect(updateGreenTable);
roasted['currentIndexChanged(int)'].connect(function() {
table.clear();
var query = new QSqlQuery();
var q = "SELECT unroasted_id FROM roasting_log WHERE roasted_id = ";
q += roasted.currentData();
q += " AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = ";
q += roasted.currentData();
q += ")";
query.exec(q);
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);
}
}
}
query = query.invalidate();
drawTag();
});
var batchTag = findChildObject(this, 'batchTag');
var filtersModel = filters.model();
filtersModel.dataChanged.connect(function() {
drawTag()
});
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();
styleFile = new QFile(QSettings.value("config") + "/Scripts/barcode.css");
styleFile.open(1);
output.writeTextElement("style", styleFile.readToString());
styleFile.close();
output.writeStartElement("script");
scriptFile = new QFile(QSettings.value("config") + "/Scripts/barcode.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.writeStartElement("div");
output.writeAttribute("id", "barcode");
output.writeAttribute("class", "barcode128h");
output.writeAttribute("align", "center");
output.writeEndElement();
output.writeStartElement("script");
output.writeCharacters("var strBarcodeHTML = code128('00" + batch.text + "', 'C'); document.getElementById('barcode').innerHTML = strBarcodeHTML;");
output.writeEndElement();
output.writeStartElement("p");
output.writeAttribute("class", "code-text");
output.writeCharacters("00" + batch.text);
output.writeEndElement();
var cdt = new Date(Date.now());
output.writeTextElement("p", cdt.toLocaleDateString(TTR("reports", "en-US")));
for(var i = 0; table.data(i, 1, 0).value != ""; i++) {
output.writeStartElement("p");
output.writeCharacters(table.data(i, 1, 0) + unitBox.currentText);
output.writeCharacters(" ");
output.writeCharacters(table.data(i, 0, 0));
output.writeEndElement();
}
output.writeTextElement("p", "Total Green Weight: " + green.text + unitBox.currentText);
for(var i = 0; filters.data(i, 0, 0).value != ""; i++) {
output.writeStartElement("p");
output.writeCharacters(filters.data(i, 0, 0) + "=" + filters.data(i, 1, 0));
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);
});
var savebutton = findChildObject(this, 'savebutton');
savebutton.clicked.connect(function() {
var data = new Object;
data.roasted = Number(roasted.currentData());
data.green_weight = Number(green.text);
var greensdata = new Array;
for(var i = 0; table.data(i, 1, 0).value != ""; i++) {
var greendata = new Object;
greendata.id = Number(table.data(i, 0, 32));
switch(unitBox.currentIndex) {
case 0:
greendata.weight = convertToPounds(table.data(i, 1, 0), "g");
break;
case 1:
greendata.weight = convertToPounds(table.data(i, 1, 0), "Kg");
break;
case 2:
greendata.weight = convertToPounds(table.data(i, 1, 0), "oz");
break;
default:
greendata.weight = Number(table.data(i, 1, 0));
break;
}
greensdata.push(greendata);
}
data.greens = greensdata;
var filtersdata = new Array;
for(var i = 0; filters.data(i, 0, 0).value != ""; i++) {
var filterdata = new Object;
filterdata.key = filters.data(i, 0, 0).value;
filterdata.value = filters.data(i, 1, 0).value;
filtersdata.push(filterdata);
}
data.filters = filtersdata;
var query = QSqlQuery();
if(saved) {
query.prepare("UPDATE scheduled_roasts SET data = :data WHERE id = :id");
} else {
query.prepare("INSERT INTO scheduled_roasts(id, data) VALUES (:id, :data)");
}
query.bind(":id", batch.text);
query.bind(":data", JSON.stringify(data));
query.exec();
editingstack.setCurrentIndex(0);
saved = false;
deletebutton.enabled = false;
});
deletebutton.clicked.connect(function() {
if(saved) {
var query = QSqlQuery();
query.prepare("DELETE FROM scheduled_roasts WHERE id = :id");
query.bind(":id", Number(batch.text));
query.exec();
query = query.invalidate();
}
editingstack.setCurrentIndex(0);
saved = false;
deletebutton.enabled = false;
});
var notifier = Application.subscribe("scheduledroastschange");
notifier.notify.connect(function() {
batches.setQuery("SELECT id, (SELECT name FROM items WHERE id = (data#>>'{roasted}')::numeric), (data#>>'{green_weight}')::numeric FROM scheduled_roasts WHERE machine IS NULL");
});
]]>