* Copyright (C) 2015 Laurent Destailleur * Copyright (C) 2018 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/expensereport/payment/payment.php * \ingroup Expense Report * \brief Page to add payment of an expense report */ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php'; require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; // Load translation files required by the page $langs->loadLangs(array('bills', 'banks', 'trips')); $id = GETPOST("id", 'int'); $ref = GETPOST('ref', 'alpha'); $action = GETPOST('action', 'aZ09'); $amounts = array(); $accountid = GETPOST('accountid', 'int'); $cancel = GETPOST('cancel'); // Security check $socid = 0; if ($user->socid > 0) { $socid = $user->socid; } /* * Actions */ if ($action == 'add_payment') { $error = 0; if ($cancel) { $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id; header("Location: ".$loc); exit; } $expensereport = new ExpenseReport($db); $result = $expensereport->fetch($id, $ref); if (!$result) { $error++; setEventMessages($expensereport->error, $expensereport->errors, 'errors'); } $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int')); if (!(GETPOST("fk_typepayment", 'int') > 0)) { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors'); $error++; } if ($datepaid == '') { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors'); $error++; } if (!empty($conf->banque->enabled) && !($accountid > 0)) { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors'); $error++; } if (!$error) { $paymentid = 0; $total = 0; // Read possible payments foreach ($_POST as $key => $value) { if (substr($key, 0, 7) == 'amount_') { $amounts[$expensereport->fk_user_author] = price2num(GETPOST($key)); $total += price2num(GETPOST($key)); } } if (count($amounts) <= 0) { $error++; $errmsg = 'ErrorNoPaymentDefined'; } if (!$error) { $db->begin(); // Create a line of payments $payment = new PaymentExpenseReport($db); $payment->fk_expensereport = $expensereport->id; $payment->datepaid = $datepaid; $payment->amounts = $amounts; // Tableau de montant $payment->total = $total; $payment->fk_typepayment = GETPOST("fk_typepayment", 'int'); $payment->num_payment = GETPOST("num_payment", 'alphanothtml'); $payment->note_public = GETPOST("note_public", 'restricthtml'); if (!$error) { $paymentid = $payment->create($user); if ($paymentid < 0) { setEventMessages($payment->error, $payment->errors, 'errors'); $error++; } } if (!$error) { $result = $payment->addPaymentToBank($user, 'payment_expensereport', '(ExpenseReportPayment)', $accountid, '', ''); if (!$result > 0) { setEventMessages($payment->error, $payment->errors, 'errors'); $error++; } } if (!$error) { $payment->fetch($paymentid); if ($expensereport->total_ttc - $payment->amount == 0) { $result = $expensereport->setPaid($expensereport->id, $user); if (!$result > 0) { setEventMessages($payment->error, $payment->errors, 'errors'); $error++; } } } if (!$error) { $db->commit(); $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id; header('Location: '.$loc); exit; } else { $db->rollback(); } } } $action = 'create'; } /* * View */ llxHeader(); $form = new Form($db); // Form to create expense report payment if ($action == 'create' || empty($action)) { $expensereport = new ExpenseReport($db); $expensereport->fetch($id, $ref); $total = $expensereport->total_ttc; // autofill remainder amount if (!empty($conf->use_javascript_ajax)) { print "\n".'\n"; } print load_fiche_titre($langs->trans("DoPayment")); print '
'; print ''; print ''; print ''; print ''; print dol_get_fiche_head(null, '0', '', -1); $linkback = ''; // $linkback = '' . $langs->trans("BackToList") . ''; dol_banner_tab($expensereport, 'ref', $linkback, 1, 'ref', 'ref', ''); print '
'; print '
'; print ''."\n"; print ''; print ''; $sql = "SELECT sum(p.amount) as total"; $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p, ".MAIN_DB_PREFIX."expensereport as e"; $sql .= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = ".((int) $id); $sql .= ' AND e.entity IN ('.getEntity('expensereport').')'; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); $sumpaid = $obj->total; $db->free($resql); } print ''; print ''; print '
'.$langs->trans("Period").''.get_date_range($expensereport->date_debut, $expensereport->date_fin, "", $langs, 0).'
'.$langs->trans("Amount").''.price($expensereport->total_ttc, 0, $outputlangs, 1, -1, -1, $conf->currency).'
'.$langs->trans("AlreadyPaid").''.price($sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'
'.$langs->trans("RemainderToPay").''.price($total - $sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'
'; print '
'; print dol_get_fiche_end(); print dol_get_fiche_head(); print ''."\n"; print '"; print ''; print '\n"; print ''; if (!empty($conf->banque->enabled)) { print ''; print ''; print ''; } // Number print ''; print ''."\n"; print ''; print ''; print ''; print ''; print '
'.$langs->trans("Date").''; $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int')); $datepayment = ($datepaid == '' ? (empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : '') : $datepaid); print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1); print "
'.$langs->trans("PaymentMode").''; $form->select_types_paiements(GETPOSTISSET("fk_typepayment") ? GETPOST("fk_typepayment", 'alpha') : $expensereport->fk_c_paiement, "fk_typepayment"); print "
'.$langs->trans('AccountToDebit').''; $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", "int") : $expensereport->accountid, "accountid", 0, '', 2); // Show open bank account list print '
'.$langs->trans('Numero'); print ' ('.$langs->trans("ChequeOrTransferNumber").')'; print '
'.$langs->trans("Comments").'
'; print dol_get_fiche_end(); print '
'; // List of expenses ereport not already paid completely $num = 1; $i = 0; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; $total = 0; $totalrecu = 0; while ($i < $num) { $objp = $expensereport; print ''; print '"; print '"; print '"; print '"; print '"; print "\n"; $total += $objp->total; $total_ttc += $objp->total_ttc; $totalrecu += $objp->am; $i++; } if ($i > 1) { // Print total print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; } print "
'.$langs->trans("ExpenseReport").''.$langs->trans("Amount").''.$langs->trans("AlreadyPaid").''.$langs->trans("RemainderToPay").''.$langs->trans("Amount").'
'.$expensereport->getNomUrl(1)."'.price($objp->total_ttc)."'.price($sumpaid)."'.price($objp->total_ttc - $sumpaid)."'; if ($sumpaid < $objp->total_ttc) { $namef = "amount_".$objp->id; $nameRemain = "remain_".$objp->id; // autofill remainder amount if (!empty($conf->use_javascript_ajax)) { // autofill remainder amount print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->total_ttc - $sumpaid)."'"); // autofill remainder amount } $remaintopay = $objp->total_ttc - $sumpaid; // autofill remainder amount print ''; // autofill remainder amount print ''; } else { print '-'; } print "
'.$langs->trans("Total").':'.price($total_ttc).''.price($totalrecu).''.price($total_ttc - $totalrecu).' 
"; print $form->buttonsSaveCancel(); print "
\n"; } // End of page llxFooter(); $db->close();