listbyaccount.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /* Copyright (C) 2016 Neil Orley <neil.orley@oeris.fr>
  3. * Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  4. * Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2013-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  6. * Copyright (C) 2018 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/accountancy/bookkeeping/listbyaccount.php
  23. * \ingroup Accountancy (Double entries)
  24. * \brief List operation of book keeping ordered by account number
  25. */
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. // Load translation files required by the page
  34. $langs->loadLangs(array("accountancy"));
  35. $action = GETPOST('action', 'alpha');
  36. $search_date_start = dol_mktime(0, 0, 0, GETPOST('search_date_startmonth', 'int'), GETPOST('search_date_startday', 'int'), GETPOST('search_date_startyear', 'int'));
  37. $search_date_end = dol_mktime(0, 0, 0, GETPOST('search_date_endmonth', 'int'), GETPOST('search_date_endday', 'int'), GETPOST('search_date_endyear', 'int'));
  38. $search_doc_date = dol_mktime(0, 0, 0, GETPOST('doc_datemonth', 'int'), GETPOST('doc_dateday', 'int'), GETPOST('doc_dateyear', 'int'));
  39. $search_accountancy_code = GETPOST("search_accountancy_code");
  40. $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
  41. if ($search_accountancy_code_start == - 1) {
  42. $search_accountancy_code_start = '';
  43. }
  44. $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
  45. if ($search_accountancy_code_end == - 1) {
  46. $search_accountancy_code_end = '';
  47. }
  48. $search_doc_ref = GETPOST('search_doc_ref', 'alpha');
  49. $search_label_operation = GETPOST('search_label_operation', 'alpha');
  50. $search_direction = GETPOST('search_direction', 'alpha');
  51. $search_ledger_code = GETPOST('search_ledger_code', 'alpha');
  52. $search_debit = GETPOST('search_debit', 'alpha');
  53. $search_credit = GETPOST('search_credit', 'alpha');
  54. // Load variable for pagination
  55. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : (empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION) ? $conf->liste_limit : $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  56. $sortfield = GETPOST('sortfield', 'alpha');
  57. $sortorder = GETPOST('sortorder', 'alpha');
  58. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  59. if (empty($page) || $page < 0) { $page = 0; }
  60. $offset = $limit * $page;
  61. $pageprev = $page - 1;
  62. $pagenext = $page + 1;
  63. if ($sortorder == "") $sortorder = "ASC";
  64. if ($sortfield == "") $sortfield = "t.doc_date,t.rowid";
  65. if (empty($search_date_start) && empty($search_date_end) && GETPOSTISSET('search_date_startday') && GETPOSTISSET('search_date_startmonth') && GETPOSTISSET('search_date_starthour')) {
  66. $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear ";
  67. $sql .= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'";
  68. $sql .= $db->plimit(1);
  69. $res = $db->query($sql);
  70. if ($res->num_rows > 0) {
  71. $fiscalYear = $db->fetch_object($res);
  72. $search_date_start = strtotime($fiscalYear->date_start);
  73. $search_date_end = strtotime($fiscalYear->date_end);
  74. } else {
  75. $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  76. $year_start = dol_print_date(dol_now(), '%Y');
  77. if (dol_print_date(dol_now(), '%m') < $month_start) $year_start--; // If current month is lower that starting fiscal month, we start last year
  78. $year_end = $year_start + 1;
  79. $month_end = $month_start - 1;
  80. if ($month_end < 1)
  81. {
  82. $month_end = 12;
  83. $year_end--;
  84. }
  85. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  86. $search_date_end = dol_get_last_day($year_end, $month_end);
  87. }
  88. }
  89. $object = new BookKeeping($db);
  90. /*
  91. * Action
  92. */
  93. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) // All tests are required to be compatible with all browsers
  94. {
  95. $search_doc_date = '';
  96. $search_accountancy_code = '';
  97. $search_accountancy_code_start = '';
  98. $search_accountancy_code_end = '';
  99. $search_label_account = '';
  100. $search_doc_ref = '';
  101. $search_label_operation = '';
  102. $search_direction = '';
  103. $search_ledger_code = '';
  104. $search_date_start = '';
  105. $search_date_end = '';
  106. $search_date_startyear = '';
  107. $search_date_startmonth = '';
  108. $search_date_startday = '';
  109. $search_date_endyear = '';
  110. $search_date_endmonth = '';
  111. $search_date_endday = '';
  112. $search_debit = '';
  113. $search_credit = '';
  114. }
  115. // Must be after the remove filter action, before the export.
  116. $param = '';
  117. $filter = array();
  118. if (!empty($search_date_start)) {
  119. $filter['t.doc_date>='] = $search_date_start;
  120. $param .= '&search_date_startmonth='.GETPOST('search_date_startmonth', 'int').'&search_date_startday='.GETPOST('search_date_startday', 'int').'&search_date_startyear='.GETPOST('search_date_startyear', 'int');
  121. }
  122. if (!empty($search_date_end)) {
  123. $filter['t.doc_date<='] = $search_date_end;
  124. $param .= '&search_date_endmonth='.GETPOST('search_date_endmonth', 'int').'&search_date_endday='.GETPOST('search_date_endday', 'int').'&search_date_endyear='.GETPOST('search_date_endyear', 'int');
  125. }
  126. if (!empty($search_doc_date)) {
  127. $filter['t.doc_date'] = $search_doc_date;
  128. $param .= '&doc_datemonth='.GETPOST('doc_datemonth', 'int').'&doc_dateday='.GETPOST('doc_dateday', 'int').'&doc_dateyear='.GETPOST('doc_dateyear', 'int');
  129. }
  130. if (!empty($search_accountancy_code_start)) {
  131. $filter['t.numero_compte>='] = $search_accountancy_code_start;
  132. $param .= '&search_accountancy_code_start='.urlencode($search_accountancy_code_start);
  133. }
  134. if (!empty($search_accountancy_code_end)) {
  135. $filter['t.numero_compte<='] = $search_accountancy_code_end;
  136. $param .= '&search_accountancy_code_end='.urlencode($search_accountancy_code_end);
  137. }
  138. if (!empty($search_label_account)) {
  139. $filter['t.label_compte'] = $search_label_account;
  140. $param .= '&search_label_compte='.urlencode($search_label_account);
  141. }
  142. if (!empty($search_doc_ref)) {
  143. $filter['t.doc_ref'] = $search_doc_ref;
  144. $param .= '&search_doc_ref='.urlencode($search_doc_ref);
  145. }
  146. if (!empty($search_label_operation)) {
  147. $filter['t.label_operation'] = $search_label_operation;
  148. $param .= '&search_label_operation='.urlencode($search_label_operation);
  149. }
  150. if (!empty($search_direction)) {
  151. $filter['t.sens'] = $search_direction;
  152. $param .= '&search_direction='.urlencode($search_direction);
  153. }
  154. if (!empty($search_ledger_code)) {
  155. $filter['t.code_journal'] = $search_ledger_code;
  156. $param .= '&search_ledger_code='.urlencode($search_ledger_code);
  157. }
  158. if (!empty($search_debit)) {
  159. $filter['t.debit'] = $search_debit;
  160. $param .= '&search_debit='.urlencode($search_debit);
  161. }
  162. if (!empty($search_credit)) {
  163. $filter['t.credit'] = $search_credit;
  164. $param .= '&search_credit='.urlencode($search_credit);
  165. }
  166. if ($action == 'delmouvconfirm') {
  167. $mvt_num = GETPOST('mvt_num', 'int');
  168. if (!empty($mvt_num)) {
  169. $result = $object->deleteMvtNum($mvt_num);
  170. if ($result < 0) {
  171. setEventMessages($object->error, $object->errors, 'errors');
  172. }
  173. Header("Location: listbyaccount.php");
  174. exit();
  175. }
  176. }
  177. /*
  178. * View
  179. */
  180. $formaccounting = new FormAccounting($db);
  181. $formother = new FormOther($db);
  182. $form = new Form($db);
  183. $title_page = $langs->trans("Bookkeeping").' '.strtolower($langs->trans("By")).' '.strtolower($langs->trans("AccountAccounting"));
  184. llxHeader('', $title_page);
  185. // List
  186. $nbtotalofrecords = '';
  187. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  188. $nbtotalofrecords = $object->fetchAllByAccount($sortorder, $sortfield, 0, 0, $filter);
  189. if ($nbtotalofrecords < 0) {
  190. setEventMessages($object->error, $object->errors, 'errors');
  191. }
  192. }
  193. $result = $object->fetchAllByAccount($sortorder, $sortfield, $limit, $offset, $filter);
  194. if ($result < 0) {
  195. setEventMessages($object->error, $object->errors, 'errors');
  196. }
  197. $num = count($object->lines);
  198. if ($action == 'delmouv') {
  199. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?mvt_num='.GETPOST('mvt_num'), $langs->trans('DeleteMvt'), $langs->trans('ConfirmDeleteMvtPartial'), 'delmouvconfirm', '', 0, 1);
  200. print $formconfirm;
  201. }
  202. if ($action == 'delbookkeepingyear') {
  203. $form_question = array();
  204. $delyear = GETPOST('delyear');
  205. if (empty($delyear)) {
  206. $delyear = dol_print_date(dol_now(), '%Y');
  207. }
  208. $year_array = $formaccounting->selectyear_accountancy_bookkepping($delyear, 'delyear', 0, 'array');
  209. $form_question['delyear'] = array(
  210. 'name' => 'delyear',
  211. 'type' => 'select',
  212. 'label' => $langs->trans('DelYear'),
  213. 'values' => $year_array,
  214. 'default' => $delyear
  215. );
  216. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans('DeleteMvt'), $langs->trans('ConfirmDeleteMvt'), 'delbookkeepingyearconfirm', $form_question, 0, 1, 250);
  217. print $formconfirm;
  218. }
  219. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  220. print '<input type="hidden" name="token" value="'.newToken().'">';
  221. print '<input type="hidden" name="action" value="list">';
  222. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  223. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  224. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  225. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  226. print '<input type="hidden" name="page" value="'.$page.'">';
  227. $newcardbutton .= dolGetButtonTitle($langs->trans('ViewFlatList'), '', 'fa fa-list paddingleft', DOL_URL_ROOT.'/accountancy/bookkeeping/list.php?'.$param);
  228. $newcardbutton .= dolGetButtonTitle($langs->trans('NewAccountingMvt'), '', 'fa fa-plus-circle paddingleft', './card.php?action=create');
  229. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.urlencode($contextpage);
  230. if ($limit > 0 && $limit != $conf->liste_limit) $param .= '&limit='.urlencode($limit);
  231. print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $result, $nbtotalofrecords, 'title_accountancy', 0, $viewflat.$newcardbutton, '', $limit);
  232. // Reverse sort order
  233. if (preg_match('/^asc/i', $sortorder)) $sortorder = "asc";
  234. else $sortorder = "desc";
  235. print '<div class="div-table-responsive">';
  236. print '<table class="noborder centpercent">';
  237. print '<tr class="liste_titre">';
  238. print '<td class="liste_titre">';
  239. print '<div class="nowrap">';
  240. print $langs->trans('From').' ';
  241. print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array(), 1, 1, 'maxwidth200');
  242. print '</div>';
  243. print '<div class="nowrap">';
  244. print $langs->trans('to').' ';
  245. print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array(), 1, 1, 'maxwidth200');
  246. print '</div>';
  247. print '</td>';
  248. print '<td class="liste_titre"></td>';
  249. print '<td class="liste_titre center">';
  250. print $langs->trans('From').': ';
  251. print $form->selectDate($search_date_start, 'search_date_start', 0, 0, 1);
  252. print '<br>';
  253. print $langs->trans('to').': ';
  254. print $form->selectDate($search_date_end, 'search_date_end', 0, 0, 1);
  255. print '</td>';
  256. print '<td class="liste_titre"><input type="text" size="7" class="flat" name="search_doc_ref" value="'.dol_escape_htmltag($search_doc_ref).'"/></td>';
  257. print '<td class="liste_titre"><input type="text" size="7" class="flat" name="search_label_operation" value="'.dol_escape_htmltag($search_label_operation).'"/></td>';
  258. print '<td class="liste_titre right"><input type="text" class="flat" name="search_debit" size="4" value="'.dol_escape_htmltag($search_debit).'"></td>';
  259. print '<td class="liste_titre right"><input type="text" class="flat" name="search_credit" size="4" value="'.dol_escape_htmltag($search_credit).'"></td>';
  260. print '<td class="liste_titre center"><input type="text" name="search_ledger_code" size="3" value="'.dol_escape_htmltag($search_ledger_code).'"></td>';
  261. print '<td class="liste_titre right" colspan="2">';
  262. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  263. print $searchpicto;
  264. print '</td>';
  265. print '</tr>';
  266. print '<tr class="liste_titre">';
  267. print_liste_field_titre("AccountAccountingShort", $_SERVER['PHP_SELF']);
  268. print_liste_field_titre("TransactionNumShort", $_SERVER['PHP_SELF'], "t.piece_num", "", $param, '', $sortfield, $sortorder, 'right ');
  269. print_liste_field_titre("Docdate", $_SERVER['PHP_SELF'], "t.doc_date", "", $param, '', $sortfield, $sortorder, 'center ');
  270. print_liste_field_titre("Piece", $_SERVER['PHP_SELF'], "t.doc_ref", "", $param, "", $sortfield, $sortorder);
  271. print_liste_field_titre("Label");
  272. print_liste_field_titre("Debit", $_SERVER['PHP_SELF'], "t.debit", "", $param, '', $sortfield, $sortorder, 'right ');
  273. print_liste_field_titre("Credit", $_SERVER['PHP_SELF'], "t.credit", "", $param, '', $sortfield, $sortorder, 'right ');
  274. print_liste_field_titre("Codejournal", $_SERVER['PHP_SELF'], "t.code_journal", "", $param, '', $sortfield, $sortorder, 'center ');
  275. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $param, "", 'width="60"', $sortfield, $sortorder, 'center ');
  276. print "</tr>\n";
  277. $total_debit = 0;
  278. $total_credit = 0;
  279. $sous_total_debit = 0;
  280. $sous_total_credit = 0;
  281. $displayed_account_number = null; // Start with undefined to be able to distinguish with empty
  282. $i = 0;
  283. while ($i < min($num, $limit))
  284. {
  285. $line = $object->lines[$i];
  286. $total_debit += $line->debit;
  287. $total_credit += $line->credit;
  288. $accountg = length_accountg($line->numero_compte);
  289. //if (empty($accountg)) $accountg = '-';
  290. // Is it a break ?
  291. if ($accountg != $displayed_account_number || !isset($displayed_account_number)) {
  292. // Affiche un Sous-Total par compte comptable
  293. if (isset($displayed_account_number)) {
  294. print '<tr class="liste_total"><td class="right" colspan="5">'.$langs->trans("SubTotal").':</td><td class="nowrap right">'.price($sous_total_debit).'</td><td class="nowrap right">'.price($sous_total_credit).'</td>';
  295. print "<td>&nbsp;</td>\n";
  296. print "<td>&nbsp;</td>\n";
  297. print '</tr>';
  298. }
  299. // Show the break account
  300. $colspan = 9;
  301. print "<tr>";
  302. print '<td colspan="'.$colspan.'" style="font-weight:bold; border-bottom: 1pt solid black;">';
  303. if ($line->numero_compte != "" && $line->numero_compte != '-1') print length_accountg($line->numero_compte).' : '.$object->get_compte_desc($line->numero_compte);
  304. else print '<span class="error">'.$langs->trans("Unknown").'</span>';
  305. print '</td>';
  306. print '</tr>';
  307. $displayed_account_number = $accountg;
  308. //if (empty($displayed_account_number)) $displayed_account_number='-';
  309. $sous_total_debit = 0;
  310. $sous_total_credit = 0;
  311. }
  312. print '<tr class="oddeven">';
  313. print '<td>&nbsp;</td>';
  314. print '<td class="right"><a href="./card.php?piece_num='.$line->piece_num.'">'.$line->piece_num.'</a></td>';
  315. print '<td class="center">'.dol_print_date($line->doc_date, 'day').'</td>';
  316. // TODO Add a link according to doc_type and fk_doc
  317. print '<td class="nowrap">';
  318. //if ($line->doc_type == 'supplier_invoice')
  319. //if ($line->doc_type == 'customer_invoice')
  320. print $line->doc_ref;
  321. print '</td>';
  322. // Affiche un lien vers la facture client/fournisseur
  323. $doc_ref = preg_replace('/\(.*\)/', '', $line->doc_ref);
  324. print strlen(length_accounta($line->subledger_account)) == 0 ? '<td>'.$line->label_operation.'</td>' : '<td>'.$line->label_operation.'<br><span style="font-size:0.8em">('.length_accounta($line->subledger_account).')</span></td>';
  325. print '<td class="nowrap right">'.($line->debit ? price($line->debit) : '').'</td>';
  326. print '<td class="nowrap right">'.($line->credit ? price($line->credit) : '').'</td>';
  327. $accountingjournal = new AccountingJournal($db);
  328. $result = $accountingjournal->fetch('', $line->code_journal);
  329. $journaltoshow = (($result > 0) ? $accountingjournal->getNomUrl(0, 0, 0, '', 0) : $line->code_journal);
  330. print '<td class="center">'.$journaltoshow.'</td>';
  331. print '<td class="center">';
  332. print '<a href="'.DOL_URL_ROOT.'/accountancy/bookkeeping/card.php?piece_num='.$line->piece_num.'">'.img_edit().'</a>&nbsp;';
  333. print '<a href="'.$_SERVER['PHP_SELF'].'?action=delmouv&mvt_num='.$line->piece_num.$param.'&page='.$page.'">'.img_delete().'</a>';
  334. print '</td>';
  335. print "</tr>\n";
  336. // Comptabilise le sous-total
  337. $sous_total_debit += $line->debit;
  338. $sous_total_credit += $line->credit;
  339. $i++;
  340. }
  341. // Show sub-total of last shown account
  342. print '<tr class="liste_total">';
  343. print '<td class="right" colspan="5">'.$langs->trans("SubTotal").':</td><td class="nowrap right">'.price($sous_total_debit).'</td><td class="nowrap right">'.price($sous_total_credit).'</td>';
  344. print '<td class="nowraponall center">';
  345. print price($sous_total_debit - $sous_total_credit);
  346. print '</td>';
  347. print '<td></td>';
  348. print '</tr>';
  349. // Show total
  350. print '<tr class="liste_total">';
  351. print '<td class="right" colspan="5">'.$langs->trans("Total").':</td>';
  352. print '<td class="nowraponall right">';
  353. print price($total_debit);
  354. print '</td>';
  355. print '<td class="nowraponall right">';
  356. print price($total_credit);
  357. print '</td>';
  358. print '<td></td>';
  359. print '<td></td>';
  360. print '</tr>';
  361. print "</table>";
  362. print '</div>';
  363. print '</form>';
  364. // End of page
  365. llxFooter();
  366. $db->close();