fitness-web/frontend/models/TransferSearch.php

127 lines
3.9 KiB
PHP

<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Transfer;
use yii\base\Object;
use yii\db\Query;
use yii\db\Expression;
use common\models\Account;
use yii\helpers\ArrayHelper;
/**
* 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 $accounts;
public $types;
/**
* @inheritdoc
*/
public function rules()
{
return [
// [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
// [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ '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']],
// [ [ 'types' ], function ($attribute, $params) {
// if (!is_array($this->$attribute)) {
// $this->addError($attribute, 'Invalid array');
// }
// }],
];
}
/**
* 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');
return $dataProvider;
}
$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]);
return $dataProvider;
}
public function totalsTransfers($params){
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
if ( $this->hasErrors() ){
return;
}
$accounts = Account::find()->orderBy("name asc")->all();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
$idUser = Yii::$app->user->id;
$totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
$this->totalsCreatedAt = $totals['created_at'];
$this->totalsPaidAt = $totals['paid_at'];
$this->totalsCreatedAtPaid = $totals['created_at_paid'];
$this->totalsCreatedAtNotPaid = $totals['created_at_not_paid'];
$this->totalsPaidAtNotCreatedAt = $totals['paid_at_not_created_at'];
}
}