123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <window id="schedule">
- <menu name="File">
- <item id="print" shortcut="Ctrl+P">Print...</item>
- </menu>
- <menu name="Schedule">
- <item id="deleteall">Delete All</item>
- </menu>
- <layout type="horizontal">
- <splitter type="horizontal" id="splitter">
- <widget>
- <layout type="vertical">
- <layout type="horizontal">
- <button name="Delete" type="push" id="deletebutton" />
- <stretch />
- <button name="New" type="push" id="newbutton" />
- </layout>
- <sqlview id="batches" />
- </layout>
- </widget>
- <widget>
- <layout type="stack" id="editingstack">
- <page>
- </page>
- <page>
- <layout type="vertical">
- <layout type="horizontal">
- <label>Scheduled Batch ID</label>
- <line id="batch" writable="false" />
- </layout>
- <tabbar id="tabs" />
- <layout type="stack" id="pages">
- <page>
- <layout type="vertical">
- <layout type="horizontal">
- <label>Unit:</label>
- <sqldrop id="unit" />
- <stretch />
- </layout>
- <layout type="horizontal">
- <label>Roasted Coffee:</label>
- <sqldrop data="0" display="1" showdata="true" id="roasted">
- <null />
- <query>SELECT id, name FROM items WHERE category='Coffee: Roasted' AND id IN (SELECT item FROM current_items) ORDER BY name</query>
- </sqldrop>
- <stretch />
- </layout>
- <label>Green Coffee:</label>
- <sqltablearray columns="3" id="greens">
- <column name="Coffee" delegate="sql" showdata="true" null="true" nulltext="Delete" nulldata="delete" data="0" display="1">SELECT id, name FROM coffees WHERE quantity <> 0 ORDER BY name</column>
- <column name="Weight" delegate="numeric" />
- <column name="Remaining" />
- </sqltablearray>
- <layout type="horizontal">
- <label>Green Weight:</label>
- <line id="green" writable="false">0.0</line>
- </layout>
- </layout>
- </page>
- <page>
- <layout type="vertical">
- <sqltablearray columns="2" id="filters">
- <column name="Filter" />
- <column name="Value" />
- </sqltablearray>
- </layout>
- </page>
- <page>
- <layout type="vertical">
- <webview id="batchTag" />
- </layout>
- </page>
- </layout>
- <layout type="horizontal">
- <printerselector id="printerlist" />
- <button name="Print" id="printbutton" type="push" />
- </layout>
- <layout type="horizontal">
- <stretch />
- <button name="Save" id="savebutton" type="push" />
- </layout>
- </layout>
- </page>
- </layout>
- </widget>
- </splitter>
- </layout>
- <program>
- <![CDATA[
- var window = this;
- var saved = false;
- this.windowTitle = TTR("schedule", "Typica - Roasting Schedule");
- var splitter = findChildObject(this, 'splitter');
- window.aboutToClose.connect(function() {
- splitter.saveState("script/schedule/splitter");
- });
- splitter.restoreState("script/schedule/splitter");
- var deletebutton = findChildObject(this, 'deletebutton');
- deletebutton.enabled = false;
- var editingstack = findChildObject(this, 'editingstack');
- var newbutton = findChildObject(this, 'newbutton');
- var batch = findChildObject(this, 'batch');
- var roasted = findChildObject(this, 'roasted');
- var filters = findChildObject(this, 'filters');
- var table = findChildObject(this, 'greens');
- var green = findChildObject(this, 'green');
- var pages = findChildObject(this, 'pages');
- newbutton.clicked.connect(function() {
- saved = false;
- roasted.setCurrentIndex(0);
- filters.clear();
- table.clear();
- green.text = "0.0";
- tabs.setCurrentIndex(0);
- editingstack.setCurrentIndex(1);
- var query = new QSqlQuery();
- query.exec("SELECT nextval('scheduled_roasts_id_seq'::regclass)");
- if(query.next()) {
- batch.text = query.value(0);
- }
- query = query.invalidate();
- drawTag();
- });
- var batches = findChildObject(this, 'batches');
- batches.setQuery("SELECT id, (SELECT name FROM items WHERE id = (data#>>'{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;
- tabs.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('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg.dtd">');
- 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");
- });
- ]]>
- </program>
- </window>
|