WIP: schedulers..

This commit is contained in:
2025-10-08 15:53:45 +00:00
parent f440223a26
commit cf65b86547
11 changed files with 54 additions and 59 deletions

View File

@@ -236,7 +236,7 @@ $sugar_config = array (
'log_memory_usage' => false,
'logger' =>
array (
'level' => 'debug',
'level' => 'bimit',
'file' =>
array (
'ext' => '.log',

View File

@@ -68,7 +68,6 @@ $current_user->getSystemUser();
///////////////////////////////////////////////////////////////////////////////
//// PREP FOR SCHEDULER PID
$GLOBALS['log']->debug('--------------------------------------------> at cron.php <--------------------------------------------');
$cachePath = $GLOBALS['sugar_config']['cache_dir'].'modules/Schedulers';
$pid = 'pid.php';
@@ -79,14 +78,10 @@ if(!is_file($cachePath.'/'.$pid)) {
if(is_writable($cachePath)) { // the "file" does not yet exist
write_array_to_file('timestamp', array(strtotime(date('H:i'))) , $cachePath.'/'.$pid);
require_once($cachePath.'/'.$pid);
} else {
$GLOBALS['log']->fatal('Scheduler cannot write PID file. Please check permissions on '.$cachePath);
}
} else {
if(is_writable($cachePath.'/'.$pid)) {
require_once($cachePath.'/'.$pid);
} else {
$GLOBALS['log']->fatal('Scheduler cannot read the PID file. Please check permissions on '.$cachePath);
}
}
//// END PREP FOR SCHEDULER PID
@@ -102,11 +97,7 @@ if($timestamp[0] < strtotime(date('H:i'))) {
$s = new Scheduler();
$s->flushDeadJobs();
$s->checkPendingJobs();
} else {
$GLOBALS['log']->fatal('Scheduler cannot write PID file. Please check permissions on '.$cachePath);
}
} else {
$GLOBALS['log']->fatal('If you see a whole string of these, there is a chance someone is attacking your system.');
}
$exit_on_cleanup = true;
sugar_cleanup($exit_on_cleanup);

View File

@@ -65,6 +65,7 @@ class LoggerManager {
'error' => 25,
'fatal' => 10,
'security' => 5,
'bimit' => 2,
'off' => 0,
);

View File

@@ -2,8 +2,10 @@
$id = $_REQUEST['apilo_details'];
$apilo_config = loadApiloConfiguration();
brecho('halo');
brecho(loadApiloOrder($apilo_config['token'], $id));
function loadApiloOrder($token, $orderId) {
brecho($token);
$url = "https://twinpol.apilo.com/rest/api/orders/".$orderId."/";
$headers = [
'Authorization: Bearer ' . $token,
@@ -17,6 +19,7 @@ function loadApiloOrder($token, $orderId) {
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
brecho($response);
if ($httpCode !== 200) {
return false;
}

View File

@@ -39,7 +39,7 @@ function getOrder($token, $orderId)
function brecho($msg)
{
echo '<br><pre>';
var_dump($msg);
print_r($msg);
echo PHP_EOL;
echo '</pre><br>';
}

View File

@@ -111,7 +111,7 @@ function copyToClipboard(text) {
}
function openDetails(id, origin) {
window.console.log(origin);
if (origin.startsWith('apilo')) {
if (origin.startsWith('Apilo')) {
window.open("index.php?module=EcmInvoiceOuts&action=ecommerce&apilo_details="+id, "_blank");
} else {
window.open("index.php?module=EcmInvoiceOuts&action=ecommerce&baselinker_details="+id, "_blank");

View File

@@ -109,12 +109,17 @@
{$ROW.document_no}
{if $ROW.origin == 'amazon'}
<img src="modules/EcmSales/images/pdf.gif" onclick="copyToClipboard('{$ROW.url}')"
style="cursor:pointer;"></a>
style="cursor:pointer;"/>
{/if}
{if $ROW.origin == 'allegro'}
<img src="modules/EcmSales/images/pdf.gif"
onclick="window.open('https://panel-g.baselinker.com/printouts/printout_invoices.php?id={$ROW.id}')"
style="cursor:pointer;" {/if} </td>
style="cursor:pointer;"/>
{/if}
<img src="modules/EcmSales/images/convert.gif"
onclick="openDetails('{$ROW.order_no}', '{$ROW.origin}')"
style="cursor:pointer;"/>
</td>
<td>
<a target="_blank"
href="index.php?module=EcmStockDocOuts&action=DetailView&record={$ROW.wz_id}">{$ROW.wz_document_no}</a>

View File

@@ -1,36 +1,31 @@
<?php
// enable error reporting
error_reporting(LC_ALL);
ini_set('display_errors', 1);
//error_reporting(LC_ALL);
//ini_set('display_errors', 1);
// ?XDEBUG_SESSION_START=PHPSTORM
importInvoices();
function importInvoices()
//importInvoices();
function apilo_importInvoices()
{
$apilo_config = loadApiloConfiguration();
$apilo_config = apilo_loadConfiguration();
$db = $GLOBALS['db'];
$GLOBALS['log']->info('----- Importing invoices from Apilo, config', $apilo_config);
$dbRes = $db->query("SELECT COUNT(id) as last_id FROM ecommerce_invoices WHERE origin LIKE 'apilo%'");
$offset = intval($db->fetchByAssoc($dbRes)['last_id']);
$GLOBALS['log']->info('----- Importing invoices from Apilo, offset', $offset);
$invoices = loadApiloInvoices($apilo_config['token'], $offset);
$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
if (isset($invoices->error)) {
if (refreshApiloToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
$apilo_config = loadApiloConfiguration();
$invoices = loadApiloInvoices($apilo_config['token'], $offset);
if (apilo_refreshToken($apilo_config['refreshToken'], $apilo_config['clientId'], $apilo_config['clientSecret']) == true) {
//$apilo_config = apilo_loadConfiguration();
//$invoices = apilo_loadInvoices($apilo_config['token'], $offset);
} else {
return false;
}
}
brecho(count($invoices->documents));
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, documents count', count($invoices->documents));
$GLOBALS['log']->info('----- Importing invoices from Apilo, documents count', count($invoices->documents));
$platforms = loadApiloPlatformsList($apilo_config['token']);
$platforms = apilo_loadPlatformsList($apilo_config['token']);
if (!$platforms) {
return false;
@@ -38,15 +33,15 @@ function importInvoices()
if (isset($invoices->documents) && count($invoices->documents) > 0) {
foreach ($invoices->documents as $invoice) {
addapiloInvoice($invoice, $platforms, $apilo_config['token']);
apilo_addInvoice($invoice, $platforms, $apilo_config['token']);
}
}
$GLOBALS['log']->info('----- Importing invoices from Apilo, all processed, return TRUE');
$GLOBALS['log']->bimit('----- Importing invoices from Apilo, all processed, return TRUE');
return true;
}
function loadApiloConfiguration()
function apilo_loadConfiguration()
{
global $db;
$db = $GLOBALS['db'];
$dbRes = $db->query("SELECT * FROM config WHERE category='apilo'");
$config = [];
while ($row = $db->fetchByAssoc($dbRes)) {
@@ -54,7 +49,7 @@ function loadApiloConfiguration()
}
return $config;
}
function loadApiloInvoices($token, $offset)
function apilo_loadInvoices($token, $offset)
{
$url = "https://twinpol.apilo.com/rest/api/finance/documents/";
$params = [
@@ -81,7 +76,7 @@ function loadApiloInvoices($token, $offset)
}
return json_decode($response);
}
function refreshApiloToken($refreshToken, $clientId, $clientSecret)
function apilo_refreshToken($refreshToken, $clientId, $clientSecret)
{
$url = "https://twinpol.apilo.com/rest/auth/token/";
@@ -126,11 +121,11 @@ function refreshApiloToken($refreshToken, $clientId, $clientSecret)
}
return false;
}
function addApiloInvoice($invoice, $platforms, $token)
function apilo_addInvoice($invoice, $platforms, $token)
{
$db = $GLOBALS['db'];
$platformId = loadApiloOrderPlatformId($token, $invoice->orderId);
$platformId = apilo_loadOrderPlatformId($token, $invoice->orderId);
if (!$platformId) {
return false;
}
@@ -231,12 +226,12 @@ function addApiloInvoice($invoice, $platforms, $token)
if (isset($invoice->documentItems) && is_array($invoice->documentItems)) {
$db->query(sprintf("DELETE FROM ecommerce_invoices_products WHERE invoice_id='%s'", $db->quote($invoice->id)));
foreach ($invoice->documentItems as $item) {
addApiloInvoiceProduct($invoice->id, $item);
apilo_addInvoiceProduct($invoice->id, $item);
}
}
return true;
}
function addApiloInvoiceProduct($invoiceId, $item)
function apilo_addInvoiceProduct($invoiceId, $item)
{
$db = $GLOBALS['db'];
@@ -290,7 +285,7 @@ function addApiloInvoiceProduct($invoiceId, $item)
return false;
}
}
function loadApiloPlatformsList($token) {
function apilo_loadPlatformsList($token) {
$url = "https://twinpol.apilo.com/rest/api/orders/platform/map/";
$headers = [
'Authorization: Bearer ' . $token,
@@ -309,7 +304,7 @@ function loadApiloPlatformsList($token) {
}
return json_decode($response);
}
function loadApiloOrderPlatformId($token, $orderId) {
function apilo_loadOrderPlatformId($token, $orderId) {
$url = "https://twinpol.apilo.com/rest/api/orders/".$orderId."/";
$headers = [
'Authorization: Bearer ' . $token,

View File

@@ -106,7 +106,6 @@ class Scheduler extends SugarBean {
*/
function fire() {
if(empty($this->job)) { // only execute when valid
$GLOBALS['log']->fatal('Scheduler tried to fire an empty job!!');
return false;
}
@@ -126,16 +125,18 @@ class Scheduler extends SugarBean {
$job->retrieve($jobId);
if($exJob[0] == 'function') {
$GLOBALS['log']->debug('----->Scheduler found a job of type FUNCTION');
require_once('modules/Schedulers/_AddJobsHere.php');
$job->setJobFlag(1);
$func = $exJob[1];
$GLOBALS['log']->debug('----->SchedulersJob firing '.$func);
$GLOBALS['log']->bimit('Starting function', $job->scheduler->id, $job->scheduler->name, $func);
$res = call_user_func($func);
if($res) {
$GLOBALS['log']->bimit('Setting JOB as finished', $job->scheduler->id, $job->scheduler->name, $res);
$job->setJobFlag(2);
$job->finishJob();
return true;
@@ -340,21 +341,14 @@ class Scheduler extends SugarBean {
$this->cleanJobLog();
$allSchedulers = $this->get_full_list('', 'schedulers.status=\'Active\'');
$GLOBALS['log']->info('-----> Scheduler found [ '.count($allSchedulers).' ] ACTIVE jobs');
if(!empty($allSchedulers)) {
foreach($allSchedulers as $focus) {
if($focus->fireQualified()) {
if($focus->fire()) {
$GLOBALS['log']->debug('----->Scheduler Job completed successfully');
} else {
$GLOBALS['log']->fatal('----->Scheduler Job FAILED');
}
$GLOBALS['log']->bimit('Starting job', $focus->id, $focus->name);
$res = $focus->fire();
$GLOBALS['log']->bimit('Finished job', $focus->id, $focus->name, $res);
}
}
} else {
$GLOBALS['log']->debug('----->No Schedulers found');
}
}

View File

@@ -71,27 +71,33 @@ $job_strings = array(
function importApiloInvoices() {
try {
$GLOBALS['log']->info('----- Importing invoices from Apilo, _addJobsHereReturn, starting');
$GLOBALS['log']->bimit('_addJobsHere, importApiloInvoices started');
$GLOBALS['db']->query("USE preDb_0dcc87940d3655fa574b253df04ca1c3;");
$GLOBALS['log']->bimit('_addJobsHere, importApiloInvoices db query done');
require_once('modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importApiloInvoices.php');
$GLOBALS['log']->bimit('_addJobsHere, importApiloInvoices file loadded');
$GLOBALS['log']->bimit('_addJobsHere, importApiloInvoices importInvoices() fired');
$res = importInvoices();
$GLOBALS['log']->info('----- Importing invoices from Apilo, _addJobsHereReturn', $res);
$GLOBALS['log']->bimit('_addJobsHere, importApiloInvoices finished', $res);
return true;
} catch (Exception $e) {
$GLOBALS['log']->info('----- Importing invoices from Apilo, _addJobsHereReturn, error catched', $e);
$GLOBALS['log']->bimit('_addJobsHere, importApiloInvoices error', $e);
return false;
}
}
function importBaselinkerInvoices()
{
try {
$GLOBALS['log']->bimit('_addJobsHere, importBaselinkerInvoices started');
$GLOBALS['db']->query("USE preDb_0dcc87940d3655fa574b253df04ca1c3;");
require_once('modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/importBaselinkerInvoices.php');
importFV('7688'); // FV Polska
//importFV('15356'); // FV Polska (stare)
importFV('53096'); // FV Temu
$GLOBALS['log']->bimit('_addJobsHere, importBaselinkerInvoices finished');
return true;
} catch (Exception $e) {
$GLOBALS['log']->bimit('_addJobsHere, importBaselinkerInvoices error', $e);
return false;
}
}

View File

@@ -1,4 +1,4 @@
<?php
$logger = $GLOBALS['log'];
$logger->debug('test');
$logger->bimit('test');
echo 'test';