index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  4. * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  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/accountancy/expensereport/index.php
  22. * \ingroup Accountancy (Double entries)
  23. * \brief Home expense report ventilation
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array("compta", "bills", "other", "accountancy"));
  32. $validatemonth = GETPOST('validatemonth', 'int');
  33. $validateyear = GETPOST('validateyear', 'int');
  34. $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  35. if (GETPOST("year", 'int')) {
  36. $year_start = GETPOST("year", 'int');
  37. } else {
  38. $year_start = dol_print_date(dol_now(), '%Y');
  39. if (dol_print_date(dol_now(), '%m') < $month_start) {
  40. $year_start--; // If current month is lower that starting fiscal month, we start last year
  41. }
  42. }
  43. $year_end = $year_start + 1;
  44. $month_end = $month_start - 1;
  45. if ($month_end < 1) {
  46. $month_end = 12;
  47. $year_end--;
  48. }
  49. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  50. $search_date_end = dol_get_last_day($year_end, $month_end);
  51. $year_current = $year_start;
  52. // Validate History
  53. $action = GETPOST('action', 'aZ09');
  54. $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  55. // Security check
  56. if (!isModEnabled('accounting')) {
  57. accessforbidden();
  58. }
  59. if ($user->socid > 0) {
  60. accessforbidden();
  61. }
  62. if (!$user->hasRight('accounting', 'mouvements', 'lire')) {
  63. accessforbidden();
  64. }
  65. /*
  66. * Actions
  67. */
  68. if (($action == 'clean' || $action == 'validatehistory') && $user->hasRight('accounting', 'bind', 'write')) {
  69. // Clean database
  70. $db->begin();
  71. $sql1 = "UPDATE ".MAIN_DB_PREFIX."expensereport_det as erd";
  72. $sql1 .= " SET fk_code_ventilation = 0";
  73. $sql1 .= ' WHERE erd.fk_code_ventilation NOT IN';
  74. $sql1 .= ' (SELECT accnt.rowid ';
  75. $sql1 .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as accnt';
  76. $sql1 .= ' INNER JOIN '.MAIN_DB_PREFIX.'accounting_system as syst';
  77. $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid='.((int) $conf->global->CHARTOFACCOUNTS).' AND accnt.entity = '.((int) $conf->entity).')';
  78. $sql1 .= ' AND erd.fk_expensereport IN (SELECT rowid FROM '.MAIN_DB_PREFIX.'expensereport WHERE entity = '.((int) $conf->entity).')';
  79. $sql1 .= ' AND fk_code_ventilation <> 0';
  80. dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
  81. $resql1 = $db->query($sql1);
  82. if (!$resql1) {
  83. $error++;
  84. $db->rollback();
  85. setEventMessages($db->lasterror(), null, 'errors');
  86. } else {
  87. $db->commit();
  88. }
  89. // End clean database
  90. }
  91. if ($action == 'validatehistory') {
  92. $error = 0;
  93. $nbbinddone = 0;
  94. $notpossible = 0;
  95. $db->begin();
  96. // Now make the binding
  97. $sql1 = "SELECT erd.rowid, accnt.rowid as suggestedid";
  98. $sql1 .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
  99. $sql1 .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees as t ON erd.fk_c_type_fees = t.id";
  100. $sql1 .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as accnt ON t.accountancy_code = accnt.account_number AND accnt.active = 1 AND accnt.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND accnt.entity =".((int) $conf->entity).",";
  101. $sql1 .= " ".MAIN_DB_PREFIX."expensereport as er";
  102. $sql1 .= " WHERE erd.fk_expensereport = er.rowid AND er.entity = ".((int) $conf->entity);
  103. $sql1 .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.") AND erd.fk_code_ventilation <= 0";
  104. if ($validatemonth && $validateyear) {
  105. $sql1 .= dolSqlDateFilter('erd.date', 0, $validatemonth, $validateyear);
  106. }
  107. dol_syslog('htdocs/accountancy/expensereport/index.php');
  108. $result = $db->query($sql1);
  109. if (!$result) {
  110. $error++;
  111. setEventMessages($db->lasterror(), null, 'errors');
  112. } else {
  113. $num_lines = $db->num_rows($result);
  114. $i = 0;
  115. while ($i < min($num_lines, 10000)) { // No more than 10000 at once
  116. $objp = $db->fetch_object($result);
  117. $lineid = $objp->rowid;
  118. $suggestedid = $objp->suggestedid;
  119. if ($suggestedid > 0) {
  120. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."expensereport_det";
  121. $sqlupdate .= " SET fk_code_ventilation = ".((int) $suggestedid);
  122. $sqlupdate .= " WHERE fk_code_ventilation <= 0 AND rowid = ".((int) $lineid);
  123. $resqlupdate = $db->query($sqlupdate);
  124. if (!$resqlupdate) {
  125. $error++;
  126. setEventMessages($db->lasterror(), null, 'errors');
  127. break;
  128. } else {
  129. $nbbinddone++;
  130. }
  131. } else {
  132. $notpossible++;
  133. }
  134. $i++;
  135. }
  136. if ($num_lines > 10000) {
  137. $notpossible += ($num_lines - 10000);
  138. }
  139. }
  140. if ($error) {
  141. $db->rollback();
  142. } else {
  143. $db->commit();
  144. setEventMessages($langs->trans('AutomaticBindingDone', $nbbinddone, $notpossible), null, 'mesgs');
  145. }
  146. }
  147. /*
  148. * View
  149. */
  150. llxHeader('', $langs->trans("ExpenseReportsVentilation"));
  151. $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
  152. $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
  153. print load_fiche_titre($langs->trans("ExpenseReportsVentilation")."&nbsp;".$textprevyear."&nbsp;".$langs->trans("Year")."&nbsp;".$year_start."&nbsp;".$textnextyear, '', 'title_accountancy');
  154. print '<span class="opacitymedium">'.$langs->trans("DescVentilExpenseReport").'</span><br>';
  155. print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("DescVentilExpenseReportMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")).'<br>';
  156. print '</span><br>';
  157. $y = $year_current;
  158. $buttonbind = '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=validatehistory&token='.newToken().'&year='.$year_current.'">'.$langs->trans("ValidateHistory").'</a>';
  159. print_barre_liste(img_picto('', 'unlink', 'class="paddingright fa-color-unset"').$langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
  160. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
  161. print '<div class="div-table-responsive-no-min">';
  162. print '<table class="noborder centpercent">';
  163. print '<tr class="liste_titre"><td class="minwidth100">'.$langs->trans("Account").'</td>';
  164. print '<td>'.$langs->trans("Label").'</td>';
  165. for ($i = 1; $i <= 12; $i++) {
  166. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  167. if ($j > 12) {
  168. $j -= 12;
  169. }
  170. $cursormonth = $j;
  171. if ($cursormonth > 12) {
  172. $cursormonth -= 12;
  173. }
  174. $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
  175. $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
  176. print '<td width="60" class="right">';
  177. if (!empty($tmp['mday'])) {
  178. $param = 'search_date_startday=1&search_date_startmonth='.$cursormonth.'&search_date_startyear='.$cursoryear;
  179. $param .= '&search_date_endday='.$tmp['mday'].'&search_date_endmonth='.$tmp['mon'].'&search_date_endyear='.$tmp['year'];
  180. $param .= '&search_month='.$tmp['mon'].'&search_year='.$tmp['year'];
  181. print '<a href="'.DOL_URL_ROOT.'/accountancy/expensereport/list.php?'.$param.'">';
  182. }
  183. print $langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT));
  184. if (!empty($tmp['mday'])) {
  185. print '</a>';
  186. }
  187. print '</td>';
  188. }
  189. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  190. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
  191. $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
  192. for ($i = 1; $i <= 12; $i++) {
  193. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  194. if ($j > 12) {
  195. $j -= 12;
  196. }
  197. $sql .= " SUM(".$db->ifsql("MONTH(er.date_debut)=".$j, "erd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  198. }
  199. $sql .= " SUM(erd.total_ht) as total";
  200. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
  201. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
  202. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
  203. $sql .= " WHERE er.date_debut >= '".$db->idate($search_date_start)."'";
  204. $sql .= " AND er.date_debut <= '".$db->idate($search_date_end)."'";
  205. // Define begin binding date
  206. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  207. $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  208. }
  209. $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
  210. $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
  211. $sql .= " AND aa.account_number IS NULL";
  212. $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
  213. $sql .= ' ORDER BY aa.account_number';
  214. dol_syslog('/accountancy/expensereport/index.php', LOG_DEBUG);
  215. $resql = $db->query($sql);
  216. if ($resql) {
  217. $num = $db->num_rows($resql);
  218. while ($row = $db->fetch_row($resql)) {
  219. print '<tr class="oddeven">';
  220. print '<td>';
  221. if ($row[0] == 'tobind') {
  222. print '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>';
  223. } else {
  224. print length_accountg($row[0]);
  225. }
  226. print '</td>';
  227. print '<td>';
  228. if ($row[0] == 'tobind') {
  229. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.((int) $y), $langs->transnoentitiesnoconv("ToBind"));
  230. } else {
  231. print $row[1];
  232. }
  233. print '</td>';
  234. for ($i = 2; $i <= 13; $i++) {
  235. print '<td class="right nowraponall amount">';
  236. print price($row[$i]);
  237. print '</td>';
  238. }
  239. print '<td class="right nowraponall amount"><b>'.price($row[14]).'</b></td>';
  240. print '</tr>';
  241. }
  242. $db->free($resql);
  243. if ($num == 0) {
  244. print '<tr class="oddeven"><td colspan="16">';
  245. print '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>';
  246. print '</td></tr>';
  247. }
  248. } else {
  249. print $db->lasterror(); // Show last sql error
  250. }
  251. print "</table>\n";
  252. print '</div>';
  253. print '<br>';
  254. print_barre_liste(img_picto('', 'link', 'class="paddingright fa-color-unset"').$langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  255. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
  256. print '<div class="div-table-responsive-no-min">';
  257. print '<table class="noborder centpercent">';
  258. print '<tr class="liste_titre"><td class="minwidth100">'.$langs->trans("Account").'</td>';
  259. print '<td>'.$langs->trans("Label").'</td>';
  260. for ($i = 1; $i <= 12; $i++) {
  261. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  262. if ($j > 12) {
  263. $j -= 12;
  264. }
  265. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  266. }
  267. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  268. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
  269. $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
  270. for ($i = 1; $i <= 12; $i++) {
  271. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  272. if ($j > 12) {
  273. $j -= 12;
  274. }
  275. $sql .= " SUM(".$db->ifsql("MONTH(er.date_debut)=".$j, "erd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  276. }
  277. $sql .= " ROUND(SUM(erd.total_ht),2) as total";
  278. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
  279. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
  280. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
  281. $sql .= " WHERE er.date_debut >= '".$db->idate($search_date_start)."'";
  282. $sql .= " AND er.date_debut <= '".$db->idate($search_date_end)."'";
  283. // Define begin binding date
  284. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  285. $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  286. }
  287. $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
  288. $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
  289. $sql .= " AND aa.account_number IS NOT NULL";
  290. $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
  291. dol_syslog('htdocs/accountancy/expensereport/index.php');
  292. $resql = $db->query($sql);
  293. if ($resql) {
  294. $num = $db->num_rows($resql);
  295. while ($row = $db->fetch_row($resql)) {
  296. print '<tr class="oddeven">';
  297. print '<td>';
  298. if ($row[0] == 'tobind') {
  299. print '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>';
  300. } else {
  301. print length_accountg($row[0]);
  302. }
  303. print '</td>';
  304. print '<td>';
  305. if ($row[0] == 'tobind') {
  306. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.((int) $y), $langs->transnoentitiesnoconv("ToBind"));
  307. } else {
  308. print $row[1];
  309. }
  310. print '</td>';
  311. for ($i = 2; $i <= 13; $i++) {
  312. print '<td class="right nowraponall amount">';
  313. print price($row[$i]);
  314. print '</td>';
  315. }
  316. print '<td class="right nowraponall amount"><b>'.price($row[14]).'</b></td>';
  317. print '</tr>';
  318. }
  319. $db->free($resql);
  320. } else {
  321. print $db->lasterror(); // Show last sql error
  322. }
  323. print "</table>\n";
  324. print '</div>';
  325. if ($conf->global->MAIN_FEATURES_LEVEL > 0) { // This part of code looks strange. Why showing a report where results depends on next step (so not yet available) ?
  326. print '<br>';
  327. print '<br>';
  328. print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  329. //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
  330. print '<div class="div-table-responsive-no-min">';
  331. print '<table class="noborder centpercent">';
  332. print '<tr class="liste_titre"><td class="left">'.$langs->trans("Total").'</td>';
  333. for ($i = 1; $i <= 12; $i++) {
  334. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  335. if ($j > 12) {
  336. $j -= 12;
  337. }
  338. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  339. }
  340. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  341. $sql = "SELECT '".$db->escape($langs->trans("TotalExpenseReport"))."' AS label,";
  342. for ($i = 1; $i <= 12; $i++) {
  343. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  344. if ($j > 12) {
  345. $j -= 12;
  346. }
  347. $sql .= " SUM(".$db->ifsql("MONTH(er.date_create)=".$j, "erd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  348. }
  349. $sql .= " SUM(erd.total_ht) as total";
  350. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
  351. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
  352. $sql .= " WHERE er.date_debut >= '".$db->idate($search_date_start)."'";
  353. $sql .= " AND er.date_debut <= '".$db->idate($search_date_end)."'";
  354. // Define begin binding date
  355. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  356. $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  357. }
  358. $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
  359. $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
  360. dol_syslog('htdocs/accountancy/expensereport/index.php');
  361. $resql = $db->query($sql);
  362. if ($resql) {
  363. $num = $db->num_rows($resql);
  364. while ($row = $db->fetch_row($resql)) {
  365. print '<tr><td>'.$row[0].'</td>';
  366. for ($i = 1; $i <= 12; $i++) {
  367. print '<td class="right nowraponall amount">'.price($row[$i]).'</td>';
  368. }
  369. print '<td class="right nowraponall amount"><b>'.price($row[13]).'</b></td>';
  370. print '</tr>';
  371. }
  372. $db->free($resql);
  373. } else {
  374. print $db->lasterror(); // Show last sql error
  375. }
  376. print "</table>\n";
  377. print '</div>';
  378. }
  379. // End of page
  380. llxFooter();
  381. $db->close();