categories_list.php 35 KB

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