MaterielsTable.php 5.22 KB
<?php
namespace App\Model\Table;

use App\Model\Entity\Materiel;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Materiels Model
 *
 * @property \Cake\ORM\Association\BelongsTo $SurCategories
 * @property \Cake\ORM\Association\BelongsTo $Categories
 * @property \Cake\ORM\Association\BelongsTo $SousCategories
 * @property \Cake\ORM\Association\BelongsTo $GroupesThematiques
 * @property \Cake\ORM\Association\BelongsTo $GroupesMetiers
 * @property \Cake\ORM\Association\BelongsTo $Organismes
 * @property \Cake\ORM\Association\BelongsTo $Sites
 * @property \Cake\ORM\Association\HasMany $Documents
 * @property \Cake\ORM\Association\HasMany $Emprunts
 * @property \Cake\ORM\Association\HasMany $Suivis
 */
class MaterielsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('materiels');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('SurCategories', [
            'foreignKey' => 'sur_categorie_id'
        ]);
        $this->belongsTo('Categories', [
            'foreignKey' => 'categorie_id'
        ]);
        $this->belongsTo('SousCategories', [
            'foreignKey' => 'sous_categorie_id'
        ]);
        $this->belongsTo('GroupesThematiques', [
            'foreignKey' => 'groupes_thematique_id'
        ]);
        $this->belongsTo('GroupesMetiers', [
            'foreignKey' => 'groupes_metier_id'
        ]);
        $this->belongsTo('Organismes', [
            'foreignKey' => 'organisme_id'
        ]);
        $this->belongsTo('Sites', [
            'foreignKey' => 'site_id'
        ]);
        $this->hasMany('Documents', [
            'foreignKey' => 'materiel_id'
        ]);
        $this->hasMany('Emprunts', [
            'foreignKey' => 'materiel_id'
        ]);
        $this->hasMany('Suivis', [
            'foreignKey' => 'materiel_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        $validator
            ->allowEmpty('designation');

        $validator
            ->allowEmpty('numero_laboratoire')
            ->add('numero_laboratoire', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

        $validator
            ->allowEmpty('description');

        $validator
            ->boolean('materiel_administratif')
            ->allowEmpty('materiel_administratif');

        $validator
            ->boolean('materiel_technique')
            ->allowEmpty('materiel_technique');

        $validator
            ->allowEmpty('status');

        $validator
            ->date('date_acquisition')
            ->allowEmpty('date_acquisition');

        $validator
            ->allowEmpty('fournisseur');

        $validator
            ->numeric('prix_ht')
            ->allowEmpty('prix_ht');

        $validator
            ->allowEmpty('eotp');

        $validator
            ->allowEmpty('numero_commande');

        $validator
            ->allowEmpty('code_comptable');

        $validator
            ->allowEmpty('numero_serie');

        $validator
            ->allowEmpty('numero_inventaire_organisme');

        $validator
            ->allowEmpty('numero_inventaire_old');

        $validator
            ->date('date_archivage')
            ->allowEmpty('date_archivage');

        $validator
            ->boolean('etiquette')
            ->allowEmpty('etiquette');

        $validator
            ->allowEmpty('lieu_stockage');

        $validator
            ->allowEmpty('lieu_detail');

        $validator
            ->allowEmpty('nom_responsable');

        $validator
            ->allowEmpty('email_responsable');

        $validator
            ->allowEmpty('nom_createur');

        $validator
            ->allowEmpty('nom_modificateur');

        $validator
            ->date('date_reception')
            ->allowEmpty('date_reception');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['numero_laboratoire']));
        $rules->add($rules->existsIn(['sur_categorie_id'], 'SurCategories'));
        $rules->add($rules->existsIn(['categorie_id'], 'Categories'));
        $rules->add($rules->existsIn(['sous_categorie_id'], 'SousCategories'));
        $rules->add($rules->existsIn(['groupes_thematique_id'], 'GroupesThematiques'));
        $rules->add($rules->existsIn(['groupes_metier_id'], 'GroupesMetiers'));
        $rules->add($rules->existsIn(['organisme_id'], 'Organismes'));
        $rules->add($rules->existsIn(['site_id'], 'Sites'));
        return $rules;
    }
}