|
- <?php
-
-
-
- namespace domain\Document\Invoice;
-
- use common\components\ActiveDataProvider;
- use common\helpers\GlobalParam;
- use yii\data\Sort;
-
- class InvoiceSearch extends Invoice
- {
- var $is_paid;
- var $username;
-
- public function rules()
- {
- return [
- [[], 'safe'],
- [['is_sent', 'is_paid'], 'boolean'],
- [['comment', 'address', 'status', 'username', 'date'], 'string'],
- [['name', 'reference', 'username'], 'string', 'max' => 255],
- ];
- }
-
- public function search($params)
- {
- $invoiceModule = InvoiceModule::getInstance();
- $invoiceRepository = $invoiceModule->getRepository();
- $optionsSearch = $invoiceRepository->getDefaultOptionsSearch();
-
- $sort = new Sort([
- 'attributes' => [
- 'status',
- 'reference',
- 'name',
- 'date',
- 'username' => [
- 'asc' => ['IF(ISNULL(user_invoice.name_legal_person), user_invoice.lastname, user_invoice.name_legal_person)' => SORT_ASC],
- 'desc' => ['IF(ISNULL(user_invoice.name_legal_person), user_invoice.lastname, user_invoice.name_legal_person)' => 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($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)))]);
- }
-
-
- if(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');
- }
- }
-
-
- $isPaid = $this->is_paid;
- if(is_numeric($isPaid)) {
- $dataProvider->filterByCallback(function($model) use ($isPaid) {
- $filterModel = false;
- $invoiceModule = InvoiceModule::getInstance();
- if(is_numeric($isPaid)) {
- $isInvoicePaid = $invoiceModule->getSolver()->isInvoicePaid($model);
- if(($isPaid && !$isInvoicePaid) || (!$isPaid && $isInvoicePaid)) {
- $filterModel = true;
- }
- }
- return $filterModel;
- });
- }
-
- return $dataProvider;
- }
- }
|