*
* 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');
// Security check
if (!$user->hasRight('bankimport', 'read')) {
accessforbidden();
}
/*
* 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 '
';
// Title
print load_fiche_titre($langs->trans("Transaction"), '', 'bank');
// Card header
print '
';
print '
';
// Reference
print '';
print '| '.$langs->trans("Ref").' | ';
print ''.dol_escape_htmltag($object->ref).' | ';
print '
';
// IBAN
print '';
print '| '.$langs->trans("AccountIBAN").' | ';
print ''.dol_escape_htmltag($object->iban).' | ';
print '
';
// Date
print '';
print '| '.$langs->trans("Date").' | ';
print ''.dol_print_date($object->date_trans, 'day').' | ';
print '
';
// Value date
if ($object->date_value) {
print '';
print '| '.$langs->trans("DateValue").' | ';
print ''.dol_print_date($object->date_value, 'day').' | ';
print '
';
}
// Name
print '';
print '| '.$langs->trans("Counterparty").' | ';
print ''.dol_escape_htmltag($object->name).' | ';
print '
';
// Counterparty IBAN
if ($object->counterparty_iban) {
print '';
print '| '.$langs->trans("CounterpartyIBAN").' | ';
print ''.dol_escape_htmltag($object->counterparty_iban).' | ';
print '
';
}
// Amount
print '';
print '| '.$langs->trans("Amount").' | ';
print '';
if ($object->amount >= 0) {
print '+'.price($object->amount, 0, $langs, 1, -1, 2, $object->currency).'';
} else {
print ''.price($object->amount, 0, $langs, 1, -1, 2, $object->currency).'';
}
print ' | ';
print '
';
// Label
if ($object->label) {
print '';
print '| '.$langs->trans("Label").' | ';
print ''.dol_escape_htmltag($object->label).' | ';
print '
';
}
// Description
print '';
print '| '.$langs->trans("Description").' | ';
print ''.nl2br(dol_escape_htmltag($object->description)).' | ';
print '
';
// End-to-End ID
if ($object->end_to_end_id) {
print '';
print '| '.$langs->trans("EndToEndId").' | ';
print ''.dol_escape_htmltag($object->end_to_end_id).' | ';
print '
';
}
// Mandate ID
if ($object->mandate_id) {
print '';
print '| '.$langs->trans("MandateId").' | ';
print ''.dol_escape_htmltag($object->mandate_id).' | ';
print '
';
}
// Status
print '';
print '| '.$langs->trans("Status").' | ';
print ''.$object->getLibStatut(4).' | ';
print '
';
// 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 '';
print '| '.$langs->trans("Invoice").' | ';
print ''.$invoice->getNomUrl(1).' | ';
print '
';
}
// 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 '';
print '| '.$langs->trans("SupplierInvoice").' | ';
print ''.$invoice->getNomUrl(1).' | ';
print '
';
}
// Linked PDF statement
if (!empty($object->fk_statement)) {
dol_include_once('/bankimport/class/bankstatement.class.php');
$stmt = new BankImportStatement($db);
$stmt->fetch($object->fk_statement);
print '';
print '| '.$langs->trans("PDFStatement").' | ';
print '';
print '';
print img_picto($langs->trans("ViewPDFStatement"), 'pdf').' ';
print $langs->trans("StatementNumber").' '.$stmt->statement_number.'/'.$stmt->statement_year;
print '';
if ($stmt->date_from && $stmt->date_to) {
print ' ('.dol_print_date($stmt->date_from, 'day').' - '.dol_print_date($stmt->date_to, 'day').')';
}
print ' | ';
print '
';
}
// 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 '';
print '| '.$langs->trans("ThirdParty").' | ';
print ''.$soc->getNomUrl(1).' | ';
print '
';
}
// Import date
print '';
print '| '.$langs->trans("DateCreation").' | ';
print ''.dol_print_date($object->datec, 'dayhour').' | ';
print '
';
print '
';
print '
';
// Actions buttons
print '';
// Show matches if found
$matches = $_SESSION['bankimport_matches_'.$object->id] ?? array();
if (!empty($matches)) {
print '
';
print load_fiche_titre($langs->trans("MatchesFound", count($matches)), '', 'object_invoice');
print '';
print '
';
print '';
print '| '.$langs->trans("Type").' | ';
print ''.$langs->trans("Ref").' | ';
print ''.$langs->trans("ThirdParty").' | ';
print ''.$langs->trans("Amount").' | ';
print ''.$langs->trans("DateDue").' | ';
print ''.$langs->trans("Score").' | ';
print ''.$langs->trans("MatchReason").' | ';
print ' | ';
print '
';
// 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 '';
print '| '.($match['type'] == 'facture' ? $langs->trans("Invoice") : $langs->trans("SupplierInvoice")).' | ';
// 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 ''.$inv->getNomUrl(1).' | ';
} else {
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
$inv = new FactureFournisseur($db);
$inv->fetch($match['id']);
print ''.$inv->getNomUrl(1).' | ';
}
// 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 ''.$soc->getNomUrl(1).' | ';
} else {
print ''.dol_escape_htmltag($match['socname']).' | ';
}
print ''.price($match['amount'], 0, $langs, 1, -1, 2, 'EUR').' | ';
print ''.($match['date_due'] ? dol_print_date($match['date_due'], 'day') : '-').' | ';
// Score with color
$scoreColor = $match['match_score'] >= 80 ? 'green' : ($match['match_score'] >= 60 ? 'orange' : 'gray');
print ''.$match['match_score'].'% | ';
// Match reasons as badges
print '';
if (!empty($match['match_reasons'])) {
foreach ($match['match_reasons'] as $reason) {
$label = $reasonLabels[$reason] ?? $reason;
print ''.$label.' ';
}
}
print ' | ';
print '';
print 'id.'&action=linkto&linktype='.$match['type'].'&linkid='.$match['id'].'&token='.newToken().'">'.$langs->trans("Link").'';
print ' | ';
print '
';
}
print '
';
print '
';
}
} else {
print ''.$langs->trans("RecordNotFound").'
';
}
// Back link
print '';
llxFooter();
$db->close();