Exemplo n.º 1
0
 /**
  * Add method
  *
  * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
  */
 public function add()
 {
     $this->request->allowMethod(['ajax']);
     $alerta = $this->Alertas->newEntity();
     $codLocal = $this->request->data['codLocal'];
     $equipamento = $this->Alertas->Equipamentos->find()->where(['tombo' => $this->request->data['tomboEquipamento']])->contain(['Locals'])->all()->first();
     $bolsistas = UsersController::getBolistas($codLocal);
     if ($this->request->is('ajax')) {
         date_default_timezone_set("America/Fortaleza");
         $alerta = $this->Alertas->patchEntity($alerta, $this->request->data);
         $alerta->dataAlerta = date('Y-m-d H:i:s');
         if ($this->Alertas->save($alerta)) {
             if (!empty($bolsistas)) {
                 foreach ($bolsistas as $bolsista) {
                     $data = ['alerta' => $alerta, 'nome' => $bolsista->nome, 'email' => $bolsista->email, 'equipamento' => $equipamento, 'data' => $this->request->data];
                     $this->mailer($data, 'alerta', 'SGL - Alerta de Equipamento');
                     $bolsistaAlertas = $this->Alertas->BolsistasAlertas->newEntity();
                     $bolsistaAlertas->alerta_id = $alerta->id;
                     $bolsistaAlertas->matricula_bolsista = $bolsista->matricula;
                     $this->Alertas->BolsistasAlertas->save($bolsistaAlertas);
                 }
             }
             echo 'Cadastrado';
         } else {
             echo 'Erro';
         }
     }
     $this->set(compact('alerta'));
     $this->set('_serialize', ['alerta']);
 }
Exemplo n.º 2
0
 /**
  * Relatorio method
  *
  * @param string|null $codigoLocal Local codigo
  * @return \Cake\Network\Response -> arquivo pdf | redirect
  */
 public function relatorio($codigoLocal = null)
 {
     if ($this->request->is('post')) {
         $local = $this->Locals->find()->where(['codigo' => $codigoLocal])->all()->first();
         if (is_null($local)) {
             throw new \Cake\Datasource\Exception\RecordNotFoundException("Ops! Ambiente não encontrado.", 404);
         }
         date_default_timezone_set("America/Fortaleza");
         $dataInicio = date('Y-m-d H:i:s', strtotime($this->request->data['dataInicio'] . '00:00:00'));
         $dataFim = date('Y-m-d H:i:s', strtotime($this->request->data['dataFim'] . '23:59:59'));
         if ($dataInicio > $dataFim || $dataInicio > date('Y-m-d H:i:s')) {
             $this->Flash->error(__('Período inválido.'));
             return $this->redirect(['action' => 'relatorio', $codigoLocal]);
         }
         $local = $this->Locals->find()->where(['codigo' => $codigoLocal])->first();
         /** Coordenador **/
         $coordenadores = UsersController::getCoordenadores($local->codigo);
         /** Bolsistas **/
         $bolsistas = UsersController::getBolistas($local->codigo);
         $equipamentos = $this->Locals->Equipamentos->find()->where(['codLocal' => $codigoLocal])->order(['Equipamentos.nome' => 'ASC'])->contain(['Locals', 'TipoEquipamentos', 'Users', 'Alertas' => function ($q) {
             return $q->where(['dataAlerta >' => date('Y-m-d H:i:s', strtotime($this->request->data['dataInicio'] . '00:00:00'))])->andWhere(['dataAlerta <' => date('Y-m-d H:i:s', strtotime($this->request->data['dataFim'] . '23:59:59'))]);
         }])->all()->toArray();
         $this->response->header(['Content-type: application/pdf']);
         return PdfsController::relatorioLocal($local, $coordenadores, $bolsistas, $equipamentos, $dataInicio, $dataFim);
     }
 }