exchangerateupdater.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/custom/bbus/class/mnbexchangerate.class.php';
  3. require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  4. class ExchangeRateUpdater
  5. {
  6. const EUR = 'EUR';
  7. public $db;
  8. public $entities = [];
  9. public $errors;
  10. /**
  11. *
  12. */
  13. public function __construct()
  14. {
  15. global $db;
  16. $this->db = $db;
  17. }
  18. /**
  19. *
  20. */
  21. public function loadRateFromBank(string $currency): float
  22. {
  23. return (new MnbExchangeRate)->currencyExchangeRate($currency);
  24. }
  25. /**
  26. *
  27. */
  28. public function updateRate(): bool
  29. {
  30. $success = true;
  31. $this->errors = [];
  32. $eur = 1/$this->loadRateFromBank(self::EUR);
  33. $this->loadEntities();
  34. if (!empty($this->entities)) {
  35. foreach ($this->entities as $entity) {
  36. if ($this->updateEntity(self::EUR, $eur, $entity) === false) {
  37. $success = false;
  38. }
  39. }
  40. }
  41. return $success;
  42. }
  43. /**
  44. *
  45. */
  46. private function updateEntity(string $currency, float $rate, DaoMulticompany $entity): bool
  47. {
  48. $result = false;
  49. $multiCurrencyId = $this->getIdFromCode($currency, $entity->id);
  50. $currencyRate = new CurrencyRate($this->db);
  51. $currencyRate->fk_multicurrency = $multiCurrencyId;
  52. $currencyRate->entity = $entity->id;
  53. $currencyRate->date_sync = date('Y-m-d H:i:s');
  54. $currencyRate->rate = $rate;
  55. $result = $currencyRate->create(intval($multiCurrencyId));
  56. if ($result > 0) {
  57. $result = true;
  58. } else {
  59. $this->errors[] = $currency.' entity: '.$entity->label.' ('.$entity->id.')';
  60. dol_syslog("currencyRate:createRate", LOG_WARNING);
  61. }
  62. return $result;
  63. }
  64. /**
  65. *
  66. */
  67. public function loadEntities(): void
  68. {
  69. $entityDao = (new DaoMulticompany($this->db));
  70. $entityDao->getEntities(false, [], true);
  71. $this->entities = $entityDao->entities;
  72. }
  73. /**
  74. * Get id of currency from code
  75. *
  76. * @param string $code code value search
  77. * @param int $entity entity value search
  78. *
  79. * @return int 0 if not found, >0 if OK
  80. */
  81. public function getIdFromCode($code, $entity): int
  82. {
  83. $id = 0;
  84. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE code = '".$this->db->escape($code)."' AND entity = ".$entity;
  85. dol_syslog(__METHOD__, LOG_DEBUG);
  86. $resql = $this->db->query($sql);
  87. if ($resql && $obj = $this->db->fetch_object($resql)) {
  88. $id = $obj->rowid;
  89. }
  90. return $id;
  91. }
  92. /**
  93. *
  94. */
  95. public static function getExchangeRate(string $currency, int $entity): array
  96. {
  97. global $db;
  98. $result = [];
  99. $sql = "
  100. SELECT ROUND(1/cr.rate, 2) AS \"{$currency}\", cr.date_sync AS last_update
  101. FROM ".MAIN_DB_PREFIX."multicurrency AS c
  102. LEFT JOIN ".MAIN_DB_PREFIX."multicurrency_rate AS cr ON cr.fk_multicurrency=c.rowid
  103. WHERE c.code ILIKE '{$currency}'
  104. AND cr.entity={$entity}
  105. ORDER BY cr.date_sync DESC
  106. LIMIT 1
  107. ";
  108. $row = $db->query($sql);
  109. if ($row) {
  110. $obj = $db->fetch_object($row);
  111. $result = (array)$obj;
  112. $result[$currency] = (float)$result[$currency];
  113. }
  114. return $result;
  115. }
  116. }