card.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /* Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/compta/bank/various_expenses/card.php
  20. * \ingroup bank
  21. * \brief Page of various expenses
  22. */
  23. require '../../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/paymentvarious.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
  31. if (! empty($conf->projet->enabled))
  32. {
  33. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  35. }
  36. // Load translation files required by the page
  37. $langs->loadLangs(array("compta", "banks", "bills", "users", "accountancy"));
  38. // Get parameters
  39. $id = GETPOST('id', 'int');
  40. $action = GETPOST('action', 'alpha');
  41. $cancel = GETPOST('cancel', 'aZ09');
  42. $backtopage = GETPOST('backtopage', 'alpha');
  43. $accountid=GETPOST("accountid") > 0 ? GETPOST("accountid","int") : 0;
  44. $label=GETPOST("label","alpha");
  45. $sens=GETPOST("sens","int");
  46. $amount=GETPOST("amount");
  47. $paymenttype=GETPOST("paymenttype");
  48. $accountancy_code=GETPOST("accountancy_code","int");
  49. $projectid = (GETPOST('projectid','int') ? GETPOST('projectid', 'int') : GETPOST('fk_project','int'));
  50. // Security check
  51. $socid = GETPOST("socid","int");
  52. if ($user->societe_id) $socid=$user->societe_id;
  53. $result = restrictedArea($user, 'banque', '', '', '');
  54. $object = new PaymentVarious($db);
  55. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  56. $hookmanager->initHooks(array('variouscard','globalcard'));
  57. /**
  58. * Actions
  59. */
  60. $parameters=array();
  61. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  62. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  63. if (empty($reshook))
  64. {
  65. // Link to a project
  66. if ($action == 'classin' && $user->rights->banque->modifier)
  67. {
  68. $object->fetch($id);
  69. $object->setProject(GETPOST('projectid'));
  70. }
  71. if ($cancel)
  72. {
  73. if ($action != 'addlink')
  74. {
  75. $urltogo=$backtopage?$backtopage:dol_buildpath('/compta/bank/various_payment/list.php',1);
  76. header("Location: ".$urltogo);
  77. exit;
  78. }
  79. if ($id > 0 || ! empty($ref)) $ret = $object->fetch($id,$ref);
  80. $action='';
  81. }
  82. if ($action == 'add')
  83. {
  84. $error=0;
  85. $datep=dol_mktime(12,0,0, GETPOST("datepmonth",'int'), GETPOST("datepday",'int'), GETPOST("datepyear",'int'));
  86. $datev=dol_mktime(12,0,0, GETPOST("datevmonth",'int'), GETPOST("datevday",'int'), GETPOST("datevyear",'int'));
  87. if (empty($datev)) $datev=$datep;
  88. $object->ref=''; // TODO
  89. $object->accountid=GETPOST("accountid",'int') > 0 ? GETPOST("accountid","int") : 0;
  90. $object->datev=$datev;
  91. $object->datep=$datep;
  92. $object->amount=price2num(GETPOST("amount",'alpha'));
  93. $object->label=GETPOST("label",'none');
  94. $object->note=GETPOST("note",'none');
  95. $object->type_payment=GETPOST("paymenttype",'int') > 0 ? GETPOST("paymenttype", "int") : 0;
  96. $object->num_payment=GETPOST("num_payment",'alpha');
  97. $object->fk_user_author=$user->id;
  98. $object->accountancy_code=GETPOST("accountancy_code") > 0 ? GETPOST("accountancy_code","int") : "";
  99. $object->sens=GETPOST('sens');
  100. $object->fk_project= GETPOST('fk_project','int');
  101. if (empty($datep) || empty($datev))
  102. {
  103. $langs->load('errors');
  104. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
  105. $error++;
  106. }
  107. if (empty($object->type_payment) || $object->type_payment < 0)
  108. {
  109. $langs->load('errors');
  110. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PaymentMode")), null, 'errors');
  111. $error++;
  112. }
  113. if (empty($object->amount))
  114. {
  115. $langs->load('errors');
  116. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Amount")), null, 'errors');
  117. $error++;
  118. }
  119. if (! empty($conf->banque->enabled) && ! $object->accountid > 0)
  120. {
  121. $langs->load('errors');
  122. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
  123. $error++;
  124. }
  125. if (! empty($conf->accounting->enabled) && ! $object->accountancy_code)
  126. {
  127. $langs->load('errors');
  128. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("AccountAccounting")), null, 'errors');
  129. $error++;
  130. }
  131. if (! $error)
  132. {
  133. $db->begin();
  134. $ret=$object->create($user);
  135. if ($ret > 0)
  136. {
  137. $db->commit();
  138. $urltogo=($backtopage ? $backtopage : DOL_URL_ROOT.'/compta/bank/various_payment/list.php');
  139. header("Location: ".$urltogo);
  140. exit;
  141. }
  142. else
  143. {
  144. $db->rollback();
  145. setEventMessages($object->error, $object->errors, 'errors');
  146. $action="create";
  147. }
  148. }
  149. $action='create';
  150. }
  151. if ($action == 'delete')
  152. {
  153. $result=$object->fetch($id);
  154. if ($object->rappro == 0)
  155. {
  156. $db->begin();
  157. $ret=$object->delete($user);
  158. if ($ret > 0)
  159. {
  160. if ($object->fk_bank)
  161. {
  162. $accountline=new AccountLine($db);
  163. $result=$accountline->fetch($object->fk_bank);
  164. 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)
  165. }
  166. if ($result >= 0)
  167. {
  168. $db->commit();
  169. header("Location: ".DOL_URL_ROOT.'/compta/bank/various_payment/list.php');
  170. exit;
  171. }
  172. else
  173. {
  174. $object->error=$accountline->error;
  175. $db->rollback();
  176. setEventMessages($object->error, $object->errors, 'errors');
  177. }
  178. }
  179. else
  180. {
  181. $db->rollback();
  182. setEventMessages($object->error, $object->errors, 'errors');
  183. }
  184. }
  185. else
  186. {
  187. setEventMessages('Error try do delete a line linked to a conciliated bank transaction', null, 'errors');
  188. }
  189. }
  190. }
  191. /*
  192. * View
  193. */
  194. llxHeader("",$langs->trans("VariousPayment"));
  195. $form = new Form($db);
  196. if (! empty($conf->accounting->enabled)) $formaccounting = new FormAccounting($db);
  197. if (! empty($conf->projet->enabled)) $formproject = new FormProjets($db);
  198. if ($id)
  199. {
  200. $object = new PaymentVarious($db);
  201. $result = $object->fetch($id);
  202. if ($result <= 0)
  203. {
  204. dol_print_error($db);
  205. exit;
  206. }
  207. }
  208. /* ************************************************************************** */
  209. /* */
  210. /* Create mode */
  211. /* */
  212. /* ************************************************************************** */
  213. if ($action == 'create')
  214. {
  215. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  216. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  217. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  218. print '<input type="hidden" name="action" value="add">';
  219. print load_fiche_titre($langs->trans("NewVariousPayment"),'', 'title_accountancy.png');
  220. dol_fiche_head('', '');
  221. print '<table class="border" width="100%">';
  222. // Date payment
  223. print '<tr><td>';
  224. print $form->editfieldkey('DatePayment', 'datep', '', $object, 0, 'string', '', 1).'</td><td>';
  225. print $form->selectDate((empty($datep)?-1:$datep),"datep",'','','','add',1,1);
  226. print '</td></tr>';
  227. // Date value for bank
  228. print '<tr><td>';
  229. print $form->editfieldkey('DateValue', 'datev', '', $object, 0).'</td><td>';
  230. print $form->selectDate((empty($datev)?-1:$datev),"datev",'','','','add',1,1);
  231. print '</td></tr>';
  232. // Label
  233. print '<tr><td>';
  234. print $form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).'</td><td>';
  235. print '<input name="label" id="label" class="minwidth300" value="'.($label?$label:$langs->trans("VariousPayment")).'">';
  236. print '</td></tr>';
  237. // Sens
  238. print '<tr><td>';
  239. print $form->editfieldkey('Sens', 'sens', '', $object, 0, 'string', '', 1).'</td><td>';
  240. $sensarray=array( '0' => $langs->trans("Debit"), '1' => $langs->trans("Credit"));
  241. print $form->selectarray('sens',$sensarray,$sens);
  242. print '</td></tr>';
  243. // Amount
  244. print '<tr><td>';
  245. print $form->editfieldkey('Amount', 'amount', '', $object, 0, 'string', '', 1).'</td><td>';
  246. print '<input name="amount" id="amount" class="minwidth100" value="'.$amount.'">';
  247. print '</td></tr>';
  248. // Bank
  249. if (! empty($conf->banque->enabled))
  250. {
  251. print '<tr><td>';
  252. print $form->editfieldkey('BankAccount', 'selectaccountid', '', $object, 0, 'string', '', 1).'</td><td>';
  253. $form->select_comptes($accountid,"accountid",0,'',1); // Affiche liste des comptes courant
  254. print '</td></tr>';
  255. }
  256. // Type payment
  257. print '<tr><td>';
  258. print $form->editfieldkey('PaymentMode', 'selectpaymenttype', '', $object, 0, 'string', '', 1).'</td><td>';
  259. $form->select_types_paiements($paymenttype, "paymenttype");
  260. print '</td></tr>';
  261. // Number
  262. if (! empty($conf->banque->enabled))
  263. {
  264. // Number
  265. print '<tr><td><label for="num_payment">'.$langs->trans('Numero');
  266. print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
  267. print '</label></td>';
  268. print '<td><input name="num_payment" id="num_payment" type="text" value="'.GETPOST("num_payment").'"></td></tr>'."\n";
  269. }
  270. // Accountancy account
  271. if (! empty($conf->accounting->enabled))
  272. {
  273. print '<tr><td class="fieldrequired">'.$langs->trans("AccountAccounting").'</td>';
  274. print '<td>';
  275. print $formaccounting->select_account($accountancy_code, 'accountancy_code', 1, null, 1, 1, '');
  276. print '</td></tr>';
  277. }
  278. else // For external software
  279. {
  280. print '<tr><td>'.$langs->trans("AccountAccounting").'</td>';
  281. print '<td class="maxwidthonsmartphone"><input class="minwidth100" name="accountancy_code" value="'.$accountancy_code.'">';
  282. print '</td></tr>';
  283. }
  284. // Project
  285. if (! empty($conf->projet->enabled))
  286. {
  287. $formproject=new FormProjets($db);
  288. // Associated project
  289. $langs->load("projects");
  290. print '<tr><td>'.$langs->trans("Project").'</td><td>';
  291. $numproject=$formproject->select_projects(-1, $projectid,'fk_project',0,0,1,1);
  292. print '</td></tr>';
  293. }
  294. // Other attributes
  295. $parameters=array();
  296. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  297. print $hookmanager->resPrint;
  298. print '</table>';
  299. dol_fiche_end();
  300. print '<div class="center">';
  301. print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
  302. print ' &nbsp; ';
  303. print '<input type="button" class="button" value="'.$langs->trans("Cancel").'" onclick="javascript:history.go(-1)">';
  304. print '</div>';
  305. print '</form>';
  306. }
  307. /* ************************************************************************** */
  308. /* */
  309. /* View mode */
  310. /* */
  311. /* ************************************************************************** */
  312. if ($id)
  313. {
  314. $head=various_payment_prepare_head($object);
  315. dol_fiche_head($head, 'card', $langs->trans("VariousPayment"), -1, 'payment');
  316. $morehtmlref='<div class="refidno">';
  317. // Project
  318. if (! empty($conf->projet->enabled))
  319. {
  320. $langs->load("projects");
  321. $morehtmlref.=$langs->trans('Project') . ' ';
  322. if ($user->rights->banque->modifier)
  323. {
  324. if ($action != 'classify')
  325. $morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  326. if ($action == 'classify') {
  327. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  328. $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  329. $morehtmlref.='<input type="hidden" name="action" value="classin">';
  330. $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  331. $morehtmlref.=$formproject->select_projects(0, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  332. $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  333. $morehtmlref.='</form>';
  334. } else {
  335. $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
  336. }
  337. } else {
  338. if (! empty($object->fk_project)) {
  339. $proj = new Project($db);
  340. $proj->fetch($object->fk_project);
  341. $morehtmlref.=$proj->getNomUrl(1);
  342. } else {
  343. $morehtmlref.='';
  344. }
  345. }
  346. }
  347. $morehtmlref.='</div>';
  348. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/various_payment/list.php?restore_lastsearch_values=1'.(! empty($socid)?'&socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  349. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
  350. print '<div class="fichecenter">';
  351. print '<div class="underbanner clearboth"></div>';
  352. print '<table class="border" width="100%">';
  353. // Label
  354. print '<tr><td class="titlefield">'.$langs->trans("Label").'</td><td>'.$object->label.'</td></tr>';
  355. // Payment date
  356. print "<tr>";
  357. print '<td>'.$langs->trans("DatePayment").'</td><td>';
  358. print dol_print_date($object->datep,'day');
  359. print '</td></tr>';
  360. // Value date
  361. print '<tr><td>'.$langs->trans("DateValue").'</td><td>';
  362. print dol_print_date($object->datev,'day');
  363. print '</td></tr>';
  364. // Debit / Credit
  365. if ($object->sens == '1') $sens = $langs->trans("Credit"); else $sens = $langs->trans("Debit");
  366. print '<tr><td>'.$langs->trans("Sens").'</td><td>'.$sens.'</td></tr>';
  367. print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($object->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
  368. // Accountancy code
  369. print '<tr><td class="nowrap">';
  370. print $langs->trans("AccountAccounting");
  371. print '</td><td>';
  372. if (! empty($conf->accounting->enabled))
  373. {
  374. $accountingaccount = new AccountingAccount($db);
  375. $accountingaccount->fetch('',$object->accountancy_code, 1);
  376. print $accountingaccount->getNomUrl(0,1,1,'',1);
  377. } else {
  378. print $object->accountancy_code;
  379. }
  380. print '</td></tr>';
  381. if (! empty($conf->banque->enabled))
  382. {
  383. if ($object->fk_account > 0)
  384. {
  385. $bankline=new AccountLine($db);
  386. $bankline->fetch($object->fk_bank);
  387. print '<tr>';
  388. print '<td>'.$langs->trans('BankTransactionLine').'</td>';
  389. print '<td colspan="3">';
  390. print $bankline->getNomUrl(1,0,'showall');
  391. print '</td>';
  392. print '</tr>';
  393. }
  394. }
  395. // Other attributes
  396. $parameters=array('socid'=>$object->id);
  397. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  398. print '</table>';
  399. print '</div>';
  400. print '</div>';
  401. print '<div class="clearboth"></div>';
  402. dol_fiche_end();
  403. /*
  404. * Action buttons
  405. */
  406. print '<div class="tabsAction">'."\n";
  407. if ($object->rappro == 0)
  408. {
  409. if (! empty($user->rights->banque->modifier))
  410. {
  411. print '<a class="butActionDelete" href="card.php?id='.$object->id.'&action=delete">'.$langs->trans("Delete").'</a>';
  412. }
  413. else
  414. {
  415. print '<a class="butActionRefused classfortooltip" href="#" title="'.(dol_escape_htmltag($langs->trans("NotAllowed"))).'">'.$langs->trans("Delete").'</a>';
  416. }
  417. }
  418. else
  419. {
  420. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("LinkedToAConciliatedTransaction").'">'.$langs->trans("Delete").'</a>';
  421. }
  422. print "</div>";
  423. }
  424. // End of page
  425. llxFooter();
  426. $db->close();