card.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /* Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/compta/bank/various_expenses/card.php
  19. * \ingroup bank
  20. * \brief Page of various expenses
  21. */
  22. require '../../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/paymentvarious.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  27. if (! empty($conf->accounting->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  28. if (! empty($conf->accounting->enabled)) require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  29. $langs->load("compta");
  30. $langs->load("banks");
  31. $langs->load("bills");
  32. $langs->load("users");
  33. $langs->load("accountancy");
  34. $id=GETPOST("id",'int');
  35. $action=GETPOST('action','alpha');
  36. $cancel=GETPOST('cancel','alpha');
  37. $accountid=GETPOST("accountid") > 0 ? GETPOST("accountid","int") : 0;
  38. $label=GETPOST("label","alpha");
  39. $sens=GETPOST("sens","int");
  40. $amount=GETPOST("amount");
  41. $paymenttype=GETPOST("paymenttype");
  42. $accountancy_code=GETPOST("accountancy_code","int");
  43. // Security check
  44. $socid = GETPOST("socid","int");
  45. if ($user->societe_id) $socid=$user->societe_id;
  46. $result = restrictedArea($user, 'banque', '', '', '');
  47. $object = new PaymentVarious($db);
  48. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  49. $hookmanager->initHooks(array('variouscard','globalcard'));
  50. /**
  51. * Actions
  52. */
  53. if (! empty($cancel))
  54. {
  55. header("Location: index.php");
  56. exit;
  57. }
  58. if ($action == 'add' && empty($cancel))
  59. {
  60. $error=0;
  61. $datep=dol_mktime(12,0,0, GETPOST("datepmonth"), GETPOST("datepday"), GETPOST("datepyear"));
  62. $datev=dol_mktime(12,0,0, GETPOST("datevmonth"), GETPOST("datevday"), GETPOST("datevyear"));
  63. if (empty($datev)) $datev=$datep;
  64. $object->accountid=GETPOST("accountid") > 0 ? GETPOST("accountid","int") : 0;
  65. $object->datev=$datev;
  66. $object->datep=$datep;
  67. $object->amount=price2num(GETPOST("amount"));
  68. $object->label=GETPOST("label");
  69. $object->note=GETPOST("note");
  70. $object->type_payment=GETPOST("paymenttype") > 0 ? GETPOST("paymenttype", "int") : 0;
  71. $object->num_payment=GETPOST("num_payment");
  72. $object->fk_user_author=$user->id;
  73. $object->accountancy_code=GETPOST("accountancy_code") > 0 ? GETPOST("accountancy_code","int") : "";
  74. if (empty($datep) || empty($datev))
  75. {
  76. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
  77. $error++;
  78. }
  79. if (empty($object->type_payment) || $object->type_payment < 0)
  80. {
  81. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PaymentMode")), null, 'errors');
  82. $error++;
  83. }
  84. if (empty($object->amount))
  85. {
  86. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Amount")), null, 'errors');
  87. $error++;
  88. }
  89. if (! empty($conf->banque->enabled) && ! $object->accountid > 0)
  90. {
  91. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
  92. $error++;
  93. }
  94. if (! $error)
  95. {
  96. $db->begin();
  97. $ret=$object->create($user);
  98. if ($ret > 0)
  99. {
  100. $db->commit();
  101. header("Location: index.php");
  102. exit;
  103. }
  104. else
  105. {
  106. $db->rollback();
  107. setEventMessages($object->error, $object->errors, 'errors');
  108. $action="create";
  109. }
  110. }
  111. $action='create';
  112. }
  113. if ($action == 'delete')
  114. {
  115. $result=$object->fetch($id);
  116. if ($object->rappro == 0)
  117. {
  118. $db->begin();
  119. $ret=$object->delete($user);
  120. if ($ret > 0)
  121. {
  122. if ($object->fk_bank)
  123. {
  124. $accountline=new AccountLine($db);
  125. $result=$accountline->fetch($object->fk_bank);
  126. if ($result > 0) $result=$accountline->delete($user); // $result may be 0 if not found (when bank entry was deleted manually and fk_bank point to nothing)
  127. }
  128. if ($result >= 0)
  129. {
  130. $db->commit();
  131. header("Location: ".DOL_URL_ROOT.'/compta/salaries/index.php');
  132. exit;
  133. }
  134. else
  135. {
  136. $object->error=$accountline->error;
  137. $db->rollback();
  138. setEventMessages($object->error, $object->errors, 'errors');
  139. }
  140. }
  141. else
  142. {
  143. $db->rollback();
  144. setEventMessages($object->error, $object->errors, 'errors');
  145. }
  146. }
  147. else
  148. {
  149. setEventMessages('Error try do delete a line linked to a conciliated bank transaction', null, 'errors');
  150. }
  151. }
  152. /*
  153. * View
  154. */
  155. llxHeader("",$langs->trans("VariousPayment"));
  156. $form = new Form($db);
  157. if (! empty($conf->accounting->enabled)) $formaccounting = New FormAccounting($db);
  158. if ($id)
  159. {
  160. $object = new PaymentVarious($db);
  161. $result = $object->fetch($id);
  162. if ($result <= 0)
  163. {
  164. dol_print_error($db);
  165. exit;
  166. }
  167. }
  168. /* ************************************************************************** */
  169. /* */
  170. /* Create mode */
  171. /* */
  172. /* ************************************************************************** */
  173. if ($action == 'create')
  174. {
  175. print '<form name="salary" action="'.$_SERVER["PHP_SELF"].'" method="post">';
  176. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  177. print '<input type="hidden" name="action" value="add">';
  178. print load_fiche_titre($langs->trans("NewVariousPayment"),'', 'title_accountancy.png');
  179. dol_fiche_head('', '');
  180. print '<table class="border" width="100%">';
  181. // Date payment
  182. print '<tr><td>';
  183. print fieldLabel('DatePayment','datep',1).'</td><td>';
  184. print $form->select_date((empty($datep)?-1:$datep),"datep",'','','','add',1,1);
  185. print '</td></tr>';
  186. // Date value for bank
  187. print '<tr><td>';
  188. print fieldLabel('DateValue','datev',0).'</td><td>';
  189. print $form->select_date((empty($datev)?-1:$datev),"datev",'','','','add',1,1);
  190. print '</td></tr>';
  191. // Label
  192. print '<tr><td>';
  193. print fieldLabel('Label','label',1).'</td><td>';
  194. print '<input name="label" id="label" class="minwidth300" value="'.($label?$label:$langs->trans("VariousPayment")).'">';
  195. print '</td></tr>';
  196. // Sens
  197. print '<tr><td>';
  198. print fieldLabel('Sens','sens',1).'</td><td>';
  199. $sensarray=array( '0' => $langs->trans("Debit"), '1' => $langs->trans("Credit"));
  200. print $form->selectarray('sens',$sensarray,$sens);
  201. print '</td></tr>';
  202. // Amount
  203. print '<tr><td>';
  204. print fieldLabel('Amount','amount',1).'</td><td>';
  205. print '<input name="amount" id="amount" class="minwidth100" value="'.$amount.'">';
  206. print '</td></tr>';
  207. // Bank
  208. if (! empty($conf->banque->enabled))
  209. {
  210. print '<tr><td>';
  211. print fieldLabel('BankAccount','selectaccountid',1).'</td><td>';
  212. $form->select_comptes($accountid,"accountid",0,'',1); // Affiche liste des comptes courant
  213. print '</td></tr>';
  214. }
  215. // Type payment
  216. print '<tr><td>';
  217. print fieldLabel('PaymentMode','selectpaymenttype',1).'</td><td>';
  218. $form->select_types_paiements($paymenttype, "paymenttype");
  219. print '</td></tr>';
  220. // Number
  221. if (! empty($conf->banque->enabled))
  222. {
  223. // Number
  224. print '<tr><td><label for="num_payment">'.$langs->trans('Numero');
  225. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  226. print '</label></td>';
  227. print '<td><input name="num_payment" id="num_payment" type="text" value="'.GETPOST("num_payment").'"></td></tr>'."\n";
  228. }
  229. // Accountancy account
  230. if (! empty($conf->accounting->enabled))
  231. {
  232. print '<tr><td>'.$langs->trans("AccountAccounting").'</td>';
  233. print '<td>';
  234. print $formaccounting->select_account($accountancy_code, 'accountancy_code', 1, null, 1, 1, '');
  235. print '</td></tr>';
  236. }
  237. else // For external software
  238. {
  239. print '<tr><td>'.$langs->trans("AccountAccounting").'</td>';
  240. print '<td class="maxwidthonsmartphone"><input class="minwidth100" name="accountancy_code" value="'.$accountancy_code.'">';
  241. print '</td></tr>';
  242. }
  243. // Other attributes
  244. $parameters=array('colspan' => ' colspan="1"');
  245. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  246. print '</table>';
  247. dol_fiche_end();
  248. print '<div class="center">';
  249. print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
  250. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  251. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  252. print '</div>';
  253. print '</form>';
  254. }
  255. /* ************************************************************************** */
  256. /* */
  257. /* View mode */
  258. /* */
  259. /* ************************************************************************** */
  260. if ($id)
  261. {
  262. $head=various_payment_prepare_head($object);
  263. dol_fiche_head($head, 'card', $langs->trans("VariousPayment"), 0, 'payment');
  264. print '<table class="border" width="100%">';
  265. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/various_payment/index.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  266. print "<tr>";
  267. print '<td class="titlefield">'.$langs->trans("Ref").'</td><td>';
  268. print $form->showrefnav($object, 'id', $linkback, 1, 'rowid', 'ref', '');
  269. print '</td></tr>';
  270. // Label
  271. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$object->label.'</td></tr>';
  272. print "<tr>";
  273. print '<td>'.$langs->trans("DatePayment").'</td><td>';
  274. print dol_print_date($object->datep,'day');
  275. print '</td></tr>';
  276. print '<tr><td>'.$langs->trans("DateValue").'</td><td>';
  277. print dol_print_date($object->datev,'day');
  278. print '</td></tr>';
  279. // Debit / Credit
  280. if ($object->sens == '1') $sens = $langs->trans("Credit"); else $sens = $langs->trans("Debit");
  281. print '<tr><td>'.$langs->trans("Sens").'</td><td>'.$sens.'</td></tr>';
  282. print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($object->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  283. // Accountancy code
  284. print '<tr><td class="nowrap">';
  285. print $langs->trans("AccountAccounting");
  286. print '</td><td>';
  287. if (! empty($conf->accounting->enabled))
  288. {
  289. $accountancyaccount = new AccountingAccount($db);
  290. $accountancyaccount->fetch('',$object->accountancy_code);
  291. print $accountancyaccount->getNomUrl(1);
  292. // print length_accountg($object->accountancy_code);
  293. } else {
  294. print $object->accountancy_code;
  295. }
  296. print '</td></tr>';
  297. if (! empty($conf->banque->enabled))
  298. {
  299. if ($object->fk_account > 0)
  300. {
  301. $bankline=new AccountLine($db);
  302. $bankline->fetch($object->fk_bank);
  303. print '<tr>';
  304. print '<td>'.$langs->trans('BankTransactionLine').'</td>';
  305. print '<td colspan="3">';
  306. print $bankline->getNomUrl(1,0,'showall');
  307. print '</td>';
  308. print '</tr>';
  309. }
  310. }
  311. // Other attributes
  312. $parameters=array('colspan' => ' colspan="1"');
  313. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  314. print '</table>';
  315. dol_fiche_end();
  316. /*
  317. * Action buttons
  318. */
  319. print '<div class="tabsAction">'."\n";
  320. if ($object->rappro == 0)
  321. {
  322. if (! empty($user->rights->banque->delete))
  323. {
  324. print '<a class="butActionDelete" href="card.php?id='.$object->id.'&action=delete">'.$langs->trans("Delete").'</a>';
  325. }
  326. else
  327. {
  328. print '<a class="butActionRefused" href="#" title="'.(dol_escape_htmltag($langs->trans("NotAllowed"))).'">'.$langs->trans("Delete").'</a>';
  329. }
  330. }
  331. else
  332. {
  333. print '<a class="butActionRefused" href="#" title="'.$langs->trans("LinkedToAConciliatedTransaction").'">'.$langs->trans("Delete").'</a>';
  334. }
  335. print "</div>";
  336. }
  337. llxFooter();
  338. $db->close();