Exemple #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());
}
QString InspectionDetailsView::renderHTML(bool)
{
    QString customer_uuid = settings->selectedCustomerUUID();
    QString circuit_uuid = settings->selectedCircuitUUID();
    QString inspection_uuid = settings->selectedInspectionUUID();

    QString html; MTTextStream out(&html);

    writeServiceCompany(out);

    writeCustomersTable(out, customer_uuid);
    out << "<br>";
    writeCircuitsTable(out, customer_uuid, circuit_uuid, 8);

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

    Inspection inspection(inspection_uuid);
    QString inspection_date = inspection.date();
    Inspection::Type type = inspection.type();
    MTQuery nom_inspection_record = Inspection::query({{"circuit_uuid", circuit_uuid}, {"inspection_type", "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_uuid + "/circuit:" + circuit_uuid + (type == Inspection::Repair ? "/repair:" : "/inspection:") + inspection_uuid + "/edit");
    *el << QApplication::translate("MainWindow", "%1:").arg(Inspection::titleForInspectionType(type));
    *el << "&nbsp;" << settings->mainWindowSettings().formatDateTime(inspection_date);
    div.newLine();

    VariableEvaluation::EvaluationContext var_evaluation(customer_uuid, circuit_uuid);
    VariableEvaluation::Variable *variable = NULL;

    var_evaluation.setNominalInspection(nominal_ins);

    MTSqlQuery tables = Table::query({{"scope", Variable::Inspection}}).select("name, variables", "position");
    tables.exec();

    QSet<QString> all_variables;

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

    if (!settings->isShowNotesChecked())
        all_variables.remove("notes");

    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("name");
        }
        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.savedValues(), _table);
        }
    }
    div << table->customHtml(2);

    MTQuery inspections_compressor_query = InspectionCompressor::query({{"inspection_uuid", inspection_uuid}});
    ListOfVariantMaps inspections_compressors = inspections_compressor_query.listAll();
    if (inspections_compressors.count()) {
        VariableEvaluation::EvaluationContext compressor_var_evaluation(customer_uuid, circuit_uuid, 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) {
            Compressor compressor(inspections_compressors.at(i).value("compressor_uuid").toString());
            QString name = compressor.name();
            QString sn = compressor.serialNumber();

            *(header_row->addHeaderCell("width=\"50%\"")) << (sn.isEmpty() ? name : QString("%1 (%2)").arg(name).arg(sn));
            _table = table_row->addCell("style=\"vertical-align: top;\"")->table();
            for (int n = 0; n < compressor_vars.count(); ++n) {
                if (compressor_vars[n]->parentUUID().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(settings->toolBarStack()->isCO2EquivalentChecked(), true, circuit);
    QStringList warnings_list = listWarnings(warnings, circuit, nominal_ins, inspection.savedValues());
    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());
}