Example #1
0
 /**
  * @param $attribute
  * @param $params
  * Kiem tra gio xin trong don co phep khong
  */
 public function hoursAllow($attribute, $params)
 {
     //Neu manager or hrm ko ok thi ko can kiem tra nua.
     //        if($this->manager_ok != 1 || $this->hrm_ok != 1)
     //            return true;
     $reason = ReasonApplication::findOne($this->reason);
     //thoi gian cho phep cua kieu nghi phep do tinh trong 1 lan nghi phep
     if (isset($reason->time_allow) && $reason->type == 1 && $this->hours_off > $reason->time_allow) {
         $this->addError($attribute, "The maximum time to leave is " . $reason->time_allow . "h");
     }
     //thoi gian cho phep cua kieu nghi phep do tinh trong 1 nam nghi phep
     //nghi trong truong hop khong co giay to
     if (isset($reason->time_allow) && $reason->type == 12) {
         $hours = Yii::$app->db->createCommand("SELECT SUM(hours_off) AS hours FROM `application` WHERE (`user_id`=" . $this->user_id . ") AND (`manager_ok`=1) AND (`hrm_ok`=1) AND (`reason`=3) AND year(from_date) = " . date('Y', strtotime($this->from_date)))->queryOne();
         if ($hours['hours'] == '') {
             $hours['hours'] = 0;
         }
         if ($reason->time_allow < (int) $hours['hours'] + $this->hours_off) {
             $this->addError($attribute, "The maximum time to leave is " . $reason->time_allow . "h. You leave " . $hours['hours'] . "h.");
         }
     }
     //thoi gian nghi phep hang nam.
     if ($this->reason == 13) {
         $hours = Yii::$app->db->createCommand("SELECT SUM(hours_off) AS hours FROM `application` WHERE (`user_id`=" . $this->user_id . ") AND (`manager_ok`=1) AND (`hrm_ok`=1) AND (`reason`=13) AND year(from_date) = " . date('Y', strtotime($this->from_date)))->queryOne();
         $userYear = UserDate::findOne(['user_id' => $this->user_id, 'year' => date('Y', strtotime($this->from_date))]);
         if ($hours['hours'] == '') {
             $hours['hours'] = 0;
         }
         if ($userYear === null) {
             $this->addError($attribute, "You don't set time leave in " . date('Y', strtotime($this->from_date)) . ". Please contact to human resource manager");
             return;
         }
         if ($hours['hours'] + $this->hours_off > $userYear->entitlement) {
             $this->addError($attribute, "The maximum time to leave is " . $userYear->entitlement . "h in " . $userYear->year . " . You leave " . $hours['hours'] . "h.");
         }
     }
     //Nghi bu tru vao thoi gian lam them.
     //11: nghi bu cho gio lam them
     //12: lam them gio
     //14: chuyen gio lam them thanh tien
     if ($this->reason == 11 || $this->reason == 14) {
         $timeover = Yii::$app->db->createCommand("SELECT SUM(hours_off) AS hours FROM `application` WHERE (`user_id`=" . $this->user_id . ") AND (`manager_ok`=1) AND (`hrm_ok`=1) AND (`reason`=12)")->queryOne();
         $timeLeave = Yii::$app->db->createCommand("SELECT SUM(hours_off) AS hours FROM `application` WHERE (`user_id`=" . $this->user_id . ") AND (`manager_ok`=1) AND (`hrm_ok`=1) AND (`reason`=11)")->queryOne();
         $timeMoney = Yii::$app->db->createCommand("SELECT SUM(hours_off) AS hours FROM `application` WHERE (`user_id`=" . $this->user_id . ") AND (`manager_ok`=1) AND (`hrm_ok`=1) AND (`reason`=14)")->queryOne();
         if ($timeover['hours'] == '') {
             $timeover['hours'] = 0;
         }
         if ($timeLeave['hours'] == '') {
             $timeLeave['hours'] = 0;
         }
         if ($timeMoney['hours'] == '') {
             $timeMoney['hours'] = 0;
         }
         if ($timeover['hours'] - $timeLeave['hours'] - $timeMoney['hours'] - $this->hours_off < 0) {
             $this->addError($attribute, "The overtime remaining is " . ($timeover['hours'] - $timeLeave['hours'] - $timeMoney['hours']) . "h. ");
         }
     }
 }
 public function actionApplicationOfType($id, $user_id = 0)
 {
     $model = new Application();
     $model->type = $id;
     $reason = array();
     if ($id == 0) {
         //Don xin nghi phep (co tru phep va khong tru phep)
         $reason = ReasonApplication::find()->where(['type_id' => -2])->orWhere(['type_id' => 0])->orderBy(['name' => SORT_ASC])->all();
     } else {
         if ($id == -1) {
             //Don xin nghi bu
             $reason = ReasonApplication::find()->where(['type_id' => -1])->orderBy(['name' => SORT_ASC])->all();
         } else {
             if ($id == 1) {
                 //Don xin lam bu
                 $reason = ReasonApplication::find()->where(['type_id' => 1])->orderBy(['name' => SORT_ASC])->all();
             } else {
                 if ($id == -11) {
                     //Don xin chuyen gio overtime thanh tien
                     $reason = ReasonApplication::find()->where(['type_id' => -11])->orderBy(['name' => SORT_ASC])->all();
                 } else {
                     throw new NotFoundHttpException("This type of application does not exist");
                 }
             }
         }
     }
     if ($model->load(Yii::$app->request->post())) {
         //neu la don xin chuyen thoi gian lam them qua tien thi gan manager = 1;
         $r = ReasonApplication::findOne($model->reason);
         if ($r->type_id == -11) {
             $model->manager_ok = 1;
             $model->manager_readed = 1;
         }
         $model->user_id = Yii::$app->user->id;
         //Thiet lap trong truong hop hrm lam don xin nghi
         if (Yii::$app->user->can('hrm')) {
             $model->hrm_readed = 1;
             $model->hrm_ok = 1;
         }
         //Thiet lap trong truong hop hrm lam don chuyen gio them thanh tien cho nhan vien
         if ($id == -11) {
             $model->to_date = $model->from_date;
             $model->user_id = $user_id;
         }
         $from_date_temp = $model->from_date;
         $model->from_date = date('Y-m-d', strtotime(str_replace('/', '-', $model->from_date)));
         $to_date_tem = $model->to_date;
         $model->to_date = date('Y-m-d', strtotime(str_replace('/', '-', $model->to_date)));
         $model->date_create = date('Y-m-d H:i:s');
         //Kiem tra gio co hop phep ko? va luu lai
         if ($model->validate(['hours_off']) && $model->save()) {
             //Truong hop la nhan vien xin nghi, lam bu, nghi bu
             //Gui thu cho manager cua no
             if (Yii::$app->user->can('staff') && $id != -11) {
                 // Gui mail bao cho manager biet co dua xin nghi.
                 $link = Html::a('Click me!', Yii::$app->urlManager->createAbsoluteUrl(['application/check-application', 'id' => $model->id]));
                 $reason = ReasonApplication::findOne(['id' => $model->reason]);
                 $user_info = UserInfo::findOne(['user_id' => $model->user_id]);
                 $user_manager = UserInfo::findOne(['user_id' => $user_info->manager]);
                 $body = $this->renderPartial('email_request', ['model' => $model, 'reason' => $reason, 'link' => $link, 'user_info' => $user_info, 'user_manager' => $user_manager]);
                 if ($user_manager !== null && $this->sendMail($user_manager->email, $user_info->email, $user_info->full_name, $reason->name, $body)) {
                     //if($user_manager !== null && $this->sendMail("*****@*****.**","*****@*****.**",$user_info->full_name,$reason->name,$str)){
                     Yii::$app->session->setFlash('success', 'Your message has been sent to the manager !');
                 } else {
                     Yii::$app->session->setFlash('error', 'Unable to send a letter to the manager. Please call the manager. Manager\'s phone: .' . $user_manager->phone);
                 }
             }
             //Truong hop la don xin chuyen thoi gian thanh tien.
             if ($id == -11) {
             }
             //Truong hop manager xin nghi viec.
             if (Yii::$app->user->can('manager') && $id != -11) {
             }
             return $this->redirect(['view', 'id' => $model->id]);
         }
         $model->from_date = $from_date_temp;
         $model->to_date = $to_date_tem;
     }
     if ($id == -11) {
         return $this->render('application-time-to-money', ['model' => $model, 'reason' => $reason]);
     }
     return $this->render('application-of-type', ['model' => $model, 'reason' => $reason]);
 }
 /**
  * Finds the ReasonApplication model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ReasonApplication the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ReasonApplication::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }