PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/phreedom/classes/currency.php

http://phreedom.googlecode.com/
PHP | 251 lines | 213 code | 13 blank | 25 comment | 32 complexity | 80dbf62a348e165ebfa4bc67fa39c5da MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. // +-----------------------------------------------------------------+
  3. // | PhreeBooks Open Source ERP |
  4. // +-----------------------------------------------------------------+
  5. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  6. // | http://www.PhreeSoft.com |
  7. // +-----------------------------------------------------------------+
  8. // | This program is free software: you can redistribute it and/or |
  9. // | modify it under the terms of the GNU General Public License as |
  10. // | published by the Free Software Foundation, either version 3 of |
  11. // | the License, or any later version. |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  16. // | GNU General Public License for more details. |
  17. // +-----------------------------------------------------------------+
  18. // Path: /modules/phreedom/classes/currency.php
  19. //
  20. // define how do we update currency exchange rates. Possible values are 'oanda' 'yahoo'
  21. // xe no longer works as of 2012-02-01
  22. define('CURRENCY_SERVER_PRIMARY', 'oanda');
  23. define('CURRENCY_SERVER_BACKUP', 'yahoo');
  24. class currency {
  25. function __construct() {
  26. $this->security_id = $_SESSION['admin_security'][SECURITY_ID_CONFIGURATION];
  27. $this->db_table = TABLE_CURRENCIES;
  28. $this->title = SETUP_TITLE_CURRENCIES;
  29. $this->extra_buttons = true;
  30. $this->help_path = '07.08.02';
  31. $this->def_currency = DEFAULT_CURRENCY;
  32. }
  33. function btn_save($id = '') {
  34. global $db, $messageStack;
  35. if ($this->security_id < 3) {
  36. $messageStack->add_session(ERROR_NO_PERMISSION,'error');
  37. return false;
  38. }
  39. $title = db_prepare_input($_POST['title']);
  40. $code = strtoupper(db_prepare_input($_POST['code']));
  41. if ($_POST['decimal_precise'] == '') $_POST['decimal_precise'] = $_POST['decimal_places'];
  42. $sql_data_array = array(
  43. 'title' => $title,
  44. 'code' => $code,
  45. 'symbol_left' => db_prepare_input($_POST['symbol_left']),
  46. 'symbol_right' => db_prepare_input($_POST['symbol_right']),
  47. 'decimal_point' => db_prepare_input($_POST['decimal_point']),
  48. 'thousands_point' => db_prepare_input($_POST['thousands_point']),
  49. 'decimal_places' => db_prepare_input($_POST['decimal_places']),
  50. 'decimal_precise' => db_prepare_input($_POST['decimal_precise']),
  51. 'value' => db_prepare_input($_POST['value']),
  52. );
  53. if ($id) {
  54. db_perform($this->db_table, $sql_data_array, 'update', "currencies_id = " . (int)$id);
  55. gen_add_audit_log(SETUP_LOG_CURRENCY . TEXT_UPDATE, $title);
  56. } else {
  57. db_perform($this->db_table, $sql_data_array);
  58. gen_add_audit_log(SETUP_LOG_CURRENCY . TEXT_ADD, $title);
  59. }
  60. if (isset($_POST['default']) && ($_POST['default'] == 'on')) {
  61. // first check to see if there are any general ledger entries
  62. $result = $db->Execute("select id from " . TABLE_JOURNAL_MAIN . " limit 1");
  63. if ($result->RecordCount() > 0) {
  64. $messageStack->add_session(SETUP_ERROR_CANNOT_CHANGE_DEFAULT,'error');
  65. } else {
  66. write_configure('DEFAULT_CURRENCY', db_input($code));
  67. $db->Execute("alter table " . TABLE_JOURNAL_MAIN . "
  68. change currencies_code currencies_code CHAR(3) NOT NULL DEFAULT '" . db_input($code) . "'");
  69. $this->def_currency = db_input($code);
  70. $this->btn_update();
  71. }
  72. }
  73. return true;
  74. }
  75. function btn_update() { // updates the currency rates
  76. global $db, $messageStack;
  77. $message = array();
  78. /* commented out so everyone can update currency exchange rates
  79. validate_security($security_level, 1);
  80. */
  81. $server_used = CURRENCY_SERVER_PRIMARY;
  82. $currency = $db->Execute("select currencies_id, code, title from " . $this->db_table);
  83. while (!$currency->EOF) {
  84. if ($currency->fields['code'] == $this->def_currency) { // skip default currency
  85. $currency->MoveNext();
  86. continue;
  87. }
  88. $quote_function = 'quote_'.CURRENCY_SERVER_PRIMARY;
  89. $rate = $this->$quote_function($currency->fields['code'], $this->def_currency);
  90. if (empty($rate) && (gen_not_null(CURRENCY_SERVER_BACKUP))) {
  91. $message[] = sprintf(SETUP_WARN_PRIMARY_SERVER_FAILED, CURRENCY_SERVER_PRIMARY, $currency->fields['title'], $currency->fields['code']);
  92. $messageStack->add(sprintf(SETUP_WARN_PRIMARY_SERVER_FAILED, CURRENCY_SERVER_PRIMARY, $currency->fields['title'], $currency->fields['code']), 'caution');
  93. $quote_function = 'quote_'.CURRENCY_SERVER_BACKUP;
  94. $rate = $this->$quote_function($currency->fields['code'], $this->def_currency);
  95. $server_used = CURRENCY_SERVER_BACKUP;
  96. }
  97. if ($rate <> 0) {
  98. $db->Execute("update " . $this->db_table . " set value = '" . $rate . "', last_updated = now()
  99. where currencies_id = '" . (int)$currency->fields['currencies_id'] . "'");
  100. $message[] = sprintf(SETUP_INFO_CURRENCY_UPDATED, $currency->fields['title'], $currency->fields['code'], $server_used);
  101. $messageStack->add(sprintf(SETUP_INFO_CURRENCY_UPDATED, $currency->fields['title'], $currency->fields['code'], $server_used), 'success');
  102. } else {
  103. $message[] = sprintf(SETUP_ERROR_CURRENCY_INVALID, $currency->fields['title'], $currency->fields['code'], $server_used);
  104. $messageStack->add(sprintf(SETUP_ERROR_CURRENCY_INVALID, $currency->fields['title'], $currency->fields['code'], $server_used), 'error');
  105. }
  106. $currency->MoveNext();
  107. }
  108. if (sizeof($message) > 0) $this->message = implode("\n", $message);
  109. return true;
  110. }
  111. function quote_oanda($code, $base = DEFAULT_CURRENCY) {
  112. $page = file('http://www.oanda.com/convert/fxdaily?value=1&redirected=1&exch='.$code.'&format=CSV&dest=Get+Table&sel_list=' . $base);
  113. $match = array();
  114. preg_match('/(.+),(\w{3}),([0-9.]+),([0-9.]+)/i', implode('', $page), $match);
  115. return (sizeof($match) > 0) ? $match[3] : false;
  116. }
  117. function quote_yahoo($to, $from = DEFAULT_CURRENCY) {
  118. $page = file_get_contents('http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='.$from.$to.'=X');
  119. if ($page) $parts = explode(',', trim($page));
  120. return ($parts[1] > 0) ? $parts[1] : false;
  121. }
  122. function btn_delete($id = 0) {
  123. global $db, $messageStack;
  124. if ($this->security_id < 4) {
  125. $messageStack->add_session(ERROR_NO_PERMISSION, 'error');
  126. return false;
  127. }
  128. // Can't delete default currency or last currency
  129. $result = $db->Execute("select currencies_id from " . $this->db_table . " where code = '" . DEFAULT_CURRENCY . "'");
  130. if ($result->fields['currencies_id'] == $id) {
  131. $messageStack->add(ERROR_CANNOT_DELETE_DEFAULT_CURRENCY, 'error');
  132. return false;
  133. }
  134. $result = $db->Execute("select code, title from " . $this->db_table . " where currencies_id = '" . $id . "'");
  135. $test_1 = $db->Execute("select id from " . TABLE_JOURNAL_MAIN . " where currencies_code = '" . $result->fields['code'] . "' limit 1");
  136. if ($test_1->RecordCount() > 0) {
  137. $messageStack->add(ERROR_CURRENCY_DELETE_IN_USE, 'error');
  138. return false;
  139. }
  140. $db->Execute("delete from " . $this->db_table . " where currencies_id = '" . $id . "'");
  141. gen_add_audit_log(SETUP_LOG_CURRENCY . TEXT_DELETE, $result->fields['title']);
  142. return true;
  143. }
  144. function build_main_html() {
  145. global $db, $messageStack;
  146. $content = array();
  147. $content['thead'] = array(
  148. 'value' => array(SETUP_CURRENCY_NAME, SETUP_CURRENCY_CODES, TEXT_VALUE, TEXT_ACTION),
  149. 'params' => 'width="100%" cellspacing="0" cellpadding="1"',
  150. );
  151. $result = $db->Execute("select currencies_id, title, code, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, last_updated, value
  152. from " . TABLE_CURRENCIES);
  153. $rowCnt = 0;
  154. while (!$result->EOF) {
  155. $actions = '';
  156. if ($this->security_id > 1) $actions .= html_icon('actions/edit-find-replace.png', TEXT_EDIT, 'small', 'onclick="loadPopUp(\'currency_edit\', ' . $result->fields['currencies_id'] . ')"') . chr(10);
  157. if ($this->security_id > 3 && $result->fields['code'] <> DEFAULT_CURRENCY) $actions .= html_icon('emblems/emblem-unreadable.png', TEXT_DELETE, 'small', 'onclick="if (confirm(\'' . SETUP_CURR_DELETE_INTRO . '\')) subjectDelete(\'currency\', ' . $result->fields['currencies_id'] . ')"') . chr(10);
  158. $content['tbody'][$rowCnt] = array(
  159. array('value' => DEFAULT_CURRENCY==$result->fields['code'] ? '<b>'.htmlspecialchars($result->fields['title']).' ('.TEXT_DEFAULT.')</b>' : htmlspecialchars($result->fields['title']),
  160. 'params'=> 'style="cursor:pointer" onclick="loadPopUp(\'currency_edit\',\''.$result->fields['currencies_id'].'\')"'),
  161. array('value' => $result->fields['code'],
  162. 'params'=> 'style="cursor:pointer" onclick="loadPopUp(\'currency_edit\',\''.$result->fields['currencies_id'].'\')"'),
  163. array('value' => number_format($result->fields['value'], 8),
  164. 'params'=> 'style="cursor:pointer" onclick="loadPopUp(\'currency_edit\',\''.$result->fields['currencies_id'].'\')"'),
  165. array('value' => $actions,
  166. 'params'=> 'align="right"'),
  167. );
  168. $result->MoveNext();
  169. $rowCnt++;
  170. }
  171. return html_datatable('currency_table', $content);
  172. }
  173. function build_form_html($action, $id) {
  174. global $db;
  175. $sql = "select * from " . $this->db_table . " where currencies_id = '" . $id . "'";
  176. $result = $db->Execute($sql);
  177. if ($action == 'new') {
  178. $cInfo = '';
  179. } else {
  180. $cInfo = new objectInfo($result->fields);
  181. }
  182. $output = '<table class="ui-widget" style="border-style:none;width:100%">' . chr(10);
  183. $output .= ' <thead class="ui-widget-header">' . "\n";
  184. $output .= ' <tr>' . chr(10);
  185. $output .= ' <th colspan="2">' . ($action=='new' ? SETUP_INFO_HEADING_NEW_CURRENCY : SETUP_INFO_HEADING_EDIT_CURRENCY) . '</th>' . chr(10);
  186. $output .= ' </tr>' . chr(10);
  187. $output .= ' </thead>' . "\n";
  188. $output .= ' <tbody class="ui-widget-content">' . "\n";
  189. $output .= ' <tr>' . chr(10);
  190. $output .= ' <td colspan="2">' . ($action=='new' ? SETUP_CURR_INSERT_INTRO : SETUP_CURR_EDIT_INTRO) . '</td>' . chr(10);
  191. $output .= ' </tr>' . chr(10);
  192. $output .= ' <tr>' . chr(10);
  193. $output .= ' <td>' . SETUP_INFO_CURRENCY_TITLE . '</td>' . chr(10);
  194. $output .= ' <td nowrap="nowrap">' . html_input_field('title', $cInfo->title, '', true) . '</td>' . chr(10);
  195. $output .= ' </tr>' . chr(10);
  196. $output .= ' <tr>' . chr(10);
  197. $output .= ' <td>' . SETUP_INFO_CURRENCY_CODE . '</td>' . chr(10);
  198. $output .= ' <td nowrap="nowrap">' . html_input_field('code', $cInfo->code, '', true) . '</td>' . chr(10);
  199. $output .= ' </tr>' . chr(10);
  200. $output .= ' <tr>' . chr(10);
  201. $output .= ' <td>' . SETUP_INFO_CURRENCY_SYMBOL_LEFT . '</td>' . chr(10);
  202. $output .= ' <td>' . html_input_field('symbol_left', htmlspecialchars($cInfo->symbol_left)) . '</td>' . chr(10);
  203. $output .= ' </tr>' . chr(10);
  204. $output .= ' <tr>' . chr(10);
  205. $output .= ' <td>' . SETUP_INFO_CURRENCY_SYMBOL_RIGHT . '</td>' . chr(10);
  206. $output .= ' <td>' . html_input_field('symbol_right', htmlspecialchars($cInfo->symbol_right)) . '</td>' . chr(10);
  207. $output .= ' </tr>' . chr(10);
  208. $output .= ' <tr>' . chr(10);
  209. $output .= ' <td>' . SETUP_INFO_CURRENCY_DECIMAL_POINT . '</td>' . chr(10);
  210. $output .= ' <td nowrap="nowrap">' . html_input_field('decimal_point', $cInfo->decimal_point, '', true) . '</td>' . chr(10);
  211. $output .= ' </tr>' . chr(10);
  212. $output .= ' <tr>' . chr(10);
  213. $output .= ' <td>' . SETUP_INFO_CURRENCY_THOUSANDS_POINT . '</td>' . chr(10);
  214. $output .= ' <td>' . html_input_field('thousands_point', $cInfo->thousands_point) . '</td>' . chr(10);
  215. $output .= ' </tr>' . chr(10);
  216. $output .= ' <tr>' . chr(10);
  217. $output .= ' <td>' . SETUP_INFO_CURRENCY_DECIMAL_PLACES . '</td>' . chr(10);
  218. $output .= ' <td>' . html_input_field('decimal_places', $cInfo->decimal_places, '', true) . '</td>' . chr(10);
  219. $output .= ' </tr>' . chr(10);
  220. $output .= ' <tr>' . chr(10);
  221. $output .= ' <td>' . SETUP_INFO_CURRENCY_DECIMAL_PRECISE . '</td>' . chr(10);
  222. $output .= ' <td nowrap="nowrap">' . html_input_field('decimal_precise', $cInfo->decimal_precise, '', true) . '</td>' . chr(10);
  223. $output .= ' </tr>' . chr(10);
  224. $output .= ' <tr>' . chr(10);
  225. $output .= ' <td>' . SETUP_INFO_CURRENCY_VALUE . '</td>' . chr(10);
  226. $output .= ' <td>' . html_input_field('value', $cInfo->value) . '</td>' . chr(10);
  227. $output .= ' </tr>' . chr(10);
  228. if (DEFAULT_CURRENCY != $cInfo->code) {
  229. $output .= ' <tr>' . chr(10);
  230. $output .= ' <td colspan="2">' . html_checkbox_field('default', 'on', false) . ' ' . SETUP_INFO_SET_AS_DEFAULT . '</td>' . chr(10);
  231. $output .= ' </tr>' . chr(10);
  232. }
  233. $output .= ' </tbody>' . "\n";
  234. $output .= '</table>' . chr(10);
  235. return $output;
  236. }
  237. }
  238. ?>