*
* 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/compta/loan/payment.php
* \ingroup Loan
* \brief Page to add payment of a loan
*/
require '../../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/compta/loan/class/loan.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/loan/class/paymentloan.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
$langs->load("bills");
$langs->load("loan");
$chid=GETPOST("id");
$action=GETPOST('action');
$amounts = array();
// Security check
$socid=0;
if ($user->societe_id > 0)
{
$socid = $user->societe_id;
}
/*
* Actions
*/
if ($action == 'add_payment')
{
$error=0;
if ($_POST["cancel"])
{
$loc = DOL_URL_ROOT.'/compta/loan/card.php?id='.$chid;
header("Location: ".$loc);
exit;
}
$datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
if (! $_POST["paymenttype"] > 0)
{
$mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
$error++;
}
if ($datepaid == '')
{
$mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("Date"));
$error++;
}
if (! empty($conf->banque->enabled) && ! $_POST["accountid"] > 0)
{
$mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("AccountToCredit"));
$error++;
}
if (! $error)
{
$paymentid = 0;
// Read possible payments
foreach ($_POST as $key => $value)
{
if (substr($key,0,7) == 'amount_')
{
$other_chid = substr($key,7);
$amounts[$other_chid] = price2num($_POST[$key]);
}
}
if (count($amounts) <= 0)
{
$error++;
$errmsg='ErrorNoPaymentDefined';
}
if (! $error)
{
$db->begin();
// Create a line of payments
$payment = new PaymentLoan($db);
$payment->chid = $chid;
$payment->datepaid = $datepaid;
$payment->amounts = $amounts; // Tableau de montant
$payment->paymenttype = $_POST["paymenttype"];
$payment->num_payment = $_POST["num_payment"];
$payment->note = $_POST["note"];
if (! $error)
{
$paymentid = $payment->create($user);
if ($paymentid < 0)
{
$errmsg=$payment->error;
$error++;
}
}
if (! $error)
{
$result=$payment->addPaymentToBank($user,'payment_loan','(LoanPayment)',$_POST['accountid'],'','');
if (! $result > 0)
{
$errmsg=$payment->error;
$error++;
}
}
if (! $error)
{
$db->commit();
$loc = DOL_URL_ROOT.'/compta/loan/card.php?id='.$chid;
header('Location: '.$loc);
exit;
}
else
{
$db->rollback();
}
}
}
$_GET["action"]='create';
}
/*
* View
*/
llxHeader();
$form=new Form($db);
// Form to create loan's payment
if ($_GET["action"] == 'create')
{
$loan = new Loan($db);
$loan->fetch($chid);
$total = $loan->capital;
print_fiche_titre($langs->trans("DoPayment"));
print "
\n";
if ($mesg)
{
print "
$mesg
";
}
print '\n";
}
$db->close();
llxFooter();