fitness-web/frontend/models/TransferSearch.php
2015-11-04 15:39:02 +01:00

115 lines
3.2 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 $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(['id_user' => 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]);
return $dataProvider;
}
public function totalsTransfers($params){
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
$accounts = Account::find()->orderBy("name asc")->all();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
$idUser = Yii::$app->user->id;
$this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
}
}