OpenXE/cronjobs/artikeluebertragen.php

253 lines
8.7 KiB
PHP
Raw Normal View History

2021-05-21 08:49:41 +02:00
<?php
use Xentral\Components\Logger\Logger;
/** @var Logger $logger */
$logger = $app->Container->get('Logger');
2024-06-26 17:01:40 +02:00
if (file_exists(dirname(__DIR__) . '/www/lib/class.erpapi_custom.php') && !class_exists('erpAPICustom')) {
include_once dirname(__DIR__) . '/www/lib/class.erpapi_custom.php';
2021-05-21 08:49:41 +02:00
}
2024-06-26 17:01:40 +02:00
if (empty($app->Conf)) {
$conf = new Config();
$app->Conf = $conf;
2021-05-21 08:49:41 +02:00
}
2024-06-26 17:01:40 +02:00
if (empty($app->DB) || empty($app->DB->connection)) {
$app->DB = new DB($app->Conf->WFdbhost, $app->Conf->WFdbname, $app->Conf->WFdbuser, $app->Conf->WFdbpass, null, $app->Conf->WFdbport);
2021-05-21 08:49:41 +02:00
}
2024-06-26 17:01:40 +02:00
if (!isset($app->erp) || !$app->erp) {
if (class_exists('erpAPICustom')) {
$erp = new erpAPICustom($app);
} else {
$erp = new erpAPI($app);
}
$app->erp = $erp;
2021-05-21 08:49:41 +02:00
}
2024-06-26 17:01:40 +02:00
if (empty($app->remote)) {
if (is_file(dirname(__DIR__) . '/www/lib/class.remote_custom.php')) {
if (!class_exists('RemoteCustom')) {
require_once dirname(__DIR__) . '/www/lib/class.remote_custom.php';
}
$app->remote = new RemoteCustom($app);
} else {
$app->remote = new Remote($app);
2021-05-21 08:49:41 +02:00
}
}
$logger->debug(
'Start'
);
2021-05-21 08:49:41 +02:00
$app->DB->Update(
2024-06-26 17:01:40 +02:00
"UPDATE `prozessstarter`
2021-05-21 08:49:41 +02:00
SET `mutexcounter` = `mutexcounter` + 1
WHERE `mutex` = 1 AND (`parameter` = 'artikeluebertragen') AND `aktiv` = 1"
);
2024-06-26 17:01:40 +02:00
if ($app->DB->Select("SELECT `mutex` FROM `prozessstarter` WHERE (`parameter` = 'artikeluebertragen') LIMIT 1") == 1) {
$logger->debug(
'Mutex'
);
// return;
2021-05-21 08:49:41 +02:00
}
$articles = $app->DB->SelectArr(
'SELECT `id`,`shop`,`artikel` FROM `shopexport_artikeluebertragen_check` ORDER BY `id`'
2021-05-21 08:49:41 +02:00
);
2024-06-26 17:01:40 +02:00
if (!empty($articles)) {
/** @var Shopexport $objShopexport */
$objShopexport = $app->loadModule('shopexport');
if ($objShopexport !== null && method_exists($objShopexport, 'addChangedArticles')) {
$objShopexport->addChangedArticles();
}
2021-05-21 08:49:41 +02:00
}
$logger->debug(
'Prepare',
[
'articles' => $articles
]
);
2021-05-21 08:49:41 +02:00
$anzChecked = [];
$anzChanged = [];
$lastids = [];
2024-06-26 17:01:40 +02:00
while (!empty($articles)) {
foreach ($articles as $article) {
if (empty($anzChanged[$article['shop']])) {
$anzChanged[$article['shop']] = 0;
}
if (empty($anzChecked[$article['shop']])) {
$anzChecked[$article['shop']] = 0;
}
if (!isset($lastids[$article['shop']])) {
$lastids[$article['shop']] = (int) $app->erp->GetKonfiguration(
'shopexport_artikeluebertragen_check_lastid_' . $article['shop']
);
}
$changed = $objShopexport->hasArticleHashChanged($article['artikel'], $article['shop']);
if ($changed['changed']) {
$app->DB->Insert(
sprintf(
'INSERT INTO `shopexport_artikeluebertragen` (`artikel`, `shop`, `check_nr`) VALUES (%d, %d, %d)',
$article['artikel'], $article['shop'], $lastids[$article['shop']]
)
);
$anzChanged[$article['shop']]++;
}
$anzChecked[$article['shop']]++;
$app->DB->Delete(
sprintf(
'DELETE FROM `shopexport_artikeluebertragen_check` WHERE `id` = %d',
$article['id']
)
);
$app->DB->Update(
sprintf(
"UPDATE `shopexport` SET `autosendarticle_last` = NOW() WHERE `id` = %d",
$article['shop']
)
);
2021-05-21 08:49:41 +02:00
}
2024-06-26 17:01:40 +02:00
$app->erp->SetKonfigurationValue(
'shopexport_artikeluebertragen_check_changed_' . $article['shop'],
$anzChanged[$article['shop']]
);
$app->erp->SetKonfigurationValue(
'shopexport_artikeluebertragen_check_checked_' . $article['shop'],
$anzChecked[$article['shop']]
);
if (method_exists($app->erp, 'canRunCronjob') && !$app->erp->canRunCronjob(['artikeluebertragen'])) {
$logger->debug(
'!canRunCronjob'
);
2024-06-26 17:01:40 +02:00
return;
2021-05-21 08:49:41 +02:00
}
2024-06-26 17:01:40 +02:00
$articles = $app->DB->SelectArr(
'SELECT `id`,`shop`,`artikel` FROM `shopexport_artikeluebertragen_check` ORDER BY `id` LIMIT 10'
2021-05-21 08:49:41 +02:00
);
$app->DB->Update(
2024-06-26 17:01:40 +02:00
"UPDATE `prozessstarter`
2021-05-21 08:49:41 +02:00
SET `letzteausfuerhung`=NOW(), `mutex` = 1,`mutexcounter`=0
WHERE `parameter` = 'artikeluebertragen'"
2024-06-26 17:01:40 +02:00
);
2021-05-21 08:49:41 +02:00
}
$check = $app->DB->Select('SELECT COUNT(`id`) FROM `shopexport_artikeluebertragen`');
$app->DB->Update(
2024-06-26 17:01:40 +02:00
"UPDATE `prozessstarter`
2021-05-21 08:49:41 +02:00
SET `letzteausfuerhung`=NOW(), `mutex` = 1,`mutexcounter`=0
WHERE `parameter` = 'artikeluebertragen'"
);
/*
2024-06-26 17:01:40 +02:00
while ($check > 0) {
$shopartikel = $app->DB->Query(
"SELECT `id`,`shop`,`artikel`,`check_nr` FROM `shopexport_artikeluebertragen` ORDER BY `id` LIMIT 10"
);
if (!empty($shopartikel)) {
$anz = 0;
while ($row = $app->DB->Fetch_Assoc($shopartikel)) {
if (!isset($lastids[$row['shop']])) {
$lastids[$row['shop']] = (int) $app->erp->GetKonfiguration('shopexport_artikeluebertragen_check_lastid_' . $row['shop']);
}
$anz++;
try {
$app->remote->RemoteSendArticleList($row['shop'], array($row['artikel']));
$app->erp->LagerSync($row['artikel'], true);
} catch (Execption $exception) {
$app->erp->LogFile($app->DB->real_escape_string($exception->getMessage()));
}
$app->DB->Delete(
sprintf(
'DELETE FROM `shopexport_artikeluebertragen` WHERE `id`= %d LIMIT 1',
$row['id']
)
);
if (!empty($row['check_nr']) && $row['check_nr'] == $lastids[$row['shop']]) {
$transfered = 1 + (int) $app->erp->GetKonfiguration('shopexport_artikeluebertragen_check_transfered_' . $row['shop']);
$app->erp->SetKonfigurationValue('shopexport_artikeluebertragen_check_transfered_' . $row['shop'], $transfered);
}
$app->DB->Update(
"UPDATE `prozessstarter`
2021-05-21 08:49:41 +02:00
SET `letzteausfuerhung`=NOW(), `mutex` = 1,`mutexcounter`=0
WHERE `parameter` = 'artikeluebertragen'"
2024-06-26 17:01:40 +02:00
);
}
2021-05-21 08:49:41 +02:00
2024-06-26 17:01:40 +02:00
$app->DB->free($shopartikel);
}
2021-05-21 08:49:41 +02:00
2024-06-26 17:01:40 +02:00
if (method_exists($app->erp, 'canRunCronjob') && !$app->erp->canRunCronjob(['artikeluebertragen'])) {
return;
}
2021-05-21 08:49:41 +02:00
2024-06-26 17:01:40 +02:00
$check = $app->DB->Select('SELECT COUNT(`id`) FROM `shopexport_artikeluebertragen`');
}*/
$sql = "SELECT DISTINCT `shop`, shopexport.`bezeichnung` FROM `shopexport_artikeluebertragen` INNER JOIN shopexport ON shopexport.id = `shop`";
$shops_to_transmit = $app->DB->SelectArr($sql);
//$app->erp->LogFile('Cronjob artikeluebertragen '.(!empty($shops_to_transmit)?count($shops_to_transmit):0)." shops", print_r($shops_to_transmit, true));
$logger->debug(
'{count} Shops',
[
'count' => (!empty($shops_to_transmit)?count($shops_to_transmit):0),
'shops_to_transmit' => $shops_to_transmit
]
);
foreach ($shops_to_transmit as $shop_to_transmit) {
$sql = "SELECT `artikel` FROM `shopexport_artikeluebertragen` WHERE `shop` = '".$shop_to_transmit['shop']."'";
$articles_to_transmit = $app->DB->SelectArr($sql);
$logger->debug(
'{bezeichnung} (Shop {shop_to_transmit}) {count} Artikel',
[
'shop_to_transmit' => $shop_to_transmit['shop'],
'bezeichnung' => $shop_to_transmit['bezeichnung'],
'count' => (!empty($articles_to_transmit)?count($articles_to_transmit):0),
'articles_to_transmit' => $articles_to_transmit
]
);
if (!empty($articles_to_transmit)) {
$article_ids_to_transmit = array_column($articles_to_transmit, 'artikel');
try {
$result = $app->remote->RemoteSendArticleList($shop_to_transmit['shop'], $article_ids_to_transmit); // Expected result is array $articles_to_transmit, field status contains transmission status
} catch (Execption $exception) {
$logger->error(
'Fehler {bezeichnung} (Shop {shop_to_transmit}) {count} Artikel',
[
'shop_to_transmit' => $shop_to_transmit['shop'],
'bezeichnung' => $shop_to_transmit['bezeichnung'],
'count' => (!empty($articles_to_transmit)?count($articles_to_transmit):0),
'exception' => $exception
]
);
}
foreach ($result as $article) {
$app->erp->LagerSync($article['artikel'], true);
}
} else {
}
2021-05-21 08:49:41 +02:00
}
$logger->debug(
'Ende'
);
2021-05-21 08:49:41 +02:00
$app->DB->Update(
2024-06-26 17:01:40 +02:00
"UPDATE `prozessstarter`
2021-05-21 08:49:41 +02:00
SET `letzteausfuerhung`= NOW(), `mutex` = 0,`mutexcounter`=0
WHERE `parameter` = 'artikeluebertragen'"
);