add collections to backend

This commit is contained in:
2015-11-04 15:39:02 +01:00
parent 48a00e4bdc
commit 4303409fe2
22 changed files with 212 additions and 266 deletions

View File

@@ -11,6 +11,7 @@ use yii\base\Object;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use common\models\Account;
use common\components\Helper;
/**
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
@@ -29,6 +30,9 @@ class TransferSearch extends Transfer
public $accountTotals;
public $fullTotal;
public $types;
public $totals;
/**
* @inheritdoc
*/
@@ -39,6 +43,7 @@ class TransferSearch extends Transfer
// [[ 'searchObjectName' ], 'string'],
[[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
['types', 'each', 'rule' => ['integer']],
];
}
@@ -66,17 +71,8 @@ class TransferSearch extends Transfer
'query' => $query,
]);
// $selectObjectName = "";
// $selectObjectName .= " case when transfer.type = " .self::TYPE_PRODUCT ." then product.name ";
// $selectObjectName .= " when transfer.type = " .self::TYPE_TICKET ." then ticket_type.name ";
// $selectObjectName .= " when transfer.type = " .self::TYPE_MONEY_MOVEMENT_OUT ." then 'Pénzmozgás' ";
// $selectObjectName .= " else '' ";
// $selectObjectName .= " end as object_name";
$query->addSelect( ['*' ]);
// $query->joinWith('ticket');
// $query->joinWith('ticketType');
// $query->joinWith('product');
$this->load($params);
@@ -92,94 +88,33 @@ class TransferSearch extends Transfer
'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 ."%'"));
$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 search2(){
$query = new Query() ;
$query->select('*');
}
public function totals($params){
$query = new Query();
$result = [ ];
public function totalsTransfers( ){
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
$query->addSelect( [ new Expression( 'transfer.id_account as account'), new Expression( ' COALESCE(sum( ( case when direction = '.Transfer::DIRECTION_OUT.' then -1 else 1 end )* 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;
$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);
}
}