complete document service

This commit is contained in:
iIronside 2022-11-16 14:24:52 +03:00
parent 5c0badaf92
commit c52dd918db
20 changed files with 649 additions and 177 deletions

View File

@ -78,7 +78,7 @@ return [
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
'baseUrl' => '', // TODO /secure
'baseUrl' => '/secure',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
'text/xml' => 'yii/web/XmlParser',

View File

@ -2,16 +2,13 @@
namespace backend\modules\document\controllers;
use backend\modules\card\models\UserCard;
use backend\modules\document\models\DocumentTemplate;
use common\classes\Debug;
use kartik\mpdf\Pdf;
use Yii;
use backend\modules\document\models\Document;
use backend\modules\document\models\DocumentSearch;
use common\services\DocumentService;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* DocumentController implements the CRUD actions for Document model.
@ -70,8 +67,8 @@ class DocumentController extends Controller
{
$model = new Document();
if ($model->load(Yii::$app->request->post()) && $model->validate()) { // //$model->save(false)
$this::generateDocumentBody($model);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
DocumentService::generateDocumentBody($model);
$model->save(false);
return $this->redirect(['view', 'id' => $model->id]);
}
@ -142,7 +139,7 @@ class DocumentController extends Controller
* @param integer $id
* @throws NotFoundHttpException
*/
public function actionUpdateDocumentBody($id)
public function actionUpdateDocumentBody($id): string
{
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_UPDATE_DOCUMENT_BODY;
@ -157,67 +154,33 @@ class DocumentController extends Controller
]);
}
public function generateDocumentBody(Document $model)
public function actionDownloadPdf($id): string
{
$templateModel = DocumentTemplate::findOne($model->template_id);
preg_match_all('/(\${\w+})/', $templateModel->template_body,$out);
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_DOWNLOAD_DOCUMENT;
$document = $templateModel->template_body;;
foreach ($out[0] as $field) {
if (str_contains($document, $field)) {
if($field == '${contract_number}') {
$fieldValue = 101;
} elseif ($field == '${title}') {
$fieldValue = $model->title;
} elseif ($field == '${company}') {
$fieldValue = $model->company->name;
} elseif ($field == '${manager}') {
$fieldValue = $model->manager->userCard->fio;
} elseif ($field == '${contractor_company}') {
$fieldValue = $model->company->name;
} elseif ($field == '${contractor_manager}') {
$fieldValue = $model->manager->userCard->fio;
}
} else {
$fieldValue = $field;
}
$document = str_replace($field, $fieldValue, $document);
}
$model->body = $document;
if ($model->validate()) {
DocumentService::downloadPdf($id);
}
public function actionDownloadPdf($id)
Yii::$app->session->setFlash('error', $model->getFirstError('body'));
return $this->render('download', [
'model' => Document::findOne($id)
]);
}
public function actionDownloadDocx($id): string
{
$model = Document::findOne($id);
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_DOWNLOAD_DOCUMENT;
$pdf = new Pdf(); // or new Pdf();
$mpdf = $pdf->api; // fetches mpdf api
// $mpdf->SetHeader('Resume ' . $model->ti . '||Generated by ITGuild.info At: ' . date("d/m/Y")); // call methods or set any properties
$mpdf->SetFooter('{PAGENO}');
$mpdf->WriteHtml($model->body); // call mpdf write html
echo $mpdf->Output("{$model->title}", 'D'); // call the mpdf api output as needed
if ($model->validate()) {
DocumentService::downloadPdf($id);
}
public function actionDownloadDocx($id)
{
$model = Document::findOne($id);
$pw = new \PhpOffice\PhpWord\PhpWord();
// (B) ADD HTML CONTENT
$section = $pw->addSection();
$resumeText = str_replace(array('<br/>', '<br>', '</br>'), ' ', $model->body);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $resumeText, false, false);
// (C) SAVE TO DOCX ON SERVER
// $pw->save("convert.docx", "Word2007");
// (D) OR FORCE DOWNLOAD
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=\"$model->title.docx\"");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pw, "Word2007");
$objWriter->save("php://output");
exit();
Yii::$app->session->setFlash('error', $model->getFirstError('body'));
return $this->render('download', [
'model' => Document::findOne($id)
]);
}
}

View File

@ -0,0 +1,127 @@
<?php
namespace backend\modules\document\controllers;
use Yii;
use backend\modules\document\models\DocumentField;
use backend\modules\document\models\DocumentFieldSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* DocumentFieldController implements the CRUD actions for DocumentField model.
*/
class DocumentFieldController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all DocumentField models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new DocumentFieldSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single DocumentField model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new DocumentField model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new DocumentField();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing DocumentField model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing DocumentField model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the DocumentField model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return DocumentField the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = DocumentField::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace backend\modules\document\models;
class DocumentField extends \common\models\DocumentField
{
}

View File

@ -0,0 +1,69 @@
<?php
namespace backend\modules\document\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\document\models\DocumentField;
/**
* DocumentFieldSearch represents the model behind the search form of `backend\modules\document\models\DocumentField`.
*/
class DocumentFieldSearch extends DocumentField
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id'], 'integer'],
[['title', 'field_template'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = DocumentField::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'field_template', $this->field_template]);
return $dataProvider;
}
}

View File

@ -0,0 +1,25 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentField */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="document-field-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'field_template')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,31 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentFieldSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="document-field-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'field_template') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,18 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentField */
$this->title = 'Создать ноое поле';
$this->params['breadcrumbs'][] = ['label' => 'Document Fields', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="document-field-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,31 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\document\models\DocumentFieldSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Поля документов';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="document-field-index">
<p>
<?= Html::a('Создать поле', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'title',
'field_template',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,19 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentField */
$this->title = 'Изменить поле: ' . $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Document Fields', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->title, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="document-field-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,37 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\DocumentField */
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Document Fields', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="document-field-view">
<p>
<?= Html::a('Список', ['index', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'field_template',
],
]) ?>
</div>

View File

@ -1,6 +1,7 @@
<?php
use asmoday74\ckeditor5\EditorClassic;
use backend\modules\document\models\DocumentField;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -31,14 +32,6 @@ use yii\widgets\ActiveForm;
]
]); ?>
<!-- composer require --prefer-dist stkevich/yii2-ckeditor5 "*"-->
<!-- --><?//= $form->field($model, 'text')->widget(CKEditor::className(),[
// 'editorOptions' => [ TODO
// 'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать
// 'inline' => false, //по умолчанию false
// ],
// ]); ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
@ -47,14 +40,7 @@ use yii\widgets\ActiveForm;
</div>
</div>
<!-- ['Акт', $time],-->
<!-- ['Акт сверки', $time],-->
<!-- ['Детализация', $time],-->
<!-- ['Доверенность', $time],-->
<!-- ['Договор', $time],-->
<!-- ['Доп соглашение к договору', $time],-->
<!-- ['Транспортная накладная', $time],-->
<!-- ['Ценовой лист', $time],-->
<div class="col-md-4">
<div class="table-responsive">
<table class="table" id="fieldNameTable">
@ -65,63 +51,17 @@ use yii\widgets\ActiveForm;
</tr>
</thead>
<tbody>
<tr class="info">
<td class="table-cell"> договора</td>
<td class="table-cell">${contract_number}</td>
<?php
foreach (DocumentField::getTitleFieldTemplateArr() as $fieldTitle => $fieldTemplate) {
echo "
<tr class='info'>
<td class='table-cell'>$fieldTitle</td>
<td class='table-cell'>$fieldTemplate</td>
</tr>
<tr class="info">
<td class="table-cell">Название</td>
<td class="table-cell">${title}</td>
</tr>
<tr class="info">
<td class="table-cell">Компания</td>
<td class="table-cell">${company}</td>
</tr>
<tr class="info">
<td class="table-cell">Представитель</td>
<td class="table-cell">${manager}</td>
</tr>
<tr class="info">
<td class="table-cell">Компания контрагент</td>
<td class="table-cell">${contractor_company}</td>
</tr>
<tr class="info">
<td class="table-cell">Представитель контрагента</td>
<td class="table-cell">${contractor_manager}</td>
</tr>
<!-- <tr class="info">-->
<!-- <td class="table-cell"></td>-->
<!-- <td class="table-cell"></td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell"> документа</td>-->
<!-- <td class="table-cell">${document_number}</td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell">от </td>-->
<!-- <td class="table-cell">${from}</td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell">сумма с НДС</td>-->
<!-- <td class="table-cell">${sum_with_NDS}</td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell">НДС</td>-->
<!-- <td class="table-cell">${NDS}</td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell">цена</td>-->
<!-- <td class="table-cell">${price}</td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell">к договору</td>-->
<!-- <td class="table-cell">${to_the_contract}</td>-->
<!-- </tr>-->
<!-- <tr class="info">-->
<!-- <td class="table-cell"></td>-->
<!-- <td class="table-cell">${number}</td>-->
<!-- </tr>-->
";
}
?>
</tbody>
</table>
</div>
<div>

View File

@ -1,12 +1,11 @@
<?php
use asmoday74\ckeditor5\EditorClassic;
//use backend\modules\card\models\ResumeTemplate;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\document\models\Document */
@ -19,9 +18,7 @@ $this->params['breadcrumbs'][] = 'Загрузить';
<div class="form-group">
<?= Html::a('Редактировать документ', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Просмотр документа', ['view', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
</div>
</div
<div class="resume-form">
@ -42,11 +39,10 @@ $this->params['breadcrumbs'][] = 'Загрузить';
<?php ActiveForm::end(); ?>
</div>
<div>
<p>
<?= Html::a('Скачать pdf', ['download-pdf', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
<?= Html::a('Скачать docx', ['download-docx', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
</p>
</div>
</div>

View File

@ -1,5 +1,7 @@
<?php
use common\classes\Debug;
use common\models\Document;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\DetailView;
@ -53,7 +55,7 @@ $this->params['breadcrumbs'][] = $this->title;
'attribute' => 'body',
'format' => 'raw',
],
// 'body:ntext',
'created_at',
'updated_at',
],

View File

@ -27,15 +27,16 @@
['label' => 'Доп. поля', 'icon' => 'file-text-o', 'url' => ['/settings/additional-fields'], 'active' => \Yii::$app->controller->id == 'additional-fields'],
['label' => 'Должность', 'icon' => 'spotify', 'url' => ['/settings/position'], 'active' => \Yii::$app->controller->id == 'position'],
['label' => 'Навыки', 'icon' => 'flask', 'url' => ['/settings/skill'], 'active' => \Yii::$app->controller->id == 'skill'],
['label' => 'Шаблоны резюме', 'icon' => 'file', 'url' => ['/card/resume-template'], 'active' => \Yii::$app->controller->id == 'resume-template'],
['label' => 'Шаблоны резюме', 'icon' => 'address-card ', 'url' => ['/card/resume-template'], 'active' => \Yii::$app->controller->id == 'resume-template'],
['label' => 'Шаблоны документов', 'icon' => 'file', 'url' => ['/document/document-template'], 'active' => \Yii::$app->controller->id == 'document-template'],
['label' => 'Поля документов', 'icon' => 'file-text', 'url' => ['/document/document-field'], 'active' => \Yii::$app->controller->id == 'document-field'],
],
//'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
[
'label' => 'Профили', 'icon' => 'address-book-o', 'url' => '#', //'active' => \Yii::$app->controller->id == 'user-card',
'items' => $menuItems,
//'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
[
'label' => 'Сотрудники', 'icon' => 'users', 'url' => '#',
@ -43,19 +44,13 @@
['label' => 'Менеджеры', 'icon' => 'user-circle-o', 'url' => ['/employee/manager'], 'active' => \Yii::$app->controller->id == 'manager'],
['label' => 'Работники', 'icon' => 'user', 'url' => ['/employee/manager-employee'], 'active' => \Yii::$app->controller->id == 'manager-employee'],
],
// 'visible' => Yii::$app->user->can('confidential_information')
],
[
'label' => 'Документы', 'icon' => 'archive', 'url' => '#',
'items' => [
['label' => 'Документы', 'icon' => 'file-text', 'url' => ['/document/document'], 'active' => \Yii::$app->controller->id == 'document'],
],
// 'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
['label' => 'Документы', 'icon' => 'archive', 'url' => ['/document/document'], 'active' => \Yii::$app->controller->id == 'document', 'visible' => Yii::$app->user->can('confidential_information')],
[
'label' => 'Проекты', 'icon' => 'cubes', 'url' => ['#'],
'items' => $projectItems,
// 'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
[
'label' => 'Задачи', 'icon' => 'tasks', 'url' => '#',
@ -63,7 +58,7 @@
['label' => 'Задачи', 'icon' => 'minus', 'url' => ['/task/task'], 'active' => \Yii::$app->controller->id == 'task'],
['label' => 'Исполнители задачи', 'icon' => 'users', 'url' => ['/task/task-user'], 'active' => \Yii::$app->controller->id == 'task-user'],
],
// 'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
['label' => 'Компании', 'icon' => 'building', 'url' => ['/company/company'], 'active' => \Yii::$app->controller->id == 'company', ], // 'visible' => Yii::$app->user->can('confidential_information')
[
@ -72,7 +67,7 @@
['label' => 'Компании', 'icon' => 'building', 'url' => ['/hh/hh'], 'active' => \Yii::$app->controller->id == 'hh'],
['label' => 'Вакансии', 'icon' => 'user-md', 'url' => ['/hh/hh-job'], 'active' => \Yii::$app->controller->id == 'hh-job'],
],
// 'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
['label' => 'Баланс', 'icon' => 'dollar', 'url' => ['/balance/balance'], 'active' => \Yii::$app->controller->id == 'balance', 'visible' => Yii::$app->user->can('confidential_information')],
['label' => 'Отпуска', 'icon' => 'plane', 'url' => ['/holiday/holiday'], 'active' => \Yii::$app->controller->id == 'holiday', 'visible' => Yii::$app->user->can('confidential_information')],
@ -101,7 +96,7 @@
['label' => 'Анкеты пользователей', 'icon' => 'drivers-license', 'url' => ['/questionnaire/user-questionnaire'], 'active' => \Yii::$app->controller->id == 'user-questionnaire'],
['label' => 'Ответы пользователей', 'icon' => 'comments', 'url' => ['/questionnaire/user-response'], 'active' => \Yii::$app->controller->id == 'user-response'],
],
// 'visible' => Yii::$app->user->can('confidential_information')
'visible' => Yii::$app->user->can('confidential_information')
],
['label' => 'Тестовые задания', 'icon' => 'file-text-o', 'url' => ['/test/test-task'], 'active' => \Yii::$app->controller->id == 'options', 'visible' => Yii::$app->user->can('confidential_information')],

View File

@ -2,7 +2,6 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
@ -28,8 +27,8 @@ use yii\db\Expression;
*/
class Document extends \yii\db\ActiveRecord
{
const SCENARIO_GENERATE_DOCUMENT_BODY = 'generate_document_body';
const SCENARIO_UPDATE_DOCUMENT_BODY = 'update_document_body';
const SCENARIO_DOWNLOAD_DOCUMENT = 'download_document';
/**
* {@inheritdoc}
@ -67,9 +66,14 @@ class Document extends \yii\db\ActiveRecord
[['contractor_manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => Manager::className(), 'targetAttribute' => ['contractor_manager_id' => 'id']],
[['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentTemplate::className(), 'targetAttribute' => ['template_id' => 'id']],
[['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => Manager::className(), 'targetAttribute' => ['manager_id' => 'id']],
// ['resumeTemplateId', 'required', 'on' => self::SCENARIO_GENERATE_RESUME_TEXT],
// ['resumeTemplateId', 'integer', 'on' => self::SCENARIO_GENERATE_RESUME_TEXT],
['body', 'required', 'on' => self::SCENARIO_UPDATE_DOCUMENT_BODY],
['body', function ($attribute, $params) {
preg_match_all('/(\${\w+})/', $this->$attribute,$out);
if (!empty($out[0])) {
$this->addError('body', 'В теле документа все переменные должны бвть заменены!');
}
}, 'on' => self::SCENARIO_DOWNLOAD_DOCUMENT
],
];
}

View File

@ -0,0 +1,51 @@
<?php
namespace common\models;
use Yii;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "document_field".
*
* @property int $id
* @property string $title
* @property string $field_template
*/
class DocumentField extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'document_field';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title', 'field_template'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Название',
'field_template' => 'Шаблон поля',
];
}
public static function getTitleFieldTemplateArr(): array
{
return ArrayHelper::map(self::find()->all(), 'title', 'field_template');
}
}

View File

@ -3,6 +3,9 @@
namespace common\services;
use common\models\Document;
use common\models\DocumentTemplate;
use DateTime;
use kartik\mpdf\Pdf;
class DocumentService
{
@ -25,4 +28,94 @@ class DocumentService
->asArray()->all();
}
public static function getDocumentNumber(): string
{
$documents = Document::find()->where(['DATE(`created_at`)' => date('Y-m-d')])->orderBy('id DESC')->all();
$date = new DateTime();
foreach ($documents as $document) {
preg_match_all('/\b\d{2}\.\d{2}\.\d{4}\/\d{3}\b/', $document->body,$out);
if (!empty($out[0])) {
$num = substr($out[0][0], -3);
$num++;
$num = str_pad($num, 3, "0", STR_PAD_LEFT);
return $date->format('d.m.Y') . '/' . $num;
}
}
return $date->format('d.m.Y') . '/001';
}
public static function downloadPdf($id)
{
$model = Document::findOne($id);
$pdf = new Pdf(); // or new Pdf();
$mpdf = $pdf->api; // fetches mpdf api
$mpdf->SetFooter('{PAGENO}');
$mpdf->WriteHtml($model->body); // call mpdf write html
echo $mpdf->Output("{$model->title}", 'D'); // call the mpdf api output as needed
}
public static function downloadDocx($id)
{
$model = Document::findOne($id);
$pw = new \PhpOffice\PhpWord\PhpWord();
// (B) ADD HTML CONTENT
$section = $pw->addSection();
$documentText = str_replace(array('<br/>', '<br>', '</br>'), ' ', $model->body);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $documentText, false, false);
// (C) SAVE TO DOCX ON SERVER
// $pw->save("convert.docx", "Word2007");
// (D) OR FORCE DOWNLOAD
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=\"$model->title.docx\"");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pw, "Word2007");
$objWriter->save("php://output");
exit();
}
public static function generateDocumentBody(Document $model)
{
$templateModel = DocumentTemplate::findOne($model->template_id);
preg_match_all('/(\${\w+})/', $templateModel->template_body,$out);
$document = $templateModel->template_body;;
foreach ($out[0] as $field) {
if (str_contains($document, $field)) {
switch ($field)
{
case '${contract_number}':
$fieldValue = DocumentService::getDocumentNumber();
break;
case '${title}':
$fieldValue = $model->title;
break;
case '${company}':
$fieldValue = $model->company->name;
break;
case '${manager}':
$fieldValue = $model->manager->userCard->fio;
break;
case '${contractor_company}':
$fieldValue = $model->contractorCompany->name;
break;
case '${contractor_manager}':
$fieldValue = $model->contractorManager->userCard->fio;
break;
default:
$fieldValue = $field;
break;
}
$document = str_replace($field, $fieldValue, $document);
}
}
$model->body = $document;
}
}

View File

@ -15,7 +15,6 @@ class m221108_125006_drop_unnecessary_tables_for_document extends Migration
$this->dropTable('accompanying_document');
$this->dropTable('document_field_value');
$this->dropTable('template_document_field');
$this->dropTable('document_field');
$this->dropTable('document');
$this->dropTable('template');
}
@ -48,13 +47,6 @@ class m221108_125006_drop_unnecessary_tables_for_document extends Migration
$this->addForeignKey('document_manager', 'document', 'manager_id', 'manager', 'id');
$this->createTable('{{%document_field}}', [
'id' => $this->primaryKey(),
'title' => $this->string(),
'field_template' => $this->string(),
]);
$this->createTable('{{%template_document_field}}', [
'id' => $this->primaryKey(),
'template_id' => $this->integer(11)->notNull(),

View File

@ -0,0 +1,71 @@
<?php
use common\models\DocumentField;
use yii\db\Migration;
/**
* Class m221115_094557_add_update_default_and_add_new_fields_in_document_field_table
*/
class m221115_094557_update_default_and_add_new_fields_in_document_field_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$documentFields = DocumentField::find()->all();
foreach ($documentFields as $documentField) {
$documentField->field_template = '${' . $documentField->field_template . '}';
$documentField->update();
}
Yii::$app->db->createCommand()->batchInsert('document_field', [ 'title', 'field_template'],
[
['№ договора', '${contract_number}'],
['Название', '${title}'],
['Компания', '${company}'],
['Представитель', '${manager}'],
['Компания контрагент', '${contractor_company}'],
['Представитель контрагента', '${contractor_manager}']
])->execute();
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
Yii::$app->db->createCommand()->delete('document_field',
[
'in', 'title', [
'№ договора',
'Название',
'Компания',
'Представитель',
'Компания контрагент',
'Представитель контрагента',
]
])->execute();
$documentFields = DocumentField::find()->all();
foreach ($documentFields as $documentField) {
$documentField->field_template = str_replace(['${', '}'], '', $documentField->field_template);
$documentField->update();
}
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m221115_094557_add_update_default_andaddnewfieldsin_document_field_table cannot be reverted.\n";
return false;
}
*/
}