Fix: Error management. Payment date is not preselected to avoid errors.

This commit is contained in:
Laurent Destailleur 2008-07-04 10:44:54 +00:00
parent 09ae698e43
commit e3377b1e29

View File

@ -20,11 +20,11 @@
*/ */
/** /**
\file htdocs/compta/paiement.php \file htdocs/compta/paiement.php
\ingroup compta \ingroup compta
\brief Page to create a payment \brief Page to create a payment
\version $Id$ \version $Id$
*/ */
include_once('./pre.inc.php'); include_once('./pre.inc.php');
require_once(DOL_DOCUMENT_ROOT.'/paiement.class.php'); require_once(DOL_DOCUMENT_ROOT.'/paiement.class.php');
@ -47,162 +47,168 @@ $amountsresttopay=array();
$addwarning=0; $addwarning=0;
/* /*
* Action add_paiement et confirm_paiement * Action add_paiement et confirm_paiement
*/ */
if ($_POST['action'] == 'add_paiement' || $_POST['action'] == 'confirm_paiement') if ($_POST['action'] == 'add_paiement' || $_POST['action'] == 'confirm_paiement')
{ {
$error = 0; $error = 0;
$datepaye = dolibarr_mktime(12, 0 , 0, $datepaye = dolibarr_mktime(12, 0 , 0,
$_POST['remonth'], $_POST['remonth'],
$_POST['reday'], $_POST['reday'],
$_POST['reyear']); $_POST['reyear']);
$paiement_id = 0; $paiement_id = 0;
// Verifie si des paiements sont sup<75>rieurs au montant facture // Verifie si des paiements sont sup<75>rieurs au montant facture
foreach ($_POST as $key => $value) foreach ($_POST as $key => $value)
{
if (substr($key,0,7) == 'amount_')
{
$cursorfacid = substr($key,7);
$amounts[$cursorfacid] = price2num($_POST[$key]);
$totalpaiement = $totalpaiement + $amounts[$cursorfacid];
$tmpfacture=new Facture($db);
$tmpfacture->fetch($cursorfacid);
$amountsresttopay[$cursorfacid]=price2num($tmpfacture->total_ttc-$tmpfacture->getSommePaiement());
if ($amounts[$cursorfacid] && $amounts[$cursorfacid] > $amountsresttopay[$cursorfacid])
{
$addwarning=1;
$formquestion['text'] = img_warning($langs->trans("PaymentHigherThanReminderToPay")).' Attention, le montant de paiement pour une ou plusieurs facture est superieur au reste a payer.';
$formquestion['text'].='<br>Corriger votre saisie, sinon, confirmer et penser a creer un avoir du trop percu lors de la fermeture de chacune des factures surpayees.';
}
$formquestion[$i++]=array('type' => 'hidden','name' => $key, 'value' => $_POST[$key]);
}
}
// Effectue les verifications des parametres
if ($_POST['paiementid'] <= 0)
{
$fiche_erreur_message = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('PaymentMode')).'</div>';
$error++;
}
if ($conf->banque->enabled)
{
// Si module bank actif, un compte est obligatoire lors de la saisie
// d'un paiement
if (! $_POST['accountid'])
{ {
if (substr($key,0,7) == 'amount_')
{
$cursorfacid = substr($key,7);
$amounts[$cursorfacid] = price2num($_POST[$key]);
$totalpaiement = $totalpaiement + $amounts[$cursorfacid];
$tmpfacture=new Facture($db);
$tmpfacture->fetch($cursorfacid);
$amountsresttopay[$cursorfacid]=price2num($tmpfacture->total_ttc-$tmpfacture->getSommePaiement());
if ($amounts[$cursorfacid] && $amounts[$cursorfacid] > $amountsresttopay[$cursorfacid])
{
$addwarning=1;
$formquestion['text'] = img_warning($langs->trans("PaymentHigherThanReminderToPay")).' Attention, le montant de paiement pour une ou plusieurs facture est superieur au reste a payer.';
$formquestion['text'].='<br>Corriger votre saisie, sinon, confirmer et penser a creer un avoir du trop percu lors de la fermeture de chacune des factures surpayees.';
}
$formquestion[$i++]=array('type' => 'hidden','name' => $key, 'value' => $_POST[$key]);
}
}
// Effectue les verifications des parametres
if ($_POST['paiementid'] <= 0)
{
$fiche_erreur_message = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('PaymentMode')).'</div>';
$error++;
}
if ($conf->banque->enabled)
{
// Si module bank actif, un compte est obligatoire lors de la saisie
// d'un paiement
if (! $_POST['accountid'])
{
$fiche_erreur_message = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('AccountToCredit')).'</div>'; $fiche_erreur_message = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('AccountToCredit')).'</div>';
$error++; $error++;
}
}
if ($totalpaiement == 0)
{
$fiche_erreur_message = '<div class="error">'.$langs->transnoentities('ErrorFieldRequired',$langs->trans('Amount')).'</div>';
$error++;
}
if (empty($datepaye))
{
$fiche_erreur_message = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('Date')).'</div>';
$error++;
} }
}
if ($totalpaiement == 0)
{
$fiche_erreur_message = '<div class="error">'.$langs->transnoentities('ErrorFieldRequired',$langs->trans('Amount')).'</div>';
$error++;
}
} }
/* /*
* Action add_paiement * Action add_paiement
*/ */
if ($_POST['action'] == 'add_paiement') if ($_POST['action'] == 'add_paiement')
{ {
if ($error) if ($error)
{ {
$_POST['action']=''; $_POST['action']='';
$_GET['action'] = 'create'; $_GET['action'] = 'create';
} }
// Le reste propre a cette action s'affiche en bas de page. // Le reste propre a cette action s'affiche en bas de page.
} }
/* /*
* Action confirm_paiement * Action confirm_paiement
*/ */
if ($_POST['action'] == 'confirm_paiement' && $_POST['confirm'] == 'yes') if ($_POST['action'] == 'confirm_paiement' && $_POST['confirm'] == 'yes')
{ {
if (! $error) if (! $error)
{ {
$db->begin(); $db->begin();
// Creation de la ligne paiement // Creation de la ligne paiement
$paiement = new Paiement($db); $paiement = new Paiement($db);
$paiement->datepaye = $datepaye; $paiement->datepaye = $datepaye;
$paiement->amounts = $amounts; // Tableau de montant $paiement->amounts = $amounts; // Tableau de montant
$paiement->paiementid = $_POST['paiementid']; $paiement->paiementid = $_POST['paiementid'];
$paiement->num_paiement = $_POST['num_paiement']; $paiement->num_paiement = $_POST['num_paiement'];
$paiement->note = $_POST['comment']; $paiement->note = $_POST['comment'];
$paiement_id = $paiement->create($user); $paiement_id = $paiement->create($user);
if ($paiement_id > 0) if ($paiement_id > 0)
{ {
if ($conf->banque->enabled) if ($conf->banque->enabled)
{ {
// Insertion dans llx_bank // Insertion dans llx_bank
$label = "(CustomerInvoicePayment)"; $label = "(CustomerInvoicePayment)";
$acc = new Account($db, $_POST['accountid']); $acc = new Account($db, $_POST['accountid']);
//paiementid contient "CHQ ou VIR par exemple" //paiementid contient "CHQ ou VIR par exemple"
$bank_line_id = $acc->addline($paiement->datepaye, $bank_line_id = $acc->addline($paiement->datepaye,
$paiement->paiementid, $paiement->paiementid,
$label, $label,
$totalpaiement, $totalpaiement,
$paiement->num_paiement, $paiement->num_paiement,
'', '',
$user, $user,
$_POST['chqemetteur'], $_POST['chqemetteur'],
$_POST['chqbank']); $_POST['chqbank']);
// Mise a jour fk_bank dans llx_paiement. // Mise a jour fk_bank dans llx_paiement.
// On connait ainsi le paiement qui a g<>n<EFBFBD>r<EFBFBD> l'<27>criture bancaire // On connait ainsi le paiement qui a g<>n<EFBFBD>r<EFBFBD> l'<27>criture bancaire
if ($bank_line_id > 0) if ($bank_line_id > 0)
{ {
$paiement->update_fk_bank($bank_line_id); $paiement->update_fk_bank($bank_line_id);
// Mise a jour liens (pour chaque facture concern<72>es par le paiement) // Mise a jour liens (pour chaque facture concern<72>es par le paiement)
foreach ($paiement->amounts as $key => $value) foreach ($paiement->amounts as $key => $value)
{ {
$facid = $key; $facid = $key;
$fac = new Facture($db); $fac = new Facture($db);
$fac->fetch($facid); $fac->fetch($facid);
$fac->fetch_client(); $fac->fetch_client();
$acc->add_url_line($bank_line_id, $acc->add_url_line($bank_line_id,
$paiement_id, $paiement_id,
DOL_URL_ROOT.'/compta/paiement/fiche.php?id=', DOL_URL_ROOT.'/compta/paiement/fiche.php?id=',
'(paiement)', '(paiement)',
'payment'); 'payment');
$acc->add_url_line($bank_line_id, $acc->add_url_line($bank_line_id,
$fac->client->id, $fac->client->id,
DOL_URL_ROOT.'/compta/fiche.php?socid=', DOL_URL_ROOT.'/compta/fiche.php?socid=',
$fac->client->nom, $fac->client->nom,
'company'); 'company');
} }
} }
else else
{ {
$error++; $error++;
} }
} }
} }
else else
{ {
$error++; $error++;
} }
if ($error == 0) if ($error == 0)
{ {
$loc = DOL_URL_ROOT.'/compta/paiement/fiche.php?id='.$paiement_id; $loc = DOL_URL_ROOT.'/compta/paiement/fiche.php?id='.$paiement_id;
$db->commit(); $db->commit();
Header('Location: '.$loc); Header('Location: '.$loc);
exit; exit;
} }
else else
{ {
$db->rollback(); $db->rollback();
} }
} }
} }
// S<>curit<69> acc<63>s client // S<>curit<69> acc<63>s client
@ -214,7 +220,7 @@ if ($user->societe_id > 0)
/* /*
* Affichage * View
*/ */
llxHeader(); llxHeader();
@ -227,11 +233,11 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
{ {
$facture = new Facture($db); $facture = new Facture($db);
$result=$facture->fetch($facid); $result=$facture->fetch($facid);
if ($result >= 0) if ($result >= 0)
{ {
$facture->fetch_client(); $facture->fetch_client();
$title=''; $title='';
if ($facture->type != 2) $title.=$langs->trans("EnterPaymentReceivedFromCustomer"); if ($facture->type != 2) $title.=$langs->trans("EnterPaymentReceivedFromCustomer");
if ($facture->type == 2) $title.=$langs->trans("EnterPaymentDueToCustomer"); if ($facture->type == 2) $title.=$langs->trans("EnterPaymentDueToCustomer");
@ -244,12 +250,12 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
llxFooter(); llxFooter();
exit; exit;
} }
// Initialise donnees pour page de confirmation // Initialise donnees pour page de confirmation
if ($_POST["action"] == 'add_paiement') if ($_POST["action"] == 'add_paiement')
{ {
$i=0; $i=0;
$formquestion[$i++]=array('type' => 'hidden','name' => 'facid', 'value' => $facture->id); $formquestion[$i++]=array('type' => 'hidden','name' => 'facid', 'value' => $facture->id);
$formquestion[$i++]=array('type' => 'hidden','name' => 'socid', 'value' => $facture->socid); $formquestion[$i++]=array('type' => 'hidden','name' => 'socid', 'value' => $facture->socid);
$formquestion[$i++]=array('type' => 'hidden','name' => 'type', 'value' => $facture->type); $formquestion[$i++]=array('type' => 'hidden','name' => 'type', 'value' => $facture->type);
@ -268,11 +274,7 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
print '<tr><td>'.$langs->trans('Company').'</td><td colspan="2">'.$facture->client->getNomUrl(4)."</td></tr>\n"; print '<tr><td>'.$langs->trans('Company').'</td><td colspan="2">'.$facture->client->getNomUrl(4)."</td></tr>\n";
print '<tr><td>'.$langs->trans('Date').'</td><td>'; print '<tr><td>'.$langs->trans('Date').'</td><td>';
if (!empty($_POST['remonth']) && !empty($_POST['reday']) && !empty($_POST['reyear'])) $html->select_date(-1,'','','',0,"add_paiement");
$sel_date=mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
else
$sel_date='';
$html->select_date($sel_date,'','','','',"add_paiement");
print '</td>'; print '</td>';
print '<td>'.$langs->trans('Comments').'</td></tr>'; print '<td>'.$langs->trans('Comments').'</td></tr>';
@ -286,15 +288,15 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
print '<tr>'; print '<tr>';
if ($conf->banque->enabled) if ($conf->banque->enabled)
{ {
if ($facture->type != 2) print '<td>'.$langs->trans('AccountToCredit').'</td>'; if ($facture->type != 2) print '<td>'.$langs->trans('AccountToCredit').'</td>';
if ($facture->type == 2) print '<td>'.$langs->trans('AccountToDebit').'</td>'; if ($facture->type == 2) print '<td>'.$langs->trans('AccountToDebit').'</td>';
print '<td>'; print '<td>';
$html->select_comptes(empty($_POST['accountid'])?'':$_POST['accountid'],'accountid',0,'',1); $html->select_comptes(empty($_POST['accountid'])?'':$_POST['accountid'],'accountid',0,'',1);
print '</td>'; print '</td>';
} }
else else
{ {
print '<td colspan="2">&nbsp;</td>'; print '<td colspan="2">&nbsp;</td>';
} }
print "</tr>\n"; print "</tr>\n";
@ -330,7 +332,7 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
} }
else else
{ {
$sql .= ' AND type = 2'; $sql .= ' AND type = 2';
} }
$sql .= ' GROUP BY f.facnumber'; $sql .= ' GROUP BY f.facnumber';
$resql = $db->query($sql); $resql = $db->query($sql);
@ -365,10 +367,10 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
print '<tr '.$bc[$var].'>'; print '<tr '.$bc[$var].'>';
print '<td>'; print '<td>';
$facturestatic->ref=$objp->facnumber; $facturestatic->ref=$objp->facnumber;
$facturestatic->id=$objp->facid; $facturestatic->id=$objp->facid;
$facturestatic->type=$objp->type; $facturestatic->type=$objp->type;
print $facturestatic->getNomUrl(1,''); print $facturestatic->getNomUrl(1,'');
print "</td>\n"; print "</td>\n";
// Date // Date
@ -376,10 +378,10 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
// Prix // Prix
print '<td align="right">'.price($objp->total_ttc).'</td>'; print '<td align="right">'.price($objp->total_ttc).'</td>';
// Recu // Recu
print '<td align="right">'.price($objp->am).'</td>'; print '<td align="right">'.price($objp->am).'</td>';
// Reste a payer // Reste a payer
print '<td align="right">'.price($objp->total_ttc - $objp->am).'</td>'; print '<td align="right">'.price($objp->total_ttc - $objp->am).'</td>';
@ -393,11 +395,11 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
print '<td align="center" width="16">'; print '<td align="center" width="16">';
if ($amounts[$facturestatic->id] && $amounts[$facturestatic->id] > $amountsresttopay[$facturestatic->id]) if ($amounts[$facturestatic->id] && $amounts[$facturestatic->id] > $amountsresttopay[$facturestatic->id])
{ {
print ' '.img_warning($langs->trans("PaymentHigherThanReminderToPay")); print ' '.img_warning($langs->trans("PaymentHigherThanReminderToPay"));
} }
print '</td>'; print '</td>';
print "</tr>\n"; print "</tr>\n";
$total+=$objp->total; $total+=$objp->total;
@ -431,24 +433,24 @@ if ($_GET['action'] == 'create' || $_POST['action'] == 'confirm_paiement' || $_P
{ {
print '<tr><td colspan="3" align="center"><input type="submit" class="button" value="'.$langs->trans('Save').'"></td></tr>'; print '<tr><td colspan="3" align="center"><input type="submit" class="button" value="'.$langs->trans('Save').'"></td></tr>';
} }
print '</table>'; print '</table>';
// Message d'erreur // Message d'erreur
if ($fiche_erreur_message) if ($fiche_erreur_message)
{ {
print '<tr><td colspan="3" align="center">'.$fiche_erreur_message.'</td></tr>'; print $fiche_erreur_message;
} }
// Formulaire confirmation // Formulaire confirmation
if ($_POST["action"] == 'add_paiement') if ($_POST["action"] == 'add_paiement')
{ {
// print '<tr><td colspan="3">'; // print '<tr><td colspan="3">';
print '<br />'; print '<br />';
$text=$langs->trans('ConfirmCustomerPayment',$totalpaiement,$langs->trans("Currency".$conf->monnaie)); $text=$langs->trans('ConfirmCustomerPayment',$totalpaiement,$langs->trans("Currency".$conf->monnaie));
$html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$facture->id.'&socid='.$facture->socid.'&type='.$facture->type,$langs->trans('ReceivedCustomersPayments'),$text,'confirm_paiement',$formquestion); $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$facture->id.'&socid='.$facture->socid.'&type='.$facture->type,$langs->trans('ReceivedCustomersPayments'),$text,'confirm_paiement',$formquestion);
// print '</td></tr>'; // print '</td></tr>';
} }
print "</form>\n"; print "</form>\n";