File: /www/exchange0old/exchange/admin/cron/reset_debtors_trade_limit.php
<?php
/**
* Created by PhpStorm.
* User: Admin
* Date: 05.03.2018
* Time: 10:10
*/
$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);
$db = Bin_Db::connect();
$selectDebtorsSql = "SELECT dd.debtor_id,
dd.trade_limit,
od.user_id,
od.organisation_id
FROM debtors_detail dd
JOIN debtor_relation dr ON dr.debtor_id = dd.debtor_id
JOIN organisation_details od ON od.user_id = dr.trader_id
WHERE dd.type = 'debtor'";
$debtors = $db->query($selectDebtorsSql)->getResultArray();
$count = 0;
foreach ($debtors as $debtor) {
$updateSql = "UPDATE debtors_detail SET trade_limit = 0 WHERE debtor_id = ':debtor_id'";
$selectLogs = "SELECT * FROM activity_logs WHERE log_element_id = ':debtor_id' AND title LIKE '%DEBTOR_TRADE_LIMIT'";
$addLogSql = "INSERT INTO activity_logs (
title,
link,
log_element_id,
user_id,
debtor_id,
organisation_id,
created_at,
old_value
) VALUES (
':title',
'system',
':debtor_id',
':user_id',
':debtor_id',
':organisation_id',
NOW(),
':value'
)";
$logArray = array(
':title' => 'ADD_DEBTOR_TRADE_LIMIT',
':user_id' => $debtor['user_id'],
':debtor_id' => $debtor['debtor_id'],
':organisation_id' => $debtor['organisation_id'],
':value' => 0,
);
$db->query($updateSql, array(':debtor_id' => $debtor['debtor_id']));
$updated = $db->affectedRows();
$logs = $db->query($selectLogs, array(':debtor_id' => $debtor['debtor_id']))->getNumRows();
if (!$logs) {
$db->query($addLogSql, array_merge($logArray, array(':value' => $debtor['trade_limit'])));
}
if ($updated) {
$db->query($addLogSql, array_merge($logArray, array(
':title' => 'UPDATE_DEBTOR_TRADE_LIMIT',
)));
$count++;
}
}
echo $count . ' debtors updated';