<?php
 namespace App\OfferProfileRules; class Criterion_ServicePrice extends CriterionAbstract { public function __construct($operation, $price, $currency) { parent::__construct($operation, ['value'=>$price, 'currency'=>$currency]); } public function checkFields(\App\OfferProfileRules\Fields $fields) { if ($fields->servicePriceValue > 0 and $fields->servicePriceCurrency != $this->values['currency']) { if (!$fields->currencyDate) { throw new \Exception("You must set the currency date."); } $servicePrice = \App\CurrencyExchange\Rates::getInstance($fields->currencyDate)->convert($fields->servicePriceValue, $fields->servicePriceCurrency, $this->values['currency']); return $this->checkField($servicePrice); } elseif ($fields->servicePriceValue and $fields->servicePriceValue > 0 and $fields->servicePriceCurrency == $this->values['currency']) { return $this->checkField($fields->servicePriceValue); } elseif ($fields->servicePriceValue or $fields->servicePriceValue == 0) { return $this->checkField(0); } } protected function checkField($value) { switch ($this->operation) { case 'equal': { if ($value == $this->values['value']) { return true; } return false; } case 'smaller': { if ($value < $this->values['value']) { return true; } return false; } case 'smallerOrEqual': { if ($value <= $this->values['value']) { return true; } return false; } case 'greater': { if ($value > $this->values['value']) { return true; } return false; } case 'greaterOrEqual': { if ($value >= $this->values['value']) { return true; } return false; } default: { throw new \Exception("Invalid operation `{$this->operation}`."); } } } public function checkAssignedValues(\App\MessagesCollector $msgs) { if (count($this->values)==0 or !$this->values['currency'] or !$this->values['value'] ) { $msgs->addWarning("The service price and the currency must be both set."); } if ($this->values['currency'] and !\App\Config\Currencies::getInstance()->check($this->values['currency'])) { $msgs->addWarning("The currency is invalid."); } if ($this->values['value'] and (!is_numeric($this->values['value']) or $this->values['value']<0) ) { $msgs->addWarning("The service price must be numeric and greater than 0."); } } protected function createDescription_getEntityName($code) { return $code; } public function createDescription() { $_LANG = \App\Loader_Lang::getInstance()->getAll(); $entityField = $this->getEntity(); return sprintf($_LANG['OfferProfileRules']['criterionDescription2'], $_LANG['OfferProfileRules']['criterionFieldName'][$entityField], '<i>'.$_LANG['OfferProfileRules']['criterionOperation'][$this->operation].'</i>', '<b>'.implode(' ', $this->values).'</b>'); } }