'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, ]); // $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); 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 search2(){ $query = new Query() ; $query->select('*'); } 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( ( 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; } }