dolibarr/htdocs/compta/recap-compta.php

317 lines
10 KiB
PHP
Raw Normal View History

2005-02-28 16:19:57 +01:00
<?php
/* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2017 Pierre-Henry Favre <support@atm-consulting.fr>
2005-02-28 16:19:57 +01:00
*
* 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
2005-02-28 16:19:57 +01:00
* (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
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2005-02-28 16:19:57 +01:00
*/
/**
* \file htdocs/compta/recap-compta.php
* \ingroup compta
* \brief Page de fiche recap customer
*/
2005-02-28 16:19:57 +01:00
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
2005-02-28 16:19:57 +01:00
2018-05-27 09:27:09 +02:00
// Load translation files required by the page
2005-02-28 16:19:57 +01:00
$langs->load("companies");
2021-02-23 21:09:01 +01:00
if (!empty($conf->facture->enabled)) {
$langs->load("bills");
}
2005-02-28 16:19:57 +01:00
$id = GETPOST('id') ?GETPOST('id', 'int') : GETPOST('socid', 'int');
2017-01-01 19:53:05 +01:00
// Security check
2021-02-23 21:09:01 +01:00
if ($user->socid) {
$id = $user->socid;
}
2017-01-01 19:53:05 +01:00
$result = restrictedArea($user, 'societe', $id, '&societe');
2005-02-28 16:19:57 +01:00
2017-01-01 19:53:05 +01:00
$object = new Societe($db);
2021-02-23 21:09:01 +01:00
if ($id > 0) {
$object->fetch($id);
}
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
$hookmanager->initHooks(array('recapcomptacard', 'globalcard'));
// Load variable for pagination
$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
2020-09-18 17:13:01 +02:00
$sortfield = GETPOST('sortfield', 'aZ09comma');
2020-09-17 14:31:25 +02:00
$sortorder = GETPOST('sortorder', 'aZ09comma');
$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
2021-02-23 21:09:01 +01:00
if (empty($page) || $page == -1) {
$page = 0;
} // If $page is not defined, or '' or -1
$offset = $limit * $page;
$pageprev = $page - 1;
$pagenext = $page + 1;
2021-02-23 21:09:01 +01:00
if (!$sortfield) {
$sortfield = "f.datef,f.rowid"; // Set here default search field
}
if (!$sortorder) {
$sortorder = "DESC";
}
$arrayfields = array(
'f.datef'=>array('label'=>"Date", 'checked'=>1),
//...
);
2018-05-29 16:29:14 +02:00
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
$hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
2018-05-29 16:29:14 +02:00
/*
* Actions
*/
$parameters = array('socid' => $id);
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
2021-02-23 21:09:01 +01:00
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
// None
2005-02-28 16:19:57 +01:00
/*
* View
2005-02-28 16:19:57 +01:00
*/
$form = new Form($db);
$userstatic = new User($db);
2013-03-22 17:10:17 +01:00
$title = $langs->trans("ThirdParty").' - '.$langs->trans("Summary");
2021-02-23 21:09:01 +01:00
if (!empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) {
$title = $object->name.' - '.$langs->trans("Summary");
}
$help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
llxHeader('', $title, $help_url);
2021-02-23 21:09:01 +01:00
if ($id > 0) {
$param = '';
2021-02-23 21:09:01 +01:00
if ($id > 0) {
$param .= '&socid='.$id;
}
$head = societe_prepare_head($object);
2020-10-22 22:50:03 +02:00
print dol_get_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '', 0, '', '', 1);
2020-10-27 18:19:31 +01:00
print dol_get_fiche_end();
2018-02-15 14:47:08 +01:00
2021-02-23 21:09:01 +01:00
if (!empty($conf->facture->enabled) && $user->rights->facture->lire) {
// Invoice list
print load_fiche_titre($langs->trans("CustomerPreview"));
2019-11-05 21:24:41 +01:00
print '<table class="noborder tagtable liste centpercent">';
print '<tr class="liste_titre">';
2021-02-23 21:09:01 +01:00
if (!empty($arrayfields['f.datef']['checked'])) {
print_liste_field_titre($arrayfields['f.datef']['label'], $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
}
print '<td>'.$langs->trans("Element").'</td>';
print '<td>'.$langs->trans("Status").'</td>';
2019-02-14 16:59:24 +01:00
print '<td class="right">'.$langs->trans("Debit").'</td>';
print '<td class="right">'.$langs->trans("Credit").'</td>';
print '<td class="right">'.$langs->trans("Balance").'</td>';
print '<td class="right">'.$langs->trans("Author").'</td>';
print '</tr>';
2018-02-15 14:47:08 +01:00
$TData = array();
$sql = "SELECT s.nom, s.rowid as socid, f.ref, f.total_ttc, f.datef as df,";
$sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
$sql .= " u.login, u.rowid as userid";
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
$sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = ".((int) $object->id);
$sql .= " AND f.entity IN (".getEntity('invoice').")";
$sql .= " AND f.fk_user_valid = u.rowid";
$sql .= $db->order($sortfield, $sortorder);
$resql = $db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resql) {
2012-02-01 18:29:06 +01:00
$num = $db->num_rows($resql);
2012-02-01 18:29:06 +01:00
// Boucle sur chaque facture
2021-02-23 21:09:01 +01:00
for ($i = 0; $i < $num; $i++) {
2012-02-01 18:29:06 +01:00
$objf = $db->fetch_object($resql);
2012-02-01 18:29:06 +01:00
$fac = new Facture($db);
$ret = $fac->fetch($objf->facid);
2021-02-23 21:09:01 +01:00
if ($ret < 0) {
2012-02-01 18:29:06 +01:00
print $fac->error."<br>";
continue;
}
$totalpaye = $fac->getSommePaiement();
2018-02-15 14:47:08 +01:00
$userstatic->id = $objf->userid;
$userstatic->login = $objf->login;
2018-02-15 14:47:08 +01:00
$values = array(
'fk_facture' => $objf->facid,
'date' => $fac->date,
2018-02-15 14:47:08 +01:00
'datefieldforsort' => $fac->date.'-'.$fac->ref,
'link' => $fac->getNomUrl(1),
'status' => $fac->getLibStatut(2, $totalpaye),
'amount' => $fac->total_ttc,
'author' => $userstatic->getLoginUrl(1)
);
2018-02-15 14:47:08 +01:00
$parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
$reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
2021-02-23 21:09:01 +01:00
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
2018-02-15 14:47:08 +01:00
$TData[] = $values;
2012-02-01 18:29:06 +01:00
// Paiements
$sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
$sql .= " p.fk_user_creat, u.login, u.rowid as userid";
$sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
$sql .= " ".MAIN_DB_PREFIX."paiement as p";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
$sql .= " WHERE pf.fk_paiement = p.rowid";
$sql .= " AND p.entity = ".$conf->entity;
2021-06-09 15:36:47 +02:00
$sql .= " AND pf.fk_facture = ".((int) $fac->id);
$sql .= " ORDER BY p.datep ASC, p.rowid ASC";
2012-02-01 18:29:06 +01:00
$resqlp = $db->query($sql);
2021-02-23 21:09:01 +01:00
if ($resqlp) {
2012-02-01 18:29:06 +01:00
$nump = $db->num_rows($resqlp);
$j = 0;
2021-02-23 21:09:01 +01:00
while ($j < $nump) {
2012-02-01 18:29:06 +01:00
$objp = $db->fetch_object($resqlp);
2018-02-15 14:47:08 +01:00
$paymentstatic = new Paiement($db);
$paymentstatic->id = $objp->rowid;
2018-02-15 14:47:08 +01:00
$userstatic->id = $objp->userid;
$userstatic->login = $objp->login;
2018-02-15 14:47:08 +01:00
$values = array(
2018-02-15 14:47:08 +01:00
'fk_paiement' => $objp->rowid,
'date' => $db->jdate($objp->dp),
2018-02-15 14:47:08 +01:00
'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
'link' => $langs->trans("Payment").' '.$paymentstatic->getNomUrl(1),
'status' => '',
'amount' => -$objp->amount,
'author' => $userstatic->getLoginUrl(1)
);
2018-02-15 14:47:08 +01:00
$parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
$reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
2021-02-23 21:09:01 +01:00
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
2018-02-15 14:47:08 +01:00
$TData[] = $values;
2012-02-01 18:29:06 +01:00
$j++;
}
2012-02-01 18:29:06 +01:00
$db->free($resqlp);
2020-05-21 15:05:19 +02:00
} else {
2012-02-01 18:29:06 +01:00
dol_print_error($db);
}
}
2020-05-21 15:05:19 +02:00
} else {
2012-02-01 18:29:06 +01:00
dol_print_error($db);
}
2018-02-15 14:47:08 +01:00
if (empty($TData)) {
print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoInvoice").'</td></tr>';
} else {
2018-02-15 14:47:08 +01:00
// Sort array by date ASC to calucalte balance
$TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
// Balance calculation
2018-02-15 14:47:08 +01:00
$balance = 0;
foreach ($TData as &$data1) {
$balance += $data1['amount'];
$data1['balance'] += $balance;
}
2018-02-15 14:47:08 +01:00
// Resorte array to have elements on the required $sortorder
$TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
$totalDebit = 0;
$totalCredit = 0;
2018-02-15 14:47:08 +01:00
// Display array
foreach ($TData as $data) {
2017-10-23 09:01:58 +02:00
$html_class = '';
2021-02-23 21:09:01 +01:00
if (!empty($data['fk_facture'])) {
$html_class = 'facid-'.$data['fk_facture'];
} elseif (!empty($data['fk_paiement'])) {
$html_class = 'payid-'.$data['fk_paiement'];
}
2018-02-15 14:47:08 +01:00
2017-10-23 09:01:58 +02:00
print '<tr class="oddeven '.$html_class.'">';
2018-02-15 14:47:08 +01:00
2019-03-07 20:46:38 +01:00
print "<td class=\"center\">";
2021-02-23 21:09:01 +01:00
if (!empty($data['fk_facture'])) {
print dol_print_date($data['date'], 'day');
} elseif (!empty($data['fk_paiement'])) {
print dol_print_date($data['date'], 'dayhour');
}
2018-02-15 14:47:08 +01:00
print "</td>\n";
2018-05-29 16:29:14 +02:00
print '<td>'.$data['link']."</td>\n";
2018-02-15 14:47:08 +01:00
2019-03-07 20:46:38 +01:00
print '<td class="left">'.$data['status'].'</td>';
2019-02-14 16:59:24 +01:00
print '<td class="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
2019-03-07 20:46:38 +01:00
$totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
2019-03-07 20:46:38 +01:00
2019-02-14 16:59:24 +01:00
print '<td class="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
$totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
2019-03-07 20:46:38 +01:00
2018-02-15 14:47:08 +01:00
// Balance
2021-03-29 13:00:17 +02:00
print '<td class="right"><span class="amount">'.price($data['balance'])."</span></td>\n";
2018-02-15 14:47:08 +01:00
// Author
2019-02-14 16:59:24 +01:00
print '<td class="nowrap right">';
print $data['author'];
print '</td>';
2018-02-15 14:47:08 +01:00
print "</tr>\n";
}
2018-02-15 14:47:08 +01:00
print '<tr class="liste_total">';
print '<td colspan="3">&nbsp;</td>';
2019-02-14 16:59:24 +01:00
print '<td class="right">'.price($totalDebit).'</td>';
print '<td class="right">'.price($totalCredit).'</td>';
print '<td class="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
print '<td></td>';
print "</tr>\n";
}
2018-02-15 14:47:08 +01:00
2012-02-01 18:29:06 +01:00
print "</table>";
}
2020-05-21 15:05:19 +02:00
} else {
2012-02-01 18:29:06 +01:00
dol_print_error($db);
2005-02-28 16:19:57 +01:00
}
2005-07-15 02:26:54 +02:00
2012-02-01 18:29:06 +01:00
llxFooter();
2005-07-15 02:26:54 +02:00
2005-02-28 16:19:57 +01:00
$db->close();