예제 #1
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);
}
예제 #2
0
QString OperatorReportView::renderHTML(bool)
{
    QString customer_uuid = settings->selectedCustomerUUID();
    int year = settings->toolBarStack()->filterSinceValue();
    int month_from = settings->toolBarStack()->filterMonthFromValue();
    int month_until = settings->toolBarStack()->filterMonthUntilValue();
    bool show_circuit_name = settings->toolBarStack()->isShowCircuitNameChecked();
    bool CO2_equivalent = settings->toolBarStack()->isCO2EquivalentChecked();
    bool min_5tCO2 = settings->toolBarStack()->isMin5tCO2EquivalentChecked();

    if (year == 0)
        year = QDate::currentDate().year() - 1;
    QString date_from = QString("%1.%2").arg(year).arg(month_from, 2, 10, QChar('0'));
    QString date_until = QString("%1.%2").arg(year).arg(month_until + 1, 2, 10, QChar('0'));

    QString interval_string = QString::number(year);
    if (month_from > 1 || month_until < 12) {
        if (month_from == month_until)
            interval_string.prepend(QString("%1 ").arg(longMonthName(month_from)));
        else
            interval_string.prepend(QString::fromUtf8("%1 \342\200\223 %2 ")
                                    .arg(longMonthName(month_from))
                                    .arg(longMonthName(month_until)));
    }

    QString html; MTTextStream out(&html);

    writeServiceCompany(out);

    Customer customer(customer_uuid);
    customer.readOperatorValues();
    out << "<table cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\">";
    out << "<tr><th style=\"font-size: medium; background-color: floralwhite;\">";
    out << tr("Operator Report: %1").arg(interval_string) << "</th></tr></table><br>";
    out << "<table cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\">";
    out << "<tr><th colspan=\"3\">" << tr("Owner information") << "</th></tr><tr>";
    out << "<th>" << Customer::attributes().value("id") << "</th>";
    out << "<th>" << Customer::attributes().value("company") << "</th>";
    out << "<th>" << Customer::attributes().value("address") << "</th>";
    out << "</tr><tr>";
    out << "<td>" << toolTipLink("customer", customer.companyID(), customer_uuid) << "</td>";
    out << "<td>" << MTVariant(customer.value("company")) << "</td>";
    out << "<td>" << MTVariant(customer.value("address"), MTVariant::Address) << "</td>";
    out << "</tr><tr><th colspan=\"3\">" << tr("Operator information") << "</th></tr><tr>";
    out << "<th>" << Customer::attributes().value("id") << "</th>";
    out << "<th>" << Customer::attributes().value("company") << "</th>";
    out << "<th>" << Customer::attributes().value("address") << "</th>";
    out << "</tr><tr>";
    out << "<td>" << customer.stringValue("operator_id") << "</td>";
    out << "<td>" << MTVariant(customer.value("operator_company")) << "</td>";
    out << "<td>" << MTVariant(customer.value("operator_address"), MTVariant::Address) << "</td>";
    out << "</tr></table><br>";
    out << "<table cellspacing=\"0\" cellpadding=\"4\" style=\"width:100%;\" class=\"highlight\">";
    out << "<tr><th colspan=\"" << (8 + show_circuit_name + CO2_equivalent) << "\" style=\"font-size: medium; background-color: aliceblue;\">";
    out << tr("Circuit information", "Operator report") << "</th></tr><tr>";
    out << "<th rowspan=\"2\">" << QApplication::translate("Circuit", "ID") << "</th>";
    if (show_circuit_name) {
        out << "<th rowspan=\"2\">" << QApplication::translate("Circuit", "Name") << "</th>";
    }
    out << "<th rowspan=\"2\">" << QApplication::translate("Circuit", "Refrigerant") << "</th>";
    if (CO2_equivalent)
        out << "<th rowspan=\"2\">" << QApplication::translate("MainWindow", "GWP") << "</th>";
    out << "<th rowspan=\"2\">" << QApplication::translate("Circuit", "Field of application") << "</th>";
    QString unit = CO2_equivalent ? QApplication::translate("Units", "t of CO\342\202\202 equivalent") : QApplication::translate("Units", "kg");
    out << "<th colspan=\"4\">" << QString("%1 (%2)").arg(QApplication::translate("Circuit", "Refrigerant amount")).arg(unit) << "</th>";
    out << "<th rowspan=\"2\">" << QApplication::translate("Circuit", "Place of operation") << "</th>";
    out << "</tr><tr>";
    out << "<th>" << ((month_from > 1 || month_until < 12) ?
                          tr("At the beginning of the period") :
                          tr("At the beginning of the year")) << "</th>";
    out << "<th>" << tr("Added") << "</th>";
    out << "<th>" << tr("Recovered") << "</th>";
    out << "<th>" << ((month_from > 1 || month_until < 12) ?
                          tr("At the end of the period") :
                          tr("At the end of the year")) << "</th>";
    out << "</tr>";

    MTQuery inspections = Inspection::query();
    inspections.addFilter("inspection_type <> ?", QString::number(Inspection::NominalInspection));
    if (month_from > 1)
        inspections.addFilter("date >= ?", date_from);
    if (month_until < 12)
        inspections.addFilter("date < ?", date_until);
    if (month_from <= 1 || month_until >= 12)
        inspections.addFilter("date", QString("%1%").arg(year));

    QVariantMap nominal_inspection_parents = {{"inspection_type", Inspection::NominalInspection}};

    QVariantMap sums;
    ListOfVariantMaps nominal_inspections;
    QString nominal_inspection_date, commissioning_date, decommissioning_date;
    double refrigerant_amount, refrigerant_amount_begin, refrigerant_amount_end;

    MTQuery circuits_query = Circuit::query({{"customer_uuid", customer_uuid}});
    if (!settings->toolBarStack()->isFilterEmpty()) {
        circuits_query.addFilter(settings->toolBarStack()->filterColumn(), settings->toolBarStack()->filterKeyword());
    }

    MTSqlQuery circuits = circuits_query.select("uuid, id, name, refrigerant, refrigerant_amount, field, operation, disused, hermetic, commissioning, decommissioning", "id, name");
    circuits.exec();
    while (circuits.next()) {
        QString circuit_uuid = circuits.stringValue("uuid");
        QString circuit_id = circuits.stringValue("id");

        inspections.parents().insert("circuit_uuid", circuit_uuid);
        sums = inspections.sumAll("refr_add_am, refr_reco");

        commissioning_date = circuits.stringValue("commissioning").left(7);
        if (commissioning_date >= date_until)
            continue;
        decommissioning_date = circuits.stringValue("decommissioning").left(7);
        if (circuits.intValue("disused") <= Circuit::Commissioned)
            decommissioning_date = "9999";
        else if (decommissioning_date.isEmpty())
            decommissioning_date = QString::number(QDate::currentDate().year());
        if (decommissioning_date < date_from)
            continue;
        refrigerant_amount = circuits.doubleValue("refrigerant_amount");
        refrigerant_amount_begin = 0.0;
        refrigerant_amount_end = refrigerant_amount;
        if (commissioning_date < date_from)
            refrigerant_amount_begin += refrigerant_amount;

        nominal_inspection_parents.insert("circuit_uuid", circuit_uuid);
        nominal_inspections = Inspection::query(nominal_inspection_parents).listAll("date, refr_add_am, refr_reco", "date ASC");
        foreach (const QVariantMap &nominal_inspection, nominal_inspections) {
            nominal_inspection_date = nominal_inspection.value("date", "9999").toString().left(7);
            if (nominal_inspection_date < date_from)
                refrigerant_amount_begin += nominal_inspection.value("refr_add_am", 0.0).toDouble()
                        - nominal_inspection.value("refr_reco", 0.0).toDouble();
            if (nominal_inspection_date < date_until)
                refrigerant_amount_end += nominal_inspection.value("refr_add_am", 0.0).toDouble()
                        - nominal_inspection.value("refr_reco", 0.0).toDouble();
        }

        if (decommissioning_date >= date_from && decommissioning_date < date_until)
            refrigerant_amount_end = 0.0;

        QString refrigerant = circuits.stringValue("refrigerant");
        double GWP = refrigerantGWP(refrigerant);
        if (min_5tCO2) {
            bool hermetic = circuits.boolValue("hermetic");
            if (CO2_equivalent ? (qMax(refrigerant_amount_begin, refrigerant_amount_end) * GWP < (hermetic ? 10000.0 : 5000.0))
                               : (qMax(refrigerant_amount_begin, refrigerant_amount_end) < (hermetic ? 6.0 : 3.0)))
                continue;
        }

        out << "<tr onclick=\"window.location = 'customer:" << customer_uuid
            << "/circuit:" << circuit_uuid << "'\" style=\"cursor: pointer;\">";
        out << "<td>" << toolTipLink("customer/circuit", circuit_id.rightJustified(5, '0'), customer_uuid, circuit_uuid) << "</td>";
        if (show_circuit_name) {
            out << "<td>" << MTVariant(circuits.stringValue("name")) << "</td>";
        }
        out << "<td>" << refrigerant << "</td>";
        double multiplier = CO2_equivalent ? (GWP / 1000.0) : 1.0;
        if (CO2_equivalent)
            out << "<td>" << GWP << "</td>";
        QString field = circuits.stringValue("field");
        out << "<td>" << fieldsOfApplication().value(field, field) << "</td>";
        out << "<td>" << refrigerant_amount_begin * multiplier << "</td>";
        out << "<td>" << sums.value("refr_add_am").toDouble() * multiplier << "</td>";
        out << "<td>" << sums.value("refr_reco").toDouble() * multiplier << "</td>";
        out << "<td>" << refrigerant_amount_end * multiplier << "</td>";
        out << "<td>" << MTVariant(circuits.value("operation")) << "</td>";
        out << "</tr>";
    }