diff --git a/backend/modules/balance/controllers/BalanceController.php b/backend/modules/balance/controllers/BalanceController.php index af6ab28..b223b08 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,16 @@ 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'); + } + 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',[ @@ -50,7 +61,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', 'Баланса добавлен'); @@ -64,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); @@ -72,7 +82,7 @@ class BalanceController extends Controller return $this->redirect(['view', 'id' => $model->id]); } - return $this->render('update',[ + return $this->render('update', [ 'model' => $model, ]); } @@ -92,4 +102,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..98556a8 100644 --- a/backend/modules/balance/models/Balance.php +++ b/backend/modules/balance/models/Balance.php @@ -4,48 +4,21 @@ namespace backend\modules\balance\models; +use common\classes\Debug; use common\models\FieldsValue; 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 { - 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..1f232eb 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; @@ -17,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'] ]; } @@ -47,9 +52,8 @@ 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, ]); @@ -62,6 +66,7 @@ class BalanceSearch extends Balance return $dataProvider; } + // grid filtering conditions $query->andFilterWhere([ 'id' => $this->id, @@ -70,19 +75,15 @@ 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', $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->andFilterWhere(['between', 'summ', $summ_from, $summ_to]); + $query->orderBy('balance.dt_add DESC'); 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..b5ab6a2 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,11 @@ $this->params['breadcrumps'][] = $this->title;

'btn btn-success']) ?>

+

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

+fields); ?> $dataProvider, 'filterModel' => $searchModel, @@ -27,11 +33,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 +45,7 @@ $this->params['breadcrumps'][] = $this->title; ], [ 'attribute' => 'summ', - 'filter' => \backend\widgets\SummRangeWidget::widget([ + 'filter' => \backend\widgets\SummRangeWidget::widget([ 'model' => $searchModel, ]), @@ -47,9 +53,31 @@ $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}", + + ]); + }, + 'filter' => \backend\widgets\AdditionalFieldsFilterWidget::widget(['model' => $searchModel]), + 'headerOptions' => ['width' => '300'], + + ], ['class' => 'yii\grid\ActionColumn'], ], ]); ?> 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/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/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/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..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". @@ -18,6 +21,49 @@ 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( + [ + 'item_id' => \Yii::$app->request->get('id'), +// 'item_id' => $this->id, + 'item_type' => FieldsValueNew::TYPE_BALANCE, + ]) + ->with('field') + ->all(); + $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]; @@ -31,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'; @@ -50,6 +101,7 @@ class Balance extends \yii\db\ActiveRecord 'type' => 'Тип', 'summ' => 'Сумма', 'dt_add' => 'Дата добавления', + 'value' => 'Значение', ]; } @@ -64,7 +116,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() +// { +// +// } }