<?php

namespace Modules\Partners\Suppliers\ContactPersons;

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 corporates list
		$listObj = new \App\EntityList\ContactPersons();
		$listObj->setColumns(['addTime', 'idCompany', 'title', 'firstName', 'lastName', 'email',
			'telephone', 'telephoneMobile', 'fax', 'job'
		]);

		$listObj->setFilter('type', 'LIKE', 'supplier');

		// corporate filter
		if ($filters['supplier']['id']) {
			$listObj->setFilter('idCompany', 'LIKE', $filters['supplier']['id']);
		}

		// name filter
		if ($filters['name']) {
			$listObj->setFilter('CONCAT(lastName, firstName)', 'LIKE', "%{$filters['name']}%");
		}

		// text filter
		if ($filters['text']) {
			$listObj->setCustomFilter('email LIKE ? OR telephone LIKE ? OR telephoneMobile LIKE ? OR comments LIKE ?',
					["%{$filters['text']}%", "%{$filters['text']}%", "%{$filters['text']}%", "%{$filters['text']}%"]);
		}

		$listObj = $this->entityList_inheritListingParams($listingParams, $listObj);

		// these must be called in this specific order
		$this->recordsList = $listObj->exportRecords();
		$this->totalRecordsCount = $listObj->getRecordsCount();
	}

}
