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]); }
public function createCompaniesZip($tasks_id) { $task = Tasks::findOne($tasks_id); foreach ($task->requests as $request) { $name = $this->translit($request->company->name) . '_' . $tasks_id . '.zip'; $zip = new ZipArchive(); $destination = 'uploads/certificates/companies_zip/' . $name; if ($zip->open($destination, ZipArchive::CREATE) !== true) { return false; } foreach ($request->requestedCertificates as $requested_certificate) { $wagons = preg_split('/[\\s,]+/', $requested_certificate->wagons); foreach ($wagons as $wagon) { if ($lost = CertificatesLost::find()->where(['tasks_id' => $tasks_id, 'wagon' => $wagon, 'certificates_id' => $requested_certificate->certificate->id])->one()) { $lost->delete(); } if (!$zip->addFile('uploads/certificates/certificates_txt/' . $requested_certificate->certificate->code . '_' . $wagon . '.txt', $requested_certificate->certificate->code . '_' . $wagon . '.txt')) { $lost = new CertificatesLost(); $lost->tasks_id = $tasks_id; $lost->wagon = $wagon; $lost->certificates_id = $requested_certificate->certificate->id; $lost->save(); } } } $zip->close(); if (file_exists($destination)) { $this->writeFilePath($destination, $name, 'companies-zip', $tasks_id); } } $this->clearCertificatesTxtFolder(); }