From b2312730c3eecda82660b7219b832aa7f6f05c26 Mon Sep 17 00:00:00 2001 From: SoHardKI Date: Tue, 25 Jun 2019 18:28:20 +0300 Subject: [PATCH] add_additional_fields --- .../balance/controllers/BalanceController.php | 12 ++++- backend/modules/balance/models/Balance.php | 37 +-------------- .../modules/balance/models/BalanceSearch.php | 17 ++----- .../balance/views/balance/_additional.php | 2 + .../modules/balance/views/balance/index.php | 33 +++++++++++-- backend/widgets/DateRangeWidget.php | 2 + backend/widgets/SummRangeWidget.php | 1 + backend/widgets/views/date_range.php | 2 + common/models/Balance.php | 47 ++++++++++++++++++- common/models/FieldsValueNew.php | 5 ++ 10 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 backend/modules/balance/views/balance/_additional.php diff --git a/backend/modules/balance/controllers/BalanceController.php b/backend/modules/balance/controllers/BalanceController.php index af6ab28..012b6a7 100644 --- a/backend/modules/balance/controllers/BalanceController.php +++ b/backend/modules/balance/controllers/BalanceController.php @@ -7,6 +7,7 @@ use backend\modules\balance\models\BalanceSearch; use common\classes\Debug; use common\models\FieldsValue; use common\models\FieldsValueNew; +use DateTime; use Yii; use yii\data\ActiveDataProvider; use yii\web\Controller; @@ -18,6 +19,11 @@ class BalanceController extends Controller public function actionIndex() { $searchModel = new BalanceSearch(); + if(\Yii::$app->request->get('month')) + { + $searchModel->dt_from = date('Y-m-01'); + $searchModel->dt_to = date('Y-m-t'); + } $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index',[ @@ -28,6 +34,10 @@ 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]) @@ -50,7 +60,6 @@ class BalanceController extends Controller if ($model->load(Yii::$app->request->post())) { $model->dt_add = strtotime($model->dt_add); $model->save(); -// Debug::dd($model); Yii::$app->session->addFlash('success', 'Баланса добавлен'); @@ -92,4 +101,5 @@ class BalanceController extends Controller throw new NotFoundHttpException('The requested page does not exist.'); } + } \ No newline at end of file diff --git a/backend/modules/balance/models/Balance.php b/backend/modules/balance/models/Balance.php index 9076748..1560f81 100644 --- a/backend/modules/balance/models/Balance.php +++ b/backend/modules/balance/models/Balance.php @@ -4,6 +4,7 @@ namespace backend\modules\balance\models; +use common\classes\Debug; use common\models\FieldsValue; use common\models\FieldsValueNew; use common\models\ProjectUser; @@ -11,41 +12,5 @@ use yii\helpers\ArrayHelper; class Balance extends \common\models\Balance { - public $fields; - public function init() - { - parent::init(); - $fieldValue = FieldsValueNew::find() - ->where( - [ - //'balance_id' => \Yii::$app->request->get('id'), - 'item_id' => \Yii::$app->request->get('id'), - 'item_type' => FieldsValueNew::TYPE_BALANCE, - ]) - ->all(); - $array = []; - if (!empty($fieldValue)) { - foreach ($fieldValue as $item) { - array_push($array, ['field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order]); - } - $this->fields = $array; - } else { - $this->fields = [ - [ - 'field_id' => null, - 'value' => null, - 'order' => null, - ], - ]; - } - -// $user = ArrayHelper::getColumn(ProjectUser::find()->where(['project_id' => \Yii::$app->request->get('id')])->all(), -// 'card_id'); -// -// if (!empty($user)) { -// $this->user = $user; -// -// } - } } \ No newline at end of file diff --git a/backend/modules/balance/models/BalanceSearch.php b/backend/modules/balance/models/BalanceSearch.php index 8623741..bcec379 100644 --- a/backend/modules/balance/models/BalanceSearch.php +++ b/backend/modules/balance/models/BalanceSearch.php @@ -3,6 +3,8 @@ namespace backend\modules\balance\models; use common\classes\Debug; +use common\models\FieldsValueNew; +use DateTime; use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; @@ -49,7 +51,6 @@ class BalanceSearch extends Balance $query = Balance::find(); // add conditions that should always apply here - $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); @@ -70,19 +71,11 @@ class BalanceSearch extends Balance 'dt_add' => $this->dt_add, ]); - //Debug::dd($this); + $query->andFilterWhere(['>=','dt_add', strtotime($this->dt_from) ?: null]); + $query->andFilterWhere(['<=','dt_add', strtotime($this->dt_to) ?: null]); - if($this->dt_from && $this->dt_to){ - $query->where(['between', 'dt_add', strtotime($this->$this->dt_from), strtotime($this->$this->dt_to)]); - } - if($this->dt_from){ - $query->where(['>', 'dt_add', strtotime($this->$this->dt_from)]); - } - $summ_from = $this->summ_from ?: 0; - $summ_to = $this->summ_to ?: 9999999999; - - $query->andFilterWhere(['between', 'summ', $summ_from, $summ_to]); + $query->andFilterWhere(['between', 'summ', $this->summ_from ?: 0, $this->summ_to ?: 9999999999]); return $dataProvider; } diff --git a/backend/modules/balance/views/balance/_additional.php b/backend/modules/balance/views/balance/_additional.php new file mode 100644 index 0000000..fc91c32 --- /dev/null +++ b/backend/modules/balance/views/balance/_additional.php @@ -0,0 +1,2 @@ +field->name;?> - +value;?> \ No newline at end of file diff --git a/backend/modules/balance/views/balance/index.php b/backend/modules/balance/views/balance/index.php index 170b1c5..f46eb21 100644 --- a/backend/modules/balance/views/balance/index.php +++ b/backend/modules/balance/views/balance/index.php @@ -5,6 +5,7 @@ use yii\data\ActiveDataProvider; use yii\helpers\Html; use yii\grid\GridView; use yii\web\View; +use yii\widgets\ListView; /* @var $this yii\web\View */ /* @var $searchModel backend\modules\company\models\BalanceSearch */ @@ -17,6 +18,9 @@ $this->params['breadcrumps'][] = $this->title;

'btn btn-success']) ?>

+

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

$dataProvider, 'filterModel' => $searchModel, @@ -27,11 +31,11 @@ $this->params['breadcrumps'][] = $this->title; 'value' => function ($model) { return \common\models\Balance::getTypeName($model->type); }, - 'filter' => kartik\select2\Select2::widget([ + 'filter' => kartik\select2\Select2::widget([ 'model' => $searchModel, 'attribute' => 'type', 'data' => \common\models\Balance::getTypeList(), - 'options' => ['placeholder' => 'Начните вводить...','class' => 'form-control'], + 'options' => ['placeholder' => 'Начните вводить...', 'class' => 'form-control'], 'pluginOptions' => [ 'allowClear' => true ], @@ -39,7 +43,7 @@ $this->params['breadcrumps'][] = $this->title; ], [ 'attribute' => 'summ', - 'filter' => \backend\widgets\SummRangeWidget::widget([ + 'filter' => \backend\widgets\SummRangeWidget::widget([ 'model' => $searchModel, ]), @@ -47,9 +51,30 @@ $this->params['breadcrumps'][] = $this->title; [ 'attribute' => 'dt_add', 'value' => 'dt_add', - 'filter' => \yii\jui\DatePicker::widget(['language' => 'ru', 'dateFormat' => 'dd-MM-yyyy']), + 'filter' => \backend\widgets\DateRangeWidget::widget([ +// 'language' => 'ru', +// 'dateFormat' => 'dd-MM-yyyy' + 'model' => $searchModel, + ]), 'format' => 'html', ], + [ + 'label' => 'Доп. информация', + 'format' => 'raw', + 'value' => function ($model) { + $dataProvider = new ActiveDataProvider([ + 'query' => $model->getFieldsValues(), + ]); + return ListView::widget([ + 'dataProvider' => $dataProvider, + 'itemView' => '_additional', + 'layout' => "{items}", + + ]); + }, + 'headerOptions' => ['width' => '300'], + + ], ['class' => 'yii\grid\ActionColumn'], ], ]); ?> diff --git a/backend/widgets/DateRangeWidget.php b/backend/widgets/DateRangeWidget.php index 513362f..98a1b07 100644 --- a/backend/widgets/DateRangeWidget.php +++ b/backend/widgets/DateRangeWidget.php @@ -4,11 +4,13 @@ namespace backend\widgets; +use common\classes\Debug; use yii\base\Widget; class DateRangeWidget extends Widget { public $model; + public $range_attribute; public function run() { diff --git a/backend/widgets/SummRangeWidget.php b/backend/widgets/SummRangeWidget.php index cc97005..ab99119 100644 --- a/backend/widgets/SummRangeWidget.php +++ b/backend/widgets/SummRangeWidget.php @@ -4,6 +4,7 @@ namespace backend\widgets; +use common\classes\Debug; use yii\base\Widget; class SummRangeWidget extends Widget diff --git a/backend/widgets/views/date_range.php b/backend/widgets/views/date_range.php index 5a253f7..bd1d9d1 100644 --- a/backend/widgets/views/date_range.php +++ b/backend/widgets/views/date_range.php @@ -9,6 +9,7 @@ echo DatePicker::widget([ 'attribute' => 'dt_from', 'language' => 'ru', 'dateFormat' => 'dd-MM-yyyy', + 'options' => ['placeholder' => 'Выберите дату (От)'], ]); echo " - "; @@ -18,4 +19,5 @@ echo DatePicker::widget([ 'attribute' => 'dt_to', 'language' => 'ru', 'dateFormat' => 'dd-MM-yyyy', + 'options' => ['placeholder' => 'Выберите дату (До)'], ]); \ No newline at end of file diff --git a/common/models/Balance.php b/common/models/Balance.php index 105a7c8..606f355 100644 --- a/common/models/Balance.php +++ b/common/models/Balance.php @@ -18,6 +18,51 @@ class Balance extends \yii\db\ActiveRecord const TYPE_ACTIVE = 1; const TYPE_PASSIVE = 0; + public $fields; + + public function init() + { + parent::init(); + $fieldValue = FieldsValueNew::find() + ->where( + [ + //'balance_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) { + array_push($array, + ['field_id' => $item->field_id, + 'value' => $item->value, + 'order' => $item->order, + 'field_name' => $item->field->name]); + } + $this->fields = $array; + } else { + $this->fields = [ + [ + 'field_id' => null, + 'value' => null, + 'order' => null, + 'field_name' => null, + ], + ]; + } +// $user = ArrayHelper::getColumn(ProjectUser::find()->where(['project_id' => \Yii::$app->request->get('id')])->all(), +// 'card_id'); +// +// if (!empty($user)) { +// $this->user = $user; +// +// } + } + public static function getTypeName($id) { return self::getTypeList()[$id]; @@ -64,7 +109,7 @@ class Balance extends \yii\db\ActiveRecord */ public function getFieldsValues() { - return $this->hasMany(FieldsValueNew::class, ['item_id' => 'id', 'item_type' => FieldsValueNew::TYPE_BALANCE]); + return $this->hasMany(FieldsValueNew::class, ['item_id' => 'id'])->where(['item_type' => FieldsValueNew::TYPE_BALANCE])->with('field'); } public function afterSave($insert, $changedAttributes) diff --git a/common/models/FieldsValueNew.php b/common/models/FieldsValueNew.php index f2a9fa7..a6bf835 100644 --- a/common/models/FieldsValueNew.php +++ b/common/models/FieldsValueNew.php @@ -62,4 +62,9 @@ class FieldsValueNew extends \yii\db\ActiveRecord { return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']); } + +// public function getadditional_fields() +// { +// +// } }