DocumentsController.php 8.62 KB
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use FPDF;

/**
 * Documents Controller
 *
 * @property \App\Model\Table\DocumentsTable $Documents
 * @property \Cake\ORM\Association\BelongsTo $TypeDocuments
 */
class DocumentsController extends AppController
{

	/**
	 * @param $user
	 *
	 * Give authorization for documents
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{
		$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
		$role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
		$action = $this->request->params['action'];
		
		// Super-Admin peut accéder à chaque action
		if($role == 'Super Administrateur') return true;
		
		if (in_array($action, ['admission', 'sortie'])) {
			if ($this->userHasRole('Administration')) {
				return true;
			}
		}

		//Pour tout le monde
		if (in_array($action, ['view', 'add', 'ficheMateriel'])) return true;
	}
	
    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
    	$this->paginate = [
    			'contain' => ['TypeDocuments'],
    	];
        $documents = $this->paginate($this->Documents);

        $materiel = $this->Documents->Materiels;
        
        $this->set(compact('documents', 'materiel'));
        $this->set('_serialize', ['documents']);
    }

    /**
     * View method
     *
     * @param string|null $id Document id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $document = $this->Documents->get($id, [
            'contain' => ['TypeDocuments']
        ]);

        $materiel = $this->Documents->Materiels->find()->where(['id =' => $document->materiel_id])->first();

        if($materiel != null) {
        	$this->set('materiel', $materiel);
        }
        else {
        	$suivi = $this->Documents->Suivis->find()->where(['id =' => $document->suivi_id])->first();
        	$this->set('suivi', $suivi);
        }
       
        $this->set('document', $document);
        $this->set('_serialize', ['document']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $document = $this->Documents->newEntity();
        if ($this->request->is('post')) {
            $document = $this->Documents->patchEntity($document, $this->request->data);

            if ($this->Documents->save($document)) {
                $this->Flash->success(__('Le fichier a bien été ajouté.'));
                
                $id = $document->materiel_id;
                if(empty($id)) {
                	$id = $document->suivi_id;
                	return $this->redirect(['controller' => 'suivis', 'action' => 'view', $id]);
                } else {
                	return $this->redirect(['controller' => 'materiels', 'action' => 'view', $id]);
                }
                
            } else {
                $this->Flash->error(__('Le fichier n\'a pas pu être ajouté.'));
            }
        }
        
        if(isset($this->passedArgs[1]) && $this->passedArgs[1] == 'mat') {
        	$materiel = $this->Documents->Materiels->find('list', [ 'keyField' => 'id', 'valueField' => 'numero_laboratoire'])->where(['id =' => $this->passedArgs[0]]);
        	$this->set('materiel', $materiel);
        }
        else {
        	$suivi = $this->Documents->Suivis->find('list', [ 'keyField' => 'id', 'valueField' => 'id'])->where(['id =' => $this->passedArgs[0]]);
        	$this->set('suivi', $suivi);
        }
         
        
        $typesD = $this->Documents->TypeDocuments->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'TypeDocuments.nom']);
        
        $this->set(compact('document', 'typesD'));
        $this->set('_serialize', ['document']);
    }


    /**
     * Delete method
     *
     * @param string|null $id Document id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $document = $this->Documents->get($id);
        
        if ($this->Documents->delete($document)) {
            $this->Flash->success(__('Le fichier a bien été supprimé.'));
        } else {
            $this->Flash->error(__('Le fichier n\'a pas pu être supprimé.'));
        }
        
        $id = $document->materiel_id;
        if(empty($id)) {
        	$id = $document->suivi_id;
        	return $this->redirect(['controller' => 'suivis', 'action' => 'view', $id]);
        } else {
        	return $this->redirect(['controller' => 'materiels', 'action' => 'view', $id]);
        }
    }
    
    
    public function sortie($labNumber) {   
    	$this->set('fpdf', new FPDF ( 'P', 'mm', 'A4' ));
    }
    
    public function admission($labNumber) {
    	
    	$this->set ( 'fpdf', new FPDF ( 'P', 'mm', 'A4' ) );
    	// Find the concerned materiel
    	$materiel = TableRegistry::get('Materiels')->find('all', ['conditions' => ['numero_laboratoire' => $labNumber]])->first(); // End find
    	
    	// Get the administration user name
    	$userName = $this->LdapAuth->user('username');
    	$numeroLab = $materiel->numero_laboratoire;
    	$dateAcquisition = $materiel->date_acquisition;
    	$dateAcquisition = $dateAcquisition;
    	$numeroCommande = $materiel->numero_commande;
    	$designation = $materiel->designation;
    	if(isset($materiel->organisme_id) && !empty($materiel->organisme_id)) {
    		$organisme = TableRegistry::get('Organismes')->find('all')->where(['id =' => $materiel->organisme_id])->first()->nom;
    	}
    	else {
    		$organisme = "";
    	}

    	$fournisseur = $materiel->fournisseur;
    	$numeroOrganisme = $materiel->numero_inventaire_organisme;
    	$eotp = $materiel->eotp;
    	$prix = $materiel->prix_ht;
    
    	// Build the data array
    	$TDoc = [
    			'organisme' => $organisme,
    			'numlab' => $numeroLab,
    			'designation' => $designation,
    			'dateAcquis' => $dateAcquisition,
    			'numCde' => $numeroCommande,
    			'fournisseur' => $fournisseur,
    			'eotp' => $eotp,
    			'prix' => $prix,
    			'numOrg' => $numeroOrganisme
    	];
    
    	// set the data for the document (accessible par $data dans le document)
    	$this->set ( 'data', $TDoc );
    
    } // End fct admission
    
    public function ficheMateriel($labNumber) {
    	
    	// Find the concerned materiel
    	$materiel = TableRegistry::get('Materiels')->find('all', ['conditions' => ['numero_laboratoire' => $labNumber]])->first(); // End find
    	
    	if(isset($materiel->sur_categorie_id)) {
    		$surCategorie = TableRegistry::get('SurCategories')->find()->where(['id =' => $materiel->sur_categorie_id])->first()->nom;
    	}
    	else {
    		$surCategorie = ' ';
    	}
    	
    	if(isset($materiel->categorie_id)) {
    		$categorie = TableRegistry::get('Categories')->find()->where(['id =' => $materiel->categorie_id])->first()->nom;
    	}else {
    		$categorie = ' ';
    	}
    	
    	if(isset($materiel->sous_categorie_id)) {
    		$sousCategorie = TableRegistry::get('SousCategories')->find()->where(['id =' => $materiel->sous_categorie_id])->first()->nom;
    	}else {
    		$sousCategorie = ' ';
    	}
    	
    	if(isset($materiel->groupes_thematique_id)) {
    		$groupesThematique = TableRegistry::get('GroupesThematiques')->find()->where(['id =' => $materiel->groupes_thematique_id])->first()->nom;
    	}else {
    		$groupesThematique = ' ';
    	}
    	
    	if(isset($materiel->groupes_metier_id)) {
    		$groupesMetier = TableRegistry::get('GroupesMetiers')->find()->where(['id =' => $materiel->groupes_metier_id])->first()->nom;
    	}else {
    		$groupesMetier = ' ';
    	}
    	
    	if(isset($materiel->organisme_id)) {
    		$organisme = TableRegistry::get('Organismes')->find()->where(['id =' => $materiel->organisme_id])->first()->nom;
    	}else {
    		$organisme = ' ';
    	}
    	
    	if(isset($materiel->site_id)) {
    		$site = TableRegistry::get('Sites')->find()->where(['id =' => $materiel->site_id])->first()->nom;
    	}else {
    		$site = ' ';
    	}

    	 
    	
    	// set the data materiel for the document (accessible par $materiel dans le document)
    	$this->set(compact('materiel', 'surCategorie', 'categorie', 'sousCategorie', 'groupesThematique', 'groupesMetier', 'organisme', 'site'));
    	$this->set ( 'fpdf', new FPDF ( 'P', 'mm', 'A4' ) );
    } 
    
}