dolibarr  13.0.2
cabyuser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2013 Antoine Iauch <aiauch@gpcsolutions.fr>
6  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
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 
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31 
32 // Load translation files required by the page
33 $langs->load("accountancy");
34 
35 $socid = GETPOST('socid', 'int');
36 
37 // Security check
38 if ($user->socid > 0) $socid = $user->socid;
39 if (!empty($conf->comptabilite->enabled)) $result = restrictedArea($user, 'compta', '', '', 'resultat');
40 if (!empty($conf->accounting->enabled)) $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
41 
42 // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
43 $modecompta = $conf->global->ACCOUNTING_MODE;
44 if (GETPOST("modecompta")) $modecompta = GETPOST("modecompta");
45 
46 $sortorder = isset($_GET["sortorder"]) ? $_GET["sortorder"] : $_POST["sortorder"];
47 $sortfield = isset($_GET["sortfield"]) ? $_GET["sortfield"] : $_POST["sortfield"];
48 if (!$sortorder) $sortorder = "asc";
49 if (!$sortfield) $sortfield = "name";
50 
51 // Date range
52 $year = GETPOST("year");
53 $month = GETPOST("month");
54 $date_startyear = GETPOST("date_startyear");
55 $date_startmonth = GETPOST("date_startmonth");
56 $date_startday = GETPOST("date_startday");
57 $date_endyear = GETPOST("date_endyear");
58 $date_endmonth = GETPOST("date_endmonth");
59 $date_endday = GETPOST("date_endday");
60 if (empty($year))
61 {
62  $year_current = strftime("%Y", dol_now());
63  $month_current = strftime("%m", dol_now());
64  $year_start = $year_current;
65 } else {
66  $year_current = $year;
67  $month_current = strftime("%m", dol_now());
68  $year_start = $year;
69 }
70 $date_start = dol_mktime(0, 0, 0, $_REQUEST["date_startmonth"], $_REQUEST["date_startday"], $_REQUEST["date_startyear"]);
71 $date_end = dol_mktime(23, 59, 59, $_REQUEST["date_endmonth"], $_REQUEST["date_endday"], $_REQUEST["date_endyear"]);
72 // Quarter
73 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
74 {
75  $q = GETPOST("q") ?GETPOST("q") : 0;
76  if ($q == 0)
77  {
78  // We define date_start and date_end
79  $month_start = GETPOST("month") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
80  $year_end = $year_start;
81  $month_end = $month_start;
82  if (!GETPOST("month")) // If month not forced
83  {
84  if (!GETPOST('year') && $month_start > $month_current)
85  {
86  $year_start--;
87  $year_end--;
88  }
89  $month_end = $month_start - 1;
90  if ($month_end < 1) $month_end = 12;
91  else $year_end++;
92  }
93  $date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false);
94  }
95  if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }
96  if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); }
97  if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); }
98  if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); }
99 } else {
100  // TODO We define q
101 }
102 // $date_start and $date_end are defined. We force $year_start and $nbofyear
103 $tmps = dol_getdate($date_start);
104 $year_start = $tmps['year'];
105 $tmpe = dol_getdate($date_end);
106 $year_end = $tmpe['year'];
107 $nbofyear = ($year_end - $year_start) + 1;
108 
109 $commonparams = array();
110 $commonparams['modecompta'] = $modecompta;
111 $commonparams['sortorder'] = $sortorder;
112 $commonparams['sortfield'] = $sortfield;
113 
114 $headerparams = array();
115 $headerparams['date_startyear'] = $date_startyear;
116 $headerparams['date_startmonth'] = $date_startmonth;
117 $headerparams['date_startday'] = $date_startday;
118 $headerparams['date_endyear'] = $date_endyear;
119 $headerparams['date_endmonth'] = $date_endmonth;
120 $headerparams['date_endday'] = $date_endday;
121 $headerparams['q'] = $q;
122 
123 $tableparams = array();
124 $tableparams['search_categ'] = $selected_cat;
125 $tableparams['subcat'] = ($subcat === true) ? 'yes' : '';
126 
127 // Adding common parameters
128 $allparams = array_merge($commonparams, $headerparams, $tableparams);
129 $headerparams = array_merge($commonparams, $headerparams);
130 $tableparams = array_merge($commonparams, $tableparams);
131 
132 foreach ($allparams as $key => $value) {
133  $paramslink .= '&'.$key.'='.$value;
134 }
135 
136 /*
137  * View
138  */
139 
140 llxHeader();
141 
142 $form = new Form($db);
143 
144 // TODO Report from bookkeeping not yet available, so we switch on report on business events
145 if ($modecompta == "BOOKKEEPING") $modecompta = "CREANCES-DETTES";
146 if ($modecompta == "BOOKKEEPINGCOLLECTED") $modecompta = "RECETTES-DEPENSES";
147 
148 // Show report header
149 if ($modecompta == "CREANCES-DETTES") {
150  $name = $langs->trans("Turnover").', '.$langs->trans("ByUserAuthorOfInvoice");
151  $calcmode = $langs->trans("CalcModeDebt");
152  //$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
153  $description = $langs->trans("RulesCADue");
154  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description .= $langs->trans("DepositsAreNotIncluded");
155  else $description .= $langs->trans("DepositsAreIncluded");
156  $builddate = dol_now();
157  //$exportlink=$langs->trans("NotYetAvailable");
158 } elseif ($modecompta == "RECETTES-DEPENSES")
159 {
160  $name = $langs->trans("TurnoverCollected").', '.$langs->trans("ByUserAuthorOfInvoice");
161  $calcmode = $langs->trans("CalcModeEngagement");
162  //$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
163  $description = $langs->trans("RulesCAIn");
164  $description .= $langs->trans("DepositsAreIncluded");
165  $builddate = dol_now();
166  //$exportlink=$langs->trans("NotYetAvailable");
167 } elseif ($modecompta == "BOOKKEEPING")
168 {
169  // TODO
170 } elseif ($modecompta == "BOOKKEEPINGCOLLECTED")
171 {
172  // TODO
173 }
174 $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
175 if ($date_end == dol_time_plus_duree($date_start, 1, 'y') - 1) $periodlink = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_start - 1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year_start + 1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
176 else $periodlink = '';
177 
178 $moreparam = array();
179 if (!empty($modecompta)) $moreparam['modecompta'] = $modecompta;
180 
181 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode);
182 
183 if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING')
184 {
185  print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
186 }
187 
188 
189 $name = array();
190 
191 // Show array
192 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
193 print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
194 // Extra parameters management
195 foreach ($headerparams as $key => $value)
196 {
197  print '<input type="hidden" name="'.$key.'" value="'.$value.'">';
198 }
199 
200 $catotal = 0;
201 if ($modecompta == 'CREANCES-DETTES') {
202  $sql = "SELECT u.rowid as rowid, u.lastname as name, u.firstname as firstname, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
203  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
204  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid";
205  $sql .= " WHERE f.fk_statut in (1,2)";
206  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
207  $sql .= " AND f.type IN (0,1,2,5)";
208  } else {
209  $sql .= " AND f.type IN (0,1,2,3,5)";
210  }
211  if ($date_start && $date_end) {
212  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
213  }
214 } else {
215  /*
216  * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
217  * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
218  */
219  $sql = "SELECT u.rowid as rowid, u.lastname as name, u.firstname as firstname, sum(pf.amount) as amount_ttc";
220  $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
221  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid ";
222  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid";
223  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.rowid = pf.fk_paiement";
224  $sql .= " WHERE 1=1";
225  if ($date_start && $date_end) {
226  $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
227  }
228 }
229 $sql .= " AND f.entity IN (".getEntity('invoice').")";
230 if ($socid) $sql .= " AND f.fk_soc = ".$socid;
231 $sql .= " GROUP BY u.rowid, u.lastname, u.firstname";
232 $sql .= " ORDER BY u.rowid";
233 
234 $amount = array();
235 
236 dol_syslog("cabyuser", LOG_DEBUG);
237 $result = $db->query($sql);
238 if ($result) {
239  $num = $db->num_rows($result);
240  $i = 0;
241  while ($i < $num) {
242  $obj = $db->fetch_object($result);
243  $amount_ht[$obj->rowid] = $obj->amount;
244  $amount[$obj->rowid] = $obj->amount_ttc;
245  $name[$obj->rowid] = $obj->name.' '.$obj->firstname;
246  $catotal_ht += $obj->amount;
247  $catotal += $obj->amount_ttc;
248  $i++;
249  }
250 } else {
251  dol_print_error($db);
252 }
253 
254 // Adding old-version payments, non-bound by "paiement_facture" then without User
255 if ($modecompta != 'CREANCES-DETTES') {
256  $sql = "SELECT -1 as rowidx, '' as name, '' as firstname, sum(DISTINCT p.amount) as amount_ttc";
257  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
258  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
259  $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
260  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
261  $sql .= " WHERE pf.rowid IS NULL";
262  $sql .= " AND p.fk_bank = b.rowid";
263  $sql .= " AND b.fk_account = ba.rowid";
264  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
265  if ($date_start && $date_end) {
266  $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
267  }
268  $sql .= " GROUP BY rowidx, name, firstname";
269  $sql .= " ORDER BY rowidx";
270 
271  $result = $db->query($sql);
272  if ($result) {
273  $num = $db->num_rows($result);
274  $i = 0;
275  while ($i < $num) {
276  $obj = $db->fetch_object($result);
277  $amount[$obj->rowidx] = $obj->amount_ttc;
278  $name[$obj->rowidx] = $obj->name.' '.$obj->firstname;
279  $catotal += $obj->amount_ttc;
280  $i++;
281  }
282  } else {
283  dol_print_error($db);
284  }
285 }
286 
287 $moreforfilter = '';
288 
289 print '<div class="div-table-responsive">';
290 print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
291 
292 print "<tr class=\"liste_titre\">";
294  $langs->trans("User"),
295  $_SERVER["PHP_SELF"],
296  "name",
297  "",
298  $paramslink,
299  "",
300  $sortfield,
301  $sortorder
302 );
303 if ($modecompta == 'CREANCES-DETTES') {
305  $langs->trans('AmountHT'),
306  $_SERVER["PHP_SELF"],
307  "amount_ht",
308  "",
309  $paramslink,
310  'class="right"',
311  $sortfield,
312  $sortorder
313  );
314 } else {
316 }
318  $langs->trans("AmountTTC"),
319  $_SERVER["PHP_SELF"],
320  "amount_ttc",
321  "",
322  $paramslink,
323  'class="right"',
324  $sortfield,
325  $sortorder
326 );
328  $langs->trans("Percentage"),
329  $_SERVER["PHP_SELF"], "amount_ttc",
330  "",
331  $paramslink,
332  'class="right"',
333  $sortfield,
334  $sortorder
335 );
337  $langs->trans("OtherStatistics"),
338  $_SERVER["PHP_SELF"],
339  "",
340  "",
341  "",
342  'align="center" width="20%"'
343 );
344 print "</tr>\n";
345 
346 if (count($amount)) {
347  $arrayforsort = $name;
348 
349  // We define arrayforsort
350  if ($sortfield == 'name' && $sortorder == 'asc') {
351  asort($name);
352  $arrayforsort = $name;
353  }
354  if ($sortfield == 'name' && $sortorder == 'desc') {
355  arsort($name);
356  $arrayforsort = $name;
357  }
358  if ($sortfield == 'amount_ht' && $sortorder == 'asc') {
359  asort($amount_ht);
360  $arrayforsort = $amount_ht;
361  }
362  if ($sortfield == 'amount_ht' && $sortorder == 'desc') {
363  arsort($amount_ht);
364  $arrayforsort = $amount_ht;
365  }
366  if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
367  asort($amount);
368  $arrayforsort = $amount;
369  }
370  if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
371  arsort($amount);
372  $arrayforsort = $amount;
373  }
374 
375  $i = 0;
376 
377  foreach ($arrayforsort as $key => $value) {
378  print '<tr class="oddeven">';
379 
380  // Third party
381  $fullname = $name[$key];
382  if ($key >= 0) {
383  $linkname = '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$key.'">'.img_object($langs->trans("ShowUser"), 'user').' '.$fullname.'</a>';
384  } else {
385  $linkname = $langs->trans("PaymentsNotLinkedToUser");
386  }
387  print "<td>".$linkname."</td>\n";
388 
389  // Amount w/o VAT
390  print '<td class="right">';
391  if ($modecompta == 'RECETTES-DEPENSES') {
392  if ($key > 0) {
393  //print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
394  } else {
395  //print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
396  }
397  } elseif ($modecompta == 'CREANCES-DETTES') {
398  if ($key > 0) {
399  print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
400  } else {
401  //print '<a href="#">';
402  }
403  print price($amount_ht[$key]);
404  if ($key > 0) print '</a>';
405  }
406  print '</td>';
407 
408  // Amount with VAT
409  print '<td class="right">';
410  if ($modecompta == 'RECETTES-DEPENSES') {
411  if ($key > 0) {
412  //print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid='.$key.'">';
413  } else {
414  //print '<a href="'.DOL_URL_ROOT.'/compta/paiement/list.php?userid=-1">';
415  }
416  } elseif ($modecompta == 'CREANCES-DETTES') {
417  if ($key > 0) {
418  print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?userid='.$key.'">';
419  } else {
420  //print '<a href="#">';
421  }
422  }
423  print price($amount[$key]);
424  if ($modecompta == 'RECETTES-DEPENSES') {
425  if ($key > 0) {
426  //print '</a>';
427  } else {
428  //print '</a>';
429  }
430  } elseif ($modecompta == 'CREANCES-DETTES') {
431  if ($key > 0) {
432  print '</a>';
433  }
434  }
435  print '</td>';
436 
437  // Percent
438  print '<td class="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : '&nbsp;').'</td>';
439 
440  // Other stats
441  print '<td class="center">';
442  if (!empty($conf->propal->enabled) && $key > 0) {
443  print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("ProposalStats"), "stats").'</a>&nbsp;';
444  }
445  if (!empty($conf->commande->enabled) && $key > 0) {
446  print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("OrderStats"), "stats").'</a>&nbsp;';
447  }
448  if (!empty($conf->facture->enabled) && $key > 0) {
449  print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("InvoiceStats"), "stats").'</a>&nbsp;';
450  }
451  print '</td>';
452  print "</tr>\n";
453  $i++;
454  }
455 
456  // Total
457  print '<tr class="liste_total">';
458  print '<td>'.$langs->trans("Total").'</td>';
459  if ($modecompta != 'CREANCES-DETTES') {
460  print '<td></td>';
461  } else {
462  print '<td class="right">'.price($catotal_ht).'</td>';
463  }
464  print '<td class="right">'.price($catotal).'</td>';
465  print '<td>&nbsp;</td>';
466  print '<td>&nbsp;</td>';
467  print '</tr>';
468 
469  $db->free($result);
470 }
471 
472 print "</table>";
473 print '</div>';
474 print '</form>';
475 
476 // End of page
477 llxFooter();
478 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode= 'auto')
Return date for now.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink= '', $moreparam=array(), $calcmode= '', $varlink= '')
Show header of a report.
Definition: report.lib.php:41
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:481
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...
llxHeader()
Empty header.
Definition: wrapper.php:45
Class to manage generation of HTML components Only common components must be here.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone= '')
Return an array with locale date info.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print $_SERVER["PHP_SELF"]
Edit parameters.
img_next($titlealt= 'default', $moreatt= '')
Show next logo.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
print
Draft customers invoices.
Definition: index.php:89
img_previous($titlealt= 'default', $moreatt= '')
Show previous logo.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
llxFooter()
Empty footer.
Definition: wrapper.php:59
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:114
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin= '1', $morecss= '', $textfordropdown= '')
Show information for admin users or standard users.