*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*/
/**
\file htdocs/compta/paiement_charge.php
\ingroup compta
\brief Page de création d'un paiement d'une charge
\version $Revision$
*/
include_once("./pre.inc.php");
include_once("../chargesociales.class.php");
include_once(DOL_DOCUMENT_ROOT."/compta/bank/account.class.php");
$langs->load("bills");
$chid=isset($_GET["id"])?$_GET["id"]:$_POST["id"];
// Sécurité accés client
if ($user->societe_id > 0)
{
$action = '';
$socid = $user->societe_id;
}
/*
* Actions ajoute paiement
*/
if ($_POST["action"] == 'add_paiement')
{
if ($_POST["cancel"])
{
$loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
Header("Location: $loc");
exit;
}
if ($_POST["paiementtype"] > 0)
{
$datepaye = $db->idate(dolibarr_mktime(12, 0 , 0,
$_POST["remonth"],
$_POST["reday"],
$_POST["reyear"]));
$paiement_id = 0;
$amounts = array();
foreach ($_POST as $key => $value)
{
if (substr($key,0,7) == 'amount_')
{
$other_chid = substr($key,7);
$amounts[$other_chid] = $_POST[$key];
}
}
$db->begin();
// Creation de la ligne paiement
$paiement = new PaiementCharge($db);
$paiement->chid = $chid;
$paiement->datepaye = $datepaye;
$paiement->amounts = $amounts; // Tableau de montant
$paiement->paiementtype = $_POST["paiementtype"];
$paiement->num_paiement = $_POST["num_paiement"];
$paiement->note = $_POST["note"];
$paiement_id = $paiement->create($user);
if ($paiement_id > 0)
{
// On determine le montant total du paiement
$total=0;
foreach ($paiement->amounts as $key => $value)
{
$chid = $key;
$value = trim($value);
$amount = round(price2num($value), 2); // Un round est ok si nb avec '.'
if (is_numeric($amount)) $total += $amount;
}
$total = price2num($total);
// Insertion dans llx_bank
$langs->load("banks");
$label = $langs->trans("SocialContributionPayment");
$acc = new Account($db, $_POST["accountid"]);
$bank_line_id = $acc->addline($paiement->datepaye, $paiement->paiementtype, $label, -abs($total), $paiement->num_paiement, '', $user);
// Mise a jour fk_bank dans llx_paiementcharge. On connait ainsi le paiement qui a généré l'écriture bancaire
if ($bank_line_id > 0)
{
$paiement->update_fk_bank($bank_line_id);
// Mise a jour liens (pour chaque charge concernée par le paiement)
foreach ($paiement->amounts as $key => $value)
{
$acc->add_url_line($bank_line_id, $chid, DOL_URL_ROOT.'/compta/sociales/charges.php?id=', '(socialcontribution)','sc');
}
$db->commit();
$loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
Header("Location: $loc");
exit;
}
else {
$db->rollback();
$mesg = "Echec de la création entrée compte: ".$db->error();
}
}
else
{
$db->rollback();
$mesg = "Echec de la création du paiement: paiement_id=$paiement_id ".$db->error();
}
}
else
{
$mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
}
$_GET["action"]='create';
}
/*
* Affichage
*/
llxHeader();
$html=new Form($db);
/*
* Formulaire de creation d'un paiement de charge
*/
if ($_GET["action"] == 'create')
{
$charge = new ChargeSociales($db);
$charge->fetch($chid);
$total = $charge->amount;
print_titre($langs->trans("DoPayment"));
print "
\n";
if ($mesg)
{
print "