123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919 |
- <window id="batchWindow">
- <menu name="File">
- <item id="print" shortcut="Ctrl+P">Print...</item>
- </menu>
- <menu name="Batch">
- <item id="new" shortcut="Ctrl+N">New Batch...</item>
- </menu>
- <layout type="vertical">
- <layout type="horizontal">
- <label>Scheduled Batch ID:</label>
- <line id="search" />
- <stretch />
- </layout>
- <tabbar id="tabs" />
- <layout type="stack" id="pages">
- <page>
- <layout type="vertical">
- <layout type="horizontal">
- <label>Filter:</label>
- <line id="filter" />
- <label>Value:</label>
- <line id="filtervalue" />
- <stretch />
- </layout>
- <sqlview id="batches" />
- </layout>
- </page>
- <page>
- <layout type="vertical">
- <splitter type="horizontal" id="splitter">
- <widget id="required">
- <layout type="vertical">
- <layout type="horizontal">
- <label>Machine:</label>
- <line id="machine" writable="false" />
- <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="positivenumeric" />
- <column name="Remaining" />
- </sqltablearray>
- <layout type="horizontal">
- <label>Green Weight:</label>
- <line id="green" writable="false">0.0</line>
- </layout>
- <layout type="horizontal">
- <button name="Load Profile" type="push" id="load" />
- <button name="No Profile" type="push" id="noprofile" />
- </layout>
- <layout type="horizontal">
- <label>Time:</label>
- <line id="time" writable="false" />
- <label>Duration:</label>
- <line id="duration" writable="false" />
- </layout>
- <layout type="horizontal">
- <label>Roasted Weight:</label>
- <line id="roast" validator="numeric">0.0</line>
- <label>Weight Loss:</label>
- <line id="wloss" writable="false" />
- <button type="check" id="approval" name="Approved" />
- <stretch />
- </layout>
- <layout type="horizontal">
- <label>Annotation:</label>
- <textarea id="annotation" />
- </layout>
- <layout type="horizontal">
- <button name="Submit" id="submit" type="push" />
- <button name="Save log as target profile" type="check" id="target" />
- </layout>
- </layout>
- </widget>
- <widget>
- <layout type="vertical">
- <label>Connected Scales</label>
- <layout type="vertical" id="scales" />
- <label>Expected Weight Loss</label>
- <line id="lossspec" writable="false" />
- <label>Expected Roasted Weight</label>
- <layout type="horizontal">
- <label>Min:</label>
- <line id="minroastweight" writable="false" />
- </layout>
- <layout type="horizontal">
- <label>Expected:</label>
- <line id="expectedroastweight" writable="false" />
- </layout>
- <layout type="horizontal">
- <label>Max:</label>
- <line id="maxroastweight" writable="false" />
- </layout>
- <label>Degree of Roast</label>
- <label>Whole Bean:</label>
- <roastcoloredit id="wholecolor" />
- <label id="wholespec">-</label>
- <label>Ground:</label>
- <roastcoloredit id="groundcolor" />
- <label id="groundspec">-</label>
- <label>Specification Details</label>
- <textarea id="specnotes" />
- <layout type="horizontal">
- <label>File ID:</label>
- <line id="filenofield" writable="false" />
- </layout>
- <stretch />
- </layout>
- </widget>
- <widget id="tagcontainer" ignoreSizePolicy="true">
- <layout type="vertical">
- <webview id="batchTag" />
- <layout type="horizontal">
- <printerselector id="printerlist" />
- <button name="Print" id="printbutton" type="push" />
- </layout>
- </layout>
- </widget>
- </splitter>
- </layout>
- </page>
- </layout>
- </layout>
- <program>
- <![CDATA[
- var scheduledID = 0;
- var window = this;
- var splitter = findChildObject(this, 'splitter');
- window.aboutToClose.connect(function() {
- splitter.saveState("script/newbatch/splitter");
- });
- splitter.setCollapsible(0, false);
- splitter.restoreState("script/newbatch/splitter");
- var tabs = findChildObject(this, 'tabs');
- tabs.addTab(TTR("batchWindow", "Schedule"));
- tabs.addTab(TTR("batchWindow", "Batch"));
- var pages = findChildObject(this, 'pages');
- tabs.currentChanged.connect(function(index) {
- pages.setCurrentIndex(index);
- });
- var batches = findChildObject(this, 'batches');
- var filter = findChildObject(this, 'filter');
- var filtervalue = findChildObject(this, 'filtervalue');
- filter.setText(QSettings.value("script/newbatch/filter", ""));
- filtervalue.setText(QSettings.value("script/newbatch/filtervalue", ""));
- var updateSchedule = function() {
- var filterclause = "";
- if(filter.text.length > 0) {
- filterclause = " AND data->'filters' @> "
- filterclause += "'[{\"key\":\"" + filter.text + '"';
- if(filtervalue.text.length > 0) {
- filterclause += ',"value":"' + filtervalue.text + '"';
- }
- filterclause += "}]'";
- }
- var q = "SELECT id, (SELECT name FROM items WHERE id = (data#>>'{roasted}')::numeric), (data#>>'{green_weight}')::numeric FROM scheduled_roasts WHERE machine IS NULL" + filterclause;
- batches.setQuery(q);
- };
- filter.editingFinished.connect(function() {
- QSettings.setValue("script/newbatch/filter", filter.text);
- updateSchedule();
- });
- filtervalue.editingFinished.connect(function() {
- QSettings.setValue("script/newbatch/filtervalue", filtervalue.text);
- updateSchedule();
- });
- updateSchedule();
- if(batches.rows() > 0) {
- tabs.setCurrentIndex(0);
- } else {
- tabs.setCurrentIndex(1);
- }
- batches.setHeaderData(0, TTR("batchWindow", "ID"));
- batches.setHeaderData(1, TTR("batchWindow", "Roasted Coffee"));
- batches.setHeaderData(2, TTR("batchWindow", "Green Weight"));
- batches.alternatingRowColors = true;
- batches.selectionMode = 1;
- batches.selectionBehavior = 1;
- batches.editTreiggers = 0;
- var snotifier = Application.subscribe("scheduledroastschange");
- snotifier.notify.connect(function() {
- updateSchedule();
- });
- 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 machine = findChildObject(this, "machine");
- machine.setText(selectedRoasterName + " (" + selectedRoasterID + ")");
- var newMenu = findChildObject(this, 'new');
- newMenu.triggered.connect(function() {
- var bwindow = createWindow("batchWindow");
- bwindow.windowTitle = TTR("batchWindow", "Typica - [*]New Batch");
- });
- var batch = this;
- var table = findChildObject(this, 'greens');
- var green = findChildObject(this, 'green');
- var model = table.model();
- var lossField = findChildObject(this, 'wloss');
- lossField.maximumWidth = 80;
- var roasted = findChildObject(this, 'roasted');
- var roastwt = findChildObject(this, 'roast');
- roastwt.maximumWidth = 80;
- var scalesLayout = findChildObject(this, 'scales');
- scalesLayout.spacing = 10;
- var batchTag = findChildObject(this, 'batchTag');
- if(navigationwindow.loggingWindow.scales.length > 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 rowColor = QBrush("black");
- if(displayValue < 0)
- {
- rowColor = QBrush("red");
- }
- 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);
- table.setData(r, 0, rowColor, 9);
- table.setData(r, 1, rowColor, 9);
- table.setData(r, 2, rowColor, 9);
- }
- }
- }
- 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);
- var wholespec = findChildObject(this, 'wholespec');
- var groundspec = findChildObject(this, 'groundspec');
- roasted['currentIndexChanged(int)'].connect(function() {
- 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 + ")";
- batch.windowTitle = title;
- 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);
- 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.prepare("SELECT loss, tolerance, notes, spec 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 = "";
- 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);
- if(query.value(3).length > 0) {
- var spec = JSON.parse(query.value(3));
- var wholespectext = "";
- var groundspectext = "";
- if(spec.color.whole.expected != undefined) {
- wholespectext += spec.color.whole.expected;
- if(spec.color.whole.tolerance != undefined) {
- wholespectext += " +/- " + spec.color.whole.tolerance;
- }
- wholespec.text = wholespectext;
- } else {
- wholespec.text = "";
- }
- if(spec.color.ground.expected != undefined) {
- groundspectext += spec.color.ground.expected;
- if(spec.color.ground.tolerance != undefined) {
- groundspectext += " +/- " + spec.color.ground.tolerance;
- }
- groundspec.text = groundspectext;
- } else {
- groundspec.text = "";
- }
- } else {
- wholespec.text = "";
- groundspec.text = "";
- }
- } else {
- lossspec.text = "";
- specnotes.plainText = "";
- wholespec.text = "";
- groundspec.text = "";
- }
- query = query.invalidate();
- drawTag();
- });
- 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');
- var markTaken = function(id) {
- var query = new QSqlQuery();
- query.prepare("UPDATE scheduled_roasts SET machine = :mid WHERE id = :id");
- query.bind(":mid", selectedRoasterID);
- query.bind(":id", scheduledID);
- query.exec();
- query = query.invalidate();
- }
- 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() {
- if(scheduledID > 0) {
- markTaken();
- }
- 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();
- 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 buffer = new QBuffer(query.value(0));
- var pname = query.value(1);
- Windows.loggingWindow.loadPlan(buffer, pname);
- }
- }
- query = query.invalidate();
- }
- 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.clearLog();
- navigationwindow.loggingWindow.raise();
- navigationwindow.loggingWindow.activateWindow();
- if(scheduledID > 0) {
- markTaken();
- }
- }
- 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 wholecolor = findChildObject(this, 'wholecolor');
- var groundcolor = findChildObject(this, 'groundcolor');
- 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, person, additional_data) 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 + "}', :user, :extradata) 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.bind(":user", Application.currentTypicaUser());
- var extradata = new Object;
- var colordata = new Object;
- if(wholecolor.value.length > 0) {
- colordata.whole = wholecolor.value;
- }
- if(groundcolor.value.length > 0) {
- colordata.ground = groundcolor.value;
- }
- if(colordata.whole || colordata.ground) {
- extradata.color = colordata;
- }
- query2.bind(":extradata", JSON.stringify(extradata));
- 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();
- }
- if(scheduledID > 0) {
- query.prepare("UPDATE scheduled_roasts SET machine = :mid, time = :time WHERE id = :id");
- query.bind(":mid", selectedRoasterID);
- query.bind(":time", timefield.text);
- query.bind(":id", scheduledID);
- 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('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 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/qrcode.js");
- scriptFile.open(1);
- output.writeCDATA(scriptFile.readToString());
- scriptFile.close();
- output.writeEndElement();
-
- 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.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", "barcode");
- output.writeAttribute("class", "barcode128h");
- output.writeAttribute("align", "center");
- output.writeEndElement();
-
- output.writeStartElement("script");
- var c128data = 'var strBarcodeHTML = code128("';
- c128data += filenofield.text;
- c128data += '", "C");'
- c128data += 'document.getElementById("barcode").innerHTML = strBarcodeHTML;';
- output.writeCDATA(c128data);
- output.writeEndElement();
-
- 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);
- });
- var loadBatch = function(id) {
- var query = new QSqlQuery();
- query.prepare("SELECT data FROM scheduled_roasts WHERE id = :id");
- query.bind(":id", id);
- query.exec();
- if(query.next()) {
- scheduledID = id;
- tabs.setCurrentIndex(1);
- 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;
- }
- }
- }
- }
- } else {
- displayInfo(TTR("batchWindow", "Search Failed"),
- TTR("batchWindow", "Scheduled batch ID not found."));
- }
- query = query.invalidate();
- drawTag();
- };
- batches.selectEntry.connect(function(id) {
- loadBatch(id);
- });
- var search = findChildObject(this, 'search');
- search.returnPressed.connect(function() {
- if(search.text.length == 0) {
- return;
- }
- if(Number(search.text)) {
- loadBatch(Number(search.text));
- }
- search.text = "";
- });
- ]]>
- </program>
- </window>
|