dolibarr.bankimport/card.php
2026-02-13 18:30:28 +01:00

380 lines
12 KiB
PHP

<?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 bankimport/card.php
* \ingroup bankimport
* \brief Card page for a single bank transaction
*/
// 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/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
dol_include_once('/bankimport/class/banktransaction.class.php');
dol_include_once('/bankimport/lib/bankimport.lib.php');
/**
* @var Conf $conf
* @var DoliDB $db
* @var Translate $langs
* @var User $user
*/
$langs->loadLangs(array("bankimport@bankimport", "banks", "bills"));
$id = GETPOSTINT('id');
$ref = GETPOST('ref', 'alpha');
$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');
/*
* Actions
*/
$object = new BankImportTransaction($db);
if ($id > 0 || !empty($ref)) {
$result = $object->fetch($id, $ref);
if ($result <= 0) {
setEventMessages($langs->trans("RecordNotFound"), null, 'errors');
}
}
// Set status
if ($action == 'setstatus' && $object->id > 0) {
$newstatus = GETPOSTINT('status');
$result = $object->setStatus($newstatus, $user);
if ($result > 0) {
setEventMessages($langs->trans("StatusUpdated"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
exit;
} else {
setEventMessages($object->error, null, 'errors');
}
}
// Find matches
if ($action == 'findmatches' && $object->id > 0) {
$matches = $object->findMatches();
if (count($matches) > 0) {
$_SESSION['bankimport_matches_'.$object->id] = $matches;
setEventMessages($langs->trans("MatchesFound", count($matches)), null, 'mesgs');
} else {
setEventMessages($langs->trans("NoMatchesFound"), null, 'warnings');
}
}
// Link to object
if ($action == 'linkto' && $object->id > 0) {
$linktype = GETPOST('linktype', 'alpha');
$linkid = GETPOSTINT('linkid');
if ($linktype && $linkid > 0) {
$result = $object->linkTo($linktype, $linkid, $user);
if ($result > 0) {
setEventMessages($langs->trans("LinkCreated"), null, 'mesgs');
unset($_SESSION['bankimport_matches_'.$object->id]);
header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
exit;
} else {
setEventMessages($object->error, null, 'errors');
}
}
}
/*
* View
*/
$form = new Form($db);
$title = $langs->trans("Transaction").' - '.$object->ref;
llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-bankimport page-card');
if ($object->id > 0) {
print '<div class="fichecenter">';
// Title
print load_fiche_titre($langs->trans("Transaction"), '', 'bank');
// Card header
print '<div class="underbanner clearboth"></div>';
print '<table class="border centpercent tableforfield">';
// Reference
print '<tr>';
print '<td class="titlefield">'.$langs->trans("Ref").'</td>';
print '<td>'.dol_escape_htmltag($object->ref).'</td>';
print '</tr>';
// IBAN
print '<tr>';
print '<td>'.$langs->trans("AccountIBAN").'</td>';
print '<td>'.dol_escape_htmltag($object->iban).'</td>';
print '</tr>';
// Date
print '<tr>';
print '<td>'.$langs->trans("Date").'</td>';
print '<td>'.dol_print_date($object->date_trans, 'day').'</td>';
print '</tr>';
// Value date
if ($object->date_value) {
print '<tr>';
print '<td>'.$langs->trans("DateValue").'</td>';
print '<td>'.dol_print_date($object->date_value, 'day').'</td>';
print '</tr>';
}
// Name
print '<tr>';
print '<td>'.$langs->trans("Counterparty").'</td>';
print '<td><strong>'.dol_escape_htmltag($object->name).'</strong></td>';
print '</tr>';
// Counterparty IBAN
if ($object->counterparty_iban) {
print '<tr>';
print '<td>'.$langs->trans("CounterpartyIBAN").'</td>';
print '<td>'.dol_escape_htmltag($object->counterparty_iban).'</td>';
print '</tr>';
}
// Amount
print '<tr>';
print '<td>'.$langs->trans("Amount").'</td>';
print '<td>';
if ($object->amount >= 0) {
print '<span style="color: green; font-size: 1.3em; font-weight: bold;">+'.price($object->amount, 0, $langs, 1, -1, 2, $object->currency).'</span>';
} else {
print '<span style="color: red; font-size: 1.3em; font-weight: bold;">'.price($object->amount, 0, $langs, 1, -1, 2, $object->currency).'</span>';
}
print '</td>';
print '</tr>';
// Label
if ($object->label) {
print '<tr>';
print '<td>'.$langs->trans("Label").'</td>';
print '<td>'.dol_escape_htmltag($object->label).'</td>';
print '</tr>';
}
// Description
print '<tr>';
print '<td>'.$langs->trans("Description").'</td>';
print '<td>'.nl2br(dol_escape_htmltag($object->description)).'</td>';
print '</tr>';
// End-to-End ID
if ($object->end_to_end_id) {
print '<tr>';
print '<td>'.$langs->trans("EndToEndId").'</td>';
print '<td>'.dol_escape_htmltag($object->end_to_end_id).'</td>';
print '</tr>';
}
// Mandate ID
if ($object->mandate_id) {
print '<tr>';
print '<td>'.$langs->trans("MandateId").'</td>';
print '<td>'.dol_escape_htmltag($object->mandate_id).'</td>';
print '</tr>';
}
// Status
print '<tr>';
print '<td>'.$langs->trans("Status").'</td>';
print '<td>'.$object->getLibStatut(4).'</td>';
print '</tr>';
// Linked invoice
if ($object->fk_facture > 0) {
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
$invoice = new Facture($db);
$invoice->fetch($object->fk_facture);
print '<tr>';
print '<td>'.$langs->trans("Invoice").'</td>';
print '<td>'.$invoice->getNomUrl(1).'</td>';
print '</tr>';
}
// Linked supplier invoice
if ($object->fk_facture_fourn > 0) {
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
$invoice = new FactureFournisseur($db);
$invoice->fetch($object->fk_facture_fourn);
print '<tr>';
print '<td>'.$langs->trans("SupplierInvoice").'</td>';
print '<td>'.$invoice->getNomUrl(1).'</td>';
print '</tr>';
}
// Linked third party
if ($object->fk_societe > 0) {
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
$soc = new Societe($db);
$soc->fetch($object->fk_societe);
print '<tr>';
print '<td>'.$langs->trans("ThirdParty").'</td>';
print '<td>'.$soc->getNomUrl(1).'</td>';
print '</tr>';
}
// Import date
print '<tr>';
print '<td>'.$langs->trans("DateCreation").'</td>';
print '<td>'.dol_print_date($object->datec, 'dayhour').'</td>';
print '</tr>';
print '</table>';
print '</div>';
// Actions buttons
print '<div class="tabsAction">';
if ($object->status == BankImportTransaction::STATUS_NEW) {
// Find matches button
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=findmatches&token='.newToken().'">'.$langs->trans("FindMatches").'</a>';
// Set as ignored
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=setstatus&status='.BankImportTransaction::STATUS_IGNORED.'&token='.newToken().'">'.$langs->trans("SetAsIgnored").'</a>';
}
if ($object->status == BankImportTransaction::STATUS_IGNORED) {
// Reopen
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=setstatus&status='.BankImportTransaction::STATUS_NEW.'&token='.newToken().'">'.$langs->trans("Reopen").'</a>';
}
print '</div>';
// Show matches if found
$matches = $_SESSION['bankimport_matches_'.$object->id] ?? array();
if (!empty($matches)) {
print '<br>';
print load_fiche_titre($langs->trans("MatchesFound", count($matches)), '', 'object_invoice');
print '<div class="div-table-responsive">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th>'.$langs->trans("Type").'</th>';
print '<th>'.$langs->trans("Ref").'</th>';
print '<th>'.$langs->trans("ThirdParty").'</th>';
print '<th class="right">'.$langs->trans("Amount").'</th>';
print '<th>'.$langs->trans("DateDue").'</th>';
print '<th>'.$langs->trans("Score").'</th>';
print '<th>'.$langs->trans("MatchReason").'</th>';
print '<th></th>';
print '</tr>';
// Translate match reasons
$reasonLabels = array(
'ref' => $langs->trans("MatchByRef"),
'ref_client' => $langs->trans("MatchByClientRef"),
'ref_supplier' => $langs->trans("MatchBySupplierRef"),
'amount' => $langs->trans("MatchByAmount"),
'amount_close' => $langs->trans("MatchByAmountClose"),
'name_exact' => $langs->trans("MatchByNameExact"),
'name_similar' => $langs->trans("MatchByNameSimilar"),
'iban' => $langs->trans("MatchByIBAN")
);
foreach ($matches as $match) {
print '<tr class="oddeven">';
print '<td>'.($match['type'] == 'facture' ? $langs->trans("Invoice") : $langs->trans("SupplierInvoice")).'</td>';
// Get invoice link
if ($match['type'] == 'facture') {
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
$inv = new Facture($db);
$inv->fetch($match['id']);
print '<td>'.$inv->getNomUrl(1).'</td>';
} else {
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
$inv = new FactureFournisseur($db);
$inv->fetch($match['id']);
print '<td>'.$inv->getNomUrl(1).'</td>';
}
// Third party with link
if ($match['socid'] > 0) {
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
$soc = new Societe($db);
$soc->fetch($match['socid']);
print '<td>'.$soc->getNomUrl(1).'</td>';
} else {
print '<td>'.dol_escape_htmltag($match['socname']).'</td>';
}
print '<td class="right">'.price($match['amount'], 0, $langs, 1, -1, 2, 'EUR').'</td>';
print '<td>'.($match['date_due'] ? dol_print_date($match['date_due'], 'day') : '-').'</td>';
// Score with color
$scoreColor = $match['match_score'] >= 80 ? 'green' : ($match['match_score'] >= 60 ? 'orange' : 'gray');
print '<td><span style="color: '.$scoreColor.'; font-weight: bold;">'.$match['match_score'].'%</span></td>';
// Match reasons as badges
print '<td>';
if (!empty($match['match_reasons'])) {
foreach ($match['match_reasons'] as $reason) {
$label = $reasonLabels[$reason] ?? $reason;
print '<span class="badge badge-secondary">'.$label.'</span> ';
}
}
print '</td>';
print '<td class="center">';
print '<a class="butActionSmall" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=linkto&linktype='.$match['type'].'&linkid='.$match['id'].'&token='.newToken().'">'.$langs->trans("Link").'</a>';
print '</td>';
print '</tr>';
}
print '</table>';
print '</div>';
}
} else {
print '<div class="error">'.$langs->trans("RecordNotFound").'</div>';
}
// Back link
print '<div class="tabsAction">';
print '<a class="butAction" href="'.dol_buildpath('/bankimport/list.php', 1).'">'.$langs->trans("BackToList").'</a>';
print '</div>';
llxFooter();
$db->close();