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

/**
 * Supplier payment gatherer - return list with id supplier payment
 * @author Oana Stan
 */
class SupplierPayment extends GathererAbstract
{
	/**
	 * Recordset resulted after $db->Execute
	 */
	protected $invoicePaymentsIterator = NULL;

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

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

		////////// 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->setFilter("doc.blockTime", '>', 0);
		}

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

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

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

			return ['invoicePaymentId' => $result['id']];

		}
		return NULL;
	}
}