Currencies.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ VIPFDgylDLvuc8Z8wybRdCZtmB9+daP/bsaoZ7cT994=
  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. * currencies table service class
  14. */
  15. namespace Ppb\Service\Table;
  16. use Ppb\Db\Table\Currencies as CurrenciesTable;
  17. class Currencies extends AbstractServiceTable
  18. {
  19. /**
  20. *
  21. * default currency
  22. *
  23. * @var string
  24. */
  25. protected $_defaultCurrency;
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $settings = $this->getSettings();
  30. if (isset($settings['currency'])) {
  31. $this->setDefaultCurrency(
  32. $settings['currency']);
  33. }
  34. $this->setTable(
  35. new CurrenciesTable());
  36. }
  37. /**
  38. *
  39. * set the default currency variable
  40. *
  41. * @param string $currency
  42. *
  43. * @return \Ppb\Service\Table\Currencies
  44. */
  45. public function setDefaultCurrency($currency)
  46. {
  47. $this->_defaultCurrency = $currency;
  48. return $this;
  49. }
  50. /**
  51. *
  52. * get all currencies
  53. * to be used for the currency selector
  54. *
  55. * @param string $column
  56. *
  57. * @return array
  58. */
  59. public function getMultiOptions($column = null)
  60. {
  61. $data = array();
  62. $translate = $this->getTranslate();
  63. $rows = $this->_table->fetchAll();
  64. foreach ($rows as $row) {
  65. if ($column === null) {
  66. $column = 'description';
  67. }
  68. $data[(string)$row['iso_code']] = $translate->_($row[$column]);
  69. }
  70. return $data;
  71. }
  72. /**
  73. *
  74. * get all table columns needed to generate the currencies management
  75. * table in the admin area
  76. *
  77. * @return array
  78. */
  79. public function getColumns()
  80. {
  81. return array(
  82. array(
  83. 'label' => $this->_('ISO Code'),
  84. 'class' => 'size-mini',
  85. 'element_id' => 'iso_code',
  86. ),
  87. array(
  88. 'label' => $this->_('Symbol'),
  89. 'class' => 'size-mini',
  90. 'element_id' => 'symbol',
  91. ),
  92. array(
  93. 'label' => $this->_('Description'),
  94. 'element_id' => 'description',
  95. ),
  96. array(
  97. 'label' => $this->_('Conversion Rate'),
  98. 'class' => 'size-medium',
  99. 'element_id' => 'conversion_rate',
  100. ),
  101. array(
  102. 'label' => $this->_('Delete'),
  103. 'class' => 'size-mini',
  104. 'element_id' => array(
  105. 'id', 'delete'
  106. ),
  107. ),
  108. );
  109. }
  110. /**
  111. *
  112. * get all form elements that are needed to generate the currencies
  113. * management table in the admin area
  114. *
  115. * @return array
  116. */
  117. public function getElements()
  118. {
  119. return array(
  120. array(
  121. 'id' => 'id',
  122. 'element' => 'hidden',
  123. ),
  124. array(
  125. 'id' => 'iso_code',
  126. 'element' => 'text',
  127. 'attributes' => array(
  128. 'class' => 'form-control input-mini',
  129. ),
  130. ),
  131. array(
  132. 'id' => 'symbol',
  133. 'element' => 'text',
  134. 'attributes' => array(
  135. 'class' => 'form-control input-mini',
  136. ),
  137. ),
  138. array(
  139. 'id' => 'description',
  140. 'element' => 'text',
  141. 'attributes' => array(
  142. 'class' => 'form-control input-large',
  143. ),
  144. ),
  145. array(
  146. 'id' => 'conversion_rate',
  147. 'element' => 'text',
  148. 'prefix' => '1 ' . $this->_defaultCurrency . ' = ',
  149. 'attributes' => array(
  150. 'class' => 'form-control input-small',
  151. ),
  152. ),
  153. array(
  154. 'id' => 'delete',
  155. 'element' => 'checkbox',
  156. ),
  157. );
  158. }
  159. /**
  160. *
  161. * fetches all matched rows
  162. *
  163. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  164. * @param string|array $order
  165. * @param int $count
  166. * @param int $offset
  167. *
  168. * @return array
  169. */
  170. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  171. {
  172. if ($order === null) {
  173. $order = 'iso_code ASC';
  174. }
  175. return parent::fetchAll($where, $order, $count, $offset);
  176. }
  177. /**
  178. *
  179. * convert an amount from a currency to another
  180. * using iso codes
  181. *
  182. * @param float $amount
  183. * @param string|null $currencyFrom iso code of the currency to be converted
  184. * @param string|null $currencyTo iso code of the currency the conversion is made to
  185. *
  186. * @return float
  187. */
  188. public function convertAmount($amount, $currencyFrom = null, $currencyTo = null)
  189. {
  190. if ($currencyFrom == $currencyTo || $currencyFrom === null) {
  191. return $amount;
  192. }
  193. $settings = $this->getSettings();
  194. if ($currencyTo === null) {
  195. $currencyTo = $settings['currency'];
  196. }
  197. $from = $this->findBy('iso_code', $currencyFrom);
  198. $to = $this->findBy('iso_code', $currencyTo);
  199. return number_format((($to['conversion_rate'] / $from['conversion_rate']) * $amount),
  200. $settings['currency_decimals'], '.', '');
  201. }
  202. /**
  203. *
  204. * return the currency symbol of a currency based on the iso code
  205. * if a symbol is not set, will return the iso code input
  206. *
  207. * @param string $isoCode
  208. *
  209. * @return string
  210. */
  211. public function getSymbol($isoCode)
  212. {
  213. $row = $this->findBy('iso_code', $isoCode);
  214. if (!empty($row['symbol'])) {
  215. return $row['symbol'];
  216. }
  217. return $isoCode;
  218. }
  219. }