dolibarr 21.0.0-alpha
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
31
32// Load translation files required by the page
33$langs->load("companies");
34if (isModEnabled('invoice')) {
35 $langs->load("bills");
36}
37
38$id = GETPOST('id') ? GETPOSTINT('id') : GETPOSTINT('socid');
39
40// Security check
41if ($user->socid > 0) {
42 $id = $user->socid;
43}
44
45$result = restrictedArea($user, 'societe', $id, '&societe');
46
47$object = new Societe($db);
48if ($id > 0) {
49 $object->fetch($id);
50}
51
52// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
53$hookmanager->initHooks(array('recapcomptacard', 'globalcard'));
54
55// Load variable for pagination
56$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
57$sortfield = GETPOST('sortfield', 'aZ09comma');
58$sortorder = GETPOST('sortorder', 'aZ09comma');
59$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
60if (empty($page) || $page == -1) {
61 $page = 0;
62} // If $page is not defined, or '' or -1
63$offset = $limit * $page;
64$pageprev = $page - 1;
65$pagenext = $page + 1;
66if (!$sortfield) {
67 $sortfield = "f.datef,f.rowid"; // Set here default search field
68}
69if (!$sortorder) {
70 $sortorder = "DESC";
71}
72
73
74$arrayfields = array(
75 'f.datef'=>array('label'=>"Date", 'checked'=>1),
76 //...
77);
78
79// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
80$hookmanager->initHooks(array('supplierbalencelist', 'globalcard'));
81
82
83/*
84 * Actions
85 */
86
87$parameters = array('socid' => $id);
88$reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
89if ($reshook < 0) {
90 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
91}
92
93// None
94
95
96/*
97 * View
98 */
99
100$form = new Form($db);
101$userstatic = new User($db);
102
103$title = $langs->trans("ThirdParty").' - '.$langs->trans("Summary");
104if (getDolGlobalString('MAIN_HTML_TITLE') && preg_match('/thirdpartynameonly/', getDolGlobalString('MAIN_HTML_TITLE')) && $object->name) {
105 $title = $object->name.' - '.$langs->trans("Summary");
106}
107$help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
108
109llxHeader('', $title, $help_url);
110
111if ($id > 0) {
112 $param = '';
113 if ($id > 0) {
114 $param .= '&socid='.$id;
115 }
116
117 $head = societe_prepare_head($object);
118
119 print dol_get_fiche_head($head, 'customer', $langs->trans("ThirdParty"), 0, 'company');
120 dol_banner_tab($object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom', '', '', 0, '', '', 1);
121 print dol_get_fiche_end();
122
123 if (isModEnabled('invoice') && $user->hasRight('facture', 'lire')) {
124 // Invoice list
125 print load_fiche_titre($langs->trans("CustomerPreview"));
126
127 print '<table class="noborder tagtable liste centpercent">';
128 print '<tr class="liste_titre">';
129 if (!empty($arrayfields['f.datef']['checked'])) {
130 print_liste_field_titre($arrayfields['f.datef']['label'], $_SERVER["PHP_SELF"], "f.datef", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
131 }
132 print '<td>'.$langs->trans("Element").'</td>';
133 print '<td>'.$langs->trans("Status").'</td>';
134 print '<td class="right">'.$langs->trans("Debit").'</td>';
135 print '<td class="right">'.$langs->trans("Credit").'</td>';
136 print '<td class="right">'.$langs->trans("Balance").'</td>';
137 print '<td class="right">'.$langs->trans("Author").'</td>';
138 print '</tr>';
139
140 $TData = array();
141
142 $sql = "SELECT s.nom, s.rowid as socid, f.ref, f.total_ttc, f.datef as df,";
143 $sql .= " f.paye as paye, f.fk_statut as statut, f.rowid as facid,";
144 $sql .= " u.login, u.rowid as userid";
145 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f,".MAIN_DB_PREFIX."user as u";
146 $sql .= " WHERE f.fk_soc = s.rowid AND s.rowid = ".((int) $object->id);
147 $sql .= " AND f.entity IN (".getEntity('invoice').")";
148 $sql .= " AND f.fk_user_valid = u.rowid";
149 $sql .= $db->order($sortfield, $sortorder);
150
151 $resql = $db->query($sql);
152 if ($resql) {
153 $num = $db->num_rows($resql);
154
155 // Boucle sur chaque facture
156 for ($i = 0; $i < $num; $i++) {
157 $objf = $db->fetch_object($resql);
158
159 $fac = new Facture($db);
160 $ret = $fac->fetch($objf->facid);
161 if ($ret < 0) {
162 print $fac->error."<br>";
163 continue;
164 }
165 $totalpaid = $fac->getSommePaiement();
166
167 $userstatic->id = $objf->userid;
168 $userstatic->login = $objf->login;
169
170 $values = array(
171 'fk_facture' => $objf->facid,
172 'date' => $fac->date,
173 'datefieldforsort' => $fac->date.'-'.$fac->ref,
174 'link' => $fac->getNomUrl(1),
175 'status' => $fac->getLibStatut(2, $totalpaid),
176 'amount' => $fac->total_ttc,
177 'author' => $userstatic->getLoginUrl(1)
178 );
179
180 $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic);
181 $reshook = $hookmanager->executeHooks('facdao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
182 if ($reshook < 0) {
183 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
184 }
185
186 $TData[] = $values;
187
188 // Paiements
189 $sql = "SELECT p.rowid, p.datep as dp, pf.amount, p.statut,";
190 $sql .= " p.fk_user_creat, u.login, u.rowid as userid";
191 $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf,";
192 $sql .= " ".MAIN_DB_PREFIX."paiement as p";
193 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON p.fk_user_creat = u.rowid";
194 $sql .= " WHERE pf.fk_paiement = p.rowid";
195 $sql .= " AND p.entity = ".$conf->entity;
196 $sql .= " AND pf.fk_facture = ".((int) $fac->id);
197 $sql .= " ORDER BY p.datep ASC, p.rowid ASC";
198
199 $resqlp = $db->query($sql);
200 if ($resqlp) {
201 $nump = $db->num_rows($resqlp);
202 $j = 0;
203
204 while ($j < $nump) {
205 $objp = $db->fetch_object($resqlp);
206
207 $paymentstatic = new Paiement($db);
208 $paymentstatic->id = $objp->rowid;
209
210 $userstatic->id = $objp->userid;
211 $userstatic->login = $objp->login;
212
213 $values = array(
214 'fk_paiement' => $objp->rowid,
215 'date' => $db->jdate($objp->dp),
216 'datefieldforsort' => $db->jdate($objp->dp).'-'.$fac->ref,
217 'link' => $langs->trans("Payment").' '.$paymentstatic->getNomUrl(1),
218 'status' => '',
219 'amount' => -$objp->amount,
220 'author' => $userstatic->getLoginUrl(1)
221 );
222
223 $parameters = array('socid' => $id, 'values' => &$values, 'fac' => $fac, 'userstatic' => $userstatic, 'paymentstatic' => $paymentstatic);
224 $reshook = $hookmanager->executeHooks('paydao', $parameters, $object); // Note that $parameters['values'] and $object may have been modified by some hooks
225 if ($reshook < 0) {
226 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
227 }
228
229 $TData[] = $values;
230
231 $j++;
232 }
233
234 $db->free($resqlp);
235 } else {
236 dol_print_error($db);
237 }
238 }
239 } else {
240 dol_print_error($db);
241 }
242
243 if (empty($TData)) {
244 print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">'.$langs->trans("NoInvoice").'</span></td></tr>';
245 } else {
246 // Sort array by date ASC to calculate balance
247 $TData = dol_sort_array($TData, 'datefieldforsort', 'ASC');
248
249 // Balance calculation
250 $balance = 0;
251 foreach ($TData as &$data1) {
252 $balance += $data1['amount'];
253 if (!isset($data1['balance'])) {
254 $data1['balance'] = 0;
255 }
256 $data1['balance'] += $balance;
257 }
258
259 // Resorte array to have elements on the required $sortorder
260 $TData = dol_sort_array($TData, 'datefieldforsort', $sortorder);
261
262 $totalDebit = 0;
263 $totalCredit = 0;
264
265 // Display array
266 foreach ($TData as $data) {
267 $html_class = '';
268 if (!empty($data['fk_facture'])) {
269 $html_class = 'facid-'.$data['fk_facture'];
270 } elseif (!empty($data['fk_paiement'])) {
271 $html_class = 'payid-'.$data['fk_paiement'];
272 }
273
274 print '<tr class="oddeven '.$html_class.'">';
275
276 $datedetail = dol_print_date($data['date'], 'dayhour');
277 if (!empty($data['fk_facture'])) {
278 $datedetail = dol_print_date($data['date'], 'day');
279 }
280 print '<td class="center" title="'.dol_escape_htmltag($datedetail).'">';
281 print dol_print_date($data['date'], 'day');
282 print "</td>\n";
283
284 print '<td>'.$data['link']."</td>\n";
285
286 print '<td class="left">'.$data['status'].'</td>';
287
288 print '<td class="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
289
290 $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
291
292 print '<td class="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
293 $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
294
295 // Balance
296 print '<td class="right"><span class="amount">'.price($data['balance'])."</span></td>\n";
297
298 // Author
299 print '<td class="nowrap right">';
300 print $data['author'];
301 print '</td>';
302
303 print "</tr>\n";
304 }
305
306 print '<tr class="liste_total">';
307 print '<td colspan="3">&nbsp;</td>';
308 print '<td class="right">'.price($totalDebit).'</td>';
309 print '<td class="right">'.price($totalCredit).'</td>';
310 print '<td class="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
311 print '<td></td>';
312 print "</tr>\n";
313 }
314
315 print "</table>";
316 }
317} else {
318 dol_print_error($db);
319}
320
321llxFooter();
322
323$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:70
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.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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)
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_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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...
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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.