importzugferd/class/cron_importzugferd.class.php

299 lines
9 KiB
PHP

<?php
/* Copyright (C) 2026 ZUGFeRD Import Module
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
/**
* \file class/cron_importzugferd.class.php
* \ingroup importzugferd
* \brief Cron job class for fetching ZUGFeRD invoices from mailbox
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
dol_include_once('/importzugferd/class/zugferdparser.class.php');
dol_include_once('/importzugferd/class/zugferdimport.class.php');
dol_include_once('/importzugferd/class/actions_importzugferd.class.php');
/**
* Class CronImportZugferd
* Cronjob handler for fetching ZUGFeRD invoices from IMAP mailbox
*/
class CronImportZugferd
{
/**
* @var DoliDB Database handler
*/
public $db;
/**
* @var string Error message
*/
public $error = '';
/**
* @var array Error messages
*/
public $errors = array();
/**
* @var string Output message
*/
public $output = '';
/**
* @var int Number of imported invoices
*/
public $imported_count = 0;
/**
* @var int Number of skipped invoices (duplicates)
*/
public $skipped_count = 0;
/**
* @var int Number of errors
*/
public $error_count = 0;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* 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
);
}
}