add collections to backend

This commit is contained in:
rocho 2015-11-04 15:39:02 +01:00
parent 48a00e4bdc
commit 4303409fe2
22 changed files with 212 additions and 266 deletions

View File

@ -36,6 +36,10 @@ class AdminMenuStructure{
$today = \Yii::$app->formatter->asDate( strtotime('today UTC') );
$tomorrow = \Yii::$app->formatter->asDate( ( 60 *60 *24 + time()));
$todayDatetime = \Yii::$app->formatter->asDatetime( strtotime('today UTC') );
$tomorrowDatetime = \Yii::$app->formatter->asDatetime( strtotime('tomorrow UTC') );
// if ( $this->can('backend.user.index')){
$items[] = ['label' => 'Felhasználók', 'url' =>['/user/index']];
// }
@ -53,7 +57,7 @@ class AdminMenuStructure{
$items[] = ['label' => 'Tranzakciók', 'url' => ['/transfer/index' , 'TransferSearch[start]' =>$today,'TransferSearch[end]' => $tomorrow ] ];
$items[] = ['label' => 'Kassza müveletek', 'url' => ['/account-state/index'] ];
$items[] = ['label' => 'Zárások', 'url' => ['/collection/index' , 'CollectionSearch[start]' =>$today,'CollectionSearch[end]' => $tomorrow ] ];
$items[] = ['label' => 'Zárások', 'url' => ['/collection/index' , 'CollectionSearch[start]' =>$todayDatetime,'CollectionSearch[end]' => $tomorrowDatetime ] ];
if ( count($items) > 0 ){
$userMainMenu = ['label' => 'Beállítások', 'url' => null,

View File

@ -8,6 +8,9 @@ use backend\models\CollectionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Account;
use common\models\Transfer;
use common\models\User;
/**
* CollectionController implements the CRUD actions for Collection model.
@ -15,6 +18,25 @@ use yii\filters\VerbFilter;
class CollectionController extends \backend\controllers\BackendController
{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
// allow authenticated users
[
'actions' => [ 'index','view' ],
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
/**
* Lists all Collection models.
* @return mixed
@ -22,7 +44,13 @@ class CollectionController extends \backend\controllers\BackendController
public function actionIndex()
{
$searchModel = new CollectionSearch();
$searchModel->accounts = Account::read();
$searchModel->accountMap = Account::toAccaountMap($searchModel->accounts);
$searchModel->users = User::find()->all( );
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$searchModel->searchTotal();
return $this->render('index', [
'searchModel' => $searchModel,
@ -37,60 +65,19 @@ class CollectionController extends \backend\controllers\BackendController
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Collection model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Collection();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_collection]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Collection model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_collection]);
} else {
return $this->render('update', [
$accounts = Account::read();
$accountMap = Account::toAccaountMap($accounts);
$totals = Transfer::mkTotals($model->start, $model->end, $model->id_user, null, $model->id_account, $accounts, $accountMap);
return $this->render('view', [
'model' => $model,
'totals' => $totals,
]);
}
}
/**
* Deletes an existing Collection model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Collection model based on its primary key value.

View File

@ -1,5 +1,4 @@
<?php
namespace backend\controllers;
use Yii;
@ -45,12 +44,11 @@ class TransferController extends \backend\controllers\BackendController
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$accounts = Account::readAccounts();
$searchModel->totalsTransfers();
$users = User::read();
$searchModel->totals(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,

View File

@ -6,20 +6,33 @@ use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Collection;
use common\components\Helper;
/**
* CollectionSearch represents the model behind the search form about `common\models\Collection`.
*/
class CollectionSearch extends Collection
{
public $accounts;
public $accountMap;
public $users;
public $timestampStart;
public $timestampEnd;
public $totals;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_collection', 'id_user', 'created_by', 'id_account', 'money', 'type'], 'integer'],
[['start', 'end', 'created_at', 'updated_at'], 'safe'],
[[ 'start', ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[['id_account','id_user'],'integer']
];
}
@ -56,18 +69,17 @@ class CollectionSearch extends Collection
}
$query->andFilterWhere([
'id_collection' => $this->id_collection,
'id_user' => $this->id_user,
'created_by' => $this->created_by,
'id_account' => $this->id_account,
'money' => $this->money,
'start' => $this->start,
'end' => $this->end,
'type' => $this->type,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
Helper::inInterval($query, 'collection.end', $this->timestampStart, $this->timestampEnd);
return $dataProvider;
}
public function searchTotal(){
$this->totals = Collection::mkReceptionTotal($this->timestampStart, $this->timestampEnd , Yii::$app->user->id, [Collection::TYPE_RECEPTION], $this->id_account, $this->accounts, $this->accountMap);
}
}

View File

@ -11,6 +11,7 @@ use yii\base\Object;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use common\models\Account;
use common\components\Helper;
/**
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
@ -29,6 +30,9 @@ class TransferSearch extends Transfer
public $accountTotals;
public $fullTotal;
public $types;
public $totals;
/**
* @inheritdoc
*/
@ -39,6 +43,7 @@ class TransferSearch extends Transfer
// [[ 'searchObjectName' ], 'string'],
[[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
['types', 'each', 'rule' => ['integer']],
];
}
@ -66,17 +71,8 @@ class TransferSearch extends Transfer
'query' => $query,
]);
// $selectObjectName = "";
// $selectObjectName .= " case when transfer.type = " .self::TYPE_PRODUCT ." then product.name ";
// $selectObjectName .= " when transfer.type = " .self::TYPE_TICKET ." then ticket_type.name ";
// $selectObjectName .= " when transfer.type = " .self::TYPE_MONEY_MOVEMENT_OUT ." then 'Pénzmozgás' ";
// $selectObjectName .= " else '' ";
// $selectObjectName .= " end as object_name";
$query->addSelect( ['*' ]);
// $query->joinWith('ticket');
// $query->joinWith('ticketType');
// $query->joinWith('product');
$this->load($params);
@ -92,94 +88,33 @@ class TransferSearch extends Transfer
'transfer.id_user' => $this->id_user,
]);
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
// if ( isset($this->searchObjectName))
// $query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'"));
$created_condition = ['and',[ '>=', 'transfer.created_at', $this->timestampStart ] ,[ '<', 'transfer.created_at', $this->timestampEnd ] ];
$paid_condition = ['and',[ '>=', 'transfer.paid_at', $this->timestampStart ] ,[ '<', 'transfer.paid_at', $this->timestampEnd ] ];
$query->andFilterWhere(['or' , $created_condition , $paid_condition]);
return $dataProvider;
}
public function search2(){
$query = new Query() ;
$query->select('*');
}
public function totals($params){
$query = new Query();
$result = [ ];
public function totalsTransfers( ){
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
$query->addSelect( [ new Expression( 'transfer.id_account as account'), new Expression( ' COALESCE(sum( ( case when direction = '.Transfer::DIRECTION_OUT.' then -1 else 1 end )* transfer.money ),0) as money' )]);
$query->from('transfer');
// $query->leftJoin('ticket', 'transfer.type = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' );
// $query->leftJoin('ticket_type', 'ticket.id_ticket_type = ticket_type.id_ticket_type ' );
// $query->leftJoin('product', 'product.id_product = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' );
$this->load($params);
if (!$this->validate()) {
return ;
}
$query->andFilterWhere([
'transfer.id_account' => $this->id_account,
'transfer.type' => $this->type,
'transfer.id_user' => $this->id_user,
]);
$query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] );
$query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] );
// if ( isset($this->searchObjectName) && !empty($this->searchObjectName)){
// $query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'"));
// }
$query->groupBy('transfer.id_account');
$command = $query->createCommand();
// $command->sql returns the actual SQL
$result = $command->queryAll();
$accounts = Account::find()->orderBy("name asc")->all();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
$idUser = Yii::$app->user->id;
$this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
foreach ($result as $item){
$account = "";
if ( array_key_exists($item['account'], $accountMap)){
$account = $accountMap[$item['account']];
}
$accountTotal = [
'label' => $account,
'money' => $item['money']
];
$accountTotals[] = $accountTotal;
$fullTotal['money'] += $item['money'];
}
$this->accountTotals = $accountTotals;
$this->fullTotal = $fullTotal;
// return $result;
}
}

View File

@ -2,12 +2,22 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\components\HtmlHelper;
use kartik\widgets\DateTimePicker;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $model backend\models\CollectionSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$accountOptions = ['' =>Yii::t('frontend/collection','All')]+ HtmlHelper::mkAccountOptions($model->accounts);
$userOptions = ['' =>Yii::t('frontend/collection','All')]+ ArrayHelper::map($model->users,'id','username');
?>
<div class="collection-search">
<?php $form = ActiveForm::begin([
@ -15,29 +25,35 @@ use yii\widgets\ActiveForm;
'method' => 'get',
]); ?>
<?= $form->field($model, 'id_collection') ?>
<?= $form->field($model, 'id_user') ?>
<div class='row'>
<div class='col-md-3'>
<?= $form->field($model, 'id_account')->dropDownList( $accountOptions ) ?>
</div>
<div class='col-md-3'>
<?= $form->field($model, 'id_user')->dropDownList( $userOptions ) ?>
</div>
<div class="col-md-3">
<?= $form->field($model, 'start')->widget(DateTimePicker::classname(), [
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy.mm.dd hh:ii'
]
]) ?>
</div>
<div class="col-md-3">
<?= $form->field($model, 'end') ->widget(DateTimePicker::classname(), [
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy.mm.dd hh:ii'
]
]) ?>
</div>
<?= $form->field($model, 'created_by') ?>
<?= $form->field($model, 'id_account') ?>
<?= $form->field($model, 'money') ?>
<?php // echo $form->field($model, 'start') ?>
<?php // echo $form->field($model, 'end') ?>
<?php // echo $form->field($model, 'type') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
</div>
<div class="form-group">
<?= Html::submitButton(Yii::t('backend/collection', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('backend/collection', 'Reset'), ['class' => 'btn btn-default']) ?>
<?= Html::submitButton(Yii::t('frontend/collection', 'Search'), ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>

View File

@ -15,27 +15,37 @@ $this->params['breadcrumbs'][] = $this->title;
<h1><?= Html::encode($this->title) ?></h1>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('backend/collection', 'Create Collection'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id_collection',
'id_user',
'created_by',
'id_account',
'money',
// 'start',
// 'end',
// 'type',
// 'created_at',
// 'updated_at',
[
'attribute' => 'id_user',
'value' => 'userName',
],
[
'attribute' => 'id_account',
'value' => 'accountName',
],
'money:integer',
[
'attribute' => 'start',
'value' => function($model){
return Yii::$app->formatter->asDatetime($model->start,'yyyy-MM-dd HH:mm:ss');
}
],
[
'attribute' => 'end',
'value' => function($model){
return Yii::$app->formatter->asDatetime($model->end,'yyyy-MM-dd HH:mm:ss');
}
],
'created_at:datetime',
['class' => 'yii\grid\ActionColumn'],
['class' => 'yii\grid\ActionColumn',
'template' => '{view}'
],
],
]); ?>

View File

@ -2,6 +2,7 @@
use yii\helpers\Html;
use yii\widgets\DetailView;
use common\components\AccountStatisticWidget;
/* @var $this yii\web\View */
/* @var $model common\models\Collection */
@ -14,31 +15,30 @@ $this->params['breadcrumbs'][] = $this->title;
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('backend/collection', 'Update'), ['update', 'id' => $model->id_collection], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('backend/collection', 'Delete'), ['delete', 'id' => $model->id_collection], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('backend/collection', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id_collection',
'id_user',
'created_by',
'id_account',
'money',
'start',
'end',
'type',
'created_at',
'updated_at',
[
'attribute' =>'id_user',
'value' => $model->userName
],
[
'attribute' =>'id_account',
'value' => $model->accountName
],
'money:integer',
'start:datetime',
'end:datetime',
'created_at:datetime',
],
]) ?>
<?php
echo AccountStatisticWidget::widget([
'totals' => $totals
]);
?>
</div>

View File

@ -26,7 +26,7 @@ use kartik\widgets\DatePicker;
<?= $form->field($model, 'id_account')->dropDownList( ['' => Yii::t('common/transfer', 'All')] +HtmlHelper::mkAccountOptions($accounts) ) ?>
</div>
<div class='col-md-4'>
<?= $form->field($model, 'type')->dropDownList( ['' => Yii::t('common/transfer', 'All')] + Transfer::types() ) ?>
<?php echo $form->field($model, 'types')->checkboxList( Transfer::types()) ?>
</div>
<div class='col-md-4'>
<?= $form->field($model, 'id_user')->dropDownList( ['' => Yii::t('common/transfer', 'All')] +ArrayHelper::map($users,'id' , 'username') ) ?>

View File

@ -6,6 +6,7 @@ use yii\widgets\DetailView;
use yii\base\Widget;
use yii\base\Object;
use yii\data\ArrayDataProvider;
use common\components\AccountStatisticWidget;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\TransferSearch */
@ -28,42 +29,15 @@ $this->params['breadcrumbs'][] = $this->title;
<h1><?= Html::encode($this->title) ?></h1>
<?php echo $this->render('_search', ['model' => $searchModel, 'accounts' => $accounts,'users' => $users,]); ?>
<?php
echo AccountStatisticWidget::widget([
'totals' => $searchModel->totals
]);
?>
<h2>Összesen</h2>
<?php
echo DetailView::widget([
'model' => $searchModel->fullTotal,
'attributes' => [
[
'value' => $searchModel->fullTotal['money'],
'label' => $searchModel->fullTotal['label'],
],
]
])
?>
<h2>Kasszák összesen</h2>
<?php
echo GridView::widget([
'dataProvider' => new ArrayDataProvider([
'allModels' => $searchModel->accountTotals,
'sort' => false,
'pagination' => false,
]) ,
'columns' => [
[
'label' => Yii::t('common/transfer','Account'),
'value' => 'label'
],
[
'label' => Yii::t('common/transfer','Money total'),
'value' => 'money'
],
]
]
);
?>
<h2>Pénzmozgások</h2>
<h2>Tranzakciók</h2>
<?= GridView::widget([
'tableOptions' => ['class' => 'table table-striped table-bordered table-transfer'],
'dataProvider' => $dataProvider,

View File

@ -1,5 +1,5 @@
<?php
namespace frontend\components;
namespace common\components;
use yii\base\Widget;
@ -8,7 +8,7 @@ class AccountStatisticWidget extends Widget{
public $totals;
public $viewFile = '//common/_account_statistic';
public $viewFile = '@common/views/common/_account_statistic';
public function init(){
parent::init();

View File

@ -13,4 +13,18 @@ class Helper
return $result;
}
public static function notInInterval($query ,$field , $start,$end ){
$query->andFilterWhere( ['or', [ '<', $field , isset( $start ) ? $start : '1900-01-01' ] ,[ '>=' , $field , isset($end) ? $end : '3000-01-01' ] ] );
}
public static function notPaid($query ,$field , $start,$end ){
$query->andFilterWhere( ['or', [ '<', $field , isset( $start ) ? $start : '1900-01-01' ] ,[ '>=' , $field , isset($end) ? $end : '3000-01-01' ] ,[ "transfer.status" => Transfer::STATUS_NOT_PAID ] ] );
}
public static function inInterval($query ,$field , $start,$end ){
$query->andFilterWhere([ '>=', $field , $start ] );
$query->andFilterWhere([ '<' , $field , $end ] );
}
}

View File

@ -114,6 +114,9 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
'created_at' => Yii::t('common/transfer', 'Created At'),
'updated_at' => Yii::t('common/transfer', 'Updated At'),
'paid_at' => Yii::t('common/transfer', 'Paid At'),
'types' => Yii::t('common/transfer', 'Types'),
'start' => Yii::t('common/transfer', 'Start'),
'end' => Yii::t('common/transfer', 'End'),
];
}

View File

@ -44,6 +44,7 @@ class CollectionController extends Controller
$searchModel->accounts = Account::read();
$searchModel->accountMap = Account::toAccaountMap($searchModel->accounts);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$searchModel->searchTotal();

View File

@ -14,6 +14,7 @@ class CollectionSearch extends Collection
{
public $accounts;
public $accountMap;
public $users;
public $timestampStart;
public $timestampEnd;

View File

@ -68,13 +68,16 @@ class TransferSearch extends Transfer
'query' => $query,
]);
$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');
return $dataProvider;
$query->where('0=1');
}
$query->andWhere(['id_user' => Yii::$app->user->id ] );
$query->andFilterWhere([
'id_account' => $this->id_account,
@ -98,11 +101,6 @@ class TransferSearch extends Transfer
'money' => 0
];
// if ( $this->hasErrors() ){
// return;
// }
$accounts = Account::find()->orderBy("name asc")->all();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
@ -110,11 +108,6 @@ class TransferSearch extends Transfer
$this->totals = Transfer::mkTotals($this->timestampStart, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
// $this->totalsCreatedAt = $totals['created_at'];
// $this->totalsPaidAt = $totals['paid_at'];
// $this->totalsCreatedAtPaid = $totals['created_at_paid'];
// $this->totalsCreatedAtNotPaid = $totals['created_at_not_paid'];
// $this->totalsPaidAtNotCreatedAt = $totals['paid_at_not_created_at'];
}

View File

@ -2,8 +2,8 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\widgets\DateTimePicker;
use frontend\components\HtmlHelper;
use kartik\widgets\DateTimePicker;
/* @var $this yii\web\View */
/* @var $model frontend\models\CollectionSearch */

View File

@ -2,7 +2,7 @@
use yii\helpers\Html;
use frontend\components\TransferTotalWidget;
use frontend\components\AccountStatisticWidget;
use common\components\AccountStatisticWidget;
/* @var $this yii\web\View */

View File

@ -2,7 +2,7 @@
use yii\helpers\Html;
use yii\widgets\DetailView;
use frontend\components\AccountStatisticWidget;
use common\components\AccountStatisticWidget;
/* @var $this yii\web\View */
/* @var $model common\models\Collection */

View File

@ -1,7 +1,6 @@
<?php
use frontend\components\TransferTotalWidget;
use frontend\components\AccountStatisticWidget;
use common\components\AccountStatisticWidget;
?>
<?php

View File

@ -48,7 +48,6 @@ use kartik\widgets\DateTimePicker;
<?= $form->field($model, 'id_account')->dropDownList($accountOptions) ?>
</div>
<div class="col-md-4">
<?php //echo $form->field($model, 'type')->dropDownList( ['' =>'Mind']+ Transfer::types()) ?>
<?php echo $form->field($model, 'types')->checkboxList( Transfer::types()) ?>
</div>
</div>