From be3ca9d5b495f2f9e4fa2d4c0f9eb3a674983c47 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 2 Jul 2005 16:33:39 +0000 Subject: [PATCH] Fix: Pb sur les centimes dans le paiement d'une charge. --- htdocs/chargesociales.class.php | 20 ++-- htdocs/compta/paiement_charge.php | 150 ++++++++++++++++-------------- 2 files changed, 86 insertions(+), 84 deletions(-) diff --git a/htdocs/chargesociales.class.php b/htdocs/chargesociales.class.php index 97a80c54ee8..4b6779ba1ce 100644 --- a/htdocs/chargesociales.class.php +++ b/htdocs/chargesociales.class.php @@ -48,8 +48,9 @@ class PaiementCharge { } /** - * \brief Creation d'un paiement de charge sociale - * + * \brief Creation d'un paiement de charge sociale + * \user user Utilisateur qui crée le paiement + * \return int <0 si erreur, >0 si ok */ function create($user) { $sql_err = 0; @@ -63,14 +64,11 @@ class PaiementCharge { { $facid = $key; $value = trim($value); - $amount = round(ereg_replace(",",".",$value), 2); - - if (is_numeric($amount)) - { - $total += $amount; - } + $amount = round(ereg_replace(",",".",$value), 2); // Un round est ok si nb avec '.' + if (is_numeric($amount)) $total += $amount; } - $total = round(ereg_replace(",",".",$total), 2); + $total = ereg_replace(",",".",$total); + if ($total > 0) { $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount, fk_typepaiement, num_paiement, note, fk_user_creat)"; @@ -78,9 +76,7 @@ class PaiementCharge { if ( $this->db->query($sql) ) { - $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge"); - } else { @@ -90,7 +86,7 @@ class PaiementCharge { } - if ( $total > 0 && $sql_err == 0 ) + if ($total > 0 && $sql_err == 0) { $this->db->commit(); return $this->id; diff --git a/htdocs/compta/paiement_charge.php b/htdocs/compta/paiement_charge.php index d2796e2ce66..5f6bc342074 100755 --- a/htdocs/compta/paiement_charge.php +++ b/htdocs/compta/paiement_charge.php @@ -38,86 +38,92 @@ $chid=isset($_GET["id"])?$_GET["id"]:$_POST["id"]; */ if ($_POST["action"] == 'add_paiement') { - if ($_POST["paiementtype"] > 0) + if ($_POST["paiementtype"] > 0) { - $datepaye = $db->idate(mktime(12, 0 , 0, - $_POST["remonth"], - $_POST["reday"], - $_POST["reyear"])); + $datepaye = $db->idate(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]; - } - } - - // TODO Mettre toute la chaine dans une même transaction - - // 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) + $paiement_id = 0; + $amounts = array(); + foreach ($_POST as $key => $value) { - $chid = $key; - $value = trim($value); - $amount = round(ereg_replace(",",".",$value), 2); - if (is_numeric($amount)) - { - $total += $amount; - } + if (substr($key,0,7) == 'amount_') + { + $other_chid = substr($key,7); + + $amounts[$other_chid] = $_POST[$key]; + } } - - // Insertion dans llx_bank - $label = "Règlement charge"; - $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) { - $paiement->update_fk_bank($bank_line_id); + + $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(ereg_replace(",",".",$value), 2); // Un round est ok si nb avec '.' + if (is_numeric($amount)) $total += $amount; + } + $total = ereg_replace(",",".",$total); + + // Insertion dans llx_bank + $label = "Règlement charge"; + $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) + //{ + // $chid = $key; + // $fac = new Facture($db); + // $fac->fetch($chid); + // $fac->fetch_client(); + // $acc->add_url_line($bank_line_id, $paiement_id, DOL_URL_ROOT.'/compta/paiement/fiche.php?id=', "(paiement)"); + // $acc->add_url_line($bank_line_id, $fac->client->id, DOL_URL_ROOT.'/compta/fiche.php?socid=', $fac->client->nom); + //} + + $db->commit(); + + $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid; + Header("Location: $loc"); + exit; + } + else { + $db->rollback(); + $fiche_erreur_message = "Echec de la création entrée compte: ".$db->error(); + } + } + else + { + $db->rollback(); + $fiche_erreur_message = "Echec de la création du paiement: paiement_id=$paiement_id ".$db->error(); } - - // Mise a jour liens (pour chaque charge concernée par le paiement) - //foreach ($paiement->amounts as $key => $value) - //{ - // $chid = $key; - // $fac = new Facture($db); - // $fac->fetch($chid); - // $fac->fetch_client(); - // $acc->add_url_line($bank_line_id, $paiement_id, DOL_URL_ROOT.'/compta/paiement/fiche.php?id=', "(paiement)"); - // $acc->add_url_line($bank_line_id, $fac->client->id, DOL_URL_ROOT.'/compta/fiche.php?socid=', $fac->client->nom); - //} - - $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid; - Header("Location: $loc"); - } - else - { - // Il y a eu erreur - $fiche_erreur_message = "Echec de la création du paiement: ".$db->error(); - } } - else + else { - $fiche_erreur_message = "Vous devez sélectionner un mode de paiement"; + $fiche_erreur_message = "Vous devez sélectionner un mode de paiement"; } }