probe_saledoc2.txt 2>&1 */ if (PHP_SAPI !== 'cli') { http_response_code(403); exit('Solo da riga di comando'); } set_time_limit(0); $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; } } } require_once __DIR__ . '/class/MagoClient.php'; $opts = getopt('', ['from::']); $from = $opts['from'] ?? date('Y') . '-01-01'; $mago = MagoClient::fromEnv(); try { $mago->login(); // 1) tutte le testate del periodo, solo i campi utili $fields = [ 'SaleDocId', 'DocumentType', 'DocNo', 'DocumentDate', 'CustSupp', 'TaxJournal', 'InvoicingTaxJournal', 'InvRsn', 'ShippingReason', 'EIDocumentType', 'InvoiceFollows', 'Issued', 'Cancelled', 'PostedToAccounting', 'CorrectionDocument', 'StubBook' ]; $docs = []; $minId = PHP_INT_MAX; foreach ( $mago->iterate('MA_SaleDoc', [ 'filters' => [['key' => 'DocumentDate', 'value' => $from, 'operator' => '>=']], 'fields' => $fields, ]) as $r ) { $id = (int)$r['SaleDocId']; $docs[$id] = $r; $minId = min($minId, $id); } echo "Documenti dal $from: " . count($docs) . PHP_EOL; // 2) importi: leggo i totali con SaleDocId >= il più piccolo del periodo $tax = []; if ($docs) { foreach ( $mago->iterate('MA_SaleDocSummary', [ 'filters' => [['key' => 'SaleDocId', 'value' => (string)$minId, 'operator' => '>=']], 'fields' => ['SaleDocId', 'TaxableAmount', 'TotalAmount'], ]) as $r ) { $tax[(int)$r['SaleDocId']] = (float)$r['TaxableAmount']; } } echo "Totali letti: " . count($tax) . PHP_EOL; // 3) riepilogo per tipo $sum = []; $add = function (&$arr, $v) { $v = (string)$v; if ($v === '') $v = '(vuoto)'; $arr[$v] = ($arr[$v] ?? 0) + 1; }; foreach ($docs as $id => $r) { $t = (string)$r['DocumentType']; if (!isset($sum[$t])) $sum[$t] = [ 'documenti' => 0, 'imponibile' => 0.0, 'senza_importo' => 0, 'annullati' => 0, 'in_contabilita' => 0, 'TaxJournal' => [], 'InvRsn' => [], 'ShippingReason' => [], 'EIDocumentType' => [], 'StubBook' => [], 'InvoiceFollows' => [], 'esempi' => [] ]; $s = &$sum[$t]; $s['documenti']++; if (isset($tax[$id])) $s['imponibile'] += $tax[$id]; else $s['senza_importo']++; if (!empty($r['Cancelled'])) $s['annullati']++; if (!empty($r['PostedToAccounting'])) $s['in_contabilita']++; $add($s['TaxJournal'], $r['TaxJournal'] ?? ''); $add($s['InvRsn'], $r['InvRsn'] ?? ''); $add($s['ShippingReason'], $r['ShippingReason'] ?? ''); $add($s['EIDocumentType'], $r['EIDocumentType'] ?? ''); $add($s['StubBook'], $r['StubBook'] ?? ''); $add($s['InvoiceFollows'], !empty($r['InvoiceFollows']) ? 'si' : 'no'); if (count($s['esempi']) < 4) $s['esempi'][] = $r['DocNo'] . ' del ' . substr((string)$r['DocumentDate'], 0, 10) . ' cliente ' . $r['CustSupp'] . ' imponibile ' . ($tax[$id] ?? '?'); unset($s); } ksort($sum); foreach ($sum as &$s) { $s['imponibile'] = round($s['imponibile'], 2); foreach (['TaxJournal', 'InvRsn', 'ShippingReason', 'EIDocumentType', 'StubBook', 'InvoiceFollows'] as $k) arsort($s[$k]); } unset($s); echo PHP_EOL . '=== Riepilogo per DocumentType ===' . PHP_EOL; echo json_encode($sum, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; } catch (Throwable $e) { fwrite(STDERR, PHP_EOL . 'ERRORE: ' . $e->getMessage() . PHP_EOL); } finally { $mago->logoff(); echo PHP_EOL . 'Logoff eseguito.' . PHP_EOL; }