<?php

namespace App\AutoProcessRules;

class ImportedDocIssue_RecordsList extends RecordsListAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\AutoProcessRules\RecordsListAbstract::getRecords()
	 */
	public function getRecords()
	{

		// GET RULE DETAILS -----------------------------------------------------------------------
		$ruleEnt = \Entity\AutoProcessRule::select($this->idRule);

		// GET
		$importedDocsListObj = new \App\EntityList\ImportedDocuments();
		$this->populateListObject($importedDocsListObj, $ruleEnt);
		$importedDocsListEx = $importedDocsListObj->exportRecords();

		$recordsList = [];
		foreach($importedDocsListEx as $record) {
			$recordsList[] = $record['id'];
		}

		return $recordsList;

	}


	/**
	 * Applies all filters on imported documents list.
	 * @param \App\EntityList\ImportedDocuments $importedDocsListObj
	 * @param \Entity\AutoProcessRule $ruleEnt
	 */
	protected function populateListObject(\App\EntityList\ImportedDocuments $importedDocsListObj, \Entity\AutoProcessRule $ruleEnt)
	{

		$filters = $this->getFilters();

		// APPLY ALL FITERS ON THE LIST -----------------------------------------------------------

		// doc category - it is actually mandatory
		if ($filters['docCategory']) {
			$importedDocsListObj->setFilter('importedDocument.docCategory', '=', $filters['docCategory']);
		}

		// add standard filters
		$importedDocsListObj->setFilter('importedDocument.issueTime', 'IS NULL');
		$importedDocsListObj->setFilter('importedDocument.systemStatus', '=', 'added');
		$importedDocsListObj->setFilter('importedDocument.status', '!=', 'blocked');

		// doc date
		if ($filters['docDate']) {
			$startDate = $filters['docDate']->getStartDate()->getActualDate();
			if ($startDate) {
				$importedDocsListObj->setFilter('importedDocument.docDate', '>=', $startDate);
			}
			$endDate = $filters['docDate']->getEndDate()->getActualDate();
			if ($endDate) {
				$importedDocsListObj->setFilter('importedDocument.docDate', '<=', $endDate);
			}
		}
		// doc add date
		if ($filters['docAddDate']) {
			$startDate = $filters['docAddDate']->getStartDate()->getActualDate();
			if ($startDate) {
				$importedDocsListObj->setFilter('importedDocument.addTime', '>=', $startDate.' 00:00:00');
			}
			$endDate = $filters['docAddDate']->getEndDate()->getActualDate();
			if ($endDate) {
				$importedDocsListObj->setFilter('importedDocument.addTime', '<=', $endDate.' 23:59:59');
			}
		}

		// external systems
		if ($filters['docExternalSystems']) {
			$importedDocsListObj->setFilter('importedDocument.idExternalSystem', 'IN', $filters['docExternalSystems']);
		}

		// doc currency
		if ($filters['docCurrencies']) {
			$importedDocsListObj->setFilter('importedDocument.docCurrency', 'IN', $filters['docCurrencies']);
		}

		// doc type
		if ($filters['docType']) {
			$importedDocsListObj->setFilter('importedDocument.docType', '=', $filters['docType']);
		}

		// apply partner type filter
		if ($filters['clientType'] == 'corporate') {
			if ($filters['corporates']) {
				$importedDocsListObj->setFilter('importedDocument.idCorporate', 'IN', $filters['corporates']);
			} else {
				$importedDocsListObj->setFilter('importedDocument.idCorporate', '!=', 0);
			}
			// apply corporate segment filtering
			if ($filters['corporateGroups']) {
				$corporateGroupsHelper = new \App\ReportsData\Helpers\PartnerGroups\Corporate();
				$corporateGroupsHelper->setGroupElementsLinear($filters['corporateGroups']);
				$idCorporatesList = $corporateGroupsHelper->getPartnerIdsList();
				if ($idCorporatesList) {
					$importedDocsListObj->setFilter('importedDocument.idCorporate', 'IN', $idCorporatesList);
				} else {
					$importedDocsListObj->setCustomFilter('0');
				}
			}
		}
		if ($filters['clientType'] == 'customer') {
			if ($filters['customers']) {
				$importedDocsListObj->setFilter('importedDocument.idCustomer', 'IN', $filters['customers']);
			} else {
				$importedDocsListObj->setFilter('importedDocument.idCustomer', '!=', 0);
			}
		}
		if ($filters['clientType'] == 'supplier') {
			if ($filters['suppliers']) {
				$importedDocsListObj->setFilter('importedDocument.idSupplier', 'IN', $filters['suppliers']);
			} else {
				$importedDocsListObj->setFilter('importedDocument.idSupplier', '!=', 0);
			}
		}

		// set order by (oldest documents first)
		$importedDocsListObj->setOrderBy('id', 'ASC');

		// set the limit
		if ($ruleEnt->recordsCountOnExecution) {
			$importedDocsListObj->setLimit($ruleEnt->recordsCountOnExecution);
		}

	}

}



