94 lines
3.3 KiB
PHP
94 lines
3.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Sync acquisti mescole da Mago4 (sola lettura su Mago).
|
|
*
|
|
* php mago_sync_mix_cli.php ultimi 90 giorni (incrementale, per il cron)
|
|
* php mago_sync_mix_cli.php --from=2024-01-01 storico (prima volta)
|
|
* php mago_sync_mix_cli.php --prices stampa anche il prezzo €/kg per mescola
|
|
*
|
|
* Cron consigliato (2 volte al giorno nei giorni lavorativi, lontano dal sync ordini dei :40):
|
|
* 52 6,13 * * 1-5 flock -n /tmp/mago_sync_mix.lock php /percorso/userarea/mago_sync_mix_cli.php >> /home/ubuntu/logs/mago_sync_mix.log 2>&1
|
|
*/
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
|
http_response_code(403);
|
|
exit("Solo da terminale\n");
|
|
}
|
|
|
|
set_time_limit(0);
|
|
date_default_timezone_set('Europe/Rome');
|
|
|
|
// --- vendor e .env: come mago_sync_cli.php (userarea e fino a 3 cartelle sopra) ---
|
|
$dirs = [__DIR__, dirname(__DIR__), dirname(__DIR__, 2), dirname(__DIR__, 3)];
|
|
foreach ($dirs as $d) {
|
|
if (file_exists($d . '/vendor/autoload.php')) {
|
|
require_once $d . '/vendor/autoload.php';
|
|
break;
|
|
}
|
|
}
|
|
if (class_exists('Dotenv\Dotenv')) {
|
|
foreach ($dirs as $d) {
|
|
if (file_exists($d . '/.env')) {
|
|
Dotenv\Dotenv::createImmutable($d)->safeLoad();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- DB della piattaforma ---
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
require_once __DIR__ . '/class/MagoClient.php';
|
|
require_once __DIR__ . '/class/MagoMixPurchases.php';
|
|
|
|
$opt = getopt('', ['from::', 'prices']);
|
|
$from = (isset($opt['from']) && preg_match('/^\d{4}-\d{2}-\d{2}$/', (string)$opt['from']))
|
|
? $opt['from']
|
|
: date('Y-m-d', strtotime('-' . MagoMixPurchases::SYNC_DAYS . ' days'));
|
|
|
|
$t0 = microtime(true);
|
|
$log = function ($s) {
|
|
echo '[' . date('Y-m-d H:i:s') . "] $s\n";
|
|
};
|
|
|
|
$mago = null;
|
|
$exit = 0;
|
|
try {
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$mago = MagoClient::fromEnv();
|
|
$log("Sync acquisti mescole dal $from");
|
|
MagoMixPurchases::sync($pdo, $mago, $from, $log);
|
|
|
|
$lines = MagoMixPurchases::lines($pdo);
|
|
$c = array_count_values(array_column($lines, 'match'));
|
|
$log(sprintf(
|
|
"Righe in archivio: %d · abbinate %d · da abbinare %d · ambigue %d · escluse (recuperi…) %d",
|
|
count($lines),
|
|
$c['ok'] ?? 0,
|
|
$c['none'] ?? 0,
|
|
$c['ambiguous'] ?? 0,
|
|
$c['excluded'] ?? 0
|
|
));
|
|
|
|
if (isset($opt['prices'])) {
|
|
$prices = MagoMixPurchases::prices($pdo, MagoMixPurchases::PRICE_MONTHS, $lines);
|
|
$names = $pdo->query("SELECT id, nome FROM mescole")->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
uasort($prices, fn($a, $b) => $b['kg_total'] <=> $a['kg_total']);
|
|
echo "\nPrezzo €/kg (media " . MagoMixPurchases::PRICE_MONTHS . " mesi)\n";
|
|
foreach ($prices as $id => $p) {
|
|
printf(" %-45s %7.3f €/kg ultimo %7.3f del %s%s\n", mb_substr($names[$id] ?? "#$id", 0, 45), $p['price'], $p['last_price'], $p['last_date'], $p['old'] ? ' (vecchio)' : '');
|
|
}
|
|
}
|
|
} catch (Throwable $e) {
|
|
$log('ERRORE: ' . $e->getMessage());
|
|
$exit = 1;
|
|
} finally {
|
|
if ($mago) {
|
|
$ok = $mago->logoff();
|
|
$log('Logoff Mago: ' . ($ok ? 'OK' : 'NON riuscito → ' . $mago->getLastLogoffError()));
|
|
}
|
|
$log('Durata: ' . round(microtime(true) - $t0, 1) . ' s');
|
|
}
|
|
exit($exit);
|