diff options
Diffstat (limited to 'generator/after-bake/src/Controller/ServicesController.php')
-rw-r--r-- | generator/after-bake/src/Controller/ServicesController.php | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/generator/after-bake/src/Controller/ServicesController.php b/generator/after-bake/src/Controller/ServicesController.php new file mode 100644 index 0000000..a3a034b --- /dev/null +++ b/generator/after-bake/src/Controller/ServicesController.php @@ -0,0 +1,259 @@ +<?php +/** + * Copyright 2016 Ludovic Pouzenc <ludovic@pouzenc.fr> + * Copyright 2016 Nicolas Goaziou <mail@nicolasgoaziou.fr> + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see <http://www.gnu.org/licenses/>. +**/ +namespace App\Controller; + +use App\Controller\AppController; +use Cake\Datasource\ConnectionManager; +use Cake\I18n\Time; +use DateTime; + +/** + * Services Controller + * + * @property \App\Model\Table\ServicesTable $Services + */ +class ServicesController extends AppController +{ + + public function initialize() + { + parent::initialize(); + if ($this->request->action === 'index') { + $this->loadComponent('Paginator', [ 'limit' => 15, 'order' => [ 'Services.id' => 'DESC' ] ]); + $this->loadComponent('Search.Prg'); + } + } + + /** + * Index method + * + * @return void + */ + public function index() + { + $this->paginate = [ + 'contain' => ['Adherents', 'ServiceTypes', 'ServiceStatuts', 'Ippubliques'] + ]; + $query = $this->Services + ->find('search', $this->Services->filterParams($this->request->query)); + $this->set('services', $this->paginate($query)); + $this->set('_serialize', ['services']); + + $this->loadModel('ServiceTypes'); + $this->set('serviceTypes', $this->ServiceTypes->find('list')->toArray()); + $this->loadModel('ServiceStatuts'); + $this->set('serviceStatuts', $this->ServiceStatuts->find('list')->toArray()); + } + + /** + * View method + * + * @param string|null $id Service id. + * @return \Cake\Network\Response|null + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function view($id = null) + { + $service = $this->Services->get($id, [ + 'contain' => ['Adherents', 'ServiceTypes', 'ServiceStatuts', 'Ippubliques', 'Equipements' => ['EquipementModeles','Ipmgmt']] + ]); + $this->set('service', $service); + $this->set('_serialize', ['service']); + } + + /** + * Add method + * + * @return void Redirects on successful add, renders view otherwise. + */ + public function add() + { + $service = $this->Services->newEntity(); + //$service = $this->Services->newEntity(['date_debut' => new DateTime('now')]); + if ($this->request->is('post')) { + //FIXME il y a un pépin avec la saisie des datetime, voir si ya pas un helper / widget qui va mieux + $this->request->data['date_debut'] = new DateTime('now'); + + if ( is_numeric($this->request->data('service_type_id')) ) { + $serviceTypeId = $this->request->data('service_type_id'); + + // Utilisation du prix par défaut (pour le type de service donné) + $this->loadModel('ServiceTypes'); + $serviceType = $this->ServiceTypes->get($serviceTypeId); + if ( $serviceType ) { + $this->request->data['prix_ht'] = $serviceType->prix_base_ht; + } + + // Statut de service "Actif CHD (service_statut_id 4) si service de type Adhésion (service_type_id 1) + // Statut de service "Prévu" (service_statut_id 1) par défaut + $this->request->data['service_statut_id'] = ($serviceTypeId==1)?4:1; + } + + $service = $this->Services->patchEntity($service, $this->request->data); + if ($this->Services->save($service)) { + $this->Flash->success(__('The service has been saved.')); + return $this->redirect(['controller' => 'Adherents', 'action' => 'view', $service->adherent_id]); + } else { + debug($service); + debug($this->request->data); + $this->Flash->error(__('The service could not be saved. Please, try again.')); + } + } + + $defaultAdherent = NULL; + $defaultServiceType = 1; + $defaultServiceStatut = 4; + + if ( is_numeric($this->request->query('a')) ) { + $defaultAdherent = $this->request->query('a'); + // Services non résiliés de l'adhérent passé en argument + $hasOtherServices = $this->Services->find() + ->select('id') + ->where(['adherent_id =' => $defaultAdherent, 'service_statut_id !=' => 6]) + ->first(); + if ( $hasOtherServices ) { + $defaultServiceType = 2; + $defaultServiceStatut = 1; + } + } + + $adherents = $this->Services->Adherents->find('list'); + $serviceTypes = $this->Services->ServiceTypes->find('list'); + $serviceStatuts = $this->Services->ServiceStatuts->find('list'); + $ippubliques = $this->Services->Ippubliques->find('list'); + $this->set(compact('service', 'defaultAdherent', 'adherents', 'defaultServiceType', 'serviceTypes', 'defaultServiceStatut', 'serviceStatuts', 'ippubliques')); + $this->set('_serialize', ['service']); + } + + /** + * Edit method + * + * @param string|null $id Service id. + * @return void Redirects on successful edit, renders view otherwise. + * @throws \Cake\Network\Exception\NotFoundException When record not found. + */ + public function edit($id = null) + { + $service = $this->Services->get($id, [ + 'contain' => ['Adherents', 'ServiceTypes'] + ]); + if ($this->request->is(['patch', 'post', 'put'])) { + //FIXME : forbid change of read-only fields in the form + $service = $this->Services->patchEntity($service, $this->request->data); + if ($this->Services->save($service)) { + $this->Flash->success(__('The service has been saved.')); + return $this->redirect(['controller' => 'Adherents', 'action' => 'view', $service->adherent_id]); + } else { + $this->Flash->error(__('The service could not be saved. Please, try again.')); + } + } + //$adherents = $this->Services->Adherents->find('list'); + //$serviceTypes = $this->Services->ServiceTypes->find('list'); + $serviceStatuts = $this->Services->ServiceStatuts->find('list'); + //$ippubliques = $this->Services->Ippubliques->find('list'); + $this->set(compact('service', 'serviceStatuts')); + $this->set('_serialize', ['service']); + } + + public function activate($id = null) + { + $this->request->allowMethod(['post']); + $this->loadModel('Equipements'); + $service = $this->Services->get($id); + if ( $service ) { + if ( $service->ippublique_id === NULL && $service->service_statut_id === 1 /* Prévu */ ) { + //TODO : quick'n'dirty, à améliorer + $conn = ConnectionManager::get('default'); + $rawq='SELECT FREE_IP(' . $service->id . ')'; + $stmt = $conn->execute($rawq); + $free_ip = current($stmt->fetch()); + // Fin quick'n'dirty + + if ( strlen($free_ip) > 7 ) { + $patch = array( + 'service_statut_id' => 4, /* Actif CHD */ + 'ippublique_id' => $free_ip, + 'date_debut' => new DateTime('now'), + ); + $service = $this->Services->patchEntity($service, $patch); + if ($this->Services->save($service)) { + $this->Flash->success(__('The service has been saved.')); + } else { + $this->Flash->error(__('The service could not be saved. Please, try again.')); + } + } else { + $this->Flash->error(__('Can\'t find a free IP public address. Please make sure that equipements are associated to the service.')); + } + } else { + $this->Flash->error(__('Can\'t activate the service. Bad service state.')); + } + return $this->redirect(['controller' => 'Adherents', 'action' => 'view', $service->adherent_id]); + } + } + + public function migrate($id = null) + { + $this->request->allowMethod(['post']); + $this->loadModel('Equipements'); + $service = $this->Services->get($id, [ + 'contain' => ['Equipements'] + ]); + + //TODO : quick'n'dirty, à améliorer + $conn = ConnectionManager::get('default'); + $rawq='SELECT FREE_IP(' . $service->id . ')'; + $stmt = $conn->execute($rawq); + $free_ip = current($stmt->fetch()); + // Fin quick'n'dirty + + $new_service = $this->Services->newEntity([ + 'adherent_id' => $service->adherent_id, + 'service_type_id' => $service->service_type_id, + 'prix_ht' => $service->prix_ht, + 'description' => $service->description, + 'lat' => $service->lat, + 'lng' => $service->lng, + 'service_statut_id' => 4, + 'ippublique_id' => $free_ip, + 'date_debut' => Time::now(), + ]); + + $service->date_fin = Time::now(); + $service->service_statut_id = 3; + + if ( (strlen($free_ip) > 7) && $this->Services->save($new_service)) { + $fail = false; + foreach ( $service->equipements as $equipement ) { + $equipement->service_id = $new_service->id; + if ( ! $this->Equipements->save($equipement) ) { + $fail = true; + } + } + if ( $fail==false && $this->Services->save($service) ) { + $this->Flash->success(__('The service has been saved.')); + return $this->redirect(['controller' => 'Adherents', 'action' => 'view', $service->adherent_id]); + } + } + $this->Flash->error(__('The service could not be saved. Please, try again.')); + $this->set(compact('service', 'new_service')); + $this->set('_serialize', ['service', 'new_service']); + } +} |