<?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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

/**
 *	\file       bankimport/bankimportindex.php
 *	\ingroup    bankimport
 *	\brief      Dashboard page for BankImport 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 && 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';

/**
 * @var Conf $conf
 * @var DoliDB $db
 * @var HookManager $hookmanager
 * @var Translate $langs
 * @var User $user
 */

// Load translation files required by the page
$langs->loadLangs(array("bankimport@bankimport", "banks"));

$action = GETPOST('action', 'aZ09');

// Security check
if (!$user->hasRight('bankimport', 'read')) {
	accessforbidden();
}
$socid = GETPOSTINT('socid');
if (!empty($user->socid) && $user->socid > 0) {
	$action = '';
	$socid = $user->socid;
}


/*
 * View
 */

$form = new Form($db);
dol_include_once('/bankimport/class/bankstatement.class.php');

llxHeader("", $langs->trans("BankImportArea"), '', '', 0, 0, '', '', '', 'mod-bankimport page-index');

print load_fiche_titre($langs->trans("BankImportArea"), '', 'bank');

// Reminder: check if statements are outdated
$reminderEnabled = getDolGlobalString('BANKIMPORT_REMINDER_ENABLED', '1');
if ($reminderEnabled) {
	$reminderMonths = getDolGlobalInt('BANKIMPORT_REMINDER_MONTHS') ?: 3;
	$stmtCheck = new BankImportStatement($db);
	$lastEndDate = $stmtCheck->getLatestStatementEndDate();
	$thresholdDate = dol_time_plus_duree(dol_now(), -$reminderMonths, 'm');

	if ($lastEndDate === null) {
		// No statements at all
		print '<div class="warning">';
		print img_warning().' '.$langs->trans("ReminderNoStatements");
		print ' <a href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'">'.$langs->trans("UploadPDFStatement").'</a>';
		print '</div><br>';
	} elseif ($lastEndDate < $thresholdDate) {
		$monthsAgo = (int) round((dol_now() - $lastEndDate) / (30 * 24 * 3600));
		print '<div class="warning">';
		print img_warning().' '.$langs->trans("ReminderOutdatedStatements", dol_print_date($lastEndDate, 'day'), $monthsAgo);
		print ' <a href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'">'.$langs->trans("UploadPDFStatement").'</a>';
		print '</div><br>';
	}
}

// Payment matching notification
$bankAccountId = getDolGlobalInt('BANKIMPORT_BANK_ACCOUNT_ID');
if (!empty($bankAccountId)) {
	$sqlNewCount = "SELECT COUNT(*) as cnt FROM ".MAIN_DB_PREFIX."bankimport_transaction";
	$sqlNewCount .= " WHERE entity IN (".getEntity('banktransaction').")";
	$sqlNewCount .= " AND status = 0";
	$resNewCount = $db->query($sqlNewCount);
	$newCount = 0;
	if ($resNewCount) {
		$objNewCount = $db->fetch_object($resNewCount);
		$newCount = (int) $objNewCount->cnt;
	}

	if ($newCount > 0) {
		print '<div class="info" style="border-left: 4px solid #2196F3; background: #e3f2fd; padding: 12px; margin-bottom: 15px;">';
		print img_picto('', 'payment', 'class="pictofixedwidth"');
		print '<strong>'.$langs->trans("PendingPaymentMatches", $newCount).'</strong>';
		print '<br>'.$langs->trans("PendingPaymentMatchesDesc");
		print ' <a class="butAction" style="margin-left: 10px;" href="'.dol_buildpath('/bankimport/confirm.php', 1).'?mainmenu=bank&leftmenu=bankimport">';
		print $langs->trans("ReviewAndConfirm");
		print '</a>';
		print '</div>';
	}
} else {
	print '<div class="warning" style="margin-bottom: 15px;">';
	print img_warning().' '.$langs->trans("NoBankAccountConfigured");
	print ' <a href="'.dol_buildpath('/bankimport/admin/setup.php', 1).'">'.$langs->trans("GoToSetup").'</a>';
	print '</div>';
}

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

// -----------------------------------------------
// Widget: Letzte 10 importierte Buchungen
// -----------------------------------------------
$max = 10;

$sql = "SELECT t.rowid, t.ref, t.date_trans, t.name, t.description, t.amount, t.currency, t.status";
$sql .= " FROM ".MAIN_DB_PREFIX."bankimport_transaction as t";
$sql .= " WHERE t.entity IN (".getEntity('banktransaction').")";
$sql .= " ORDER BY t.date_trans DESC, t.rowid DESC";
$sql .= $db->plimit($max, 0);

$resql = $db->query($sql);

print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th colspan="4">';
print $langs->trans("LastImportedTransactions");

// Count total
$sqlcount = "SELECT COUNT(*) as total FROM ".MAIN_DB_PREFIX."bankimport_transaction WHERE entity IN (".getEntity('banktransaction').")";
$rescount = $db->query($sqlcount);
if ($rescount) {
	$objcount = $db->fetch_object($rescount);
	if ($objcount->total > 0) {
		print '<a class="paddingleft" href="'.dol_buildpath('/bankimport/list.php', 1).'?mainmenu=bank&leftmenu=bankimport">';
		print '<span class="badge">'.$objcount->total.'</span>';
		print '</a>';
	}
}
print '</th>';
print '</tr>';

if ($resql) {
	$num = $db->num_rows($resql);

	if ($num > 0) {
		$i = 0;
		while ($i < $num) {
			$obj = $db->fetch_object($resql);

			print '<tr class="oddeven">';

			// Date
			print '<td class="nowraponall">';
			print dol_print_date($db->jdate($obj->date_trans), 'day');
			print '</td>';

			// Name + Description
			print '<td class="tdoverflowmax200">';
			print '<a href="'.dol_buildpath('/bankimport/card.php', 1).'?id='.$obj->rowid.'&mainmenu=bank&leftmenu=bankimport">';
			print dol_escape_htmltag(dol_trunc($obj->name, 30));
			print '</a>';
			if ($obj->description) {
				print '<br><span class="opacitymedium small">'.dol_escape_htmltag(dol_trunc($obj->description, 40)).'</span>';
			}
			print '</td>';

			// Amount
			print '<td class="right nowraponall">';
			if ($obj->amount >= 0) {
				print '<span class="amount" style="color: green;">+'.price($obj->amount, 0, $langs, 1, -1, 2, $obj->currency).'</span>';
			} else {
				print '<span class="amount" style="color: red;">'.price($obj->amount, 0, $langs, 1, -1, 2, $obj->currency).'</span>';
			}
			print '</td>';

			// Status
			print '<td class="right nowraponall">';
			switch ($obj->status) {
				case 0:
					print '<span class="badge badge-status4 badge-status">'.$langs->trans("New").'</span>';
					break;
				case 1:
					print '<span class="badge badge-status1 badge-status">'.$langs->trans("Matched").'</span>';
					break;
				case 2:
					print '<span class="badge badge-status6 badge-status">'.$langs->trans("Reconciled").'</span>';
					break;
				case 9:
					print '<span class="badge badge-status5 badge-status">'.$langs->trans("Ignored").'</span>';
					break;
			}
			print '</td>';

			print '</tr>';
			$i++;
		}
	} else {
		print '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("NoTransactionsInDatabase").'</td></tr>';
	}

	$db->free($resql);
} else {
	dol_print_error($db);
}

print '</table>';

// Link "Alle anzeigen"
if (!empty($objcount) && $objcount->total > 0) {
	print '<div class="right" style="margin-top: 5px;">';
	print '<a href="'.dol_buildpath('/bankimport/list.php', 1).'?mainmenu=bank&leftmenu=bankimport">';
	print $langs->trans("ShowAll").' &raquo;';
	print '</a>';
	print '</div>';
}


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


// -----------------------------------------------
// Widget: Letzte 5 PDF-Kontoauszüge
// -----------------------------------------------
$maxpdf = 5;

$sql2 = "SELECT s.rowid, s.statement_number, s.statement_year, s.iban, s.date_from, s.date_to,";
$sql2 .= " s.opening_balance, s.closing_balance, s.filename, s.filepath, s.filesize, s.datec";
$sql2 .= " FROM ".MAIN_DB_PREFIX."bankimport_statement as s";
$sql2 .= " WHERE s.entity IN (".getEntity('bankstatement').")";
$sql2 .= " ORDER BY s.datec DESC";
$sql2 .= $db->plimit($maxpdf, 0);

$resql2 = $db->query($sql2);

print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th colspan="5">';
print $langs->trans("LastPDFStatements");

// Count total
$sqlcount2 = "SELECT COUNT(*) as total FROM ".MAIN_DB_PREFIX."bankimport_statement WHERE entity IN (".getEntity('bankstatement').")";
$rescount2 = $db->query($sqlcount2);
if ($rescount2) {
	$objcount2 = $db->fetch_object($rescount2);
	if ($objcount2->total > 0) {
		print '<a class="paddingleft" href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'?mainmenu=bank&leftmenu=bankimport">';
		print '<span class="badge">'.$objcount2->total.'</span>';
		print '</a>';
	}
}
print '</th>';
print '</tr>';

if ($resql2) {
	$num2 = $db->num_rows($resql2);

	if ($num2 > 0) {
		$i = 0;
		while ($i < $num2) {
			$obj2 = $db->fetch_object($resql2);

			print '<tr class="oddeven">';

			// Statement number / Year
			print '<td class="nowraponall">';
			print '<strong>'.dol_escape_htmltag($obj2->statement_number).'</strong>/'.$obj2->statement_year;
			print '</td>';

			// IBAN (shortened)
			print '<td class="tdoverflowmax150">';
			if ($obj2->iban) {
				print dol_escape_htmltag(dol_trunc($obj2->iban, 20));
			} else {
				print '<span class="opacitymedium">-</span>';
			}
			print '</td>';

			// Period
			print '<td class="center nowraponall">';
			if ($obj2->date_from && $obj2->date_to) {
				print dol_print_date($db->jdate($obj2->date_from), 'day').' - '.dol_print_date($db->jdate($obj2->date_to), 'day');
			} elseif ($obj2->date_from) {
				print dol_print_date($db->jdate($obj2->date_from), 'day').' -';
			} else {
				print '<span class="opacitymedium">-</span>';
			}
			print '</td>';

			// Closing balance
			print '<td class="right nowraponall">';
			if ($obj2->closing_balance !== null && $obj2->closing_balance !== '') {
				$color = (float) $obj2->closing_balance >= 0 ? '' : 'color: red;';
				print '<span style="'.$color.'">'.price($obj2->closing_balance, 0, $langs, 1, -1, 2, 'EUR').'</span>';
			} else {
				print '<span class="opacitymedium">-</span>';
			}
			print '</td>';

			// Actions
			print '<td class="center nowraponall">';
			if ($obj2->filepath && file_exists($obj2->filepath)) {
				print '<a class="paddingright" href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'?action=view&id='.$obj2->rowid.'&token='.newToken().'" target="_blank" title="'.$langs->trans("View").'">';
				print img_picto($langs->trans("View"), 'eye');
				print '</a>';

				print '<a href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'?action=download&id='.$obj2->rowid.'&token='.newToken().'" title="'.$langs->trans("Download").'">';
				print img_picto($langs->trans("Download"), 'download');
				print '</a>';
			}
			print '</td>';

			print '</tr>';
			$i++;
		}
	} else {
		print '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("NoPDFStatementsFound").'</td></tr>';
	}

	$db->free($resql2);
} else {
	dol_print_error($db);
}

print '</table>';

// Links
print '<div class="right" style="margin-top: 5px;">';
if (!empty($objcount2) && $objcount2->total > 0) {
	print '<a href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'?mainmenu=bank&leftmenu=bankimport">';
	print $langs->trans("ShowAll");
	print '</a>';
	print ' | ';
}
print '<a href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'?mainmenu=bank&leftmenu=bankimport">';
print $langs->trans("UploadNew").' &raquo;';
print '</a>';
print '</div>';


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

// End of page
llxFooter();
$db->close();
