| Current Path : /var/www/html/hr/controllers/ |
| Current File : /var/www/html/hr/controllers/LeaveapplicationController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\LeaveApplication;
use app\models\LeaveApplicationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use yii\helpers\Url;
use app\models\MenuAccess;
use yii\helpers\ArrayHelper;
use app\models\Log;
use app\models\LogSearch;
use yii\web\UploadedFile;
use app\models\User;
use app\models\ParamList;
use yii\helpers\Html;
use app\models\PublicHolidays;
/**
* LeaveapplicationController implements the CRUD actions for LeaveApplication model.
*/
class LeaveapplicationController extends Controller {
public $menu_id = 84;
public $menu_id2 = 83;
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['GET'],
],
],
];
}
/**
* Lists all LeaveApplication models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new LeaveApplicationSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionIndex2() {
$searchModel = new LeaveApplicationSearch();
$dataProvider = $searchModel->search2(Yii::$app->request->queryParams);
return $this->render('index2', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id2]);
}
/**
* Displays a single LeaveApplication model.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id, $uuid) {
return $this->render('view', ['model' => $this->findModel($id, $uuid),]);
}
/**
* Creates a new LeaveApplication model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new LeaveApplication();
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->uuid = Uuid::uuid4();
$model->status = 'pending';
$model->user_id = Yii::$app->user->identity->id;
if ($model->load(Yii::$app->request->post())) {
$model->attachment = UploadedFile::getInstance($model, 'attachment');
if (!empty($model->attachment)) {
$file_name = date("YmdHis") . '.' . $model->attachment->extension;
if ($model->attachment->saveAs('uploads/leave_application/' . $file_name))
$model->attachment = $file_name;
}
if ($model->save()) {
//send email
if (($modelApprover = User::findOne(Yii::$app->user->identity->id)) !== null) {
$approver_id = $modelApprover->staff_approver;
$staff_name = $modelApprover->name;
if (($modelApprover2 = User::findOne($approver_id)) !== null) {
$approver_name = $modelApprover2->name;
$approver_email = $modelApprover2->email;
$ParamList = new ParamList();
$email_content = '<html>
<body>
<div style="font-family: \'Century Gothic\', CenturyGothic, AppleGothic, sans-serif; font-size: 14px;background-color:rgb(234,234,234);padding:2em 0;">
<div style="width:70%;background-color:white;margin:0 auto;padding:5em;border:1px solid #ccc;">
<p>Hi ' . $approver_name . ',</p>
<p>' . $staff_name . ' have apply for leave from HR System.</p>
<p>
Leave Type : ' . $ParamList->getlabel(58, $model->leave_type) . '<br>
Reason : ' . $model->reason . '<br>
Start Date : ' . date("d M Y", strtotime($model->start_dt)) . '<br>
End Date : ' . date("d M Y", strtotime($model->end_dt)) . '<br>
Leave Length : ' . $ParamList->getlabel(60, $model->leave_length) . '<br>
Total Days : ' . $model->total_days . ' day(s)
</p>
<p>
<a href="' . Url::base(true) . '/index.php?r=leaveapplication/approval&uuid=' . $model->uuid . '&approved_by=' . $approver_id . '">Please click here to approve / reject ' . $staff_name . ' leave application</a>
</p>
<p>- Admin -</p>
</div>
</div>
</body>
</html>';
// Yii::$app->mailer->compose()
// ->setFrom(Yii::$app->params['senderEmail'])
// ->setTo($approver_email)
// ->setSubject('HR System Staff Leave Application')
// ->setHtmlBody($email_content)
// ->send();
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($approver_email, 'HR System Staff Leave Application', $email_content, $headers);
}
}
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$this->getView()->registerJs("swal.fire({
position: 'center-center',
type: 'success',
title: 'Leave application successfully send to approver',
showConfirmButton: false,
timer: 3000,
}).then(function(result){window.location = '" . Url::to(['leaveapplication/index']) . "';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function actionUpdatestatus($id, $status, $approved_by) {
if (($model = LeaveApplication::findOne($id)) !== null) {
$model->status = $status;
$model->approved_dt = date("Y-m-d H:i:s");
$model->approved_by = $approved_by;
if ($model->save(false)) {
$ParamList = new ParamList();
//send email
if (($modelUser = User::findOne($model->user_id)) !== null) {
$leave_dt = '';
if ($model->start_dt == $model->end_dt)
$leave_dt = 'on ' . date("d M Y", strtotime($model->start_dt));
else
$leave_dt = 'from ' . date("d M Y", strtotime($model->start_dt)) . ' until ' . date("d M Y", strtotime($model->end_dt));
$email_content = '<html>
<body>
<div style="font-family: \'Century Gothic\', CenturyGothic, AppleGothic, sans-serif; font-size: 14px;background-color:rgb(234,234,234);padding:2em 0;">
<div style="width:70%;background-color:white;margin:0 auto;padding:5em;border:1px solid #ccc;">
<p>Hi ' . $modelUser->name . ',</p>
<p>Your application for leave ' . $leave_dt . ' have been ' . strtolower($ParamList->getlabel(59, $status)) . '</p>
<p>- Admin -</p>
</div>
</div>
</body>
</html>';
/*Yii::$app->mailer->compose()
->setFrom(Yii::$app->params['senderEmail'])
->setTo($modelUser->email)
->setSubject('HR System Staff Leave Application')
->setHtmlBody($email_content)
->send();*/
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($modelUser->email, 'HR System Staff Leave Application', $email_content, $headers);
}
return 'success';
}
}
}
public function actionApproval($uuid, $approved_by) {
if (($model = LeaveApplication::findOne(['uuid' => $uuid])) !== null) {
$User = new User();
$ParamList = new ParamList();
$output = '<html>
<head>
<title>' . Yii::$app->params['systemName'] . '</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.table th{width:50%;text-align:right;white-space:nowrap;}
</style>
</head>
<body>
<div class="container p-5">
<div class="card pb-3" style="width:700px;margin:0 auto;">
<div class="text-center pt-4 pb-4">
<h5>BLUNET HR SYSTEM</h5>
<h6><b>Leave Application</b></h6>
</div>
<table class="table table-striped">
<tr><th>Staff Name</th><td>' . $User->getusername($model->user_id) . '</td></tr>
<tr><th>Leave Type</th><td>' . $ParamList->getlabel(58, $model->leave_type) . '</td></tr>
<tr><th>Start Date</th><td>' . date("d M Y", strtotime($model->start_dt)) . '</td></tr>
<tr><th>End Date</th><td>' . date("d M Y", strtotime($model->end_dt)) . '</td></tr>
<tr><th>Leave Length</th><td>' . $ParamList->getlabel(60, $model->leave_length) . '</td></tr>
<tr><th>Total Days</th><td>' . $model->total_days . '</td></tr>
<tr><th>Reason</th><td>' . $model->reason . '</td></tr>
<tr><th>Attachment</th><td>' . Html::a($model->attachment, 'uploads/leave_application/' . $model->attachment, ['target' => '_blank']) . '</td></tr>
<tr><th>Apply Date</th><td>' . date("d M Y", strtotime($model->created_dt)) . '</td></tr>
<tr><th>Status</th><td>' . $ParamList->getlabel(59, $model->status) . '</td></tr>
</table>';
if ($model->status == 'pending') {
$output .= '<center>
<button class="btn btn-success" onclick="updatestatus(' . $model->id . ',\'approve\',' . $approved_by . ')">Approve</button>
<button class="btn btn-danger" onclick="updatestatus(' . $model->id . ',\'reject\',' . $approved_by . ')">Reject</button>
</center>';
}
$output .= '</div>
</div>
<script>
function updatestatus(id, status, approved_by) {
swal.fire({
title: "Are you sure to " + status + " this leave application?",
text: "You won\'t be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, proceed!"
}).then((result) => {
if (result.value) {
swal.fire({
title: "Loading",
text: "Please Wait",
allowOutsideClick: false,
showConfirmButton: false,
onBeforeOpen: () => {
swal.showLoading()
},
});
$.ajax({
url: "index.php?r=leaveapplication/updatestatus",
method: "GET",
data: {"id": id, "status": status, "approved_by": approved_by},
success: function (response) {
if (response === "success") {
swal.close();
swal.fire({
position: "center-center",
type: "success",
title: "Leave application have been " + status,
showConfirmButton: false,
timer: 1500, }).then(function (result) {
location.reload();
});
}
},
});
}
});
}
</script>
</body>
</html>';
echo $output;
die();
}
}
public function actionCalculatedays($start_dt, $end_dt) {
//deduct end - start
$start = new \DateTime($start_dt);
$end = new \DateTime($end_dt);
$end->modify('+1 day');
$interval = $end->diff($start);
$days = $interval->days;
$period = new \DatePeriod($start, new \DateInterval('P1D'), $end);
//$holidays = array('2012-09-07');
//$holidays = ArrayHelper::map(PublicHolidays::find()->where(['cat_id' => $cat_id, 'status' => 1])->all(), 'code', 'label');
foreach ($period as $dt) {
$curr = $dt->format('D');
//escape weekend
if ($curr == 'Sat' || $curr == 'Sun') {
$days--;
}
$modelHolidays = PublicHolidays::find()
->andFilterWhere(['<=', 'start_dt', $dt->format('Y-m-d')])
->andFilterWhere(['>=', 'end_dt', $dt->format('Y-m-d')])->all();
if (!empty($modelHolidays))
$days--;
//deduct public holiday
//else if (in_array($dt->format('Y-m-d'), $holidays)) {
// $days--;
//}
}
return $days;
}
/**
* Updates an existing LeaveApplication model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id, $uuid) {
$model = $this->findModel($id, $uuid);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id, 'uuid' => $model->uuid]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing LeaveApplication model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id, $uuid) {
$this->findModel($id, $uuid)->delete();
return $this->redirect(['index']);
}
/**
* Finds the LeaveApplication model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @param string $uuid
* @return LeaveApplication the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id, $uuid) {
if (($model = LeaveApplication::findOne(['id' => $id, 'uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}