fitness-web/frontend/models/TransferSearch.php
2021-09-27 20:43:58 +02:00

105 lines
3.0 KiB
PHP

<?php
namespace frontend\models;
use Yii;
use yii\data\ActiveDataProvider;
use common\models\Transfer;
use common\models\Account;
use common\components\Helper;
/**
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
*/
class TransferSearch extends Transfer
{
public $start;
public $end;
public $timestampStart;
public $timestampEnd;
// public $totalsCreatedAt = ['total' => 0, 'accounts' =>[] ];
// public $totalsCreatedAtNotPaid= ['total' => 0, 'accounts' =>[]];
// public $totalsCreatedAtPaid= ['total' => 0, 'accounts' =>[]];
// public $totalsPaidAt= ['total' => 0, 'accounts' =>[]];
// public $totalsPaidAtNotCreatedAt= ['total' => 0, 'accounts' =>[]];
public $totals;
public $accounts;
public $types;
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ 'start', ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[ [ 'id_account' ] , 'integer'],
['types', 'each', 'rule' => ['integer']],
];
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Transfer::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
$query->where('0=1');
}
$query->andWhere( ['or',['id_user' => Yii::$app->user->id ] , ['paid_by' => Yii::$app->user->id],] );
$query->andFilterWhere([
'id_account' => $this->id_account,
'type' => $this->type,
]);
$query->andFilterWhere(['in' ,'type', $this->types]);
$created_condition = ['and',[ '>=', 'transfer.created_at', $this->timestampStart ] ,[ '<', 'transfer.created_at', $this->timestampEnd ] ];
$paid_condition = ['and',[ '>=', 'transfer.paid_at', $this->timestampStart ] ,[ '<', 'transfer.paid_at', $this->timestampEnd ] ];
$query->andFilterWhere(['or' , $created_condition , $paid_condition]);
Helper::restrictIfNotAdminTheStartDate($query, $this->timestampStart);
return $dataProvider;
}
/**
* @throws \yii\db\Exception
*/
public function totalsTransfers(){
$accounts = Account::find()->orderBy("name asc")->all();
$idUser = Yii::$app->user->id;
$this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts);
}
}