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

/**
 * Supplier invoice gatherer - return invoice entity
 * @author Oana Stan
 */
class SupplierInvoices extends GathererAbstract
{

	/**
	 * Recordset resulted after $db->Execute
	 */
	protected $invoicesIterator = NULL;

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

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

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

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

		// filter only issued invoices
		if ($this->filters['invoiceIssued']) {
			$listObj->setFilter('blockTime', '>', 0);
		}

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

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

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