예제 #1
0
QString InspectionDetailsView::renderHTML()
{
    QString customer_id = settings->selectedCustomer();
    QString circuit_id = settings->selectedCircuit();
    QString inspection_date = settings->selectedInspection();

    QString html; MTTextStream out(&html);

    if (settings->mainWindowSettings().serviceCompanyInformationVisible()) {
        HTMLTable *service_company = writeServiceCompany();
        out << service_company->html();
        delete service_company;
        out << "<br>";
    }

    writeCustomersTable(out, customer_id);
    out << "<br>";
    writeCircuitsTable(out, customer_id, circuit_id, 8);

    QVariantMap circuit = Circuit(customer_id, circuit_id).list("*, " + circuitRefrigerantAmountQuery());

    Inspection inspection_record(customer_id, circuit_id, inspection_date);
    QVariantMap inspection = inspection_record.list();
    bool nominal = inspection.value("nominal").toInt();
    bool repair = inspection.value("repair").toInt();
    Inspection nom_inspection_record(customer_id, circuit_id, "");
    nom_inspection_record.parents().insert("nominal", "1");
    nom_inspection_record.addFilter("date <= ?", inspection_date);
    QVariantMap nominal_ins = nom_inspection_record.list("*", "date DESC");

    HTMLTable *table = new HTMLTable("cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\" class=\"no_border\""),
        *_table;
    HTMLTableRow *header_row = table->addRow();
    HTMLTableRow *table_row = table->addRow();
    HTMLTableCell *cell;
    HTMLParentElement *el;
    HTMLDiv div;

    div << html;
    div.newLine();

    el = div.table("cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\" class=\"no_border\"")
            ->addRow()->addHeaderCell("colspan=\"2\" style=\"font-size: medium; background-color: lightgoldenrodyellow;\"")
            ->link("customer:" + customer_id + "/circuit:" + circuit_id + (repair ? "/repair:" : "/inspection:") + inspection_date + "/edit");
    if (nominal) *el << tr("Nominal Inspection:");
    else if (repair) *el << tr("Repair:");
    else *el << tr("Inspection:");
    *el << "&nbsp;" << settings->mainWindowSettings().formatDateTime(inspection_date);
    div.newLine();

    VariableEvaluation::EvaluationContext var_evaluation(customer_id, circuit_id);
    VariableEvaluation::Variable *variable = NULL;

    var_evaluation.setNominalInspection(nominal_ins);

    Table tables_record("", QString(), MTDictionary("scope", "1"));
    MTSqlQuery tables = tables_record.select("id, variables", Qt::DescendingOrder);
    tables.setForwardOnly(true);
    tables.exec();

    QSet<QString> all_variables;

    Variables vars;
    while (vars.next()) {
        if (vars.parentID().isEmpty())
            all_variables << vars.id();
    }

    while (tables.next() || all_variables.count()) {
        QStringList table_vars;
        cell = header_row->addHeaderCell("width=\"50%\"");
        if (tables.isValid()) {
            table_vars = tables.stringValue("variables").split(";");
            all_variables.subtract(table_vars.toSet());
            *cell << tables.stringValue("id");
        }
        else {
            table_vars = all_variables.toList();
            all_variables.clear();
            *cell << tr("Other");
        }

        _table = table_row->addCell("style=\"vertical-align: top;\"")->table();

        for (int n = 0; n < table_vars.count(); ++n) {
            variable = var_evaluation.variable(table_vars.at(n));
            if (!variable) continue;
            showVariableInInspectionTable(variable, var_evaluation, inspection, _table);
        }
    }
    div << table->customHtml(2);

    InspectionsCompressor inspections_compressor_rec(QString(), MTDictionary(QStringList() << "customer_id" << "circuit_id" << "date",
                                                                             QStringList() << customer_id << circuit_id << inspection_date));
    ListOfVariantMaps inspections_compressors = inspections_compressor_rec.listAll();
    if (inspections_compressors.count()) {
        VariableEvaluation::EvaluationContext compressor_var_evaluation = VariableEvaluation::EvaluationContext(customer_id, circuit_id, Variable::Compressor);
        QList<VariableEvaluation::Variable *> compressor_vars = compressor_var_evaluation.listVariables();

        table = new HTMLTable("cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\" class=\"no_border\"");
        header_row = table->addRow();
        table_row = table->addRow();

        for (int i = 0; i < inspections_compressors.count(); ++i) {
            QVariantMap compressor = Compressor(inspections_compressors.at(i).value("compressor_id").toString()).list();

            *(header_row->addHeaderCell("width=\"50%\"")) << compressor.value("name").toString();
            _table = table_row->addCell("style=\"vertical-align: top;\"")->table();
            for (int n = 0; n < compressor_vars.count(); ++n) {
                if (compressor_vars[n]->parentID().isEmpty())
                    showVariableInInspectionTable(compressor_vars[n], compressor_var_evaluation, inspections_compressors[i], _table);
            }
        }
        div.newLine();
        *(div.table("cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\" class=\"no_border\"")->addRow()->addHeaderCell("style=\"font-size: medium;\""))
                << tr("Compressors");
        div.newLine();
        div << table->customHtml(2);
    }

//*** Warnings ***
    Warnings warnings(QSqlDatabase::database(), true, circuit);
    QStringList warnings_list = listWarnings(warnings, circuit, nominal_ins, inspection);
    if (warnings_list.count()) {
        div.newLine();
        _table = div.table("cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\"");
        *(_table->addRow()->addHeaderCell("style=\"font-size: medium;\"")) << tr("Warnings");
        *(_table->addRow()->addCell()) << warnings_list.join(", ");
    }
    return viewTemplate("inspection").arg(div.html());
}
예제 #2
0
QString AgendaView::renderHTML()
{
    bool CO2_equivalent = settings->toolBarStack()->isCO2EquivalentChecked();

    QString html; MTTextStream out(&html);

    if (settings->mainWindowSettings().serviceCompanyInformationVisible()) {
        HTMLTable *service_company = writeServiceCompany();
        out << service_company->html();
        delete service_company;
        out << "<br>";
    }

    QMultiMap<QString, QList<QVariant> > next_inspections_map;

    MultiMapOfVariantMaps customers(Customer("").mapAll("id", "company"));

    MTRecord circuits_record("circuits", "id", "", MTDictionary("disused", "0"));
    if (!settings->toolBarStack()->isFilterEmpty()) {
        circuits_record.addFilter(settings->toolBarStack()->filterColumn(), settings->toolBarStack()->filterKeyword());
    }
    circuits_record.addJoin("LEFT JOIN (SELECT customer, circuit, MAX(date) AS date FROM inspections"
                            " WHERE outside_interval = 0 GROUP BY customer, circuit) AS ins"
                            " ON ins.customer = circuits.parent AND ins.circuit = circuits.id");
    circuits_record.addJoin("LEFT JOIN (SELECT i.customer, i.circuit, i.date, i.nominal, i.refr_add_am FROM inspections AS i"
                            " LEFT JOIN inspections AS j ON i.customer = j.customer AND i.circuit = j.circuit"
                            " AND i.date < j.date WHERE j.date IS NULL) AS all_ins"
                            " ON all_ins.customer = circuits.parent AND all_ins.circuit = circuits.id");
    MTSqlQuery circuits = circuits_record.select("circuits.parent, circuits.id, circuits.name, circuits.operation, circuits.refrigerant, "
                                                 + circuitRefrigerantAmountQuery()
                                                 + ", circuits.hermetic, circuits.leak_detector, circuits.inspection_interval,"
                                                 " COALESCE(ins.date, circuits.commissioning) AS last_regular_inspection,"
                                                 " COALESCE(all_ins.date, circuits.commissioning) AS last_inspection,"
                                                 " all_ins.nominal, all_ins.refr_add_am");
    circuits.setForwardOnly(true);
    circuits.exec();
    while (circuits.next()) {
        QString refrigerant = circuits.stringValue("refrigerant");
        double refrigerant_amount = circuits.doubleValue("refrigerant_amount");
        int inspection_interval = Warnings::circuitInspectionInterval(refrigerant, refrigerant_amount, CO2_equivalent,
                                                                      circuits.intValue("hermetic"),
                                                                      circuits.intValue("leak_detector"),
                                                                      circuits.intValue("inspection_interval"));
        if (inspection_interval) {
            QString last_regular_inspection_date = circuits.stringValue("last_regular_inspection");
            if (last_regular_inspection_date.isEmpty())
                continue;
            QString next_regular_inspection_date = QDate::fromString(last_regular_inspection_date.split("-").first(), DATE_FORMAT)
                    .addDays(inspection_interval).toString(DATE_FORMAT);
            QString last_inspection_date = circuits.stringValue("last_inspection");
            if (!last_inspection_date.isEmpty()) {
                QString next_inspection_date = QDate::fromString(last_inspection_date.split("-").first(), DATE_FORMAT)
                        .addDays(30).toString(DATE_FORMAT);
                if (next_inspection_date < next_regular_inspection_date &&
                        circuits.intValue("nominal") == 0 && circuits.doubleValue("refr_add_am") > 0.0)
                    next_inspections_map.insert(next_inspection_date,
                                                QList<QVariant>()
                                                    << circuits.stringValue("parent")
                                                    << circuits.stringValue("id")
                                                    << circuits.stringValue("name")
                                                    << circuits.stringValue("operation")
                                                    << refrigerant
                                                    << refrigerant_amount
                                                    << last_inspection_date
                                                    << true);
            }
            next_inspections_map.insert(next_regular_inspection_date,
                                        QList<QVariant>()
                                            << circuits.stringValue("parent")
                                            << circuits.stringValue("id")
                                            << circuits.stringValue("name")
                                            << circuits.stringValue("operation")
                                            << refrigerant
                                            << refrigerant_amount
                                            << last_regular_inspection_date
                                            << false);
        }
    }

    out << "<table cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\"><tr>";
    out << "<th colspan=\"7\" style=\"font-size: medium;\">" << tr("Agenda") << "</th></tr>";
    out << "<tr><th>" << tr("Next inspection") << "</th><th>" << tr("Customer") << "</th>";
    out << "<th>" << tr("Circuit") << "</th><th>" << QApplication::translate("Circuit", "Place of operation") << "</th>";
    out << "<th>" << QApplication::translate("Circuit", "Refrigerant") << "</th>";
    out << "<th>" << replaceUnsupportedCharacters(QApplication::translate("MainWindow", "CO\342\202\202 equivalent")) << "</th>";
    out << "<th>" << tr("Last inspection") << "</th></tr>";

    QMapIterator<QString, QList<QVariant> > i(next_inspections_map);
    while (i.hasNext()) { i.next();
        QString customer = i.value().value(0).toString();
        QString circuit = i.value().value(1).toString();
        QString circuit_name = i.value().value(2).toString();
        QString operation = i.value().value(3).toString();
        QString refrigerant = i.value().value(4).toString();
        double refrigerant_amount = i.value().value(5).toDouble();
        QString last_inspection_date = i.value().value(6).toString();
        bool reinspection = i.value().value(7).toBool();
        int days_to = QDate::currentDate().daysTo(QDate::fromString(i.key(), DATE_FORMAT));
        QString next_inspection;
        switch (days_to) {
            case -1: next_inspection = tr("Yesterday"); break;
            case 0: next_inspection = tr("Today"); break;
            case 1: next_inspection = tr("Tomorrow"); break;
            default: next_inspection = settings->mainWindowSettings().formatDate(i.key()); break;
        }
        QString colour;
        if (days_to < 0) colour = "tomato";
        else if (days_to < 31) colour = "yellow";
        out << "<tr><td class=\"" << colour << "\">";
        if (reinspection)
            out << "<i>";
        out << next_inspection;
        if (reinspection)
            out << "*</i>";
        out << "</td><td class=\"" << colour << "\"><a href=\"customer:" << customer << "\">";
        out << formatCompanyID(customer) << " (" << escapeString(customers.value(customer).value("company").toString()) << ")</a></td>";
        out << "<td class=\"" << colour << "\"><a href=\"customer:" << customer << "/circuit:" << circuit << "\">";
        out << circuit.rightJustified(5, '0');
        if (!circuit_name.isEmpty()) { out << " (" << escapeString(circuit_name) << ")"; }
        out << "</a></td>";
        out << "<td class=\"" << colour << "\">" << escapeString(operation) << "</td>";
        out << "<td class=\"" << colour << "\">" << refrigerant_amount << "&nbsp;" << QApplication::translate("Units", "kg")
            << " " << escapeString(refrigerant) << "</td>";
        out << "<td class=\"" << colour << "\">" << CO2Equivalent(refrigerant, refrigerant_amount)
            << "&nbsp;" << QApplication::translate("Units", "t") << "</td>";
        out << "<td class=\"" << colour << "\">";
        if (last_inspection_date.contains("-"))
            out << "<a href=\"customer:" << customer << "/circuit:" << circuit << "/inspection:"
                << last_inspection_date << "\">" << settings->mainWindowSettings().formatDateTime(last_inspection_date) << "</a>";
        else
            out << settings->mainWindowSettings().formatDate(last_inspection_date);
        out << "</td></tr>";
    }
    out << "</table>";

    return viewTemplate("agenda")
            .arg(settings->isPrinterFriendlyVersionChecked() ? "/*" : "")
            .arg(settings->isPrinterFriendlyVersionChecked() ? "*/" : "")
            .arg(html);
}