OpenXE/cronjobs/lagerzahlen.php

181 lines
6.4 KiB
PHP
Raw Normal View History

2021-05-21 08:49:41 +02:00
<?php
2024-05-24 18:59:43 +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-05-24 18:59:43 +02:00
if (empty($app) || !class_exists('ApplicationCore') || !($app instanceof ApplicationCore)) {
$app = new app_t();
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
if (empty($app->Conf)) {
$conf = new Config();
$app->Conf = $conf;
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
if (empty($app->DB)) {
$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-05-24 18:59:43 +02:00
if (empty($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-05-24 18:59:43 +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
}
}
2023-03-04 13:37:01 +01:00
$app->erp->LogFile("Lagerzahlen-Synchronisation Start");
2021-05-21 08:49:41 +02:00
//$app->DB->Update("UPDATE artikel SET cache_lagerplatzinhaltmenge='999'");
$firmendatenid = $app->DB->Select("SELECT MAX(id) FROM firmendaten LIMIT 1");
2024-05-24 18:59:43 +02:00
$app->DB->Update(
2021-05-21 08:49:41 +02:00
"UPDATE `prozessstarter`
SET `mutexcounter` = `mutexcounter` + 1
WHERE `mutex` = 1 AND `parameter` = 'lagerzahlen' AND `aktiv` = 1"
2024-05-24 18:59:43 +02:00
);
if (!$app->DB->Select(
2021-05-21 08:49:41 +02:00
"SELECT `id` FROM `prozessstarter` WHERE `mutex` = 0 AND `parameter` = 'lagerzahlen' AND `aktiv` = 1"
2024-05-24 18:59:43 +02:00
)) {
$app->erp->LogFile("Lagerzahlen-Synchronisation Ende: Prozessstarter-Mutex nicht bereit");
2021-05-21 08:49:41 +02:00
return;
2024-05-24 18:59:43 +02:00
}
2023-03-04 13:37:01 +01:00
2024-05-24 18:59:43 +02:00
$shops = $app->DB->SelectArr('SELECT * FROM `shopexport` WHERE `aktiv` = 1');
if (empty($shops)) {
2023-03-04 13:37:01 +01:00
$app->erp->LogFile("Lagerzahlen-Synchronisation Ende: Keine aktiven Shops");
2021-05-21 08:49:41 +02:00
return;
2024-05-24 18:59:43 +02:00
}
$shopByIds = [];
foreach ($shops as $shop) {
2021-05-21 08:49:41 +02:00
$shopByIds[$shop['id']] = $shop;
2024-05-24 18:59:43 +02:00
}
$shopIds = array_keys($shopByIds);
$shopIdsStr = implode(',', $shopIds);
$hours = 12;
$hoursShop = 48;
$lagerartikel = $app->DB->SelectFirstCols(
"SELECT a.id
2021-05-21 08:49:41 +02:00
FROM `artikel` AS `a`
LEFT JOIN (
SELECT ao2.artikel, 1 AS `autolagerlampe`,
MAX(ao2.storage_cache = -999) AS `cache_reseted`,
MAX(HOUR(TIMEDIFF(NOW(), `last_storage_transfer`))) AS `last_storage_transfer_hours`
FROM `artikel_onlineshops` AS `ao2`
INNER JOIN `artikel` AS `art`
ON ao2.artikel = art.id AND (art.autolagerlampe = 1 OR (ao2.autolagerlampe = 1 AND ao2.ausartikel = 0))
WHERE ao2.aktiv = 1 AND ao2.shop IN ({$shopIdsStr})
GROUP BY ao2.artikel
) AS `ao` ON a.id = ao.artikel
WHERE (a.geloescht = 0 OR a.geloescht IS NULL)
AND (
a.lagerartikel = 1
OR (a.stueckliste = 1 AND a.juststueckliste = 1)
)
AND (a.autolagerlampe = 1 OR ao.artikel IS NOT NULL)
AND (a.shop > 0 OR a.shop2 > 0 OR a.shop3 > 0 OR ao.artikel IS NOT NULL)
AND a.nummer <> 'DEL'
AND (
a.cache_lagerplatzinhaltmenge = -999
OR ao.cache_reseted = 1
OR a.laststorage_sync < a.laststorage_changed
OR a.laststorage_sync < DATE_SUB(NOW(), INTERVAL {$hours} HOUR)
OR (ao.last_storage_transfer_hours IS NULL OR ao.last_storage_transfer_hours > {$hoursShop})
)
ORDER BY a.cache_lagerplatzinhaltmenge = -999 DESC,
ao.cache_reseted DESC,
a.laststorage_sync"
2024-05-24 18:59:43 +02:00
);
2023-03-04 13:37:01 +01:00
2024-05-24 18:59:43 +02:00
if (empty($lagerartikel)) {
2023-03-04 13:37:01 +01:00
$app->erp->LogFile("Lagerzahlen-Synchronisation Ende: Keine fälligen Artikel");
2021-05-21 08:49:41 +02:00
return;
2024-05-24 18:59:43 +02:00
}
2021-05-21 08:49:41 +02:00
2024-05-24 18:59:43 +02:00
try {
2021-05-21 08:49:41 +02:00
$r = new ReflectionMethod($app->erp, 'LagerSync');
$params = $r->getParameters();
$anzargs = count($params);
2024-05-24 18:59:43 +02:00
} catch (Exception $e) {
2021-05-21 08:49:41 +02:00
$anzargs = 2;
2024-05-24 18:59:43 +02:00
}
2021-05-21 08:49:41 +02:00
2024-05-24 18:59:43 +02:00
$clagerartikel = $lagerartikel ? count($lagerartikel) : 0;
$app->erp->LogFile('Lagerzahlen-Synchronisation, Artikel gesamt: ' . $clagerartikel);
foreach ($lagerartikel as $articleCounter => $articleId) {
2021-05-21 08:49:41 +02:00
$app->DB->Update(
2024-05-24 18:59:43 +02:00
"UPDATE `prozessstarter`
2021-05-21 08:49:41 +02:00
SET `mutex` = 1 , `mutexcounter` = 0, `letzteausfuerhung` = NOW()
WHERE `parameter` = 'lagerzahlen' AND `aktiv` = 1"
);
try {
2024-05-24 18:59:43 +02:00
if ($anzargs > 2) {
$message .= $app->erp->LagerSync($articleId, false, $shopByIds); // 2nd parameter is print_echo -> prints via echo to logfile (big print_r)
} else {
$message .= $app->erp->LagerSync($articleId, false);
}
} catch (Exception $exception) {
$message .= '<br>' . $exception->getMessage();
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
if ($message != '') {
$message .= '<br>';
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
if ($articleCounter % 10 === 0 && method_exists($app->erp, 'canRunCronjob') && !$app->erp->canRunCronjob(['lagerzahlen'])) {
$app->DB->Update(
"UPDATE `prozessstarter`
SET `mutex` = 0 , `mutexcounter` = 0, `letzteausfuerhung` = NOW()
WHERE `parameter` = 'lagerzahlen' AND `aktiv` = 1"
);
$app->erp->LogFile("Lagerzahlen-Synchronisation Ende: lagerzahlen-Job kann nicht geladen werden");
return;
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
$app->erp->LogFile("Lagerzahlen-Synchronisation: Warte 10 Sekunden");
2021-05-21 08:49:41 +02:00
usleep(10000);
2024-05-24 18:59:43 +02:00
}
$app->DB->Update(
2021-05-21 08:49:41 +02:00
"UPDATE `prozessstarter`
SET `mutex` = 0 , `mutexcounter` = 0, `letzteausfuerhung` = NOW()
WHERE `parameter` = 'lagerzahlen' AND `aktiv` = 1"
2024-05-24 18:59:43 +02:00
);
if ($message != '' && $erp->Firmendaten('systemmailsabschalten') == 0 && $erp->GetFirmaMail() != '') {
2021-05-21 08:49:41 +02:00
try {
2024-05-24 18:59:43 +02:00
if ($erp->Firmendaten('systemmailsempfaenger') != '') {
$erp->MailSend(
$erp->GetFirmaMail(),
$erp->GetFirmaName(),
$erp->Firmendaten('systemmailsempfaenger'),
'Lagerverwaltung',
'Systemmeldung: Auto Update Lagerlampen',
$message
);
} else {
if ($erp->GetFirmaBCC1() != '') {
$erp->MailSend(
$erp->GetFirmaMail(),
$erp->GetFirmaName(),
$erp->GetFirmaBCC1(),
'Lagerverwaltung',
'Systemmeldung: Auto Update Lagerlampen',
$message
);
}
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
} catch (Exception $exception) {
$app->erp->LogFile("Lagerzahlen-Synchronisation Exception:" . $app->DB->real_escape_string($exception->getMessage()));
2021-05-21 08:49:41 +02:00
}
2024-05-24 18:59:43 +02:00
}
2021-05-21 08:49:41 +02:00
2024-05-24 18:59:43 +02:00
$app->erp->LogFile("Lagerzahlen-Synchronisation Ende");
2023-03-04 13:37:01 +01:00
2021-05-21 08:49:41 +02:00