public function calculateWaterDemand($geom = null)
 {
     $zone = $this->zone_name;
     while ($zone != null) {
         $formula = WaterRequestFormulas::model()->find('zone=:zone', array(':zone' => $zone));
         if ($formula) {
             break;
         }
         $zone = Zones::parentZone($zone);
     }
     //Yii::log('ctype_digit='.print_r(ctype_digit($geom_id),true).' geom_id='.$geom_id , CLogger::LEVEL_INFO, 'calculateWaterDemand');  // DEBUG
     if ($geom == null) {
         $comuni = Geometry::Get_City_State($this->wr_geometry_id);
     } else {
         $comuni = Geometry::Get_City_State_ByWKT($geom);
     }
     /*
     		if(!ctype_digit($geom_id)){
     			$comuni = Geometry::Get_City_State_ByWKT($geom_id);
     		}else{
     			$comuni = Geometry::Get_City_State($this->wr_geometry_id);
     		}*/
     if (count($comuni)) {
         $city_state = $comuni[0]['nome'];
     } else {
         Yii::log('non trovo il comune. geom_id=' . $geom, CLogger::LEVEL_INFO, 'calculateWaterDemand');
         // DEBUG
         $city_state = 'Pisa';
     }
     $water_supply = WaterSupply::model()->find('lower(city_state)=:city_state', array(':city_state' => strtolower($city_state)));
     if (!$water_supply) {
         Yii::log('Manca la WaterSupply. $city_state=' . $city_state, CLogger::LEVEL_INFO, 'calculateWaterDemand');
         // DEBUG
         return -1;
     }
     $dg = $water_supply->daily_maximum_water_supply;
     $da = $water_supply->yearly_average_water_supply;
     $fformula = str_ireplace(array('ae', 'dg', 'da'), array($this->pe, $dg, $da), $formula->formula);
     //TODO: mettere nel config il valore delle stringe ae, dg e da
     $ret = Math::safe_eval($fformula);
     if (is_numeric($ret)) {
         return $ret;
     }
     return -1;
 }
 /**
  * Popup form to insert a new Geometry with Zone or add a Zone to an existing Geometry
  * @param string $wr_id
  * @param string $type
  * @param string $geom_or_id
  * @param string $zone_id
  * @throws CHttpException
  */
 public function actionPopup($wr_id, $type, $geom_or_id, $zone_id = null)
 {
     $wr_model = WaterRequests::model()->findByPk($wr_id);
     if ($wr_model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     if (!Yii::app()->user->checkAccess('updateWaterRequest', array('waterRequest' => $wr_model))) {
         echo Yii::t('waterrequest', 'You are not authorized to edit');
         Yii::app()->end();
     }
     //Yii::log('wr_id='.$wr_id.' type='.$type.' geom_or_id='.$geom_or_id.' $zone_id='.$zone_id, CLogger::LEVEL_INFO, 'popup');
     if ($type == 'update_geom') {
         $geom_id = $_POST['geom_id'];
         $geom_model = WaterRequestGeometries::model()->findByPk($geom_id);
         if (!$geom_model) {
             throw new CHttpException(404, 'The requested page does not exist.');
         }
         //Yii::app()->clientScript->scriptMap=array('jquery.yiiactiveform.js'=>false,'jquery.uniform.js'=>false,'jquery.js'=>false,);
         $this->renderPartial('/waterRequestGeometries/_popupeditgeom', array('model' => $geom_model), false, true);
         Yii::app()->end();
         return;
     }
     $geom_already_exist = false;
     if ($type == 'zone') {
         $geom_id = $geom_or_id;
         $geom_model = WaterRequestGeometries::model()->findByPk($geom_id);
         if ($geom_model) {
             $geom_already_exist = true;
         }
     }
     if (!$geom_already_exist) {
         // If geometry is outside working area, fail the popup.
         $city_state = Geometry::Get_City_State($geom_or_id);
         if ($city_state == null) {
             Yii::log('Comune non trovato. $g=' . $geom_or_id, CLogger::LEVEL_INFO, 'actionPopup');
             // DEBUG
             $this->renderPartial('_popup_fail');
             Yii::app()->end();
         }
         $water_supply = WaterSupply::model()->find('lower(city_state)=:city_state', array(':city_state' => strtolower($city_state)));
         if (!$water_supply) {
             Yii::log('Disegnato fuori. $city_state=' . $city_state, CLogger::LEVEL_INFO, 'actionPopup');
             // DEBUG
             $this->renderPartial('_popup_fail');
             Yii::app()->end();
         }
         $geom_model = new WaterRequestGeometries();
         $geom_model->wr_id = $wr_id;
     }
     $zone_model = null;
     if ($zone_id) {
         $zone_model = WaterRequestGeometryZones::model()->findByPk($zone_id);
     }
     if (!$zone_model) {
         $zone_model = new WaterRequestGeometryZones();
     }
     //Yii::app()->clientScript->scriptMap=array('jquery.yiiactiveform.js'=>false,'jquery.uniform.js'=>false,'jquery.js'=>false,);
     $this->renderPartial('_popupform', array('model' => $zone_model, 'geom_model' => $geom_model, 'geom_already_exist' => $geom_already_exist), false, true);
     Yii::app()->end();
 }
Exemple #3
0
 public function updateOperativeMargin()
 {
     if ($this->status == WaterRequests::CONFIRMED_STATUS && $this->phase == 2 || $this->status == WaterRequests::COMPLETED_STATUS && $this->phase == 3) {
         //loop into geoms
         foreach ($this->geometries as $geom) {
             $service_area = Geometry::Get_Service_Area_Detailed($geom->id);
             $city_state = Geometry::Get_City_State($geom->id);
             if ($service_area == null) {
                 $operative_margin = OperativeMargin::model()->findAll('lower(area)=:area', array(':area' => strtolower($city_state)));
                 $dummy_operative_margin = DummyOperativeMargin::model()->findAll('lower(area)=:area', array(':area' => strtolower($city_state)));
                 if (count($operative_margin) == 0 || count($dummy_operative_margin) == 0) {
                     Yii::log('Manca il margine operativo del comune. area=' . $city_state, CLogger::LEVEL_INFO, 'updateOperativeMargin');
                     // DEBUG
                     return;
                 }
             } else {
                 $operative_margin = SAOperativeMargin::model()->findAll('lower(area)=:area', array(':area' => strtolower($service_area['area'])));
                 $dummy_operative_margin = DummySAOperativeMargin::model()->findAll('lower(area)=:area', array(':area' => strtolower($service_area['area'])));
                 if (count($operative_margin) == 0 || count($dummy_operative_margin) == 0) {
                     Yii::log('Manca il margine operativo dell\'area di servizio. area=' . $service_area['area'], CLogger::LEVEL_INFO, 'updateOperativeMargin');
                     // DEBUG
                     return;
                 }
             }
             if ($this->phase == 2) {
                 foreach ($dummy_operative_margin as $dop) {
                     $dop->margin = $dop->margin - $geom->geom_water_demand;
                     $dop->save();
                 }
             } else {
                 foreach ($operative_margin as $op) {
                     $op->margin = $op->margin - $geom->geom_water_demand;
                     $op->save();
                     // it has a parent, release the water from the dummy tables
                     if ($this->parent_wr) {
                         $parent = $this->parent_wr;
                         foreach ($parent->geometries as $g) {
                             $sa = Geometry::Get_Service_Area_Detailed($g->id);
                             $cs = Geometry::Get_City_State($g->id);
                             if (strcmp($service_area, $sa) === 0 && strcmp($city_state, $cs) === 0) {
                                 foreach ($dummy_operative_margin as $dop) {
                                     $dop->margin = $dop->margin + $geom->geom_water_demand;
                                     $dop->save();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #4
0
 public static function feasibilityCheck($geom_id, $wd)
 {
     //Yii::log('geom_id= '.print_r($geom_id, true).' wd= '.print_r($wd, true), CLogger::LEVEL_INFO, 'feasibilitycheck()');  // DEBUG
     $ret = array();
     $service_area = Geometry::Get_Service_Area_Detailed($geom_id);
     $city_state = Geometry::Get_City_State($geom_id);
     //Yii::log('city_state= '.print_r($city_state, true).' wd= '.print_r($wd, true), CLogger::LEVEL_INFO, 'feasibilitycheck()');  // DEBUG
     if ($city_state == null || $wd == null) {
         $ret['sarea'] = null;
         $ret['city'] = '--';
         $ret['scenari'] = null;
         $ret['margin'] = -1;
         $ret['maximum_water_supply'] = -1;
         return $ret;
     }
     $ret['city'] = ucwords(strtolower($city_state));
     // uso city_state ma potrei usare service_area->desc_area se Get_Service_Area ritornasse un array
     if (!$service_area) {
         $ret['sarea'] = null;
         $water_supply = DummyOperativeMargin::model()->findAll('lower(area)=:area', array(':area' => strtolower($city_state)), array('limit' => 3));
         if (!$water_supply) {
             Yii::log('Cannot find Operative Margin for $city_state=' . $city_state, CLogger::LEVEL_INFO, 'feasibilityCheck');
             // DEBUG
             $ret['scenari'] = null;
             $ret['margin'] = -1;
             $ret['maximum_water_supply'] = -1;
             return $ret;
         }
     } else {
         $ret['sarea'] = $service_area['desc_area'];
         $water_supply = DummySAOperativeMargin::model()->findAll('lower(area)=:area', array(':area' => strtolower($service_area['area'])), array('limit' => 3));
     }
     $ret['scenari'] = array();
     foreach ($water_supply as $ws) {
         //Yii::log(print_r($ws->attributes, true), CLogger::LEVEL_INFO, 'foreach');  // DEBUG
         $operative_margin = $ws->margin;
         if ($ws->scenario == null) {
             $ret['maximum_water_supply'] = Math::wd_round($operative_margin);
             if ($wd > $operative_margin) {
                 $ret['margin'] = -1;
             } else {
                 if ($wd > $operative_margin - (int) Yii::app()->params['water_demand_range']) {
                     $ret['margin'] = 0;
                 } else {
                     $ret['margin'] = 1;
                 }
             }
         } else {
             $item = array();
             $item['scenario'] = $ws->scenario;
             $item['maximum_water_supply'] = Math::wd_round($operative_margin);
             if ($wd > $operative_margin) {
                 $item['margin'] = -1;
             } else {
                 if ($wd > $operative_margin - (int) Yii::app()->params['water_demand_range']) {
                     $item['margin'] = 0;
                 } else {
                     $item['margin'] = 1;
                 }
             }
             array_push($ret['scenari'], $item);
         }
     }
     return $ret;
 }