<?php
namespace Modules\Admin\Config\ImportedOrders\Services;

/**
 * Model class for listing
 * @author adrian.voicu
 */
class Model extends \App\Mvc\Model_ListingAbstract
{

	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{
		$filters = $listingParams->getFilters();
		$listObj = new \App\EntityList\ImportedOrdersMatches();
		$listObj->setColumns(['importedOrdersMatches.entityType', 'importedOrdersMatches.idEntityExtSys', 'importedOrdersMatches.idSystem',
			'importedOrdersMatches.idEntityTina', 'importedOrdersMatches.idUpdateUser', 'importedOrdersMatches.lastUpdateTime',
			'importedOrdersSystems.name' => 'systemName'
		]);
		$listObj->setFilter('importedOrdersMatches.entityType', '=', 'serviceType');

		if ($filters['idSystem']) {
			$listObj->setFilter('importedOrdersMatches.idSystem', '=', $filters['idSystem']);
		}

		if ($filters['externalServiceCode']) {
			$listObj->setFilter('importedOrdersMatches.idEntityExtSys', '=', $filters['externalServiceCode']);
		}

		if ($filters['idService']) {
			$listObj->setFilter('importedOrdersMatches.idEntityTina', '=', $filters['idService']);
		}

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

		$result = $listObj->exportRecords();
		$services = \App\Config\Services_Basic::getInstance()->getListLinear();
		$list = [];

		if ($result) {
			foreach ($result as $row) {
				$list[] = [
						'id' => $row['idSystem'].'|'.$row['idEntityExtSys'],
						'idSystem' => $row['idSystem'],
						'entityType' => $row['entityType'],
						'externalServiceCode' => $row['idEntityExtSys'],
						'name' => $row['systemName'],
						'tinaService' => $services[$row['idEntityTina']]['name'].' ('.$row['idEntityTina'].')',
						'idAddUser' => $row['idUpdateUser'],
						'lastUpdateTime' => $row['lastUpdateTime']
				];
			}
		}

		$this->recordsList = $list;
		$this->totalRecordsCount = $listObj->getRecordsCount();

	}
}