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

76 lines
1.9 KiB
PHP
Raw Normal View History

2021-07-28 17:08:08 +03:00
<?php
namespace frontend\modules\api\models;
use common\models\Reports;
use yii\base\Model;
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;
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;
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'],
[[ 'user_card_id'], 'integer', 'min' => 0],
2021-07-28 17:08:08 +03:00
];
}
public function byParams()
{
2023-01-19 16:29:06 +03:00
$queryBuilder = Reports::find()->with('task');
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]);
}
if (isset($this->user_card_id)) {
2023-01-19 16:29:06 +03:00
$queryBuilder->andWhere(['reports.user_card_id' => $this->user_card_id]);
2021-07-28 17:08:08 +03:00
}
2023-01-19 16:29:06 +03:00
$queryBuilder->limit($this->limit)
->offset($this->offset);
return $queryBuilder->asArray()->all();
}
2021-07-28 17:08:08 +03:00
2023-01-19 16:29:06 +03:00
public function findByDate()
{
return Reports::find()->with('task')
->where(['reports.user_card_id' => $this->user_card_id])
2023-01-19 16:45:21 +03:00
->andWhere(['reports.created_at', 'reports.created_at', $this->fromDate, $this->toDate])
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')
2023-01-19 16:35:26 +03:00
->where(['reports.user_card_id' => $this->user_card_id])
2023-01-19 16:45:21 +03:00
->andWhere(['reports.created_at' => $this->date])
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
}