card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.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. // Load translation files required by the page
  31. $langs->loadLangs(array("bills","accountancy"));
  32. $mesg = '';
  33. $action = GETPOST('action','aZ09');
  34. $backtopage = GETPOST('backtopage','alpha');
  35. $id = GETPOST('id', 'int');
  36. $ref = GETPOST('ref', 'alpha');
  37. $rowid = GETPOST('rowid', 'int');
  38. $cancel = GETPOST('cancel','alpha');
  39. // Security check
  40. $object = new AccountingAccount($db);
  41. /*
  42. * Action
  43. */
  44. if (GETPOST('cancel','alpha'))
  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','string');
  62. }
  63. else
  64. {
  65. $account_number = clean_account(GETPOST('account_number','string'));
  66. }
  67. if (GETPOST('account_parent','int') <= 0)
  68. {
  69. $account_parent = 0;
  70. }
  71. else
  72. {
  73. $account_parent = GETPOST('account_parent','int');
  74. }
  75. $object->fk_pcg_version = $obj->pcg_version;
  76. $object->pcg_type = GETPOST('pcg_type','alpha');
  77. $object->pcg_subtype = GETPOST('pcg_subtype','alpha');
  78. $object->account_number = $account_number;
  79. $object->account_parent = $account_parent;
  80. $object->account_category = GETPOST('account_category','alpha');
  81. $object->label = GETPOST('label', 'alpha');
  82. $object->active = 1;
  83. $res = $object->create($user);
  84. if ($res == - 3) {
  85. $error = 1;
  86. $action = "create";
  87. setEventMessages($object->error, $object->errors, 'errors');
  88. }
  89. elseif ($res == - 4) {
  90. $error = 2;
  91. $action = "create";
  92. setEventMessages($object->error, $object->errors, 'errors');
  93. }
  94. elseif ($res < 0)
  95. {
  96. $error++;
  97. setEventMessages($object->error, $object->errors, 'errors');
  98. $action = "create";
  99. }
  100. if (! $error)
  101. {
  102. setEventMessages("RecordCreatedSuccessfully",null,'mesgs');
  103. $urltogo=$backtopage?$backtopage:dol_buildpath('/accountancy/admin/account.php',1);
  104. header("Location: ".$urltogo);
  105. exit;
  106. }
  107. }
  108. } else if ($action == 'edit' && $user->rights->accounting->chartofaccount) {
  109. if (! $cancel) {
  110. $result = $object->fetch($id);
  111. $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
  112. dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
  113. $result2 = $db->query($sql);
  114. $obj = $db->fetch_object($result2);
  115. // Clean code
  116. // To manage zero or not at the end of the accounting account
  117. if($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
  118. {
  119. $account_number = GETPOST('account_number','string');
  120. }
  121. else
  122. {
  123. $account_number = clean_account(GETPOST('account_number','string'));
  124. }
  125. if (GETPOST('account_parent','int') <= 0)
  126. {
  127. $account_parent = 0;
  128. }
  129. else
  130. {
  131. $account_parent = GETPOST('account_parent','int');
  132. }
  133. $object->fk_pcg_version = $obj->pcg_version;
  134. $object->pcg_type = GETPOST('pcg_type','alpha');
  135. $object->pcg_subtype = GETPOST('pcg_subtype','alpha');
  136. $object->account_number = $account_number;
  137. $object->account_parent = $account_parent;
  138. $object->account_category = GETPOST('account_category','alpha');
  139. $object->label = GETPOST('label', 'alpha');
  140. $result = $object->update($user);
  141. if ($result > 0) {
  142. $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
  143. header("Location: " . $urltogo);
  144. exit();
  145. } else {
  146. $mesg = $object->error;
  147. }
  148. } else {
  149. $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
  150. header("Location: " . $urltogo);
  151. exit();
  152. }
  153. } else if ($action == 'delete' && $user->rights->accounting->chartofaccount) {
  154. $result = $object->fetch($id);
  155. if (! empty($object->id)) {
  156. $result = $object->delete($user);
  157. if ($result > 0) {
  158. header("Location: account.php");
  159. exit;
  160. }
  161. }
  162. if ($result < 0) {
  163. setEventMessages($object->error, $object->errors, 'errors');
  164. }
  165. }
  166. /*
  167. * View
  168. */
  169. $form = new Form($db);
  170. $formaccounting = new FormAccounting($db);
  171. $accountsystem = new AccountancySystem($db);
  172. $accountsystem->fetch($conf->global->CHARTOFACCOUNTS);
  173. $title = $langs->trans('AccountAccounting') ." - ". $langs->trans('Card');
  174. $helpurl = '';
  175. llxheader('', $title, $helpurl);
  176. // Create mode
  177. if ($action == 'create') {
  178. print load_fiche_titre($langs->trans('NewAccountingAccount'));
  179. print '<form name="add" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
  180. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  181. print '<input type="hidden" name="action" value="add">';
  182. dol_fiche_head();
  183. print '<table class="border" width="100%">';
  184. // Chart of account
  185. print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("Chartofaccounts") . '</span></td>';
  186. print '<td>';
  187. print $accountsystem->ref;
  188. print '</td></tr>';
  189. // Account number
  190. print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
  191. print '<td><input name="account_number" size="30" value="' . $object->account_number . '"></td></tr>';
  192. // Label
  193. print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
  194. print '<td><input name="label" size="70" value="' . $object->label . '"></td></tr>';
  195. // Account parent
  196. print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
  197. print '<td>';
  198. print $formaccounting->select_account($object->account_parent, 'account_parent', 1, null, 0, 0, 'minwidth200');
  199. print '</td></tr>';
  200. // Category
  201. print '<tr><td>' . $langs->trans("AccountingCategory") . '</td>';
  202. print '<td>';
  203. $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
  204. print '</td></tr>';
  205. // Chart of accounts type
  206. print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
  207. print '<td>';
  208. print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
  209. print '</td></tr>';
  210. // Chart of acounts subtype
  211. print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
  212. print '<td>';
  213. print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
  214. print '</td></tr>';
  215. print '</table>';
  216. dol_fiche_end();
  217. print '<div class="center">';
  218. print '<input class="button" type="submit" value="' . $langs->trans("Save") . '">';
  219. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  220. print '<input class="button" type="submit" name="cancel" value="' . $langs->trans("Cancel") . '">';
  221. print '</div>';
  222. print '</form>';
  223. }
  224. else if ($id > 0 || $ref) {
  225. $result = $object->fetch($id, $ref, 1);
  226. if ($result > 0) {
  227. dol_htmloutput_mesg($mesg);
  228. $head = accounting_prepare_head($object);
  229. // Edit mode
  230. if ($action == 'update')
  231. {
  232. dol_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'billr');
  233. print '<form name="update" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
  234. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  235. print '<input type="hidden" name="action" value="edit">';
  236. print '<input type="hidden" name="id" value="' . $id . '">';
  237. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  238. print '<table class="border" width="100%">';
  239. // Account number
  240. print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
  241. print '<td><input name="account_number" size="30" value="' . $object->account_number . '"</td></tr>';
  242. // Label
  243. print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
  244. print '<td><input name="label" size="70" value="' . $object->label . '"</td></tr>';
  245. // Account parent
  246. print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
  247. print '<td>';
  248. print $formaccounting->select_account($object->account_parent, 'account_parent', 1);
  249. print '</td></tr>';
  250. // Category
  251. print '<tr><td>'.$langs->trans("AccountingCategory").'</td>';
  252. print '<td>';
  253. $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
  254. print '</td></tr>';
  255. // Chart of accounts type
  256. print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
  257. print '<td>';
  258. print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
  259. print '</td></tr>';
  260. // Chart of accounts subtype
  261. print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
  262. print '<td>';
  263. print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
  264. print '</td></tr>';
  265. print '</table>';
  266. dol_fiche_end();
  267. print '<div class="center">';
  268. print '<input type="submit" class="button" value="' . $langs->trans("Save") . '">';
  269. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  270. print '<input type="submit" name="cancel" class="button" value="' . $langs->trans("Cancel") . '">';
  271. print '</div>';
  272. print '</form>';
  273. } else {
  274. // View mode
  275. $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/account.php?restore_lastsearch_values=1">' . $langs->trans("BackToList") . '</a>';
  276. dol_fiche_head($head, 'card', $langs->trans('AccountAccounting'), -1, 'billr');
  277. dol_banner_tab($object, 'ref', $linkback, 1, 'account_number', 'ref');
  278. print '<div class="fichecenter">';
  279. print '<div class="underbanner clearboth"></div>';
  280. print '<table class="border" width="100%">';
  281. // Label
  282. print '<tr><td class="titlefield">' . $langs->trans("Label") . '</td>';
  283. print '<td colspan="2">' . $object->label . '</td></tr>';
  284. // Account parent
  285. $accp = new AccountingAccount($db);
  286. if (! empty($object->account_parent)) {
  287. $accp->fetch($object->account_parent, '');
  288. }
  289. print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
  290. print '<td colspan="2">' . $accp->account_number . ' - ' . $accp->label . '</td></tr>';
  291. // Category
  292. print "<tr><td>".$langs->trans("AccountingCategory")."</td><td colspan='2'>".$object->account_category_label."</td>";
  293. // Chart of accounts type
  294. print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
  295. print '<td colspan="2">' . $object->pcg_type . '</td></tr>';
  296. // Chart of accounts subtype
  297. print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
  298. print '<td colspan="2">' . $object->pcg_subtype . '</td></tr>';
  299. print '</table>';
  300. print '</div>';
  301. dol_fiche_end();
  302. /*
  303. * Actions buttons
  304. */
  305. print '<div class="tabsAction">';
  306. if (! empty($user->rights->accounting->chartofaccount)) {
  307. print '<a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?action=update&id=' . $id . '">' . $langs->trans('Modify') . '</a>';
  308. } else {
  309. print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('Modify') . '</a>';
  310. }
  311. if (! empty($user->rights->accounting->chartofaccount)) {
  312. print '<a class="butActionDelete" href="' . $_SERVER["PHP_SELF"] . '?action=delete&id=' . $id . '">' . $langs->trans('Delete') . '</a>';
  313. } else {
  314. print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('Delete') . '</a>';
  315. }
  316. print '</div>';
  317. }
  318. } else {
  319. dol_print_error($db, $object->error, $object->errors);
  320. }
  321. }
  322. // End of page
  323. llxFooter();
  324. $db->close();