示例#1
0
 private static function EmployeeDo(&$Employee, &$Employeelog, $row)
 {
     $result = false;
     $xls_attributes_employee = self::xls_attributes_employee($row);
     //    $sqlstr = empty($xls_attributes_employee['id_build']) ? ' and id_build is null' : ' and id_build = :id_build';
     // Находим сотрудника в базе, если не находим создаем новую запись
     /*    $Employee = self::GetRowsPDO('select auth_user_fullname, from employee where id_dolzh = :id_dolzh and id_podraz = :id_podraz' . $sqlstr, array_merge([
            'id_dolzh' => $xls_attributes_employee['id_dolzh'],
            'id_podraz' => $xls_attributes_employee['id_podraz']
            ], empty($xls_attributes_employee['id_build']) ? [] : ['id_build' => $xls_attributes_employee['id_build']] ));
           */
     //  if (!empty($search))
     $search = Employee::find()->joinWith('idperson')->where(array_merge(['id_dolzh' => $xls_attributes_employee['id_dolzh'], 'id_podraz' => $xls_attributes_employee['id_podraz']], empty($xls_attributes_employee['id_build']) ? [] : ['id_build' => $xls_attributes_employee['id_build']]))->andFilterWhere(['like', 'auth_user_fullname', $xls_attributes_employee['employee_fio'], false])->one();
     if (!empty($search)) {
         $Employee = $search;
     }
     if ($Employee->isNewRecord) {
         //Если новая запись (Нет соответствия по ФИО, Должности, Подразделению, Зданию)
         $Employee->attributes = $xls_attributes_employee;
         $AuthuserCount = Authuser::find()->where(['like', 'auth_user_fullname', $xls_attributes_employee['employee_fio'], false])->count();
         /*    $search2 = Authuser::find()
               ->where(['like', 'auth_user_fullname', $xls_attributes_employee['employee_fio']])
               ->one(); */
         $Authuser = $AuthuserCount == 1 ? Authuser::find()->where(['like', 'auth_user_fullname', $xls_attributes_employee['employee_fio'], false])->one() : false;
         if (empty($Authuser) || $AuthuserCount > 1) {
             $Authuser = new Authuser();
             $Authuser->auth_user_fullname = $xls_attributes_employee['employee_fio'];
             $Authuser->auth_user_login = Proc::CreateLogin($xls_attributes_employee['employee_fio']);
             $Authuser->auth_user_password = Yii::$app->getSecurity()->generatePasswordHash('11111111');
         }
         if (isset($Authuser->scenarios()['import1c'])) {
             $Authuser->scenario = 'import1c';
         }
         /*  $Person = new Authuser;
             if (!empty($search2))
             $Person = $search2; */
         if ($Authuser->isNewRecord) {
             if ($Authuser->validate()) {
                 $Authuser->save(false);
                 $Employee->id_person = $Authuser->getPrimaryKey();
             } else {
                 $Employeelog->employeelog_type = 3;
                 $Employeelog->employeelog_message = 'Ошибка при добавлении записи: ';
                 foreach ($Authuser->getErrors() as $fields) {
                     $Employeelog->employeelog_message .= implode(' ', $fields) . ' ';
                 }
                 self::$logreport_errors++;
             }
         } else {
             $Employee->id_person = $Authuser->getPrimaryKey();
         }
         $Employeelog->employee_fio = Authuser::findOne($Employee->id_person)->auth_user_fullname;
         //self::GetNameByID('auth_user', 'auth_user_fullname', $Employee->id_person);
         $Employeelog->dolzh_name = Dolzh::findOne($Employee->id_dolzh)->dolzh_name;
         //self::GetNameByID('dolzh', 'dolzh_name', $Employee->id_dolzh);
         $Employeelog->podraz_name = Podraz::findOne($Employee->id_podraz)->podraz_name;
         //self::GetNameByID('podraz', 'podraz_name', $Employee->id_podraz);
         if (!empty($Employee->id_build)) {
             $Employeelog->build_name = Build::findOne($Employee->id_build)->build_name;
         }
         //self::GetNameByID('build', 'build_name', $Employee->id_build)
         // Валидируем значения модели и пишем в лог
         $result = self::ImportValidate($Employee, $Employeelog);
     } else {
         // Если изменения не внесены пишем в лог
         //   $Employeelog->attributes = $Employee->attributes;
         /*   if (!empty($Employee->employee_dateinactive)) {             // Если есть в excel, то убрать дату неактивности специальности
                       $Employeelog->employeelog_type = 2;
                       $Employeelog->employeelog_message = 'Запись изменена. Очищена дата неактивности специальности "' . Yii::$app->formatter->asDate($Employee->employee_dateinactive) . '"';
                       $Employeelog->employee_fio = Authuser::findOne($Employee->id_person)->auth_user_fullname;
                       $Employeelog->dolzh_name = Dolzh::findOne($Employee->id_dolzh)->dolzh_name;
                       $Employeelog->podraz_name = Podraz::findOne($Employee->id_podraz)->podraz_name;
                       if (!empty($Employee->id_build))
                       $Employeelog->build_name = Build::findOne($Employee->id_build)->build_name;
         
                       $Employee->employee_dateinactive = null;
         
                       $result = self::ImportValidate($Employee, $Employeelog);
                       } */
         $Employeelog->employee_fio = Authuser::findOne($Employee->id_person)->auth_user_fullname;
         //self::GetNameByID('auth_user', 'auth_user_fullname', $Employee->id_person);
         $Employeelog->dolzh_name = Dolzh::findOne($Employee->id_dolzh)->dolzh_name;
         //self::GetNameByID('dolzh', 'dolzh_name', $Employee->id_dolzh);
         $Employeelog->podraz_name = Podraz::findOne($Employee->id_podraz)->podraz_name;
         //self::GetNameByID('podraz', 'podraz_name', $Employee->id_podraz);
         if (!empty($Employee->id_build)) {
             $Employeelog->build_name = Build::findOne($Employee->id_build)->build_name;
         }
         //self::GetNameByID('build', 'build_name', $Employee->id_build);
         // Добавляем в лог не измененные значения ActiveRecord
         $result = self::JustAddToLog($Employee, $Employeelog);
     }
     return $result;
 }