2021-07-28 17:08:08 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace frontend\modules\api\models;
|
|
|
|
|
|
|
|
use common\models\Reports;
|
2022-12-20 19:24:38 +03:00
|
|
|
use common\models\ReportsTask;
|
2021-07-28 17:08:08 +03:00
|
|
|
use yii\base\Model;
|
|
|
|
|
|
|
|
class ReportSearchForm extends Model
|
|
|
|
{
|
|
|
|
public $limit;
|
|
|
|
public $offset;
|
|
|
|
public $fromDate;
|
|
|
|
public $toDate;
|
|
|
|
public $user_id;
|
2021-12-06 17:07:27 +03:00
|
|
|
/**
|
|
|
|
* @var false
|
|
|
|
*/
|
|
|
|
public $byDate;
|
2022-02-15 12:42:56 +03:00
|
|
|
public $date;
|
2021-07-28 17:08:08 +03:00
|
|
|
|
|
|
|
public function __construct($config = [])
|
|
|
|
{
|
|
|
|
$this->limit = 10;
|
|
|
|
$this->offset = 0;
|
|
|
|
$this->user_id = null;
|
|
|
|
|
|
|
|
$this->toDate = date('Y-m-d', time());
|
|
|
|
$this->fromDate = date('Y-m-01', time());
|
2022-02-15 12:42:56 +03:00
|
|
|
$this->date = date('Y-m-d');
|
2021-12-06 17:07:27 +03:00
|
|
|
$this->byDate = false;
|
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'],
|
2022-02-15 12:42:56 +03:00
|
|
|
[['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'],
|
2021-12-06 17:07:27 +03:00
|
|
|
[['limit', 'offset', 'user_id'], 'integer', 'min' => 0],
|
2021-07-28 17:08:08 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function byParams()
|
|
|
|
{
|
|
|
|
$queryBuilder = Reports::find()
|
2021-12-06 17:07:27 +03:00
|
|
|
->with('task');
|
|
|
|
|
|
|
|
if ($this->byDate) {
|
2022-02-15 12:42:56 +03:00
|
|
|
$queryBuilder->andWhere(['reports.created_at' => $this->date]);
|
2021-12-06 17:07:27 +03:00
|
|
|
} else {
|
|
|
|
$queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$queryBuilder->limit($this->limit)
|
2021-07-28 17:08:08 +03:00
|
|
|
->offset($this->offset);
|
|
|
|
|
2021-12-06 17:07:27 +03:00
|
|
|
if (isset($this->user_id)) {
|
2021-09-14 12:21:12 +03:00
|
|
|
$queryBuilder->andWhere(['user_card_id' => $this->user_id]);
|
2021-07-28 17:08:08 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 15:52:25 +03:00
|
|
|
$data = $queryBuilder->asArray()->all();
|
2021-07-28 17:08:08 +03:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2022-12-20 19:24:38 +03:00
|
|
|
|
|
|
|
public function findByDate(): array
|
|
|
|
{
|
|
|
|
return Reports::find()
|
|
|
|
->where(['between', 'reports.created_at', $this->fromDate, $this->toDate])
|
2023-01-10 17:45:53 +03:00
|
|
|
->andWhere(['user_card_id' => $this->user_id])
|
2022-12-20 19:24:38 +03:00
|
|
|
->all();
|
|
|
|
}
|
2021-07-28 17:08:08 +03:00
|
|
|
}
|