guild/frontend/modules/api/models/ReportSearchForm.php

83 lines
2.0 KiB
PHP
Raw Normal View History

2021-07-28 17:08:08 +03:00
<?php
namespace frontend\modules\api\models;
2024-02-01 00:57:42 +03:00
use common\classes\Debug;
2021-07-28 17:08:08 +03:00
use yii\base\Model;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
2021-07-28 17:08:08 +03:00
2023-01-19 16:29:06 +03:00
/** */
2021-07-28 17:08:08 +03:00
class ReportSearchForm extends Model
{
2023-01-19 16:29:06 +03:00
public $user_card_id;
2024-02-01 00:57:42 +03:00
public $user_id;
2021-07-28 17:08:08 +03:00
public $limit;
public $offset;
2023-01-19 16:29:06 +03:00
/** @var string */
public $date;
2021-07-28 17:08:08 +03:00
public $fromDate;
public $toDate;
public function __construct($config = [])
{
$this->limit = 10;
$this->offset = 0;
$this->user_card_id = null;
2024-02-01 00:57:42 +03:00
$this->user_id = null;
2021-07-28 17:08:08 +03:00
2023-01-19 16:29:06 +03:00
$this->toDate = date('Y-m-d');
$this->fromDate = date('Y-m-d');
2022-02-15 12:42:56 +03:00
$this->date = date('Y-m-d');
2021-07-28 17:08:08 +03:00
parent::__construct($config);
}
public function rules(): array
{
return [
2021-12-06 17:07:27 +03:00
[['byDate'], 'safe'],
2023-01-19 16:29:06 +03:00
[['fromDate', 'toDate', 'date'], 'string'],
// [['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'],
2024-02-01 00:57:42 +03:00
[[ 'user_card_id', 'user_id'], 'integer', 'min' => 0],
2021-07-28 17:08:08 +03:00
];
}
/**
* @return array|ActiveRecord
*/
public function byParams(): array | ActiveRecord
2021-07-28 17:08:08 +03:00
{
$queryBuilder = Reports::find();
2021-12-06 17:07:27 +03:00
2023-01-19 16:29:06 +03:00
if ($this->fromDate && $this->toDate) {
2021-12-06 17:07:27 +03:00
$queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]);
}
2024-02-01 00:57:42 +03:00
if (isset($this->user_id)) {
$queryBuilder->andWhere(['reports.user_id' => $this->user_id]);
2021-07-28 17:08:08 +03:00
}
2024-02-01 23:46:13 +03:00
// $queryBuilder->limit($this->limit)
// ->offset($this->offset);
2023-01-19 16:29:06 +03:00
return $queryBuilder->all();
2023-01-19 16:29:06 +03:00
}
2021-07-28 17:08:08 +03:00
2023-01-19 16:29:06 +03:00
public function findByDate()
{
return Reports::find()->with('task')
2024-02-01 00:57:42 +03:00
->where(['reports.user_id' => $this->user_id])
2023-01-19 16:48:49 +03:00
->andWhere(['reports.created_at' => $this->date])
2023-01-19 16:29:06 +03:00
->asArray()->all();
2021-07-28 17:08:08 +03:00
}
2022-12-20 19:24:38 +03:00
2023-01-19 16:29:06 +03:00
public function reportsByDate()
2022-12-20 19:24:38 +03:00
{
2023-01-19 16:29:06 +03:00
return Reports::find()->with('task')
2024-02-01 00:57:42 +03:00
->where(['reports.user_id' => $this->user_id])
2023-01-19 16:48:49 +03:00
->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate])
2023-01-19 16:29:06 +03:00
->asArray()->all();
2022-12-20 19:24:38 +03:00
}
2021-07-28 17:08:08 +03:00
}