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

namespace backend\controllers; namespace backend\controllers;


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


]; ];
} }


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() public function actionCreate()
{ {
$class = $this->getClass(); $class = $this->getClass();
public function actionDownload($id) public function actionDownload($id)
{ {
$document = $this->findModel($id); $document = $this->findModel($id);
return $document->generatePdf(Pdf::DEST_BROWSER);
return $document->downloadPdf();
} }


public function actionSend($id) public function actionSend($id)
if ($document) { if ($document) {
$document->changeStatus(Document::STATUS_VALID); $document->changeStatus(Document::STATUS_VALID);
$document->save(); $document->save();

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

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

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

<tr class="<?php if(isset($displayOrders) && $displayOrders): ?>order<?php endif; ?>"> <tr class="<?php if(isset($displayOrders) && $displayOrders): ?>order<?php endif; ?>">
<td class="align-left"> <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): ?> <?php if($productOrder->unit == 'piece' && isset($productOrder->product->weight) && $productOrder->product->weight): ?>
<span class="weight"> / <?= $productOrder->product->weight ?> g</span> <span class="weight"> / <?= $productOrder->product->weight ?> g</span>
<?php endif; ?> <?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> <br /><small><?= Html::encode($productOrder->product->description) ?></small>
<?php endif; ?> <?php endif; ?>
</td> </td>

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

<tr> <tr>
<td> <td>
<strong><?= Html::encode($order->getUsername()) ; ?></strong> <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> </td>
<?php if($displayPrices): ?> <?php if($displayPrices): ?>
<td class="align-center"></td> <td class="align-center"></td>

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

/index.php /index.php
/index-test.php /index-test.php
/pdf/*

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



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


class Document extends ActiveRecordCommon class Document extends ActiveRecordCommon
{ {
$totalVat += $order->getTotalVat($type); $totalVat += $order->getTotalVat($type);
} }


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


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




$ordersArray = $this->orders; $ordersArray = $this->orders;


foreach($ordersArray as $order) {
foreach ($ordersArray as $order) {
$order->init($this->tax_calculation_method); $order->init($this->tax_calculation_method);
$fieldNameVat = $order->getFieldNameAmount($typeTotal, 'vat'); $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] = 0;
} }
$totalVatArray[$idTaxRate] += $vat; $totalVatArray[$idTaxRate] += $vat;
} }
} }


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) public function generatePdf($destination)
{ {
$producer = GlobalParam::getCurrentProducer(); $producer = GlobalParam::getCurrentProducer();
$marginBottom = 40; $marginBottom = 40;
} }


$this->initDirectoryPdf();

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


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


return $email->send(); return $email->send();
} }
return $displayOrders; 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() 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() public function isInvoicePrice()

Loading…
Cancel
Save