<?php

namespace Modules\Documents\ImportedDocuments\ProcessDocument\Model\InvoiceSuggestions;

class Corporate extends GeneralAbstract
{

	/**
	 * The invoices db table field name for filtering by partner.
	 * @var string
	 */
	protected $partnerDbFieldName = 'inv.idCorporate';

	/**
	 *
	 * {@inheritDoc}
	 * @see \Modules\Documents\ImportedDocuments\ProcessDocument\Model\InvoiceSuggestions\GeneralAbstract::search()
	 */
	public function search()
	{
		$invEntList = new \App\EntityList\Invoices();
		$invEntList->setColumns(['invoiceDate']);
		$invEntList->applyUserRestriction(false);
		$invEntList->setFilter($this->partnerDbFieldName, '=', $this->idPartner);
		$invEntList->setFilter('inv.cashedStatus', 'IN', ['n', 'p']);
		$invEntList->setFilter('inv.voidTime', '=', '0');
		$invEntList->setFilter('inv.temporary', '=', '0');

		// if the invoice sign is set, the suggestions will contain only the invoices with that sign
		if ($this->invoiceSign=='1') {
			$invEntList->setFilter('inv.printTotal', '>=', 0);
		}
		elseif ($this->invoiceSign=='-1') {
			$invEntList->setFilter('inv.printTotal', '<', 0);
		}

		// if this restriction is active, the suggestions won't contain the invoices that aren't issued
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Invoices/Cashings/Listing-cashInvoiceNotIssued')) {
			$invEntList->setFilter('inv.issueTime', '!=', '0');
		}

		$invEntList->setOrderBy('invoiceDate', 'ASC');

		$this->invoiceSuggestions = $invEntList->exportRecordsByColumn('id');

	}
}