report by date

This commit is contained in:
andrey 2021-12-06 17:07:27 +03:00
parent 31c35676f3
commit 25377d272d

View File

@ -14,6 +14,10 @@ class ReportSearchForm extends Model
public $fromDate; public $fromDate;
public $toDate; public $toDate;
public $user_id; public $user_id;
/**
* @var false
*/
public $byDate;
public function __construct($config = []) public function __construct($config = [])
{ {
@ -23,6 +27,7 @@ class ReportSearchForm extends Model
$this->toDate = date('Y-m-d', time()); $this->toDate = date('Y-m-d', time());
$this->fromDate = date('Y-m-01', time()); $this->fromDate = date('Y-m-01', time());
$this->byDate = false;
parent::__construct($config); parent::__construct($config);
} }
@ -30,6 +35,7 @@ class ReportSearchForm extends Model
public function rules(): array public function rules(): array
{ {
return [ return [
[['byDate'], 'safe'],
[['fromDate', 'toDate'], 'date', 'format' => 'php:Y-m-d'], [['fromDate', 'toDate'], 'date', 'format' => 'php:Y-m-d'],
[['limit', 'offset', 'user_id'], 'integer', 'min' => 0], [['limit', 'offset', 'user_id'], 'integer', 'min' => 0],
]; ];
@ -38,12 +44,18 @@ class ReportSearchForm extends Model
public function byParams() public function byParams()
{ {
$queryBuilder = Reports::find() $queryBuilder = Reports::find()
->with('task') ->with('task');
->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate])
->limit($this->limit) if ($this->byDate) {
$queryBuilder->andWhere(['reports.created_at' => $this->byDate]);
} else {
$queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]);
}
$queryBuilder->limit($this->limit)
->offset($this->offset); ->offset($this->offset);
if(isset($this->user_id)) { if (isset($this->user_id)) {
$queryBuilder->andWhere(['user_card_id' => $this->user_id]); $queryBuilder->andWhere(['user_card_id' => $this->user_id]);
} }