<?php
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
 *
 * 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    importzugferdindex.php
 * \ingroup importzugferd
 * \brief   Home page of the ZUGFeRD Import module
 */

// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
    $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
    $i--;
    $j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
    $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
    $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../main.inc.php")) {
    $res = @include "../main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
    $res = @include "../../main.inc.php";
}
if (!$res) {
    die("Include of main fails");
}

require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';

dol_include_once('/importzugferd/class/zugferdimport.class.php');
dol_include_once('/importzugferd/lib/importzugferd.lib.php');

// Load translation files
$langs->loadLangs(array("importzugferd@importzugferd"));

// Security check
if (!isModEnabled('importzugferd')) {
    accessforbidden('Module not enabled');
}
if (!$user->hasRight('importzugferd', 'import', 'read')) {
    accessforbidden();
}

/*
 * View
 */

$form = new Form($db);

$title = $langs->trans('ModuleImportZugferdName');
llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-importzugferd page-index');

print load_fiche_titre($title, '', 'fa-file-import');

print '<div class="fichecenter">';

// Statistics box
print '<div class="fichethirdleft">';

print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td colspan="2">'.$langs->trans('Statistics').'</td>';
print '</tr>';

// Count imports by status
$sql = "SELECT status, COUNT(*) as nb FROM ".MAIN_DB_PREFIX."importzugferd_import";
$sql .= " WHERE entity = ".(int)$conf->entity;
$sql .= " GROUP BY status";

$stats = array(0 => 0, 1 => 0, 2 => 0);
$resql = $db->query($sql);
if ($resql) {
    while ($obj = $db->fetch_object($resql)) {
        $stats[$obj->status] = $obj->nb;
    }
}

$import = new ZugferdImport($db);

print '<tr class="oddeven">';
print '<td>'.$langs->trans('TotalImported').'</td>';
print '<td class="right">'.array_sum($stats).'</td>';
print '</tr>';

print '<tr class="oddeven">';
print '<td>'.$import->LibStatut(0, 1).'</td>';
print '<td class="right">'.$stats[0].'</td>';
print '</tr>';

print '<tr class="oddeven">';
print '<td>'.$import->LibStatut(1, 1).'</td>';
print '<td class="right">'.$stats[1].'</td>';
print '</tr>';

print '<tr class="oddeven">';
print '<td>'.$import->LibStatut(2, 1).'</td>';
print '<td class="right">'.$stats[2].'</td>';
print '</tr>';

print '</table>';
print '</div>';

print '</div>'; // fichethirdleft

// Quick actions and recent imports
print '<div class="fichetwothirdright">';

print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans('QuickActions').'</td>';
print '</tr>';

print '<tr class="oddeven">';
print '<td>';
print '<a class="button" href="'.dol_buildpath('/importzugferd/import.php', 1).'">';
print '<span class="fa fa-file-import paddingright"></span> '.$langs->trans('ZugferdImport');
print '</a>';
print ' &nbsp; ';
print '<a class="button" href="'.dol_buildpath('/importzugferd/list.php', 1).'">';
print '<span class="fa fa-list paddingright"></span> '.$langs->trans('ImportList');
print '</a>';
print ' &nbsp; ';
print '<a class="button" href="'.dol_buildpath('/importzugferd/mapping.php', 1).'">';
print '<span class="fa fa-exchange-alt paddingright"></span> '.$langs->trans('ProductMapping');
print '</a>';
print '</td>';
print '</tr>';

print '</table>';
print '</div>';

// Recent imports
print '<br>';
print '<div class="div-table-responsive-no-min">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td colspan="6">'.$langs->trans('RecentImports').'</td>';
print '</tr>';

$sql = "SELECT i.rowid, i.ref, i.invoice_number, i.invoice_date, i.seller_name, i.total_ttc, i.status";
$sql .= " FROM ".MAIN_DB_PREFIX."importzugferd_import as i";
$sql .= " WHERE i.entity = ".(int)$conf->entity;
$sql .= " ORDER BY i.date_creation DESC";
$sql .= " LIMIT 10";

$resql = $db->query($sql);
if ($resql) {
    $num = $db->num_rows($resql);
    if ($num > 0) {
        while ($obj = $db->fetch_object($resql)) {
            print '<tr class="oddeven">';
            print '<td><a href="'.dol_buildpath('/importzugferd/card.php', 1).'?id='.$obj->rowid.'">'.$obj->ref.'</a></td>';
            print '<td>'.dol_escape_htmltag($obj->invoice_number).'</td>';
            print '<td>'.dol_print_date($db->jdate($obj->invoice_date), 'day').'</td>';
            print '<td>'.dol_escape_htmltag($obj->seller_name).'</td>';
            print '<td class="right">'.price($obj->total_ttc).' EUR</td>';
            print '<td>'.$import->LibStatut($obj->status, 0).'</td>';
            print '</tr>';
        }
    } else {
        print '<tr class="oddeven"><td colspan="6" class="opacitymedium">'.$langs->trans('NoRecordFound').'</td></tr>';
    }
}

print '</table>';
print '</div>';

print '</div>'; // fichetwothirdright

print '</div>'; // fichecenter

print '<div class="clearboth"></div>';

llxFooter();
$db->close();
