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

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

/**
 * Documents Controller
 *
 * @property \App\Model\Table\DocumentsTable $Documents
 */
class DocumentsController extends AppController
{

	/**
	 * @param $user
	 *
	 * Give authorization for documents
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{

		if (in_array($action, ['admission', 'sortie'])) {
			if (in_array($role, ['Administration', 'Administration Plus', 'Super Administrateur'])) {
				return true;
			}
		}
	
		return parent::isAuthorized($user);
	}
	
    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Materiels', 'Suivis']
        ];
        $documents = $this->paginate($this->Documents);

        $this->set(compact('documents'));
        $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' => ['Materiels', 'Suivis']
        ]);

        $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é.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('Le fichier n\'a pas pu être ajouté.'));
            }
        }
        $materiels = $this->Documents->Materiels->find('list', ['limit' => 200]);
        $suivis = $this->Documents->Suivis->find('list', ['limit' => 200]);
        $this->set(compact('document', 'materiels', 'suivis'));
        $this->set('_serialize', ['document']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Document id.
     * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $document = $this->Documents->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
            if ($this->Documents->save($document)) {
                $this->Flash->success(__('Le fichier a bien été édité.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('Le fichier n\'a pas pu être édité.'));
            }
        }
        $materiels = $this->Documents->Materiels->find('list', ['limit' => 200]);
        $suivis = $this->Documents->Suivis->find('list', ['limit' => 200]);
        $this->set(compact('document', 'materiels', 'suivis'));
        $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é.'));
        }
        return $this->redirect(['action' => 'index']);
    }
    
    
    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->Auth->user('username');
    	$numeroLab = $materiel->numero_laboratoire;
    	$dateAcquisition = $materiel->date_acquisition;
    	// convert to French format dd-mm-yyyy :
    	$dateAcquisition = date ( "d-m-Y", strtotime ( $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
    
    
}