Exemplo n.º 1
0
 public function actionRequests($tasks_id = null, $id = null, $new = null)
 {
     if ($tasks_id) {
         $task = Tasks::findOne($tasks_id);
     } elseif (!Tasks::find()->where(['state' => 1])->one()) {
         $task = new Tasks();
         $task->save();
     } else {
         $task = Tasks::find()->where(['state' => 1])->one();
     }
     $certificates = Certificates::find()->where(['state' => 1])->all();
     if ($new) {
         $request = new Requests();
         $requested_certificates = [];
         foreach ($certificates as $certificate) {
             $requested_certificates[$certificate->id] = new RequestedCertificates();
             $requested_certificates[$certificate->id]->certificates_id = $certificate->id;
         }
     } else {
         $request = Requests::findOne($id);
         $requested_certificates = [];
         foreach ($certificates as $certificate) {
             if ($request && RequestedCertificates::find()->where(['requests_id' => $request->id, 'certificates_id' => $certificate->id])->one()) {
                 $requested_certificates[$certificate->id] = RequestedCertificates::find()->where(['requests_id' => $request->id, 'certificates_id' => $certificate->id])->one();
             } else {
                 $requested_certificates[$certificate->id] = new RequestedCertificates();
                 $requested_certificates[$certificate->id]->certificates_id = $certificate->id;
             }
         }
     }
     $companies = ArrayHelper::map(Companies::find()->all(), 'id', 'name');
     $requests = new ActiveDataProvider(['query' => Requests::find()->where(['tasks_id' => $tasks_id]), 'pagination' => ['pageSize' => 20]]);
     if ($request && $request->load(Yii::$app->request->post())) {
         $request->tasks_id = $task->id;
         $request->save();
         if (Model::loadMultiple($requested_certificates, Yii::$app->request->post())) {
             foreach ($requested_certificates as $key => $requested_certificate) {
                 if (!$requested_certificate->wagons) {
                     $requested_certificate->delete();
                     continue;
                 }
                 $requested_certificate->requests_id = $request->id;
                 $requested_certificate->save();
             }
         }
         if ($new) {
             Yii::$app->getSession()->setFlash('success', 'Запрос добавлен');
         } else {
             Yii::$app->getSession()->setFlash('success', 'Изменения сохранены');
         }
     } else {
         return $this->render('requests', ['task' => $task, 'companies' => $companies, 'requests' => $requests, 'request' => $request, 'requested_certificates' => $requested_certificates, 'new' => $new, 'excels' => FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'excel'])->all() ? FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'excel'])->all() : '', 'excel_zip' => FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'excel-zip'])->one() ? FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'excel-zip'])->one()->path : '', 'companies_zip' => FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'companies-zip'])->all() ? FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'companies-zip'])->all() : '', 'lost' => CertificatesLost::find()->where(['tasks_id' => $tasks_id])->all() ? CertificatesLost::find()->where(['tasks_id' => $tasks_id])->all() : '']);
     }
     return $this->redirect(['/certificates/requests', 'tasks_id' => $task->id]);
 }
Exemplo n.º 2
0
 public function createTaskZip($tasks_id)
 {
     $task = Tasks::findOne($tasks_id);
     $name = date('d-m-Y_H-i', $task->created_at) . '.zip';
     $file_paths = FilePaths::find()->where(['tasks_id' => $tasks_id, 'type' => 'excel'])->all();
     $zip = new ZipArchive();
     $destination = 'uploads/certificates/zip/' . $name;
     if ($zip->open($destination, ZipArchive::CREATE) !== true) {
         return false;
     }
     foreach ($file_paths as $path) {
         $zip->addFile($path->path, $path->name);
     }
     $zip->close();
     $this->writeFilePath($destination, $name, 'excel-zip', $tasks_id);
 }
Exemplo n.º 3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getFilePaths()
 {
     return $this->hasMany(FilePaths::className(), ['requests_id' => 'id']);
 }