Emprunt.php 5.9 KB
<?php
namespace App\Model\Entity;


use Cake\ORM\Entity;
use App\Controller\EmpruntsController;
use Cake\I18n\FrozenTime;
use Cake\I18n\FrozenDate;

const DEBUG = false;


/**
 * Emprunt Entity.
 *
 * @property int $id
 * @property int $materiel_id
 * @property \App\Model\Entity\Materiel $materiel
 * @property \Cake\I18n\Time $date_emprunt
 * @property \Cake\I18n\Time $date_retour_emprunt
 * @property bool $emprunt_interne
 * @property string $laboratoire
 * @property int $site_id
 * @property string $e_lieu_detail
 * @property string $nom_emprunteur
 * @property string $email_emprunteur
 * @property string $tel
 * @property string $commentaire
 * @property string $nom_createur
 * @property string $nom_modificateur
 * @property \Cake\I18n\Time $created
 * @property \Cake\I18n\Time $modified
 */
class Emprunt extends Entity {

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
        '*' => true,
        'id' => false
    ];
    
    // Instance du controleur EmpruntsController pour messages de debug
    private $mycontroller = null;
    
    
    private function d($arg) {
        if (! $this->mycontroller) $this->mycontroller = new EmpruntsController();
        $this->mycontroller->myDebug($arg);
    }
    
    // Si on veut que ce champ virtuel soit exporté systématiquement dans l'entité
    //protected $_virtual = ['status_from_dates'];
    
    // Ajoute le champ VIRTUEL (calculé) "status_from_dates" à l'entité Emprunt
    protected function _getStatusFromDates() {
        //return $this->_properties['lieu'] . '  ' . $this->_properties['last_name'];
        //return "un";

        //$today = new FrozenTime('2015-06-15 08:23:45');
        
        /* 
         * now
         */
        $today = new FrozenTime('now'); // 'time' => '2020-07-03T16:50:59+02:00',
        //$today = new FrozenDate('now'); // 'time' => '2020-07-03T00:00:00+00:00',
        $this->d('*** today:');
        /*
         * Type Cakephp FrozenTime (Paris)
         * 
        object(Cake\I18n\FrozenTime) {
            'time' => '2020-07-03T16:50:59+02:00',
            'timezone' => 'Europe/Paris',
            'fixedNowTime' => false
        }
        */
        //$today = new \DateTime('now'); // '2020-07-03 16:50:24.244057'
        //$today->setTimezone('Europe/Paris');
        $this->d($today);
        $this->d($today->format('d/m/y'));
        $this->d($today->format('d/m/yy'));
        $this->d($today->format('yy/m/d'));
        $this->d($today->format('y/m/d'));
        
        /* 
         * date_emprunt
         */
        $this->d('*** date_emprunt:');
        /*
         * Type Cakephp FrozenDate (UTC)
         * 
         object(Cake\I18n\FrozenDate) {
            'time' => '2020-04-17T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false
         }
         */
        $this->d($this->date_emprunt); 
        //echo($this->date_emprunt); // '17/04/2020'
        $this->d($this->date_emprunt->__toString()); // '17/04/2020'
        //$date_emprunt = new \DateTime(strtr($this->_properties['date_emprunt'],'/','-')) ; //->format('d/m/y');
        //$date_emprunt = new \DateTime(str($this->date_emprunt)) ; //->format('d/m/y');
        /*
         * Type DateTime (Paris)
         * 
        object(DateTime) {
        	date => '2020-04-17 00:00:00.000000'
        	timezone_type => (int) 3
        	timezone => 'Europe/Paris'
        }
         */
        //$date_emprunt = new \DateTime($this->date_emprunt->toFormattedDateString()) ; //->format('d/m/y');
        $date_emprunt = $this->date_emprunt;
        //$date_emprunt = new \DateTime(strtr($this->date_emprunt,'/','-')) ; //->format('d/m/y');
        //$this->d($date_emprunt);
        $this->d($date_emprunt->format('d/m/y'));
        $this->d($date_emprunt->format('d/m/yy'));
        $this->d($date_emprunt->format('yy/m/d'));
        $this->d($date_emprunt->format('y/m/d'));
        
        /*
         * date_retour_emprunt
         * 
         */
        //$date_retour = new \DateTime(strtr($this->_properties['date_retour_emprunt'],'/','-'));
        $this->d('*** date_retour_emprunt:');
        $this->d($this->date_retour_emprunt);
        //$date_retour = new \DateTime(strtr($this->date_retour_emprunt,'/','-'));
        $date_retour = $this->date_retour_emprunt;
        //$this->d($date_retour);
        $this->d($date_retour->format('d/m/y'));
        $this->d($date_retour->format('d/m/yy'));
        $this->d($date_retour->format('yy/m/d'));
        $this->d($date_retour->format('y/m/d'));
        // On ajoute 23h59m59s à date_retour pour autoriser le retour jqa la fin de la journée
        $this->d('*** date_retour_emprunt + 23h59m59s:');
        //$date_retour->add(new \DateInterval('PT23H59M59S')); //->format('d/m/y');
        $date_retour = new FrozenTime($date_retour);
        $date_retour = $date_retour->modify('+23hour +59min +59sec');
        $this->d($date_retour);
        $this->d($date_retour->format('d/m/yy'));

        /*
         * delay : today - date_emprunt
         *
         */
        $delay = $today->diff($date_emprunt)->days;
        $this->d("*** delay1 $delay");
        if ($today < $date_emprunt) {
            $status = "A VENIR";
            $delay = "dans " . ($delay==0?1:$delay);
        }
        else {
            if ($today <= $date_retour) {
                $status = "EN COURS";
            }
            else {
                $status = "TERMINÉ";
                $delay = $today->diff($date_retour)->days;
                $this->d("*** delay2 $delay");
            }
            $delay = "depuis " . ($delay==0?1:$delay);
        }
        return [$status, $delay];
        
    }
    
}