Fix: Payment amount was lost or with wrong sign

This commit is contained in:
Laurent Destailleur 2010-12-27 18:46:45 +00:00
parent 885f5d47a2
commit 1f8ce55a43
6 changed files with 130 additions and 107 deletions

View File

@ -21,8 +21,7 @@
/** /**
* \file htdocs/compta/paiement/class/paiement.class.php * \file htdocs/compta/paiement/class/paiement.class.php
* \ingroup facture * \ingroup facture
* \brief Fichier de la classe des paiement de factures clients * \brief File of class to manage payments of customers invoices
* \remarks Cette classe est presque identique a paiementfourn.class.php
* \version $Id$ * \version $Id$
*/ */
@ -41,8 +40,9 @@ class Paiement
var $ref; var $ref;
var $facid; var $facid;
var $datepaye; var $datepaye;
var $amount; var $total; // deprecated
var $total; var $amount; // Total amount of payment
var $amounts=array(); // Array of amounts
var $author; var $author;
var $paiementid; // Type de paiement. Stocke dans fk_paiement var $paiementid; // Type de paiement. Stocke dans fk_paiement
// de llx_paiement qui est lie aux types de // de llx_paiement qui est lie aux types de
@ -93,7 +93,8 @@ class Paiement
$this->date = $this->db->jdate($obj->dp); $this->date = $this->db->jdate($obj->dp);
$this->datepaye = $this->db->jdate($obj->dp); $this->datepaye = $this->db->jdate($obj->dp);
$this->numero = $obj->num_paiement; $this->numero = $obj->num_paiement;
$this->montant = $obj->amount; $this->montant = $obj->amount; // deprecated
$this->amount = $obj->amount;
$this->note = $obj->note; $this->note = $obj->note;
$this->type_libelle = $obj->type_libelle; $this->type_libelle = $obj->type_libelle;
$this->type_code = $obj->type_code; $this->type_code = $obj->type_code;
@ -130,26 +131,26 @@ class Paiement
$error = 0; $error = 0;
// Clean parameters $now=dol_now();
$now=dol_now();
$this->total = 0; // Clean parameters
$totalamount = 0;
foreach ($this->amounts as $key => $value) // How payment is dispatch foreach ($this->amounts as $key => $value) // How payment is dispatch
{ {
$value = price2num($value,'MT'); $newvalue = price2num($value,'MT');
$this->amounts[$key] = $value; $this->amounts[$key] = $newvalue;
$this->total += $value; $totalamount += $newvalue;
} }
$this->total = price2num($this->total); $totalamount = price2num($totalamount);
// Check parameters // Check parameters
if ($this->total == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
$this->db->begin(); $this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (datec, datep, amount, fk_paiement, num_paiement, note, fk_user_creat)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement (datec, datep, amount, fk_paiement, num_paiement, note, fk_user_creat)";
$sql.= " VALUES ('".$this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', '".$this->total."', ".$this->paiementid.", '".$this->num_paiement."', '".addslashes($this->note)."', ".$user->id.")"; $sql.= " VALUES ('".$this->db->idate($now)."', '".$this->db->idate($this->datepaye)."', '".$totalamount."', ".$this->paiementid.", '".$this->num_paiement."', '".addslashes($this->note)."', ".$user->id.")";
dol_syslog(get_class($this)."::Create insert paiement sql=".$sql); dol_syslog(get_class($this)."::Create insert paiement sql=".$sql);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
@ -201,6 +202,8 @@ class Paiement
if (! $error) if (! $error)
{ {
$this->amount=$totalamount;
$this->total=$totalamount; // deprecated
$this->db->commit(); $this->db->commit();
return $this->id; return $this->id;
} }
@ -325,15 +328,16 @@ class Paiement
$acc = new Account($this->db); $acc = new Account($this->db);
$acc->fetch($accountid); $acc->fetch($accountid);
$total=$this->total; $totalamount=$this->amount;
if ($mode == 'payment') $total=$this->total; if (empty($totalamount)) $totalamount=$this->total; // For backward compatibility
if ($mode == 'payment_supplier') $total=-$total; if ($mode == 'payment') $totalamount=$totalamount;
if ($mode == 'payment_supplier') $totalamount=-$totalamount;
// Insert payment into llx_bank // Insert payment into llx_bank
$bank_line_id = $acc->addline($this->datepaye, $bank_line_id = $acc->addline($this->datepaye,
$this->paiementid, // Payment mode id or code ("CHQ or VIR for example") $this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
$label, $label,
$total, $totalamount,
$this->num_paiement, $this->num_paiement,
'', '',
$user, $user,

View File

@ -42,9 +42,9 @@ if ($user->societe_id > 0)
/* /*
* Actions ajoute paiement * Actions add payment
*/ */
if ($_POST["action"] == 'add_paiement') if ($_POST["action"] == 'add_payment')
{ {
$error=0; $error=0;
@ -78,7 +78,6 @@ if ($_POST["action"] == 'add_paiement')
$paymentid = 0; $paymentid = 0;
// Read possible payments // Read possible payments
// FIXME add error message if no payment is defined
foreach ($_POST as $key => $value) foreach ($_POST as $key => $value)
{ {
if (substr($key,0,7) == 'amount_') if (substr($key,0,7) == 'amount_')
@ -88,47 +87,56 @@ if ($_POST["action"] == 'add_paiement')
} }
} }
$db->begin(); if (sizeof($amounts) <= 0)
{
// Create a line of payments $error++;
$paiement = new PaymentSocialContribution($db); $errmsg='ErrorNoPaymentDefined';
$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"];
if (! $error)
{
$paymentid = $paiement->create($user);
if ($paymentid < 0)
{
$errmsg=$paiement->error;
$error++;
}
}
if (! $error) if (! $error)
{ {
$result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)',$_POST['accountid'],'',''); $db->begin();
if (! $result > 0)
{
$errmsg=$paiement->error;
$error++;
}
}
if (! $error) // Create a line of payments
{ $paiement = new PaymentSocialContribution($db);
$db->commit(); $paiement->chid = $chid;
$loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid; $paiement->datepaye = $datepaye;
Header('Location: '.$loc); $paiement->amounts = $amounts; // Tableau de montant
exit; $paiement->paiementtype = $_POST["paiementtype"];
} $paiement->num_paiement = $_POST["num_paiement"];
else $paiement->note = $_POST["note"];
{
$db->rollback(); if (! $error)
{
$paymentid = $paiement->create($user);
if ($paymentid < 0)
{
$errmsg=$paiement->error;
$error++;
}
}
if (! $error)
{
$result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)',$_POST['accountid'],'','');
if (! $result > 0)
{
$errmsg=$paiement->error;
$error++;
}
}
if (! $error)
{
$db->commit();
$loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
Header('Location: '.$loc);
exit;
}
else
{
$db->rollback();
}
} }
} }
@ -164,10 +172,10 @@ if ($_GET["action"] == 'create')
print "<div class=\"error\">$mesg</div>"; print "<div class=\"error\">$mesg</div>";
} }
print '<form name="add_paiement" action="paiement_charge.php" method="post">'; print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'; print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print "<input type=\"hidden\" name=\"id\" value=\"$charge->id\">"; print '<input type="hidden" name="id" value="'.$charge->id.'">';
print '<input type="hidden" name="action" value="add_paiement">'; print '<input type="hidden" name="action" value="add_payment">';
print '<table cellspacing="0" class="border" width="100%" cellpadding="2">'; print '<table cellspacing="0" class="border" width="100%" cellpadding="2">';
@ -202,7 +210,7 @@ if ($_GET["action"] == 'create')
print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>'; print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
$datepaye = dol_mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]); $datepaye = dol_mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
$datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaye):0; $datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaye):0;
$html->select_date($datepayment,'','','','',"add_paiement",1,1); $html->select_date($datepayment,'','','','',"add_payment",1,1);
print "</td>"; print "</td>";
print '<td>'.$langs->trans("Comments").'</td></tr>'; print '<td>'.$langs->trans("Comments").'</td></tr>';
@ -213,7 +221,7 @@ if ($_GET["action"] == 'create')
print '<td rowspan="3" valign="top"><textarea name="comment" wrap="soft" cols="40" rows="'.ROWS_3.'"></textarea></td></tr>'; print '<td rowspan="3" valign="top"><textarea name="comment" wrap="soft" cols="40" rows="'.ROWS_3.'"></textarea></td></tr>';
print '<tr>'; print '<tr>';
print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>'; print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
print '<td>'; print '<td>';
$html->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$charge->accountid, "accountid", 0, "courant=1",1); // Affiche liste des comptes courant $html->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$charge->accountid, "accountid", 0, "courant=1",1); // Affiche liste des comptes courant
print '</td></tr>'; print '</td></tr>';

View File

@ -308,7 +308,7 @@ if ($chid > 0)
// Type // Type
print "<tr><td>".$langs->trans("Type")."</td><td>".$cha->type_libelle."</td><td>".$langs->trans("Payments")."</td></tr>"; print "<tr><td>".$langs->trans("Type")."</td><td>".$cha->type_libelle."</td><td>".$langs->trans("Payments")."</td></tr>";
// Period en date // Period end date
print "<tr><td>".$langs->trans("PeriodEndDate")."</td>"; print "<tr><td>".$langs->trans("PeriodEndDate")."</td>";
print "<td>"; print "<td>";
if ($_GET['action'] == 'edit') if ($_GET['action'] == 'edit')
@ -399,9 +399,10 @@ if ($chid > 0)
// Amount // Amount
print '<tr><td>'.$langs->trans("AmountTTC").'</td><td>'.price($cha->amount).'</td></tr>'; print '<tr><td>'.$langs->trans("AmountTTC").'</td><td>'.price($cha->amount).'</td></tr>';
// Status
print '<tr><td>'.$langs->trans("Status").'</td><td>'.$cha->getLibStatut(4).'</td></tr>'; print '<tr><td>'.$langs->trans("Status").'</td><td>'.$cha->getLibStatut(4).'</td></tr>';
print '<tr><td coslpan="2">&nbsp;</td></tr>'; print '<tr><td colspan="2">&nbsp;</td></tr>';
if ($_GET['action'] == 'edit') if ($_GET['action'] == 'edit')
{ {

View File

@ -18,9 +18,9 @@
*/ */
/** /**
* \file htdocs/compta/sociales/class/chargesociales.class.php * \file htdocs/compta/sociales/class/paymentsocialcontribution.class.php
* \ingroup facture * \ingroup facture
* \brief Fichier de la classe des charges sociales * \brief File of class to manage payment of social contributions
* \version $Id$ * \version $Id$
*/ */
@ -45,7 +45,8 @@ class PaymentSocialContribution extends CommonObject
var $datec=''; var $datec='';
var $tms=''; var $tms='';
var $datep=''; var $datep='';
var $amount; var $amount; // Total amount of payment
var $amounts=array(); // Array of amounts
var $fk_typepaiement; var $fk_typepaiement;
var $num_paiement; var $num_paiement;
var $note; var $note;
@ -64,9 +65,10 @@ class PaymentSocialContribution extends CommonObject
} }
/** /**
* \brief Creation d'un paiement de charge sociale dans la base * Create payment of social contribution into database.
* \param user Utilisateur qui cree le paiement * Use this->amounts to have list of lines for the payment
* \return int <0 si KO, id du paiement cree si OK * @param user User making payment
* @return int <0 if KO, id of payment if OK
*/ */
function create($user) function create($user)
{ {
@ -74,15 +76,15 @@ class PaymentSocialContribution extends CommonObject
$error=0; $error=0;
// Validation parametres $now=dol_now();
// Validate parametres
if (! $this->datepaye) if (! $this->datepaye)
{ {
$this->error='ErrorBadValueForParameters'; $this->error='ErrorBadValueForParameters';
return -1; return -1;
} }
$now=dol_now();
// Clean parameters // Clean parameters
if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge); if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
if (isset($this->amount)) $this->amount=trim($this->amount); if (isset($this->amount)) $this->amount=trim($this->amount);
@ -93,24 +95,30 @@ class PaymentSocialContribution extends CommonObject
if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat); if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif); if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
$totalamount = 0;
foreach ($this->amounts as $key => $value) // How payment is dispatch
{
$newvalue = price2num($value,'MT');
$this->amounts[$key] = $newvalue;
$totalamount += $newvalue;
}
$totalamount = price2num($totalamount);
// Check parameters
if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
$this->db->begin(); $this->db->begin();
$total=0; if ($totalamount != 0)
foreach ($this->amounts as $key => $value)
{
$amount = price2num(trim($value), 'MT');
$total += $amount;
}
if ($total != 0)
{ {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
$sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)"; $sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
$sql.= " VALUES ($this->chid, '".$this->db->idate($now)."', "; $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
$sql.= " '".$this->db->idate($this->datepaye)."', "; $sql.= " '".$this->db->idate($this->datepaye)."',";
$sql.= price2num($total); $sql.= " ".$totalamount.",";
$sql.= ", ".$this->paiementtype.", '".addslashes($this->num_paiement)."', '".addslashes($this->note)."', ".$user->id.","; $sql.= " ".$this->paiementtype.", '".addslashes($this->num_paiement)."', '".addslashes($this->note)."', ".$user->id.",";
$sql.= "0)"; $sql.= " 0)";
dol_syslog(get_class($this)."::create sql=".$sql); dol_syslog(get_class($this)."::create sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
@ -125,9 +133,11 @@ class PaymentSocialContribution extends CommonObject
} }
if ($total != 0 && ! $error) if ($totalamount != 0 && ! $error)
{ {
$this->db->commit(); $this->amount=$totalamount;
$this->total=$totalamount; // deprecated
$this->db->commit();
return $this->id; return $this->id;
} }
else else
@ -442,7 +452,7 @@ class PaymentSocialContribution extends CommonObject
/** /**
* A record into bank for payment with links between this bank record and invoices of payment. * Add record into bank for payment with links between this bank record and invoices of payment.
* All payment properties must have been set first like after a call to create(). * All payment properties must have been set first like after a call to create().
* @param user Object of user making payment * @param user Object of user making payment
* @param mode 'payment_sc' * @param mode 'payment_sc'

View File

@ -20,19 +20,18 @@
*/ */
/** /**
\file htdocs/fourn/class/paiementfourn.class.php * \file htdocs/fourn/class/paiementfourn.class.php
\ingroup fournisseur, facture * \ingroup fournisseur, facture
\brief Classe paiement fournisseur * \brief File of class to manage payments of suppliers invoices
\remarks Cette classe est presque identique a paiement.class.php * \version $Id$
\version $Id$ */
*/
require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'); require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'); require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php');
/** /**
\class PaiementFourn * \class PaiementFourn
\brief Classe permettant la gestion des paiements des factures fournisseurs * \brief Classe permettant la gestion des paiements des factures fournisseurs
*/ */
class PaiementFourn extends Paiement class PaiementFourn extends Paiement
{ {
var $db; var $db;
@ -44,8 +43,9 @@ class PaiementFourn extends Paiement
var $ref; var $ref;
var $facid; var $facid;
var $datepaye; var $datepaye;
var $amount;
var $total; var $total;
var $amount; // Total amount of payment
var $amounts=array(); // Array of amounts
var $author; var $author;
var $paiementid; // Type de paiement. Stocke dans fk_paiement var $paiementid; // Type de paiement. Stocke dans fk_paiement
// de llx_paiement qui est lie aux types de // de llx_paiement qui est lie aux types de

View File

@ -1192,7 +1192,7 @@ function img_object($alt, $object, $cssclass='')
{ {
global $conf,$langs; global $conf,$langs;
$cssclass = (!empty($cssclass)?'class="'.$cssclass.'"':''); $cssclass = (!empty($cssclass)?' class="'.$cssclass.'"':'');
$path = 'theme/'.$conf->theme; $path = 'theme/'.$conf->theme;
$url = DOL_URL_ROOT; $url = DOL_URL_ROOT;
@ -1204,7 +1204,7 @@ function img_object($alt, $object, $cssclass='')
if (DOL_URL_ROOT_ALT && ! file_exists(DOL_DOCUMENT_ROOT.'/'.$path.'/img/object_'.$object.'.png')) $url = DOL_URL_ROOT_ALT; if (DOL_URL_ROOT_ALT && ! file_exists(DOL_DOCUMENT_ROOT.'/'.$path.'/img/object_'.$object.'.png')) $url = DOL_URL_ROOT_ALT;
} }
return '<img src="'.$url.'/'.$path.'/img/object_'.$object.'.png" border="0" alt="'.dol_escape_htmltag($alt).'" title="'.dol_escape_htmltag($alt).'" '.$cssclass.'>'; return '<img src="'.$url.'/'.$path.'/img/object_'.$object.'.png" border="0" alt="'.dol_escape_htmltag($alt).'" title="'.dol_escape_htmltag($alt).'"'.$cssclass.'>';
} }
/** /**