db = $db; } /** * Check if import should run based on configured frequency * * @return bool True if import should run */ protected function shouldRunImport() { global $conf; $frequency = getDolGlobalString('IMPORTZUGFERD_IMPORT_FREQUENCY', 'manual'); if ($frequency === 'manual') { return false; } // Get last run timestamp $lastRun = getDolGlobalInt('IMPORTZUGFERD_LAST_IMPORT_RUN', 0); $now = dol_now(); // Calculate minimum interval based on frequency $interval = 0; switch ($frequency) { case 'hourly': $interval = 3600; // 1 hour break; case 'daily': $interval = 86400; // 24 hours break; case 'weekly': $interval = 604800; // 7 days break; } // Check if enough time has passed if ($now - $lastRun < $interval) { return false; } return true; } /** * Update last run timestamp */ protected function updateLastRunTime() { global $conf; dolibarr_set_const($this->db, 'IMPORTZUGFERD_LAST_IMPORT_RUN', dol_now(), 'chaine', 0, '', $conf->entity); } /** * Main import method - imports from both folder and mailbox * * @return int 0 if OK, <0 if error */ public function runScheduledImport() { global $conf, $user, $langs; $langs->load('importzugferd@importzugferd'); // Check if we should run based on frequency if (!$this->shouldRunImport()) { $this->output = 'Skipped - not scheduled to run (frequency: '.getDolGlobalString('IMPORTZUGFERD_IMPORT_FREQUENCY', 'manual').')'; return 0; } // Reset counters $this->imported_count = 0; $this->skipped_count = 0; $this->error_count = 0; $this->errors = array(); $folderResult = $this->importFromFolder(); $mailboxResult = $this->fetchFromMailbox(); // Update last run time $this->updateLastRunTime(); // Build combined output $this->output = sprintf( "Scheduled import complete. Imported: %d, Skipped: %d, Errors: %d", $this->imported_count, $this->skipped_count, $this->error_count ); if ($this->error_count > 0 && !empty($this->errors)) { $this->output .= "\nErrors: " . implode(", ", array_slice($this->errors, 0, 5)); } return ($this->error_count > 0) ? -1 : 0; } /** * Import ZUGFeRD invoices from watch folder * * @return int 0 if OK, <0 if error */ public function importFromFolder() { global $conf, $user, $langs; $langs->load('importzugferd@importzugferd'); $watchFolder = getDolGlobalString('IMPORTZUGFERD_WATCH_FOLDER'); $archiveFolder = getDolGlobalString('IMPORTZUGFERD_ARCHIVE_FOLDER'); $errorFolder = getDolGlobalString('IMPORTZUGFERD_ERROR_FOLDER'); $autoCreate = getDolGlobalString('IMPORTZUGFERD_AUTO_CREATE_INVOICE'); // Validate settings if (empty($watchFolder) || !is_dir($watchFolder)) { $this->output = 'Watch folder not configured or not accessible'; return 0; // Not an error, just not configured } // Load admin user for import actions $admin_user = new User($this->db); $admin_user->fetch(1); // Find PDF files $files = glob($watchFolder . '/*.pdf'); $files = array_merge($files, glob($watchFolder . '/*.PDF')); if (empty($files)) { return 0; } // Ensure archive folder exists if configured if (!empty($archiveFolder) && !is_dir($archiveFolder)) { dol_mkdir($archiveFolder); } // Ensure error folder exists if configured if (!empty($errorFolder) && !is_dir($errorFolder)) { dol_mkdir($errorFolder); } foreach ($files as $file) { // Use ZugferdImport::importFromFile for consistent handling $import = new ZugferdImport($this->db); $result = $import->importFromFile($admin_user, $file, $autoCreate); if ($result > 0) { $this->imported_count++; dol_syslog("CronImportZugferd: Imported invoice from folder: " . basename($file), LOG_INFO); // Archive the file $this->moveFile($file, $archiveFolder, 'imported_'); } elseif ($result == -2) { // Duplicate - already imported $this->skipped_count++; dol_syslog("CronImportZugferd: Skipped duplicate invoice: " . basename($file), LOG_INFO); // Archive duplicates - delete if no archive folder if (!$this->moveFile($file, $archiveFolder, 'duplicate_')) { @unlink($file); } } else { $this->error_count++; $this->errors[] = basename($file) . ': ' . $import->error; dol_syslog("CronImportZugferd: Error importing: " . basename($file) . " - " . $import->error, LOG_WARNING); // Try error folder first, fall back to archive folder if (!$this->moveFile($file, $errorFolder, 'error_')) { // Use archive folder as fallback for errors $this->moveFile($file, $archiveFolder, 'error_'); } } } return 0; } /** * Fetch ZUGFeRD invoices from configured IMAP mailbox * * @return int 0 if OK, <0 if error */ public function fetchFromMailbox() { global $conf, $user, $langs; $langs->load('importzugferd@importzugferd'); // Get IMAP settings $host = getDolGlobalString('IMPORTZUGFERD_IMAP_HOST'); $port = getDolGlobalString('IMPORTZUGFERD_IMAP_PORT', '993'); $imap_user = getDolGlobalString('IMPORTZUGFERD_IMAP_USER'); $password = getDolGlobalString('IMPORTZUGFERD_IMAP_PASSWORD'); $folder = getDolGlobalString('IMPORTZUGFERD_IMAP_FOLDER', 'INBOX'); $ssl = getDolGlobalString('IMPORTZUGFERD_IMAP_SSL'); $auto_create = getDolGlobalString('IMPORTZUGFERD_AUTO_CREATE_INVOICE'); // Validate settings if (empty($host) || empty($imap_user) || empty($password)) { $this->error = 'IMAP settings not configured'; $this->output = $this->error; return -1; } // Build mailbox string $mailbox = '{' . $host . ':' . $port . '/imap'; if ($ssl) { $mailbox .= '/ssl'; } $mailbox .= '/novalidate-cert}' . $folder; // Connect to IMAP $connection = @imap_open($mailbox, $imap_user, $password); if (!$connection) { $this->error = 'IMAP connection failed: ' . imap_last_error(); $this->output = $this->error; return -2; } // Search for unread messages with attachments $messages = imap_search($connection, 'UNSEEN'); if ($messages === false) { $this->output = 'No new messages found'; imap_close($connection); return 0; } $temp_dir = $conf->importzugferd->dir_output . '/temp'; if (!is_dir($temp_dir)) { dol_mkdir($temp_dir); } // Load admin user for import actions $admin_user = new User($this->db); $admin_user->fetch(1); // Fetch admin user $actions = new ActionsImportZugferd($this->db); foreach ($messages as $msg_num) { $structure = imap_fetchstructure($connection, $msg_num); // Check for attachments $attachments = $this->getAttachments($connection, $msg_num, $structure); foreach ($attachments as $attachment) { // Check if it's a PDF if (strtolower($attachment['type']) !== 'pdf') { continue; } // Save attachment temporarily $temp_file = $temp_dir . '/' . uniqid('zugferd_') . '.pdf'; file_put_contents($temp_file, $attachment['data']); // Check if it's a ZUGFeRD PDF $parser = new ZugferdParser($this->db); $result = $parser->extractFromPdf($temp_file); if ($result > 0) { // It's a ZUGFeRD invoice, try to import $result = $actions->processPdf($temp_file, $admin_user, $auto_create); if ($result > 0) { $this->imported_count++; dol_syslog("CronImportZugferd: Imported invoice from email, ID: " . $result, LOG_INFO); } elseif ($result == -3) { // Duplicate $this->skipped_count++; dol_syslog("CronImportZugferd: Skipped duplicate invoice", LOG_INFO); } else { $this->error_count++; $this->errors[] = $actions->error; dol_syslog("CronImportZugferd: Error importing invoice: " . $actions->error, LOG_WARNING); } } // Clean up temp file if (file_exists($temp_file)) { unlink($temp_file); } } // Mark message as read imap_setflag_full($connection, (string)$msg_num, '\\Seen'); } imap_close($connection); // Build output message $this->output = sprintf( "Processed %d messages. Imported: %d, Skipped (duplicates): %d, Errors: %d", count($messages), $this->imported_count, $this->skipped_count, $this->error_count ); if ($this->error_count > 0) { $this->output .= "\nErrors: " . implode(", ", $this->errors); } return 0; } /** * Extract attachments from email * * @param resource $connection IMAP connection * @param int $msg_num Message number * @param object $structure Message structure * @param string $part_num Part number for nested parts * @return array Attachments */ private function getAttachments($connection, $msg_num, $structure, $part_num = '') { $attachments = array(); // Check if it's a multipart message if (isset($structure->parts) && count($structure->parts)) { foreach ($structure->parts as $key => $part) { $attachments = array_merge( $attachments, $this->getAttachments($connection, $msg_num, $part, ($part_num ? $part_num . '.' : '') . ($key + 1)) ); } } else { // Check if this part is an attachment $attachment = $this->extractAttachment($connection, $msg_num, $structure, $part_num); if ($attachment) { $attachments[] = $attachment; } } return $attachments; } /** * Extract a single attachment * * @param resource $connection IMAP connection * @param int $msg_num Message number * @param object $part Part structure * @param string $part_num Part number * @return array|null Attachment data or null */ private function extractAttachment($connection, $msg_num, $part, $part_num) { $filename = ''; // Get filename from parameters if (isset($part->dparameters)) { foreach ($part->dparameters as $param) { if (strtolower($param->attribute) === 'filename') { $filename = $param->value; break; } } } if (empty($filename) && isset($part->parameters)) { foreach ($part->parameters as $param) { if (strtolower($param->attribute) === 'name') { $filename = $param->value; break; } } } // Check if it's a PDF attachment if (empty($filename) || !preg_match('/\.pdf$/i', $filename)) { return null; } // Get attachment data if ($part_num) { $data = imap_fetchbody($connection, $msg_num, $part_num); } else { $data = imap_body($connection, $msg_num); } // Decode based on encoding if (isset($part->encoding)) { switch ($part->encoding) { case 3: // BASE64 $data = base64_decode($data); break; case 4: // QUOTED-PRINTABLE $data = quoted_printable_decode($data); break; } } // Get file extension $ext = pathinfo($filename, PATHINFO_EXTENSION); return array( 'filename' => $filename, 'type' => strtolower($ext), 'data' => $data ); } /** * Move file to target folder with proper error handling * * @param string $file Source file path * @param string $targetFolder Target folder path * @param string $prefix Filename prefix (e.g., 'imported_', 'duplicate_', 'error_') * @return bool True if moved/deleted, false on failure */ protected function moveFile($file, $targetFolder, $prefix = '') { if (!file_exists($file)) { dol_syslog("CronImportZugferd: File not found: " . $file, LOG_WARNING); return false; } // If target folder is configured and exists/writable if (!empty($targetFolder)) { // Create folder if it doesn't exist if (!is_dir($targetFolder)) { $result = dol_mkdir($targetFolder); if ($result < 0) { dol_syslog("CronImportZugferd: Failed to create folder: " . $targetFolder, LOG_WARNING); } } if (is_dir($targetFolder) && is_writable($targetFolder)) { $targetPath = $targetFolder . '/' . $prefix . date('Y-m-d_His') . '_' . basename($file); if (@rename($file, $targetPath)) { dol_syslog("CronImportZugferd: Moved file to: " . $targetPath, LOG_INFO); return true; } else { // Try copy + delete as fallback (for cross-filesystem moves) if (@copy($file, $targetPath)) { @unlink($file); dol_syslog("CronImportZugferd: Copied file to: " . $targetPath, LOG_INFO); return true; } else { dol_syslog("CronImportZugferd: Failed to move/copy file to: " . $targetPath, LOG_ERR); return false; } } } else { dol_syslog("CronImportZugferd: Target folder not writable: " . $targetFolder, LOG_WARNING); } } // No target folder configured or not writable - delete file from watch folder // to prevent re-processing (except for errors without error folder) if ($prefix !== 'error_') { if (@unlink($file)) { dol_syslog("CronImportZugferd: Deleted processed file: " . $file, LOG_INFO); return true; } else { dol_syslog("CronImportZugferd: Failed to delete file: " . $file, LOG_ERR); return false; } } // Error files stay in watch folder if no error folder configured dol_syslog("CronImportZugferd: Keeping error file in watch folder: " . $file, LOG_INFO); return true; } }