129 lines
3.4 KiB
PHP
129 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace backend\models;
|
|
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use yii\data\ActiveDataProvider;
|
|
use common\models\Transfer;
|
|
use yii\db\Expression;
|
|
use yii\base\Object;
|
|
use yii\db\Query;
|
|
use yii\helpers\ArrayHelper;
|
|
use common\models\Account;
|
|
use common\components\Helper;
|
|
use common\components\RoleDefinition;
|
|
|
|
/**
|
|
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
|
|
*/
|
|
class TransferSearch extends Transfer
|
|
{
|
|
public $searchObjectName;
|
|
public $searchTypeName;
|
|
public $searchUserName;
|
|
public $start;
|
|
public $end;
|
|
|
|
public $timestampStart;
|
|
public $timestampEnd;
|
|
|
|
public $accountTotals;
|
|
public $fullTotal;
|
|
|
|
public $types;
|
|
public $totals;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[[ 'id_account','id_user', 'type'], 'integer'],
|
|
// [[ 'searchObjectName' ], 'string'],
|
|
[[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
|
|
[[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
|
|
['types', 'each', 'rule' => ['integer']],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function scenarios()
|
|
{
|
|
// bypass scenarios() implementation in the parent class
|
|
return Model::scenarios();
|
|
}
|
|
|
|
/**
|
|
* Creates data provider instance with search query applied
|
|
*
|
|
* @param array $params
|
|
*
|
|
* @return ActiveDataProvider
|
|
*/
|
|
public function search($params)
|
|
{
|
|
$query = Transfer::find();
|
|
|
|
|
|
if ( !RoleDefinition::isAdmin() ){
|
|
$query->innerJoin("user_account_assignment",'transfer.id_account = user_account_assignment.id_account' );
|
|
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id ]);
|
|
}
|
|
|
|
$dataProvider = new ActiveDataProvider([
|
|
'query' => $query,
|
|
]);
|
|
|
|
|
|
$query->addSelect( ['*' ]);
|
|
|
|
$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([
|
|
'transfer.id_account' => $this->id_account,
|
|
'transfer.type' => $this->type,
|
|
'transfer.id_user' => $this->id_user,
|
|
]);
|
|
|
|
$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( ){
|
|
$accountTotals = [];
|
|
$fullTotal = [
|
|
'label' => Yii::t('common/transfer', 'Total'),
|
|
'money' => 0
|
|
];
|
|
|
|
|
|
$accounts = Account::read();
|
|
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
|
|
$idUser = $this->id_user;
|
|
|
|
|
|
$this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|