categories_list.php 35 KB

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