|
|
@@ -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() |