listbyaccount.php 17 KB

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