1<?php
2/* Copyright (C) 2011-2019  Alexandre Spangaro      <aspangaro@open-dsi.fr>
3 * Copyright (C) 2014-2020  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 <https://www.gnu.org/licenses/>.
20 */
21
22/**
23 *  \file       htdocs/salaries/card.php
24 *  \ingroup    salaries
25 *  \brief      Page of salaries payments
26 */
27
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/salaries.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
34if (!empty($conf->projet->enabled))
35{
36	require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
37	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
38}
39
40// Load translation files required by the page
41$langs->loadLangs(array("compta", "banks", "bills", "users", "salaries", "hrm"));
42if (!empty($conf->projet->enabled))	$langs->load("projects");
43
44$id = GETPOST("id", 'int');
45$action = GETPOST('action', 'aZ09');
46$cancel = GETPOST('cancel', 'aZ09');
47$accountid = GETPOST('accountid', 'int') > 0 ? GETPOST('accountid', 'int') : 0;
48$projectid = (GETPOST('projectid', 'int') ? GETPOST('projectid', 'int') : GETPOST('fk_project', 'int'));
49
50$datep = dol_mktime(12, 0, 0, GETPOST("datepmonth", 'int'), GETPOST("datepday", 'int'), GETPOST("datepyear", 'int'));
51$datev = dol_mktime(12, 0, 0, GETPOST("datevmonth", 'int'), GETPOST("datevday", 'int'), GETPOST("datevyear", 'int'));
52$datesp = dol_mktime(12, 0, 0, GETPOST("datespmonth", 'int'), GETPOST("datespday", 'int'), GETPOST("datespyear", 'int'));
53$dateep = dol_mktime(12, 0, 0, GETPOST("dateepmonth", 'int'), GETPOST("dateepday", 'int'), GETPOST("dateepyear", 'int'));
54
55// Security check
56$socid = GETPOST("socid", "int");
57if ($user->socid) $socid = $user->socid;
58$result = restrictedArea($user, 'salaries', '', '', '');
59
60$object = new PaymentSalary($db);
61$extrafields = new ExtraFields($db);
62
63// fetch optionals attributes and labels
64$extrafields->fetch_name_optionals_label($object->table_element);
65
66// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
67$hookmanager->initHooks(array('salarycard', 'globalcard'));
68
69
70/**
71 * Actions
72 */
73
74if ($cancel)
75{
76	header("Location: list.php");
77	exit;
78}
79
80// Link to a project
81if ($action == 'classin' && $user->rights->banque->modifier)
82{
83	$object->fetch($id);
84	$object->setProject($projectid);
85}
86
87if ($action == 'add' && empty($cancel))
88{
89	$error = 0;
90
91	if (empty($datev)) $datev = $datep;
92
93	$type_payment = dol_getIdFromCode($db, GETPOST("paymenttype", 'alpha'), 'c_paiement', 'code', 'id', 1);
94
95	$object->accountid = GETPOST("accountid", 'int') > 0 ? GETPOST("accountid", "int") : 0;
96	$object->fk_user = GETPOST("fk_user", 'int') > 0 ? GETPOST("fk_user", "int") : 0;
97	$object->datev = $datev;
98	$object->datep = $datep;
99	$object->amount = price2num(GETPOST("amount", 'alpha'));
100	$object->label = GETPOST("label", 'alphanohtml');
101	$object->datesp = $datesp;
102	$object->dateep = $dateep;
103	$object->note = GETPOST("note", 'restricthtml');
104	$object->type_payment = ($type_payment > 0 ? $type_payment : 0);
105	$object->num_payment = GETPOST("num_payment", 'alphanohtml');
106	$object->fk_user_author = $user->id;
107	$object->fk_project = $projectid;
108
109	// Set user current salary as ref salary for the payment
110	$fuser = new User($db);
111	$fuser->fetch(GETPOST("fk_user", "int"));
112	$object->salary = $fuser->salary;
113
114	// Fill array 'array_options' with data from add form
115	$ret = $extrafields->setOptionalsFromPost(null, $object);
116	if ($ret < 0) $error++;
117
118	if (empty($datep) || empty($datev) || empty($datesp) || empty($dateep))
119	{
120		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
121		$error++;
122	}
123	if (empty($object->fk_user) || $object->fk_user < 0)
124	{
125		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Employee")), null, 'errors');
126		$error++;
127	}
128	if (empty($type_payment) || $type_payment < 0)
129	{
130		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PaymentMode")), null, 'errors');
131		$error++;
132	}
133	if (empty($object->amount))
134	{
135		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Amount")), null, 'errors');
136		$error++;
137	}
138	if (!empty($conf->banque->enabled) && !$object->accountid > 0)
139	{
140		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
141		$error++;
142	}
143
144	if (!$error)
145	{
146		$db->begin();
147
148		$ret = $object->create($user);
149		if ($ret > 0)
150		{
151			$db->commit();
152
153			if (GETPOST('saveandnew', 'alpha')) {
154				setEventMessages($langs->trans("RecordSaved"), '', 'mesgs');
155				header("Location: card.php?action=create&fk_project=".urlencode($projectid)."&accountid=".urlencode($accountid).'&paymenttype='.urlencode(GETPOST('paymenttype', 'az09')).'&datepday='.GETPOST("datepday", 'int').'&datepmonth='.GETPOST("datepmonth", 'int').'&datepyear='.GETPOST("datepyear", 'int'));
156				exit;
157			} else {
158				header("Location: list.php");
159				exit;
160			}
161		} else {
162			$db->rollback();
163			setEventMessages($object->error, $object->errors, 'errors');
164			$action = "create";
165		}
166	}
167
168	$action = 'create';
169}
170
171if ($action == 'delete')
172{
173	$result = $object->fetch($id);
174
175	if ($object->rappro == 0)
176	{
177		$db->begin();
178
179		$ret = $object->delete($user);
180		if ($ret > 0)
181		{
182			if ($object->fk_bank)
183			{
184				$accountline = new AccountLine($db);
185				$result = $accountline->fetch($object->fk_bank);
186				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)
187			}
188
189			if ($result >= 0)
190			{
191				$db->commit();
192				header("Location: ".DOL_URL_ROOT.'/salaries/list.php');
193				exit;
194			} else {
195				$object->error = $accountline->error;
196				$db->rollback();
197				setEventMessages($object->error, $object->errors, 'errors');
198			}
199		} else {
200			$db->rollback();
201			setEventMessages($object->error, $object->errors, 'errors');
202		}
203	} else {
204		setEventMessages('Error try do delete a line linked to a conciliated bank transaction', null, 'errors');
205	}
206}
207
208
209/*
210 *	View
211 */
212
213llxHeader("", $langs->trans("SalaryPayment"));
214
215$form = new Form($db);
216if (!empty($conf->projet->enabled)) $formproject = new FormProjets($db);
217
218if ($id)
219{
220	$object = new PaymentSalary($db);
221	$result = $object->fetch($id);
222	if ($result <= 0)
223	{
224		dol_print_error($db);
225		exit;
226	}
227}
228
229// Create
230if ($action == 'create')
231{
232	$year_current = strftime("%Y", dol_now());
233	$pastmonth = strftime("%m", dol_now()) - 1;
234	$pastmonthyear = $year_current;
235	if ($pastmonth == 0)
236	{
237		$pastmonth = 12;
238		$pastmonthyear--;
239	}
240
241	$datespmonth = GETPOST('datespmonth', 'int');
242	$datespday = GETPOST('datespday', 'int');
243	$datespyear = GETPOST('datespyear', 'int');
244	$dateepmonth = GETPOST('dateepmonth', 'int');
245	$dateepday = GETPOST('dateepday', 'int');
246	$dateepyear = GETPOST('dateepyear', 'int');
247	$datesp = dol_mktime(0, 0, 0, $datespmonth, $datespday, $datespyear);
248	$dateep = dol_mktime(23, 59, 59, $dateepmonth, $dateepday, $dateepyear);
249
250	if (empty($datesp) || empty($dateep)) // We define date_start and date_end
251	{
252		$datesp = dol_get_first_day($pastmonthyear, $pastmonth, false); $dateep = dol_get_last_day($pastmonthyear, $pastmonth, false);
253	}
254
255	print '<form name="salary" action="'.$_SERVER["PHP_SELF"].'" method="post">';
256	print '<input type="hidden" name="token" value="'.newToken().'">';
257	print '<input type="hidden" name="action" value="add">';
258
259	print load_fiche_titre($langs->trans("NewSalaryPayment"), '', 'object_payment');
260
261	print dol_get_fiche_head('', '');
262
263	print '<table class="border centpercent">';
264
265	// Date payment
266	print '<tr><td>';
267	print $form->editfieldkey('DatePayment', 'datep', '', $object, 0, 'string', '', 1).'</td><td>';
268	print $form->selectDate((empty($datep) ? '' : $datep), "datep", 0, 0, 0, 'add', 1, 1);
269	print '</td></tr>';
270
271	// Date value for bank
272	print '<tr><td>';
273	print $form->editfieldkey('DateValue', 'datev', '', $object, 0).'</td><td>';
274	print $form->selectDate((empty($datev) ?-1 : $datev), "datev", '', '', '', 'add', 1, 1);
275	print '</td></tr>';
276
277	// Employee
278	print '<tr><td>';
279	print $form->editfieldkey('Employee', 'fk_user', '', $object, 0, 'string', '', 1).'</td><td>';
280	$noactive = 0; // We keep active and unactive users
281	print $form->select_dolusers(GETPOST('fk_user', 'int'), 'fk_user', 1, '', 0, '', '', 0, 0, 0, 'AND employee=1', 0, '', 'maxwidth300', $noactive);
282	print '</td></tr>';
283
284	// Label
285	print '<tr><td>';
286	print $form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).'</td><td>';
287	print '<input name="label" id="label" class="minwidth300" value="'.(GETPOST("label") ?GETPOST("label") : $langs->trans("SalaryPayment")).'">';
288	print '</td></tr>';
289
290	// Date start period
291	print '<tr><td>';
292	print $form->editfieldkey('DateStartPeriod', 'datesp', '', $object, 0, 'string', '', 1).'</td><td>';
293	print $form->selectDate($datesp, "datesp", '', '', '', 'add');
294	print '</td></tr>';
295
296	// Date end period
297	print '<tr><td>';
298	print $form->editfieldkey('DateEndPeriod', 'dateep', '', $object, 0, 'string', '', 1).'</td><td>';
299	print $form->selectDate($dateep, "dateep", '', '', '', 'add');
300	print '</td></tr>';
301
302	// Amount
303	print '<tr><td>';
304	print $form->editfieldkey('Amount', 'amount', '', $object, 0, 'string', '', 1).'</td><td>';
305	print '<input name="amount" id="amount" class="minwidth100" value="'.GETPOST("amount").'">';
306	print '</td></tr>';
307
308	// Bank
309	if (!empty($conf->banque->enabled))
310	{
311		print '<tr><td>';
312		print $form->editfieldkey('BankAccount', 'selectaccountid', '', $object, 0, 'string', '', 1).'</td><td>';
313		$form->select_comptes($accountid, "accountid", 0, '', 1); // Affiche liste des comptes courant
314		print '</td></tr>';
315	}
316
317	// Type payment
318	print '<tr><td>';
319	print $form->editfieldkey('PaymentMode', 'selectpaymenttype', '', $object, 0, 'string', '', 1).'</td><td>';
320	$form->select_types_paiements(GETPOST("paymenttype", 'aZ09'), "paymenttype", '', 2);
321	print '</td></tr>';
322
323	// Number
324	if (!empty($conf->banque->enabled))
325	{
326		// Number
327		print '<tr><td><label for="num_payment">'.$langs->trans('Numero');
328		print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
329		print '</label></td>';
330		print '<td><input name="num_payment" id="num_payment" type="text" value="'.GETPOST("num_payment").'"></td></tr>'."\n";
331	}
332
333	// Project
334	if (!empty($conf->projet->enabled))
335	{
336	    $formproject = new FormProjets($db);
337
338	    print '<tr><td>'.$langs->trans("Project").'</td><td>';
339	    $formproject->select_projects(-1, $projectid, 'fk_project', 0, 0, 1, 1);
340	    print '</td></tr>';
341	}
342
343	// Other attributes
344	$parameters = array();
345	$reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
346	print $hookmanager->resPrint;
347	if (empty($reshook))
348	{
349		print $object->showOptionals($extrafields, 'edit');
350	}
351
352	print '</table>';
353
354	print dol_get_fiche_end();
355
356	print '<div class="center">';
357	print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
358	print '&nbsp;&nbsp; &nbsp;&nbsp;';
359	print '<input type="submit" class="button" name="saveandnew" value="'.$langs->trans("SaveAndNew").'">';
360	print '&nbsp;&nbsp; &nbsp;&nbsp;';
361	print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
362	print '</div>';
363
364	print '</form>';
365}
366
367
368/* ************************************************************************** */
369/*                                                                            */
370/* View mode                                                                  */
371/*                                                                            */
372/* ************************************************************************** */
373
374if ($id)
375{
376	$head = salaries_prepare_head($object);
377
378	print dol_get_fiche_head($head, 'card', $langs->trans("SalaryPayment"), -1, 'payment');
379
380	$linkback = '<a href="'.DOL_URL_ROOT.'/salaries/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
381
382	$morehtmlref = '<div class="refidno">';
383
384	// Employee
385	$userstatic = new User($db);
386	$userstatic->fetch($object->fk_user);
387	$morehtmlref .= $langs->trans('Employee').' : '.$userstatic->getNomUrl(1);
388
389	// Project
390	if (!empty($conf->projet->enabled))
391	{
392		$morehtmlref .= '<br>'.$langs->trans('Project').' ';
393		if ($user->rights->salaries->write)
394		{
395			if ($action != 'classify') {
396				$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&amp;id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
397			}
398			if ($action == 'classify') {
399				//$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
400				$morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
401				$morehtmlref .= '<input type="hidden" name="action" value="classin">';
402				$morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
403				$morehtmlref .= $formproject->select_projects(0, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
404				$morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
405				$morehtmlref .= '</form>';
406			} else {
407				$morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
408			}
409		} else {
410			if (!empty($object->fk_project)) {
411				$proj = new Project($db);
412				$proj->fetch($object->fk_project);
413				$morehtmlref .= '<a href="'.DOL_URL_ROOT.'/projet/card.php?id='.$object->fk_project.'" title="'.$langs->trans('ShowProject').'">';
414				$morehtmlref .= $proj->ref;
415				$morehtmlref .= '</a>';
416			} else {
417				$morehtmlref .= '';
418			}
419		}
420	}
421	$morehtmlref .= '</div>';
422
423	dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', '');
424
425	print '<div class="fichecenter">';
426	print '<div class="underbanner clearboth"></div>';
427
428	print '<table class="border centpercent">';
429
430	// Label
431	print '<tr><td class="titlefield">'.$langs->trans("Label").'</td><td>'.$object->label.'</td></tr>';
432
433	print "<tr>";
434	print '<td>'.$langs->trans("DateStartPeriod").'</td><td>';
435	print dol_print_date($object->datesp, 'day');
436	print '</td></tr>';
437
438	print '<tr><td>'.$langs->trans("DateEndPeriod").'</td><td>';
439	print dol_print_date($object->dateep, 'day');
440	print '</td></tr>';
441
442	print "<tr>";
443	print '<td>'.$langs->trans("DatePayment").'</td><td>';
444	print dol_print_date($object->datep, 'day');
445	print '</td></tr>';
446
447	print '<tr><td>'.$langs->trans("DateValue").'</td><td>';
448	print dol_print_date($object->datev, 'day');
449	print '</td></tr>';
450
451	print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($object->amount, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
452
453	if (!empty($conf->banque->enabled))
454	{
455		if ($object->fk_account > 0)
456		{
457			$bankline = new AccountLine($db);
458			$bankline->fetch($object->fk_bank);
459
460			print '<tr>';
461			print '<td>'.$langs->trans('BankTransactionLine').'</td>';
462			print '<td>';
463			print $bankline->getNomUrl(1, 0, 'showall');
464			print '</td>';
465			print '</tr>';
466		}
467	}
468
469	// Other attributes
470	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
471
472	print '</table>';
473
474	print '</div>';
475
476	print dol_get_fiche_end();
477
478
479	// Action buttons
480
481	print '<div class="tabsAction">'."\n";
482	if ($object->rappro == 0)
483	{
484		if (!empty($user->rights->salaries->delete))
485		{
486			print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=delete&token='.newToken().'">'.$langs->trans("Delete").'</a></div>';
487		} else {
488			print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="'.(dol_escape_htmltag($langs->trans("NotAllowed"))).'">'.$langs->trans("Delete").'</a></div>';
489		}
490	} else {
491		print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("LinkedToAConciliatedTransaction").'">'.$langs->trans("Delete").'</a></div>';
492	}
493	print "</div>";
494}
495
496// End of page
497llxFooter();
498$db->close();
499