/** * Ajax request to submit data * @param array $request the request * @return array ajax format status */ public function submitFormData($request) { $errors = array(); $data = R3EcoGisHelper::getBuildingData($this->bu_id); $this->mu_id = $data['mu_id']; $request = array_merge(array('us_id' => ''), $request); $request['em_id'] = forceInteger($request['id'], 0, false, '.'); if ($this->act != 'del') { // Change required fields if ($request['em_is_production'] == 'T') { $request['up_id'] = null; $this->setFieldAttrib('esu_id', array('required' => true)); } else { if ($request['us_id'] == '') { $this->setFieldAttrib('esu_id', array('required' => true)); } else { $this->setFieldAttrib('up_id', array('required' => true)); } } if ($request['us_id'] == '') { $request['esu_id'] = R3EcoGisHelper::getEnergySourceUdmID($this->do_id, $request['es_id'], $request['udm_id'], $this->mu_id); } else { $request['esu_id'] = ''; } $request['emo_id'] = R3EcoGisHelper::getEnergyMeterObjectIdByCode('BUILDING'); $request['em_object_id'] = $request['bu_id']; $errors = $this->checkFormData($request); } if (count($errors) > 0) { return $this->getAjaxErrorResult($errors); } else { $id = $this->applyData($request); R3EcoGisEventNotifier::notifyDataChanged($this, array('data_changed' => true)); return array('status' => R3_AJAX_NO_ERROR, 'js' => "submitFormDataDoneMeter({$id})"); } }
/** * Check the compatibility between the building purpose use and the actions for the given building * @param integer $bu_id building id */ protected function updateBuildingPurposeUseCompatibility($bu_id, $new_bpu_id) { $db = ezcDbInstance::get(); $bu_id = (int) $bu_id; $new_bpu_id = (int) $new_bpu_id; $emo_id = R3EcoGisHelper::getEnergyMeterObjectIdByCode('BUILDING'); $sql = "SELECT gc_id FROM building_purpose_use WHERE bpu_id={$new_bpu_id}"; $new_gc_id = $db->query($sql)->fetchColumn(); if ($new_gc_id !== null) { $sql = "UPDATE action_catalog SET gc_id={$new_gc_id} WHERE gc_id<>{$new_gc_id} AND emo_id={$emo_id} AND ac_object_id={$bu_id}"; $db->exec($sql); } }
/** * Return the data for a single customer */ public function getData($id = null) { $lang = R3Locale::getLanguageID(); $db = ezcDbInstance::get(); if ($this->act != 'add') { $q = $db->createSelectQuery(); $q->select('*')->from('action_catalog_data ac')->where("ac_id={$this->id}"); $vlu = $db->query($q)->fetch(PDO::FETCH_ASSOC); $vlu['mu_name'] = $vlu["mu_name_{$lang}"]; // Autocomplete $vlu['ac_expected_co2_reduction'] = $vlu['ac_expected_co2_reduction_calc'] / 1000; // Get the calculated value - the db value is not used $vlu['ac_expected_energy_saving_mwh'] = $vlu['ac_expected_energy_saving_kwh'] / 1000; $vlu['ac_green_electricity_purchase_mwh'] = R3NumberFormat($vlu['ac_green_electricity_purchase_kwh'] / 1000, 2, true); $vlu['ac_co2_reduction_tco2'] = R3NumberFormat($vlu['ac_co2_reduction'] / 1000, 2, true); $vlu['ac_expected_renewable_energy_production_mwh'] = $vlu['ac_expected_renewable_energy_production_kwh'] / 1000; // Get the calculated value - the db value is not used // get expected energy savings $vlu['ac_expected_energy_savings'] = array(); $sql = "SELECT action_catalog_energy.*, energy_source_udm.*, " . " esu_kwh_factor * ace_expected_energy_saving AS ace_expected_energy_saving_kwh, " . " esu_co2_factor * ace_expected_energy_saving AS ace_expected_co2_reduction_calc " . " FROM action_catalog_energy " . " INNER JOIN energy_source_udm USING(esu_id) " . " WHERE ac_id={$this->id} " . "ORDER BY ace_id"; $r = $db->query($sql); while ($row = $r->fetch()) { $lkp = $this->getLookupDataForEnergySavings($row['ges_id']); $tmp = array('ges_id' => $row['ges_id'], 'es_id' => $row['es_id'], 'udm_id' => $row['udm_id'], 'ac_expected_energy_saving' => R3NumberFormat($row['ace_expected_energy_saving'], 2, true), 'ac_expected_energy_saving_mwh' => R3NumberFormat($row['ace_expected_energy_saving_kwh'] / 1000, 2, true), 'ac_expected_co2_reduction' => R3NumberFormat($row['ace_expected_co2_reduction_calc'] / 1000, 2, true), 'es_id_consumption_values' => $lkp['es_id_consumption_values'], 'udm_id_consumption_values' => $lkp['udm_id_consumption_values']); $vlu['ac_expected_energy_savings'][] = $tmp; } // get related actions $vlu['ac_related_actions'] = array(); $vlu['ac_related_required_actions'] = array(); $vlu['ac_related_excluded_actions'] = array(); $sql = "SELECT action_catalog.ac_id, ac_name_{$lang}, acd_type " . " FROM action_catalog " . " INNER JOIN action_catalog_dependencies on action_catalog.ac_id = action_catalog_dependencies.ac_related_id " . " WHERE action_catalog_dependencies.ac_id={$this->id}"; $r = $db->query($sql); while ($row = $r->fetch()) { if ($row['acd_type'] == 'R') { $index = 'ac_related_required_actions'; } else { if ($row['acd_type'] == 'D') { $index = 'ac_related_actions'; } else { if ($row['acd_type'] == 'E') { $index = 'ac_related_excluded_actions'; } else { throw new exceltion('Invalid value for acd_type'); } } } $tmp = array('ac_id' => $row['ac_id'], 'ac_name' => $row['ac_name_' . $lang]); array_push($vlu[$index], $tmp); } // get benefit year $vlu['ac_benefit_year'] = array(); $sql = "SELECT acby_year, acby_benefit \r\n FROM action_catalog_benefit_year\r\n WHERE ac_id={$this->id}\r\n ORDER BY acby_year"; $vlu['enable_benefit_year'] = 'F'; foreach ($db->query($sql, PDO::FETCH_ASSOC) as $row) { $vlu['ac_benefit_year'][] = $row; $vlu['enable_benefit_year'] = 'T'; } if ($this->bu_id != '') { $vlu['bu_id'] = $this->bu_id; } $vlu = array_merge($vlu, R3EcoGisHelper::getChangeLogData('action_catalog', $vlu['ac_id'])); } else { $vlu = array(); $vlu['ac_id'] = null; $vlu['bu_id'] = $this->bu_id; $mu_values = R3EcoGisHelper::getMunicipalityList($this->do_id); if (count($mu_values) == 1) { $vlu['mu_id'] = key($mu_values); } else { if ($this->auth->getParam('mu_id') > 0) { $vlu['mu_id'] = $this->auth->getParam('mu_id'); } } if (isset($vlu['mu_id']) && $vlu['mu_id'] != '') { $vlu['mu_name'] = R3EcoGisHelper::getMunicipalityName($vlu['mu_id']); } if ($this->bu_id != '') { $vlu['mu_id'] = $db->query('SELECT mu_id FROM building WHERE bu_id=' . (int) $this->bu_id)->fetchColumn(); $emo_id = R3EcoGisHelper::getEnergyMeterObjectIdByCode('BUILDING'); $codePart1 = $db->query('SELECT bu_code FROM building WHERE bu_id=' . (int) $this->bu_id)->fetchColumn(); $codePart2 = $db->query("SELECT LPAD((COUNT(ac_code) + 1)::TEXT, 2, '0') FROM action_catalog WHERE emo_id={$emo_id} AND ac_object_id=" . (int) $this->bu_id)->fetchColumn(); if ($codePart1 == '') { $vlu['ac_code'] = $codePart2; } else { $vlu['ac_code'] = "{$codePart1}-{$codePart2}"; } // Ricavo Macro-settore, Settore, Sub-settore list($gc_id, $gc_id_parent) = R3EcoGisHelper::getGlobalCategoryForActionCatalogBuilding($this->bu_id); if ($gc_id_parent === null || $gc_id === null) { throw new Exception(_("Destinazione d'uso dell'edificio mancante, o categoria PAES non valida")); } $vlu['gc_parent_id'] = $gc_id_parent; $vlu['gc_id'] = $gc_id; } else { if (isset($vlu['mu_id'])) { $vlu['ac_code'] = (int) $db->query('SELECT MAX(ac_code) FROM action_catalog WHERE mu_id=' . (int) $vlu['mu_id'])->fetchColumn() + 1; } } $vlu['ac_benefit_end_date'] = $this->auth->getConfigValue('APPLICATION', 'DEFAULT_ACTION_BENEFIT_END_DATE', '2020-12-31'); } $this->data = $vlu; // Save the data (prevent multiple sql) return $vlu; }