dolibarr  9.0.0
card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011-2018 Alexandre Spangaro <aspangaro@zendsi.com>
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 
28 require '../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/salaries/class/paymentsalary.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/salaries.lib.php';
33 if (! empty($conf->projet->enabled))
34 {
35  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
36  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
37 }
38 
39 // Load translation files required by the page
40 $langs->loadLangs(array("compta","banks","bills","users","salaries","hrm"));
41 if (! empty($conf->projet->enabled)) $langs->load("projects");
42 
43 $id=GETPOST("id",'int');
44 $action=GETPOST('action','aZ09');
45 $cancel= GETPOST('cancel', 'aZ09');
46 $projectid = (GETPOST('projectid','int') ? GETPOST('projectid', 'int') : GETPOST('fk_project','int'));
47 
48 // Security check
49 $socid = GETPOST("socid","int");
50 if ($user->societe_id) $socid=$user->societe_id;
51 $result = restrictedArea($user, 'salaries', '', '', '');
52 
53 $object = new PaymentSalary($db);
54 
55 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
56 $hookmanager->initHooks(array('salarycard','globalcard'));
57 
58 
59 
64 if ($cancel)
65 {
66  header("Location: list.php");
67  exit;
68 }
69 
70 // Link to a project
71 if ($action == 'classin' && $user->rights->banque->modifier)
72 {
73  $object->fetch($id);
74  $object->setProject(GETPOST('projectid'));
75 }
76 
77 if ($action == 'add' && empty($cancel))
78 {
79  $error=0;
80 
81  $datep=dol_mktime(12,0,0, GETPOST("datepmonth",'int'), GETPOST("datepday",'int'), GETPOST("datepyear",'int'));
82  $datev=dol_mktime(12,0,0, GETPOST("datevmonth",'int'), GETPOST("datevday",'int'), GETPOST("datevyear",'int'));
83  $datesp=dol_mktime(12,0,0, GETPOST("datespmonth",'int'), GETPOST("datespday",'int'), GETPOST("datespyear",'int'));
84  $dateep=dol_mktime(12,0,0, GETPOST("dateepmonth",'int'), GETPOST("dateepday",'int'), GETPOST("dateepyear",'int'));
85  if (empty($datev)) $datev=$datep;
86 
87  $type_payment = dol_getIdFromCode($db, GETPOST("paymenttype", 'alpha'), 'c_paiement', 'code', 'id', 1);
88 
89  $object->accountid=GETPOST("accountid") > 0 ? GETPOST("accountid","int") : 0;
90  $object->fk_user=GETPOST("fk_user") > 0 ? GETPOST("fk_user","int") : 0;
91  $object->datev=$datev;
92  $object->datep=$datep;
93  $object->amount=price2num(GETPOST("amount"));
94  $object->label=GETPOST("label");
95  $object->datesp=$datesp;
96  $object->dateep=$dateep;
97  $object->note=GETPOST("note");
98  $object->type_payment=($type_payment > 0 ? $type_payment : 0);
99  $object->num_payment=GETPOST("num_payment");
100  $object->fk_user_author=$user->id;
101  $object->fk_project= GETPOST('fk_project','int');
102 
103  // Set user current salary as ref salaray for the payment
104  $fuser=new User($db);
105  $fuser->fetch(GETPOST("fk_user","int"));
106  $object->salary=$fuser->salary;
107 
108  if (empty($datep) || empty($datev) || empty($datesp) || empty($dateep))
109  {
110  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
111  $error++;
112  }
113  if (empty($object->fk_user) || $object->fk_user < 0)
114  {
115  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Employee")), null, 'errors');
116  $error++;
117  }
118  if (empty($type_payment) || $type_payment < 0)
119  {
120  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PaymentMode")), null, 'errors');
121  $error++;
122  }
123  if (empty($object->amount))
124  {
125  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Amount")), null, 'errors');
126  $error++;
127  }
128  if (! empty($conf->banque->enabled) && ! $object->accountid > 0)
129  {
130  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
131  $error++;
132  }
133 
134  if (! $error)
135  {
136  $db->begin();
137 
138  $ret=$object->create($user);
139  if ($ret > 0)
140  {
141  $db->commit();
142  header("Location: list.php");
143  exit;
144  }
145  else
146  {
147  $db->rollback();
148  setEventMessages($object->error, $object->errors, 'errors');
149  $action="create";
150  }
151  }
152 
153  $action='create';
154 }
155 
156 if ($action == 'delete')
157 {
158  $result=$object->fetch($id);
159 
160  if ($object->rappro == 0)
161  {
162  $db->begin();
163 
164  $ret=$object->delete($user);
165  if ($ret > 0)
166  {
167  if ($object->fk_bank)
168  {
169  $accountline=new AccountLine($db);
170  $result=$accountline->fetch($object->fk_bank);
171  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)
172  }
173 
174  if ($result >= 0)
175  {
176  $db->commit();
177  header("Location: ".DOL_URL_ROOT.'/compta/salaries/list.php');
178  exit;
179  }
180  else
181  {
182  $object->error=$accountline->error;
183  $db->rollback();
184  setEventMessages($object->error, $object->errors, 'errors');
185  }
186  }
187  else
188  {
189  $db->rollback();
190  setEventMessages($object->error, $object->errors, 'errors');
191  }
192  }
193  else
194  {
195  setEventMessages('Error try do delete a line linked to a conciliated bank transaction', null, 'errors');
196  }
197 }
198 
199 
200 /*
201  * View
202  */
203 
204 llxHeader("",$langs->trans("SalaryPayment"));
205 
206 $form = new Form($db);
207 if (! empty($conf->projet->enabled)) $formproject = new FormProjets($db);
208 
209 if ($id)
210 {
211  $object = new PaymentSalary($db);
212  $result = $object->fetch($id);
213  if ($result <= 0)
214  {
215  dol_print_error($db);
216  exit;
217  }
218 }
219 
220 // Create
221 if ($action == 'create')
222 {
223  $year_current = strftime("%Y",dol_now());
224  $pastmonth = strftime("%m",dol_now()) - 1;
225  $pastmonthyear = $year_current;
226  if ($pastmonth == 0)
227  {
228  $pastmonth = 12;
229  $pastmonthyear--;
230  }
231 
232  $datespmonth = GETPOST('datespmonth', 'int');
233  $datespday = GETPOST('datespday', 'int');
234  $datespyear = GETPOST('datespyear', 'int');
235  $dateepmonth = GETPOST('dateepmonth', 'int');
236  $dateepday = GETPOST('dateepday', 'int');
237  $dateepyear = GETPOST('dateepyear', 'int');
238  $datesp=dol_mktime(0, 0, 0, $datespmonth, $datespday, $datespyear);
239  $dateep=dol_mktime(23, 59, 59, $dateepmonth, $dateepday, $dateepyear);
240 
241  if (empty($datesp) || empty($dateep)) // We define date_start and date_end
242  {
243  $datesp=dol_get_first_day($pastmonthyear,$pastmonth,false); $dateep=dol_get_last_day($pastmonthyear,$pastmonth,false);
244  }
245 
246  print '<form name="salary" action="'.$_SERVER["PHP_SELF"].'" method="post">';
247  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
248  print '<input type="hidden" name="action" value="add">';
249 
250  print load_fiche_titre($langs->trans("NewSalaryPayment"),'', 'title_accountancy.png');
251 
252  dol_fiche_head('', '');
253 
254  print '<table class="border" width="100%">';
255 
256  // Date payment
257  print '<tr><td>';
258  print fieldLabel('DatePayment','datep',1).'</td><td>';
259  print $form->selectDate((empty($datep)?-1:$datep), "datep", '', '', '', 'add', 1, 1);
260  print '</td></tr>';
261 
262  // Date value for bank
263  print '<tr><td>';
264  print fieldLabel('DateValue','datev',0).'</td><td>';
265  print $form->selectDate((empty($datev)?-1:$datev), "datev", '', '', '', 'add', 1, 1);
266  print '</td></tr>';
267 
268  // Employee
269  print '<tr><td>';
270  print fieldLabel('Employee','fk_user',1).'</td><td>';
271  $noactive=0; // We keep active and unactive users
272  print $form->select_dolusers(GETPOST('fk_user','int'), 'fk_user', 1, '', 0, '', '', 0, 0, 0, 'AND employee=1', 0, '', 'maxwidth300', $noactive);
273  print '</td></tr>';
274 
275  // Label
276  print '<tr><td>';
277  print fieldLabel('Label','label',1).'</td><td>';
278  print '<input name="label" id="label" class="minwidth300" value="'.(GETPOST("label")?GETPOST("label"):$langs->trans("SalaryPayment")).'">';
279  print '</td></tr>';
280 
281  // Date start period
282  print '<tr><td>';
283  print fieldLabel('DateStartPeriod','datesp',1).'</td><td>';
284  print $form->selectDate($datesp, "datesp", '', '', '', 'add');
285  print '</td></tr>';
286 
287  // Date end period
288  print '<tr><td>';
289  print fieldLabel('DateEndPeriod','dateep',1).'</td><td>';
290  print $form->selectDate($dateep, "dateep", '', '', '', 'add');
291  print '</td></tr>';
292 
293  // Amount
294  print '<tr><td>';
295  print fieldLabel('Amount','amount',1).'</td><td>';
296  print '<input name="amount" id="amount" class="minwidth100" value="'.GETPOST("amount").'">';
297  print '</td></tr>';
298 
299  // Project
300  if (! empty($conf->projet->enabled))
301  {
302  $formproject=new FormProjets($db);
303 
304  print '<tr><td>'.$langs->trans("Project").'</td><td>';
305 
306  $numproject=$formproject->select_projects(-1, $projectid,'fk_project',0,0,1,1);
307 
308  print '</td></tr>';
309  }
310 
311  // Bank
312  if (! empty($conf->banque->enabled))
313  {
314  print '<tr><td>';
315  print fieldLabel('BankAccount','selectaccountid',1).'</td><td>';
316  $form->select_comptes($_POST["accountid"],"accountid",0,'',1); // Affiche liste des comptes courant
317  print '</td></tr>';
318  }
319 
320  // Type payment
321  print '<tr><td>';
322  print fieldLabel('PaymentMode','selectpaymenttype',1).'</td><td>';
323  $form->select_types_paiements(GETPOST("paymenttype"), "paymenttype", '', 2);
324  print '</td></tr>';
325 
326  // Number
327  if (! empty($conf->banque->enabled))
328  {
329  // Number
330  print '<tr><td><label for="num_payment">'.$langs->trans('Numero');
331  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
332  print '</label></td>';
333  print '<td><input name="num_payment" id="num_payment" type="text" value="'.GETPOST("num_payment").'"></td></tr>'."\n";
334  }
335 
336  // Other attributes
337  $parameters=array();
338  $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
339  print $hookmanager->resPrint;
340 
341  print '</table>';
342 
343  dol_fiche_end();
344 
345  print '<div class="center">';
346  print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
347  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
348  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
349  print '</div>';
350 
351  print '</form>';
352 }
353 
354 
355 /* ************************************************************************** */
356 /* */
357 /* View mode */
358 /* */
359 /* ************************************************************************** */
360 
361 if ($id)
362 {
363 
364  $head=salaries_prepare_head($object);
365 
366  dol_fiche_head($head, 'card', $langs->trans("SalaryPayment"), -1, 'payment');
367 
368  $linkback = '<a href="'.DOL_URL_ROOT.'/compta/salaries/list.php?restore_lastsearch_values=1'.(! empty($socid)?'&socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
369 
370  $morehtmlref='<div class="refidno">';
371 
372  // Employee
373  $userstatic=new User($db);
374  $userstatic->fetch($object->fk_user);
375  $morehtmlref.=$langs->trans('Employee') . ' : ' . $userstatic->getNomUrl(1);
376 
377  // Project
378  if (! empty($conf->projet->enabled))
379  {
380  $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
381  if ($user->rights->salaries->write)
382  {
383  if ($action != 'classify')
384  $morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
385  if ($action == 'classify') {
386  //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
387  $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
388  $morehtmlref.='<input type="hidden" name="action" value="classin">';
389  $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
390  $morehtmlref.=$formproject->select_projects(0, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
391  $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
392  $morehtmlref.='</form>';
393  } else {
394  $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
395  }
396  } else {
397  if (! empty($object->fk_project)) {
398  $proj = new Project($db);
399  $proj->fetch($object->fk_project);
400  $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $object->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
401  $morehtmlref.=$proj->ref;
402  $morehtmlref.='</a>';
403  } else {
404  $morehtmlref.='';
405  }
406  }
407  }
408  $morehtmlref.='</div>';
409 
410  dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', '');
411 
412  print '<div class="fichecenter">';
413  print '<div class="underbanner clearboth"></div>';
414 
415  print '<table class="border" width="100%">';
416 
417  // Label
418  print '<tr><td class="titlefield">'.$langs->trans("Label").'</td><td>'.$object->label.'</td></tr>';
419 
420  print "<tr>";
421  print '<td>'.$langs->trans("DateStartPeriod").'</td><td>';
422  print dol_print_date($object->datesp,'day');
423  print '</td></tr>';
424 
425  print '<tr><td>'.$langs->trans("DateEndPeriod").'</td><td>';
426  print dol_print_date($object->dateep,'day');
427  print '</td></tr>';
428 
429  print "<tr>";
430  print '<td>'.$langs->trans("DatePayment").'</td><td>';
431  print dol_print_date($object->datep,'day');
432  print '</td></tr>';
433 
434  print '<tr><td>'.$langs->trans("DateValue").'</td><td>';
435  print dol_print_date($object->datev,'day');
436  print '</td></tr>';
437 
438  print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($object->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
439 
440  if (! empty($conf->banque->enabled))
441  {
442  if ($object->fk_account > 0)
443  {
444  $bankline=new AccountLine($db);
445  $bankline->fetch($object->fk_bank);
446 
447  print '<tr>';
448  print '<td>'.$langs->trans('BankTransactionLine').'</td>';
449  print '<td>';
450  print $bankline->getNomUrl(1,0,'showall');
451  print '</td>';
452  print '</tr>';
453  }
454  }
455 
456  // Other attributes
457  $parameters=array();
458  $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
459  print $hookmanager->resPrint;
460 
461  print '</table>';
462 
463  print '</div>';
464 
465  dol_fiche_end();
466 
467 
468  /*
469  * Action buttons
470  */
471  print '<div class="tabsAction">'."\n";
472  if ($object->rappro == 0)
473  {
474  if (! empty($user->rights->salaries->delete))
475  {
476  print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=delete">'.$langs->trans("Delete").'</a>';
477  }
478  else
479  {
480  print '<a class="butActionRefused" href="#" title="'.(dol_escape_htmltag($langs->trans("NotAllowed"))).'">'.$langs->trans("Delete").'</a>';
481  }
482  }
483  else
484  {
485  print '<a class="butActionRefused" href="#" title="'.$langs->trans("LinkedToAConciliatedTransaction").'">'.$langs->trans("Delete").'</a>';
486  }
487  print "</div>";
488 }
489 
490 // End of page
491 llxFooter();
492 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
Class to manage salary payments.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
img_edit($titlealt='default', $float=0, $other='class="pictoedit"')
Show logo editer/modifier fiche.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Class to manage Dolibarr users.
Definition: user.class.php:41
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:453
Class to manage bank transaction lines.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage generation of HTML components Only common components must be here.
Class to manage projects.
Class to manage building of HTML components.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
dol_now($mode='gmt')
Return date for now.
if($_POST["cancel"]==$langs->trans("Cancel") &&! $id) if($action=='add' && $_POST["cancel"]<> $langs->trans("Cancel")) if($action=='delete') if($id) $form
Actions.
Definition: card.php:153
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
fieldLabel($langkey, $fieldkey, $fieldrequired=0)
Show a string with the label tag dedicated to the HTML edit field.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.
$parameters
Actions.
Definition: card.php:114