guild/common/services/DocumentService.php

125 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2022-01-07 15:04:11 +03:00
<?php
namespace common\services;
2022-01-10 15:22:51 +03:00
use common\models\Document;
2022-11-16 14:24:52 +03:00
use common\models\DocumentTemplate;
use DateTime;
use kartik\mpdf\Pdf;
2022-01-07 15:04:11 +03:00
class DocumentService
{
2022-01-16 23:54:13 +03:00
public static function getDocumentList($document_type): array
2022-01-10 15:22:51 +03:00
{
2022-01-16 23:54:13 +03:00
if (!empty($document_type)) {
return Document::find()->joinWith('template')
->where(['document_type' => $document_type])->asArray()->all();
}
else {
return Document::find()->asArray()->all();
2022-01-07 15:04:11 +03:00
}
}
2022-01-16 23:54:13 +03:00
public static function getDocument($document_id)
2022-01-07 15:04:11 +03:00
{
2022-01-16 23:54:13 +03:00
return Document::find()
->joinWith(['documentFieldValues.field'])
->where(['document.id' => $document_id])
->asArray()->all();
2022-01-10 15:22:51 +03:00
}
2022-11-16 14:24:52 +03:00
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->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();
2022-12-01 14:11:29 +03:00
$html = str_replace(array('<br/>', '<br>', '</br>'), ' ', $model->body);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
$section->setStyle();
2022-11-16 14:24:52 +03:00
// (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);
$fields = $templateModel->getFields();
2022-11-16 14:24:52 +03:00
$document = $templateModel->template_body;;
foreach ($fields as $field) {
2022-11-16 14:24:52 +03:00
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 ?? $field;
2022-11-16 14:24:52 +03:00
break;
case '${manager}':
$fieldValue = $model->manager->userCard->fio ?? $field;
2022-11-16 14:24:52 +03:00
break;
case '${contractor_company}':
$fieldValue = $model->contractorCompany->name ?? $field;
2022-11-16 14:24:52 +03:00
break;
case '${contractor_manager}':
$fieldValue = $model->contractorManager->userCard->fio ?? $field;
2022-11-16 14:24:52 +03:00
break;
default:
$fieldValue = $field;
break;
}
if ($fieldValue == $field) {
continue;
} else {
$document = str_replace($field, $fieldValue, $document);
}
2022-11-16 14:24:52 +03:00
}
}
$model->body = $document;
}
2022-01-07 15:04:11 +03:00
}