/** * @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 searchUser($params, $user_id) { $query = UserDate::find(); $query->select('user_info.*, year, entitlement, entitlement - (SELECT SUM(hours_off) AS hours FROM `application` WHERE user_id = ' . $user_id . ' AND manager_ok = 1 AND hrm_ok = 1 AND reason = 13 AND YEAR(from_date) = user_date.`year`) AS balance'); $query->joinWith(['user']); $query->where(['user_date.user_id' => $user_id]); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'year' => $this->year, 'entitlement' => $this->entitlement, 'balance' => $this->balance]); return $dataProvider; }
public function getApplications() { return $this->hasMany(UserDate::className(), ['id' => 'reason']); }
/** * @return \yii\db\ActiveQuery */ public function getUserDates() { return $this->hasMany(UserDate::className(), ['user_id' => 'user_id']); }
public function actionCreateForStaff($id) { $model = new UserDate(); $model->user_id = $id; $model->year = date('Y'); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['userinfo/index']); } else { return $this->render('create', ['model' => $model]); } }