<?php

namespace App\OfferProfileRules;

class Criterion_OwnerCorporateSegment extends CriterionAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::checkFields()
	 */
	public function checkFields(\App\OfferProfileRules\Fields $fields)
	{
		return $this->checkField(\Entity\Corporate::select($fields->idOwnerCorporate)->idSegment);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::checkAssignedValues()
	 */
	public function checkAssignedValues(\App\MessagesCollector $msgs)
	{

		// check that at least one value set
		if (($this->operation=='equal' or $this->operation=='notEqual') and !$this->values) {
			$msgs->addWarning("No company segment set.");
		}
		else {
			// check the corporate segment
			foreach($this->values as $idCorpSegment) {
				if (!\Entity\CorporateSegment::select($idCorpSegment)->recordExists()) {
					$msgs->addWarning("The corporate segment id `{$idCorpSegment}` is not valid.");
				}
			}
		}

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\OfferProfileRules\CriterionAbstract::createDescription_getEntityName()
	 */
	protected function createDescription_getEntityName($code)
	{
		return \Entity\CorporateSegment::select($code)->name;
	}

}
