<?php
namespace App\Model\Table;

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


class EstimatesTable extends Table{
    
    public function initialize(array $config) {
        parent::initialize($config);
        
        $this->table('invoices');
        
        $this->belongsTo('Clients', [
            'foreignKey' => 'client_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Projects', [
            'foreignKey' => 'project_id',
            'joinType' => 'INNER'
        ]);
        
    }
    
    
    
    public function validationDefault(Validator $validator)
    {
       
         $validator
             ->integer('client_id')
             ->notEmpty('client_id');
         
         $validator
             ->requirePresence('project_id')  
             ->integer('project_id')
             ->notEmpty('project_id');
         
         $validator
             ->requirePresence('contact_id') 
             ->integer('contact_id')
             ->notEmpty('contact_id');
         
         $validator
             ->notEmpty('issuedate','Issue date cannot be left empty');
         
         $validator
             ->notEmpty('duedate','Due date cannot be left empty');
         
         $validator
            ->notEmpty('currency');
             
         
         $validator
             ->notEmpty('tax')
             ->numeric('tax','It must be numeric'); 

          $validator
                ->numeric('secondtax','It must be numeric')
                ->notEmpty('secondtax');
          
          $validator
                ->numeric('discount','It must be numeric')
                ->notEmpty('discount');
       
          $validator
             ->allowEmpty('terms');
       
        return $validator;
    }
    
}

