PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/stats/cabyuser.php

https://github.com/asterix14/dolibarr
PHP | 308 lines | 227 code | 34 blank | 47 comment | 71 complexity | 7070405a6367aa12095042d92e5848f0 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.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 2 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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/stats/cabyuser.php
  21. * \brief Page reporting Salesover by user
  22. */
  23. require('../../main.inc.php');
  24. require_once(DOL_DOCUMENT_ROOT."/core/lib/report.lib.php");
  25. require_once(DOL_DOCUMENT_ROOT."/core/lib/tax.lib.php");
  26. require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
  27. // Security check
  28. $socid = isset($_REQUEST["socid"])?$_REQUEST["socid"]:'';
  29. if ($user->societe_id > 0) $socid = $user->societe_id;
  30. if (!$user->rights->compta->resultat->lire && !$user->rights->accounting->comptarapport->lire)
  31. accessforbidden();
  32. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  33. $modecompta = $conf->global->COMPTA_MODE;
  34. if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
  35. $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
  36. $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
  37. if (! $sortorder) $sortorder="asc";
  38. if (! $sortfield) $sortfield="name";
  39. // Date range
  40. $year=GETPOST("year");
  41. $month=GETPOST("month");
  42. if (empty($year))
  43. {
  44. $year_current = strftime("%Y",dol_now());
  45. $month_current = strftime("%m",dol_now());
  46. $year_start = $year_current;
  47. } else {
  48. $year_current = $year;
  49. $month_current = strftime("%m",dol_now());
  50. $year_start = $year;
  51. }
  52. $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
  53. $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
  54. // Quarter
  55. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  56. {
  57. $q=GETPOST("q")?GETPOST("q"):0;
  58. if ($q==0)
  59. {
  60. // We define date_start and date_end
  61. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  62. $year_end=$year_start;
  63. $month_end=$month_start;
  64. if (! GETPOST("month")) // If month not forced
  65. {
  66. if (! GETPOST('year') && $month_start > $month_current)
  67. {
  68. $year_start--;
  69. $year_end--;
  70. }
  71. $month_end=$month_start-1;
  72. if ($month_end < 1) $month_end=12;
  73. else $year_end++;
  74. }
  75. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  76. }
  77. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  78. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  79. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  80. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  81. }
  82. else
  83. {
  84. // TODO We define q
  85. }
  86. /*
  87. * View
  88. */
  89. llxHeader();
  90. $form=new Form($db);
  91. // Affiche en-tete du rapport
  92. if ($modecompta=="CREANCES-DETTES")
  93. {
  94. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice");
  95. $nom.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  96. $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
  97. //$periodlink="<a href='".$_SERVER["PHP_SELF"]."?year=".($year-1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year+1)."&modecompta=".$modecompta."'>".img_next()."</a>";
  98. $description=$langs->trans("RulesCADue");
  99. $builddate=time();
  100. //$exportlink=$langs->trans("NotYetAvailable");
  101. }
  102. else {
  103. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByUserAuthorOfInvoice");
  104. $nom.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=CREANCES-DETTES">','</a>').')';
  105. $period=$form->select_date($date_start,'date_start',0,0,0,'',1,0,1).' - '.$form->select_date($date_end,'date_end',0,0,0,'',1,0,1);
  106. //$periodlink="<a href='".$_SERVER["PHP_SELF"]."?year=".($year-1)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year+1)."&modecompta=".$modecompta."'>".img_next()."</a>";
  107. $description=$langs->trans("RulesCAIn");
  108. $builddate=time();
  109. //$exportlink=$langs->trans("NotYetAvailable");
  110. }
  111. $moreparam=array();
  112. if (! empty($modecompta)) $moreparam['modecompta']=$modecompta;
  113. report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,$moreparam);
  114. // Charge tableau
  115. $catotal=0;
  116. if ($modecompta == 'CREANCES-DETTES')
  117. {
  118. $sql = "SELECT u.rowid as rowid, u.name as name, u.firstname as firstname, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
  119. $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
  120. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid";
  121. $sql.= " WHERE f.fk_statut in (1,2) ";
  122. $sql.= " AND (";
  123. $sql.= " f.type = 0"; // Standard
  124. $sql.= " OR f.type = 1"; // Replacement
  125. $sql.= " OR f.type = 2"; // Credit note
  126. $sql.= " OR f.type = 3"; // Deposit
  127. $sql.= ")";
  128. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  129. }
  130. else
  131. {
  132. /*
  133. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  134. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  135. */
  136. $sql = "SELECT u.rowid as rowid, u.name as name, u.firstname as firstname, sum(pf.amount) as amount_ttc";
  137. $sql.= " FROM ".MAIN_DB_PREFIX."user as u" ;
  138. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.fk_user_author = u.rowid ";
  139. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid";
  140. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.rowid = pf.fk_paiement";
  141. $sql.= " WHERE 1=1";
  142. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  143. }
  144. $sql.= " AND f.entity = ".$conf->entity;
  145. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  146. $sql .= " GROUP BY u.rowid, u.name, u.firstname";
  147. $sql .= " ORDER BY u.rowid";
  148. $result = $db->query($sql);
  149. if ($result)
  150. {
  151. $num = $db->num_rows($result);
  152. $i=0;
  153. while ($i < $num)
  154. {
  155. $obj = $db->fetch_object($result);
  156. $amount[$obj->rowid] = $obj->amount_ttc;
  157. $name[$obj->rowid] = $obj->name.' '.$obj->firstname;
  158. $catotal+=$obj->amount_ttc;
  159. $i++;
  160. }
  161. }
  162. else {
  163. dol_print_error($db);
  164. }
  165. // On ajoute les paiements ancienne version, non lies par paiement_facture donc sans user
  166. if ($modecompta != 'CREANCES-DETTES')
  167. {
  168. $sql = "SELECT -1 as rowidx, '' as name, '' as firstname, sum(p.amount) as amount_ttc";
  169. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  170. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  171. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  172. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  173. $sql.= " WHERE pf.rowid IS NULL";
  174. $sql.= " AND p.fk_bank = b.rowid";
  175. $sql.= " AND b.fk_account = ba.rowid";
  176. $sql.= " AND ba.entity = ".$conf->entity;
  177. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  178. $sql.= " GROUP BY rowidx, name, firstname";
  179. $sql.= " ORDER BY rowidx";
  180. $result = $db->query($sql);
  181. if ($result)
  182. {
  183. $num = $db->num_rows($result);
  184. $i=0;
  185. while ($i < $num)
  186. {
  187. $obj = $db->fetch_object($result);
  188. $amount[$obj->rowidx] = $obj->amount_ttc;
  189. $name[$obj->rowidx] = $obj->name.' '.$obj->firstname;
  190. $catotal+=$obj->amount_ttc;
  191. $i++;
  192. }
  193. }
  194. else {
  195. dol_print_error($db);
  196. }
  197. }
  198. $i = 0;
  199. print "<table class=\"noborder\" width=\"100%\">";
  200. print "<tr class=\"liste_titre\">";
  201. print_liste_field_titre($langs->trans("User"),$_SERVER["PHP_SELF"],"name","",'&amp;year='.($year).'&modecompta='.$modecompta,"",$sortfield,$sortorder);
  202. print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"amount_ttc","",'&amp;year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder);
  203. print_liste_field_titre($langs->trans("Percentage"),$_SERVER["PHP_SELF"],"amount_ttc","",'&amp;year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder);
  204. print_liste_field_titre($langs->trans("OtherStatistics"),$_SERVER["PHP_SELF"],"","","",'align="center" width="20%"');
  205. print "</tr>\n";
  206. $var=true;
  207. if (count($amount))
  208. {
  209. $arrayforsort=$name;
  210. // We define arrayforsort
  211. if ($sortfield == 'name' && $sortorder == 'asc') {
  212. asort($name);
  213. $arrayforsort=$name;
  214. }
  215. if ($sortfield == 'name' && $sortorder == 'desc') {
  216. arsort($name);
  217. $arrayforsort=$name;
  218. }
  219. if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
  220. asort($amount);
  221. $arrayforsort=$amount;
  222. }
  223. if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
  224. arsort($amount);
  225. $arrayforsort=$amount;
  226. }
  227. foreach($arrayforsort as $key => $value)
  228. {
  229. $var=!$var;
  230. print "<tr ".$bc[$var].">";
  231. // Third party
  232. $fullname=$name[$key];
  233. if ($key >= 0) {
  234. $linkname='<a href="'.DOL_URL_ROOT.'/user/fiche.php?id='.$key.'">'.img_object($langs->trans("ShowUser"),'user').' '.$fullname.'</a>';
  235. }
  236. else {
  237. $linkname=$langs->trans("PaymentsNotLinkedToUser");
  238. }
  239. print "<td>".$linkname."</td>\n";
  240. // Amount
  241. print '<td align="right">';
  242. if ($modecompta != 'CREANCES-DETTES')
  243. {
  244. if ($key > 0) print '<a href="'.DOL_URL_ROOT.'/compta/paiement/liste.php?userid='.$key.'">';
  245. else print '<a href="'.DOL_URL_ROOT.'/compta/paiement/liste.php?userid=-1">';
  246. }
  247. else
  248. {
  249. if ($key > 0) print '<a href="'.DOL_URL_ROOT.'/compta/facture.php?userid='.$key.'">';
  250. else print '<a href="#">';
  251. }
  252. print price($amount[$key]);
  253. print '</td>';
  254. // Percent
  255. print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal,2).'%' : '&nbsp;').'</td>';
  256. // Other stats
  257. print '<td align="center">';
  258. if ($conf->propal->enabled && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
  259. if ($conf->commande->enabled && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
  260. if ($conf->facture->enabled && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?userid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
  261. print '</td>';
  262. print "</tr>\n";
  263. $i++;
  264. }
  265. // Total
  266. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">'.price($catotal).'</td><td>&nbsp;</td>';
  267. print '<td>&nbsp;</td>';
  268. print '</tr>';
  269. $db->free($result);
  270. }
  271. print "</table>";
  272. $db->close();
  273. llxFooter();
  274. ?>