dolibarr 23.0.3
recap-compta.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2017 Pierre-Henry Favre <support@atm-consulting.fr>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
32require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
33
42// Load translation files required by the page
43$langs->load("companies");
44if (isModEnabled('invoice')) {
45 $langs->load("bills");
46}
47
48$id = GETPOST('id') ? GETPOSTINT('id') : GETPOSTINT('socid');
49
50// Security check
51if ($user->socid > 0) {
52 $id = $user->socid;
53}
54
55// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
56$hookmanager->initHooks(array('recapcomptacard', 'globalcard'));
57
58$result = restrictedArea($user, 'societe', $id, '&societe');
59
60$object = new Societe($db);
61if ($id > 0) {
62 $object->fetch($id);
63}
64
65
66// Load variable for pagination
67$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
68$sortfield = GETPOST('sortfield', 'aZ09comma');
69$sortorder = GETPOST('sortorder', 'aZ09comma');
70$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
71if (empty($page) || $page == -1) {
72 $page = 0;
73} // If $page is not defined, or '' or -1
74$offset = $limit * $page;
75$pageprev = $page - 1;
76$pagenext = $page + 1;
77if (!$sortfield) {
78 $sortfield = "f.datef,f.rowid"; // Set here default search field
79}
80if (!$sortorder) {
81 $sortorder = "DESC";
82}
83
84
85$arrayfields = array(
86 'f.datef' => array('label' => "Date", 'checked' => 1),
87 //...
88);
89
90// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
91$hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
92
93
94/*
95 * Actions
96 */
97
98$parameters = array('socid' => $id);
99$reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
100if ($reshook < 0) {
101 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
102}
103
104// None
105
106
107/*
108 * View
109 */
110
111$form = new Form($db);
112$userstatic = new User($db);
113
114$title = $langs->trans("ThirdParty").' - '.$langs->trans("Summary");
115if (getDolGlobalString('MAIN_HTML_TITLE') && preg_match('/thirdpartynameonly/', getDolGlobalString('MAIN_HTML_TITLE')) && $object->name) {
116 $title = $object->name.' - '.$langs->trans("Summary");
117}
118$help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
119
120llxHeader('', $title, $help_url);
121
122if ($id > 0) {
123 $param = '';
124 if ($id > 0) {
125 $param .= '&socid='.$id;
126 }
127
128 $head = societe_prepare_head($object);
129
130 print dol_get_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
131 dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '', 0, '', '', 1);
132 print dol_get_fiche_end();
133
134 if (isModEnabled('invoice') && $user->hasRight('facture', 'lire')) {
135 // Invoice list
136 print load_fiche_titre($langs->trans("CustomerPreview"));
137
138 print '<table class="noborder tagtable liste centpercent">';
139 print '<tr class="liste_titre">';
140 if (!empty($arrayfields['f.datef']['checked'])) {
141 print_liste_field_titre($arrayfields['f.datef']['label'], $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
142 }
143 print '<td>'.$langs->trans("Element").'</td>';
144 print '<td>'.$langs->trans("Status").'</td>';
145 print '<td class="right">'.$langs->trans("Debit").'</td>';
146 print '<td class="right">'.$langs->trans("Credit").'</td>';
147 print '<td class="right">'.$langs->trans("Balance").'</td>';
148 print '<td class="right">'.$langs->trans("Author").'</td>';
149 print '</tr>';
150
154 $TData = array();
155
156 $sql = "SELECT s.nom, s.rowid as socid, f.ref, f.total_ttc, f.datef as df,";
157 $sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
158 $sql .= " u.login, u.rowid as userid";
159 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
160 $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = ".((int) $object->id);
161 $sql .= " AND f.entity IN (".getEntity('invoice').")";
162 $sql .= " AND f.fk_user_valid = u.rowid";
163 $sql .= $db->order($sortfield, $sortorder);
164
165 $resql = $db->query($sql);
166 if ($resql) {
167 $num = $db->num_rows($resql);
168
169 // Boucle sur chaque facture
170 for ($i = 0; $i < $num; $i++) {
171 $objf = $db->fetch_object($resql);
172
173 $fac = new Facture($db);
174 $ret = $fac->fetch($objf->facid);
175 if ($ret < 0) {
176 print $fac->error."<br>";
177 continue;
178 }
179
180 $alreadypaid = $fac->getSommePaiement();
181 $alreadypaid += $fac->getSumDepositsUsed();
182 $alreadypaid += $fac->getSumCreditNotesUsed();
183
184 $userstatic->id = $objf->userid;
185 $userstatic->login = $objf->login;
186
187 $values = array(
188 'fk_facture' => $objf->facid,
189 'date' => $fac->date,
190 'datefieldforsort' => $fac->date.'-'.$fac->ref,
191 'link' => $fac->getNomUrl(1),
192 'status' => $fac->getLibStatut(2, $alreadypaid),
193 'amount' => $fac->total_ttc,
194 'author' => $userstatic->getLoginUrl(1)
195 );
196
197 $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
198 $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
199 if ($reshook < 0) {
200 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
201 }
202
203 $TData[] = $values;
204
205 // Paiements
206 $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
207 $sql .= " p.fk_user_creat, u.login, u.rowid as userid";
208 $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
209 $sql .= " ".MAIN_DB_PREFIX."paiement as p";
210 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
211 $sql .= " WHERE pf.fk_paiement = p.rowid";
212 $sql .= " AND p.entity = ".$conf->entity;
213 $sql .= " AND pf.fk_facture = ".((int) $fac->id);
214 $sql .= " ORDER BY p.datep ASC, p.rowid ASC";
215
216 $resqlp = $db->query($sql);
217 if ($resqlp) {
218 $nump = $db->num_rows($resqlp);
219 $j = 0;
220
221 while ($j < $nump) {
222 $objp = $db->fetch_object($resqlp);
223
224 $paymentstatic = new Paiement($db);
225 $paymentstatic->id = $objp->rowid;
226
227 $userstatic->id = $objp->userid;
228 $userstatic->login = $objp->login;
229
230 $values = array(
231 'fk_paiement' => $objp->rowid,
232 'date' => $db->jdate($objp->dp),
233 'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
234 'link' => $langs->trans("Payment").' '.$paymentstatic->getNomUrl(1),
235 'status' => '',
236 'amount' => -$objp->amount,
237 'author' => $userstatic->getLoginUrl(1)
238 );
239
240 $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
241 $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
242 if ($reshook < 0) {
243 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
244 }
245
246 $TData[] = $values;
247
248 $j++;
249 }
250
251 $db->free($resqlp);
252 } else {
253 dol_print_error($db);
254 }
255 }
256 } else {
257 dol_print_error($db);
258 }
259
260 if (empty($TData)) {
261 print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">'.$langs->trans("NoInvoice").'</span></td></tr>';
262 } else {
263 // Sort array by date ASC to calculate balance
264 $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
265
266 // Balance calculation
267 $balance = 0;
268 foreach (array_keys($TData) as $key) {
269 $balance += $TData[$key]['amount'];
270 if (!array_key_exists('balance', $TData[$key])) {
271 $TData[$key]['balance'] = 0;
272 }
273 $TData[$key]['balance'] += $balance;
274 }
275
276 // Resorte array to have elements on the required $sortorder
277 $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
278
279 $totalDebit = 0;
280 $totalCredit = 0;
281
282 // Display array
283 foreach ($TData as $data) {
284 $html_class = '';
285 if (!empty($data['fk_facture'])) {
286 $html_class = 'facid-'.$data['fk_facture'];
287 } elseif (!empty($data['fk_paiement'])) {
288 $html_class = 'payid-'.$data['fk_paiement'];
289 }
290
291 print '<tr class="oddeven '.$html_class.'">';
292
293 $datedetail = dol_print_date($data['date'], 'dayhour');
294 if (!empty($data['fk_facture'])) {
295 $datedetail = dol_print_date($data['date'], 'day');
296 }
297 print '<td class="center" title="'.dol_escape_htmltag($datedetail).'">';
298 print dol_print_date($data['date'], 'day');
299 print "</td>\n";
300
301 print '<td>'.$data['link']."</td>\n";
302
303 print '<td class="left">'.$data['status'].'</td>';
304
305 print '<td class="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
306
307 $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
308
309 print '<td class="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
310 $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
311
312 // Balance
313 print '<td class="right"><span class="amount">'.price($data['balance'])."</span></td>\n";
314
315 // Author
316 print '<td class="nowrap right">';
317 print $data['author'];
318 print '</td>';
319
320 print "</tr>\n";
321 }
322
323 print '<tr class="liste_total">';
324 print '<td colspan="3">&nbsp;</td>';
325 print '<td class="right">'.price($totalDebit).'</td>';
326 print '<td class="right">'.price($totalCredit).'</td>';
327 print '<td class="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
328 print '<td></td>';
329 print "</tr>\n";
330 }
331
332 print "</table>";
333 }
334} else {
335 dol_print_error($db);
336}
337
338llxFooter();
339
340$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage invoices.
Class to manage generation of HTML components Only common components must be here.
Class to manage payments of customer invoices.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by the value of a given key, which produces ascending (default) or descending out...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.