index.php 25 KB

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