PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/stats/casoc.php

https://github.com/asterix14/dolibarr
PHP | 315 lines | 233 code | 34 blank | 48 comment | 71 complexity | f30e5fd3537cc7581d6d553fdc36960a 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. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/stats/casoc.php
  22. * \brief Page reporting CA par societe
  23. */
  24. require('../../main.inc.php');
  25. require_once(DOL_DOCUMENT_ROOT."/core/lib/report.lib.php");
  26. require_once(DOL_DOCUMENT_ROOT."/core/lib/tax.lib.php");
  27. require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
  28. $langs->load("companies");
  29. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES')
  30. $modecompta = $conf->global->COMPTA_MODE;
  31. if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta");
  32. $sortorder=isset($_GET["sortorder"])?$_GET["sortorder"]:$_POST["sortorder"];
  33. $sortfield=isset($_GET["sortfield"])?$_GET["sortfield"]:$_POST["sortfield"];
  34. if (! $sortorder) $sortorder="asc";
  35. if (! $sortfield) $sortfield="nom";
  36. // Security check
  37. $socid = isset($_REQUEST["socid"])?$_REQUEST["socid"]:'';
  38. if ($user->societe_id > 0) $socid = $user->societe_id;
  39. if (!$user->rights->compta->resultat->lire && !$user->rights->accounting->comptarapport->lire)
  40. accessforbidden();
  41. // Date range
  42. $year=GETPOST("year");
  43. $month=GETPOST("month");
  44. if (empty($year))
  45. {
  46. $year_current = strftime("%Y",dol_now());
  47. $month_current = strftime("%m",dol_now());
  48. $year_start = $year_current;
  49. } else {
  50. $year_current = $year;
  51. $month_current = strftime("%m",dol_now());
  52. $year_start = $year;
  53. }
  54. $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]);
  55. $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]);
  56. // Quarter
  57. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  58. {
  59. $q=GETPOST("q")?GETPOST("q"):0;
  60. if ($q==0)
  61. {
  62. // We define date_start and date_end
  63. $month_start=GETPOST("month")?GETPOST("month"):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  64. $year_end=$year_start;
  65. $month_end=$month_start;
  66. if (! GETPOST("month")) // If month not forced
  67. {
  68. if (! GETPOST('year') && $month_start > $month_current)
  69. {
  70. $year_start--;
  71. $year_end--;
  72. }
  73. $month_end=$month_start-1;
  74. if ($month_end < 1) $month_end=12;
  75. else $year_end++;
  76. }
  77. $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false);
  78. }
  79. if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); }
  80. if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); }
  81. if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); }
  82. if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); }
  83. }
  84. else
  85. {
  86. // TODO We define q
  87. }
  88. /*
  89. * View
  90. */
  91. llxHeader();
  92. $form=new Form($db);
  93. // Affiche en-tete de rapport
  94. if ($modecompta=="CREANCES-DETTES")
  95. {
  96. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByThirdParties");
  97. $nom.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  98. $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);
  99. //$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>';
  100. $description=$langs->trans("RulesCADue");
  101. $builddate=time();
  102. //$exportlink=$langs->trans("NotYetAvailable");
  103. }
  104. else {
  105. $nom=$langs->trans("SalesTurnover").', '.$langs->trans("ByThirdParties");
  106. $nom.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=CREANCES-DETTES">','</a>').')';
  107. $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);
  108. //$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>';
  109. $description=$langs->trans("RulesCAIn");
  110. $builddate=time();
  111. //$exportlink=$langs->trans("NotYetAvailable");
  112. }
  113. $moreparam=array();
  114. if (! empty($modecompta)) $moreparam['modecompta']=$modecompta;
  115. report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink,$moreparam);
  116. // Charge tableau
  117. $catotal=0;
  118. if ($modecompta == 'CREANCES-DETTES')
  119. {
  120. $sql = "SELECT s.rowid as socid, s.nom as name, sum(f.total) as amount, sum(f.total_ttc) as amount_ttc";
  121. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  122. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  123. $sql.= " WHERE f.fk_statut in (1,2)";
  124. $sql.= " AND (";
  125. $sql.= " f.type = 0"; // Standard
  126. $sql.= " OR f.type = 1"; // Replacement
  127. $sql.= " OR f.type = 2"; // Credit note
  128. $sql.= " OR f.type = 3"; // Deposit
  129. $sql.= ")";
  130. $sql.= " AND f.fk_soc = s.rowid";
  131. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  132. }
  133. else
  134. {
  135. /*
  136. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  137. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  138. */
  139. $sql = "SELECT s.rowid as socid, s.nom as name, sum(pf.amount) as amount_ttc";
  140. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  141. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  142. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  143. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  144. $sql .= " WHERE p.rowid = pf.fk_paiement";
  145. $sql.= " AND pf.fk_facture = f.rowid";
  146. $sql.= " AND f.fk_soc = s.rowid";
  147. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  148. }
  149. $sql.= " AND f.entity = ".$conf->entity;
  150. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  151. $sql.= " GROUP BY s.rowid, s.nom";
  152. $sql.= " ORDER BY s.rowid";
  153. $result = $db->query($sql);
  154. if ($result)
  155. {
  156. $num = $db->num_rows($result);
  157. $i=0;
  158. while ($i < $num)
  159. {
  160. $obj = $db->fetch_object($result);
  161. $amount[$obj->socid] += $obj->amount_ttc;
  162. $name[$obj->socid] = $obj->name;
  163. $catotal+=$obj->amount_ttc;
  164. $i++;
  165. }
  166. }
  167. else {
  168. dol_print_error($db);
  169. }
  170. // On ajoute les paiements anciennes version, non lies par paiement_facture
  171. if ($modecompta != 'CREANCES-DETTES')
  172. {
  173. $sql = "SELECT 'Autres' as nom, '0' as idp, sum(p.amount) as amount_ttc";
  174. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  175. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  176. $sql.= ", ".MAIN_DB_PREFIX."paiement as p";
  177. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  178. $sql.= " WHERE pf.rowid IS NULL";
  179. $sql.= " AND p.fk_bank = b.rowid";
  180. $sql.= " AND b.fk_account = ba.rowid";
  181. $sql.= " AND ba.entity = ".$conf->entity;
  182. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  183. $sql.= " GROUP BY nom, idp";
  184. $sql.= " ORDER BY nom";
  185. $result = $db->query($sql);
  186. if ($result)
  187. {
  188. $num = $db->num_rows($result);
  189. $i=0;
  190. while ($i < $num)
  191. {
  192. $obj = $db->fetch_object($result);
  193. $amount[$obj->rowid] += $obj->amount_ttc;
  194. $name[$obj->rowid] = $obj->name;
  195. $catotal+=$obj->amount_ttc;
  196. $i++;
  197. }
  198. }
  199. else {
  200. dol_print_error($db);
  201. }
  202. }
  203. $i = 0;
  204. print "<table class=\"noborder\" width=\"100%\">";
  205. print "<tr class=\"liste_titre\">";
  206. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"nom","",'&amp;year='.($year).'&modecompta='.$modecompta,"",$sortfield,$sortorder);
  207. print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"amount_ttc","",'&amp;year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder);
  208. print_liste_field_titre($langs->trans("Percentage"),$_SERVER["PHP_SELF"],"amount_ttc","",'&amp;year='.($year).'&modecompta='.$modecompta,'align="right"',$sortfield,$sortorder);
  209. print_liste_field_titre($langs->trans("OtherStatistics"),$_SERVER["PHP_SELF"],"","","",'align="center" width="20%"');
  210. print "</tr>\n";
  211. $var=true;
  212. if (count($amount))
  213. {
  214. $arrayforsort=$name;
  215. // On definit tableau arrayforsort
  216. if ($sortfield == 'nom' && $sortorder == 'asc') {
  217. asort($name);
  218. $arrayforsort=$name;
  219. }
  220. if ($sortfield == 'nom' && $sortorder == 'desc') {
  221. arsort($name);
  222. $arrayforsort=$name;
  223. }
  224. if ($sortfield == 'amount_ttc' && $sortorder == 'asc') {
  225. asort($amount);
  226. $arrayforsort=$amount;
  227. }
  228. if ($sortfield == 'amount_ttc' && $sortorder == 'desc') {
  229. arsort($amount);
  230. $arrayforsort=$amount;
  231. }
  232. foreach($arrayforsort as $key=>$value)
  233. {
  234. $var=!$var;
  235. print "<tr ".$bc[$var].">";
  236. // Third party
  237. $fullname=$name[$key];
  238. if ($key > 0) {
  239. $linkname='<a href="'.DOL_URL_ROOT.'/societe/soc.php?socid='.$key.'">'.img_object($langs->trans("ShowCompany"),'company').' '.$fullname.'</a>';
  240. }
  241. else {
  242. $linkname=$langs->trans("PaymentsNotLinkedToInvoice");
  243. }
  244. print "<td>".$linkname."</td>\n";
  245. // Amount
  246. print '<td align="right">';
  247. if ($modecompta != 'CREANCES-DETTES')
  248. {
  249. if ($key > 0) print '<a href="'.DOL_URL_ROOT.'/compta/paiement/liste.php?socid='.$key.'">';
  250. else print '<a href="'.DOL_URL_ROOT.'/compta/paiement/liste.php?orphelins=1">';
  251. }
  252. else
  253. {
  254. if ($key > 0) print '<a href="'.DOL_URL_ROOT.'/compta/facture.php?socid='.$key.'">';
  255. else print '<a href="#">';
  256. }
  257. print price($amount[$key]);
  258. print '</a>';
  259. print '</td>';
  260. // Percent;
  261. print '<td align="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : '&nbsp;').'</td>';
  262. // Other stats
  263. print '<td align="center">';
  264. if ($conf->propal->enabled && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/comm/propal/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("ProposalStats"),"stats").'</a>&nbsp;';
  265. if ($conf->commande->enabled && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/commande/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("OrderStats"),"stats").'</a>&nbsp;';
  266. if ($conf->facture->enabled && $key>0) print '&nbsp;<a href="'.DOL_URL_ROOT.'/compta/facture/stats/index.php?socid='.$key.'">'.img_picto($langs->trans("InvoiceStats"),"stats").'</a>&nbsp;';
  267. print '</td>';
  268. print "</tr>\n";
  269. $i++;
  270. }
  271. // Total
  272. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">'.price($catotal).'</td><td>&nbsp;</td>';
  273. print '<td>&nbsp;</td>';
  274. print '</tr>';
  275. $db->free($result);
  276. }
  277. print "</table>";
  278. print '<br>';
  279. $db->close();
  280. llxFooter();
  281. ?>