/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new EmployeeTransaction();
     $info = new EmployeeInfo();
     $user = new User();
     $photo = new EmployeePhotos();
     $address = new EmployeeAddress();
     $lang = new LanguagesKnown();
     $auth_assign = new AuthAssignment();
     // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation(array($info, $model, $user));
     if (!empty($_POST['EmployeeTransaction']) || !empty($_POST['EmployeeInfo'])) {
         $model->attributes = $_POST['EmployeeTransaction'];
         $info->attributes = $_POST['EmployeeInfo'];
         $user->attributes = $_POST['User'];
         $doa = $info->employee_joining_date;
         $dateofadmission = date("Y-m-d", strtotime($doa));
         $info->employee_joining_date = $dateofadmission;
         /** fix the attendece id card length */
         $cardid_length = strlen((string) $info->employee_attendance_card_id);
         $cardid = $info->employee_attendance_card_id;
         $digit = 0;
         $diff = 10 - $cardid_length;
         for ($i = 1; $i <= $diff; $i++) {
             $cardid = $digit . $cardid;
         }
         $info->employee_attendance_card_id = $cardid;
         $info->employee_private_email = strtolower($user->user_organization_email_id);
         $info->employee_created_by = Yii::app()->user->id;
         $info->employee_creation_date = new CDbExpression('NOW()');
         $user->user_organization_email_id = $info->employee_private_email;
         $user->user_password = md5($info->employee_private_email . $info->employee_private_email);
         $user->user_created_by = Yii::app()->user->id;
         $user->user_creation_date = new CDbExpression('NOW()');
         //$user->user_organization_id = Yii::app()->user->getState('org_id');
         $user->user_type = "employee";
         if ($info->save(false)) {
             $user->save(false);
             $address->save(false);
             $lang->save(false);
             $photo->employee_photos_path = "no-images";
             $photo->save(false);
         }
         $model->employee_transaction_employee_id = $info->employee_id;
         $model->employee_transaction_user_id = $user->user_id;
         $model->employee_transaction_emp_photos_id = $photo->employee_photos_id;
         $model->employee_transaction_emp_address_id = $address->employee_address_id;
         $model->employee_transaction_languages_known_id = $lang->languages_known_id;
         $model->employee_transaction_organization_id = Yii::app()->user->getState('org_id');
         $model->employee_status = 0;
         $model->save(false);
         // not false because it hasn't been validated
         $auth_assign->itemname = 'Employee';
         $auth_assign->userid = $user->user_id;
         $auth_assign->save();
         $this->redirect(array('update', 'id' => $model->employee_transaction_id));
     } else {
         $this->render('create', array('model' => $model, 'info' => $info, 'user' => $user));
     }
 }
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new EmployeeTransaction;
		$info=new EmployeeInfo;
		$user =new User;
		$photo =new EmployeePhotos;
		$address=new EmployeeAddress;
		$lang=new LanguagesKnown;
		$auth_assign = new AuthAssignment;


		// Uncomment the following line if AJAX validation is needed
		 $this->performAjaxValidation(array($info,$model,$user));

		if(!empty($_POST['EmployeeTransaction']) || !empty($_POST['EmployeeInfo']))
		{
			$model->attributes=$_POST['EmployeeTransaction'];
			$info->attributes=$_POST['EmployeeInfo'];
			$user->attributes=$_POST['User'];			
			
			$emp_roll_no = EmployeeInfo::model()->findAll();
			if(empty($emp_roll_no))
			{
			$empno=$info->employee_unique_id=1;
			}
			else
			{
			//$rand=mt_rand(1,2000);
				foreach($emp_roll_no as $s)
				{
		            		$emp[]=$s['employee_unique_id'];
					$empno_m=MAX($emp)+1;
				}		
				if(EmployeeInfo::model()->exists('employee_unique_id='.$empno_m))
				{
					$empno=$empno_m+1;
				}
				else
				{
					$empno=$empno_m;
				}			
			}
			$doa = $info->employee_joining_date;
			$dateofadmission = date("Y-m-d", strtotime($doa));
			$info->employee_joining_date = $dateofadmission;
			$info->employee_unique_id=$empno;
			$cardid_length = strlen((string) $info->employee_attendance_card_id);
			
			$cardid = $info->employee_attendance_card_id;
			$digit = 0;
			$diff = 10-$cardid_length;
			for($i=1;$i<=$diff;$i++)
			{
				$cardid = $digit.$cardid;
			}
			$info->employee_attendance_card_id = $cardid;
			$info->employee_private_email =strtolower($user->user_organization_email_id); 
			$info->employee_created_by = Yii::app()->user->id;
            		$info->employee_creation_date = new CDbExpression('NOW()');
			
			$user->user_organization_email_id = $info->employee_private_email;
			$user->user_password =  md5($info->employee_private_email.$info->employee_private_email);
			$user->user_created_by =  Yii::app()->user->id;
			$user->user_creation_date = new CDbExpression('NOW()');
			$user->user_type = "employee";
			if($info->save(false))  
			{  
			        $user->save(false);
				$address->save(false);
				$lang->save(false);			
				$photo->employee_photos_path = "no-images" ;
				$photo->save(false);
			}
			$model->employee_transaction_employee_id = $info->employee_id;
			$model->employee_transaction_user_id = $user->user_id;
			$model->employee_transaction_emp_photos_id = $photo->employee_photos_id;
	                $model->employee_transaction_emp_address_id = $address->employee_address_id;
			$model->employee_transaction_languages_known_id=$lang->languages_known_id;
			$model->employee_status = 0;

	        	$model->save(false); 
			EmployeeInfo::model()->updateByPk($model->employee_transaction_employee_id, array('employee_info_transaction_id'=>$model->employee_transaction_id));

			$auth_assign->itemname = 'Employee';
			$auth_assign->userid = $user->user_id;
			$auth_assign->bizrule = '';
			$auth_assign->data = '';
			$auth_assign->save(false);

			$this->redirect(array('update','id'=>$model->employee_transaction_id));

	 	}			 
		else
		{
			$this->render('create',array(
			'model'=>$model,'info'=>$info,'user'=>$user,
		));
		}
	}