175 lines
5.0 KiB
PHP
175 lines
5.0 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;
|
|
|
|
/**
|
|
* 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;
|
|
|
|
/**
|
|
* @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' ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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();
|
|
|
|
$dataProvider = new ActiveDataProvider([
|
|
'query' => $query,
|
|
]);
|
|
|
|
// $query->distinct();
|
|
$selectObjectName = "";
|
|
$selectObjectName .= " case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end as object_name";
|
|
|
|
$query->addSelect( ['*', new Expression($selectObjectName) ]);
|
|
$query->joinWith('ticket');
|
|
$query->joinWith('ticketType');
|
|
$query->joinWith('product');
|
|
|
|
$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,
|
|
]);
|
|
|
|
$query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] );
|
|
$query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] );
|
|
|
|
if ( isset($this->searchObjectName))
|
|
$query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'"));
|
|
|
|
|
|
|
|
return $dataProvider;
|
|
}
|
|
|
|
public function totals($params){
|
|
$query = new Query();
|
|
|
|
$result = [ ];
|
|
|
|
$accountTotals = [];
|
|
$fullTotal = [
|
|
'label' => Yii::t('common/transfer', 'Total'),
|
|
'money' => 0
|
|
];
|
|
|
|
$query->addSelect( [ new Expression( 'transfer.id_account as account'), new Expression( ' COALESCE(sum( transfer.money ),0) as money' )]);
|
|
$query->from('transfer');
|
|
$query->leftJoin('ticket', 'transfer.type = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' );
|
|
$query->leftJoin('ticket_type', 'ticket.id_ticket_type = ticket_type.id_ticket_type ' );
|
|
$query->leftJoin('product', 'product.id_product = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' );
|
|
|
|
$this->load($params);
|
|
|
|
if (!$this->validate()) {
|
|
return ;
|
|
}
|
|
|
|
$query->andFilterWhere([
|
|
'transfer.id_account' => $this->id_account,
|
|
'transfer.type' => $this->type,
|
|
'transfer.id_user' => $this->id_user,
|
|
]);
|
|
|
|
$query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] );
|
|
$query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] );
|
|
|
|
|
|
if ( isset($this->searchObjectName) && !empty($this->searchObjectName)){
|
|
$query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'"));
|
|
}
|
|
|
|
$query->groupBy('transfer.id_account');
|
|
|
|
$command = $query->createCommand();
|
|
// $command->sql returns the actual SQL
|
|
$result = $command->queryAll();
|
|
|
|
|
|
$accounts = Account::find()->orderBy("name asc")->all();
|
|
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
|
|
|
|
|
|
|
|
foreach ($result as $item){
|
|
$account = "";
|
|
if ( array_key_exists($item['account'], $accountMap)){
|
|
$account = $accountMap[$item['account']];
|
|
}
|
|
|
|
$accountTotal = [
|
|
'label' => $account,
|
|
'money' => $item['money']
|
|
];
|
|
|
|
$accountTotals[] = $accountTotal;
|
|
|
|
$fullTotal['money'] += $item['money'];
|
|
}
|
|
|
|
$this->accountTotals = $accountTotals;
|
|
$this->fullTotal = $fullTotal;
|
|
|
|
// return $result;
|
|
}
|
|
|
|
}
|