card.php 16 KB

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