guild/common/models/Reports.php

157 lines
4.5 KiB
PHP
Raw Normal View History

2020-02-05 12:08:01 +03:00
<?php
namespace common\models;
2021-06-07 17:18:49 +03:00
use common\classes\Debug;
2020-02-05 12:08:01 +03:00
use Yii;
/**
* This is the model class for table "reports".
*
* @property int $id
* @property string $created_at
* @property string $today
* @property string $difficulties
* @property string $tomorrow
* @property int $user_card_id
2024-02-01 00:57:42 +03:00
* @property int $user_id
* @property int $project_id
* @property int $company_id
2020-02-05 12:08:01 +03:00
* @property int $status
*
* @property UserCard $userCard
*/
class Reports extends \yii\db\ActiveRecord
{
2021-06-07 17:18:49 +03:00
public $_task;
2020-02-05 12:08:01 +03:00
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'reports';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2024-02-01 00:57:42 +03:00
[['user_card_id', 'user_id', 'status', 'company_id', 'project_id'], 'integer'],
2021-06-07 17:18:49 +03:00
[['_task'], 'checkIsArray'],
2024-02-01 00:57:42 +03:00
[['created_at'], 'required'],
2020-02-05 12:08:01 +03:00
[['today', 'difficulties', 'tomorrow', 'created_at'], 'string', 'max' => 255],
[['user_card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::class, 'targetAttribute' => ['user_card_id' => 'id']],
2024-02-01 00:57:42 +03:00
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
[['project_id'], 'exist', 'skipOnEmpty' => true, 'targetClass' => Project::class, 'targetAttribute' => ['project_id' => 'id']],
[['company_id'], 'exist', 'skipOnEmpty' => true, 'targetClass' => Company::class, 'targetAttribute' => ['company_id' => 'id']],
2020-02-05 12:08:01 +03:00
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'created_at' => 'Дата заполнения отчета',
'today' => 'Что было сделано сегодня?',
'difficulties' => 'Какие сложности возникли?',
'tomorrow' => 'Что планируется сделать завтра?',
2024-02-01 00:57:42 +03:00
'user_card_id' => 'Профиль пользователя',
'user_id' => 'Пользователь',
'status' => 'Статус',
'project_id' => 'ID проекта',
'company_id' => 'ID компании'
2020-02-05 12:08:01 +03:00
];
}
2021-06-07 17:18:49 +03:00
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
$this->saveTask();
}
public function afterFind()
{
parent::afterFind();
$this->_task = [];
if ($this->task) {
$i = 0;
foreach ($this->task as $task) {
$this->_task[$i]['task'] = $task->task;
$this->_task[$i]['hours_spent'] = $task->hours_spent;
$this->_task[$i]['minutes_spent'] = $task->minutes_spent;
2021-06-07 17:18:49 +03:00
$i++;
}
}
}
2020-02-05 12:08:01 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getUserCard()
{
return $this->hasOne(UserCard::className(), ['id' => 'user_card_id']);
}
2024-02-01 00:57:42 +03:00
/**
* @return \yii\db\ActiveQuery
*/
private function getUser(): \yii\db\ActiveQuery
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
2021-06-07 17:18:49 +03:00
public function getTask()
{
return $this->hasMany(ReportsTask::class, ['report_id' => 'id']);
}
public function saveTask()
{
ReportsTask::deleteAll(['report_id' => $this->id]);
if ($this->_task) {
foreach ($this->_task as $task) {
$taskModel = new ReportsTask();
$taskModel->report_id = $this->id;
$taskModel->task = $task['task'];
$taskModel->hours_spent = (float)$task['hours_spent'];
$taskModel->minutes_spent = (int) $task['minutes_spent'];
2021-06-07 17:18:49 +03:00
$taskModel->status = 1;
$taskModel->created_at = time();
$taskModel->save();
}
}
}
public function checkIsArray()
{
if (!is_array($this->_task)) {
$this->addError('_task', 'X is not array!');
}
}
2020-02-05 12:08:01 +03:00
public static function getFio($data)
{
$user_card = UserCard::findOne(['id' => $data->user_card_id]);
return $user_card->fio;
}
2022-12-20 19:24:38 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getReportsTask()
{
return $this->hasMany(ReportsTask::className(), ['report_id' => 'id']);
}
public function calculateOrderTime()
{
return ReportsTask::find()->where(['report_id' => $this->id])->sum('hours_spent');
}
2020-02-05 12:08:01 +03:00
}