Skip to content

Commit

Permalink
mappers: Changed pdf generator lib
Browse files Browse the repository at this point in the history
Change from PHPpdf to wkhtmltopdf:

- Added HTML templates

- Added klear help texts

- Unified composer files

- Updated documentation

Requires 'composer install' to be run.
  • Loading branch information
cruzccl authored and mmadariaga committed Jun 14, 2017
1 parent f0cbb9c commit 4e2bab4
Show file tree
Hide file tree
Showing 25 changed files with 2,161 additions and 920 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Irontec IVOZ Provider Team - First version of all files
- Gorka Gorrotxategi <gorka.g@irontec.com>
- Ivan Alonso <kaian@irontec.com>
- Luis Felipe García <lfgarcia@irontec.com>
- Mikel Madariaga <mmadariaga@irontec.com>
- Mikel Madariaga <mikel@irontec.com>
- Victor Vargas <victor@irontec.com>
6 changes: 3 additions & 3 deletions doc/sphinx/billing_and_invoices/invoices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ creation process uses templates.
.. hint:: This way, every **brand operator** can adapt which information
is shown and how this information is shown, add logos, graphs, etc..

These templates use `PHPPdf library <https://github.com/psliwa/PHPPdf>`_.
These templates use `wkhtmltopdf library <https://wkhtmltopdf.org/>`_.

The helper in the section **Brand configuration** > **Invoice templates** include
a summarized explanation of the creation of templates. In the `official site of PHPPdf
<https://github.com/psliwa/PHPPdf>`_ there is plenty additional information.
a summarized explanation of the creation of templates. In the `official site of wkhtmltopdf
<https://wkhtmltopdf.org/usage/wkhtmltopdf.txt>`_ there is plenty additional information.

By default, this section provides some basic example templates:

Expand Down
1 change: 0 additions & 1 deletion library/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*generator.log
vendor
composer.lock
7 changes: 0 additions & 7 deletions library/Composer/composer.json

This file was deleted.

69 changes: 41 additions & 28 deletions library/IvozProvider/Gearmand/Invoices/Generator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace IvozProvider\Gearmand\Invoices;

use Knp\Snappy\Pdf;

class Generator
{
protected $_invoiceId = null;
Expand Down Expand Up @@ -43,9 +45,7 @@ public function getTotals()

public function getInvoicePDFContents()
{
$pdfContents = $this->_createInvoice();

return $pdfContents;
return $this->_createInvoice();
}

protected function _createInvoice()
Expand Down Expand Up @@ -75,18 +75,6 @@ protected function _createInvoice()
$outDate->setTimezone($invoiceTz);
$outDate->addDay(1)->subSecond(1);

$engine = 'pdf';
$facade = \PHPPdf\Core\FacadeBuilder::create()
->setEngineType($engine)
->setEngineOptions(
array(
'format' => 'jpg',
'quality' => 70,
'engine' => 'imagick',
)
)
->build();

$invoiceArray = $invoice->toArray();
$invoiceArray["invoiceDate"] = $invoiceDate->toString($dateFormat);
$invoiceArray["inDate"] = $inDate->toString($dateFormat);
Expand All @@ -103,15 +91,31 @@ protected function _createInvoice()
"fixedCostsTotals" => $this->_fixedCostTotal,
"totals" => $this->_totals
);

/**
* @var $templateModel \IvozProvider\Model\InvoiceTemplates
*/
$templateModel = $invoice->getInvoiceTemplate();
if (!$templateModel) {
throw new \Exception("No template assigned.");
}
$template = $templateModel->getTemplate();
$xml = \IvozProvider\Template\Formatter::format($template, $variables);

$header = \IvozProvider\Template\Formatter::format($templateModel->getTemplateHeader(), $variables);
$body = \IvozProvider\Template\Formatter::format($templateModel->getTemplate(), $variables);
$footer = \IvozProvider\Template\Formatter::format($templateModel->getTemplateFooter(), $variables);

$this->_log("Rendering the PDF", \Zend_Log::DEBUG);
return $facade->render($xml);
$architecture = (php_uname("m") === 'x86_64') ? 'amd64' : 'i386';

$snappy = new Pdf(APPLICATION_PATH . '/../../library/vendor/bin/wkhtmltopdf-' . $architecture);
$snappy->setOption('header-html', $header);
$snappy->setOption('header-spacing', 3);
$snappy->setOption('footer-html', $footer);
$snappy->setOption('footer-spacing', 3);
$content = $snappy->getOutputFromHtml($body);
$snappy->removeTemporaryFiles();

return $content;
}

protected function _getCallData(\IvozProvider\Model\Invoices $invoice)
Expand Down Expand Up @@ -173,6 +177,11 @@ protected function _getCallData(\IvozProvider\Model\Invoices $invoice)

$this->_log("Where: ".$where, \Zend_Log::DEBUG);

$dbAdapter = $invoice->getMapper()->getDbTable()->getAdapter();
$updateCallsInvoiceId = 'UPDATE `kam_acc_cdrs` SET `invoiceId` = ' . $invoice->getId();
$updateCallsInvoiceId .= ' WHERE ' . $where;
$dbAdapter->query($updateCallsInvoiceId);

while ($continue) {

$calls = $callsMapper->fetchList($where, $order, $limit, $offset);
Expand Down Expand Up @@ -240,25 +249,29 @@ protected function _getCallData(\IvozProvider\Model\Invoices $invoice)
$inboundCalls["summary"]["numberOfCalls"] += 1;
$inboundCalls["summary"]["totalCallsDuration"] += $call->getDuration();
$inboundCalls["summary"]["totalCallsDurationFormatted"] = $this->_timeFormat($inboundCalls["summary"]["totalCallsDuration"]);
$inboundCalls["summary"]["totalPrice"] += number_format(ceil($callData["price"]*10000)/10000, 4);
$inboundCalls["summary"]["totalPrice"] = number_format(
$inboundCalls["summary"]["totalPrice"] + ceil($callData["price"]*10000)/10000,
4
);
} else {
$callSumary[$callType]["numberOfCalls"] += 1;
$callSumary[$callType]["totalCallsDuration"] += $call->getDuration();
$callSumary[$callType]["totalCallsDurationFormatted"] = $this->_timeFormat($callSumary[$callType]["totalCallsDuration"]);
$callSumary[$callType]["totalPrice"] += number_format(ceil($callData["price"]*10000)/10000, 4);
$callSumary[$callType]["totalPrice"] = number_format(
$callSumary[$callType]["totalPrice"] + ceil($callData["price"]*10000)/10000,
4
);
}
$callSumaryTotals["numberOfCalls"] += 1;
$callSumaryTotals["totalCallsDuration"] += $call->getDuration();
$callSumaryTotals["totalCallsDurationFormatted"] = $this->_timeFormat($callSumaryTotals["totalCallsDuration"]);
$callSumaryTotals["totalPrice"] += number_format(ceil($callData["price"]*10000)/10000, 4);
$callSumaryTotals["totalPrice"] = number_format(
$callSumaryTotals["totalPrice"] + ceil($callData["price"]*10000)/10000,
4
);
}
}

$dbAdapter = $invoice->getMapper()->getDbTable()->getAdapter();
$updateCallsInvoiceId = 'UPDATE `kam_acc_cdrs` SET `invoiceId` = ' . $invoice->getId();
$updateCallsInvoiceId .= ' WHERE ' . $where;
$dbAdapter->query($updateCallsInvoiceId);

$total = $callSumaryTotals["totalPrice"] + $this->_fixedCostTotal;
$totalTaxex = ceil(($total*$invoice->getTaxRate()/100)*10000)/10000;
$totalWithTaxex = ceil(($totalTaxex + $total)*100)/100;
Expand All @@ -270,8 +283,8 @@ protected function _getCallData(\IvozProvider\Model\Invoices $invoice)
);

$this->_log("Saving TotalPrice and Total price with taxes", \Zend_Log::INFO);
$this->_log("TotalPrice: ".$total, \Zend_Log::INFO);
$this->_log("Total price with taxes: ".$totalWithTaxex, \Zend_Log::INFO);
$this->_log("TotalPrice: " . $total, \Zend_Log::INFO);
$this->_log("Total price with taxes: " . $totalWithTaxex, \Zend_Log::INFO);

asort($callSumary);
asort($callsPerType);
Expand Down
36 changes: 35 additions & 1 deletion library/IvozProvider/Mapper/Sql/DbTable/InvoiceTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,46 @@ class InvoiceTemplates extends TableAbstract
'PRIMARY_POSITION' => NULL,
'IDENTITY' => false,
),
'templateHeader' =>
array (
'SCHEMA_NAME' => NULL,
'TABLE_NAME' => 'InvoiceTemplates',
'COLUMN_NAME' => 'templateHeader',
'COLUMN_POSITION' => 5,
'DATA_TYPE' => 'text',
'DEFAULT' => NULL,
'NULLABLE' => true,
'LENGTH' => NULL,
'SCALE' => NULL,
'PRECISION' => NULL,
'UNSIGNED' => NULL,
'PRIMARY' => false,
'PRIMARY_POSITION' => NULL,
'IDENTITY' => false,
),
'templateFooter' =>
array (
'SCHEMA_NAME' => NULL,
'TABLE_NAME' => 'InvoiceTemplates',
'COLUMN_NAME' => 'templateFooter',
'COLUMN_POSITION' => 6,
'DATA_TYPE' => 'text',
'DEFAULT' => NULL,
'NULLABLE' => true,
'LENGTH' => NULL,
'SCALE' => NULL,
'PRECISION' => NULL,
'UNSIGNED' => NULL,
'PRIMARY' => false,
'PRIMARY_POSITION' => NULL,
'IDENTITY' => false,
),
'brandId' =>
array (
'SCHEMA_NAME' => NULL,
'TABLE_NAME' => 'InvoiceTemplates',
'COLUMN_NAME' => 'brandId',
'COLUMN_POSITION' => 5,
'COLUMN_POSITION' => 7,
'DATA_TYPE' => 'int',
'DEFAULT' => NULL,
'NULLABLE' => false,
Expand Down
8 changes: 8 additions & 0 deletions library/IvozProvider/Mapper/Sql/Raw/InvoiceTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function toArray($model, $fields = array())
'name' => $model->getName(),
'description' => $model->getDescription(),
'template' => $model->getTemplate(),
'templateHeader' => $model->getTemplateHeader(),
'templateFooter' => $model->getTemplateFooter(),
'brandId' => $model->getBrandId(),
);
} else {
Expand Down Expand Up @@ -568,19 +570,25 @@ public function loadModel($data, $entry = null)
->setName($data['name'])
->setDescription($data['description'])
->setTemplate($data['template'])
->setTemplateHeader($data['templateHeader'])
->setTemplateFooter($data['templateFooter'])
->setBrandId($data['brandId']);
} else if ($data instanceof \Zend_Db_Table_Row_Abstract || $data instanceof \stdClass) {
$entry->setId($data->{'id'})
->setName($data->{'name'})
->setDescription($data->{'description'})
->setTemplate($data->{'template'})
->setTemplateHeader($data->{'templateHeader'})
->setTemplateFooter($data->{'templateFooter'})
->setBrandId($data->{'brandId'});

} else if ($data instanceof \IvozProvider\Model\Raw\InvoiceTemplates) {
$entry->setId($data->getId())
->setName($data->getName())
->setDescription($data->getDescription())
->setTemplate($data->getTemplate())
->setTemplateHeader($data->getTemplateHeader())
->setTemplateFooter($data->getTemplateFooter())
->setBrandId($data->getBrandId());

}
Expand Down
84 changes: 84 additions & 0 deletions library/IvozProvider/Model/Raw/InvoiceTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ class InvoiceTemplates extends ModelAbstract
*/
protected $_template;

/**
* Database var type text
*
* @var text
*/
protected $_templateHeader;

/**
* Database var type text
*
* @var text
*/
protected $_templateFooter;

/**
* Database var type int
*
Expand Down Expand Up @@ -80,6 +94,8 @@ class InvoiceTemplates extends ModelAbstract
'name'=>'name',
'description'=>'description',
'template'=>'template',
'templateHeader'=>'templateHeader',
'templateFooter'=>'templateFooter',
'brandId'=>'brandId',
);

Expand Down Expand Up @@ -289,6 +305,74 @@ public function getTemplate()
return $this->_template;
}

/**
* Sets column Stored in ISO 8601 format. *
* @param text $data
* @return \IvozProvider\Model\Raw\InvoiceTemplates
*/
public function setTemplateHeader($data)
{

if ($this->_templateHeader != $data) {
$this->_logChange('templateHeader', $this->_templateHeader, $data);
}

if ($data instanceof \Zend_Db_Expr) {
$this->_templateHeader = $data;

} else if (!is_null($data)) {
$this->_templateHeader = (string) $data;

} else {
$this->_templateHeader = $data;
}
return $this;
}

/**
* Gets column templateHeader
*
* @return text
*/
public function getTemplateHeader()
{
return $this->_templateHeader;
}

/**
* Sets column Stored in ISO 8601 format. *
* @param text $data
* @return \IvozProvider\Model\Raw\InvoiceTemplates
*/
public function setTemplateFooter($data)
{

if ($this->_templateFooter != $data) {
$this->_logChange('templateFooter', $this->_templateFooter, $data);
}

if ($data instanceof \Zend_Db_Expr) {
$this->_templateFooter = $data;

} else if (!is_null($data)) {
$this->_templateFooter = (string) $data;

} else {
$this->_templateFooter = $data;
}
return $this;
}

/**
* Gets column templateFooter
*
* @return text
*/
public function getTemplateFooter()
{
return $this->_templateFooter;
}

/**
* Sets column Stored in ISO 8601 format. *
* @param int $data
Expand Down
1 change: 1 addition & 0 deletions library/IvozProvider/Template/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected function _getVariableValue($variables, $key)
return null;
}
}

return $value;
}

Expand Down
13 changes: 5 additions & 8 deletions library/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
"name": "ivozprovider",
"description": "IvozProvider",
"require": {
"psliwa/php-pdf": "~1.2"
},
"authors": [
{
"name": "Luis García",
"email": "lfgarcia@irontec.com"
}
]
"knplabs/knp-snappy": "0.4.3",
"h4cc/wkhtmltopdf-amd64": "0.12.3",
"h4cc/wkhtmltopdf-i386": "0.12.3",
"crada/php-apidoc":"@dev"
}
}
Loading

0 comments on commit 4e2bab4

Please sign in to comment.