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