var dateSelect = findChildObject(this, 'dates'); var dateQuery = new QSqlQuery(); dateQuery.exec("SELECT time::date FROM roasting_log WHERE time = (SELECT min(time) FROM roasting_log) OR time = (SELECT max(time) FROM roasting_log) ORDER BY time ASC"); dateQuery.next(); var lifetimeStartDate = dateQuery.value(0); var lifetimeEndDate; if(dateQuery.next()) { lifetimeEndDate = dateQuery.value(0); } else { lifetimeEndDate = lifetimeStartDate; } dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate); dateQuery.invalidate(); dateSelect.currentIndex = QSettings.value("script/history/dateIndex", 6); var table = findChildObject(this, 'table'); table.openEntryRow.connect(function(arg) { var details = createWindow("batchDetails"); details.loadData(table, arg); }); function refresh() { var dateRange = dateSelect.currentRange(); var startDate = "'"+dateRange[0]+"'"; var endDate = "'"+dateRange[dateRange.length - 1]+"'"; var q = "SELECT time, machine, (SELECT name FROM items WHERE id = roasted_id) AS name, unroasted_total_quantity AS green, roasted_quantity AS roasted, CASE WHEN unroasted_total_quantity = 0 THEN NULL ELSE ((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity * 100::numeric)::numeric(12,2) END AS weight_loss, duration, annotation FROM roasting_log WHERE time >= " + startDate + "::date AND time < " + endDate + "::date + interval '1 day' ORDER BY time DESC"; table.setQuery(q); table.hideColumn(1); } dateSelect.rangeUpdated.connect(function() { if(dateSelect.currentIndex != 24) { // Custom date range QSettings.setValue("script/history/dateIndex", dateSelect.currentIndex); } refresh(); }); refresh();