categories_list.php 31 KB

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