Vous êtes sur la page 1sur 3

<?

php

Yii::import('application.models._base.BaseTask');

class Task extends BaseTask


{
public $date_initial;
public $date_final;

public function rules()


{
$arrayrules = parent::rules();
return array_merge([
['authorized','filter','filter'=>'intval'],
['is_warranty','filter','filter'=>'intval'],
['is_mainstay','filter','filter'=>'intval'],
],$arrayrules, [
array('date_initial, date_final', 'safe'),
]);
}

public static function representingColumn()


{
return 'task';
}

public static function model($className=__CLASS__)


{
return parent::model($className);
}

public function getNewPrice()


{
return ($this->is_warranty==1?0:1) * $this->time_estimated * $this->urgency
* $this->project->customer->{$this->type};
}

public function getDecimal_time_estimated()


{
return $this->find([
'select' => 'TIME_TO_SEC(time_estimated) / 60 / 60 as time_estimated',
'condition' => 'id = :id',
'params' => [':id' => $this->id],
])->time_estimated;
}

public function getCompletedTasks()


{
$criteria = new CDbCriteria;
$criteria->addCondition('delivered_date BETWEEN :date_initial AND
:date_final');
$criteria->params = [
':date_initial' => $this->date_initial,
':date_final' => $this->date_final,
];
if (!empty($this->project_id)) {
$criteria->addCondition('project_id = :project_id');
$criteria->params = array_merge($criteria->params, [
':project_id' => $this->project_id,
]);
}

return new CActiveDataProvider($this, array(


'criteria' => $criteria,
));
}

public function getCompletedTasksEstimatedTimeSum()


{
$estimatedTimeSum = new DateTime('2017-01-01 00:00:00');
$completedTasks = $this->getCompletedTasks()->getData();
foreach ($completedTasks as $completedTask) {
$time_estimated = new DateTime($completedTask->time_estimated);
$estimatedTimeSum = $estimatedTimeSum->modify('+'.$time_estimated-
>format('H').' hour');
$estimatedTimeSum = $estimatedTimeSum->modify('+'.$time_estimated-
>format('i').' minute');
$estimatedTimeSum = $estimatedTimeSum->modify('+'.$time_estimated-
>format('s').' second');
}

$hours = $estimatedTimeSum->format('d') * 24 -24 ;


$hours = $hours + $estimatedTimeSum->format('H');

return $hours.':'.$estimatedTimeSum->format('i:s');
}

public function getCompletedTasksPriceSum()


{
$completedTasks = $this->getCompletedTasks()->getData();
$sumPrice = 0;
foreach ($completedTasks as $completedTask) {
$sumPrice = $sumPrice + $completedTask->price;
}

return $sumPrice;
}

public function generateSchedule()


{
$this->time_assigned = null;
$this->save();

$schedule = new Schedule();


$schedule->task_id = $this->id;
$schedule->removeTaskFromSchedule();

$model = new Schedule;


$model->user_id = $this->user_agent_id;
$model->task_id = $this->id;
$model->assignTask();

// Free memory
unset($schedule);
unset($model);
}

public function beforeSave()


{
// Hours control
$type = $this->type;
$time_estimated = new DateTime($this->time_estimated);
$hours = $time_estimated->format('H');
$minutes = $time_estimated->format('i');
$seconds = $time_estimated->format('s');
$time_estimated = $hours + $minutes/60 + $seconds/60/60;
$this->price = $this->project->customer->$type * $time_estimated * $this-
>urgency;

return true;
}
}