dolibarr 24.0.0-beta
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';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
39require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
40
41// Load translation files required by the page
42$langs->loadLangs(array("companies", "bills"));
43
44$action = GETPOST('action');
45$dol_openinpopup = GETPOST('dol_openinpopup', 'aZ09');
46
47$id = GETPOST('id') ? GETPOSTINT('id') : GETPOSTINT('socid');
48
49// Security check
50if ($user->socid > 0) {
51 $id = $user->socid;
52}
53
54// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
55$hookmanager->initHooks(array('recapcomptacard', 'globalcard'));
56
57$result = restrictedArea($user, 'societe', $id, '&societe');
58
59$object = new Societe($db);
60if ($id > 0) {
61 $object->fetch($id);
62}
63
64
65// Load variable for pagination
66$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
67$sortfield = GETPOST('sortfield', 'aZ09comma');
68$sortorder = GETPOST('sortorder', 'aZ09comma');
69$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
70if (empty($page) || $page == -1) {
71 $page = 0;
72} // If $page is not defined, or '' or -1
73$offset = $limit * $page;
74$pageprev = $page - 1;
75$pagenext = $page + 1;
76if (!$sortfield) {
77 $sortfield = "f.datef,f.rowid"; // Set here default search field
78}
79if (!$sortorder) {
80 $sortorder = "DESC";
81}
82
83
84$arrayfields = array(
85 'f.datef' => array('label' => "Date", 'checked' => 1),
86 //...
87);
88
89// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
90$hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
91
92
93/*
94 * Actions
95 */
96
97$parameters = array('socid' => $id);
98$reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
99if ($reshook < 0) {
100 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
101}
102
103// None
104
105
106/*
107 * View
108 */
109
110$form = new Form($db);
111$userstatic = new User($db);
112
113$title = $langs->trans("ThirdParty").' - '.$langs->trans("Summary");
114if (getDolGlobalString('MAIN_HTML_TITLE') && preg_match('/thirdpartynameonly/', getDolGlobalString('MAIN_HTML_TITLE')) && $object->name) {
115 $title = $object->name.' - '.$langs->trans("Summary");
116}
117$help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
118
119llxHeader('', $title, $help_url);
120
121if ($id > 0) {
122 $param = '';
123 if ($id > 0) {
124 $param .= '&socid='.$id;
125 }
126 if ($dol_openinpopup) {
127 $param .= '&dol_openinpopup='.urlencode($dol_openinpopup);
128 }
129
130 if (empty($dol_openinpopup)) {
131 $head = societe_prepare_head($object);
132
133 print dol_get_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
134 dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '', 0, '', '', 1);
135 print dol_get_fiche_end();
136 }
137
138 if (isModEnabled('invoice') && $user->hasRight('facture', 'lire')) {
139 // Invoice list
140 print load_fiche_titre($langs->trans("CustomerPreview"));
141
142 print '<table class="noborder tagtable liste centpercent">';
143 print '<tr class="liste_titre">';
144 if (!empty($arrayfields['f.datef']['checked'])) {
145 print_liste_field_titre($arrayfields['f.datef']['label'], $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
146 }
147 print '<td>'.$langs->trans("Element").'</td>';
148 print '<td>'.$langs->trans("Status").'</td>';
149 print '<td class="right">'.$langs->trans("Debit").'</td>';
150 print '<td class="right">'.$langs->trans("Credit").'</td>';
151 print '<td class="right">'.$langs->trans("Balance").'</td>';
152 print '<td class="right">'.$langs->trans("Author").'</td>';
153 print '</tr>';
154
158 $TData = array();
159
160 $sql = "SELECT s.nom, s.rowid as socid, f.ref, f.total_ttc, f.datef as df,";
161 $sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
162 $sql .= " u.login, u.rowid as userid";
163 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
164 $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = ".((int) $object->id);
165 $sql .= " AND f.entity IN (".getEntity('invoice').")";
166 $sql .= " AND f.fk_user_valid = u.rowid";
167 $sql .= $db->order($sortfield, $sortorder);
168
169 $resql = $db->query($sql);
170 if ($resql) {
171 $num = $db->num_rows($resql);
172
173 // Boucle sur chaque facture
174 for ($i = 0; $i < $num; $i++) {
175 $objf = $db->fetch_object($resql);
176
177 $fac = new Facture($db);
178 $ret = $fac->fetch($objf->facid);
179 if ($ret < 0) {
180 print $fac->error."<br>";
181 continue;
182 }
183
184 $alreadypaid = $fac->getSommePaiement();
185 $alreadypaid += $fac->getSumDepositsUsed();
186 $alreadypaid += $fac->getSumCreditNotesUsed();
187
188 $userstatic->id = $objf->userid;
189 $userstatic->login = $objf->login;
190
191 $values = array(
192 'fk_facture' => $objf->facid,
193 'date' => $fac->date,
194 'datefieldforsort' => $fac->date.'-'.$fac->ref,
195 'link' => $fac->getNomUrl(1),
196 'status' => $fac->getLibStatut(2, $alreadypaid),
197 'amount' => $fac->total_ttc,
198 'author' => $userstatic->getLoginUrl(1)
199 );
200
201 $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
202 $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
203 if ($reshook < 0) {
204 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
205 }
206
207 $TData[] = $values;
208
209 // Paiements
210 $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
211 $sql .= " p.fk_user_creat, u.login, u.rowid as userid";
212 $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
213 $sql .= " ".MAIN_DB_PREFIX."paiement as p";
214 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
215 $sql .= " WHERE pf.fk_paiement = p.rowid";
216 $sql .= " AND p.entity = ".$conf->entity;
217 $sql .= " AND pf.fk_facture = ".((int) $fac->id);
218 $sql .= " ORDER BY p.datep ASC, p.rowid ASC";
219
220 $resqlp = $db->query($sql);
221 if ($resqlp) {
222 $nump = $db->num_rows($resqlp);
223 $j = 0;
224
225 while ($j < $nump) {
226 $objp = $db->fetch_object($resqlp);
227
228 $paymentstatic = new Paiement($db);
229 $paymentstatic->id = $objp->rowid;
230
231 $userstatic->id = $objp->userid;
232 $userstatic->login = $objp->login;
233
234 $values = array(
235 'fk_paiement' => $objp->rowid,
236 'date' => $db->jdate($objp->dp),
237 'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
238 'link' => $langs->trans("Payment").' '.$paymentstatic->getNomUrl(1),
239 'status' => '',
240 'amount' => -$objp->amount,
241 'author' => $userstatic->getLoginUrl(1)
242 );
243
244 $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
245 $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
246 if ($reshook < 0) {
247 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
248 }
249
250 $TData[] = $values;
251
252 $j++;
253 }
254
255 $db->free($resqlp);
256 } else {
258 }
259 }
260 } else {
262 }
263
264 if (empty($TData)) {
265 print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">'.$langs->trans("NoInvoice").'</span></td></tr>';
266 } else {
267 // Sort array by date ASC to calculate balance
268 $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
269
270 // Balance calculation
271 $balance = 0;
272 foreach (array_keys($TData) as $key) {
273 $balance += $TData[$key]['amount'];
274 if (!array_key_exists('balance', $TData[$key])) {
275 $TData[$key]['balance'] = 0;
276 }
277 $TData[$key]['balance'] += $balance;
278 }
279
280 // Resorte array to have elements on the required $sortorder
281 $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
282
283 $totalDebit = 0;
284 $totalCredit = 0;
285
286 // Display array
287 foreach ($TData as $data) {
288 $html_class = '';
289 if (!empty($data['fk_facture'])) {
290 $html_class = 'facid-'.$data['fk_facture'];
291 } elseif (!empty($data['fk_paiement'])) {
292 $html_class = 'payid-'.$data['fk_paiement'];
293 }
294
295 print '<tr class="oddeven '.$html_class.'">';
296
297 $datedetail = dol_print_date($data['date'], 'dayhour');
298 if (!empty($data['fk_facture'])) {
299 $datedetail = dol_print_date($data['date'], 'day');
300 }
301 print '<td class="center" title="'.dol_escape_htmltag($datedetail).'">';
302 print dol_print_date($data['date'], 'day');
303 print "</td>\n";
304
305 print '<td>'.$data['link']."</td>\n";
306
307 print '<td class="left">'.$data['status'].'</td>';
308
309 print '<td class="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
310
311 $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
312
313 print '<td class="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
314 $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
315
316 // Balance
317 print '<td class="right"><span class="amount">'.price($data['balance'])."</span></td>\n";
318
319 // Author
320 print '<td class="nowrap right">';
321 print $data['author'];
322 print '</td>';
323
324 print "</tr>\n";
325 }
326
327 print '<tr class="liste_total">';
328 print '<td colspan="3">&nbsp;</td>';
329 print '<td class="right">'.price($totalDebit).'</td>';
330 print '<td class="right">'.price($totalCredit).'</td>';
331 print '<td class="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
332 print '<td></td>';
333 print "</tr>\n";
334 }
335
336 print "</table>";
337 }
338} else {
340}
341
342llxFooter();
343
344$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, $subtabs='')
Return array of tabs to used on pages for third parties cards.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.