Skip to content

Commit

Permalink
Mejoradas las opciones de tamaño de fuente y estilo al añadir textos.
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoRazorX committed Nov 11, 2023
1 parent b1a42c6 commit 535881c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
96 changes: 78 additions & 18 deletions Core/Lib/Export/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function addCompanyHeader(int $idempresa): void
$this->addImage($attFile->getFullPath());
}

$this->addText($company->nombre, ['font-size' => 16, 'font-weight' => 'B']);
$this->addText($company->nombre, ['font-size' => 16, 'font-weight' => 'bold']);
$this->addText($company->cifnif);
$this->addText($company->direccion . ', ' . $company->codpostal . ' ' . $company->ciudad
. ' (' . $company->provincia . ')');
Expand Down Expand Up @@ -138,35 +138,35 @@ public function addImage(string $filePath): self
return $this;
}

public function addModel(ModelClass $model): self
public function addModel(ModelClass $model, array $options = []): self
{
if (false === $this->pipeFalse('addModel', $model)) {
if (false === $this->pipeFalse('addModel', $model, $options)) {
return $this;
}

$this->newPage();

switch ($model->modelClassName()) {
default:
$this->addDefaultModel($model);
$this->addDefaultModel($model, $options);
break;

case 'AlbaranCliente':
case 'FacturaCliente':
case 'PedidoCliente':
case 'PresupuestoCliente':
$this->addSalesDocument($model);
$this->addSalesDocument($model, $options);
break;

case 'AlbaranProveedor':
case 'FacturaProveedor':
case 'PedidoProveedor':
case 'PresupuestoProveedor':
$this->addPurchaseDocument($model);
$this->addPurchaseDocument($model, $options);
break;

case 'Asiento':
$this->addAccountingEntry($model);
$this->addAccountingEntry($model, $options);
break;
}

Expand All @@ -183,6 +183,8 @@ public function addModelList(array $list, array $header = [], array $options = [
return $this;
}

$this->checkOptions($options);

return $this;
}

Expand All @@ -196,6 +198,8 @@ public function addTable(array $rows, array $header = [], array $options = []):
return $this;
}

$this->checkOptions($options);

// si la cabecera está vacía, la generamos a partir de la primera fila
if (empty($header)) {
$header = array_keys($rows[0]);
Expand Down Expand Up @@ -250,12 +254,12 @@ public function addText(string $text, array $options = []): self
$text .= "\n";
}

$this->checkOptions($options);

// cambiamos el tamaño de la fuente
$fontSize = $options['font-size'] ?? 10;
$fontWeight = $options['font-weight'] ?? '';
$this->pdf->SetFont($this->font_family, $fontWeight, $fontSize);
$this->pdf->SetFont($this->font_family, $options['font-weight'], $options['font-size']);

$this->pdf->write($fontSize + 5, $text);
$this->pdf->cell(0, $options['font-size'] + 5, $text, 0, 1, $options['align']);

// volvemos al tamaño de fuente por defecto
$this->pdf->SetFont($this->font_family, $this->font_weight, $this->font_size);
Expand Down Expand Up @@ -342,7 +346,7 @@ public function showHeader(bool $show): self
return $this;
}

protected function addAccountingEntry(Asiento $model): void
protected function addAccountingEntry(Asiento $model, array $options = []): void
{
// cabecera
$this->addCompanyHeader($model->getExercise()->idempresa);
Expand All @@ -364,7 +368,7 @@ protected function addAccountingEntry(Asiento $model): void
$this->addTable($rows, $header);
}

protected function addDefaultModel(ModelClass $model): void
protected function addDefaultModel(ModelClass $model, array $options = []): void
{
// si el modelo tiene idempresa, añadimos la cabecera de la empresa
if (property_exists($model, 'idempresa')) {
Expand All @@ -374,8 +378,9 @@ protected function addDefaultModel(ModelClass $model): void
}

$this->addText($model->modelClassName() . ': ' . $model->primaryDescription(), [
'align' => 'center',
'font-size' => 16,
'font-weight' => 'B',
'font-weight' => 'bold',
]);

// obtenemos los datos del modelo como array
Expand All @@ -395,7 +400,7 @@ protected function addDefaultModel(ModelClass $model): void
$this->addTable($rows);
}

protected function addPurchaseDocument(PurchaseDocument $doc): void
protected function addPurchaseDocument(PurchaseDocument $doc, array $options = []): void
{
// cabecera
$this->addCompanyHeader($doc->idempresa);
Expand Down Expand Up @@ -428,7 +433,7 @@ protected function addPurchaseDocument(PurchaseDocument $doc): void
}
}

protected function addSalesDocument(SalesDocument $doc): void
protected function addSalesDocument(SalesDocument $doc, array $options = []): void
{
// cabecera
$this->addCompanyHeader($doc->idempresa);
Expand All @@ -438,7 +443,12 @@ protected function addSalesDocument(SalesDocument $doc): void
$this->addText("\n");

// líneas
$header = ['reference', 'description', 'quantity', 'price'];
$header = [
$this->trans('reference'),
$this->trans('description'),
$this->trans('quantity'),
$this->trans('price')
];
$rows = [];
foreach ($doc->getLines() as $line) {
$rows[] = [
Expand All @@ -452,12 +462,62 @@ protected function addSalesDocument(SalesDocument $doc): void

// totales
$this->addText("\n");
$header = ['base', 'tax', 'total'];
$header = [
$this->trans('net'),
$this->trans('tax'),
$this->trans('total')
];
$rows = [$doc->neto, $doc->totaliva, $doc->total];
$this->addTable([$rows], $header);

if ($doc->observaciones) {
$this->addText($doc->observaciones);
}
}

protected function checkOptions(array &$options): void
{
switch ($options['align'] ?? '') {
case 'C':
case 'center':
$options['align'] = 'C';
break;

case 'R':
case 'right':
$options['align'] = 'R';
break;

default:
$options['align'] = 'left';
break;
}

if (isset($options['font-size'])) {
$options['font-size'] = (int)$options['font-size'];
} else {
$options['font-size'] = $this->font_size;
}

switch ($options['font-weight'] ?? '') {
case 'B':
case 'bold':
$options['font-weight'] = 'B';
break;

case 'I':
case 'italic':
$options['font-weight'] = 'I';
break;

case 'U':
case 'underline':
$options['font-weight'] = 'U';
break;

default:
$options['font-weight'] = '';
break;
}
}
}
2 changes: 1 addition & 1 deletion Core/Template/PdfEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract public function addHtml(string $html);

abstract public function addImage(string $filePath);

abstract public function addModel(ModelClass $model);
abstract public function addModel(ModelClass $model, array $options = []);

abstract public function addModelList(array $list, array $header = [], array $options = []);

Expand Down

0 comments on commit 535881c

Please sign in to comment.