<?php

namespace Modules\Partners\Customers\FrequentBuyerCards;

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 customer list
		$listObj = new \App\EntityList\FrequentBuyerCards();

		$listObj->setColumns([
				'frequentBuyerCards.cardNumber',
				'frequentBuyerCards.idCustomer',
				'frequentBuyerCards.addTime',
				'frequentBuyerCards.idAddUser',
				'frequentBuyerCards.lastUpdateTime',
				'frequentBuyerCards.idUpdateUser',
		]);

		//concatenate suppliers with pipe
		$listObj->setCustomColumn("GROUP_CONCAT(DISTINCT suppliers.`idSupplier` ORDER BY suppliers.`idSupplier` SEPARATOR '|')", "suppliers", "suppliers");

		$listObj->setGroupBy('frequentBuyerCards.id');

		// card filter
		if ($filters['cardNumber'] && strlen($filters['cardNumber'])) {
			$listObj->setFilter("frequentBuyerCards.`cardNumber`", "LIKE", "%{$filters['cardNumber']}%");
		}

		// customer filter
		if($filters['idCustomer']['id']) {
			$listObj->setFilter("frequentBuyerCards.`idCustomer`", "=", $filters['idCustomer']['id']);
		}

		// suppliers filter
		if ($filters['idSupplier']['id']) {
			$listObj->setFilter("suppliers.`idSupplier`", "=", $filters['idSupplier']['id']);
		}

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

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

	}

}
