guild/backend/modules/balance/models/BalanceSearch.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2019-06-21 18:05:58 +03:00
<?php
namespace backend\modules\balance\models;
2019-06-28 12:29:54 +03:00
use backend\modules\balance\models\Balance;
2019-06-21 18:05:58 +03:00
use common\classes\Debug;
2019-06-25 18:28:20 +03:00
use common\models\FieldsValueNew;
use DateTime;
2019-06-25 12:37:09 +03:00
use Yii;
2019-06-21 18:05:58 +03:00
use yii\base\Model;
use yii\data\ActiveDataProvider;
2019-06-25 12:37:09 +03:00
/**
* BalanceSearch represents the model behind the search form of `backend\modules\balance\models\Balance`.
*/
2019-06-21 18:05:58 +03:00
class BalanceSearch extends Balance
{
2019-06-25 12:37:09 +03:00
public $summ_from;
public $summ_to;
public $dt_from;
public $dt_to;
2019-06-26 14:03:42 +03:00
public $field_name;
public $field_value;
2019-06-28 12:29:54 +03:00
public $active_summ;
public $passive_summ;
public $difference;
2019-06-25 12:37:09 +03:00
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2019-06-26 14:03:42 +03:00
[['id', 'type', 'summ', 'summ_from', 'field_name', 'summ_to'], 'integer'],
2019-06-25 12:37:09 +03:00
[['dt_from', 'dt_to', 'dt_add'], 'safe'],
2019-06-26 14:03:42 +03:00
[['field_value'], 'string']
2019-06-25 12:37:09 +03:00
];
}
/**
* {@inheritdoc}
*/
2019-06-21 18:05:58 +03:00
public function scenarios()
{
2019-06-25 12:37:09 +03:00
// bypass scenarios() implementation in the parent class
return Model::scenarios();
2019-06-21 18:05:58 +03:00
}
2019-06-25 12:37:09 +03:00
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
2019-06-21 18:05:58 +03:00
{
2019-07-04 12:29:42 +03:00
$query = Balance::find()->distinct();
2019-06-26 14:03:42 +03:00
$query->leftJoin('fields_value_new','fields_value_new.item_id=balance.id AND fields_value_new.item_type=3');
2019-06-25 12:37:09 +03:00
// add conditions that should always apply here
2019-06-21 18:05:58 +03:00
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
2019-06-25 12:37:09 +03:00
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
2019-06-21 18:05:58 +03:00
return $dataProvider;
}
2019-06-25 12:37:09 +03:00
2019-06-26 14:03:42 +03:00
2019-06-25 12:37:09 +03:00
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'type' => $this->type,
//'summ' => $this->summ,
'dt_add' => $this->dt_add,
]);
2019-06-25 18:28:20 +03:00
$query->andFilterWhere(['>=','dt_add', strtotime($this->dt_from) ?: null]);
$query->andFilterWhere(['<=','dt_add', strtotime($this->dt_to) ?: null]);
2019-06-25 12:37:09 +03:00
2019-06-25 18:28:20 +03:00
$query->andFilterWhere(['between', 'summ', $this->summ_from ?: 0, $this->summ_to ?: 9999999999]);
2019-06-26 14:03:42 +03:00
$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');
2019-06-25 12:37:09 +03:00
return $dataProvider;
2019-06-21 18:05:58 +03:00
}
2019-06-28 12:29:54 +03:00
public function getSummInfo()
{
$query = Balance::find()
->andFilterWhere(['>=','dt_add', strtotime($this->dt_from) ?: null])
->andFilterWhere(['<=','dt_add', strtotime($this->dt_to) ?: null])
->all();
$active_summ = 0;
$passive_summ = 0;
$difference = 0;
foreach ($query as $item)
{
if($item->type == 1)
{
$active_summ += $item->summ;
} else {
$passive_summ += $item->summ;
}
}
$difference = $active_summ - $passive_summ;
2019-06-28 12:33:10 +03:00
2019-06-28 12:29:54 +03:00
return compact('active_summ', 'passive_summ', 'difference');
}
2019-06-25 12:37:09 +03:00
}