Amount.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ ccOaTUGWwug64yADwgVdcYD8vNvpFUaM/pJPVcmSW58=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.6
  11. */
  12. /**
  13. * amount display view helper class
  14. */
  15. namespace Ppb\View\Helper;
  16. use Cube\View\Helper\AbstractHelper,
  17. Cube\Db\Table\Rowset,
  18. Ppb\Service\Table\Currencies as CurrenciesService;
  19. class Amount extends AbstractHelper
  20. {
  21. /**
  22. * the maximum amount allowed for a decimal input value
  23. */
  24. const MAX_AMOUNT = 99999999999;
  25. /**
  26. * default display format
  27. */
  28. const DEFAULT_FORMAT = '%s';
  29. /**
  30. *
  31. * currencies rowset
  32. *
  33. * @var \Cube\Db\Table\Rowset\AbstractRowset
  34. */
  35. protected $_currencies;
  36. /**
  37. *
  38. * settings array
  39. *
  40. * @var array
  41. */
  42. protected $_settings;
  43. /**
  44. *
  45. * zero display value
  46. *
  47. * @var bool
  48. */
  49. protected $_zero = null;
  50. /**
  51. *
  52. * class constructor
  53. *
  54. * @param array $settings the settings array
  55. */
  56. public function __construct(array $settings)
  57. {
  58. $this->setSettings($settings);
  59. }
  60. /**
  61. *
  62. * set settings array
  63. *
  64. * @param array $settings
  65. *
  66. * @return \Ppb\View\Helper\Amount
  67. */
  68. public function setSettings(array $settings)
  69. {
  70. $this->_settings = $settings;
  71. return $this;
  72. }
  73. /**
  74. *
  75. * fetch currencies from table
  76. *
  77. * @param string $isoCode currency to fetch (by iso code)
  78. *
  79. * @return \Cube\Db\Table\Row\AbstractRow|null selected or default currency row or null if requested currency cannot be found
  80. */
  81. public function getCurrency($isoCode = null)
  82. {
  83. if (!$this->_currencies instanceof Rowset) {
  84. $service = new CurrenciesService();
  85. $this->_currencies = $service->fetchAll();
  86. }
  87. if ($isoCode === null) {
  88. $isoCode = $this->_settings['currency'];
  89. }
  90. foreach ($this->_currencies as $currency) {
  91. if ($currency['iso_code'] == $isoCode) {
  92. return $currency;
  93. }
  94. }
  95. return null;
  96. }
  97. /**
  98. *
  99. * set zero value
  100. *
  101. * @param string $zero
  102. *
  103. * @return \Ppb\View\Helper\Amount
  104. */
  105. public function setZero($zero)
  106. {
  107. $this->_zero = $zero;
  108. return $this;
  109. }
  110. /**
  111. *
  112. * amount view helper
  113. *
  114. * @param float $amount the amount to be displayed
  115. * @param string $currency the currency - default currency used if this is null
  116. * @param string $format display format, used if custom outputs are needed
  117. * eg: (+%s)
  118. * @param bool $overrideZero
  119. *
  120. * @return string|$this
  121. */
  122. public function amount($amount = null, $currency = null, $format = null, $overrideZero = false)
  123. {
  124. if ($amount === false) {
  125. return $this;
  126. }
  127. $translate = $this->getTranslate();
  128. if ($amount >= self::MAX_AMOUNT) {
  129. return $translate->_('Above');
  130. }
  131. if ($format === null) {
  132. $format = self::DEFAULT_FORMAT;
  133. }
  134. if ($amount == 0 && $overrideZero === false) {
  135. if ($this->_settings['display_free_fees']) {
  136. return sprintf($format, $translate->_('Free'));
  137. }
  138. return $translate->_($this->_zero);
  139. }
  140. $data = $this->getCurrency($currency);
  141. $symbol = $data['iso_code'];
  142. $spacing = ' ';
  143. if (!empty($data['symbol'])) {
  144. $symbol = $data['symbol'];
  145. $spacing = '';
  146. }
  147. switch ($this->_settings['currency_format']) {
  148. case '1':
  149. $amount = number_format($amount, $this->_settings['currency_decimals'], '.', ',');
  150. break;
  151. default:
  152. $amount = number_format($amount, $this->_settings['currency_decimals'], ',', '.');
  153. break;
  154. }
  155. switch ($this->_settings['currency_position']) {
  156. case '1':
  157. $output = $symbol . $spacing . $amount;
  158. break;
  159. default:
  160. $output = $amount . $spacing . $symbol;
  161. break;
  162. }
  163. return sprintf($format, $output);
  164. }
  165. }