/**
  * Creates a new EmpMaster model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new EmpMaster();
     $info = new EmpInfo();
     $user = new User();
     $address = new EmpAddress();
     $auth_assign = new AuthAssignment();
     $empUniqueId = EmpInfo::find()->max('emp_unique_id');
     $empno = null;
     if (empty($empUniqueId)) {
         $empno = $info->emp_unique_id = 1;
     } else {
         $chkId = EmpInfo::find()->where(['emp_unique_id' => $empUniqueId])->exists();
         if ($chkId) {
             $empno = $empUniqueId + 1;
         } else {
             $empno = $empUniqueId;
         }
     }
     if ($model->load(Yii::$app->request->post()) && $info->load(Yii::$app->request->post())) {
         if (Yii::$app->request->isAjax) {
             \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ActiveForm::validate($info);
         }
         if (Yii::$app->request->isAjax) {
             \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         }
         $model->attributes = $_POST['EmpMaster'];
         $info->attributes = $_POST['EmpInfo'];
         $info->emp_dob = Yii::$app->dateformatter->getDateFormat($_POST['EmpInfo']['emp_dob']);
         $info->emp_joining_date = Yii::$app->dateformatter->getDateFormat($_POST['EmpInfo']['emp_joining_date']);
         $info->emp_unique_id = $empno;
         if (empty($_POST['EmpInfo']['emp_email_id'])) {
             $info->emp_email_id = NULL;
         } else {
             $info->emp_email_id = strtolower($info->emp_email_id);
         }
         $user->user_login_id = \app\models\Organization::find()->one()->org_emp_prefix . $info->emp_unique_id;
         $user->user_password = md5($user->user_login_id . $user->user_login_id);
         $user->user_type = "E";
         $user->created_by = Yii::$app->getid->getId();
         $user->created_at = new \yii\db\Expression('NOW()');
         if ($info->save(false)) {
             $user->save(false);
             $address->save(false);
         }
         $model->emp_master_emp_address_id = $address->emp_address_id;
         $model->emp_master_emp_info_id = $info->emp_info_id;
         $model->emp_master_user_id = $user->user_id;
         $model->created_by = Yii::$app->getid->getId();
         $model->created_at = new \yii\db\Expression('NOW()');
         $model->save(false);
         $emp_info = EmpInfo::findOne($model->emp_master_emp_info_id);
         $emp_info->emp_info_emp_master_id = $model->emp_master_id;
         $emp_info->save(false);
         $auth_assign->item_name = 'Employee';
         $auth_assign->user_id = $user->user_id;
         $auth_assign->created_at = date_format(date_create(), 'U');
         $auth_assign->save(false);
         if ($model->save(false)) {
             return $this->redirect(['view', 'id' => $model->emp_master_id]);
         } else {
             return $this->render('create', ['model' => $model, 'info' => $info, 'user' => $user, 'empno' => $empno]);
         }
     } else {
         return $this->render('create', ['model' => $model, 'info' => $info, 'user' => $user, 'empno' => $empno]);
     }
 }