<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Departments Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Schooltypes
 * @property \Cake\ORM\Association\BelongsTo $Supervisings
 *
 * @method \App\Model\Entity\Department get($primaryKey, $options = [])
 * @method \App\Model\Entity\Department newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Department[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Department|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Department patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Department[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Department findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class DepartmentsTable extends Table
{

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

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

        $this->addBehavior('Timestamp');

        $this->belongsTo('Schooltypes', [
            'foreignKey' => 'schooltype_id'
        ]);
        $this->belongsTo('Supervisings', [
            'foreignKey' => 'supervising_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
            ->requirePresence('title', 'create')
            ->notEmpty('title');

        $validator
            ->notEmpty('status');

        $validator
            ->requirePresence('code', 'create')
            ->notEmpty('code');

        $validator
            ->allowEmpty('super_school_code');

        $validator
            ->integer('count')
            ->allowEmpty('count');

        $validator
            ->dateTime('delivery_date')
            ->allowEmpty('delivery_date');

        $validator
            ->integer('requested')
            ->allowEmpty('requested');

        $validator
            ->integer('delivered')
            ->allowEmpty('delivered');

        $validator
            ->integer('checkedin')
            ->allowEmpty('checkedin');

        $validator
            ->integer('delivery_count')
            ->allowEmpty('delivery_count');

        $validator
            ->allowEmpty('data');

        $validator
            ->integer('fmismatch')
            ->allowEmpty('fmismatch');

        $validator
            ->integer('pmismatch')
            ->allowEmpty('pmismatch');

        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->existsIn(['schooltype_id'], 'Schooltypes'));
        $rules->add($rules->existsIn(['supervising_id'], 'Supervisings'));

        return $rules;
    }
}
