public function actionIndex($id_app_task)
 {
     $model = AppTask::findOne($id_app_task);
     $loanModel = Loan::find()->where(['id_application' => $model->id_application])->one();
     // 如果 loan 不存在,则新建
     if (is_null($loanModel)) {
         $loanModel = new Loan();
         $loanModel->loan_number = '';
         $loanModel->id_application = $model->id_application;
         $loanModel->status = 0;
         $loanModel->loc = $model->application->loc;
         $loanModel->balance = 0;
         $loanModel->advance_rate = 50;
         $loanModel->maintainance_rate = 70;
         $loanModel->collateral_value = $model->application->collateral_value;
         $loanModel->save(false);
     }
     // 读取所以检查项
     $checkPointsData = AppTaskCheckpoint::find()->where(['id_app_task' => $id_app_task])->all();
     $checkPoints = [];
     $check_points_ctrl = true;
     foreach ($checkPointsData as $val) {
         $checkPoints[$val['id_app_task_checkpoint_type']] = $val;
         if (!$val->status) {
             $check_points_ctrl = false;
         }
     }
     //  判断是否可以 approve
     $loanModel->setScenario('review');
     $loanCheck = clone $loanModel;
     $loan_ctrl = $loanCheck->validate();
     if (Yii::$app->request->post()) {
         // 如果选择 wet­signed
         $post = Yii::$app->request->post();
         if ($post['type'] == 'wet­signed') {
             //关闭sign agreements
             $task_e_sign_box = AppTask::findOne(['id_application' => $model->id_application, 'id_app_task_type' => DictTask::TYPE_SIGN_AGREEMENTS_BOX]);
             AppTask::close($task_e_sign_box->id_app_task);
         }
         // 确认 Application Review and Check 结束
         $applicationModel = $model->application;
         //g3
         AppDocument::createPdfByApplication($applicationModel, DictDocument::TYPE_G3);
         //control_agreement
         AppDocument::createPdfByApplication($applicationModel, DictDocument::TYPE_CONTROL_AGREEMENT);
         //line_agreements
         AppDocument::createPdfByApplication($applicationModel, DictDocument::TYPE_LINE_AGREEMENT);
         //w9
         AppDocument::createPdfByApplication($applicationModel, DictDocument::TYPE_W9);
         $loanModel->id_application = $applicationModel->id_application;
         $id_application = $loanModel->id_application;
         //active log.loan officer approved the application.
         Active::operationLog($id_application, [Yii::$app->user->getIdentity()->first_name, Yii::$app->user->getIdentity()->last_name], DictActive::APPROVED_APPLICATION, Yii::$app->user->getIdentity()->id);
         AppTask::taskFinish($id_app_task);
         //find Control Agreement--type--schwab(control agreement->id_application->id_group->Coustidian)
         $application = Application::find()->andWhere(['id_application' => $id_application])->one();
         $group = Group::find()->andWhere(['id_group' => $application->id_group])->one();
         //judge for id_custodian
         //send application approval email to client and cc to advisor(email($email_type,[$task_type,$id_app_task,$loan,$group]))
         $res = SendEmail::email(DictEmail::EMAIL_LETTER_ABOUT_LINE, [DictEmail::LETTER_APPLICATION_APPROVAL, $id_app_task, $loanModel, $group]);
         if ($res) {
             //判断是否有两个client参与
             $user = SendEmail::findUserByApplication($id_application);
             if (is_null($user['co_applicant'])) {
                 Active::operationLog($id_application, [$user['applicant']->first_name, $user['applicant']->last_name, '', $user['advisor']->first_name, $user['advisor']->last_name], DictActive::SYSTEM_SEND_APPROVAL_LETTER, 0);
             } else {
                 Active::operationLog($id_application, [$user['applicant']->first_name, $user['applicant']->last_name, ',' . $user['co_applicant']->first_name . ' ' . $user['co_applicant']->last_name, $user['advisor']->first_name, $user['advisor']->last_name], DictActive::SYSTEM_SEND_APPROVAL_LETTER, 0);
             }
         }
         //active.system geneerated all the line agreements
         Active::operationLog($id_application, [], DictActive::SYSTEM_GENERATED_LINE_AGREEMENTS, 0);
         //find user
         $user = SendEmail::findUserByApplication($id_application);
         //create a task line-agreement to loan officer
         Active::operationLog($model->id_application, [DictTask::$taskType[DictTask::TYPE_LINE_AGREEMENTS], $user['supernova']->first_name, $user['supernova']->last_name], DictActive::SYSTEM_CREATE_TASK_TO_USER, 0);
         //system created task line-agreements
         //System send email new task "Line Agrrements Notification" to betterdebt($email_type,[$task_type,$id_application,$id_app_task])
         SendEmail::email(DictEmail::EMAIL_TASK_NOTIFICATION, [DictTask::TYPE_LINE_AGREEMENTS, $id_application, $id_app_task + 2]);
         if ($post['type'] != 'wet­signed') {
             // docusign
             $result_docusign = SendDocuSign::appReview($id_application);
         }
         //crate a task sign-line-agreement to client
         Active::operationLog($model->id_application, [DictTask::$taskType[DictTask::TYPE_SIGN_AGREEMENTS], $user['applicant']->first_name, $user['applicant']->last_name], DictActive::SYSTEM_CREATE_TASK_TO_USER, 0);
         //system created task line-agreements
         Active::operationLog($model->id_application, [DictTask::$taskType[DictTask::TYPE_LINE_AGREEMENTS], $user['applicant']->first_name, $user['applicant']->last_name], DictActive::SYSTEM_SEND_DOCUSIGN_EMAIL, 0);
         //system send sign agreement docusign email to client
         return $this->redirect(['/application/view', 'id' => $model->id_application]);
     }
     $data = CollateralAccount::find()->andWhere(['line_type' => 1, 'id_line' => $model->id_application])->orderBy(['data_date' => SORT_DESC])->one();
     if ($data) {
         $account = CollateralAccount::find()->andWhere(['line_type' => 1, 'id_line' => $model->id_application, 'data_date' => $data['data_date']])->orderBy(['data_date' => SORT_DESC])->all();
         $line_account = LineAccount::find()->andWhere(['line_type' => 1, 'id_line' => $model->id_application, 'data_date' => $data['data_date']])->orderBy(['data_date' => SORT_DESC])->one();
     } else {
         $account = '';
         $line_account = '';
     }
     //credit score
     $applicant = AppApplicant::find()->andWhere(['id_application' => $model->id_application])->all();
     return $this->render('index', ['model' => $model, 'loanModel' => $loanModel, 'checkPoints' => $checkPoints, 'approve_ctrl' => $check_points_ctrl && $loan_ctrl, 'collateral_account' => $account, 'line_account' => $line_account, 'date' => empty($data) ? '' : $data['data_date'], 'applicant' => empty($applicant) ? '' : $applicant]);
 }
 public function actionSubmitEsign($id_app_task)
 {
     $model = AppTask::findOne($id_app_task);
     $application = $model->application;
     $appFormModel = $application->appForm;
     $appFormModel->load(Yii::$app->request->get());
     $appFormModel->save(false);
     $appFormModel->setScenario('disclosures');
     $status = $appFormModel->validate(true);
     AppFormStatus::changeStatus($application->id_application, 4, $status);
     //检查AppForm所有项是否正确填写
     $result = $application->checkAppForm();
     if (Yii::$app->request->post()) {
         // docusign
         $result_docusign = SendDocuSign::appForm($application->id_application);
         // Finish Task
         AppTask::taskFinish($id_app_task);
         //active log. client submit app-form
         Active::operationLog($application->id_application, [Yii::$app->user->getIdentity()->first_name, Yii::$app->user->getIdentity()->last_name, 'submitted', DictTask::$taskType[DictTask::TYPE_APPLICATION_FORM]], DictActive::USER_FINISH_TASK, Yii::$app->user->getIdentity()->id);
         //find user
         $res = SendEmail::findUserByApplication($application->id_application);
         //active log. system create task sing-app-form and assign to client/advisor
         Active::operationLog($application->id_application, [DictTask::$taskType[DictTask::TYPE_SIGN_APPLICATION_FORM], $res['advisor']->first_name, $res['advisor']->last_name], DictActive::SYSTEM_CREATE_TASK_TO_USER, 0);
         //system assign to advisor
         Active::operationLog($application->id_application, [DictTask::$taskType[DictTask::TYPE_SIGN_APPLICATION_FORM], $res['applicant']->first_name, $res['applicant']->last_name], DictActive::SYSTEM_CREATE_TASK_TO_USER, 0);
         //system assign to client
         if (!is_null($res['co_applicant'])) {
             Active::operationLog($application->id_application, [DictTask::$taskType[DictTask::TYPE_SIGN_APPLICATION_FORM], $res['co_applicant']->first_name, $res['co_applicant']->last_name], DictActive::SYSTEM_CREATE_TASK_TO_USER, 0);
             //system assign to co_client
         }
         //active log. system send docusign task email to user
         Active::operationLog($application->id_application, [DictTask::$taskType[DictTask::TYPE_APPLICATION_FORM], $res['advisor']->first_name, $res['advisor']->last_name], DictActive::SYSTEM_SEND_DOCUSIGN_EMAIL, 0);
         //system send docusign email to advisor
         Active::operationLog($application->id_application, [DictTask::$taskType[DictTask::TYPE_APPLICATION_FORM], $res['applicant']->first_name, $res['applicant']->last_name], DictActive::SYSTEM_SEND_DOCUSIGN_EMAIL, 0);
         //system send docusign email to client
         //active log. system create task supplemental-materials and assign to advisor
         Active::operationLog($application->id_application, [DictTask::$taskType[DictTask::TYPE_SUPPLEMENTAL_MATERIALS], $res['advisor']->first_name, $res['advisor']->last_name], DictActive::SYSTEM_CREATE_TASK_TO_USER, 0);
         //System send email new task “Supplemental materials Notification” to client
         SendEmail::email(DictEmail::EMAIL_TASK_NOTIFICATION, [DictTask::TYPE_SUPPLEMENTAL_MATERIALS, $application->id_application, $id_app_task + 2]);
         //id_app_task_type:1(app-form);0:表示没有传递id_app_task
         if ($this->user->id_user_role == Dict::USER_ROLE_CLIENT) {
             return $this->redirect(['/loan/application']);
         } else {
             return $this->redirect(['/application/view', 'id' => $application->id_application]);
         }
     }
     return $this->renderAjax('submit_esign', ['model' => $model, 'result' => $result]);
 }