add collection changes

This commit is contained in:
2015-11-04 08:59:20 +01:00
parent 931b83040b
commit ed9c1e77cd
12 changed files with 210 additions and 326 deletions

View File

@@ -28,6 +28,7 @@ class TransferSearch extends Transfer
public $totalsCreatedAtNotPaid= ['total' => 0, 'accounts' =>[]];
public $totalsCreatedAtPaid= ['total' => 0, 'accounts' =>[]];
public $totalsPaidAt= ['total' => 0, 'accounts' =>[]];
public $totalsPaidAtNotCreatedAt= ['total' => 0, 'accounts' =>[]];
public $accounts;
@@ -95,173 +96,6 @@ class TransferSearch extends Transfer
return $dataProvider;
}
protected function mkTotalQuery($mode){
$query = new Query();
$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->andFilterWhere([
'id_account' => $this->id_account,
'type' => $this->type,
]);
$query->andWhere(['id_user' => Yii::$app->user->id]);
$query->andFilterWhere(['in' ,'type', $this->types]);
if ( $mode == 'created_at'){
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
}else if ( $mode == 'paid_at'){
$query->andFilterWhere([ '>=', 'transfer.paid_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.paid_at' , $this->timestampEnd ] );
}else if ( $mode == 'created_at_not_paid'){
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_NOT_PAID ] );
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
}else if ( $mode == 'created_at_paid'){
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] );
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
}
$query->groupBy('transfer.id_account');
return $query;
}
protected function mkTransferTotalsWithZeroValue( ){
$result = [];
foreach ( $this->accounts as $a ){
$accountTotal = [
'id_account' => $a->id_account,
'label' => $a->name,
'money' => 0
];
$result[] = $accountTotal;
}
return $result;
}
protected function mkTotalsResult($queryResult,$accountMap){
$totals = [];
$totals['total'] = 0;
$totals['accounts'] = [];
foreach ($queryResult as $item){
$account = "";
if ( array_key_exists($item['account'], $accountMap)){
$account = $accountMap[$item['account']];
}
$accountTotal = [
'id_account' => $item['account'],
'label' => $account,
'money' => $item['money']
];
$totals['accounts'][] = $accountTotal;
$totals['total'] += $item['money'];
}
return $totals;
}
protected function findByAccountInQueryResult($queryResult, $account){
$result = null;
foreach ($queryResult as $item){
if( $item['account'] == $account->id_account ){
$result = $item;
}
}
return $result;
}
protected function mkTotalsResultWithAllAvailableAccount($queryResult,$accountMap){
$totals = [];
$totals['total'] = 0;
$totals['accounts'] = [];
foreach ($this->accounts as $account){
if ( isset($this->id_account) && is_numeric($this->id_account) && $this->id_account != $account->id_account ){
continue ;
}
$accountTotal = [
'id_account' => $account->id_account,
'label' => $account->name,
'money' => 0,
];
$item = $this->findByAccountInQueryResult($queryResult, $account);
if ( isset($item)){
$accountTotal['money'] = $item['money'];
}
$totals['accounts'][] = $accountTotal;
$totals['total'] += $accountTotal['money'];
}
return $totals;
}
public function mkPaidAtTotals($accountMap){
$query = $this->mkTotalQuery('paid_at');
$command = $query->createCommand();
$result = $command->queryAll();
$paidAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $paidAtTotals;
}
public function mkCreatedAtTotals($accountMap){
$query = $this->mkTotalQuery('created_at');
$command = $query->createCommand();
$result = $command->queryAll();
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $createdAtTotals;
}
public function mkCreatedAtNotPaidTotals($accountMap){
$query = $this->mkTotalQuery('created_at_not_paid');
$command = $query->createCommand();
$result = $command->queryAll();
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $createdAtTotals;
}
public function mkCreatedAtPaidTotals($accountMap){
$query = $this->mkTotalQuery('created_at_paid');
$command = $query->createCommand();
$result = $command->queryAll();
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $createdAtTotals;
}
public function totalsTransfers($params){
$accountTotals = [];
$fullTotal = [
@@ -273,13 +107,19 @@ class TransferSearch extends Transfer
return;
}
$accounts = Account::find()->orderBy("name asc")->all();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
$idUser = Yii::$app->user->id;
$this->totalsCreatedAt = $this->mkCreatedAtTotals($accountMap);
$this->totalsPaidAt = $this->mkPaidAtTotals($accountMap);
$this->totalsCreatedAtPaid = $this->mkCreatedAtPaidTotals($accountMap);
$this->totalsCreatedAtNotPaid = $this->mkCreatedAtNotPaidTotals($accountMap);
$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'];
}