dolibarr/htdocs/don/payment/payment.php

290 lines
8.2 KiB
PHP
Raw Normal View History

2015-03-18 05:07:32 +01:00
<?php
2019-01-28 21:39:22 +01:00
/* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
2018-09-09 11:25:59 +02:00
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
2015-03-18 05:07:32 +01:00
*
* 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
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2015-03-18 05:07:32 +01:00
*/
/**
2019-12-15 10:02:20 +01:00
* \file htdocs/don/payment/payment.php
2015-03-20 06:36:48 +01:00
* \ingroup donations
2015-03-18 05:07:32 +01:00
* \brief Page to add payment of a donation
*/
2015-03-21 18:00:16 +01:00
require '../../main.inc.php';
2015-03-25 22:37:14 +01:00
require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
2015-03-18 05:07:32 +01:00
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
$langs->load("bills");
2021-03-24 19:53:31 +01:00
$chid = GETPOST("rowid", 'int');
$action = GETPOST('action', 'aZ09');
2015-03-18 05:07:32 +01:00
$amounts = array();
2021-03-24 19:53:31 +01:00
$cancel = GETPOST('cancel');
2015-03-18 05:07:32 +01:00
// Security check
$socid = 0;
if ($user->socid > 0) {
$socid = $user->socid;
2015-03-18 05:07:32 +01:00
}
$object = new Don($db);
2015-03-18 05:07:32 +01:00
/*
* Actions
*/
2021-02-25 22:18:34 +01:00
if ($action == 'add_payment') {
$error = 0;
2015-03-18 05:07:32 +01:00
2021-03-24 19:53:31 +01:00
if ($cancel) {
2015-03-25 22:37:14 +01:00
$loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
2015-03-18 05:07:32 +01:00
header("Location: ".$loc);
exit;
}
2021-03-24 19:53:31 +01:00
$datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
2015-03-18 05:07:32 +01:00
2021-03-24 19:53:31 +01:00
if (!(GETPOST("paymenttype") > 0)) {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
2015-03-18 05:07:32 +01:00
$error++;
}
2021-02-25 22:18:34 +01:00
if ($datepaid == '') {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
2015-03-18 05:07:32 +01:00
$error++;
}
2021-03-24 19:53:31 +01:00
if (!empty($conf->banque->enabled) && !(GETPOST("accountid", 'int') > 0)) {
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
$error++;
}
2015-03-18 05:07:32 +01:00
2021-02-25 22:18:34 +01:00
if (!$error) {
2015-03-18 05:07:32 +01:00
$paymentid = 0;
// Read possible payments
2021-02-25 22:18:34 +01:00
foreach ($_POST as $key => $value) {
if (substr($key, 0, 7) == 'amount_') {
$other_chid = substr($key, 7);
2022-02-22 23:44:56 +01:00
$amounts[$other_chid] = price2num(GETPOST($key));
2015-03-18 05:07:32 +01:00
}
}
2021-02-25 22:18:34 +01:00
if (count($amounts) <= 0) {
$error++;
$errmsg = 'ErrorNoPaymentDefined';
setEventMessages($errmsg, null, 'errors');
}
2021-02-25 22:18:34 +01:00
if (!$error) {
$db->begin();
// Create a line of payments
$payment = new PaymentDonation($db);
$payment->chid = $chid;
$payment->datepaid = $datepaid;
$payment->amounts = $amounts; // Tableau de montant
$payment->paymenttype = GETPOST("paymenttype", 'int');
$payment->num_payment = GETPOST("num_payment", 'alphanohtml');
$payment->note_public = GETPOST("note_public", 'restricthtml');
2021-02-25 22:18:34 +01:00
if (!$error) {
$paymentid = $payment->create($user);
2021-02-25 22:18:34 +01:00
if ($paymentid < 0) {
$errmsg = $payment->error;
setEventMessages($errmsg, null, 'errors');
$error++;
}
}
2021-02-25 22:18:34 +01:00
if (!$error) {
2022-02-22 23:44:56 +01:00
$result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', GETPOST('accountid', 'int'), '', '');
2022-08-12 15:54:50 +02:00
if (!($result > 0)) {
$errmsg = $payment->error;
setEventMessages($errmsg, null, 'errors');
$error++;
}
}
2021-02-25 22:18:34 +01:00
if (!$error) {
$db->commit();
$loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
header('Location: '.$loc);
exit;
} else {
$db->rollback();
}
}
2015-03-18 05:07:32 +01:00
}
$action = 'create';
2015-03-18 05:07:32 +01:00
}
/*
* View
*/
$form = new Form($db);
2015-03-18 05:07:32 +01:00
llxHeader();
$sql = "SELECT sum(p.amount) as total";
$sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
$sql .= " WHERE p.fk_donation = ".((int) $chid);
$resql = $db->query($sql);
2021-02-25 22:18:34 +01:00
if ($resql) {
$obj = $db->fetch_object($resql);
$sumpaid = $obj->total;
2022-05-15 22:29:19 +02:00
$db->free($resql);
}
2015-03-18 05:07:32 +01:00
// Form to create donation payment
2021-02-25 22:18:34 +01:00
if ($action == 'create') {
$object->fetch($chid);
2015-03-18 05:07:32 +01:00
$total = $object->amount;
2015-03-18 05:07:32 +01:00
print load_fiche_titre($langs->trans("DoPayment"));
2015-03-18 05:07:32 +01:00
2021-02-25 22:18:34 +01:00
if (!empty($conf->use_javascript_ajax)) {
print "\n".'<script type="text/javascript">';
2020-10-12 09:42:35 +02:00
//Add js for AutoFill
print ' $(document).ready(function () {';
print ' $(".AutoFillAmout").on(\'click touchstart\', function(){
$("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
});';
print ' });'."\n";
print ' </script>'."\n";
}
2015-03-18 05:07:32 +01:00
print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
print '<input type="hidden" name="token" value="'.newToken().'">';
2015-03-18 05:07:32 +01:00
print '<input type="hidden" name="rowid" value="'.$chid.'">';
print '<input type="hidden" name="chid" value="'.$chid.'">';
print '<input type="hidden" name="action" value="add_payment">';
2018-09-09 11:25:59 +02:00
print dol_get_fiche_head();
2015-03-18 05:07:32 +01:00
print '<table class="border centpercent tableforfieldcreate">';
2015-03-18 05:07:32 +01:00
print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
2021-03-24 19:53:31 +01:00
$datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
$datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaid : -1) : 0;
print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
2015-03-18 05:07:32 +01:00
print "</td>";
print '</tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
2020-02-16 19:33:58 +01:00
$form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->paymenttype, "paymenttype");
2015-03-18 05:07:32 +01:00
print "</td>\n";
print '</tr>';
print '<tr>';
2015-03-18 06:29:10 +01:00
print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
2015-03-18 05:07:32 +01:00
print '<td colspan="2">';
$form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : $object->accountid, "accountid", 0, '', 2); // Show open bank account list
2015-03-18 05:07:32 +01:00
print '</td></tr>';
// Number
print '<tr><td>'.$langs->trans('Numero');
print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
print '</td>';
print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
print '<tr>';
2017-01-22 12:13:32 +01:00
print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
2019-12-12 11:11:08 +01:00
print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
2015-03-18 05:07:32 +01:00
print '</tr>';
print '</table>';
print dol_get_fiche_end();
2015-03-18 05:07:32 +01:00
/*
2021-02-25 22:18:34 +01:00
* List of payments on donation
2015-03-18 05:07:32 +01:00
*/
2015-03-18 05:07:32 +01:00
$num = 1;
$i = 0;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">';
2015-03-18 05:07:32 +01:00
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Donation").'</td>';
2019-03-10 09:00:59 +01:00
print '<td class="right">'.$langs->trans("Amount").'</td>';
print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
print '<td class="center">'.$langs->trans("Amount").'</td>';
2015-03-18 05:07:32 +01:00
print "</tr>\n";
$total = 0;
$totalrecu = 0;
2015-03-18 05:07:32 +01:00
2021-02-25 22:18:34 +01:00
while ($i < $num) {
$objp = $object;
2015-03-18 05:07:32 +01:00
print '<tr class="oddeven">';
2015-03-18 05:07:32 +01:00
print '<td>'.$object->getNomUrl(1)."</td>";
2019-03-10 09:00:59 +01:00
print '<td class="right">'.price($objp->amount)."</td>";
2015-03-18 05:07:32 +01:00
2019-03-10 09:00:59 +01:00
print '<td class="right">'.price($sumpaid)."</td>";
2015-03-18 05:07:32 +01:00
2019-03-10 09:00:59 +01:00
print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
2015-03-18 05:07:32 +01:00
2019-03-10 09:00:59 +01:00
print '<td class="center">';
2021-02-25 22:18:34 +01:00
if ($sumpaid < $objp->amount) {
2015-03-18 05:07:32 +01:00
$namef = "amount_".$objp->id;
2021-02-25 22:18:34 +01:00
if (!empty($conf->use_javascript_ajax)) {
2020-10-12 09:42:35 +02:00
print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
2021-02-25 22:18:34 +01:00
}
2015-03-18 05:07:32 +01:00
print '<input type="text" size="8" name="'.$namef.'">';
2020-05-21 15:05:19 +02:00
} else {
2015-03-18 05:07:32 +01:00
print '-';
}
print "</td>";
print "</tr>\n";
2019-10-25 19:27:42 +02:00
/*$total+=$objp->total;
2015-03-18 05:07:32 +01:00
$total_ttc+=$objp->total_ttc;
2019-10-25 19:27:42 +02:00
$totalrecu+=$objp->am;*/ //Useless code ?
2015-03-18 05:07:32 +01:00
$i++;
}
2019-10-25 19:27:42 +02:00
/*if ($i > 1)
2015-03-18 05:07:32 +01:00
{
// Print total
2018-06-18 10:12:19 +02:00
print '<tr class="oddeven">';
2019-03-10 09:00:59 +01:00
print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
print '<td class="center">&nbsp;</td>';
2015-03-18 05:07:32 +01:00
print "</tr>\n";
2019-10-25 19:27:42 +02:00
}*/ //Useless code ?
2015-03-18 05:07:32 +01:00
print "</table>";
print $form->buttonsSaveCancel();
2015-03-18 05:07:32 +01:00
print "</form>\n";
}
llxFooter();
$db->close();