Browse Source

DocumentController::actionGeneratePdfValidatedDocuments()

refactoring
Guillaume Bourgeois 2 years ago
parent
commit
54ae436259
5 changed files with 78 additions and 12 deletions
  1. +23
    -1
      backend/controllers/DocumentController.php
  2. +4
    -2
      backend/views/document/_download_product_line.php
  3. +3
    -1
      backend/views/document/download.php
  4. +1
    -0
      backend/web/.gitignore
  5. +47
    -8
      common/models/Document.php

+ 23
- 1
backend/controllers/DocumentController.php View File

@@ -39,13 +39,16 @@
namespace backend\controllers;

use common\models\DeliveryNote;
use common\models\Invoice;
use common\models\PointSale;
use common\models\Product;
use common\models\Quotation;
use common\models\User;
use common\models\Document;
use common\helpers\GlobalParam;
use common\models\Order;
use common\models\UserProducer;
use kartik\mpdf\Pdf;
use yii\base\UserException;
use yii;

@@ -74,6 +77,21 @@ class DocumentController extends BackendController
];
}

public function actionGeneratePdfValidatedDocuments()
{
set_time_limit(0);

$validatedDocumentsArray = array_merge(
Quotation::find()->where(['status' => Document::STATUS_VALID])->all(),
DeliveryNote::find()->where(['status' => Document::STATUS_VALID])->all(),
Invoice::find()->where(['status' => Document::STATUS_VALID])->all()
);

foreach($validatedDocumentsArray as $document) {
$document->generatePdf(Pdf::DEST_FILE);
}
}

public function actionCreate()
{
$class = $this->getClass();
@@ -293,7 +311,7 @@ class DocumentController extends BackendController
public function actionDownload($id)
{
$document = $this->findModel($id);
return $document->generatePdf(Pdf::DEST_BROWSER);
return $document->downloadPdf();
}

public function actionSend($id)
@@ -385,6 +403,10 @@ class DocumentController extends BackendController
if ($document) {
$document->changeStatus(Document::STATUS_VALID);
$document->save();

// génération PDF
$document->generatePdf(Pdf::DEST_FILE);

Yii::$app->getSession()->setFlash('success', $this->getFlashMessage('validate', $document));
return $this->redirect([$this->getControllerUrl() . '/index']);
}

+ 4
- 2
backend/views/document/_download_product_line.php View File

@@ -1,10 +1,12 @@
<tr class="<?php if(isset($displayOrders) && $displayOrders): ?>order<?php endif; ?>">
<td class="align-left">
<?= Html::encode($productOrder->product->name) ?>
<?php if($productOrder->product): ?>
<?= Html::encode($productOrder->product->name) ?>
<?php endif; ?>
<?php if($productOrder->unit == 'piece' && isset($productOrder->product->weight) && $productOrder->product->weight): ?>
<span class="weight"> / <?= $productOrder->product->weight ?> g</span>
<?php endif; ?>
<?php if(strlen($productOrder->product->description) && $displayProductDescription): ?>
<?php if($productOrder->product && strlen($productOrder->product->description) && $displayProductDescription): ?>
<br /><small><?= Html::encode($productOrder->product->description) ?></small>
<?php endif; ?>
</td>

+ 3
- 1
backend/views/document/download.php View File

@@ -81,7 +81,9 @@ $displayProductDescription = Producer::getConfig('document_display_product_descr
<tr>
<td>
<strong><?= Html::encode($order->getUsername()) ; ?></strong>
le <?= date('d/m/Y', strtotime($order->distribution->date)) ?>
<?php if($order->distribution): ?>
le <?= date('d/m/Y', strtotime($order->distribution->date)) ?>
<?php endif; ?>
</td>
<?php if($displayPrices): ?>
<td class="align-center"></td>

+ 1
- 0
backend/web/.gitignore View File

@@ -1,2 +1,3 @@
/index.php
/index-test.php
/pdf/*

+ 47
- 8
common/models/Document.php View File

@@ -40,6 +40,8 @@ namespace common\models;

use common\helpers\GlobalParam;
use kartik\mpdf\Pdf;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use yii\base\ErrorException;

class Document extends ActiveRecordCommon
{
@@ -139,11 +141,11 @@ class Document extends ActiveRecordCommon
$totalVat += $order->getTotalVat($type);
}

if($this->isTaxCalculationMethodRoundingOfTheSum()) {
if ($this->isTaxCalculationMethodRoundingOfTheSum()) {
$totalVat = Price::round($totalVat);
}

if($withTax) {
if ($withTax) {
$amount += $totalVat;
}

@@ -160,11 +162,11 @@ class Document extends ActiveRecordCommon

$ordersArray = $this->orders;

foreach($ordersArray as $order) {
foreach ($ordersArray as $order) {
$order->init($this->tax_calculation_method);
$fieldNameVat = $order->getFieldNameAmount($typeTotal, 'vat');
foreach($order->$fieldNameVat as $idTaxRate => $vat) {
if(!isset($totalVatArray[$idTaxRate])) {
foreach ($order->$fieldNameVat as $idTaxRate => $vat) {
if (!isset($totalVatArray[$idTaxRate])) {
$totalVatArray[$idTaxRate] = 0;
}
$totalVatArray[$idTaxRate] += $vat;
@@ -252,6 +254,22 @@ class Document extends ActiveRecordCommon
}
}

public function downloadPdf()
{
$filenameComplete = $this->getFilenameComplete();

if(!file_exists($filenameComplete)) {
$this->generatePdf(Pdf::DEST_FILE);
}

if(file_exists($filenameComplete)) {
return Yii::$app->response->sendFile($filenameComplete, $this->getFilename(), ['inline'=>true]);
}
else {
throw new ErrorException('File '.$filenameComplete.' not found');
}
}

public function generatePdf($destination)
{
$producer = GlobalParam::getCurrentProducer();
@@ -282,13 +300,15 @@ class Document extends ActiveRecordCommon
$marginBottom = 40;
}

$this->initDirectoryPdf();

$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8,
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => $destination,
'content' => $content,
'filename' => $this->getFilename(),
'filename' => $this->getFilenameComplete(),
'cssFile' => Yii::getAlias('@webroot/css/document/download.css'),
'marginBottom' => $marginBottom,
'methods' => [
@@ -321,7 +341,7 @@ class Document extends ActiveRecordCommon
->setSubject('[' . $producer->name . '] ' . $subjectEmail);

$this->generatePdf(Pdf::DEST_FILE);
$email->attach($this->getFilename());
$email->attach($this->getFilenameComplete());

return $email->send();
}
@@ -416,9 +436,28 @@ class Document extends ActiveRecordCommon
return $displayOrders;
}

public function getAliasDirectoryBase()
{
return '@app/web/pdf/'.$this->id_producer.'/';
}

public function initDirectoryPdf()
{
$aliasDirectoryBase = $this->getAliasDirectoryBase();
$directoryPdf = Yii::getAlias($aliasDirectoryBase);
if(!file_exists($directoryPdf)) {
mkdir($directoryPdf, 0755);
}
}

public function getFilename()
{
return Yii::getAlias('@app/web/pdf/' . $this->getType() . '-' . $this->reference . '.pdf');
return $this->getType() . '-' . $this->reference . '.pdf';
}

public function getFilenameComplete()
{
return Yii::getAlias($this->getAliasDirectoryBase() . $this->getFilename());
}

public function isInvoicePrice()

Loading…
Cancel
Save