result.php 24 KB

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