<?php

namespace Modules\Orders\Apollo\ServicesList;

class Model extends \App\Mvc\Model_ListingAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Model_ListingAbstract::list_execute()
	 */
	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{
		$filters=$listingParams->getFilters();

		// get the departments list
		$listObj = new \App\EntityList\Apollo();
		$listObj->setColumns(['serviceCode', 'transactionStatus', 'transactionData', 'docNumber', 'docDate', 'addTime', ]);

		$listObj->setFilter("serv.transactionStatus", 'IN', ['new', 'reissue', 'reissued', 'refund', 'refunded', 'void', 'voided', ]);

		if ($filters['general']) {
			$listObj->setCustomFilter("serv.docNumber=? OR serv.idApolloBookUser=? OR serv.idApolloIssueUser=? OR serv.idApolloBookOffice=? OR serv.idApolloIssueOffice=?", [
				$filters['general'], $filters['general'], $filters['general'], $filters['general'], $filters['general'],
			]);
		}

		if ($filters['docNumber']) {
			$listObj->setFilter("serv.docNumber", '=', $filters['docNumber']);
		}

		if ($filters['docDate']['start']) {
			$listObj->setFilter("serv.docDate", ">=", $filters['docDate']['start']);
		}
		if ($filters['docDate']['end']) {
			$listObj->setFilter("serv.docDate", "<=", $filters['docDate']['end']);
		}

		if ($filters['serviceCode']) {
			$listObj->setFilter("serv.serviceCode", "=", $filters['serviceCode']);
		}
		if ($filters['transactionStatus']) {
			$listObj->setFilter("serv.transactionStatus", "=", $filters['transactionStatus']);
		}

		$listObj->setOrderBy('serv.id', 'desc');
		$listObj->setLimit($listingParams->getResultsPerPage()*$listingParams->getCurrentPage(), $listingParams->getResultsPerPage());

		// these must be called in this specific order
		$this->recordsList = 		new \App\EntityList\RecordIterator($listObj->getExecution(), "\Entity\ApolloService");
		$this->totalRecordsCount = 	$listObj->getRecordsCount();
	}
}
