result.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. <?php
  2. /* Copyright (C) 2016-2017 Jamal Elbaz <jamelbaz@gmail.com>
  3. * Copyright (C) 2016-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2018-2020 Laurent Destailleur <eldy@destailleur.fr>
  5. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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 3 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 <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/resultat/result.php
  22. * \ingroup compta, accountancy
  23. * \brief Page for accounting result
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array('compta', 'bills', 'donation', 'salaries', 'accountancy'));
  34. $error = 0;
  35. $mesg = '';
  36. $action = GETPOST('action', 'aZ09');
  37. $cat_id = GETPOST('account_category');
  38. $selectcpt = GETPOST('cpt_bk');
  39. $id = GETPOST('id', 'int');
  40. $rowid = GETPOST('rowid', 'int');
  41. $cancel = GETPOST('cancel', 'alpha');
  42. $showaccountdetail = GETPOST('showaccountdetail', 'aZ09') ?GETPOST('showaccountdetail', 'aZ09') : 'no';
  43. $date_startmonth = GETPOST('date_startmonth', 'int');
  44. $date_startday = GETPOST('date_startday', 'int');
  45. $date_startyear = GETPOST('date_startyear', 'int');
  46. $date_endmonth = GETPOST('date_endmonth', 'int');
  47. $date_endday = GETPOST('date_endday', 'int');
  48. $date_endyear = GETPOST('date_endyear', 'int');
  49. $nbofyear = 1;
  50. // Date range
  51. $year = GETPOST('year', 'int');
  52. if (empty($year)) {
  53. $year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  54. $month_current = strftime("%m", dol_now());
  55. $year_start = $year_current - ($nbofyear - 1);
  56. } else {
  57. $year_current = $year;
  58. $month_current = strftime("%m", dol_now());
  59. $year_start = $year - ($nbofyear - 1);
  60. }
  61. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  62. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  63. // We define date_start and date_end
  64. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  65. $q = GETPOST("q") ?GETPOST("q") : 0;
  66. if ($q == 0) {
  67. // We define date_start and date_end
  68. $year_end = $year_start + ($nbofyear - 1);
  69. $month_start = GETPOST("month", 'int') ?GETPOST("month", 'int') : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  70. $date_startmonth = $month_start;
  71. if (!GETPOST('month')) {
  72. if (!GETPOST("year") && $month_start > $month_current) {
  73. $year_start--;
  74. $year_end--;
  75. }
  76. $month_end = $month_start - 1;
  77. if ($month_end < 1) {
  78. $month_end = 12;
  79. } else {
  80. $year_end++;
  81. }
  82. } else {
  83. $month_end = $month_start;
  84. }
  85. $date_start = dol_get_first_day($year_start, $month_start, false);
  86. $date_end = dol_get_last_day($year_end, $month_end, false);
  87. }
  88. if ($q == 1) {
  89. $date_start = dol_get_first_day($year_start, 1, false);
  90. $date_end = dol_get_last_day($year_start, 3, false);
  91. }
  92. if ($q == 2) {
  93. $date_start = dol_get_first_day($year_start, 4, false);
  94. $date_end = dol_get_last_day($year_start, 6, false);
  95. }
  96. if ($q == 3) {
  97. $date_start = dol_get_first_day($year_start, 7, false);
  98. $date_end = dol_get_last_day($year_start, 9, false);
  99. }
  100. if ($q == 4) {
  101. $date_start = dol_get_first_day($year_start, 10, false);
  102. $date_end = dol_get_last_day($year_start, 12, false);
  103. }
  104. }
  105. if (($date_start < dol_time_plus_duree($date_end, -1, 'y')) || ($date_start > $date_end)) {
  106. $date_end = dol_time_plus_duree($date_start - 1, 1, 'y');
  107. }
  108. // $date_start and $date_end are defined. We force $start_year and $nbofyear
  109. $tmps = dol_getdate($date_start);
  110. $start_year = $tmps['year'];
  111. $start_month = $tmps['mon'];
  112. $tmpe = dol_getdate($date_end);
  113. $year_end = $tmpe['year'];
  114. $month_end = $tmpe['mon'];
  115. $nbofyear = ($year_end - $start_year) + 1;
  116. $date_start_previous = dol_time_plus_duree($date_start, -1, 'y');
  117. $date_end_previous = dol_time_plus_duree($date_end, -1, 'y');
  118. //var_dump($date_start." ".$date_end." ".$date_start_previous." ".$date_end_previous." ".$nbofyear);
  119. if ($cat_id == 0) {
  120. $cat_id = null;
  121. }
  122. // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING')
  123. $modecompta = $conf->global->ACCOUNTING_MODE;
  124. if (isModEnabled('accounting')) {
  125. $modecompta = 'BOOKKEEPING';
  126. }
  127. if (GETPOST("modecompta")) {
  128. $modecompta = GETPOST("modecompta", 'alpha');
  129. }
  130. $AccCat = new AccountancyCategory($db);
  131. // Security check
  132. $socid = GETPOST('socid', 'int');
  133. if ($user->socid > 0) {
  134. $socid = $user->socid;
  135. }
  136. if (isModEnabled('comptabilite')) {
  137. $result = restrictedArea($user, 'compta', '', '', 'resultat');
  138. }
  139. if (isModEnabled('accounting')) {
  140. $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
  141. }
  142. /*
  143. * View
  144. */
  145. $months = array(
  146. $langs->trans("MonthShort01"),
  147. $langs->trans("MonthShort02"),
  148. $langs->trans("MonthShort03"),
  149. $langs->trans("MonthShort04"),
  150. $langs->trans("MonthShort05"),
  151. $langs->trans("MonthShort06"),
  152. $langs->trans("MonthShort07"),
  153. $langs->trans("MonthShort08"),
  154. $langs->trans("MonthShort09"),
  155. $langs->trans("MonthShort10"),
  156. $langs->trans("MonthShort11"),
  157. $langs->trans("MonthShort12"),
  158. );
  159. llxheader('', $langs->trans('ReportInOut'));
  160. $formaccounting = new FormAccounting($db);
  161. $form = new Form($db);
  162. $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year - 1).'&showaccountdetail='.urlencode($showaccountdetail).'">'.img_previous().'</a>';
  163. $textnextyear = ' &nbsp; <a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year + 1).'&showaccountdetail='.urlencode($showaccountdetail).'">'.img_next().'</a>';
  164. // Affiche en-tete de rapport
  165. if ($modecompta == "CREANCES-DETTES") {
  166. $name = $langs->trans("AnnualByAccountDueDebtMode");
  167. $calcmode = $langs->trans("CalcModeDebt");
  168. $calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$start_year.(GETPOST("month") > 0 ? '&month='.GETPOST("month") : '').'&modecompta=RECETTES-DEPENSES">', '</a>').')';
  169. if (isModEnabled('accounting')) {
  170. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$start_year.'&modecompta=BOOKKEEPING">', '</a>').')';
  171. }
  172. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  173. //$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>';
  174. $description = $langs->trans("RulesResultDue");
  175. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  176. $description .= $langs->trans("DepositsAreNotIncluded");
  177. } else {
  178. $description .= $langs->trans("DepositsAreIncluded");
  179. }
  180. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  181. $description .= $langs->trans("SupplierDepositsAreNotIncluded");
  182. }
  183. $builddate = dol_now();
  184. //$exportlink=$langs->trans("NotYetAvailable");
  185. } elseif ($modecompta == "RECETTES-DEPENSES") {
  186. $name = $langs->trans("AnnualByAccountInputOutputMode");
  187. $calcmode = $langs->trans("CalcModeEngagement");
  188. $calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month") > 0 ? '&month='.GETPOST("month") : '').'&modecompta=CREANCES-DETTES">', '</a>').')';
  189. if (isModEnabled('accounting')) {
  190. $calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=BOOKKEEPING">', '</a>').')';
  191. }
  192. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  193. //$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>';
  194. $description = $langs->trans("RulesResultInOut");
  195. $builddate = dol_now();
  196. //$exportlink=$langs->trans("NotYetAvailable");
  197. } elseif ($modecompta == "BOOKKEEPING") {
  198. $name = $langs->trans("ReportInOut").', '.$langs->trans("ByPersonalizedAccountGroups");
  199. $calcmode = $langs->trans("CalcModeBookkeeping");
  200. //$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
  201. //$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
  202. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  203. $arraylist = array('no'=>$langs->trans("No"), 'yes'=>$langs->trans("AccountWithNonZeroValues"), 'all'=>$langs->trans("All"));
  204. $period .= ' &nbsp; &nbsp; '.$langs->trans("DetailByAccount").' '.$form->selectarray('showaccountdetail', $arraylist, $showaccountdetail, 0);
  205. $periodlink = $textprevyear.$textnextyear;
  206. $exportlink = '';
  207. $description = $langs->trans("RulesResultBookkeepingPersonalized");
  208. $description .= ' ('.$langs->trans("SeePageForSetup", DOL_URL_ROOT.'/accountancy/admin/categories_list.php?search_country_id='.$mysoc->country_id.'&mainmenu=accountancy&leftmenu=accountancy_admin', $langs->transnoentitiesnoconv("Accountancy").' / '.$langs->transnoentitiesnoconv("Setup").' / '.$langs->transnoentitiesnoconv("AccountingCategory")).')';
  209. $builddate = dol_now();
  210. }
  211. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array('modecompta'=>$modecompta, 'action' => ''), $calcmode);
  212. if (isModEnabled('accounting') && $modecompta != 'BOOKKEEPING') {
  213. print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
  214. }
  215. $moreforfilter = '';
  216. print '<div class="div-table-responsive">';
  217. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  218. print '<tr class="liste_titre">';
  219. print '<th class="liste_titre">'.$langs->trans("AccountingCategory").'</th>';
  220. print '<th class="liste_titre"></th>';
  221. print '<th class="liste_titre right">'.$langs->trans("PreviousPeriod").'</th>';
  222. print '<th class="liste_titre right">'.$langs->trans("SelectedPeriod").'</th>';
  223. foreach ($months as $k => $v) {
  224. if (($k + 1) >= $date_startmonth) {
  225. print '<th class="liste_titre right width50">'.$langs->trans('MonthShort'.sprintf("%02s", ($k + 1))).'</th>';
  226. }
  227. }
  228. foreach ($months as $k => $v) {
  229. if (($k + 1) < $date_startmonth) {
  230. print '<th class="liste_titre right width50">'.$langs->trans('MonthShort'.sprintf("%02s", ($k + 1))).'</th>';
  231. }
  232. }
  233. print '</tr>';
  234. if ($modecompta == 'CREANCES-DETTES') {
  235. //if (!empty($date_start) && !empty($date_end))
  236. // $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  237. } elseif ($modecompta == "RECETTES-DEPENSES") {
  238. //if (!empty($date_start) && !empty($date_end))
  239. // $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  240. } elseif ($modecompta == "BOOKKEEPING") {
  241. // Get array of all report groups that are active
  242. $cats = $AccCat->getCats(); // WARNING: Computed groups must be after group they include
  243. $unactive_cats = $AccCat->getCats(-1, 0);
  244. /*
  245. $sql = 'SELECT DISTINCT t.numero_compte as nb FROM '.MAIN_DB_PREFIX.'accounting_bookkeeping as t, '.MAIN_DB_PREFIX.'accounting_account as aa';
  246. $sql.= " WHERE t.numero_compte = aa.account_number AND aa.fk_accounting_category = 0";
  247. if (!empty($date_start) && !empty($date_end))
  248. $sql.= " AND t.doc_date >= '".$db->idate($date_start)."' AND t.doc_date <= '".$db->idate($date_end)."'";
  249. if (!empty($month)) {
  250. $sql .= " AND MONTH(t.doc_date) = " . ((int) $month);
  251. }
  252. $resql = $db->query($sql);
  253. if ($resql)
  254. {
  255. $num_rows = $db->num_rows($resql);
  256. if ($num_rows) {
  257. print '<div class="warning">Warning: There is '.$num_rows.' accounts in your ledger table that are not set into a reporting group</div>';
  258. $i = 0;
  259. //while ($i < $num) {
  260. // $obj = $db->fetch_object($resql);
  261. // $i++;
  262. //}
  263. }
  264. }
  265. else dol_print_error($db);
  266. */
  267. $j = 1;
  268. $sommes = array();
  269. $totPerAccount = array();
  270. if (!is_array($cats) && $cats < 0) {
  271. setEventMessages(null, $AccCat->errors, 'errors');
  272. } elseif (is_array($cats) && count($cats) > 0) {
  273. // Loop on each custom group of accounts
  274. foreach ($cats as $cat) {
  275. if (!empty($cat['category_type'])) {
  276. // category calculed
  277. // When we enter here, $sommes was filled by group of accounts
  278. $formula = $cat['formula'];
  279. print '<tr class="liste_total">';
  280. // Code and Label
  281. print '<td class="liste_total tdoverflowmax100" title="'.dol_escape_htmltag($cat['code']).'">';
  282. print dol_escape_htmltag($cat['code']);
  283. print '</td><td class="tdoverflowmax250" title="'.dol_escape_htmltag($cat['label']).'">';
  284. print dol_escape_htmltag($cat['label']);
  285. print '</td>';
  286. $vars = array();
  287. // Unactive categories have a total of 0 to be used in the formula.
  288. foreach ($unactive_cats as $un_cat) {
  289. $vars[$un_cat['code']] = 0;
  290. }
  291. // Previous Fiscal year (N-1)
  292. foreach ($sommes as $code => $det) {
  293. $vars[$code] = empty($det['NP']) ? 0 : $det['NP'];
  294. }
  295. $result = strtr($formula, $vars);
  296. $result = str_replace('--', '+', $result);
  297. if (preg_match('/[a-z]/i', $result)) {
  298. $r = 'Error bad formula: '.$result;
  299. $rshort = 'Err';
  300. print '<td class="liste_total right"><span class="amount" title="'.dol_escape_htmltag($r).'">'.$rshort.'</span></td>';
  301. } else {
  302. //var_dump($result);
  303. //$r = $AccCat->calculate($result);
  304. $r = dol_eval($result, 1, 1, '1');
  305. if (is_nan($r)) {
  306. $r = 0;
  307. }
  308. print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
  309. }
  310. // Year N
  311. $code = $cat['code']; // code of categorie ('VTE', 'MAR', ...)
  312. if (empty($sommes[$code]['NP'])) {
  313. $sommes[$code]['NP'] = $r;
  314. } else {
  315. $sommes[$code]['NP'] += $r;
  316. }
  317. // Current fiscal year (N)
  318. if (is_array($sommes) && !empty($sommes)) {
  319. foreach ($sommes as $code => $det) {
  320. $vars[$code] = empty($det['N']) ? 0 : $det['N'];
  321. }
  322. }
  323. $result = strtr($formula, $vars);
  324. $result = str_replace('--', '+', $result);
  325. //$r = $AccCat->calculate($result);
  326. $r = dol_eval($result, 1, 1, '1');
  327. if (is_nan($r)) {
  328. $r = 0;
  329. }
  330. print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
  331. if (empty($sommes[$code]['N'])) {
  332. $sommes[$code]['N'] = $r;
  333. } else {
  334. $sommes[$code]['N'] += $r;
  335. }
  336. // Detail by month
  337. foreach ($months as $k => $v) {
  338. if (($k + 1) >= $date_startmonth) {
  339. foreach ($sommes as $code => $det) {
  340. $vars[$code] = empty($det['M'][$k]) ? 0 : $det['M'][$k];
  341. }
  342. $result = strtr($formula, $vars);
  343. $result = str_replace('--', '+', $result);
  344. //$r = $AccCat->calculate($result);
  345. $r = dol_eval($result, 1, 1, '1');
  346. if (is_nan($r)) {
  347. $r = 0;
  348. }
  349. print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
  350. if (empty($sommes[$code]['M'][$k])) {
  351. $sommes[$code]['M'][$k] = $r;
  352. } else {
  353. $sommes[$code]['M'][$k] += $r;
  354. }
  355. }
  356. }
  357. foreach ($months as $k => $v) {
  358. if (($k + 1) < $date_startmonth) {
  359. foreach ($sommes as $code => $det) {
  360. $vars[$code] = empty($det['M'][$k]) ? 0 : $det['M'][$k];
  361. }
  362. $result = strtr($formula, $vars);
  363. $result = str_replace('--', '+', $result);
  364. //$r = $AccCat->calculate($result);
  365. $r = dol_eval($result, 1, 1, '1');
  366. if (is_nan($r)) {
  367. $r = 0;
  368. }
  369. print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
  370. if (empty($sommes[$code]['M'][$k])) {
  371. $sommes[$code]['M'][$k] = $r;
  372. } else {
  373. $sommes[$code]['M'][$k] += $r;
  374. }
  375. }
  376. }
  377. print "</tr>\n";
  378. //var_dump($sommes);
  379. } else { // normal category
  380. $code = $cat['code']; // Category code we process
  381. $totCat = array();
  382. $totCat['NP'] = 0;
  383. $totCat['N'] = 0;
  384. $totCat['M'] = array();
  385. foreach ($months as $k => $v) {
  386. $totCat['M'][$k] = 0;
  387. }
  388. // Set $cpts with array of accounts in the category/group
  389. $cpts = $AccCat->getCptsCat($cat['rowid']);
  390. // We should loop over empty $cpts array, else the category _code_ is used in the formula, which leads to wrong result if the code is a number.
  391. if (empty($cpts)) {
  392. $cpts[] = array();
  393. }
  394. $arrayofaccountforfilter = array();
  395. foreach ($cpts as $i => $cpt) { // Loop on each account.
  396. if (isset($cpt['account_number'])) {
  397. $arrayofaccountforfilter[] = $cpt['account_number'];
  398. }
  399. }
  400. // N-1
  401. if (!empty($arrayofaccountforfilter)) {
  402. $return = $AccCat->getSumDebitCredit($arrayofaccountforfilter, $date_start_previous, $date_end_previous, empty($cat['dc']) ? 0 : $cat['dc']);
  403. if ($return < 0) {
  404. setEventMessages(null, $AccCat->errors, 'errors');
  405. $resultNP = 0;
  406. } else {
  407. foreach ($cpts as $i => $cpt) { // Loop on each account found
  408. $resultNP = empty($AccCat->sdcperaccount[$cpt['account_number']]) ? 0 : $AccCat->sdcperaccount[$cpt['account_number']];
  409. if (empty($totCat['NP'])) {
  410. $totCat['NP'] = $resultNP;
  411. } else {
  412. $totCat['NP'] += $resultNP;
  413. }
  414. if (empty($sommes[$code]['NP'])) {
  415. $sommes[$code]['NP'] = $resultNP;
  416. } else {
  417. $sommes[$code]['NP'] += $resultNP;
  418. }
  419. $totPerAccount[$cpt['account_number']]['NP'] = $resultNP;
  420. }
  421. }
  422. }
  423. // Set value into column N and month M ($totCat)
  424. // This make 12 calls for each accountancy account (12 monthes M)
  425. foreach ($cpts as $i => $cpt) { // Loop on each account.
  426. // We make 1 loop for each account because we may want detail per account.
  427. // @todo Optimize to ask a 'group by' account and a filter with account in (..., ...) in request
  428. // Each month
  429. $resultN = 0;
  430. foreach ($months as $k => $v) {
  431. $monthtoprocess = $k + 1; // ($k+1) is month 1, 2, ..., 12
  432. $yeartoprocess = $start_year;
  433. if (($k + 1) < $start_month) {
  434. $yeartoprocess++;
  435. }
  436. //var_dump($monthtoprocess.'_'.$yeartoprocess);
  437. if (isset($cpt['account_number'])) {
  438. $return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start, $date_end, empty($cat['dc']) ? 0 : $cat['dc'], 'nofilter', $monthtoprocess, $yeartoprocess);
  439. if ($return < 0) {
  440. setEventMessages(null, $AccCat->errors, 'errors');
  441. $resultM = 0;
  442. } else {
  443. $resultM = $AccCat->sdc;
  444. }
  445. } else {
  446. $resultM = 0;
  447. }
  448. if (empty($totCat['M'][$k])) {
  449. $totCat['M'][$k] = $resultM;
  450. } else {
  451. $totCat['M'][$k] += $resultM;
  452. }
  453. if (empty($sommes[$code]['M'][$k])) {
  454. $sommes[$code]['M'][$k] = $resultM;
  455. } else {
  456. $sommes[$code]['M'][$k] += $resultM;
  457. }
  458. if (isset($cpt['account_number'])) {
  459. $totPerAccount[$cpt['account_number']]['M'][$k] = $resultM;
  460. }
  461. $resultN += $resultM;
  462. }
  463. if (empty($totCat)) {
  464. $totCat['N'] = $resultN;
  465. } else {
  466. $totCat['N'] += $resultN;
  467. }
  468. if (empty($sommes[$code]['N'])) {
  469. $sommes[$code]['N'] = $resultN;
  470. } else {
  471. $sommes[$code]['N'] += $resultN;
  472. }
  473. if (isset($cpt['account_number'])) {
  474. $totPerAccount[$cpt['account_number']]['N'] = $resultN;
  475. }
  476. }
  477. // Now output columns for row $code ('VTE', 'MAR', ...)
  478. print '<tr'.($showaccountdetail != 'no' ? ' class="trforbreak"' : '').'>';
  479. // Column group
  480. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($cat['code']).'">';
  481. print dol_escape_htmltag($cat['code']);
  482. print '</td>';
  483. // Label of group
  484. $labeltoshow = dol_escape_htmltag($cat['label']);
  485. if (count($cpts) > 0 && !empty($cpts[0])) { // Show example of 5 first accounting accounts
  486. $i = 0;
  487. foreach ($cpts as $cpt) {
  488. if ($i > 5) {
  489. $labeltoshow .= '...)';
  490. break;
  491. }
  492. if ($i > 0) {
  493. $labeltoshow .= ', ';
  494. } else {
  495. $labeltoshow .= ' (';
  496. }
  497. $labeltoshow .= dol_escape_htmltag($cpt['account_number']);
  498. $i++;
  499. }
  500. if ($i <= 5) {
  501. $labeltoshow .= ')';
  502. }
  503. } else {
  504. $labeltoshow .= ' - <span class="warning">'.$langs->trans("GroupIsEmptyCheckSetup").'</span>';
  505. }
  506. print '<td class="tdoverflowmax250" title="'.dol_escape_htmltag(dol_string_nohtmltag($labeltoshow)).'">';
  507. print $labeltoshow;
  508. print '</td>';
  509. print '<td class="right"><span class="amount">'.price($totCat['NP']).'</span></td>';
  510. print '<td class="right"><span class="amount">'.price($totCat['N']).'</span></td>';
  511. // Each month
  512. foreach ($totCat['M'] as $k => $v) {
  513. if (($k + 1) >= $date_startmonth) {
  514. print '<td class="right nowraponall"><span class="amount">'.price($v).'</span></td>';
  515. }
  516. }
  517. foreach ($totCat['M'] as $k => $v) {
  518. if (($k + 1) < $date_startmonth) {
  519. print '<td class="right nowraponall"><span class="amount">'.price($v).'</span></td>';
  520. }
  521. }
  522. print "</tr>\n";
  523. // Loop on detail of all accounts to output the detail
  524. if ($showaccountdetail != 'no') {
  525. foreach ($cpts as $i => $cpt) {
  526. if (isset($cpt['account_number'])) {
  527. $resultNP = $totPerAccount[$cpt['account_number']]['NP'];
  528. $resultN = $totPerAccount[$cpt['account_number']]['N'];
  529. } else {
  530. $resultNP = 0;
  531. $resultN = 0;
  532. }
  533. if ($showaccountdetail == 'all' || $resultN != 0) {
  534. print '<tr>';
  535. print '<td></td>';
  536. if (isset($cpt['account_number'])) {
  537. $labeldetail = ' &nbsp; &nbsp; '.length_accountg($cpt['account_number']).' - '.$cpt['account_label'];
  538. } else {
  539. $labeldetail = '-';
  540. }
  541. print '<td class="tdoverflowmax250" title="'.dol_escape_htmltag($labeldetail).'">';
  542. print dol_escape_htmltag($labeldetail);
  543. print '</td>';
  544. print '<td class="right"><span class="amount">'.price($resultNP).'</span></td>';
  545. print '<td class="right"><span class="amount">'.price($resultN).'</span></td>';
  546. // Make one call for each month
  547. foreach ($months as $k => $v) {
  548. if (($k + 1) >= $date_startmonth) {
  549. if (isset($cpt['account_number'])) {
  550. $resultM = $totPerAccount[$cpt['account_number']]['M'][$k];
  551. } else {
  552. $resultM = 0;
  553. }
  554. print '<td class="right"><span class="amount">'.price($resultM).'</span></td>';
  555. }
  556. }
  557. foreach ($months as $k => $v) {
  558. if (($k + 1) < $date_startmonth) {
  559. if (isset($cpt['account_number'])) {
  560. $resultM = empty($totPerAccount[$cpt['account_number']]['M'][$k]) ? 0 : $totPerAccount[$cpt['account_number']]['M'][$k];
  561. } else {
  562. $resultM = 0;
  563. }
  564. print '<td class="right"><span class="amount">'.price($resultM).'</span></td>';
  565. }
  566. }
  567. print "</tr>\n";
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. print "</table>";
  576. print '</div>';
  577. // End of page
  578. llxFooter();
  579. $db->close();