File: /www/exchange0old/exchange/admin/cron/orpa_amount_update.php
<?php
/**
* Created by PhpStorm.
* User: Admin
* Date: 23.10.2017
* Time: 20:19
*/
if (php_sapi_name() != 'cli') {
// exit();
}
$folders = explode(DIRECTORY_SEPARATOR, pathinfo(__FILE__, PATHINFO_DIRNAME));
array_pop($folders);
array_pop($folders);
include(implode(DIRECTORY_SEPARATOR, $folders) . '/Bin/init.php');
require_once(ROOT_FOLDER . 'Bin/Security.php');
include(ROOT_FOLDER . '/Bin/constants.php');
//include_once(ROOT_FOLDER . 'Bin/Smarty.php');
//include_once(ROOT_FOLDER . 'Bin/Template.php');
chdir(ROOT_FOLDER);
Bin_Config::requireAdminModel('MOrpa', 'MRva');
$orpaModel = new Model_MOrpa();
$rvaModel = new Model_MRva();
$db = Bin_Db::connect();
if ($rvaModel->isNonBussines()) {
exit;
}
$selectLastUpdateDate = "SELECT * FROM orpa_amount_update_log ORDER BY id DESC LIMIT 1";
$result = $db->query($selectLastUpdateDate)->getFirstRow();
$todayDate = date('Y-m-d');
$lastDate = array_get($result, 'date');
if (is_null($lastDate) or $lastDate < $todayDate) {
// need recalculate
$getLastDate = "SELECT currency_date FROM currency_rates ORDER BY currency_date DESC LIMIT 1";
$result = $db->query($getLastDate)->getFirstRow();
$lastCurrencyRateDate = array_get($result, 'currency_date');
$getRatesSql = "SELECT * FROM currency_rates WHERE currency_date=':date'";
$result = $db->query($getRatesSql, array(':date' => $lastCurrencyRateDate))->getResultArray();
$rateCurrencies = array_map(function ($el) {
return $el['currency_id'];
}, $result);
if ($lastCurrencyRateDate != $todayDate or !in_array(GBP, $rateCurrencies) or !in_array(USD, $rateCurrencies)) {
// wait for currency rate update
exit;
} else {
$selectAllOrpa = "SELECT id FROM orpa";
$result = $db->query($selectAllOrpa)->getResultArray();
foreach ($result as $item) {
$orpaModel->calculateAndUpdateOrpaAmounts($item['id'], true, new DateTime($lastCurrencyRateDate));
}
$db->builder()->values(array(
'date' => $todayDate,
'count' => count($result),
))->insert('orpa_amount_update_log')->execute();
}
}