card.php 13 KB

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