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 createExcels($tasks_id)
 {
     $this->tasks_id = $tasks_id;
     $requests_array = [];
     $requests = Requests::find()->where(['tasks_id' => $tasks_id])->all();
     foreach ($requests as $request) {
         $requested_certificates = RequestedCertificates::find()->where(['requests_id' => $request->id])->all();
         foreach ($requested_certificates as $requested_certificate) {
             $wagons = $this->parseWagons($requested_certificate->wagons);
             foreach ($wagons as $wagon) {
                 $requests_array[$requested_certificate->certificate->code][] = $wagon;
             }
         }
     }
     $dir = 'uploads/certificates/certificates_excel/task_' . $tasks_id . '_' . date('d.m.Y');
     if (!is_dir($dir)) {
         mkdir($dir, 0777, true);
     }
     foreach ($requests_array as $certificate => $wagons) {
         $this->writeOneCertificate($wagons, $certificate, $dir);
     }
     $this->createTaskZip($tasks_id);
 }
Exemplo n.º 3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getRequestedCertificates()
 {
     return $this->hasMany(RequestedCertificates::className(), ['requests_id' => 'id']);
 }