categories_list.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. <?php
  2. /* Copyright (C) 2004-2023 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011-2024 Alexandre Spangaro <aspangaro@easya.solutions>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/accountancy/admin/categories_list.php
  20. * \ingroup setup
  21. * \brief Page to administer data tables
  22. */
  23. // Load Dolibarr environment
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
  33. // Load translation files required by the page
  34. $langs->loadLangs(array("errors", "admin", "companies", "resource", "holiday", "accountancy", "hrm"));
  35. $action = GETPOST('action', 'aZ09') ? GETPOST('action', 'aZ09') : 'view';
  36. $confirm = GETPOST('confirm', 'alpha');
  37. $id = 32;
  38. $rowid = GETPOST('rowid', 'alpha');
  39. $code = GETPOST('code', 'alpha');
  40. // Security access
  41. if (!$user->hasRight('accounting', 'chartofaccount')) {
  42. accessforbidden();
  43. }
  44. $acts[0] = "activate";
  45. $acts[1] = "disable";
  46. $actl[0] = img_picto($langs->trans("Disabled"), 'switch_off', 'class="size15x"');
  47. $actl[1] = img_picto($langs->trans("Activated"), 'switch_on', 'class="size15x"');
  48. $listoffset = GETPOST('listoffset', 'alpha');
  49. $listlimit = GETPOST('listlimit', 'int') > 0 ? GETPOST('listlimit', 'int') : 1000;
  50. $sortfield = GETPOST("sortfield", 'aZ09comma');
  51. $sortorder = GETPOST("sortorder", 'aZ09comma');
  52. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  53. if (empty($page) || $page < 0 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
  54. // If $page is not defined, or '' or -1 or if we click on clear filters
  55. $page = 0;
  56. }
  57. $offset = $listlimit * $page;
  58. $pageprev = $page - 1;
  59. $pagenext = $page + 1;
  60. $search_country_id = GETPOST('search_country_id', 'int');
  61. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  62. $hookmanager->initHooks(array('admin'));
  63. // This page is a generic page to edit dictionaries
  64. // Put here declaration of dictionaries properties
  65. // Sort order to show dictionary (0 is space). All other dictionaries (added by modules) will be at end of this.
  66. $taborder = array(32);
  67. // Name of SQL tables of dictionaries
  68. $tabname = array();
  69. $tabname[32] = MAIN_DB_PREFIX."c_accounting_category";
  70. // Dictionary labels
  71. $tablib = array();
  72. $tablib[32] = "DictionaryAccountancyCategory";
  73. // Requests to extract data
  74. $tabsql = array();
  75. $tabsql[32] = "SELECT a.rowid as rowid, a.code as code, a.label, a.range_account, a.category_type, a.formula, a.position as position, a.fk_country as country_id, c.code as country_code, c.label as country, a.active FROM ".MAIN_DB_PREFIX."c_accounting_category as a, ".MAIN_DB_PREFIX."c_country as c WHERE a.fk_country=c.rowid and c.active=1";
  76. // Criteria to sort dictionaries
  77. $tabsqlsort = array();
  78. $tabsqlsort[32] = "position ASC";
  79. // Name of the fields in the result of select to display the dictionary
  80. $tabfield = array();
  81. $tabfield[32] = "code,label,range_account,category_type,formula,position,country";
  82. // Name of editing fields for record modification
  83. $tabfieldvalue = array();
  84. $tabfieldvalue[32] = "code,label,range_account,category_type,formula,position,country_id";
  85. // Name of the fields in the table for inserting a record
  86. $tabfieldinsert = array();
  87. $tabfieldinsert[32] = "code,label,range_account,category_type,formula,position,fk_country";
  88. // Name of the rowid if the field is not of type autoincrement
  89. // Example: "" if id field is "rowid" and has autoincrement on
  90. // "nameoffield" if id field is not "rowid" or has not autoincrement on
  91. $tabrowid = array();
  92. $tabrowid[32] = "";
  93. // Condition to show dictionary in setup page
  94. $tabcond = array();
  95. $tabcond[32] = isModEnabled('accounting');
  96. // List of help for fields
  97. $tabhelp = array();
  98. $tabhelp[32] = array('code'=>$langs->trans("EnterAnyCode"), 'category_type'=>$langs->trans("SetToYesIfGroupIsComputationOfOtherGroups"), 'formula'=>$langs->trans("EnterCalculationRuleIfPreviousFieldIsYes"));
  99. // List of check for fields (NOT USED YET)
  100. $tabfieldcheck = array();
  101. $tabfieldcheck[32] = array();
  102. // Complete all arrays with entries found into modules
  103. complete_dictionary_with_modules($taborder, $tabname, $tablib, $tabsql, $tabsqlsort, $tabfield, $tabfieldvalue, $tabfieldinsert, $tabrowid, $tabcond, $tabhelp, $tabfieldcheck);
  104. $accountingcategory = new AccountancyCategory($db);
  105. /*
  106. * Actions
  107. */
  108. if (GETPOST('button_removefilter', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter_x', 'alpha')) {
  109. $search_country_id = '';
  110. }
  111. // Actions add or modify an entry into a dictionary
  112. if (GETPOST('actionadd', 'alpha') || GETPOST('actionmodify', 'alpha')) {
  113. $listfield = explode(',', str_replace(' ', '', $tabfield[$id]));
  114. $listfieldinsert = explode(',', $tabfieldinsert[$id]);
  115. $listfieldmodify = explode(',', $tabfieldinsert[$id]);
  116. $listfieldvalue = explode(',', $tabfieldvalue[$id]);
  117. // Check that all fields are filled
  118. $ok = 1;
  119. foreach ($listfield as $f => $value) {
  120. if ($value == 'formula' && !GETPOST('formula')) {
  121. continue;
  122. }
  123. if ($value == 'range_account' && !GETPOST('range_account')) {
  124. continue;
  125. }
  126. if (($value == 'country' || $value == 'country_id') && GETPOST('country_id')) {
  127. continue;
  128. }
  129. if (!GETPOSTISSET($value) || GETPOST($value) == '') {
  130. $ok = 0;
  131. $fieldnamekey = $listfield[$f];
  132. // We take translate key of field
  133. if ($fieldnamekey == 'libelle' || ($fieldnamekey == 'label')) {
  134. $fieldnamekey = 'Label';
  135. }
  136. if ($fieldnamekey == 'code') {
  137. $fieldnamekey = 'Code';
  138. }
  139. if ($fieldnamekey == 'note') {
  140. $fieldnamekey = 'Note';
  141. }
  142. if ($fieldnamekey == 'type') {
  143. $fieldnamekey = 'Type';
  144. }
  145. if ($fieldnamekey == 'position') {
  146. $fieldnamekey = 'Position';
  147. }
  148. if ($fieldnamekey == 'category_type') {
  149. $fieldnamekey = 'Calculated';
  150. }
  151. if ($fieldnamekey == 'country') {
  152. $fieldnamekey = 'Country';
  153. }
  154. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities($fieldnamekey)), null, 'errors');
  155. }
  156. }
  157. if (GETPOSTISSET("code")) {
  158. if (GETPOST("code") == '0') {
  159. $ok = 0;
  160. setEventMessages($langs->transnoentities('ErrorCodeCantContainZero'), null, 'errors');
  161. }
  162. }
  163. if (GETPOST('position') && !is_numeric(GETPOST('position', 'alpha'))) {
  164. $langs->loadLangs(array("errors"));
  165. $ok = 0;
  166. setEventMessages($langs->transnoentities('ErrorFieldMustBeANumeric', $langs->transnoentities("Position")), null, 'errors');
  167. }
  168. // Si verif ok et action add, on ajoute la ligne
  169. if ($ok && GETPOST('actionadd', 'alpha')) {
  170. if ($tabrowid[$id]) {
  171. // Get free id for insert
  172. $newid = 0;
  173. $sql = "SELECT MAX(".$tabrowid[$id].") newid from ".$tabname[$id];
  174. $result = $db->query($sql);
  175. if ($result) {
  176. $obj = $db->fetch_object($result);
  177. $newid = ($obj->newid + 1);
  178. } else {
  179. dol_print_error($db);
  180. }
  181. }
  182. // Add new entry
  183. $sql = "INSERT INTO ".$tabname[$id]." (";
  184. // List of fields
  185. if ($tabrowid[$id] && !in_array($tabrowid[$id], $listfieldinsert)) {
  186. $sql .= $tabrowid[$id].",";
  187. }
  188. $sql .= $tabfieldinsert[$id];
  189. $sql .= ",active)";
  190. $sql .= " VALUES(";
  191. // List of values
  192. if ($tabrowid[$id] && !in_array($tabrowid[$id], $listfieldinsert)) {
  193. $sql .= $newid.",";
  194. }
  195. $i = 0;
  196. foreach ($listfieldinsert as $f => $value) {
  197. if ($value == 'entity') {
  198. $_POST[$listfieldvalue[$i]] = $conf->entity;
  199. }
  200. if ($i) {
  201. $sql .= ",";
  202. }
  203. if (GETPOST($listfieldvalue[$i]) == '' && !$listfieldvalue[$i] == 'formula') {
  204. $sql .= "null"; // For vat, we want/accept code = ''
  205. } else {
  206. $sql .= "'".$db->escape(GETPOST($listfieldvalue[$i]))."'";
  207. }
  208. $i++;
  209. }
  210. $sql .= ",1)";
  211. dol_syslog("actionadd", LOG_DEBUG);
  212. $result = $db->query($sql);
  213. if ($result) { // Add is ok
  214. setEventMessages($langs->transnoentities("RecordSaved"), null, 'mesgs');
  215. $_POST = array('id'=>$id); // Clean $_POST array, we keep only
  216. } else {
  217. if ($db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  218. setEventMessages($langs->transnoentities("ErrorRecordAlreadyExists"), null, 'errors');
  219. } else {
  220. dol_print_error($db);
  221. }
  222. }
  223. }
  224. // If check ok and action modify, we modify the line
  225. if ($ok && GETPOST('actionmodify', 'alpha')) {
  226. if ($tabrowid[$id]) {
  227. $rowidcol = $tabrowid[$id];
  228. } else {
  229. $rowidcol = "rowid";
  230. }
  231. // Modify entry
  232. $sql = "UPDATE ".$tabname[$id]." SET ";
  233. // Modifie valeur des champs
  234. if ($tabrowid[$id] && !in_array($tabrowid[$id], $listfieldmodify)) {
  235. $sql .= $tabrowid[$id]."=";
  236. $sql .= "'".$db->escape($rowid)."', ";
  237. }
  238. $i = 0;
  239. foreach ($listfieldmodify as $field) {
  240. if ($field == 'fk_country' && GETPOST('country') > 0) {
  241. $_POST[$listfieldvalue[$i]] = GETPOST('country');
  242. } elseif ($field == 'entity') {
  243. $_POST[$listfieldvalue[$i]] = $conf->entity;
  244. }
  245. if ($i) {
  246. $sql .= ",";
  247. }
  248. $sql .= $field."=";
  249. if (GETPOST($listfieldvalue[$i]) == '' && !$listfieldvalue[$i] == 'range_account') {
  250. $sql .= "null"; // For range_account, we want/accept code = ''
  251. } else {
  252. $sql .= "'".$db->escape(GETPOST($listfieldvalue[$i]))."'";
  253. }
  254. $i++;
  255. }
  256. $sql .= " WHERE ".$rowidcol." = ".((int) $rowid);
  257. dol_syslog("actionmodify", LOG_DEBUG);
  258. //print $sql;
  259. $resql = $db->query($sql);
  260. if (!$resql) {
  261. setEventMessages($db->error(), null, 'errors');
  262. }
  263. }
  264. //$_GET["id"]=GETPOST('id', 'int'); // Force affichage dictionnaire en cours d'edition
  265. }
  266. if (GETPOST('actioncancel', 'alpha')) {
  267. //$_GET["id"]=GETPOST('id', 'int'); // Force affichage dictionnaire en cours d'edition
  268. }
  269. if ($action == 'confirm_delete' && $confirm == 'yes') { // delete
  270. if ($tabrowid[$id]) {
  271. $rowidcol = $tabrowid[$id];
  272. } else {
  273. $rowidcol = "rowid";
  274. }
  275. $sql = "DELETE from ".$tabname[$id]." WHERE ".$rowidcol." = ".((int) $rowid);
  276. dol_syslog("delete", LOG_DEBUG);
  277. $result = $db->query($sql);
  278. if (!$result) {
  279. if ($db->errno() == 'DB_ERROR_CHILD_EXISTS') {
  280. setEventMessages($langs->transnoentities("ErrorRecordIsUsedByChild"), null, 'errors');
  281. } else {
  282. dol_print_error($db);
  283. }
  284. }
  285. }
  286. // activate
  287. if ($action == $acts[0]) {
  288. if ($tabrowid[$id]) {
  289. $rowidcol = $tabrowid[$id];
  290. } else {
  291. $rowidcol = "rowid";
  292. }
  293. if ($rowid) {
  294. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE ".$rowidcol." = ".((int) $rowid);
  295. } elseif ($code) {
  296. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE code = '".$db->escape($code)."'";
  297. }
  298. $result = $db->query($sql);
  299. if (!$result) {
  300. dol_print_error($db);
  301. }
  302. }
  303. // disable
  304. if ($action == $acts[1]) {
  305. if ($tabrowid[$id]) {
  306. $rowidcol = $tabrowid[$id];
  307. } else {
  308. $rowidcol = "rowid";
  309. }
  310. if ($rowid) {
  311. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE ".$rowidcol." = ".((int) $rowid);
  312. } elseif ($code) {
  313. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE code = '".$db->escape($code)."'";
  314. }
  315. $result = $db->query($sql);
  316. if (!$result) {
  317. dol_print_error($db);
  318. }
  319. }
  320. // favorite
  321. if ($action == 'activate_favorite') {
  322. if ($tabrowid[$id]) {
  323. $rowidcol = $tabrowid[$id];
  324. } else {
  325. $rowidcol = "rowid";
  326. }
  327. if ($rowid) {
  328. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE ".$rowidcol." = ".((int) $rowid);
  329. } elseif ($code) {
  330. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE code = '".$db->escape($code)."'";
  331. }
  332. $result = $db->query($sql);
  333. if (!$result) {
  334. dol_print_error($db);
  335. }
  336. }
  337. // disable favorite
  338. if ($action == 'disable_favorite') {
  339. if ($tabrowid[$id]) {
  340. $rowidcol = $tabrowid[$id];
  341. } else {
  342. $rowidcol = "rowid";
  343. }
  344. if ($rowid) {
  345. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE ".$rowidcol." = ".((int) $rowid);
  346. } elseif ($code) {
  347. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE code = '".$db->escape($code)."'";
  348. }
  349. $result = $db->query($sql);
  350. if (!$result) {
  351. dol_print_error($db);
  352. }
  353. }
  354. /*
  355. * View
  356. */
  357. $form = new Form($db);
  358. $formadmin = new FormAdmin($db);
  359. $help_url = 'EN:Module_Double_Entry_Accounting#Setup|FR:Module_Comptabilit&eacute;_en_Partie_Double#Configuration';
  360. llxHeader('', $langs->trans('DictionaryAccountancyCategory'), $help_url);
  361. $titre = $langs->trans($tablib[$id]);
  362. $linkback = '';
  363. $titlepicto = 'setup';
  364. print load_fiche_titre($titre, $linkback, $titlepicto);
  365. print '<span class="opacitymedium">'.$langs->trans("AccountingAccountGroupsDesc", $langs->transnoentitiesnoconv("ByPersonalizedAccountGroups")).'</span><br><br>';
  366. // Confirmation of the deletion of the line
  367. if ($action == 'delete') {
  368. print $form->formconfirm($_SERVER["PHP_SELF"].'?'.($page ? 'page='.$page.'&' : '').'sortfield='.$sortfield.'&sortorder='.$sortorder.'&rowid='.$rowid.'&code='.$code.'&id='.$id.($search_country_id > 0 ? '&search_country_id='.$search_country_id : ''), $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_delete', '', 0, 1);
  369. }
  370. // Complete search query with sorting criteria
  371. $sql = $tabsql[$id];
  372. if ($search_country_id > 0) {
  373. if (preg_match('/ WHERE /', $sql)) {
  374. $sql .= " AND ";
  375. } else {
  376. $sql .= " WHERE ";
  377. }
  378. $sql .= " (a.fk_country = ".((int) $search_country_id)." OR a.fk_country = 0)";
  379. }
  380. // If sort order is "country", we use country_code instead
  381. if ($sortfield == 'country') {
  382. $sortfield = 'country_code';
  383. }
  384. if (empty($sortfield)) {
  385. $sortfield = 'position';
  386. }
  387. $sql .= $db->order($sortfield, $sortorder);
  388. $sql .= $db->plimit($listlimit + 1, $offset);
  389. $fieldlist = explode(',', $tabfield[$id]);
  390. $param = '&id='.$id;
  391. if ($search_country_id > 0) {
  392. $param .= '&search_country_id='.urlencode($search_country_id);
  393. }
  394. $paramwithsearch = $param;
  395. if ($sortorder) {
  396. $paramwithsearch .= '&sortorder='.urlencode($sortorder);
  397. }
  398. if ($sortfield) {
  399. $paramwithsearch .= '&sortfield='.urlencode($sortfield);
  400. }
  401. if (GETPOST('from', 'alpha')) {
  402. $paramwithsearch .= '&from='.urlencode(GETPOST('from', 'alpha'));
  403. }
  404. if ($listlimit) {
  405. $paramwithsearch .= '&listlimit='.urlencode(GETPOST('listlimit', 'int'));
  406. }
  407. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  408. print '<input type="hidden" name="token" value="'.newToken().'">';
  409. print '<input type="hidden" name="from" value="'.dol_escape_htmltag(GETPOST('from', 'alpha')).'">';
  410. print '<input type="hidden" name="sortfield" value="'.dol_escape_htmltag($sortfield).'">';
  411. print '<input type="hidden" name="sortorder" value="'.dol_escape_htmltag($sortorder).'">';
  412. print '<div class="div-table-responsive-no-min">';
  413. print '<table class="noborder centpercent">';
  414. // Form to add a new line
  415. if ($tabname[$id]) {
  416. $fieldlist = explode(',', $tabfield[$id]);
  417. // Line for title
  418. print '<tr class="liste_titre">';
  419. // Action column
  420. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  421. print '<td></td>';
  422. }
  423. foreach ($fieldlist as $field => $value) {
  424. // Determine le nom du champ par rapport aux noms possibles
  425. // dans les dictionnaires de donnees
  426. $valuetoshow = ucfirst($fieldlist[$field]); // Par defaut
  427. $valuetoshow = $langs->trans($valuetoshow); // try to translate
  428. $class = "left";
  429. if ($fieldlist[$field] == 'type') {
  430. if ($tabname[$id] == MAIN_DB_PREFIX."c_paiement") {
  431. $valuetoshow = $form->textwithtooltip($langs->trans("Type"), $langs->trans("TypePaymentDesc"), 2, 1, img_help(1, ''));
  432. } else {
  433. $valuetoshow = $langs->trans("Type");
  434. }
  435. }
  436. if ($fieldlist[$field] == 'code') {
  437. $valuetoshow = $langs->trans("Code");
  438. $class = 'width75';
  439. }
  440. if ($fieldlist[$field] == 'libelle' || $fieldlist[$field] == 'label') {
  441. $valuetoshow = $langs->trans("Label");
  442. }
  443. if ($fieldlist[$field] == 'libelle_facture') {
  444. $valuetoshow = $langs->trans("LabelOnDocuments");
  445. }
  446. if ($fieldlist[$field] == 'country') {
  447. $valuetoshow = $langs->trans("Country");
  448. }
  449. if ($fieldlist[$field] == 'accountancy_code') {
  450. $valuetoshow = $langs->trans("AccountancyCode");
  451. }
  452. if ($fieldlist[$field] == 'accountancy_code_sell') {
  453. $valuetoshow = $langs->trans("AccountancyCodeSell");
  454. }
  455. if ($fieldlist[$field] == 'accountancy_code_buy') {
  456. $valuetoshow = $langs->trans("AccountancyCodeBuy");
  457. }
  458. if ($fieldlist[$field] == 'pcg_version' || $fieldlist[$field] == 'fk_pcg_version') {
  459. $valuetoshow = $langs->trans("Pcg_version");
  460. }
  461. if ($fieldlist[$field] == 'range_account') {
  462. $valuetoshow = $langs->trans("Comment");
  463. $class = 'width75';
  464. }
  465. if ($fieldlist[$field] == 'category_type') {
  466. $valuetoshow = $langs->trans("Calculated");
  467. }
  468. if ($valuetoshow != '') {
  469. print '<td class="'.$class.'">';
  470. if (!empty($tabhelp[$id][$value]) && preg_match('/^http(s*):/i', $tabhelp[$id][$value])) {
  471. print '<a href="'.$tabhelp[$id][$value].'">'.$valuetoshow.' '.img_help(1, $valuetoshow).'</a>';
  472. } elseif (!empty($tabhelp[$id][$value])) {
  473. print $form->textwithpicto($valuetoshow, $tabhelp[$id][$value]);
  474. } else {
  475. print $valuetoshow;
  476. }
  477. print '</td>';
  478. }
  479. }
  480. print '<td>';
  481. print '<input type="hidden" name="id" value="'.$id.'">';
  482. print '</td>';
  483. print '<td></td>';
  484. // Action column
  485. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  486. print '<td></td>';
  487. }
  488. print '</tr>';
  489. // Line to enter new values
  490. print '<tr class="oddeven nodrag nodrop nohover">';
  491. // Action column
  492. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  493. print '<td></td>';
  494. }
  495. $obj = new stdClass();
  496. // If data was already input, we define them in obj to populate input fields.
  497. if (GETPOST('actionadd', 'alpha')) {
  498. foreach ($fieldlist as $key => $val) {
  499. if (GETPOST($val) != '') {
  500. $obj->$val = GETPOST($val);
  501. }
  502. }
  503. }
  504. $tmpaction = 'create';
  505. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  506. $reshook = $hookmanager->executeHooks('createDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  507. $error = $hookmanager->error;
  508. $errors = $hookmanager->errors;
  509. if (empty($reshook)) {
  510. fieldListAccountingCategories($fieldlist, $obj, $tabname[$id], 'add');
  511. }
  512. print '<td colspan="2" class="right">';
  513. print '<input type="submit" class="button button-add" name="actionadd" value="'.$langs->trans("Add").'">';
  514. print '</td>';
  515. // Action column
  516. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  517. print '<td></td>';
  518. }
  519. print "</tr>";
  520. $colspan = count($fieldlist) + 3;
  521. if ($id == 32) {
  522. $colspan++;
  523. }
  524. }
  525. print '</table>';
  526. print '</div>';
  527. print '<div class="div-table-responsive">';
  528. print '<table class="noborder centpercent">';
  529. // List of available record in database
  530. dol_syslog("htdocs/accountancy/admin/categories_list.php", LOG_DEBUG);
  531. $resql = $db->query($sql);
  532. if ($resql) {
  533. $num = $db->num_rows($resql);
  534. $i = 0;
  535. // There is several pages
  536. if ($num > $listlimit) {
  537. print '<tr class="none"><td class="right" colspan="'.(2 + count($fieldlist)).'">';
  538. print_fleche_navigation($page, $_SERVER["PHP_SELF"], $paramwithsearch, ($num > $listlimit), '<li class="pagination"><span>'.$langs->trans("Page").' '.($page + 1).'</span></li>');
  539. print '</td></tr>';
  540. }
  541. $filterfound = 0;
  542. foreach ($fieldlist as $field => $value) {
  543. $showfield = 1; // By defaut
  544. if ($fieldlist[$field] == 'region_id' || $fieldlist[$field] == 'country_id') {
  545. $showfield = 0;
  546. }
  547. if ($showfield) {
  548. if ($value == 'country') {
  549. $filterfound++;
  550. }
  551. }
  552. }
  553. // Title line with search boxes
  554. print '<tr class="liste_titre liste_titre_add liste_titre_filter">';
  555. // Action column
  556. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  557. print '<td class="liste_titre center">';
  558. if ($filterfound) {
  559. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  560. print $searchpicto;
  561. }
  562. print '</td>';
  563. }
  564. $filterfound = 0;
  565. foreach ($fieldlist as $field => $value) {
  566. $showfield = 1; // By defaut
  567. if ($fieldlist[$field] == 'region_id' || $fieldlist[$field] == 'country_id') {
  568. $showfield = 0;
  569. }
  570. if ($showfield) {
  571. if ($value == 'country') {
  572. print '<td class="liste_titre">';
  573. print $form->select_country($search_country_id, 'search_country_id', '', 28, 'maxwidth150 maxwidthonsmartphone');
  574. print '</td>';
  575. $filterfound++;
  576. } else {
  577. print '<td class="liste_titre"></td>';
  578. }
  579. }
  580. }
  581. print '<td class="liste_titre"></td>';
  582. print '<td class="liste_titre"></td>';
  583. // Action column
  584. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  585. print '<td class="liste_titre center">';
  586. if ($filterfound) {
  587. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  588. print $searchpicto;
  589. }
  590. print '</td>';
  591. }
  592. print '</tr>';
  593. // Title of lines
  594. print '<tr class="liste_titre">';
  595. // Action column
  596. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  597. print getTitleFieldOfList('');
  598. }
  599. foreach ($fieldlist as $field => $value) {
  600. // Determines the name of the field in relation to the possible names
  601. // in data dictionaries
  602. $showfield = 1; // By default
  603. $class = "left";
  604. $sortable = 1;
  605. $valuetoshow = '';
  606. $valuetoshow = ucfirst($fieldlist[$field]); // By default
  607. $valuetoshow = $langs->trans($valuetoshow); // try to translate
  608. if ($fieldlist[$field] == 'source') {
  609. $valuetoshow = $langs->trans("Contact");
  610. }
  611. if ($fieldlist[$field] == 'price') {
  612. $valuetoshow = $langs->trans("PriceUHT");
  613. }
  614. if ($fieldlist[$field] == 'taux') {
  615. if ($tabname[$id] != MAIN_DB_PREFIX."c_revenuestamp") {
  616. $valuetoshow = $langs->trans("Rate");
  617. } else {
  618. $valuetoshow = $langs->trans("Amount");
  619. }
  620. $class = 'center';
  621. }
  622. if ($fieldlist[$field] == 'type') {
  623. $valuetoshow = $langs->trans("Type");
  624. }
  625. if ($fieldlist[$field] == 'code') {
  626. $valuetoshow = $langs->trans("Code");
  627. }
  628. if ($fieldlist[$field] == 'libelle' || $fieldlist[$field] == 'label') {
  629. $valuetoshow = $langs->trans("Label");
  630. }
  631. if ($fieldlist[$field] == 'country') {
  632. $valuetoshow = $langs->trans("Country");
  633. }
  634. if ($fieldlist[$field] == 'region_id' || $fieldlist[$field] == 'country_id') {
  635. $showfield = 0;
  636. }
  637. if ($fieldlist[$field] == 'accountancy_code') {
  638. $valuetoshow = $langs->trans("AccountancyCode");
  639. }
  640. if ($fieldlist[$field] == 'accountancy_code_sell') {
  641. $valuetoshow = $langs->trans("AccountancyCodeSell");
  642. $sortable = 0;
  643. }
  644. if ($fieldlist[$field] == 'accountancy_code_buy') {
  645. $valuetoshow = $langs->trans("AccountancyCodeBuy");
  646. $sortable = 0;
  647. }
  648. if ($fieldlist[$field] == 'fk_pcg_version') {
  649. $valuetoshow = $langs->trans("Pcg_version");
  650. }
  651. if ($fieldlist[$field] == 'account_parent') {
  652. $valuetoshow = $langs->trans("Accountsparent");
  653. }
  654. if ($fieldlist[$field] == 'pcg_type') {
  655. $valuetoshow = $langs->trans("Pcg_type");
  656. }
  657. if ($fieldlist[$field] == 'type_template') {
  658. $valuetoshow = $langs->trans("TypeOfTemplate");
  659. }
  660. if ($fieldlist[$field] == 'range_account') {
  661. $valuetoshow = $langs->trans("Comment");
  662. }
  663. if ($fieldlist[$field] == 'category_type') {
  664. $valuetoshow = $langs->trans("Calculated");
  665. }
  666. // Affiche nom du champ
  667. if ($showfield) {
  668. print getTitleFieldOfList($valuetoshow, 0, $_SERVER["PHP_SELF"], ($sortable ? $fieldlist[$field] : ''), ($page ? 'page='.$page.'&' : ''), $param, "", $sortfield, $sortorder, $class.' ');
  669. }
  670. }
  671. print getTitleFieldOfList($langs->trans("ListOfAccounts"), 0, $_SERVER["PHP_SELF"], "", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, '');
  672. print getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], "active", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, 'center ');
  673. // Action column
  674. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  675. print getTitleFieldOfList('');
  676. }
  677. print '</tr>';
  678. if ($num) {
  679. $imaxinloop = ($listlimit ? min($num, $listlimit) : $num);
  680. // Lines with values
  681. while ($i < $imaxinloop) {
  682. $obj = $db->fetch_object($resql);
  683. //print_r($obj);
  684. print '<tr class="oddeven" id="rowid-'.$obj->rowid.'">';
  685. if ($action == 'edit' && ($rowid == (!empty($obj->rowid) ? $obj->rowid : $obj->code))) {
  686. $tmpaction = 'edit';
  687. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  688. $reshook = $hookmanager->executeHooks('editDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  689. $error = $hookmanager->error;
  690. $errors = $hookmanager->errors;
  691. // Actions
  692. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  693. print '<td></td>';
  694. }
  695. // Show fields
  696. if (empty($reshook)) {
  697. fieldListAccountingCategories($fieldlist, $obj, $tabname[$id], 'edit');
  698. }
  699. print '<td></td>';
  700. print '<td class="center">';
  701. print '<div name="'.(!empty($obj->rowid) ? $obj->rowid : $obj->code).'"></div>';
  702. print '<input type="hidden" name="page" value="'.$page.'">';
  703. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  704. print '<input type="submit" class="button button-edit smallpaddingimp" name="actionmodify" value="'.$langs->trans("Modify").'">';
  705. print '<input type="submit" class="button button-cancel smallpaddingimp" name="actioncancel" value="'.$langs->trans("Cancel").'">';
  706. print '</td>';
  707. // Actions
  708. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  709. print '<td></td>';
  710. }
  711. } else {
  712. // Can an entry be erased or disabled ?
  713. $iserasable = 1;
  714. $canbedisabled = 1;
  715. $canbemodified = 1; // true by default
  716. if (isset($obj->code)) {
  717. if (($obj->code == '0' || $obj->code == '' || preg_match('/unknown/i', $obj->code))) {
  718. $iserasable = 0;
  719. $canbedisabled = 0;
  720. }
  721. }
  722. $url = $_SERVER["PHP_SELF"].'?'.($page ? 'page='.$page.'&' : '').'sortfield='.$sortfield.'&sortorder='.$sortorder.'&rowid='.(!empty($obj->rowid) ? $obj->rowid : (!empty($obj->code) ? $obj->code : '')).'&code='.(!empty($obj->code) ? urlencode($obj->code) : '');
  723. if ($param) {
  724. $url .= '&'.$param;
  725. }
  726. $url .= '&';
  727. $canbemodified = $iserasable;
  728. $tmpaction = 'view';
  729. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  730. $reshook = $hookmanager->executeHooks('viewDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  731. $error = $hookmanager->error;
  732. $errors = $hookmanager->errors;
  733. // Actions
  734. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  735. print '<td class="center">';
  736. if ($canbemodified) {
  737. print '<a class="reposition editfielda marginleftonly marginrightonly" href="'.$url.'action=edit&token='.newToken().'">'.img_edit().'</a>';
  738. }
  739. if ($iserasable) {
  740. if ($user->admin) {
  741. print '<a class="marginleftonly marginrightonly" href="'.$url.'action=delete&token='.newToken().'">'.img_delete().'</a>';
  742. }
  743. }
  744. print '</td>';
  745. }
  746. if (empty($reshook)) {
  747. foreach ($fieldlist as $field => $value) {
  748. $showfield = 1;
  749. $title = '';
  750. $class = 'tddict';
  751. $tmpvar = $fieldlist[$field];
  752. $valuetoshow = $obj->$tmpvar;
  753. if ($value == 'category_type') {
  754. $valuetoshow = yn($valuetoshow);
  755. } elseif ($valuetoshow == 'all') {
  756. $valuetoshow = $langs->trans('All');
  757. } elseif ($fieldlist[$field] == 'country') {
  758. if (empty($obj->country_code)) {
  759. $valuetoshow = '-';
  760. } else {
  761. $key = $langs->trans("Country".strtoupper($obj->country_code));
  762. $valuetoshow = ($key != "Country".strtoupper($obj->country_code) ? $obj->country_code." - ".$key : $obj->country);
  763. }
  764. } elseif (in_array($fieldlist[$field], array('label', 'formula'))) {
  765. $class = "tdoverflowmax250";
  766. $title = $valuetoshow;
  767. } elseif (in_array($fieldlist[$field], array('range_account'))) {
  768. $class = "tdoverflowmax250 small";
  769. $title = $valuetoshow;
  770. } elseif ($fieldlist[$field] == 'region_id' || $fieldlist[$field] == 'country_id') {
  771. $showfield = 0;
  772. }
  773. // Show value for field
  774. if ($showfield) {
  775. print '<!-- '.$fieldlist[$field].' --><td class="'.$class.'"'.($title ? ' title="'.dol_escape_htmltag($title).'"' : '').'>'.dol_escape_htmltag($valuetoshow).'</td>';
  776. }
  777. }
  778. }
  779. // Link to setup the group
  780. print '<td>';
  781. if (empty($obj->formula)) {
  782. // Count number of accounts into group
  783. $nbofaccountintogroup = 0;
  784. $listofaccountintogroup = $accountingcategory->getCptsCat($obj->rowid);
  785. $nbofaccountintogroup = count($listofaccountintogroup);
  786. print '<a href="'.DOL_URL_ROOT.'/accountancy/admin/categories.php?action=display&save_lastsearch_values=1&account_category='.$obj->rowid.'">';
  787. print $langs->trans("NAccounts", $nbofaccountintogroup);
  788. print '</a>';
  789. } else {
  790. print '<span class="opacitymedium">'.$langs->trans("Formula").'</span>';
  791. }
  792. print '</td>';
  793. // Active
  794. print '<td class="center" class="nowrap">';
  795. if ($canbedisabled) {
  796. print '<a href="'.$url.'action='.$acts[$obj->active].'">'.$actl[$obj->active].'</a>';
  797. } else {
  798. print $langs->trans("AlwaysActive");
  799. }
  800. print "</td>";
  801. // Actions
  802. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  803. print '<td class="center">';
  804. if ($canbemodified) {
  805. print '<a class="reposition editfielda paddingleft marginleftonly marginrightonly paddingright" href="'.$url.'action=edit&token='.newToken().'">'.img_edit().'</a>';
  806. }
  807. if ($iserasable) {
  808. if ($user->admin) {
  809. print '<a class="paddingleft marginleftonly marginrightonly paddingright" href="'.$url.'action=delete&token='.newToken().'">'.img_delete().'</a>';
  810. }
  811. }
  812. print '</td>';
  813. }
  814. }
  815. print "</tr>\n";
  816. $i++;
  817. }
  818. } else {
  819. $colspan = 10;
  820. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  821. }
  822. } else {
  823. dol_print_error($db);
  824. }
  825. print '</table>';
  826. print '</div>';
  827. print '</form>';
  828. print '<br>';
  829. // End of page
  830. llxFooter();
  831. $db->close();
  832. /**
  833. * Show fields in insert/edit mode
  834. *
  835. * @param array $fieldlist Array of fields
  836. * @param Object $obj If we show a particular record, obj is filled with record fields
  837. * @param string $tabname Name of SQL table
  838. * @param string $context 'add'=Output field for the "add form", 'edit'=Output field for the "edit form", 'hide'=Output field for the "add form" but we dont want it to be rendered
  839. * @return void
  840. */
  841. function fieldListAccountingCategories($fieldlist, $obj = null, $tabname = '', $context = '')
  842. {
  843. global $conf, $langs, $db;
  844. global $form, $mysoc;
  845. $formadmin = new FormAdmin($db);
  846. $formcompany = new FormCompany($db);
  847. if (isModEnabled('accounting')) {
  848. $formaccounting = new FormAccounting($db);
  849. }
  850. foreach ($fieldlist as $field => $value) {
  851. if ($fieldlist[$field] == 'country') {
  852. print '<td>';
  853. $fieldname = 'country';
  854. if ($context == 'add') {
  855. $fieldname = 'country_id';
  856. $preselectcountrycode = GETPOSTISSET('country_id') ? GETPOST('country_id', 'int') : $mysoc->country_code;
  857. print $form->select_country($preselectcountrycode, $fieldname, '', 28, 'maxwidth150 maxwidthonsmartphone');
  858. } else {
  859. $preselectcountrycode = (empty($obj->country_code) ? (empty($obj->country) ? $mysoc->country_code : $obj->country) : $obj->country_code);
  860. print $form->select_country($preselectcountrycode, $fieldname, '', 28, 'maxwidth150 maxwidthonsmartphone');
  861. }
  862. print '</td>';
  863. } elseif ($fieldlist[$field] == 'country_id') {
  864. if (!in_array('country', $fieldlist)) { // If there is already a field country, we don't show country_id (avoid duplicate)
  865. $country_id = (!empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : 0);
  866. print '<td>';
  867. print '<input type="hidden" name="'.$fieldlist[$field].'" value="'.$country_id.'">';
  868. print '</td>';
  869. }
  870. } elseif ($fieldlist[$field] == 'category_type') {
  871. print '<td>';
  872. print $form->selectyesno($fieldlist[$field], (!empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : ''), 1);
  873. print '</td>';
  874. } elseif ($fieldlist[$field] == 'code' && isset($obj->{$fieldlist[$field]})) {
  875. print '<td><input type="text" class="flat minwidth100" value="'.(!empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : '').'" name="'.$fieldlist[$field].'"></td>';
  876. } else {
  877. print '<td>';
  878. $class = '';
  879. if (in_array($fieldlist[$field], array('code', 'formula'))) {
  880. $class = 'maxwidth75';
  881. }
  882. if (in_array($fieldlist[$field], array('label', 'range_account'))) {
  883. $class = 'maxwidth150';
  884. }
  885. if ($fieldlist[$field] == 'position') {
  886. $class = 'maxwidth50';
  887. }
  888. print '<input type="text" class="flat'.($class ? ' '.$class : '').'" value="'.(isset($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : '').'" name="'.$fieldlist[$field].'">';
  889. print '</td>';
  890. }
  891. }
  892. }