File: D:/HostingSpaces/SBogers10/hours.komma.pro/app/Komma/Excel/Types/Row/FunctionRow.php
<?php
namespace App\Komma\Excel\Types\Row;
use App\Komma\Excel\ExcelService;
use Illuminate\Support\Collection;
class FunctionRow
{
const SUM = 1;
const TYPE_RANGE = 'range';
const TYPE_CELLS = 'cells';
public string $functionType = self::TYPE_RANGE;
public string $label;
private string $labelColumn;
private array $functionColumns;
private array $functionRenderedCells;
public function __construct(string $label, string $labelColumn, array $functionColumns)
{
$this->label = $label;
$this->labelColumn = $labelColumn;
$this->functionColumns = $functionColumns;
}
/**
* Set the function type
*
* @param string $functionType
*/
public function setFunctionType(string $functionType)
{
$this->functionType = $functionType;
}
/**
* Check if the column is the label column
*
* @param int $horizontalKey
* @return bool
*/
public function isLabelColumn(int $horizontalKey)
{
if(ExcelService::getHorizontalColumnKey($horizontalKey) === $this->labelColumn) return true;
return false;
}
/**
* Check if the column is the desired function column
*
* @param int $horizontalKey
* @return bool
*/
public function isFunctionColumn(int $horizontalKey)
{
$columnKey = ExcelService::getHorizontalColumnKey($horizontalKey);
if(in_array($columnKey, array_keys($this->functionColumns) )) return true;
return false;
}
/**
* Make the excel function
*
* @param int $horizontalKey
* @param int $groupStartRow
* @param int $currentRow
* @return string
*/
public function getRangeFunction(int $horizontalKey, int $groupStartRow, int $currentRow):string
{
$columnKey = ExcelService::getHorizontalColumnKey($horizontalKey);
// Store the cell where the function is positioned
$this->functionRenderedCells[$columnKey] = $columnKey.$currentRow;
switch ($this->functionColumns[$columnKey]) {
case self::SUM:
return '=SUM(' . $columnKey.$groupStartRow . ':' . $columnKey.($currentRow - 1) . ')';
break;
default;
throw new \UnexpectedValueException(self::class.': Undefined function method for range "' . $this->functionColumns[$columnKey] . '"');
}
}
/**
* Special summary functions
*
* @param int $horizontalKey
* @param Collection $excelGroups
* @return string
*/
public function getSummaryFunction(int $horizontalKey, Collection $excelGroups)
{
$columnKey = ExcelService::getHorizontalColumnKey($horizontalKey);
$summaryString = '=SUM(';
foreach($excelGroups as $key => $excelGroup) {
$functionRow = $excelGroup->rows->whereInstanceOf(FunctionRow::class)->first();
if(!isset($functionRow) || isset($excelGroup->codeName) && $excelGroup->codeName == 'summary') continue;
if($summaryString != '=SUM(') $summaryString .= '+' ; // On the first we can skip it
$summaryString .= $functionRow->functionRenderedCells[$columnKey];
}
$summaryString .= ')';
return $summaryString;
}
}