add_additional_fields

This commit is contained in:
SoHardKI 2019-06-25 18:28:20 +03:00
parent fe56d217c8
commit b2312730c3
10 changed files with 104 additions and 54 deletions

View File

@ -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.');
}
}

View File

@ -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;
//
// }
}
}

View File

@ -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;
}

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\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;
<p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<p>
<?= Html::a('Показать за текущий месяц', ['index', 'month' => true], ['class' => 'btn btn-primary']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $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'],
],
]); ?>

View File

@ -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()
{

View File

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

View File

@ -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' => 'Выберите дату (До)'],
]);

View File

@ -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)

View File

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