card.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
  4. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.com>
  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/bookkeeping/card.php
  21. * \ingroup Advanced accountancy
  22. * \brief Page to show book-entry
  23. */
  24. require '../../main.inc.php';
  25. // Class
  26. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  27. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  30. // Langs
  31. $langs->load("accountancy");
  32. $langs->load("bank");
  33. $langs->load("bills");
  34. $langs->load("trips");
  35. // Security check
  36. $id = GETPOST('id', 'int');
  37. if ($user->societe_id > 0) {
  38. accessforbidden();
  39. }
  40. $action = GETPOST('action','aZ09');
  41. $piece_num = GETPOST("piece_num");
  42. $mesg = '';
  43. $account_number = GETPOST('account_number');
  44. $code_tiers = GETPOST('code_tiers');
  45. if ($code_tiers == - 1) {
  46. $code_tiers = null;
  47. }
  48. $label_compte = GETPOST('label_compte');
  49. $debit = price2num(GETPOST('debit'));
  50. $credit = price2num(GETPOST('credit'));
  51. $save = GETPOST('save');
  52. if (! empty($save)) {
  53. $action = 'add';
  54. }
  55. $update = GETPOST('update');
  56. if (! empty($update)) {
  57. $action = 'confirm_update';
  58. }
  59. if ($action == "confirm_update") {
  60. $error = 0;
  61. if ((floatval($debit) != 0.0) && (floatval($credit) != 0.0)) {
  62. setEventMessages($langs->trans('ErrorDebitCredit'), null, 'errors');
  63. $error ++;
  64. }
  65. if (empty($error)) {
  66. $book = new BookKeeping($db);
  67. $result = $book->fetch($id);
  68. if ($result < 0) {
  69. setEventMessages($book->error, $book->errors, 'errors');
  70. } else {
  71. $book->numero_compte = $account_number;
  72. $book->code_tiers = $code_tiers;
  73. $book->label_compte = $label_compte;
  74. $book->debit = $debit;
  75. $book->credit = $credit;
  76. if (floatval($debit) != 0.0) {
  77. $book->montant = $debit;
  78. $book->sens = 'D';
  79. }
  80. if (floatval($credit) != 0.0) {
  81. $book->montant = $credit;
  82. $book->sens = 'C';
  83. }
  84. $result = $book->update($user);
  85. if ($result < 0) {
  86. setEventMessages($book->error, $book->errors, 'errors');
  87. } else {
  88. setEventMessages($langs->trans('Saved'), null, 'mesgs');
  89. $action = '';
  90. }
  91. }
  92. }
  93. }
  94. else if ($action == "add") {
  95. $error = 0;
  96. if ((floatval($debit) != 0.0) && (floatval($credit) != 0.0)) {
  97. setEventMessages($langs->trans('ErrorDebitCredit'), null, 'errors');
  98. $error ++;
  99. }
  100. if (empty($error)) {
  101. $book = new BookKeeping($db);
  102. $book->numero_compte = $account_number;
  103. $book->code_tiers = $code_tiers;
  104. $book->label_compte = $label_compte;
  105. $book->debit = $debit;
  106. $book->credit = $credit;
  107. $book->doc_date = GETPOST('doc_date');
  108. $book->doc_type = GETPOST('doc_type');
  109. $book->piece_num = $piece_num;
  110. $book->doc_ref = GETPOST('doc_ref');
  111. $book->code_journal = GETPOST('code_journal');
  112. $book->fk_doc = GETPOST('fk_doc');
  113. $book->fk_docdet = GETPOST('fk_docdet');
  114. if (floatval($debit) != 0.0) {
  115. $book->montant = $debit;
  116. $book->sens = 'D';
  117. }
  118. if (floatval($credit) != 0.0) {
  119. $book->montant = $credit;
  120. $book->sens = 'C';
  121. }
  122. $result = $book->createStd($user);
  123. if ($result < 0) {
  124. setEventMessages($book->error, $book->errors, 'errors');
  125. } else {
  126. setEventMessages($langs->trans('Saved'), null, 'mesgs');
  127. $action = '';
  128. }
  129. }
  130. }
  131. else if ($action == "confirm_delete") {
  132. $book = new BookKeeping($db);
  133. $result = $book->fetch($id);
  134. $piece_num = $book->piece_num;
  135. if ($result < 0) {
  136. setEventMessages($book->error, $book->errors, 'errors');
  137. } else {
  138. $result = $book->delete($user);
  139. if ($result < 0) {
  140. setEventMessages($book->error, $book->errors, 'errors');
  141. }
  142. }
  143. $action = '';
  144. }
  145. else if ($action == "confirm_create") {
  146. $error = 0;
  147. $book = new BookKeeping($db);
  148. if (! GETPOST('next_num_mvt'))
  149. {
  150. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NumPiece")), null, 'errors');
  151. $error++;
  152. }
  153. if (! $error)
  154. {
  155. $book->label_compte = '';
  156. $book->debit = 0;
  157. $book->credit = 0;
  158. $book->doc_date = $date_start = dol_mktime(0, 0, 0, GETPOST('doc_datemonth'), GETPOST('doc_dateday'), GETPOST('doc_dateyear'));
  159. $book->doc_type = GETPOST('doc_type');
  160. $book->piece_num = GETPOST('next_num_mvt');
  161. $book->doc_ref = GETPOST('doc_ref');
  162. $book->code_journal = GETPOST('code_journal');
  163. $book->fk_doc = 0;
  164. $book->fk_docdet = 0;
  165. $book->montant = 0;
  166. $result = $book->createStd($user);
  167. if ($result < 0) {
  168. setEventMessages($book->error, $book->errors, 'errors');
  169. } else {
  170. setEventMessages($langs->trans('Saved'), null, 'mesgs');
  171. $action = '';
  172. $piece_num = $book->piece_num;
  173. }
  174. }
  175. }
  176. /*
  177. * View
  178. */
  179. llxHeader();
  180. $html = new Form($db);
  181. $formaccountancy = new FormAccounting($db);
  182. /*
  183. * Confirmation to delete the command
  184. */
  185. if ($action == 'delete') {
  186. $formconfirm = $html->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id, $langs->trans('DeleteMvt'), $langs->trans('ConfirmDeleteMvt'), 'confirm_delete', '', 0, 1);
  187. print $formconfirm;
  188. }
  189. if ($action == 'create') {
  190. print load_fiche_titre($langs->trans("CreateMvts"));
  191. $book = new BookKeeping($db);
  192. $next_num_mvt = $book->getNextNumMvt();
  193. if (empty($next_num_mvt))
  194. {
  195. dol_print_error('', 'Failed to get next piece number');
  196. }
  197. print '<form action="' . $_SERVER["PHP_SELF"] . '" name="create_mvt" method="POST">';
  198. print '<input type="hidden" name="action" value="confirm_create">' . "\n";
  199. print '<input type="hidden" name="next_num_mvt" value="' . $next_num_mvt . '">' . "\n";
  200. dol_fiche_head();
  201. print '<table class="border" width="100%">';
  202. print '<tr>';
  203. print '<td class="titlefieldcreate fieldrequired">' . $langs->trans("NumPiece") . '</td>';
  204. print '<td>' . $next_num_mvt . '</td>';
  205. print '</tr>';
  206. print '<tr>';
  207. print '<td class="fieldrequired">' . $langs->trans("Docdate") . '</td>';
  208. print '<td>';
  209. print $html->select_date('', 'doc_date', '', '', '', "create_mvt", 1, 1);
  210. print '</td>';
  211. print '</tr>';
  212. print '<tr>';
  213. print '<td class="fieldrequired">'.$langs->trans("AccountancyJournal").'</td>';
  214. print '<td>';
  215. print $formaccountancy->select_journal('', 'code_journal', '', 0, '', 1, 1, 1, 1);
  216. print '</td></tr>';
  217. print '<tr>';
  218. print '<td>' . $langs->trans("Docref") . '</td>';
  219. print '<td><input type="text" class="minwidth200" name="doc_ref" value=""/></td>';
  220. print '</tr>';
  221. print '<tr>';
  222. print '<td>' . $langs->trans("Doctype") . '</td>';
  223. print '<td><input type="text" class="minwidth200" name="doc_type" value=""/></td>';
  224. print '</tr>';
  225. print '</table>';
  226. dol_fiche_end();
  227. print '<div align="center"><input type="submit" class="button" value="' . $langs->trans("Create") . '">';
  228. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="' . $langs->trans("Cancel") . '" class="button" onclick="history.go(-1)" />';
  229. print '</div>';
  230. print '</form>';
  231. } else {
  232. $book = new BookKeeping($db);
  233. $result = $book->fetchPerMvt($piece_num);
  234. if ($result < 0) {
  235. setEventMessages($book->error, $book->errors, 'errors');
  236. }
  237. if (! empty($book->piece_num)) {
  238. print load_fiche_titre($langs->trans("UpdateMvts"), '<a href="list.php">' . $langs->trans('BackToList') . '</a>');
  239. dol_fiche_head();
  240. print '<div class="fichecenter">';
  241. print '<table class="border" width="100%">';
  242. print '<tr class="pair">';
  243. print '<td class="titlefield">' . $langs->trans("NumMvts") . '</td>';
  244. print '<td>' . $book->piece_num . '</td>';
  245. print '</tr>';
  246. print '<tr class="impair">';
  247. print '<td>' . $langs->trans("Docdate") . '</td>';
  248. print '<td>' . dol_print_date($book->doc_date, 'daytextshort') . '</td>';
  249. print '</tr>';
  250. print '<tr class="pair">';
  251. print '<td>' . $langs->trans("Codejournal") . '</td>';
  252. print '<td>';
  253. $accountingjournal = new AccountingJournal($db);
  254. $accountingjournal->fetch('',$book->code_journal);
  255. print $accountingjournal->getNomUrl(0,1,1,'',1);
  256. print '</td></tr>';
  257. print '<tr class="impair">';
  258. print '<td>' . $langs->trans("Docref") . '</td>';
  259. print '<td>' . $book->doc_ref . '</td>';
  260. print '</tr>';
  261. $typelabel = $book->doc_type;
  262. if ($typelabel == 'bank') {
  263. $typelabel = $langs->trans('Bank');
  264. }
  265. if ($typelabel == 'customer_invoice') {
  266. $typelabel = $langs->trans('CustomerInvoice');
  267. }
  268. if ($typelabel == 'supplier_invoice') {
  269. $typelabel = $langs->trans('SupplierInvoice');
  270. }
  271. if ($typelabel == 'expense_report') {
  272. $typelabel = $langs->trans('ExpenseReport');
  273. }
  274. print '<tr class="pair">';
  275. print '<td>' . $langs->trans("Doctype") . '</td>';
  276. print '<td>' . $typelabel . '</td>';
  277. print '</tr>';
  278. print '</table>';
  279. print '</div>';
  280. dol_fiche_end();
  281. print '<br>';
  282. $result = $book->fetch_all_per_mvt($piece_num);
  283. if ($result < 0) {
  284. setEventMessages($book->error, $book->errors, 'errors');
  285. } else {
  286. print load_fiche_titre($langs->trans("ListeMvts"), '', '');
  287. print '<form action="' . $_SERVER["PHP_SELF"] . '?piece_num=' . $book->piece_num . '" method="post">';
  288. print '<input type="hidden" name="doc_date" value="' . $book->doc_date . '">' . "\n";
  289. print '<input type="hidden" name="doc_type" value="' . $book->doc_type . '">' . "\n";
  290. print '<input type="hidden" name="doc_ref" value="' . $book->doc_ref . '">' . "\n";
  291. print '<input type="hidden" name="code_journal" value="' . $book->code_journal . '">' . "\n";
  292. print '<input type="hidden" name="fk_doc" value="' . $book->fk_doc . '">' . "\n";
  293. print '<input type="hidden" name="fk_docdet" value="' . $book->fk_docdet . '">' . "\n";
  294. $var=False;
  295. print "<table class=\"noborder\" width=\"100%\">";
  296. if (count($book->linesmvt) > 0) {
  297. $total_debit = 0;
  298. $total_credit = 0;
  299. print '<tr class="liste_titre">';
  300. print_liste_field_titre($langs->trans("AccountAccountingShort"));
  301. print_liste_field_titre($langs->trans("Code_tiers"));
  302. print_liste_field_titre($langs->trans("Labelcompte"));
  303. print_liste_field_titre($langs->trans("Debit"), "", "", "", "", 'align="right"');
  304. print_liste_field_titre($langs->trans("Credit"), "", "", "", "", 'align="right"');
  305. print_liste_field_titre($langs->trans("Amount"), "", "", "", "", 'align="right"');
  306. print_liste_field_titre($langs->trans("Sens"), "", "", "", "", 'align="center"');
  307. print_liste_field_titre($langs->trans("Action"), "", "", "", "", 'width="60" align="center"');
  308. print "</tr>\n";
  309. foreach ($book->linesmvt as $line) {
  310. print '<tr class="oddeven">';
  311. $total_debit += $line->debit;
  312. $total_credit += $line->credit;
  313. if ($action == 'update' && $line->id == $id) {
  314. print '<td>';
  315. print $formaccounting->select_account($line->numero_compte, 'account_number', 0, array (), 1, 1, '');
  316. print '</td>';
  317. print '<td>';
  318. print $formaccounting->select_auxaccount($line->code_tiers, 'code_tiers', 1);
  319. print '</td>';
  320. print '<td><input type="text" size="15" name="label_compte" value="' . $line->label_compte . '"/></td>';
  321. print '<td align="right"><input type="text" size="6" name="debit" value="' . price($line->debit) . '"/></td>';
  322. print '<td align="right"><input type="text" size="6" name="credit" value="' . price($line->credit) . '"/></td>';
  323. print '<td align="right">' . price($line->montant) . '</td>';
  324. print '<td align="center">' . $line->sens . '</td>';
  325. print '<td>';
  326. print '<input type="hidden" name="id" value="' . $line->id . '">' . "\n";
  327. print '<input type="submit" class="button" name="update" value="' . $langs->trans("Update") . '">';
  328. print '</td>';
  329. } else {
  330. print '<td>' . length_accountg($line->numero_compte) . '</td>';
  331. print '<td>' . length_accounta($line->code_tiers) . '</td>';
  332. print '<td>' . $line->label_compte . '</td>';
  333. print '<td align="right">' . price($line->debit) . '</td>';
  334. print '<td align="right">' . price($line->credit) . '</td>';
  335. print '<td align="right">' . price($line->montant) . '</td>';
  336. print '<td align="center">' . $line->sens . '</td>';
  337. print '<td align="center">';
  338. print '<a href="./card.php?action=update&amp;id=' . $line->id . '&amp;piece_num=' . $line->piece_num . '">';
  339. print img_edit();
  340. print '</a>&nbsp;';
  341. print '<a href="./card.php?action=delete&amp;id=' . $line->id . '&amp;piece_num=' . $line->piece_num . '">';
  342. print img_delete();
  343. print '</a>';
  344. print '</td>';
  345. }
  346. print "</tr>\n";
  347. }
  348. if ($total_debit != $total_credit)
  349. {
  350. setEventMessages(null, array($langs->trans('MvtNotCorrectlyBalanced', $total_credit, $total_debit)), 'warnings');
  351. }
  352. if ($action == "" || $action == 'add') {
  353. print '<tr class="oddeven">';
  354. print '<td>';
  355. print $formaccounting->select_account($account_number, 'account_number', 0, array (), 1, 1, '');
  356. print '</td>';
  357. print '<td>';
  358. print $formaccounting->select_auxaccount($code_tiers, 'code_tiers', 1);
  359. print '</td>';
  360. print '<td><input type="text" size="15" name="label_compte" value="' . $label_compte . '"/></td>';
  361. print '<td align="right"><input type="text" class="right maxwidth50" name="debit" value="' . price($debit) . '"/></td>';
  362. print '<td align="right"><input type="text" class="right maxwidth50" name="credit" value="' . price($credit) . '"/></td>';
  363. print '<td></td>';
  364. print '<td></td>';
  365. print '<td><input type="submit" class="button" name="save" value="' . $langs->trans("Add") . '"></td>';
  366. print '</tr>';
  367. }
  368. print '</table>';
  369. print '</form>';
  370. }
  371. }
  372. } else {
  373. print load_fiche_titre($langs->trans("NoRecords"));
  374. }
  375. }
  376. llxFooter();
  377. $db->close();