380 lines
11 KiB
PHP
380 lines
11 KiB
PHP
<?php
|
|
|
|
namespace frontend\controllers;
|
|
|
|
use common\components\DateUtil;
|
|
use frontend\models\AccountstateSearchToday;
|
|
use /** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
|
|
Yii;
|
|
use common\models\AccountState;
|
|
use frontend\models\AccountstateSearch;
|
|
use yii\web\Controller;
|
|
use yii\web\NotFoundHttpException;
|
|
use common\models\Account;
|
|
use common\components\DailyListing;
|
|
use common\models\User;
|
|
use common\components\accountstate\AccountStateMail;
|
|
use common\models\Transfer;
|
|
use common\components\Helper;
|
|
use common\components\MpdfUtil;
|
|
|
|
/**
|
|
* AccountStateController implements the CRUD actions for AccountState model.
|
|
*/
|
|
class AccountStateController extends Controller {
|
|
public function behaviors() {
|
|
return [
|
|
'access' => [
|
|
'class' => \yii\filters\AccessControl::className (),
|
|
'only' => [
|
|
'index',
|
|
'open',
|
|
'close',
|
|
'view' ,
|
|
'mixed'
|
|
],
|
|
'rules' => [
|
|
// allow authenticated users
|
|
[
|
|
'allow' => true,
|
|
'roles' => [
|
|
'@'
|
|
]
|
|
]
|
|
]
|
|
]
|
|
// everything else is denied
|
|
|
|
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Lists all AccountState models.
|
|
* @return mixed
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public function actionIndex() {
|
|
|
|
if ( Helper::isReceptionAccountStateIndexToday()){
|
|
throw new NotFoundHttpException();
|
|
}
|
|
|
|
$searchModel = new AccountstateSearch ();
|
|
|
|
$searchModel->accounts = Account::read ();
|
|
$searchModel->users = User::read ();
|
|
|
|
$dataProvider = $searchModel->search ( Yii::$app->request->queryParams );
|
|
|
|
return $this->render ( 'index', [
|
|
'searchModel' => $searchModel,
|
|
'dataProvider' => $dataProvider
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Lists all AccountState models.
|
|
* @return mixed
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public function actionToday() {
|
|
|
|
$searchModel = new AccountstateSearchToday();
|
|
|
|
$dataProvider = $searchModel->search ( Yii::$app->request->queryParams );
|
|
|
|
return $this->render ( 'today', [
|
|
'searchModel' => $searchModel,
|
|
'dataProvider' => $dataProvider
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Creates a new AccountState model.
|
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function actionOpen() {
|
|
$lastStates = AccountState::readLastForUser ( AccountState::TYPE_CLOSE );
|
|
$lastStates = AccountState::modelsToArray ( $lastStates );
|
|
|
|
|
|
$model = new AccountState ();
|
|
$model->type = AccountState::TYPE_OPEN;
|
|
$model->id_user = Yii::$app->user->id;
|
|
$model->id_account = Account::readDefault ();
|
|
|
|
$account = Account::readDefaultObject();
|
|
|
|
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) {
|
|
// return $this->redirect(['view', 'id' => $model->id_account_state]);
|
|
|
|
|
|
if ( Helper::isAccountStateOpenSendMail() ){
|
|
$mail = new AccountStateMail(['model' => $model,'controller' => $this]);
|
|
$mail->sednMail();
|
|
}
|
|
|
|
$redirectTo = 'index';
|
|
|
|
if ( Helper::isReceptionAccountStateIndexToday() ){
|
|
$redirectTo = 'today';
|
|
}
|
|
|
|
return $this->redirect ( [
|
|
$redirectTo
|
|
] );
|
|
} else {
|
|
|
|
$accounts = Account::read ();
|
|
|
|
return $this->render ( 'open', [
|
|
'model' => $model,
|
|
'accounts' => $accounts,
|
|
'lastStates' => $lastStates,
|
|
'account' => $account
|
|
] );
|
|
}
|
|
}
|
|
/**
|
|
* Creates a new AccountState model.
|
|
* If creation is successful, the browser will be redirected to the 'view' page.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function actionClose() {
|
|
$lastStates = AccountState::readLastForUser ( AccountState::TYPE_OPEN );
|
|
$lastStates = AccountState::modelsToArray ( $lastStates );
|
|
$model = new AccountState ();
|
|
$model->type = AccountState::TYPE_CLOSE;
|
|
$model->id_user = Yii::$app->user->id;
|
|
$model->id_account = Account::readDefault ();
|
|
$account = Account::readDefaultObject();
|
|
|
|
$lastCassaState = AccountState::readLast(null,null, Account::readDefault());
|
|
|
|
|
|
|
|
|
|
if ( Helper::isAccountStateClosePreloadMoney()){
|
|
$model->money = $this->readCassaClose();
|
|
}
|
|
|
|
|
|
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) {
|
|
|
|
if ( Helper::isAccountStateCloseSendMail()){
|
|
$mail = new AccountStateMail(['model' => $model,'controller' => $this]);
|
|
$mail->sednMail();
|
|
}
|
|
|
|
$redirectTo = 'index';
|
|
|
|
if ( Helper::isReceptionAccountStateIndexToday() ){
|
|
$redirectTo = 'today';
|
|
}
|
|
|
|
|
|
return $this->redirect ( [
|
|
$redirectTo
|
|
] );
|
|
// return $this->redirect(['view', 'id' => $model->id_account_state]);
|
|
} else {
|
|
|
|
$accounts = Account::read ();
|
|
|
|
return $this->render ( 'close', [
|
|
'model' => $model,
|
|
'accounts' => $accounts,
|
|
'account' => $account,
|
|
'lastStates' => $lastStates ,
|
|
'lastCassaState' => $lastCassaState,
|
|
] );
|
|
}
|
|
}
|
|
|
|
protected function readCassaClose(){
|
|
$total = 0;
|
|
$openDate = null;
|
|
$cassaOpen = AccountState::readLast(AccountState::TYPE_OPEN,\Yii::$app->user->id, Account::readDefault());
|
|
if ( isset($cassaOpen )){
|
|
$total += $cassaOpen->money;
|
|
$openDate = $cassaOpen->created_at;
|
|
}
|
|
$total += Transfer::readPaidCash($openDate, date('Y-m-d H:i:s'), \Yii::$app->user->id);
|
|
return $total;
|
|
}
|
|
|
|
/**
|
|
* Finds the AccountState model based on its primary key value.
|
|
* If the model is not found, a 404 HTTP exception will be thrown.
|
|
*
|
|
* @param integer $id
|
|
* @return AccountState the loaded model
|
|
* @throws NotFoundHttpException if the model cannot be found
|
|
*/
|
|
protected function findModel($id) {
|
|
$query = AccountState::find();
|
|
$query->andWhere(['id_account_state'=> $id]);
|
|
if ( Helper::isReceptionAccountStateIndexToday()){
|
|
$query->andWhere(['id_user'=>\Yii::$app->user->id]);
|
|
$query->andWhere(['>=','account_state.created_at', DateUtil::formatDateUtc(DateUtil::todayStart()) ]);
|
|
}
|
|
$model = $query->one();
|
|
if ( $model !== null ) {
|
|
return $model;
|
|
} else {
|
|
throw new NotFoundHttpException ( 'The requested page does not exist.' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes an existing AccountState model.
|
|
* If deletion is successful, the browser will be redirected to the 'index' page.
|
|
*
|
|
* @param integer $id
|
|
* @return mixed
|
|
*/
|
|
public function actionDelete($id) {
|
|
$model = $this->findModel ( $id );
|
|
$delete = true;
|
|
if ( $model->isTypeOpen() ){
|
|
$closeStates = AccountState::find()->andWhere(['prev_state' => $model->id_account_state])->all();
|
|
if ( count($closeStates) > 0){
|
|
// throw new \Exception("Nem lehet törölni a nyitást, mert van kapcsolódó zárás!");
|
|
\Yii::$app->session->setFlash ( 'error', "Nem lehet törölni a nyitást, mert van kapcsolódó zárás!" );
|
|
$delete = false;
|
|
}
|
|
}
|
|
if ( $delete == true){
|
|
$model->delete();
|
|
\Yii::$app->session->setFlash ( 'success', 'Kassza művelet törölve' );
|
|
}
|
|
return $this->redirect ( Yii::$app->request->referrer );
|
|
}
|
|
/*
|
|
* Displays a single AccountState model.
|
|
* @param integer $id
|
|
* $var common\models\AccountState $accountState
|
|
* @return mixed
|
|
*/
|
|
public function actionView($id) {
|
|
$accountState = $this->findModel ( $id );
|
|
$output = Yii::$app->getRequest ()->getQueryParam ( 'output' );
|
|
$details = null;
|
|
if ($accountState->isTypeClose ()) {
|
|
|
|
$prev = null;
|
|
if ($accountState->type == AccountState::TYPE_CLOSE) {
|
|
if (isset ( $accountState->prev_state )) {
|
|
$prev = AccountState::findOne ( $accountState->prev_state );
|
|
}
|
|
if (isset ( $prev )) {
|
|
$accountState->start_date = $prev->created_at;
|
|
}
|
|
}
|
|
$details = new DailyListing ();
|
|
$details->loadAccountState ( $accountState );
|
|
|
|
$details->readTotalEasy ();
|
|
$details->readTotalDetailed ();
|
|
$details->readTotalMedium ();
|
|
}
|
|
|
|
if ($output == 'pdf') {
|
|
$user = User::findOne(\Yii::$app->user->id);
|
|
$mpdf= MpdfUtil::createMpdfWith6XConstructor('utf-8', 'A4-L');
|
|
$mpdf->useSubstitutions=false;
|
|
$mpdf->simpleTables = true;
|
|
$mpdf->SetHeader( \Yii::$app->params[ "company_name" ] . " - Létrehozva: " .$user->username . ", ".\Yii::$app->formatter->asDatetime(time()) );
|
|
/** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
|
|
$mpdf->setFooter('{PAGENO} / {nb}');
|
|
|
|
$stylesheet = file_get_contents( \Yii::getAlias('@vendor'.'/bower/bootstrap/dist/css/bootstrap.css')); // external css
|
|
$mpdf->WriteHTML($stylesheet,1);
|
|
|
|
|
|
$mpdf->WriteHTML($this->renderPartial("@common/views/account-state/account_state_pdf", [
|
|
'model' => $accountState,
|
|
'details' => $details
|
|
]));
|
|
/** @noinspection SpellCheckingInspection */
|
|
$type = $accountState->isTypeOpen() ? "kassza_nyitas" : "kassza_zaras";
|
|
$dt= "_letrehozva_".date("Ymd_His"). "_" . $user->username;
|
|
$fn= $type .$dt.".pdf";
|
|
$mpdf->Output($fn, 'D');
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
return $this->render ( 'view', [
|
|
'model' => $accountState,
|
|
'details' => $details
|
|
] );
|
|
}
|
|
}
|
|
|
|
|
|
public function actionMixed($id){
|
|
$accountState = $this->findModel ( $id );
|
|
$output = Yii::$app->getRequest ()->getQueryParam ( 'output' );
|
|
$details = null;
|
|
|
|
if ($accountState->isTypeClose ()) {
|
|
|
|
$prev = null;
|
|
if ($accountState->type == AccountState::TYPE_CLOSE) {
|
|
if (isset ( $accountState->prev_state )) {
|
|
$prev = AccountState::findOne ( $accountState->prev_state );
|
|
}
|
|
if (isset ( $prev )) {
|
|
$accountState->start_date = $prev->created_at;
|
|
}
|
|
}
|
|
$details = new DailyListing ();
|
|
$details->loadAccountState ( $accountState );
|
|
|
|
$details->readTotalEasy ();
|
|
$details->readTotalDetailed ();
|
|
$details->readTotalMedium ();
|
|
|
|
}
|
|
|
|
|
|
if ($output == 'pdf') {
|
|
$user = User::findOne(\Yii::$app->user->id);
|
|
$mpdf=new Mpdf('utf-8', 'A4');
|
|
$mpdf->useSubstitutions=false;
|
|
$mpdf->simpleTables = true;
|
|
$mpdf->SetHeader( \Yii::$app->params[ "company_name" ] . " - Létrehozva: " .$user->username . ", ".\Yii::$app->formatter->asDatetime(time()) );
|
|
/** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
|
|
$mpdf->setFooter('{PAGENO} / {nb}');
|
|
|
|
$stylesheet = file_get_contents( \Yii::getAlias('@vendor'.'/bower/bootstrap/dist/css/bootstrap.css')); // external css
|
|
$mpdf->WriteHTML($stylesheet,1);
|
|
|
|
$stylesheet = file_get_contents( \Yii::getAlias('@frontend'.'/web/css/account_close.css')); // external css
|
|
$mpdf->WriteHTML($stylesheet,1);
|
|
|
|
|
|
$mpdf->WriteHTML($this->renderPartial("@frontend/views/account-state/mixed_pdf", [
|
|
'model' => $accountState,
|
|
'details' => $details
|
|
]));
|
|
/** @noinspection SpellCheckingInspection */
|
|
$type = $accountState->isTypeOpen() ? "kassza_nyitas" : "kassza_zaras";
|
|
$dt= "_letrehozva_".date("Ymd_His"). "_" . $user->username;
|
|
$fn= $type .$dt.".pdf";
|
|
$mpdf->Output($fn, 'D');
|
|
exit();
|
|
|
|
}
|
|
return $this->render ( 'mixed', [
|
|
'model' => $accountState,
|
|
'details' => $details
|
|
] );
|
|
}
|
|
}
|