Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-hr/controllers/
Upload File :
Current File : /var/www/html/dyna-hr/controllers/LeavesettingsController.php

<?php

namespace app\controllers;

use Yii;
use app\models\LeaveSettings;
use app\models\LeaveSettingsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\models\ParamList;

/**
 * LeavesettingsController implements the CRUD actions for LeaveSettings model.
 */
class LeavesettingsController extends Controller {

    public $menu_id = 82;

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all LeaveSettings models.
     * @return mixed
     */
    public function actionIndex($selectedyear = '', $staffname = '') {
        if (empty($selectedyear))
            $selectedyear = date('Y');

        $query = LeaveSettings::find()
                ->innerJoin('user', 'user.id=leave_settings.user_id')
                ->where(['=', 'year', $selectedyear]);
        if (!empty($staffname))
            $query->andwhere(['LIKE', 'user.name', $staffname]);
        $query->orderBy(['user.name' => SORT_ASC])
                ->groupBy('leave_settings.user_id');
        $model = $query->all();

        $leaveType = [];
        $modelType = ParamList::find()->where(['=', 'cat_id', '58'])->andwhere(['=', 'status', '1'])->andwhere(['!=', 'code', 'unpaid'])->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($modelType)) {
            foreach ($modelType as $rowType) {
                $leaveType[$rowType->code] = $rowType->label;
            }
        }

        return $this->render('index', [
                    'menu_id' => $this->menu_id,
                    'selectedyear' => $selectedyear,
                    'staffname' => $staffname,
                    'model' => $model,
                    'leaveType' => $leaveType,
        ]);
    }

    /**
     * Displays a single LeaveSettings model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', ['model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new LeaveSettings model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new LeaveSettings();
        if ($model->load(Yii::$app->request->post())) {

            if (!empty($_POST['leavetype'])) {
                foreach ($_POST['leavetype'] as $leavecode) {
                    $modelLeave = new LeaveSettings();
                    $modelLeave->user_id = $model->user_id;
                    $modelLeave->leave_type = $leavecode;
                    $modelLeave->year = $model->year;
                    $modelLeave->days = $_POST[$leavecode . '_day'];
                    $modelLeave->created_dt = date("Y-m-d H:i:s");
                    $modelLeave->created_by = Yii::$app->user->identity->id;
                    $modelLeave->save(false);
                }
            }

            $this->getView()->registerJs("swal.fire({
                position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,
                }).then(function(result){window.location = '" . Url::to(['leavesettings/index', 'selectedyear' => $model->year
                    ]) . "';});");
        }

        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing LeaveSettings model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($user_id, $year) {
        $model = new LeaveSettings();
        $model->user_id = $user_id;
        $model->year = $year;
        if ($model->load(Yii::$app->request->post())) {

            if (!empty($_POST['leavetype'])) {

                LeaveSettings::deleteAll(['user_id' => $user_id, 'year' => $year]);

                foreach ($_POST['leavetype'] as $leavecode) {
                    $modelLeave = new LeaveSettings();
                    $modelLeave->user_id = $model->user_id;
                    $modelLeave->leave_type = $leavecode;
                    $modelLeave->year = $model->year;
                    $modelLeave->days = $_POST[$leavecode . '_day'];
                    $modelLeave->created_dt = date("Y-m-d H:i:s");
                    $modelLeave->created_by = Yii::$app->user->identity->id;
                    $modelLeave->save(false);
                }
            }

            $this->getView()->registerJs("swal.fire({
                position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,
                }).then(function(result){window.location = '" . Url::to(['leavesettings/index', 'selectedyear' => $model->year]) . "';});");
        }
        return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Deletes an existing LeaveSettings model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the LeaveSettings model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return LeaveSettings the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = LeaveSettings::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

}