Merge pull request #2 from apuc/balance

Balance
This commit is contained in:
kavalar 2019-06-26 17:04:11 +03:00 committed by GitHub
commit 66b6ebe2e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 171 additions and 57 deletions

View File

@ -7,6 +7,7 @@ use backend\modules\balance\models\BalanceSearch;
use common\classes\Debug; use common\classes\Debug;
use common\models\FieldsValue; use common\models\FieldsValue;
use common\models\FieldsValueNew; use common\models\FieldsValueNew;
use DateTime;
use Yii; use Yii;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use yii\web\Controller; use yii\web\Controller;
@ -18,6 +19,16 @@ class BalanceController extends Controller
public function actionIndex() public function actionIndex()
{ {
$searchModel = new BalanceSearch(); $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); $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index',[ return $this->render('index',[
@ -50,7 +61,6 @@ class BalanceController extends Controller
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
$model->dt_add = strtotime($model->dt_add); $model->dt_add = strtotime($model->dt_add);
$model->save(); $model->save();
// Debug::dd($model);
Yii::$app->session->addFlash('success', 'Баланса добавлен'); Yii::$app->session->addFlash('success', 'Баланса добавлен');
@ -64,7 +74,7 @@ class BalanceController extends Controller
public function actionUpdate($id) public function actionUpdate($id)
{ {
$model = $this->findModel($id); $model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
$model->dt_add = strtotime($model->dt_add); $model->dt_add = strtotime($model->dt_add);
@ -72,7 +82,7 @@ class BalanceController extends Controller
return $this->redirect(['view', 'id' => $model->id]); return $this->redirect(['view', 'id' => $model->id]);
} }
return $this->render('update',[ return $this->render('update', [
'model' => $model, 'model' => $model,
]); ]);
} }
@ -92,4 +102,5 @@ class BalanceController extends Controller
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');
} }
} }

View File

@ -4,48 +4,21 @@
namespace backend\modules\balance\models; namespace backend\modules\balance\models;
use common\classes\Debug;
use common\models\FieldsValue; use common\models\FieldsValue;
use common\models\FieldsValueNew; use common\models\FieldsValueNew;
use common\models\ProjectUser; use common\models\ProjectUser;
use yii\helpers\ArrayHelper; 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 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;
//
// }
}
} }

View File

@ -3,6 +3,8 @@
namespace backend\modules\balance\models; namespace backend\modules\balance\models;
use common\classes\Debug; use common\classes\Debug;
use common\models\FieldsValueNew;
use DateTime;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
@ -17,14 +19,17 @@ class BalanceSearch extends Balance
public $summ_to; public $summ_to;
public $dt_from; public $dt_from;
public $dt_to; public $dt_to;
public $field_name;
public $field_value;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rules() public function rules()
{ {
return [ 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'], [['dt_from', 'dt_to', 'dt_add'], 'safe'],
[['field_value'], 'string']
]; ];
} }
@ -47,9 +52,8 @@ class BalanceSearch extends Balance
public function search($params) public function search($params)
{ {
$query = Balance::find(); $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 // add conditions that should always apply here
$dataProvider = new ActiveDataProvider([ $dataProvider = new ActiveDataProvider([
'query' => $query, 'query' => $query,
]); ]);
@ -62,6 +66,7 @@ class BalanceSearch extends Balance
return $dataProvider; return $dataProvider;
} }
// grid filtering conditions // grid filtering conditions
$query->andFilterWhere([ $query->andFilterWhere([
'id' => $this->id, 'id' => $this->id,
@ -70,19 +75,15 @@ class BalanceSearch extends Balance
'dt_add' => $this->dt_add, '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; $query->andFilterWhere(['between', 'summ', $this->summ_from ?: 0, $this->summ_to ?: 9999999999]);
$summ_to = $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; return $dataProvider;
} }

View File

@ -0,0 +1,2 @@
<?= $model->field->name;?> -
<?= $model->value;?>

View File

@ -5,6 +5,7 @@ use yii\data\ActiveDataProvider;
use yii\helpers\Html; use yii\helpers\Html;
use yii\grid\GridView; use yii\grid\GridView;
use yii\web\View; use yii\web\View;
use yii\widgets\ListView;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $searchModel backend\modules\company\models\BalanceSearch */ /* @var $searchModel backend\modules\company\models\BalanceSearch */
@ -17,6 +18,11 @@ $this->params['breadcrumps'][] = $this->title;
<p> <p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?> <?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
</p> </p>
<p>
<?= Html::a('Показать за прошлый месяц', ['index', 'previous_month' => true], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Показать за текущий месяц', ['index', 'month' => true], ['class' => 'btn btn-primary']) ?>
</p>
<!-- --><?php //\common\classes\Debug::dd($searchModel->fields); ?>
<?= GridView::widget([ <?= GridView::widget([
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'filterModel' => $searchModel, 'filterModel' => $searchModel,
@ -27,11 +33,11 @@ $this->params['breadcrumps'][] = $this->title;
'value' => function ($model) { 'value' => function ($model) {
return \common\models\Balance::getTypeName($model->type); return \common\models\Balance::getTypeName($model->type);
}, },
'filter' => kartik\select2\Select2::widget([ 'filter' => kartik\select2\Select2::widget([
'model' => $searchModel, 'model' => $searchModel,
'attribute' => 'type', 'attribute' => 'type',
'data' => \common\models\Balance::getTypeList(), 'data' => \common\models\Balance::getTypeList(),
'options' => ['placeholder' => 'Начните вводить...','class' => 'form-control'], 'options' => ['placeholder' => 'Начните вводить...', 'class' => 'form-control'],
'pluginOptions' => [ 'pluginOptions' => [
'allowClear' => true 'allowClear' => true
], ],
@ -39,7 +45,7 @@ $this->params['breadcrumps'][] = $this->title;
], ],
[ [
'attribute' => 'summ', 'attribute' => 'summ',
'filter' => \backend\widgets\SummRangeWidget::widget([ 'filter' => \backend\widgets\SummRangeWidget::widget([
'model' => $searchModel, 'model' => $searchModel,
]), ]),
@ -47,9 +53,31 @@ $this->params['breadcrumps'][] = $this->title;
[ [
'attribute' => 'dt_add', 'attribute' => 'dt_add',
'value' => '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', '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'], ['class' => 'yii\grid\ActionColumn'],
], ],
]); ?> ]); ?>

View File

@ -0,0 +1,18 @@
<?php
namespace backend\widgets;
use yii\base\Widget;
class AdditionalFieldsFilterWidget extends Widget
{
public $model;
public function run()
{
return $this->render('additional_fields_filter', ['model' => $this->model]);
}
}

View File

@ -4,11 +4,13 @@
namespace backend\widgets; namespace backend\widgets;
use common\classes\Debug;
use yii\base\Widget; use yii\base\Widget;
class DateRangeWidget extends Widget class DateRangeWidget extends Widget
{ {
public $model; public $model;
public $range_attribute;
public function run() public function run()
{ {

View File

@ -4,6 +4,7 @@
namespace backend\widgets; namespace backend\widgets;
use common\classes\Debug;
use yii\base\Widget; use yii\base\Widget;
class SummRangeWidget extends Widget class SummRangeWidget extends Widget

View File

@ -0,0 +1,19 @@
<?php
/**
* @var $model \yii\db\ActiveRecord
*/
echo \kartik\select2\Select2::widget(
[
'model' => $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' => 'Значение параметра'
]);

View File

@ -9,6 +9,7 @@ echo DatePicker::widget([
'attribute' => 'dt_from', 'attribute' => 'dt_from',
'language' => 'ru', 'language' => 'ru',
'dateFormat' => 'dd-MM-yyyy', 'dateFormat' => 'dd-MM-yyyy',
'options' => ['placeholder' => 'Выберите дату (От)'],
]); ]);
echo " - "; echo " - ";
@ -18,4 +19,5 @@ echo DatePicker::widget([
'attribute' => 'dt_to', 'attribute' => 'dt_to',
'language' => 'ru', 'language' => 'ru',
'dateFormat' => 'dd-MM-yyyy', 'dateFormat' => 'dd-MM-yyyy',
'options' => ['placeholder' => 'Выберите дату (До)'],
]); ]);

View File

@ -4,6 +4,9 @@
namespace common\models; namespace common\models;
use common\classes\Debug; 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". * This is the model class for table "company".
@ -18,6 +21,49 @@ class Balance extends \yii\db\ActiveRecord
const TYPE_ACTIVE = 1; const TYPE_ACTIVE = 1;
const TYPE_PASSIVE = 0; 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) public static function getTypeName($id)
{ {
return self::getTypeList()[$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() public static function tableName()
{ {
return 'balance'; return 'balance';
@ -50,6 +101,7 @@ class Balance extends \yii\db\ActiveRecord
'type' => 'Тип', 'type' => 'Тип',
'summ' => 'Сумма', 'summ' => 'Сумма',
'dt_add' => 'Дата добавления', 'dt_add' => 'Дата добавления',
'value' => 'Значение',
]; ];
} }
@ -64,7 +116,7 @@ class Balance extends \yii\db\ActiveRecord
*/ */
public function getFieldsValues() 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) public function afterSave($insert, $changedAttributes)

View File

@ -62,4 +62,9 @@ class FieldsValueNew extends \yii\db\ActiveRecord
{ {
return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']); return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']);
} }
// public function getadditional_fields()
// {
//
// }
} }