0, 'accounts' =>[] ]; public $totalsCreatedAtNotPaid= ['total' => 0, 'accounts' =>[]]; public $totalsCreatedAtPaid= ['total' => 0, 'accounts' =>[]]; public $totalsPaidAt= ['total' => 0, 'accounts' =>[]]; public $accounts; public $types; /** * @inheritdoc */ public function rules() { return [ // [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], // [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], [[ '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']], // [ [ 'types' ], function ($attribute, $params) { // if (!is_array($this->$attribute)) { // $this->addError($attribute, 'Invalid array'); // } // }], ]; } /** * 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'); return $dataProvider; } $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; } 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){ // echo 'account="'. $this->id_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->mkTotalsResult($result, $accountMap); $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 = [ 'label' => Yii::t('common/transfer', 'Total'), 'money' => 0 ]; if ( $this->hasErrors() ){ return; } $accounts = Account::find()->orderBy("name asc")->all(); $accountMap = ArrayHelper::map( $accounts ,'id_account','name' ); $this->totalsCreatedAt = $this->mkCreatedAtTotals($accountMap); $this->totalsPaidAt = $this->mkPaidAtTotals($accountMap); $this->totalsCreatedAtPaid = $this->mkCreatedAtPaidTotals($accountMap); $this->totalsCreatedAtNotPaid = $this->mkCreatedAtNotPaidTotals($accountMap); } }