card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  4. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/accountancy/admin/card.php
  21. * \ingroup Advanced accountancy
  22. * \brief Card of accounting account
  23. */
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  26. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  27. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountancysystem.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  29. $error = 0;
  30. // Langs
  31. $langs->load("bills");
  32. $langs->load("accountancy");
  33. $mesg = '';
  34. $action = GETPOST('action','aZ09');
  35. $backtopage = GETPOST('backtopage');
  36. $id = GETPOST('id', 'int');
  37. $rowid = GETPOST('rowid', 'int');
  38. $cancel = GETPOST('cancel');
  39. // Security check
  40. $object = new AccountingAccount($db);
  41. /*
  42. * Action
  43. */
  44. if (GETPOST('cancel'))
  45. {
  46. $urltogo=$backtopage?$backtopage:dol_buildpath('/accountancy/admin/account.php',1);
  47. header("Location: ".$urltogo);
  48. exit;
  49. }
  50. if ($action == 'add' && $user->rights->accounting->chartofaccount)
  51. {
  52. if (! $cancel) {
  53. $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
  54. dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
  55. $result = $db->query($sql);
  56. $obj = $db->fetch_object($result);
  57. // Clean code
  58. // To manage zero or not at the end of the accounting account
  59. if($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
  60. {
  61. $account_number = GETPOST('account_number');
  62. }
  63. else
  64. {
  65. $account_number = clean_account(GETPOST('account_number'));
  66. }
  67. if (GETPOST('account_parent') <= 0) {
  68. $account_parent = 0;
  69. } else {
  70. $account_parent = GETPOST('account_parent','int');
  71. }
  72. $object->fk_pcg_version = $obj->pcg_version;
  73. $object->pcg_type = GETPOST('pcg_type');
  74. $object->pcg_subtype = GETPOST('pcg_subtype');
  75. $object->account_number = $account_number;
  76. $object->account_parent = $account_parent;
  77. $object->account_category = GETPOST('account_category');
  78. $object->label = GETPOST('label', 'alpha');
  79. $object->active = 1;
  80. $res = $object->create($user);
  81. if ($res == - 3) {
  82. $error = 1;
  83. $action = "create";
  84. setEventMessages($object->error, $object->errors, 'errors');
  85. }
  86. elseif ($res == - 4) {
  87. $error = 2;
  88. $action = "create";
  89. setEventMessages($object->error, $object->errors, 'errors');
  90. }
  91. elseif ($res < 0)
  92. {
  93. $error++;
  94. setEventMessages($object->error, $object->errors, 'errors');
  95. $action = "create";
  96. }
  97. if (! $error)
  98. {
  99. setEventMessages("RecordCreatedSuccessfully",null,'mesgs');
  100. $urltogo=$backtopage?$backtopage:dol_buildpath('/accountancy/admin/account.php',1);
  101. header("Location: ".$urltogo);
  102. exit;
  103. }
  104. }
  105. } else if ($action == 'edit' && $user->rights->accounting->chartofaccount) {
  106. if (! $cancel) {
  107. $result = $object->fetch($id);
  108. $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
  109. dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
  110. $result2 = $db->query($sql);
  111. $obj = $db->fetch_object($result2);
  112. // Clean code
  113. // To manage zero or not at the end of the accounting account
  114. if($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
  115. {
  116. $account_number = GETPOST('account_number');
  117. }
  118. else
  119. {
  120. $account_number = clean_account(GETPOST('account_number'));
  121. }
  122. if (GETPOST('account_parent') <= 0) {
  123. $account_parent = 0;
  124. } else {
  125. $account_parent = GETPOST('account_parent','int');
  126. }
  127. $object->fk_pcg_version = $obj->pcg_version;
  128. $object->pcg_type = GETPOST('pcg_type');
  129. $object->pcg_subtype = GETPOST('pcg_subtype');
  130. $object->account_number = $account_number;
  131. $object->account_parent = $account_parent;
  132. $object->account_category = GETPOST('account_category');
  133. $object->label = GETPOST('label', 'alpha');
  134. $result = $object->update($user);
  135. if ($result > 0) {
  136. $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
  137. header("Location: " . $urltogo);
  138. exit();
  139. } else {
  140. $mesg = $object->error;
  141. }
  142. } else {
  143. $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
  144. header("Location: " . $urltogo);
  145. exit();
  146. }
  147. } else if ($action == 'delete' && $user->rights->accounting->chartofaccount) {
  148. $result = $object->fetch($id);
  149. if (! empty($object->id)) {
  150. $result = $object->delete($user);
  151. if ($result > 0) {
  152. header("Location: account.php");
  153. exit;
  154. }
  155. }
  156. if ($result < 0) {
  157. setEventMessages($object->error, $object->errors, 'errors');
  158. }
  159. }
  160. /*
  161. * View
  162. */
  163. $title = $langs->trans('AccountAccounting') ." - ". $langs->trans('Card');
  164. $helpurl = '';
  165. llxheader('', $title, $helpurl);
  166. $form = new Form($db);
  167. $formaccounting = new FormAccounting($db);
  168. $accountsystem = new AccountancySystem($db);
  169. $accountsystem->fetch($conf->global->CHARTOFACCOUNTS);
  170. // Create mode
  171. if ($action == 'create') {
  172. print load_fiche_titre($langs->trans('NewAccountingAccount'));
  173. print '<form name="add" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
  174. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  175. print '<input type="hidden" name="action" value="add">';
  176. dol_fiche_head();
  177. print '<table class="border" width="100%">';
  178. // Chart of account
  179. print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("Chartofaccounts") . '</span></td>';
  180. print '<td>';
  181. print $accountsystem->ref;
  182. print '</td></tr>';
  183. // Account number
  184. print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
  185. print '<td><input name="account_number" size="30" value="' . $object->account_number . '"></td></tr>';
  186. // Label
  187. print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
  188. print '<td><input name="label" size="70" value="' . $object->label . '"></td></tr>';
  189. // Account parent
  190. print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
  191. print '<td>';
  192. print $formaccounting->select_account($object->account_parent, 'account_parent', 1, null, 0, 0, 'minwidth200');
  193. print '</td></tr>';
  194. // Category
  195. print '<tr><td>' . $langs->trans("AccountingCategory") . '</td>';
  196. print '<td>';
  197. $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
  198. print '</td></tr>';
  199. // Chart of accounts type
  200. print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
  201. print '<td>';
  202. print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
  203. print '</td></tr>';
  204. // Chart of acounts subtype
  205. print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
  206. print '<td>';
  207. print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
  208. print '</td></tr>';
  209. print '</table>';
  210. dol_fiche_end();
  211. print '<div class="center">';
  212. print '<input class="button" type="submit" value="' . $langs->trans("Save") . '">';
  213. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  214. print '<input class="button" type="submit" name="cancel" value="' . $langs->trans("Cancel") . '">';
  215. print '</div>';
  216. print '</form>';
  217. } else if ($id) {
  218. $result = $object->fetch($id);
  219. if ($result > 0) {
  220. dol_htmloutput_mesg($mesg);
  221. $head = accounting_prepare_head($object);
  222. // Edit mode
  223. if ($action == 'update')
  224. {
  225. dol_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'billr');
  226. print '<form name="update" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
  227. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  228. print '<input type="hidden" name="action" value="edit">';
  229. print '<input type="hidden" name="id" value="' . $id . '">';
  230. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  231. print '<table class="border" width="100%">';
  232. // Account number
  233. print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
  234. print '<td><input name="account_number" size="30" value="' . $object->account_number . '"</td></tr>';
  235. // Label
  236. print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
  237. print '<td><input name="label" size="70" value="' . $object->label . '"</td></tr>';
  238. // Account parent
  239. print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
  240. print '<td>';
  241. print $formaccounting->select_account($object->account_parent, 'account_parent', 1);
  242. print '</td></tr>';
  243. // Category
  244. print '<tr><td>'.$langs->trans("AccountingCategory").'</td>';
  245. print '<td>';
  246. $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
  247. print '</td></tr>';
  248. // Chart of accounts type
  249. print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
  250. print '<td>';
  251. print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
  252. print '</td></tr>';
  253. // Chart of accounts subtype
  254. print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
  255. print '<td>';
  256. print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
  257. print '</td></tr>';
  258. print '</table>';
  259. dol_fiche_end();
  260. print '<div class="center">';
  261. print '<input type="submit" class="button" value="' . $langs->trans("Save") . '">';
  262. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  263. print '<input type="submit" name="cancel" class="button" value="' . $langs->trans("Cancel") . '">';
  264. print '</div>';
  265. print '</form>';
  266. } else {
  267. // View mode
  268. $linkback = '<a href="../admin/account.php">' . $langs->trans("BackToChartofaccounts") . '</a>';
  269. dol_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'billr');
  270. print '<table class="border" width="100%">';
  271. // Account number
  272. print '<tr><td class="titlefield">' . $langs->trans("AccountNumber") . '</td>';
  273. print '<td>' . $object->account_number . '</td>';
  274. print '<td align="right" width="25%">' . $linkback . '</td></tr>';
  275. // Label
  276. print '<tr><td>' . $langs->trans("Label") . '</td>';
  277. print '<td colspan="2">' . $object->label . '</td></tr>';
  278. // Account parent
  279. $accp = new AccountingAccount($db);
  280. if (! empty($object->account_parent)) {
  281. $accp->fetch($object->account_parent, '');
  282. }
  283. print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
  284. print '<td colspan="2">' . $accp->account_number . ' - ' . $accp->label . '</td></tr>';
  285. // Category
  286. print "<tr><td>".$langs->trans("AccountingCategory")."</td><td colspan='2'>".$object->account_category_label."</td>";
  287. // Chart of accounts type
  288. print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
  289. print '<td colspan="2">' . $object->pcg_type . '</td></tr>';
  290. // Chart of accounts subtype
  291. print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
  292. print '<td colspan="2">' . $object->pcg_subtype . '</td></tr>';
  293. // Active
  294. print '<tr><td>' . $langs->trans("Status") . '</td>';
  295. print '<td colspan="2">';
  296. print $object->getLibStatut(4);
  297. /*if (empty($object->active)) {
  298. print img_picto($langs->trans("Disabled"), 'switch_off');
  299. } else {
  300. print img_picto($langs->trans("Activated"), 'switch_on');
  301. }*/
  302. print '</td></tr>';
  303. print '</table>';
  304. dol_fiche_end();
  305. /*
  306. * Actions buttons
  307. */
  308. print '<div class="tabsAction">';
  309. if (! empty($user->rights->accounting->chartofaccount)) {
  310. print '<a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?action=update&id=' . $id . '">' . $langs->trans('Modify') . '</a>';
  311. } else {
  312. print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('Modify') . '</a>';
  313. }
  314. if (! empty($user->rights->accounting->chartofaccount)) {
  315. print '<a class="butActionDelete" href="' . $_SERVER["PHP_SELF"] . '?action=delete&id=' . $id . '">' . $langs->trans('Delete') . '</a>';
  316. } else {
  317. print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('Delete') . '</a>';
  318. }
  319. print '</div>';
  320. }
  321. } else {
  322. dol_print_error($db);
  323. }
  324. }
  325. llxFooter();
  326. $db->close();