accountmodel.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2011-2018 Philippe Grand <philippe.grand@atoo-net.com>
  8. * Copyright (C) 2011 Remy Younes <ryounes@gmail.com>
  9. * Copyright (C) 2012-2015 Marcos García <marcosgdf@gmail.com>
  10. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@ltairis.fr>
  11. * Copyright (C) 2011-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
  12. * Copyright (C) 2015 Ferran Marcet <fmarcet@2byte.es>
  13. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. /**
  29. * \file htdocs/accountancy/admin/accountmodel.php
  30. * \ingroup Advanced accountancy
  31. * \brief Page to administer model of chart of accounts
  32. */
  33. require '../../main.inc.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  40. if (! empty($conf->accounting->enabled)) require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  41. // Load translation files required by the page
  42. $langs->loadLangs(array("errors","admin","companies","resource","holiday","compta","accountancy","hrm"));
  43. $action=GETPOST('action','aZ09')?GETPOST('action','aZ09'):'view';
  44. $confirm=GETPOST('confirm','alpha');
  45. $id=31;
  46. $rowid=GETPOST('rowid','alpha');
  47. $code=GETPOST('code','alpha');
  48. $acts[0] = "activate";
  49. $acts[1] = "disable";
  50. $actl[0] = img_picto($langs->trans("Disabled"),'switch_off');
  51. $actl[1] = img_picto($langs->trans("Activated"),'switch_on');
  52. $listoffset=GETPOST('listoffset','alpha');
  53. $listlimit=GETPOST('listlimit','int')>0?GETPOST('listlimit','int'):1000;
  54. $active = 1;
  55. $sortfield = GETPOST("sortfield",'aZ09comma');
  56. $sortorder = GETPOST("sortorder",'aZ09comma');
  57. $page = GETPOST("page",'int');
  58. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  59. $offset = $listlimit * $page ;
  60. $pageprev = $page - 1;
  61. $pagenext = $page + 1;
  62. $search_country_id = GETPOST('search_country_id','int');
  63. // Security check
  64. if ($user->societe_id > 0) accessforbidden();
  65. if (! $user->rights->accounting->chartofaccount) accessforbidden();
  66. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  67. $hookmanager->initHooks(array('admin'));
  68. // This page is a generic page to edit dictionaries
  69. // Put here declaration of dictionaries properties
  70. // Name of SQL tables of dictionaries
  71. $tabname=array();
  72. $tabname[31]= MAIN_DB_PREFIX."accounting_system";
  73. // Dictionary labels
  74. $tablib=array();
  75. $tablib[31]= "Pcg_version";
  76. // Requests to extract data
  77. $tabsql=array();
  78. $tabsql[31]= "SELECT s.rowid as rowid, pcg_version, s.label, s.fk_country as country_id, c.code as country_code, c.label as country, s.active FROM ".MAIN_DB_PREFIX."accounting_system as s, ".MAIN_DB_PREFIX."c_country as c WHERE s.fk_country=c.rowid and c.active=1";
  79. // Criteria to sort dictionaries
  80. $tabsqlsort=array();
  81. $tabsqlsort[31]="pcg_version ASC";
  82. // Nom des champs en resultat de select pour affichage du dictionnaire
  83. $tabfield=array();
  84. $tabfield[31]= "pcg_version,label,country_id,country";
  85. // Nom des champs d'edition pour modification d'un enregistrement
  86. $tabfieldvalue=array();
  87. $tabfieldvalue[31]= "pcg_version,label,country";
  88. // Nom des champs dans la table pour insertion d'un enregistrement
  89. $tabfieldinsert=array();
  90. $tabfieldinsert[31]= "pcg_version,label,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[31]= "";
  96. // Condition to show dictionary in setup page
  97. $tabcond=array();
  98. $tabcond[31]= ! empty($conf->accounting->enabled);
  99. // List of help for fields
  100. $tabhelp=array();
  101. $tabhelp[31] = array('pcg_version'=>$langs->trans("EnterAnyCode"));
  102. // List of check for fields (NOT USED YET)
  103. $tabfieldcheck=array();
  104. $tabfieldcheck[31] = array();
  105. // Define elementList and sourceList (used for dictionary type of contacts "llx_c_type_contact")
  106. $elementList = array();
  107. $sourceList=array();
  108. /*
  109. * Actions
  110. */
  111. if (GETPOST('button_removefilter','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter_x','alpha'))
  112. {
  113. $search_country_id = '';
  114. }
  115. // Actions add or modify an entry into a dictionary
  116. if (GETPOST('actionadd','alpha') || GETPOST('actionmodify','alpha'))
  117. {
  118. $listfield=explode(',', str_replace(' ', '',$tabfield[$id]));
  119. $listfieldinsert=explode(',',$tabfieldinsert[$id]);
  120. $listfieldmodify=explode(',',$tabfieldinsert[$id]);
  121. $listfieldvalue=explode(',',$tabfieldvalue[$id]);
  122. // Check that all fields are filled
  123. $ok=1;
  124. foreach ($listfield as $f => $value)
  125. {
  126. if ($value == 'country_id' && in_array($tablib[$id],array('DictionaryVAT','DictionaryRegion','DictionaryCompanyType','DictionaryHolidayTypes','DictionaryRevenueStamp','DictionaryAccountancyCategory','Pcg_version'))) continue; // For some pages, country is not mandatory
  127. if ($value == 'country' && in_array($tablib[$id],array('DictionaryCanton','DictionaryCompanyType','DictionaryRevenueStamp'))) continue; // For some pages, country is not mandatory
  128. if ($value == 'localtax1' && empty($_POST['localtax1_type'])) continue;
  129. if ($value == 'localtax2' && empty($_POST['localtax2_type'])) continue;
  130. if ($value == 'color' && empty($_POST['color'])) continue;
  131. if ($value == 'formula' && empty($_POST['formula'])) continue;
  132. if ((! isset($_POST[$value]) || $_POST[$value]=='')
  133. && (! in_array($listfield[$f], array('decalage','module','accountancy_code','accountancy_code_sell','accountancy_code_buy')) // Fields that are not mandatory
  134. && (! ($id == 10 && $listfield[$f] == 'code')) // Code is mandatory fir table 10
  135. )
  136. )
  137. {
  138. $ok=0;
  139. $fieldnamekey=$listfield[$f];
  140. // We take translate key of field
  141. if ($fieldnamekey == 'pcg_version') $fieldnamekey='Pcg_version';
  142. if ($fieldnamekey == 'libelle' || ($fieldnamekey == 'label')) $fieldnamekey='Label';
  143. if ($fieldnamekey == 'libelle_facture') $fieldnamekey = 'LabelOnDocuments';
  144. if ($fieldnamekey == 'nbjour') $fieldnamekey='NbOfDays';
  145. if ($fieldnamekey == 'decalage') $fieldnamekey='Offset';
  146. if ($fieldnamekey == 'module') $fieldnamekey='Module';
  147. if ($fieldnamekey == 'code') $fieldnamekey = 'Code';
  148. if ($fieldnamekey == 'note') $fieldnamekey = 'Note';
  149. if ($fieldnamekey == 'taux') $fieldnamekey = 'Rate';
  150. if ($fieldnamekey == 'type') $fieldnamekey = 'Type';
  151. if ($fieldnamekey == 'position') $fieldnamekey = 'Position';
  152. if ($fieldnamekey == 'unicode') $fieldnamekey = 'Unicode';
  153. if ($fieldnamekey == 'deductible') $fieldnamekey = 'Deductible';
  154. if ($fieldnamekey == 'sortorder') $fieldnamekey = 'SortOrder';
  155. if ($fieldnamekey == 'category_type') $fieldnamekey = 'Calculated';
  156. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities($fieldnamekey)), null, 'errors');
  157. }
  158. }
  159. // Other checks
  160. if ($tabname[$id] == MAIN_DB_PREFIX."c_actioncomm" && isset($_POST["type"]) && in_array($_POST["type"],array('system','systemauto'))) {
  161. $ok=0;
  162. setEventMessages($langs->transnoentities('ErrorReservedTypeSystemSystemAuto'), null, 'errors');
  163. }
  164. if (isset($_POST["code"]))
  165. {
  166. if ($_POST["code"]=='0')
  167. {
  168. $ok=0;
  169. setEventMessages($langs->transnoentities('ErrorCodeCantContainZero'), null, 'errors');
  170. }
  171. /*if (!is_numeric($_POST['code'])) // disabled, code may not be in numeric base
  172. {
  173. $ok = 0;
  174. $msg .= $langs->transnoentities('ErrorFieldFormat', $langs->transnoentities('Code')).'<br>';
  175. }*/
  176. }
  177. if (isset($_POST["country"]) && ($_POST["country"]=='0') && ($id != 2))
  178. {
  179. if (in_array($tablib[$id],array('DictionaryCompanyType','DictionaryHolidayTypes'))) // Field country is no mandatory for such dictionaries
  180. {
  181. $_POST["country"]='';
  182. }
  183. else
  184. {
  185. $ok=0;
  186. setEventMessages($langs->transnoentities("ErrorFieldRequired",$langs->transnoentities("Country")), null, 'errors');
  187. }
  188. }
  189. if (! is_numeric($_POST["code"]))
  190. {
  191. $ok=0;
  192. setEventMessages($langs->transnoentities("ErrorFieldMustBeANumeric", $langs->transnoentities("Code")), null, 'errors');
  193. }
  194. // Clean some parameters
  195. if (isset($_POST["localtax1"]) && empty($_POST["localtax1"])) $_POST["localtax1"]='0'; // If empty, we force to 0
  196. if (isset($_POST["localtax2"]) && empty($_POST["localtax2"])) $_POST["localtax2"]='0'; // If empty, we force to 0
  197. if ($_POST["accountancy_code"] <= 0) $_POST["accountancy_code"]=''; // If empty, we force to null
  198. if ($_POST["accountancy_code_sell"] <= 0) $_POST["accountancy_code_sell"]=''; // If empty, we force to null
  199. if ($_POST["accountancy_code_buy"] <= 0) $_POST["accountancy_code_buy"]=''; // If empty, we force to null
  200. // Si verif ok et action add, on ajoute la ligne
  201. if ($ok && GETPOST('actionadd','alpha'))
  202. {
  203. if ($tabrowid[$id])
  204. {
  205. // Recupere id libre pour insertion
  206. $newid=0;
  207. $sql = "SELECT max(".$tabrowid[$id].") newid from ".$tabname[$id];
  208. $result = $db->query($sql);
  209. if ($result)
  210. {
  211. $obj = $db->fetch_object($result);
  212. $newid=($obj->newid + 1);
  213. } else {
  214. dol_print_error($db);
  215. }
  216. }
  217. // Add new entry
  218. $sql = "INSERT INTO ".$tabname[$id]." (";
  219. // List of fields
  220. if ($tabrowid[$id] && ! in_array($tabrowid[$id],$listfieldinsert))
  221. $sql.= $tabrowid[$id].",";
  222. $sql.= $tabfieldinsert[$id];
  223. $sql.=",active)";
  224. $sql.= " VALUES(";
  225. // List of values
  226. if ($tabrowid[$id] && ! in_array($tabrowid[$id],$listfieldinsert))
  227. $sql.= $newid.",";
  228. $i=0;
  229. foreach ($listfieldinsert as $f => $value)
  230. {
  231. if ($value == 'price' || preg_match('/^amount/i',$value) || $value == 'taux') {
  232. $_POST[$listfieldvalue[$i]] = price2num($_POST[$listfieldvalue[$i]],'MU');
  233. }
  234. elseif ($value == 'entity') {
  235. $_POST[$listfieldvalue[$i]] = $conf->entity;
  236. }
  237. if ($i) $sql.=",";
  238. if ($_POST[$listfieldvalue[$i]] == '') $sql.="null";
  239. else $sql.="'".$db->escape($_POST[$listfieldvalue[$i]])."'";
  240. $i++;
  241. }
  242. $sql.=",1)";
  243. dol_syslog("actionadd", LOG_DEBUG);
  244. $result = $db->query($sql);
  245. if ($result) // Add is ok
  246. {
  247. setEventMessages($langs->transnoentities("RecordSaved"), null, 'mesgs');
  248. $_POST=array('id'=>$id); // Clean $_POST array, we keep only
  249. }
  250. else
  251. {
  252. if ($db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  253. setEventMessages($langs->transnoentities("ErrorRecordAlreadyExists"), null, 'errors');
  254. }
  255. else {
  256. dol_print_error($db);
  257. }
  258. }
  259. }
  260. // Si verif ok et action modify, on modifie la ligne
  261. if ($ok && GETPOST('actionmodify','alpha'))
  262. {
  263. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  264. else { $rowidcol="rowid"; }
  265. // Modify entry
  266. $sql = "UPDATE ".$tabname[$id]." SET ";
  267. // Modifie valeur des champs
  268. if ($tabrowid[$id] && ! in_array($tabrowid[$id],$listfieldmodify))
  269. {
  270. $sql.= $tabrowid[$id]."=";
  271. $sql.= "'".$db->escape($rowid)."', ";
  272. }
  273. $i = 0;
  274. foreach ($listfieldmodify as $field)
  275. {
  276. if ($field == 'price' || preg_match('/^amount/i',$field) || $field == 'taux') {
  277. $_POST[$listfieldvalue[$i]] = price2num($_POST[$listfieldvalue[$i]],'MU');
  278. }
  279. elseif ($field == 'entity') {
  280. $_POST[$listfieldvalue[$i]] = $conf->entity;
  281. }
  282. if ($i) $sql.=",";
  283. $sql.= $field."=";
  284. if ($_POST[$listfieldvalue[$i]] == '') $sql.="null";
  285. else $sql.="'".$db->escape($_POST[$listfieldvalue[$i]])."'";
  286. $i++;
  287. }
  288. $sql.= " WHERE ".$rowidcol." = '".$rowid."'";
  289. dol_syslog("actionmodify", LOG_DEBUG);
  290. //print $sql;
  291. $resql = $db->query($sql);
  292. if (! $resql)
  293. {
  294. setEventMessages($db->error(), null, 'errors');
  295. }
  296. }
  297. //$_GET["id"]=GETPOST('id', 'int'); // Force affichage dictionnaire en cours d'edition
  298. }
  299. if (GETPOST('actioncancel','alpha'))
  300. {
  301. //$_GET["id"]=GETPOST('id', 'int'); // Force affichage dictionnaire en cours d'edition
  302. }
  303. if ($action == 'confirm_delete' && $confirm == 'yes') // delete
  304. {
  305. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  306. else { $rowidcol="rowid"; }
  307. $sql = "DELETE from ".$tabname[$id]." WHERE ".$rowidcol."='".$rowid."'";
  308. dol_syslog("delete", LOG_DEBUG);
  309. $result = $db->query($sql);
  310. if (! $result)
  311. {
  312. if ($db->errno() == 'DB_ERROR_CHILD_EXISTS')
  313. {
  314. setEventMessages($langs->transnoentities("ErrorRecordIsUsedByChild"), null, 'errors');
  315. }
  316. else
  317. {
  318. dol_print_error($db);
  319. }
  320. }
  321. }
  322. // activate
  323. if ($action == $acts[0])
  324. {
  325. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  326. else { $rowidcol="rowid"; }
  327. if ($rowid) {
  328. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE ".$rowidcol."='".$rowid."'";
  329. }
  330. elseif ($code) {
  331. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE code='".$code."'";
  332. }
  333. $result = $db->query($sql);
  334. if (!$result)
  335. {
  336. dol_print_error($db);
  337. }
  338. }
  339. // disable
  340. if ($action == $acts[1])
  341. {
  342. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  343. else { $rowidcol="rowid"; }
  344. if ($rowid) {
  345. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE ".$rowidcol."='".$rowid."'";
  346. }
  347. elseif ($code) {
  348. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE code='".$code."'";
  349. }
  350. $result = $db->query($sql);
  351. if (!$result)
  352. {
  353. dol_print_error($db);
  354. }
  355. }
  356. // favorite
  357. if ($action == 'activate_favorite')
  358. {
  359. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  360. else { $rowidcol="rowid"; }
  361. if ($rowid) {
  362. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE ".$rowidcol."='".$rowid."'";
  363. }
  364. elseif ($code) {
  365. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE code='".$code."'";
  366. }
  367. $result = $db->query($sql);
  368. if (!$result)
  369. {
  370. dol_print_error($db);
  371. }
  372. }
  373. // disable favorite
  374. if ($action == 'disable_favorite')
  375. {
  376. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  377. else { $rowidcol="rowid"; }
  378. if ($rowid) {
  379. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE ".$rowidcol."='".$rowid."'";
  380. }
  381. elseif ($code) {
  382. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE code='".$code."'";
  383. }
  384. $result = $db->query($sql);
  385. if (!$result)
  386. {
  387. dol_print_error($db);
  388. }
  389. }
  390. /*
  391. * View
  392. */
  393. $form = new Form($db);
  394. $formadmin=new FormAdmin($db);
  395. llxHeader();
  396. $titre=$langs->trans($tablib[$id]);
  397. $linkback='';
  398. print load_fiche_titre($titre,$linkback,'title_accountancy');
  399. print "<br>\n";
  400. // Confirmation de la suppression de la ligne
  401. if ($action == 'delete')
  402. {
  403. print $form->formconfirm($_SERVER["PHP_SELF"].'?'.($page?'page='.urlencode($page).'&':'').'sortfield='.urlencode($sortfield).'&sortorder='.urlencode($sortorder).'&rowid='.urlencode($rowid).'&code='.urlencode($code).'&id='.urlencode($id), $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_delete','',0,1);
  404. }
  405. //var_dump($elementList);
  406. /*
  407. * Show a dictionary
  408. */
  409. if ($id)
  410. {
  411. // Complete requete recherche valeurs avec critere de tri
  412. $sql=$tabsql[$id];
  413. if ($search_country_id > 0)
  414. {
  415. if (preg_match('/ WHERE /',$sql)) $sql.= " AND ";
  416. else $sql.=" WHERE ";
  417. $sql.= " c.rowid = ".$search_country_id;
  418. }
  419. // If sort order is "country", we use country_code instead
  420. if ($sortfield == 'country') $sortfield='country_code';
  421. $sql.=$db->order($sortfield,$sortorder);
  422. $sql.=$db->plimit($listlimit+1,$offset);
  423. //print $sql;
  424. $fieldlist=explode(',',$tabfield[$id]);
  425. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  426. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  427. print '<div class="div-table-responsive">';
  428. print '<table class="noborder" width="100%">';
  429. // Form to add a new line
  430. if ($tabname[$id])
  431. {
  432. $alabelisused=0;
  433. $var=false;
  434. $fieldlist=explode(',',$tabfield[$id]);
  435. // Line for title
  436. print '<tr class="liste_titre">';
  437. foreach ($fieldlist as $field => $value)
  438. {
  439. // Determine le nom du champ par rapport aux noms possibles
  440. // dans les dictionnaires de donnees
  441. $valuetoshow=ucfirst($fieldlist[$field]); // Par defaut
  442. $valuetoshow=$langs->trans($valuetoshow); // try to translate
  443. $align="left";
  444. if ($fieldlist[$field]=='code') { $valuetoshow=$langs->trans("Code"); }
  445. if ($fieldlist[$field]=='libelle' || $fieldlist[$field]=='label')
  446. {
  447. $valuetoshow=$langs->trans("Label");
  448. if ($id != 25) $valuetoshow.="*";
  449. }
  450. if ($fieldlist[$field]=='country') {
  451. if (in_array('region_id',$fieldlist)) { print '<td>&nbsp;</td>'; continue; } // For region page, we do not show the country input
  452. $valuetoshow=$langs->trans("Country");
  453. }
  454. if ($fieldlist[$field]=='country_id') { $valuetoshow=''; }
  455. if ($fieldlist[$field]=='pcg_version' || $fieldlist[$field]=='fk_pcg_version') { $valuetoshow=$langs->trans("Pcg_version"); }
  456. if ($valuetoshow != '')
  457. {
  458. print '<td align="'.$align.'">';
  459. 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>';
  460. elseif (! empty($tabhelp[$id][$value])) print $form->textwithpicto($valuetoshow,$tabhelp[$id][$value]);
  461. else print $valuetoshow;
  462. print '</td>';
  463. }
  464. if ($fieldlist[$field]=='libelle' || $fieldlist[$field]=='label') $alabelisused=1;
  465. }
  466. print '<td>';
  467. print '<input type="hidden" name="id" value="'.$id.'">';
  468. print '</td>';
  469. print '<td style="min-width: 26px;"></td>';
  470. print '<td style="min-width: 26px;"></td>';
  471. print '</tr>';
  472. // Line to enter new values
  473. print "<tr ".$bcnd[$var].">";
  474. $obj = new stdClass();
  475. // If data was already input, we define them in obj to populate input fields.
  476. if (GETPOST('actionadd','alpha'))
  477. {
  478. foreach ($fieldlist as $key=>$val)
  479. {
  480. if (GETPOST($val))
  481. $obj->$val=GETPOST($val);
  482. }
  483. }
  484. $tmpaction = 'create';
  485. $parameters=array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  486. $reshook=$hookmanager->executeHooks('createDictionaryFieldlist',$parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  487. $error=$hookmanager->error; $errors=$hookmanager->errors;
  488. if (empty($reshook))
  489. {
  490. fieldListAccountModel($fieldlist,$obj,$tabname[$id],'add');
  491. }
  492. print '<td colspan="3" align="right">';
  493. print '<input type="submit" class="button" name="actionadd" value="'.$langs->trans("Add").'">';
  494. print '</td>';
  495. print "</tr>";
  496. $colspan=count($fieldlist)+3;
  497. if (! empty($alabelisused)) // If there is one label among fields, we show legend of *
  498. {
  499. print '<tr><td colspan="'.$colspan.'">* '.$langs->trans("LabelUsedByDefault").'.</td></tr>';
  500. }
  501. print '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>'; // Keep &nbsp; to have a line with enough height
  502. }
  503. // List of available values in database
  504. dol_syslog("htdocs/admin/dict", LOG_DEBUG);
  505. $resql=$db->query($sql);
  506. if ($resql)
  507. {
  508. $num = $db->num_rows($resql);
  509. $i = 0;
  510. $param = '&id='.$id;
  511. if ($search_country_id > 0) $param.= '&search_country_id='.$search_country_id;
  512. $paramwithsearch = $param;
  513. if ($sortorder) $paramwithsearch.= '&sortorder='.$sortorder;
  514. if ($sortfield) $paramwithsearch.= '&sortfield='.$sortfield;
  515. // There is several pages
  516. if ($num > $listlimit)
  517. {
  518. print '<tr class="none"><td align="right" colspan="'.(3+count($fieldlist)).'">';
  519. print_fleche_navigation($page, $_SERVER["PHP_SELF"], $paramwithsearch, ($num > $listlimit), '<li class="pagination"><span>'.$langs->trans("Page").' '.($page+1).'</span></li>');
  520. print '</td></tr>';
  521. }
  522. // Title line with search boxes
  523. print '<tr class="liste_titre liste_titre_add">';
  524. foreach ($fieldlist as $field => $value)
  525. {
  526. $showfield=1; // By defaut
  527. if ($fieldlist[$field]=='region_id' || $fieldlist[$field]=='country_id') { $showfield=0; }
  528. if ($showfield)
  529. {
  530. if ($value == 'country')
  531. {
  532. print '<td class="liste_titre">';
  533. print $form->select_country($search_country_id, 'search_country_id', '', 28, 'maxwidth200 maxwidthonsmartphone');
  534. print '</td>';
  535. }
  536. else
  537. {
  538. print '<td class="liste_titre"></td>';
  539. }
  540. }
  541. }
  542. print '<td class="liste_titre"></td>';
  543. print '<td class="liste_titre" colspan="2" align="right">';
  544. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  545. print $searchpicto;
  546. print '</td>';
  547. print '</tr>';
  548. // Title of lines
  549. print '<tr class="liste_titre">';
  550. foreach ($fieldlist as $field => $value)
  551. {
  552. // Determine le nom du champ par rapport aux noms possibles
  553. // dans les dictionnaires de donnees
  554. $showfield=1; // By defaut
  555. $align="left";
  556. $sortable=1;
  557. $valuetoshow='';
  558. /*
  559. $tmparray=getLabelOfField($fieldlist[$field]);
  560. $showfield=$tmp['showfield'];
  561. $valuetoshow=$tmp['valuetoshow'];
  562. $align=$tmp['align'];
  563. $sortable=$tmp['sortable'];
  564. */
  565. $valuetoshow=ucfirst($fieldlist[$field]); // By defaut
  566. $valuetoshow=$langs->trans($valuetoshow); // try to translate
  567. if ($fieldlist[$field]=='code') { $valuetoshow=$langs->trans("Code"); }
  568. if ($fieldlist[$field]=='libelle' || $fieldlist[$field]=='label')
  569. {
  570. $valuetoshow=$langs->trans("Label");
  571. if ($id != 25) $valuetoshow.="*";
  572. }
  573. if ($fieldlist[$field]=='country') { $valuetoshow=$langs->trans("Country"); }
  574. if ($fieldlist[$field]=='country_id') { $showfield=0; }
  575. if ($fieldlist[$field]=='fk_pcg_version') { $valuetoshow=$langs->trans("Pcg_version"); }
  576. // Affiche nom du champ
  577. if ($showfield)
  578. {
  579. print getTitleFieldOfList($valuetoshow, 0, $_SERVER["PHP_SELF"], ($sortable?$fieldlist[$field]:''), ($page?'page='.$page.'&':''), $param, "align=".$align, $sortfield, $sortorder);
  580. }
  581. }
  582. print getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], "active", ($page?'page='.$page.'&':''), $param, 'align="center"', $sortfield, $sortorder);
  583. print getTitleFieldOfList('');
  584. print getTitleFieldOfList('');
  585. print '</tr>';
  586. if ($num)
  587. {
  588. // Lines with values
  589. while ($i < $num)
  590. {
  591. $obj = $db->fetch_object($resql);
  592. //print_r($obj);
  593. print '<tr class="oddeven" id="rowid-'.$obj->rowid.'">';
  594. if ($action == 'edit' && ($rowid == (! empty($obj->rowid)?$obj->rowid:$obj->code)))
  595. {
  596. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  597. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  598. print '<input type="hidden" name="page" value="'.$page.'">';
  599. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  600. $tmpaction='edit';
  601. $parameters=array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  602. $reshook=$hookmanager->executeHooks('editDictionaryFieldlist',$parameters,$obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  603. $error=$hookmanager->error; $errors=$hookmanager->errors;
  604. if (empty($reshook)) fieldListAccountModel($fieldlist,$obj,$tabname[$id],'edit');
  605. print '<td colspan="3" align="right"><a name="'.(! empty($obj->rowid)?$obj->rowid:$obj->code).'">&nbsp;</a><input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">';
  606. print '&nbsp;<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'"></td>';
  607. }
  608. else
  609. {
  610. $tmpaction = 'view';
  611. $parameters=array('var'=>$var, 'fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  612. $reshook=$hookmanager->executeHooks('viewDictionaryFieldlist',$parameters,$obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  613. $error=$hookmanager->error; $errors=$hookmanager->errors;
  614. if (empty($reshook))
  615. {
  616. foreach ($fieldlist as $field => $value)
  617. {
  618. $showfield=1;
  619. $align="left";
  620. $valuetoshow=$obj->{$fieldlist[$field]};
  621. if ($value == 'type_template')
  622. {
  623. $valuetoshow = isset($elementList[$valuetoshow])?$elementList[$valuetoshow]:$valuetoshow;
  624. }
  625. if ($value == 'element')
  626. {
  627. $valuetoshow = isset($elementList[$valuetoshow])?$elementList[$valuetoshow]:$valuetoshow;
  628. }
  629. elseif ($value == 'source')
  630. {
  631. $valuetoshow = isset($sourceList[$valuetoshow])?$sourceList[$valuetoshow]:$valuetoshow;
  632. }
  633. elseif ($valuetoshow=='all') {
  634. $valuetoshow=$langs->trans('All');
  635. }
  636. elseif ($fieldlist[$field]=='country') {
  637. if (empty($obj->country_code))
  638. {
  639. $valuetoshow='-';
  640. }
  641. else
  642. {
  643. $key=$langs->trans("Country".strtoupper($obj->country_code));
  644. $valuetoshow=($key != "Country".strtoupper($obj->country_code)?$obj->country_code." - ".$key:$obj->country);
  645. }
  646. }
  647. elseif ($fieldlist[$field]=='country_id') {
  648. $showfield=0;
  649. }
  650. $class='tddict';
  651. if ($fieldlist[$field] == 'tracking') $class.=' tdoverflowauto';
  652. // Show value for field
  653. if ($showfield) print '<!-- '.$fieldlist[$field].' --><td align="'.$align.'" class="'.$class.'">'.$valuetoshow.'</td>';
  654. }
  655. }
  656. // Can an entry be erased or disabled ?
  657. $iserasable=1;$canbedisabled=1;$canbemodified=1; // true by default
  658. $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):'');
  659. if ($param) $url .= '&'.$param;
  660. $url.='&';
  661. // Active
  662. print '<td align="center" class="nowrap">';
  663. if ($canbedisabled) print '<a href="'.$url.'action='.$acts[$obj->active].'">'.$actl[$obj->active].'</a>';
  664. else print $langs->trans("AlwaysActive");
  665. print "</td>";
  666. // Modify link
  667. if ($canbemodified) print '<td align="center"><a class="reposition" href="'.$url.'action=edit">'.img_edit().'</a></td>';
  668. else print '<td>&nbsp;</td>';
  669. // Delete link
  670. if ($iserasable) print '<td align="center"><a href="'.$url.'action=delete">'.img_delete().'</a></td>';
  671. else print '<td>&nbsp;</td>';
  672. print "</tr>\n";
  673. }
  674. $i++;
  675. }
  676. }
  677. }
  678. else {
  679. dol_print_error($db);
  680. }
  681. print '</table>';
  682. print '</div>';
  683. print '</form>';
  684. }
  685. print '<br>';
  686. // End of page
  687. llxFooter();
  688. $db->close();
  689. /**
  690. * Show fields in insert/edit mode
  691. *
  692. * @param array $fieldlist Array of fields
  693. * @param Object $obj If we show a particular record, obj is filled with record fields
  694. * @param string $tabname Name of SQL table
  695. * @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
  696. * @return void
  697. */
  698. function fieldListAccountModel($fieldlist, $obj = '', $tabname = '', $context = '')
  699. {
  700. global $conf,$langs,$db;
  701. global $form;
  702. global $region_id;
  703. global $elementList,$sourceList;
  704. $formadmin = new FormAdmin($db);
  705. $formcompany = new FormCompany($db);
  706. $formaccounting = new FormAccounting($db);
  707. foreach ($fieldlist as $field => $value)
  708. {
  709. if ($fieldlist[$field] == 'country')
  710. {
  711. if (in_array('region_id',$fieldlist))
  712. {
  713. print '<td>';
  714. //print join(',',$fieldlist);
  715. print '</td>';
  716. continue;
  717. } // For state page, we do not show the country input (we link to region, not country)
  718. print '<td>';
  719. $fieldname='country';
  720. print $form->select_country((! empty($obj->country_code)?$obj->country_code:(! empty($obj->country)?$obj->country:'')), $fieldname, '', 28, 'maxwidth200 maxwidthonsmartphone');
  721. print '</td>';
  722. }
  723. elseif ($fieldlist[$field] == 'country_id')
  724. {
  725. if (! in_array('country',$fieldlist)) // If there is already a field country, we don't show country_id (avoid duplicate)
  726. {
  727. $country_id = (! empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : 0);
  728. print '<td>';
  729. print '<input type="hidden" name="'.$fieldlist[$field].'" value="'.$country_id.'">';
  730. print '</td>';
  731. }
  732. }
  733. elseif ($fieldlist[$field] == 'type_cdr') {
  734. if ($fieldlist[$field] == 'type_cdr') print '<td align="center">';
  735. else print '<td>';
  736. if ($fieldlist[$field] == 'type_cdr') {
  737. print $form->selectarray($fieldlist[$field], array(0=>$langs->trans('None'), 1=>$langs->trans('AtEndOfMonth'), 2=>$langs->trans('CurrentNext')), (! empty($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:''));
  738. } else {
  739. print $form->selectyesno($fieldlist[$field],(! empty($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:''),1);
  740. }
  741. print '</td>';
  742. }
  743. elseif ($fieldlist[$field] == 'code' && isset($obj->{$fieldlist[$field]})) {
  744. print '<td><input type="text" class="flat" value="'.(! empty($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:'').'" size="10" name="'.$fieldlist[$field].'"></td>';
  745. }
  746. else
  747. {
  748. print '<td>';
  749. $size=''; $class='';
  750. if ($fieldlist[$field]=='code') $size='size="8" ';
  751. if ($fieldlist[$field]=='position') $size='size="4" ';
  752. if ($fieldlist[$field]=='libelle') $size='centpercent';
  753. if ($fieldlist[$field]=='sortorder' || $fieldlist[$field]=='sens' || $fieldlist[$field]=='category_type') $size='size="2" ';
  754. print '<input type="text" '.$size.' class="flat'.($class?' '.$class:'').'" value="'.(isset($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:'').'" name="'.$fieldlist[$field].'">';
  755. print '</td>';
  756. }
  757. }
  758. }