<?php
namespace App\ClientCollection\Corporate\Document; abstract class XlsxAbstract extends GeneralAbstract { protected $attachmentFileName = ''; protected $sheetName = ''; protected $columns = []; protected $rowStyle = ''; private $overdueRowsList = []; private $dueInDaysRowsList = []; public function populateReport() : void { $this->loadLang(); $writer = new \OpenSpout\Writer\XLSX\Writer(); $writer->openToFile($this->tempFileFullPath); $writer->getCurrentSheet()->setName($this->sheetName); $writer->getCurrentSheet()->setColumnWidthForRange(20, 1, count($this->columns)); $this->populateReportData(); if ($this->overdueRowsList) { $this->addHeader($writer); foreach ($this->overdueRowsList as $row) { if (array_filter($row)) { $row = array_replace(array_flip(array_keys($this->columns)), $row); } $formattedCells = []; foreach ($row as $key => $cell) { if (isset($cell['style'])) { $formattedCells[] = \OpenSpout\Common\Entity\Cell::fromValue($cell['value'], $this->getStyle($cell['style'])); } else { $formattedCells[] = \OpenSpout\Common\Entity\Cell::fromValue($cell, $this->getStyle($this->columns[$key])); } } $rowObj = new \OpenSpout\Common\Entity\Row($formattedCells); $rowObj->setStyle($this->getStyle($this->rowStyle)); $writer->addRow($rowObj); } } if ($this->dueInDaysRowsList) { $this->addHeader($writer); foreach ($this->dueInDaysRowsList as $row) { if (array_filter($row)) { $row = array_replace(array_flip(array_keys($this->columns)), $row); } $formattedCells = []; foreach ($row as $key => $cell) { if (isset($cell['style'])) { $formattedCells[] = \OpenSpout\Common\Entity\Cell::fromValue($cell['value'], $this->getStyle($cell['style'])); } else { $formattedCells[] = \OpenSpout\Common\Entity\Cell::fromValue($cell, $this->getStyle($this->columns[$key])); } } $rowObj = new \OpenSpout\Common\Entity\Row($formattedCells); $rowObj->setStyle($this->getStyle($this->rowStyle)); $writer->addRow($rowObj); } } $writer->close(); } protected function addHeader(\OpenSpout\Writer\XLSX\Writer $writer) : void { $headerCells = []; foreach ($this->columns as $column => $columnType) { $headerCells[] = \OpenSpout\Common\Entity\Cell::fromValue($this->getColumnName($column), $this->getStyle('header')); } $row = new \OpenSpout\Common\Entity\Row($headerCells, $this->getStyle('header')); $writer->addRow($row); } abstract protected function loadLang() : void; abstract protected function getColumnName($columnCode) : string; abstract protected function populateReportData() : void; protected function exportRow(array $row, string $type) : void { if ($type == 'overdue') { $this->overdueRowsList[] = $row; } else { $this->dueInDaysRowsList[] = $row; } } protected function getStyle(string $styleName) : \OpenSpout\Common\Entity\Style\Style { $styleObj = new \OpenSpout\Common\Entity\Style\Style(); $styleObj ->setShouldWrapText(false) ->setFontSize(9) ->setFontName("Arial") ->setBorder( new \OpenSpout\Common\Entity\Style\Border( new \OpenSpout\Common\Entity\Style\BorderPart( \OpenSpout\Common\Entity\Style\Border::BOTTOM, \OpenSpout\Common\Entity\Style\Color::BLACK, \OpenSpout\Common\Entity\Style\Border::WIDTH_THIN, \OpenSpout\Common\Entity\Style\Border::STYLE_SOLID ) ) ); switch ($styleName) { case 'numeric': $styleObj ->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::RIGHT); break; case 'price': $styleObj ->setFormat('0.00') ->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::RIGHT); break; case 'statusOverdue': $styleObj ->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::LEFT) ->setFontColor(\OpenSpout\Common\Entity\Style\Color::RED) ->setFontBold(); break; case 'date': $styleObj ->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::RIGHT); break; case 'header': case 'footer': $styleObj ->setFontBold() ->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::LEFT); break; case 'status': $styleObj ->setCellAlignment(\OpenSpout\Common\Entity\Style\CellAlignment::LEFT) ->setFontBold(); break; case 'default': break; } return $styleObj; } }