diff --git a/common/models/Collection.php b/common/models/Collection.php index 5705a95..513a602 100644 --- a/common/models/Collection.php +++ b/common/models/Collection.php @@ -24,6 +24,9 @@ use yii\behaviors\TimestampBehavior; */ class Collection extends \common\models\BaseFitnessActiveRecord { + + const TYPE_RECEPTION = 10; + /** * @inheritdoc */ @@ -86,17 +89,17 @@ class Collection extends \common\models\BaseFitnessActiveRecord /** * @param \common\models\User $user the user * */ - public static function readLast($user){ + public static function readLast($user,$types = [Collection::TYPE_RECEPTION]){ $result = null; $query = Collection::find(); if ( isset($user)){ - $query->andWhere($user->id); + $query->andWhere(['id_user' =>$user->id]); } + $query->andFilterWhere([ 'in', 'type' ,$types]); $query->orderBy(['end' => SORT_DESC]); $query->limit(1); - $result = $query->one(); return $result; diff --git a/common/models/CollectionCreate.php b/common/models/CollectionCreate.php index e8e4f5d..0ec535e 100644 --- a/common/models/CollectionCreate.php +++ b/common/models/CollectionCreate.php @@ -27,11 +27,41 @@ class CollectionCreate extends \common\models\Collection public $lastCollection; public $accounts; + public $accountMap; public $account; public $totals; - + public $timestampEnd; + public $timestampStart; + public function rules(){ + return [ + ['end','required'], + [[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ], + [[ 'end' ], 'validateEndDate' ] + ]; + } + + public function validateEndDate($attribute,$params){ + if ( isset($this->lastCollection)){ + if ( strtotime( $this->lastCollection->end ) >= strtotime( $this->timestampEnd ) ){ + $this->addError($attribute,Yii::t('frontend/collection' ,'End date is invalid') ); + } + } + } + + public function beforeSave($insert){ + if (parent::beforeSave($insert)) { + $this->id_user = Yii::$app->user->id; + $this->created_by = Yii::$app->user->id; + $paidAt = Transfer::mkPaidAtTotals($this->timestampStart, $this->timestampEnd, $this->user->id, null, $this->account->id_account, $this->accounts, $this->accountMap); + $this->money = $paidAt['total']; + return true; + } else { + return false; + } + } + } diff --git a/common/models/Transfer.php b/common/models/Transfer.php index 79a3d0c..673325e 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -503,25 +503,18 @@ class Transfer extends \common\models\BaseFitnessActiveRecord if ( $mode == 'created_at'){ - $query->andFilterWhere([ '>=', 'transfer.created_at' , $start] ); - $query->andFilterWhere([ '<' , 'transfer.created_at' , $end ] ); + self::inInterval($query, 'transfer.created_at', $start, $end); }else if ( $mode == 'paid_at'){ - $query->andFilterWhere([ '>=', 'transfer.paid_at' , $start ] ); - $query->andFilterWhere([ '<' , 'transfer.paid_at' , $end ] ); + self::inInterval($query, 'transfer.paid_at' , $start, $end); }else if ( $mode == 'created_at_not_paid'){ - $query->andFilterWhere([ "transfer.status" => Transfer::STATUS_NOT_PAID ] ); - $query->andFilterWhere([ '>=', 'transfer.created_at' , $start ] ); - $query->andFilterWhere([ '<' , 'transfer.created_at' , $end ] ); + self::notPaid($query, 'transfer.paid_at', $start, $end); + self::inInterval($query, 'transfer.created_at', $start, $end); }else if ( $mode == 'created_at_paid'){ - $query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] ); - $query->andFilterWhere([ '>=', 'transfer.created_at' , $start ] ); - $query->andFilterWhere([ '<' , 'transfer.created_at' , $end ] ); + self::inInterval($query, 'transfer.created_at', $start, $end); + self::inInterval($query, 'transfer.paid_at', $start, $end); }else if ( $mode == 'paid_at_not_created_at'){ - $query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] ); - $query->andFilterWhere([ '>=', 'transfer.paid_at' , $start ] ); - $query->andFilterWhere([ '<' , 'transfer.paid_at' , $end ] ); - - $query->andFilterWhere( ['or', [ '<', 'transfer.created_at' , isset( $start ) ? $start : '1900-01-01' ] ,[ '>=' , 'transfer.created_at' , $end ] ] ); + self::inInterval($query, 'transfer.paid_at' , $start, $end); + self::notInInterval($query, 'transfer.created_at', $start, $end); } $query->groupBy('transfer.id_account'); @@ -531,6 +524,18 @@ class Transfer extends \common\models\BaseFitnessActiveRecord } + 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 ] ); + } + diff --git a/frontend/components/TransferTotalWidget.php b/frontend/components/TransferTotalWidget.php index f96f748..b76a6a0 100644 --- a/frontend/components/TransferTotalWidget.php +++ b/frontend/components/TransferTotalWidget.php @@ -18,6 +18,10 @@ class TransferTotalWidget extends Widget{ if ( !isset($this->options['totalHeading'] ) ){ $this->options['totalHeading'] = 'Összesen'; } + + if ( !isset($this->options['panel-type'] ) ){ + $this->options['panel-type'] = 'panel-info'; + } } diff --git a/frontend/controllers/CollectionController.php b/frontend/controllers/CollectionController.php index 37bc4e3..8b6d973 100644 --- a/frontend/controllers/CollectionController.php +++ b/frontend/controllers/CollectionController.php @@ -73,24 +73,26 @@ class CollectionController extends Controller $user = User::findOne(Yii::$app->user->id); -// $accounts = Account::find()->orderBy("name asc")->all(); - $model->accounts = Account::read(); $model->lastCollection = Collection::readLast($user); $model->id_user = $user->id; - $model->id_account = Account::readDefault(); + $model->id_account = $id_account; $model->account = Account::readOne($model->id_account); - $accountMap = ArrayHelper::map( $model->accounts ,'id_account','name' ); + $model->accountMap = ArrayHelper::map( $model->accounts ,'id_account','name' ); - $model->start = isset($model->lastCollection) ? $model->lastCollection->end :null; - $model->end = date("Y-m-d H:i:s"); + $model->timestampStart = isset($model->lastCollection) ? $model->lastCollection->end :null; + $model->timestampEnd = date("Y-m-d H:i:s"); + + $model->start = isset($model->timestampStart) ? Yii::$app->formatter->asDatetime( $model->timestampStart ) : ''; + $model->end = isset($model->timestampEnd) ? Yii::$app->formatter->asDatetime( $model->timestampEnd ): ''; + + $model->type = Collection::TYPE_RECEPTION; - $model->totals = Transfer::mkTotals($model->start, $model->end, $model->user->id, null, $model->account->id_account, $model->accounts, $accountMap); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ( $model->load(Yii::$app->request->post()) && $model->save() ) { return $this->redirect(['view', 'id' => $model->id_collection]); } else { + $model->totals = Transfer::mkTotals($model->timestampStart, $model->timestampEnd, $model->user->id, null, $model->account->id_account, $model->accounts, $model->accountMap); return $this->render('create', [ 'model' => $model, ]); diff --git a/frontend/models/TransferSearch.php b/frontend/models/TransferSearch.php index 9ef1e5c..a2b48a9 100644 --- a/frontend/models/TransferSearch.php +++ b/frontend/models/TransferSearch.php @@ -28,6 +28,7 @@ class TransferSearch extends Transfer public $totalsCreatedAtNotPaid= ['total' => 0, 'accounts' =>[]]; public $totalsCreatedAtPaid= ['total' => 0, 'accounts' =>[]]; public $totalsPaidAt= ['total' => 0, 'accounts' =>[]]; + public $totalsPaidAtNotCreatedAt= ['total' => 0, 'accounts' =>[]]; public $accounts; @@ -95,173 +96,6 @@ class TransferSearch extends Transfer return $dataProvider; } - - protected function mkTotalQuery($mode){ - - $query = new Query(); - - $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->andFilterWhere([ - 'id_account' => $this->id_account, - 'type' => $this->type, - ]); - - $query->andWhere(['id_user' => Yii::$app->user->id]); - - $query->andFilterWhere(['in' ,'type', $this->types]); - - - if ( $mode == 'created_at'){ - $query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] ); - $query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] ); - }else if ( $mode == 'paid_at'){ - $query->andFilterWhere([ '>=', 'transfer.paid_at' , $this->timestampStart ] ); - $query->andFilterWhere([ '<' , 'transfer.paid_at' , $this->timestampEnd ] ); - }else if ( $mode == 'created_at_not_paid'){ - $query->andFilterWhere([ "transfer.status" => Transfer::STATUS_NOT_PAID ] ); - $query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] ); - $query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] ); - }else if ( $mode == 'created_at_paid'){ - $query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] ); - $query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] ); - $query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] ); - } - - $query->groupBy('transfer.id_account'); - - return $query; - - - } - - protected function mkTransferTotalsWithZeroValue( ){ - $result = []; - - foreach ( $this->accounts as $a ){ - $accountTotal = [ - 'id_account' => $a->id_account, - 'label' => $a->name, - 'money' => 0 - ]; - - $result[] = $accountTotal; - } - return $result; - } - - - - protected function mkTotalsResult($queryResult,$accountMap){ - - $totals = []; - $totals['total'] = 0; - - $totals['accounts'] = []; - - foreach ($queryResult as $item){ - $account = ""; - if ( array_key_exists($item['account'], $accountMap)){ - $account = $accountMap[$item['account']]; - } - - $accountTotal = [ - 'id_account' => $item['account'], - 'label' => $account, - 'money' => $item['money'] - ]; - $totals['accounts'][] = $accountTotal; - $totals['total'] += $item['money']; - } - return $totals; - } - - protected function findByAccountInQueryResult($queryResult, $account){ - $result = null; - foreach ($queryResult as $item){ - if( $item['account'] == $account->id_account ){ - $result = $item; - } - } - return $result; - } - - protected function mkTotalsResultWithAllAvailableAccount($queryResult,$accountMap){ - - $totals = []; - $totals['total'] = 0; - - $totals['accounts'] = []; - - foreach ($this->accounts as $account){ - if ( isset($this->id_account) && is_numeric($this->id_account) && $this->id_account != $account->id_account ){ - continue ; - } - $accountTotal = [ - 'id_account' => $account->id_account, - 'label' => $account->name, - 'money' => 0, - ]; - - $item = $this->findByAccountInQueryResult($queryResult, $account); - - if ( isset($item)){ - $accountTotal['money'] = $item['money']; - } - - $totals['accounts'][] = $accountTotal; - $totals['total'] += $accountTotal['money']; - } - return $totals; - } - - - public function mkPaidAtTotals($accountMap){ - $query = $this->mkTotalQuery('paid_at'); - $command = $query->createCommand(); - $result = $command->queryAll(); - - $paidAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap); - - return $paidAtTotals; - } - - public function mkCreatedAtTotals($accountMap){ - $query = $this->mkTotalQuery('created_at'); - $command = $query->createCommand(); - $result = $command->queryAll(); - - $createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap); - - return $createdAtTotals; - } - - public function mkCreatedAtNotPaidTotals($accountMap){ - $query = $this->mkTotalQuery('created_at_not_paid'); - $command = $query->createCommand(); - $result = $command->queryAll(); - - $createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap); - - return $createdAtTotals; - } - - public function mkCreatedAtPaidTotals($accountMap){ - $query = $this->mkTotalQuery('created_at_paid'); - $command = $query->createCommand(); - $result = $command->queryAll(); - - $createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap); - - return $createdAtTotals; - } - - public function totalsTransfers($params){ $accountTotals = []; $fullTotal = [ @@ -273,13 +107,19 @@ class TransferSearch extends Transfer return; } + + $accounts = Account::find()->orderBy("name asc")->all(); $accountMap = ArrayHelper::map( $accounts ,'id_account','name' ); + $idUser = Yii::$app->user->id; - $this->totalsCreatedAt = $this->mkCreatedAtTotals($accountMap); - $this->totalsPaidAt = $this->mkPaidAtTotals($accountMap); - $this->totalsCreatedAtPaid = $this->mkCreatedAtPaidTotals($accountMap); - $this->totalsCreatedAtNotPaid = $this->mkCreatedAtNotPaidTotals($accountMap); + $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']; } diff --git a/frontend/views/collection/_form.php b/frontend/views/collection/_form.php index 107f57b..7f5dd26 100644 --- a/frontend/views/collection/_form.php +++ b/frontend/views/collection/_form.php @@ -19,6 +19,8 @@ use frontend\components\HtmlHelper; + field($model, 'end')->hiddenInput()->label(false) ?> +
+ = Html::a(Yii::t('frontend/collection', 'Refresh'), ['create', 'id_account' => $model->id_account], ['class' => 'btn btn-primary']) ?> + = Html::a(Yii::t('frontend/collection', 'Select another account'), ['select-account' ], ['class' => 'btn btn-primary']) ?> +
- [ - 'statistic' => $model->totals['paid_at'], - 'panelHeading' => 'Fizetve' , - ] - ]); +- = Html::a(Yii::t('frontend/collection', 'Update'), ['update', 'id' => $model->id_collection], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('frontend/collection', 'Delete'), ['delete', 'id' => $model->id_collection], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => Yii::t('frontend/collection', 'Are you sure you want to delete this item?'), - 'method' => 'post', - ], - ]) ?> -
= 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', ], ]) ?> diff --git a/frontend/views/common/_transfer_total.php b/frontend/views/common/_transfer_total.php index 4083a72..296dd45 100644 --- a/frontend/views/common/_transfer_total.php +++ b/frontend/views/common/_transfer_total.php @@ -1,10 +1,8 @@ -