<?php
namespace ModulesAbstract\PluginReports\Report\Model\Gatherer;

/**
 * Invoice cashing gatherer - return list with id invoice cashing
 * @author Oana Stan
 */
class InvoiceCashing extends GathererAbstract
{
	/**
	 * Recordset resulted after $db->Execute
	 */
	protected $invoiceCashingsIterator = NULL;

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\GathererAbstract::getRequiredFilters()
	 */
	public function getRequiredFilters()
	{
		return [
			'interval',
			'corporateAndSegment',
		];
	}

	public function getAcceptedFilters()
	{
		return [
			'interval',
			'corporateAndSegment',
			'documentIssued',
			'docTypes',
		];
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\GathererAbstract::objectInitialize()
	 */
	protected function objectInitialize()
	{
		$listObj = new \App\EntityList\InvoiceCashings();

		////////// APPLY FILTERS ////////////////////////////////////////////////////

		$listObj->setColumns(['doc.id']);

		// filter by invoice date
		$listObj->setFilter('doc.docDate', '>=', $this->filters['interval']['start']);
		$listObj->setFilter('doc.docDate', '<=', $this->filters['interval']['end']);
		$listObj->applyUserRestriction(false);

		// filter only issued invoices
		if ($this->filters['documentIssued']) {
			$listObj->setCustomFilter("doc.issueTime > 0 OR doc.blockTime > 0");
		}

		// filter by corporate or segment
		if ($this->filters['corporateAndSegment']['type'] == 'corporate') {
			if ($this->filters['corporateAndSegment']['entityId']) {
				$listObj->setFilter('inv.idCorporate', '=', $this->filters['corporateAndSegment']['entityId']);
			}
			else {
				$listObj->setFilter('inv.idCorporate', '!=', 0);
			}
		}
		elseif ($this->filters['corporateAndSegment']['type'] == 'segment') {
			if ($this->filters['corporateAndSegment']['entityId']) {
				// get list of corporations in that segment
				$corporateObj = new \App\EntityList\Corporates();
				$corporateObj->setFilter('idSegment', '=', $this->filters['corporateAndSegment']['entityId']);
				$corporateArray = $corporateObj->exportRecords();

				$idCorporates= [];
				foreach ($corporateArray as $corporateDetails) {
					$idCorporates[] = $corporateDetails['id'];
				}

				if ($idCorporates) {
					$listObj->setFilter('inv.idCorporate', 'IN', $idCorporates);
				}
				else {
					// the specified segment doesn't contain any corporates. Return an empty resultset
					$this->invoiceCashingsIterator = new \ADORecordSet_empty();
					return;
				}
			}
			else {
				$listObj->setFilter('inv.idCorporate', '!=', 0);
			}
		}

		if ($this->filters['docTypes']) {
			$listObj->setFilter('doc.docType', 'IN', $this->filters['docTypes']);
		}

		$this->invoiceCashingsIterator = $listObj->getExecution();
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\GathererAbstract::objectDestroy()
	 */
	protected function objectDestroy()
	{
		if ($this->invoiceCashingsIterator) {
			$this->invoiceCashingsIterator->Close();
			$this->invoiceCashingsIterator = NULL;
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\GathererAbstract::objectNext()
	 */
	protected function objectNext()
	{
		if ($this->invoiceCashingsIterator && !$this->invoiceCashingsIterator->EOF) {
			$result = $this->invoiceCashingsIterator->FetchRow();
			return ['invoiceCashingId' => $result['id']];

		}
		return NULL;
	}
}