<?php
namespace Modules\Partners\Corporates\CreditCards;

class Model extends \App\Mvc\Model_ListingAbstract
{
	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Model_ListingAbstract::list_execute()
	 */
	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{
		$filters = $listingParams->getFilters();

		$listObj = new \App\EntityList\CorporateCreditCards();
		$listObj->setColumns(['corporateCreditCards.idCorporate', 'corporateCreditCards.idCreditCard', 'corporateCreditCards.idAddUser', 'corporateCreditCards.addTime', 'creditCards.company', 'creditCards.expireDate']);

		if ($filters['corporate']['id']) {
			$listObj->setFilter('corporateCreditCards.idCorporate', '=', $filters['corporate']['id']);
		}

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

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

		$corporateCreditCards = $listObj->exportRecords();
		$corporateCreditCardsCount = $listObj->getRecordsCount();

		$recordList = [];
		foreach ($corporateCreditCards as $corporateCreditCard) {
			$corporateCC = \Entity\CorporateCreditCard::select($corporateCreditCard['id']);
			$corporateCreditCard['number'] = $corporateCC->cardDetails['truncatedNumber'];
			$recordList[] = $corporateCreditCard;
		}

		$this->recordsList = $recordList;
		$this->totalRecordsCount = $corporateCreditCardsCount;
	}

}