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