From 91e3608c4713b254e344c5540dcac8a1a2a1d76a Mon Sep 17 00:00:00 2001 From: SoHardKI Date: Wed, 26 Jun 2019 14:03:42 +0300 Subject: [PATCH] bakance_filter --- .../balance/controllers/BalanceController.php | 13 +++++++------ backend/modules/balance/models/Balance.php | 8 ++++++++ .../modules/balance/models/BalanceSearch.php | 12 ++++++++++-- .../modules/balance/views/balance/index.php | 3 +++ .../widgets/AdditionalFieldsFilterWidget.php | 18 ++++++++++++++++++ .../views/additional_fields_filter.php | 19 +++++++++++++++++++ common/models/Balance.php | 15 +++++++++++---- 7 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 backend/widgets/AdditionalFieldsFilterWidget.php create mode 100644 backend/widgets/views/additional_fields_filter.php diff --git a/backend/modules/balance/controllers/BalanceController.php b/backend/modules/balance/controllers/BalanceController.php index 012b6a7..b223b08 100644 --- a/backend/modules/balance/controllers/BalanceController.php +++ b/backend/modules/balance/controllers/BalanceController.php @@ -24,6 +24,11 @@ class BalanceController extends Controller $searchModel->dt_from = date('Y-m-01'); $searchModel->dt_to = date('Y-m-t'); } + if(\Yii::$app->request->get('previous_month')) + { + $searchModel->dt_from = date('Y-m-d', strtotime('first day of previous month')); + $searchModel->dt_to = date('Y-m-d', strtotime('last day of previous month')); + } $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index',[ @@ -34,10 +39,6 @@ class BalanceController extends Controller public function actionView($id) { - $model = Balance::find() - ->where(['id' => $id]) - ->with('fieldsValues') - ->one(); $dataProviderF = new ActiveDataProvider([ 'query' => FieldsValueNew::find() ->where(['item_id' => $id, 'item_type' => FieldsValueNew::TYPE_BALANCE]) @@ -73,7 +74,7 @@ class BalanceController extends Controller public function actionUpdate($id) { - $model = $this->findModel($id); + $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $model->dt_add = strtotime($model->dt_add); @@ -81,7 +82,7 @@ class BalanceController extends Controller return $this->redirect(['view', 'id' => $model->id]); } - return $this->render('update',[ + return $this->render('update', [ 'model' => $model, ]); } diff --git a/backend/modules/balance/models/Balance.php b/backend/modules/balance/models/Balance.php index 1560f81..98556a8 100644 --- a/backend/modules/balance/models/Balance.php +++ b/backend/modules/balance/models/Balance.php @@ -10,6 +10,14 @@ use common\models\FieldsValueNew; use common\models\ProjectUser; use yii\helpers\ArrayHelper; +/** + * This is the model class for table "company". + * + * @property int $id + * @property int $type + * @property int $summ + * @property int $dt_add + */ class Balance extends \common\models\Balance { diff --git a/backend/modules/balance/models/BalanceSearch.php b/backend/modules/balance/models/BalanceSearch.php index bcec379..1f232eb 100644 --- a/backend/modules/balance/models/BalanceSearch.php +++ b/backend/modules/balance/models/BalanceSearch.php @@ -19,14 +19,17 @@ class BalanceSearch extends Balance public $summ_to; public $dt_from; public $dt_to; + public $field_name; + public $field_value; /** * {@inheritdoc} */ public function rules() { return [ - [['id', 'type', 'summ', 'summ_from', 'summ_to'], 'integer'], + [['id', 'type', 'summ', 'summ_from', 'field_name', 'summ_to'], 'integer'], [['dt_from', 'dt_to', 'dt_add'], 'safe'], + [['field_value'], 'string'] ]; } @@ -49,7 +52,7 @@ class BalanceSearch extends Balance public function search($params) { $query = Balance::find(); - + $query->leftJoin('fields_value_new','fields_value_new.item_id=balance.id AND fields_value_new.item_type=3'); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, @@ -63,6 +66,7 @@ class BalanceSearch extends Balance return $dataProvider; } + // grid filtering conditions $query->andFilterWhere([ 'id' => $this->id, @@ -76,6 +80,10 @@ class BalanceSearch extends Balance $query->andFilterWhere(['between', 'summ', $this->summ_from ?: 0, $this->summ_to ?: 9999999999]); + $query->andFilterWhere(['fields_value_new.field_id'=>$this->field_name]); + $query->andFilterWhere(['LIKE', 'fields_value_new.value', $this->field_value]); + + $query->orderBy('balance.dt_add DESC'); return $dataProvider; } diff --git a/backend/modules/balance/views/balance/index.php b/backend/modules/balance/views/balance/index.php index f46eb21..b5ab6a2 100644 --- a/backend/modules/balance/views/balance/index.php +++ b/backend/modules/balance/views/balance/index.php @@ -19,8 +19,10 @@ $this->params['breadcrumps'][] = $this->title; 'btn btn-success']) ?>

+ true], ['class' => 'btn btn-primary']) ?> true], ['class' => 'btn btn-primary']) ?>

+fields); ?> $dataProvider, 'filterModel' => $searchModel, @@ -72,6 +74,7 @@ $this->params['breadcrumps'][] = $this->title; ]); }, + 'filter' => \backend\widgets\AdditionalFieldsFilterWidget::widget(['model' => $searchModel]), 'headerOptions' => ['width' => '300'], ], diff --git a/backend/widgets/AdditionalFieldsFilterWidget.php b/backend/widgets/AdditionalFieldsFilterWidget.php new file mode 100644 index 0000000..8fa8943 --- /dev/null +++ b/backend/widgets/AdditionalFieldsFilterWidget.php @@ -0,0 +1,18 @@ +render('additional_fields_filter', ['model' => $this->model]); + } + +} \ No newline at end of file diff --git a/backend/widgets/views/additional_fields_filter.php b/backend/widgets/views/additional_fields_filter.php new file mode 100644 index 0000000..c1951d9 --- /dev/null +++ b/backend/widgets/views/additional_fields_filter.php @@ -0,0 +1,19 @@ + $model, + 'attribute' => 'field_name', + 'data' => \common\models\Balance::getNameList(), + 'options' => ['placeholder' => 'Выбрать параметр','class' => 'form-control'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]); + +echo \yii\helpers\Html::activeTextInput($model, 'field_value', [ + 'placeholder' => 'Значение параметра' +]); \ No newline at end of file diff --git a/common/models/Balance.php b/common/models/Balance.php index 606f355..abdba61 100644 --- a/common/models/Balance.php +++ b/common/models/Balance.php @@ -4,6 +4,9 @@ namespace common\models; use common\classes\Debug; +use PHPUnit\Framework\MockObject\Matcher\DeferredError; +use yii\db\Query; +use yii\helpers\ArrayHelper; /** * This is the model class for table "company". @@ -26,14 +29,12 @@ class Balance extends \yii\db\ActiveRecord $fieldValue = FieldsValueNew::find() ->where( [ - //'balance_id' => \Yii::$app->request->get('id'), - 'item_id' => $this->id, + 'item_id' => \Yii::$app->request->get('id'), +// 'item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_BALANCE, ]) ->with('field') ->all(); -// Debug::dd($fieldValue[0]->field->name); - $array = []; if (!empty($fieldValue)) { foreach ($fieldValue as $item) { @@ -76,6 +77,11 @@ class Balance extends \yii\db\ActiveRecord ]; } + public static function getNameList() + { + return ArrayHelper::map(AdditionalFields::find()->all(),'id','name'); + } + public static function tableName() { return 'balance'; @@ -95,6 +101,7 @@ class Balance extends \yii\db\ActiveRecord 'type' => 'Тип', 'summ' => 'Сумма', 'dt_add' => 'Дата добавления', + 'value' => 'Значение', ]; }