<?php
namespace Modules\Bsp\Settlement;

class ModelGenerateReportBspOperationalStatus extends ModelGenerateReportAbstract
{
    protected function getBspIterator(\App\Mvc\View\Listing\Members\Params $listingParams)
	{
        // ignore user-filters, export EVERYTHING
        $listingParams = new \App\Mvc\View\Listing\Members\Params(__CLASS__);
        $listingParams->setFilters([
            'ticketDate' => [
                'start' => null,
                'end' => null,
            ],
        ]);
		$sqlIterator = parent::getBspIterator($listingParams);

        $sqlIterator->appendCondition("bspSettlement.operationalStatus != ''");

		return $sqlIterator;
	}

	public function generate($outputFile)
	{
		$sqlIterator = $this->getBspIterator($this->listingParams);

		// format rows, use MapResultsIterator() for this. This basically replaces the View
		$rowFormatter = new \ModulesAbstract\PluginReports\Report\Model\Iterator\MapResultsIterator();
		$rowFormatter->setSource($sqlIterator);

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();
		$rowTemplate = [
            'key' => 					        '',

			'settlementOperationalStatus' => 	'',
			'settlementOperationalDateTime' => 	'',
			'settlementOperationalUserId' => 	'',
		];

		$rowFormatter->setPreReturnCallback(function($rowFields) use ($rowTemplate) {
			$ret = $rowTemplate;

            $ret['key'] = 	$rowFields['id'];

			$ret['settlementOperationalStatus'] = 	$rowFields['operationalStatus'];
			$ret['settlementOperationalDateTime'] = $rowFields['operationalDateTime'];
			$ret['settlementOperationalUserId'] = 	$rowFields['operationalUser'];

			return $ret;
		});


		$xlsObj = new \App\Utils\CSV\Writer\BufferedWriter();
		$xlsObj->appendHeaderRow(array_keys($rowTemplate));
		$xlsObj->fromArray($rowFormatter->asIterator());
		$xlsObj->save($outputFile);

		return $this;
	}
}
