report task

This commit is contained in:
andrey 2021-06-07 17:18:49 +03:00
parent 966003adb0
commit 9f1db817a5
12 changed files with 321 additions and 12 deletions

View File

@ -3,6 +3,7 @@
namespace backend\modules\reports\controllers;
use backend\modules\card\models\UserCardSearch;
use common\classes\Debug;
use Yii;
use common\models\Reports;
use backend\modules\reports\models\ReportsSearch;
@ -79,7 +80,8 @@ class ReportsController extends Controller
{
$model = new Reports();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->load(Yii::$app->request->post())) {
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}

View File

@ -0,0 +1,12 @@
<?php
namespace backend\modules\reports\models;
use common\models\ReportsTask;
class ReportsTaskSearch extends ReportsTask
{
}

View File

@ -12,7 +12,21 @@ echo GridView::widget([
['class' => 'yii\grid\SerialColumn'],
'created_at',
'today',
[
'attribute' => 'today',
'format' => 'raw',
'value' => function ($model) {
$text = '';
if ($model->task) {
$i = 1;
foreach ($model->task as $task) {
$text .= "<p>$i. ($task->hours_spent ч.) $task->task</p>";
$i++;
}
}
return $text;
}
],
'difficulties',
'tomorrow',

View File

@ -2,6 +2,8 @@
use kartik\date\DatePicker;
use kartik\datetime\DateTimePicker;
use Symfony\Component\Console\Input\Input;
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -34,7 +36,24 @@ use yii\widgets\ActiveForm;
]).'<br>';
?>
<?= $form->field($model, 'today')->textarea(['maxlength' => true]) ?>
<?= $form->field($model, '_task')->widget(MultipleInput::class, [
'cloneButton' => true,
'max' => 10,
'columns' => [
[
'name' => 'task',
'title' => 'Задача',
],
[
'name' => 'hours_spent',
'title' => 'Кол-во часов',
'options' => [
'type' => 'number',
'style' => 'width:100px'
],
],
],
])->label('Какие задачаи были выполнены:'); ?>
<?= $form->field($model, 'difficulties')->textarea(['maxlength' => true]) ?>

View File

@ -26,7 +26,21 @@ $this->params['breadcrumbs'][] = $this->title;
['class' => 'yii\grid\SerialColumn'],
'created_at',
'today',
[
'attribute' => 'today',
'format' => 'raw',
'value' => function ($model) {
$text = '';
if ($model->task) {
$i = 1;
foreach ($model->task as $task) {
$text .= "<p>$i. ($task->hours_spent ч.) $task->task</p>";
$i++;
}
}
return $text;
}
],
'difficulties',
'tomorrow',
[

View File

@ -29,14 +29,30 @@ $this->params['breadcrumbs'][] = $this->title;
'attributes' => [
// 'id',
'created_at',
'today',
[
'attribute' => 'today',
'format' => 'raw',
'value' => function ($model) {
$text = '';
if ($model->task) {
$i = 1;
foreach ($model->task as $task) {
$text .= "<p>$i. ($task->hours_spent ч.) $task->task</p>";
$i++;
}
}
return $text;
}
],
'difficulties',
'tomorrow',
// 'user_card_id',
[
'format' => 'raw',
'attribute' => 'ФИО',
'value' => function ($data) { return \common\models\Reports::getFio($data); },
'value' => function ($data) {
return \common\models\Reports::getFio($data);
},
],
],
]) ?>

View File

@ -2,6 +2,7 @@
namespace common\models;
use common\classes\Debug;
use Yii;
/**
@ -19,6 +20,8 @@ use Yii;
*/
class Reports extends \yii\db\ActiveRecord
{
public $_task;
/**
* {@inheritdoc}
*/
@ -34,7 +37,8 @@ class Reports extends \yii\db\ActiveRecord
{
return [
[['user_card_id', 'status'], 'integer'],
[['user_card_id', 'created_at', 'today'], 'required'],
[['_task'], 'checkIsArray'],
[['user_card_id', 'created_at'], 'required'],
[['today', 'difficulties', 'tomorrow', 'created_at'], 'string', 'max' => 255],
[['user_card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::className(), 'targetAttribute' => ['user_card_id' => 'id']],
];
@ -56,6 +60,27 @@ class Reports extends \yii\db\ActiveRecord
];
}
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;
$i++;
}
}
}
/**
* @return \yii\db\ActiveQuery
*/
@ -64,6 +89,34 @@ class Reports extends \yii\db\ActiveRecord
return $this->hasOne(UserCard::className(), ['id' => 'user_card_id']);
}
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->status = 1;
$taskModel->created_at = time();
$taskModel->save();
}
}
}
public function checkIsArray()
{
if (!is_array($this->_task)) {
$this->addError('_task', 'X is not array!');
}
}
public static function getFio($data)
{
$user_card = UserCard::findOne(['id' => $data->user_card_id]);

View File

@ -0,0 +1,64 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "reports_task".
*
* @property int $id
* @property int $report_id
* @property string $task
* @property int $created_at
* @property int $status
* @property float $hours_spent
*
* @property Reports $report
*/
class ReportsTask extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'reports_task';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['report_id'], 'required'],
[['report_id', 'created_at', 'status'], 'integer'],
[['hours_spent'], 'number'],
[['task'], 'string'],
[['report_id'], 'exist', 'skipOnError' => true, 'targetClass' => Reports::className(), 'targetAttribute' => ['report_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'report_id' => 'Report ID',
'task' => 'Task',
'created_at' => 'Created At',
'status' => 'Status',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getReport()
{
return $this->hasOne(Reports::className(), ['id' => 'report_id']);
}
}

View File

@ -0,0 +1,72 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%reports_task}}`.
*/
class m210607_102350_create_reports_task_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%reports_task}}', [
'id' => $this->primaryKey(),
'report_id' => $this->integer(11)->notNull(),
'task' => $this->text(),
'hours_spent' => $this->float(6),
'created_at' => $this->integer(11),
'status' => $this->integer(1),
]);
// creates index for column `author_id`
$this->createIndex(
'idx-reports_task-report_id',
'reports_task',
'report_id'
);
// add foreign key for table `user`
$this->addForeignKey(
'fk-reports_task-report_id',
'reports_task',
'report_id',
'reports',
'id',
'CASCADE'
);
$this->alterColumn('reports', 'today', $this->text());
$this->alterColumn('reports', 'difficulties', $this->text());
$this->alterColumn('reports', 'tomorrow', $this->text());
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
// drops foreign key for table `user`
$this->dropForeignKey(
'fk-reports_task-report_id',
'reports_task'
);
// drops index for column `author_id`
$this->dropIndex(
'idx-reports_task-report_id',
'reports_task'
);
$this->alterColumn('reports', 'today', $this->string(255)->notNull());
$this->alterColumn('reports', 'difficulties', $this->string(255));
$this->alterColumn('reports', 'tomorrow', $this->string(255));
$this->dropTable('{{%reports_task}}');
}
}

View File

@ -1,6 +1,7 @@
<?php
use kartik\date\DatePicker;
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -34,7 +35,24 @@ use yii\widgets\ActiveForm;
]).'<br>';
?>
<?= $form->field($model, 'today')->textarea(['maxlength' => true]) ?>
<?= $form->field($model, '_task')->widget(MultipleInput::class, [
'cloneButton' => true,
'max' => 10,
'columns' => [
[
'name' => 'task',
'title' => 'Задача',
],
[
'name' => 'hours_spent',
'title' => 'Кол-во часов',
'options' => [
'type' => 'number',
'style' => 'width:100px'
],
],
],
])->label('Какие задачаи были выполнены:'); ?>
<?= $form->field($model, 'difficulties')->textarea(['maxlength' => true]) ?>

View File

@ -26,10 +26,20 @@ $this->params['breadcrumbs'][] = $this->title;
'created_at',
[
'attribute' => 'today',
'format' => 'raw',
'attribute' => 'Что было сделано сегодня?',
'filter' => Html::activeTextInput($searchModel, 'today', ['class' => 'form-control']),
'value' => function ($data) { return '<div class="custom-text">'.$data->today.'</div>'; },
'label' => 'Задачи',
'value' => function ($model) {
$text = '';
if ($model->task) {
$i = 1;
foreach ($model->task as $task) {
$text .= "<p>$i. ($task->hours_spent ч.) $task->task</p>";
$i++;
}
}
return $text;
}
],
[
'format' => 'raw',

View File

@ -30,7 +30,22 @@ $this->params['breadcrumbs'][] = $this->title;
'attributes' => [
// 'id',
'created_at',
'today',
[
'attribute' => 'today',
'format' => 'raw',
'label' => 'Задачи',
'value' => function ($model) {
$text = '';
if ($model->task) {
$i = 1;
foreach ($model->task as $task) {
$text .= "<p>$i. ($task->hours_spent ч.) $task->task</p>";
$i++;
}
}
return $text;
}
],
'difficulties',
'tomorrow',
// 'user_card_id',