255], ]; } public function search($params) { $invoiceModule = InvoiceModule::getInstance(); $invoiceRepository = $invoiceModule->getRepository(); $optionsSearch = $invoiceRepository->getDefaultOptionsSearch(); $sort = new Sort([ 'attributes' => [ 'status', 'reference', 'name', 'date', 'username' => [ 'asc' => ['user_invoice.lastname' => SORT_ASC, 'user_invoice.name' => SORT_ASC], 'desc' => ['user_invoice.lastname' => SORT_DESC, 'user_invoice.name' => SORT_DESC], ] ], 'defaultOrder' => [ 'status' => SORT_ASC, 'reference' => SORT_DESC ] ]); $query = Invoice::find() ->with($optionsSearch['with']) ->joinWith($optionsSearch['join_with']) ->where(['invoice.id_producer' => GlobalParam::getCurrentProducerId()]) //->orderBy('invoice.status ASC, invoice.reference DESC') ->orderBy($sort->orders) ->groupBy('invoice.id'); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => $sort, 'pagination' => [ 'pageSize' => 20, ], ]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['like', 'invoice.name', $this->name]); $query->andFilterWhere(['like', 'invoice.reference', $this->reference]); $query->andFilterWhere(['like', 'invoice.status', $this->status]); $query->andFilterWhere([ 'or', ['like', 'user_invoice.lastname', $this->username], ['like', 'user_invoice.name', $this->username], ['like', 'user_invoice.name_legal_person', $this->username], ]); if ($this->date && strlen($this->date)) { $query->andFilterWhere(['like', 'invoice.date', date('Y-m-d', strtotime(str_replace('/', '-', $this->date)))]); } // filtre envoyé if(!is_null($this->is_sent) && is_numeric($this->is_sent)) { if($this->is_sent) { $query->andWhere(['invoice.is_sent' => 1]); } else { $query->andWhere('invoice.is_sent IS NULL OR invoice.is_sent = 0'); } } // filter payé / non payé // @TODO : comprendre pourquoi la pagination ne suit pas $models = $dataProvider->getModels(); foreach($models as $index => $invoice) { if(!is_null($this->paid) && is_numeric($this->paid)) { $isInvoicePaid = $invoiceModule->getSolver()->isInvoicePaid($invoice); if(($this->paid && !$isInvoicePaid) || (!$this->paid && $isInvoicePaid)) { unset($models[$index]); } } } $dataProvider->setModels($models); return $dataProvider; } }