index.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2017 Olivier Geffroy <jeff@jeffinfo.com>
  6. * Copyright (C) 2018-2020 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. /**
  22. * \file htdocs/compta/stats/index.php
  23. * \brief Page reporting CA
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('compta', 'bills', 'donation', 'salaries'));
  31. $date_startday = GETPOST('date_startday', 'int');
  32. $date_startmonth = GETPOST('date_startmonth', 'int');
  33. $date_startyear = GETPOST('date_startyear', 'int');
  34. $date_endday = GETPOST('date_endday', 'int');
  35. $date_endmonth = GETPOST('date_endmonth', 'int');
  36. $date_endyear = GETPOST('date_endyear', 'int');
  37. $nbofyear = 4;
  38. // Date range
  39. $year = GETPOST('year', 'int');
  40. if (empty($year)) {
  41. $year_current = dol_print_date(dol_now(), "%Y");
  42. $month_current = dol_print_date(dol_now(), "%m");
  43. $year_start = $year_current - ($nbofyear - 1);
  44. } else {
  45. $year_current = $year;
  46. $month_current = dol_print_date(dol_now(), "%m");
  47. $year_start = $year - ($nbofyear - 1);
  48. }
  49. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear, 'tzserver');
  50. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear, 'tzserver');
  51. // We define date_start and date_end
  52. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  53. $q = GETPOST("q") ? GETPOST("q") : 0;
  54. if ($q == 0) {
  55. // We define date_start and date_end
  56. $year_end = $year_start + ($nbofyear - 1);
  57. $month_start = GETPOSTISSET("month") ? GETPOST("month", 'int') : ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1);
  58. if (!GETPOST('month')) {
  59. if (!GETPOST("year") && $month_start > $month_current) {
  60. $year_start--;
  61. $year_end--;
  62. }
  63. $month_end = $month_start - 1;
  64. if ($month_end < 1) {
  65. $month_end = 12;
  66. } else {
  67. $year_end++;
  68. }
  69. } else {
  70. $month_end = $month_start;
  71. }
  72. $date_start = dol_get_first_day($year_start, $month_start, false);
  73. $date_end = dol_get_last_day($year_end, $month_end, false);
  74. }
  75. if ($q == 1) {
  76. $date_start = dol_get_first_day($year_start, 1, false);
  77. $date_end = dol_get_last_day($year_start, 3, false);
  78. }
  79. if ($q == 2) {
  80. $date_start = dol_get_first_day($year_start, 4, false);
  81. $date_end = dol_get_last_day($year_start, 6, false);
  82. }
  83. if ($q == 3) {
  84. $date_start = dol_get_first_day($year_start, 7, false);
  85. $date_end = dol_get_last_day($year_start, 9, false);
  86. }
  87. if ($q == 4) {
  88. $date_start = dol_get_first_day($year_start, 10, false);
  89. $date_end = dol_get_last_day($year_start, 12, false);
  90. }
  91. }
  92. $userid = GETPOST('userid', 'int');
  93. $socid = GETPOST('socid', 'int');
  94. $tmps = dol_getdate($date_start);
  95. $mothn_start = $tmps['mon'];
  96. $year_start = $tmps['year'];
  97. $tmpe = dol_getdate($date_end);
  98. $month_end = $tmpe['mon'];
  99. $year_end = $tmpe['year'];
  100. $nbofyear = ($year_end - $year_start) + 1;
  101. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING')
  102. $modecompta = $conf->global->ACCOUNTING_MODE;
  103. if (isModEnabled('accounting')) {
  104. $modecompta = 'BOOKKEEPING';
  105. }
  106. if (GETPOST("modecompta")) {
  107. $modecompta = GETPOST("modecompta", 'alpha');
  108. }
  109. // Security check
  110. if ($user->socid > 0) {
  111. $socid = $user->socid;
  112. }
  113. if (isModEnabled('comptabilite')) {
  114. $result = restrictedArea($user, 'compta', '', '', 'resultat');
  115. }
  116. if (isModEnabled('accounting')) {
  117. $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
  118. }
  119. /*
  120. * View
  121. */
  122. $param = '';
  123. if ($date_startday && $date_startmonth && $date_startyear) {
  124. $param .= '&date_startday='.$date_startday.'&date_startmonth='.$date_startmonth.'&date_startyear='.$date_startyear;
  125. }
  126. if ($date_endday && $date_endmonth && $date_endyear) {
  127. $param .= '&date_endday='.$date_endday.'&date_endmonth='.$date_endmonth.'&date_endyear='.$date_endyear;
  128. }
  129. llxHeader();
  130. $form = new Form($db);
  131. $exportlink="";
  132. $namelink="";
  133. // Affiche en-tete du rapport
  134. if ($modecompta == "CREANCES-DETTES") {
  135. $name = $langs->trans("Turnover");
  136. $calcmode = $langs->trans("CalcModeDebt");
  137. //$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  138. if (isModEnabled('accounting')) {
  139. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '{link1}', '{link2}').')';
  140. $calcmode = str_replace('{link1}', '<a class="bold" href="'.$_SERVER["PHP_SELF"].'?'.($param ? $param : 'year_start='.$year_start).'&modecompta=BOOKKEEPING">', $calcmode);
  141. $calcmode = str_replace('{link2}', '</a>', $calcmode);
  142. }
  143. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  144. $description = $langs->trans("RulesCADue");
  145. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  146. $description .= $langs->trans("DepositsAreNotIncluded");
  147. } else {
  148. $description .= $langs->trans("DepositsAreIncluded");
  149. }
  150. $builddate = dol_now();
  151. //$exportlink=$langs->trans("NotYetAvailable");
  152. } elseif ($modecompta == "RECETTES-DEPENSES") {
  153. $name = $langs->trans("TurnoverCollected");
  154. $calcmode = $langs->trans("CalcModeEngagement");
  155. //$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
  156. //if (isModEnabled('accounting')) {
  157. //$calcmode.='<br>('.$langs->trans("SeeReportInBookkeepingMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=BOOKKEEPINGCOLLECTED">','</a>').')';
  158. //}
  159. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  160. $description = $langs->trans("RulesCAIn");
  161. $description .= $langs->trans("DepositsAreIncluded");
  162. $builddate = dol_now();
  163. //$exportlink=$langs->trans("NotYetAvailable");
  164. } elseif ($modecompta == "BOOKKEEPING") {
  165. $name = $langs->trans("Turnover");
  166. $calcmode = $langs->trans("CalcModeBookkeeping");
  167. $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '{link1}', '{link2}').')';
  168. $calcmode = str_replace('{link1}', '<a class="bold" href="'.$_SERVER["PHP_SELF"].'?'.($param ? $param : 'year_start='.$year_start).'&modecompta=CREANCES-DETTES">', $calcmode);
  169. $calcmode = str_replace('{link2}', '</a>', $calcmode);
  170. //$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  171. $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
  172. $description = $langs->trans("RulesSalesTurnoverOfIncomeAccounts");
  173. $builddate = dol_now();
  174. //$exportlink=$langs->trans("NotYetAvailable");
  175. }
  176. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzserver');
  177. $period .= ' - ';
  178. $period .= $form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzserver');
  179. $moreparam = array();
  180. if (!empty($modecompta)) {
  181. $moreparam['modecompta'] = $modecompta;
  182. }
  183. report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode);
  184. if (isModEnabled('accounting') && $modecompta != 'BOOKKEEPING') {
  185. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  186. }
  187. if ($modecompta == 'CREANCES-DETTES') {
  188. $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc";
  189. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  190. $sql .= " WHERE f.fk_statut in (1,2)";
  191. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  192. $sql .= " AND f.type IN (0,1,2,5)";
  193. } else {
  194. $sql .= " AND f.type IN (0,1,2,3,5)";
  195. }
  196. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  197. if ($socid) {
  198. $sql .= " AND f.fk_soc = ".((int) $socid);
  199. }
  200. } elseif ($modecompta == "RECETTES-DEPENSES") {
  201. /*
  202. * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
  203. * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
  204. */
  205. $sql = "SELECT date_format(p.datep, '%Y-%m') as dm, sum(pf.amount) as amount_ttc";
  206. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  207. $sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
  208. $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
  209. $sql .= " WHERE p.rowid = pf.fk_paiement";
  210. $sql .= " AND pf.fk_facture = f.rowid";
  211. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  212. if ($socid) {
  213. $sql .= " AND f.fk_soc = ".((int) $socid);
  214. }
  215. } elseif ($modecompta == "BOOKKEEPING") {
  216. $pcgverid = $conf->global->CHARTOFACCOUNTS;
  217. $pcgvercode = dol_getIdFromCode($db, $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
  218. if (empty($pcgvercode)) {
  219. $pcgvercode = $pcgverid;
  220. }
  221. $sql = "SELECT date_format(b.doc_date, '%Y-%m') as dm, sum(b.credit - b.debit) as amount_ttc";
  222. $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b,";
  223. $sql .= " ".MAIN_DB_PREFIX."accounting_account as aa";
  224. $sql .= " WHERE b.entity = ".$conf->entity; // In module double party accounting, we never share entities
  225. $sql .= " AND b.numero_compte = aa.account_number";
  226. $sql .= " AND b.doc_type = 'customer_invoice'";
  227. $sql .= " AND aa.entity = ".$conf->entity;
  228. $sql .= " AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'";
  229. $sql .= " AND aa.pcg_type = 'INCOME'"; // TODO Be able to use a custom group
  230. }
  231. $sql .= " GROUP BY dm";
  232. $sql .= " ORDER BY dm";
  233. // TODO Add a filter on $date_start and $date_end to reduce quantity on data
  234. //print $sql;
  235. $minyearmonth = $maxyearmonth = 0;
  236. $cum = array();
  237. $cum_ht = array();
  238. $total_ht = array();
  239. $total = array();
  240. $result = $db->query($sql);
  241. if ($result) {
  242. $num = $db->num_rows($result);
  243. $i = 0;
  244. while ($i < $num) {
  245. $obj = $db->fetch_object($result);
  246. $cum_ht[$obj->dm] = empty($obj->amount) ? 0 : $obj->amount;
  247. $cum[$obj->dm] = empty($obj->amount_ttc) ? 0 : $obj->amount_ttc;
  248. if ($obj->amount_ttc) {
  249. $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm);
  250. $maxyearmonth = max($maxyearmonth, $obj->dm);
  251. }
  252. $i++;
  253. }
  254. $db->free($result);
  255. } else {
  256. dol_print_error($db);
  257. }
  258. // On ajoute les paiements anciennes version, non lies par paiement_facture (very old versions)
  259. if ($modecompta == 'RECETTES-DEPENSES') {
  260. $sql = "SELECT date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ttc";
  261. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  262. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  263. $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
  264. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
  265. $sql .= " WHERE pf.rowid IS NULL";
  266. $sql .= " AND p.fk_bank = b.rowid";
  267. $sql .= " AND b.fk_account = ba.rowid";
  268. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  269. $sql .= " GROUP BY dm";
  270. $sql .= " ORDER BY dm";
  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. if (empty($cum[$obj->dm])) {
  278. $cum[$obj->dm] = $obj->amount_ttc;
  279. } else {
  280. $cum[$obj->dm] += $obj->amount_ttc;
  281. }
  282. if ($obj->amount_ttc) {
  283. $minyearmonth = ($minyearmonth ?min($minyearmonth, $obj->dm) : $obj->dm);
  284. $maxyearmonth = max($maxyearmonth, $obj->dm);
  285. }
  286. $i++;
  287. }
  288. } else {
  289. dol_print_error($db);
  290. }
  291. }
  292. $moreforfilter = '';
  293. print '<div class="div-table-responsive">';
  294. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  295. print '<tr class="liste_titre"><td>&nbsp;</td>';
  296. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  297. if ($modecompta == 'CREANCES-DETTES') {
  298. print '<td align="center" width="10%" colspan="3">';
  299. } else {
  300. print '<td align="center" width="10%" colspan="2" class="borderrightlight">';
  301. }
  302. if ($modecompta != 'BOOKKEEPING') {
  303. print '<a href="casoc.php?year='.$annee.'">';
  304. }
  305. print $annee;
  306. if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) {
  307. print '-'.($annee + 1);
  308. }
  309. if ($modecompta != 'BOOKKEEPING') {
  310. print '</a>';
  311. }
  312. print '</td>';
  313. if ($annee != $year_end) {
  314. print '<td width="15">&nbsp;</td>';
  315. }
  316. }
  317. print '</tr>';
  318. print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
  319. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  320. if ($modecompta == 'CREANCES-DETTES') {
  321. print '<td class="liste_titre right">'.$langs->trans("AmountHT").'</td>';
  322. }
  323. print '<td class="liste_titre right">';
  324. if ($modecompta == "BOOKKEEPING") {
  325. print $langs->trans("Amount");
  326. } else {
  327. print $langs->trans("AmountTTC");
  328. }
  329. print '</td>';
  330. print '<td class="liste_titre right borderrightlight">'.$langs->trans("Delta").'</td>';
  331. if ($annee != $year_end) {
  332. print '<td class="liste_titre" width="15">&nbsp;</td>';
  333. }
  334. }
  335. print '</tr>';
  336. $now_show_delta = 0;
  337. $minyear = substr($minyearmonth, 0, 4);
  338. $maxyear = substr($maxyearmonth, 0, 4);
  339. $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  340. $nowyearmonth = strftime("%Y-%m", dol_now());
  341. $maxyearmonth = max($maxyearmonth, $nowyearmonth);
  342. $now = dol_now();
  343. $casenow = dol_print_date($now, "%Y-%m");
  344. // Loop on each month
  345. $nb_mois_decalage = GETPOSTISSET('date_startmonth') ? (GETPOST('date_startmonth', 'int') - 1) : (empty($conf->global->SOCIETE_FISCAL_MONTH_START) ? 0 : ($conf->global->SOCIETE_FISCAL_MONTH_START - 1));
  346. for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) {
  347. $mois_modulo = $mois; // ajout
  348. if ($mois > 12) {
  349. $mois_modulo = $mois - 12;
  350. } // ajout
  351. if ($year_start == $year_end) {
  352. // If we show only one year or one month, we do not show month before the selected month
  353. if ($mois < $date_startmonth && $year_start <= $date_startyear) {
  354. continue;
  355. }
  356. // If we show only one year or one month, we do not show month after the selected month
  357. if ($mois > $date_endmonth && $year_end >= $date_endyear) {
  358. break;
  359. }
  360. }
  361. print '<tr class="oddeven">';
  362. // Month
  363. print "<td>".dol_print_date(dol_mktime(12, 0, 0, $mois_modulo, 1, 2000), "%B")."</td>";
  364. for ($annee = $year_start - 1; $annee <= $year_end; $annee++) { // We start one year before to have data to be able to make delta
  365. $annee_decalage = $annee;
  366. if ($mois > 12) {
  367. $annee_decalage = $annee + 1;
  368. }
  369. $case = dol_print_date(dol_mktime(1, 1, 1, $mois_modulo, 1, $annee_decalage), "%Y-%m");
  370. $caseprev = dol_print_date(dol_mktime(1, 1, 1, $mois_modulo, 1, $annee_decalage - 1), "%Y-%m");
  371. if ($annee >= $year_start) { // We ignore $annee < $year_start, we loop on it to be able to make delta, nothing is output.
  372. if ($modecompta == 'CREANCES-DETTES') {
  373. // Value turnover of month w/o VAT
  374. print '<td class="right">';
  375. if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
  376. if ($cum_ht[$case]) {
  377. $now_show_delta = 1; // On a trouve le premier mois de la premiere annee generant du chiffre.
  378. print '<a href="casoc.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price($cum_ht[$case], 1).'</a>';
  379. } else {
  380. if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
  381. print '0';
  382. } else {
  383. print '&nbsp;';
  384. }
  385. }
  386. }
  387. print "</td>";
  388. }
  389. // Value turnover of month
  390. print '<td class="right">';
  391. if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
  392. if (!empty($cum[$case])) {
  393. $now_show_delta = 1; // On a trouve le premier mois de la premiere annee generant du chiffre.
  394. if ($modecompta != 'BOOKKEEPING') {
  395. print '<a href="casoc.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">';
  396. }
  397. print price($cum[$case], 1);
  398. if ($modecompta != 'BOOKKEEPING') {
  399. print '</a>';
  400. }
  401. } else {
  402. if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
  403. print '0';
  404. } else {
  405. print '&nbsp;';
  406. }
  407. }
  408. }
  409. print "</td>";
  410. // Percentage of month
  411. print '<td class="borderrightlight right"><span class="opacitymedium">';
  412. //var_dump($annee.' '.$year_end.' '.$mois.' '.$month_end);
  413. if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
  414. if ($annee_decalage > $minyear && $case <= $casenow) {
  415. if (!empty($cum[$caseprev]) && !empty($cum[$case])) {
  416. $percent = (round(($cum[$case] - $cum[$caseprev]) / $cum[$caseprev], 4) * 100);
  417. //print "X $cum[$case] - $cum[$caseprev] - $cum[$caseprev] - $percent X";
  418. print ($percent >= 0 ? "+$percent" : "$percent").'%';
  419. }
  420. if (!empty($cum[$caseprev]) && empty($cum[$case])) {
  421. print '-100%';
  422. }
  423. if (empty($cum[$caseprev]) && !empty($cum[$case])) {
  424. //print '<td class="right">+Inf%</td>';
  425. print '-';
  426. }
  427. if (isset($cum[$caseprev]) && empty($cum[$caseprev]) && empty($cum[$case])) {
  428. print '+0%';
  429. }
  430. if (!isset($cum[$caseprev]) && empty($cum[$case])) {
  431. print '-';
  432. }
  433. } else {
  434. if ($minyearmonth <= $case && $case <= $maxyearmonth) {
  435. print '-';
  436. } else {
  437. print '&nbsp;';
  438. }
  439. }
  440. }
  441. print '</span></td>';
  442. if ($annee_decalage < $year_end || ($annee_decalage == $year_end && $mois > 12 && $annee < $year_end)) {
  443. print '<td width="15">&nbsp;</td>';
  444. }
  445. }
  446. if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
  447. if (empty($total_ht[$annee])) {
  448. $total_ht[$annee] = (empty($cum_ht[$case]) ? 0 : $cum_ht[$case]);
  449. } else {
  450. $total_ht[$annee] += (empty($cum_ht[$case]) ? 0 : $cum_ht[$case]);
  451. }
  452. if (empty($total[$annee])) {
  453. $total[$annee] = empty($cum[$case]) ? 0 : $cum[$case];
  454. } else {
  455. $total[$annee] += empty($cum[$case]) ? 0 : $cum[$case];
  456. }
  457. }
  458. }
  459. print '</tr>';
  460. }
  461. /*
  462. for ($mois = 1 ; $mois < 13 ; $mois++)
  463. {
  464. print '<tr class="oddeven">';
  465. print "<td>".dol_print_date(dol_mktime(12,0,0,$mois,1,2000),"%B")."</td>";
  466. for ($annee = $year_start ; $annee <= $year_end ; $annee++)
  467. {
  468. $casenow = dol_print_date(dol_now(),"%Y-%m");
  469. $case = dol_print_date(dol_mktime(1,1,1,$mois,1,$annee),"%Y-%m");
  470. $caseprev = dol_print_date(dol_mktime(1,1,1,$mois,1,$annee-1),"%Y-%m");
  471. // Valeur CA du mois
  472. print '<td class="right">';
  473. if ($cum[$case])
  474. {
  475. $now_show_delta=1; // On a trouve le premier mois de la premiere annee generant du chiffre.
  476. print '<a href="casoc.php?year='.$annee.'&month='.$mois.'">'.price($cum[$case],1).'</a>';
  477. }
  478. else
  479. {
  480. if ($minyearmonth < $case && $case <= max($maxyearmonth,$nowyearmonth)) { print '0'; }
  481. else { print '&nbsp;'; }
  482. }
  483. print "</td>";
  484. // Pourcentage du mois
  485. if ($annee > $minyear && $case <= $casenow) {
  486. if ($cum[$caseprev] && $cum[$case])
  487. {
  488. $percent=(round(($cum[$case]-$cum[$caseprev])/$cum[$caseprev],4)*100);
  489. //print "X $cum[$case] - $cum[$caseprev] - $cum[$caseprev] - $percent X";
  490. print '<td class="right">'.($percent>=0?"+$percent":"$percent").'%</td>';
  491. }
  492. if ($cum[$caseprev] && ! $cum[$case])
  493. {
  494. print '<td class="right">-100%</td>';
  495. }
  496. if (! $cum[$caseprev] && $cum[$case])
  497. {
  498. print '<td class="right">+Inf%</td>';
  499. }
  500. if (! $cum[$caseprev] && ! $cum[$case])
  501. {
  502. print '<td class="right">+0%</td>';
  503. }
  504. }
  505. else
  506. {
  507. print '<td class="right">';
  508. if ($minyearmonth <= $case && $case <= $maxyearmonth) { print '-'; }
  509. else { print '&nbsp;'; }
  510. print '</td>';
  511. }
  512. $total[$annee]+=$cum[$case];
  513. if ($annee != $year_end) print '<td width="15">&nbsp;</td>';
  514. }
  515. print '</tr>';
  516. }
  517. */
  518. // Show total
  519. print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td>';
  520. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  521. if ($modecompta == 'CREANCES-DETTES') {
  522. // Montant total HT
  523. if ($total_ht[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
  524. print '<td class="nowrap right">';
  525. print (empty($total_ht[$annee]) ? '0' : price($total_ht[$annee]));
  526. print "</td>";
  527. } else {
  528. print '<td>&nbsp;</td>';
  529. }
  530. }
  531. // Total amount
  532. if (!empty($total[$annee]) || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
  533. print '<td class="nowrap right">';
  534. print (empty($total[$annee]) ? '0' : price($total[$annee]));
  535. print "</td>";
  536. } else {
  537. print '<td>&nbsp;</td>';
  538. }
  539. // Pourcentage total
  540. if ($annee > $minyear && $annee <= max($nowyear, $maxyear)) {
  541. if (!empty($total[$annee - 1]) && !empty($total[$annee])) {
  542. $percent = (round(($total[$annee] - $total[$annee - 1]) / $total[$annee - 1], 4) * 100);
  543. print '<td class="nowrap borderrightlight right">';
  544. print ($percent >= 0 ? "+$percent" : "$percent").'%';
  545. print '</td>';
  546. }
  547. if (!empty($total[$annee - 1]) && empty($total[$annee])) {
  548. print '<td class="borderrightlight right">-100%</td>';
  549. }
  550. if (empty($total[$annee - 1]) && !empty($total[$annee])) {
  551. print '<td class="borderrightlight right">+'.$langs->trans('Inf').'%</td>';
  552. }
  553. if (empty($total[$annee - 1]) && empty($total[$annee])) {
  554. print '<td class="borderrightlight right">+0%</td>';
  555. }
  556. } else {
  557. print '<td class="borderrightlight right">';
  558. if (!empty($total[$annee]) || ($minyear <= $annee && $annee <= max($nowyear, $maxyear))) {
  559. print '-';
  560. } else {
  561. print '&nbsp;';
  562. }
  563. print '</td>';
  564. }
  565. if ($annee != $year_end) {
  566. print '<td width="15">&nbsp;</td>';
  567. }
  568. }
  569. print "</tr>\n";
  570. print "</table>";
  571. print '</div>';
  572. /*
  573. * En mode recettes/depenses, on complete avec les montants factures non regles
  574. * et les propales signees mais pas facturees. En effet, en recettes-depenses,
  575. * on comptabilise lorsque le montant est sur le compte donc il est interessant
  576. * d'avoir une vision de ce qui va arriver.
  577. */
  578. /*
  579. Je commente toute cette partie car les chiffres affichees sont faux - Eldy.
  580. En attendant correction.
  581. if ($modecompta != 'CREANCES-DETTES')
  582. {
  583. print '<br><table width="100%" class="noborder">';
  584. // Factures non reglees
  585. // Y a bug ici. Il faut prendre le reste a payer et non le total des factures non reglees !
  586. $sql = "SELECT f.ref, f.rowid, s.nom, s.rowid as socid, f.total_ttc, sum(pf.amount) as am";
  587. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f left join ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture";
  588. $sql .= " WHERE s.rowid = f.fk_soc AND f.paye = 0 AND f.fk_statut = 1";
  589. if ($socid)
  590. {
  591. $sql .= " AND f.fk_soc = $socid";
  592. }
  593. $sql .= " GROUP BY f.ref,f.rowid,s.nom, s.rowid, f.total_ttc";
  594. $resql=$db->query($sql);
  595. if ($resql)
  596. {
  597. $num = $db->num_rows($resql);
  598. $i = 0;
  599. if ($num)
  600. {
  601. $total_ttc_Rac = $totalam_Rac = $total_Rac = 0;
  602. while ($i < $num)
  603. {
  604. $obj = $db->fetch_object($resql);
  605. $total_ttc_Rac += $obj->total_ttc;
  606. $totalam_Rac += $obj->am;
  607. $i++;
  608. }
  609. print "<tr class="oddeven"><td class=\"right\" colspan=\"5\"><i>Facture a encaisser : </i></td><td class=\"right\"><i>".price($total_ttc_Rac)."</i></td><td colspan=\"5\"><-- bug ici car n'exclut pas le deja r?gl? des factures partiellement r?gl?es</td></tr>";
  610. }
  611. $db->free($resql);
  612. }
  613. else
  614. {
  615. dol_print_error($db);
  616. }
  617. */
  618. /*
  619. *
  620. * Propales signees, et non facturees
  621. *
  622. */
  623. /*
  624. Je commente toute cette partie car les chiffres affichees sont faux - Eldy.
  625. En attendant correction.
  626. $sql = "SELECT sum(f.total_ht) as tot_fht,sum(f.total_ttc) as tot_fttc, p.rowid, p.ref, s.nom, s.rowid as socid, p.total_ht, p.total_ttc
  627. FROM ".MAIN_DB_PREFIX."commande AS p, ".MAIN_DB_PREFIX."societe AS s
  628. LEFT JOIN ".MAIN_DB_PREFIX."co_fa AS co_fa ON co_fa.fk_commande = p.rowid
  629. LEFT JOIN ".MAIN_DB_PREFIX."facture AS f ON co_fa.fk_facture = f.rowid
  630. WHERE p.fk_soc = s.rowid
  631. AND p.fk_statut >=1
  632. AND p.facture =0";
  633. if ($socid)
  634. {
  635. $sql .= " AND f.fk_soc = ".((int) $socid);
  636. }
  637. $sql .= " GROUP BY p.rowid";
  638. $resql=$db->query($sql);
  639. if ($resql)
  640. {
  641. $num = $db->num_rows($resql);
  642. $i = 0;
  643. if ($num)
  644. {
  645. $total_pr = 0;
  646. while ($i < $num)
  647. {
  648. $obj = $db->fetch_object($resql);
  649. $total_pr += $obj->total_ttc-$obj->tot_fttc;
  650. $i++;
  651. }
  652. print "<tr class="oddeven"><td class=\"right\" colspan=\"5\"><i>Signe et non facture:</i></td><td class=\"right\"><i>".price($total_pr)."</i></td><td colspan=\"5\"><-- bug ici, ca devrait exclure le deja facture</td></tr>";
  653. }
  654. $db->free($resql);
  655. }
  656. else
  657. {
  658. dol_print_error($db);
  659. }
  660. print "<tr class="oddeven"><td class=\"right\" colspan=\"5\"><i>Total CA previsionnel : </i></td><td class=\"right\"><i>".price($total_CA)."</i></td><td colspan=\"3\"><-- bug ici car bug sur les 2 precedents</td></tr>";
  661. }
  662. print "</table>";
  663. */
  664. // End of page
  665. llxFooter();
  666. $db->close();