1<?php
2/* Copyright (C) 2015-2017  Alexandre Spangaro  <aspangaro@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18/**
19 *	    \file       htdocs/expensereport/payment/card.php
20 *		\ingroup    Expense Report
21 *		\brief      Tab payment of an expense report
22 */
23
24require '../../main.inc.php';
25require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
26require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
27require_once DOL_DOCUMENT_ROOT.'/core/modules/expensereport/modules_expensereport.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
29if (!empty($conf->banque->enabled)) {
30	require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
31}
32
33// Load translation files required by the page
34$langs->loadLangs(array('bills', 'banks', 'companies', 'trips'));
35
36$id = GETPOST('rowid') ?GETPOST('rowid', 'int') : GETPOST('id', 'int');
37$action = GETPOST('action', 'aZ09');
38$confirm = GETPOST('confirm');
39
40// Security check
41if ($user->socid) {
42	$socid = $user->socid;
43}
44// TODO Add rule to restrict access payment
45//$result = restrictedArea($user, 'facture', $id,'');
46
47$object = new PaymentExpenseReport($db);
48
49if ($id > 0) {
50	$result = $object->fetch($id);
51	if (!$result) {
52		dol_print_error($db, 'Failed to get payment id '.$id);
53	}
54}
55
56
57/*
58 * Actions
59 */
60
61// Delete payment
62if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->expensereport->supprimer) {
63	$db->begin();
64
65	$result = $object->delete($user);
66	if ($result > 0) {
67		$db->commit();
68		header("Location: ".DOL_URL_ROOT."/expensereport/index.php");
69		exit;
70	} else {
71		setEventMessages($object->error, $object->errors, 'errors');
72		$db->rollback();
73	}
74}
75
76
77/*
78 * View
79 */
80
81llxHeader('', $langs->trans("ExpenseReportPayment"));
82
83$form = new Form($db);
84
85$head = payment_expensereport_prepare_head($object);
86
87print dol_get_fiche_head($head, 'payment', $langs->trans("ExpenseReportPayment"), -1, 'payment');
88
89/*
90 * Confirm deleting of the payment
91 */
92if ($action == 'delete') {
93	print $form->formconfirm('card.php?id='.$object->id, $langs->trans("DeletePayment"), $langs->trans("ConfirmDeletePayment"), 'confirm_delete', '', 0, 2);
94}
95
96$linkback = '';
97// $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
98
99dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '');
100
101print '<div class="fichecenter">';
102print '<div class="underbanner clearboth"></div>';
103
104print '<table class="border centpercent">'."\n";
105
106// Date payment
107print '<tr><td class="titlefield">'.$langs->trans('Date').'</td><td colspan="3">'.dol_print_date($object->datep, 'day').'</td></tr>';
108
109// Mode
110print '<tr><td>'.$langs->trans('PaymentMode').'</td><td colspan="3">'.$langs->trans("PaymentType".$object->type_code).'</td></tr>';
111
112// Number
113print '<tr><td>'.$langs->trans('Numero').'</td><td colspan="3">'.$object->num_payment.'</td></tr>';
114
115// Amount
116print '<tr><td>'.$langs->trans('Amount').'</td><td colspan="3">'.price($object->amount, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
117
118// Note public
119print '<tr><td class="tdtop">'.$langs->trans('Note').'</td><td colspan="3">'.nl2br($object->note_public).'</td></tr>';
120
121$disable_delete = 0;
122// Bank account
123if (!empty($conf->banque->enabled)) {
124	if ($object->bank_account) {
125		$bankline = new AccountLine($db);
126		$bankline->fetch($object->bank_line);
127		if ($bankline->rappro) {
128			$disable_delete = 1;
129			$title_button = dol_escape_htmltag($langs->transnoentitiesnoconv("CantRemoveConciliatedPayment"));
130		}
131
132		print '<tr>';
133		print '<td>'.$langs->trans('BankTransactionLine').'</td>';
134		print '<td colspan="3">';
135		print $bankline->getNomUrl(1, 0, 'showconciliated');
136		print '</td>';
137		print '</tr>';
138
139		print '<tr>';
140		print '<td>'.$langs->trans('BankAccount').'</td>';
141		print '<td colspan="3">';
142		$accountstatic = new Account($db);
143		$accountstatic->fetch($bankline->fk_account);
144		print $accountstatic->getNomUrl(1);
145		print '</td>';
146		print '</tr>';
147	}
148}
149
150print '</table>';
151
152print '</div>';
153
154print dol_get_fiche_end();
155
156
157/*
158 * List of expense report paid
159 */
160
161$sql = 'SELECT er.rowid as eid, er.paid, er.total_ttc, per.amount';
162$sql .= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as per,'.MAIN_DB_PREFIX.'expensereport as er';
163$sql .= ' WHERE per.fk_expensereport = er.rowid';
164$sql .= ' AND er.entity IN ('.getEntity('expensereport').')';
165$sql .= ' AND per.rowid = '.((int) $id);
166
167dol_syslog("expensereport/payment/card.php", LOG_DEBUG);
168$resql = $db->query($sql);
169if ($resql) {
170	$num = $db->num_rows($resql);
171
172	$i = 0;
173	$total = 0;
174	print '<br>';
175
176	print '<div class="div-table-responsive">';
177	print '<table class="noborder centpercent">';
178
179	print '<tr class="liste_titre">';
180	print '<td>'.$langs->trans('ExpenseReport').'</td>';
181	print '<td class="right">'.$langs->trans('ExpectedToPay').'</td>';
182	print '<td class="right">'.$langs->trans('PayedByThisPayment').'</td>';
183	print '<td class="right">'.$langs->trans('RemainderToPay').'</td>';
184	print '<td class="center">'.$langs->trans('Status').'</td>';
185	print "</tr>\n";
186
187	if ($num > 0) {
188		while ($i < $num) {
189			$objp = $db->fetch_object($resql);
190
191			print '<tr class="oddeven">';
192
193			$expensereport = new ExpenseReport($db);
194			$expensereport->fetch($objp->eid);
195
196			// Expense report
197			print '<td>';
198			print $expensereport->getNomUrl(1);
199			print "</td>\n";
200
201			// Expected to pay
202			print '<td class="right">'.price($objp->total_ttc).'</td>';
203
204			// Amount paid
205			print '<td class="right">'.price($objp->amount).'</td>';
206
207			// Remain to pay
208			print '<td class="right">'.price($objp->total_ttc - $objp->amount).'</td>';
209
210			// Status
211			print '<td class="center">'.$expensereport->getLibStatut(4, $objp->amount).'</td>';
212
213			print "</tr>\n";
214
215			if ($objp->paid == 1) {	// If at least one invoice is paid, disable delete
216				$disable_delete = 2;
217				$title_button = $langs->trans("CantRemovePaymentWithOneInvoicePaid");
218			}
219			$total = $total + $objp->amount;
220			$i++;
221		}
222	}
223
224
225	print "</table>\n";
226	print '</div>';
227
228	$db->free($resql);
229} else {
230	dol_print_error($db);
231}
232
233
234
235/*
236 * Actions buttons
237 */
238print '<div class="tabsAction">';
239
240if ($action == '') {
241	if ($user->rights->expensereport->supprimer) {
242		if (!$disable_delete) {
243			print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=delete&amp;token='.newToken().'">'.$langs->trans('Delete').'</a>';
244		} else {
245			print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($title_button).'">'.$langs->trans('Delete').'</a>';
246		}
247	}
248}
249
250print '</div>';
251
252// End of page
253llxFooter();
254$db->close();
255