diff --git a/htdocs/facturefourn.class.php b/htdocs/facturefourn.class.php index 94de20e9363..c934e52f661 100644 --- a/htdocs/facturefourn.class.php +++ b/htdocs/facturefourn.class.php @@ -61,95 +61,6 @@ class FactureFourn $this->lignes = array(); } - /* - * - * - * - */ - Function add_ligne($label, $amount, $tauxtva, $qty=1, $write=0) - { - $i = sizeof($this->lignes); - - $this->lignes[$i][0] = $label; - $this->lignes[$i][1] = $amount; - $this->lignes[$i][2] = $tauxtva; - $this->lignes[$i][3] = $qty; - - if ($write) - { - - for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) - { - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)"; - $sql .= " VALUES ($this->id);"; - if ($this->db->query($sql) ) - { - $idligne = $this->db->last_insert_id(); - - $this->update_ligne($idligne, - $this->lignes[$i][0], - $this->lignes[$i][1], - $this->lignes[$i][2], - $this->lignes[$i][3]); - } - else - { - print $this->db->error(); - } - } - /* - * Mise à jour prix - */ - - $this->updateprice($this->id); - } - } - /* - * - */ - Function update_ligne($id, $label, $puht, $tauxtva, $qty=1) - { - - $puht = ereg_replace(",",".",$puht); - - $totalht = $puht * $qty; - $tva = tva($totalht, $tauxtva); - $totalttc = $totalht + $tva; - - - $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det "; - $sql .= "SET description ='".$label."'"; - $sql .= ", pu_ht = " . $puht; - $sql .= ", qty =".$qty; - $sql .= ", total_ht=".$totalht; - $sql .= ", tva=".$tva; - $sql .= ", tva_taux=".$tauxtva; - $sql .= ", total_ttc=".$totalttc; - - $sql .= " WHERE rowid = $id"; - - if (! $this->db->query($sql) ) - { - print $this->db->error() . '
'.$sql; - } - } - /* - * - */ - Function delete_ligne($id) - { - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_det "; - $sql .= " WHERE rowid = $id"; - - if (! $this->db->query($sql) ) - { - print $this->db->error() . '
'.$sql; - } - $this->updateprice($this->id); - return 1; - } /* * Création d'une facture fournisseur @@ -191,27 +102,34 @@ class FactureFourn { $idligne = $this->db->last_insert_id(); - $this->update_ligne($idligne, + $this->updateline($idligne, $this->lignes[$i][0], $this->lignes[$i][1], $this->lignes[$i][2], $this->lignes[$i][3]); } } - /* - * Mise à jour prix - */ - - $this->updateprice($this->id); - - return $this->id; - } + + /* + * Mise à jour prix + */ + + $this->updateprice($this->id); + + return $this->id; + } else - { - print $this->db->error() . '
'.$sql; - return 0; - } - } + { + if ($this->db->errno() == $this->db->ERROR_DUPLICATE) { + print "Erreur : Une facture possédant cet id existe déjà"; + } + else { + print "Erreur : ".$this->db->error() . '
'.$sql; + } + + return 0; + } + } /* * @@ -347,38 +265,81 @@ class FactureFourn } } - /* - * - * - */ - Function addline($facid, $desc, $pu, $qty) - { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture,description,price,qty) VALUES ($facid, '$desc', $pu, $qty) ;"; - $result = $this->db->query( $sql); - $this->updateprice($facid); + /* + * Ajout ligne facture fourn + */ + Function addline($desc, $pu, $tauxtva, $qty) + { + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)"; + $sql .= " VALUES ($this->id);"; + if ($this->db->query($sql) ) + { + $idligne = $this->db->last_insert_id(); + + $this->updateline($idligne, $desc, $pu, $tauxtva, $qty); + } + else + { + print $this->db->error(); + } + + // Mise a jour prix facture + $this->updateprice($this->id); + } /* - * + * Mise a jour ligne facture fourn * */ - Function updateline($rowid, $desc, $pu, $qty) + Function updateline($id, $label, $puht, $tauxtva, $qty=1) { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture-fourn_det set description='$desc',price=$pu,qty=$qty WHERE rowid = $rowid ;"; - $result = $this->db->query( $sql); - - $this->updateprice($this->id); + $puht = ereg_replace(",",".",$puht); + + $totalht = $puht * $qty; + $tva = tva($totalht, $tauxtva); + $totalttc = $totalht + $tva; + + + $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det "; + $sql .= "SET description ='".$label."'"; + $sql .= ", pu_ht = " . $puht; + $sql .= ", qty =".$qty; + $sql .= ", total_ht=".$totalht; + $sql .= ", tva=".$tva; + $sql .= ", tva_taux=".$tauxtva; + $sql .= ", total_ttc=".$totalttc; + + $sql .= " WHERE rowid = $id"; + + if (! $this->db->query($sql) ) + { + print $this->db->error() . '
'.$sql; + } + + // Mise a jour prix facture + $this->updateprice($this->id); } /* - * + * Supprime ligne facture fourn * */ Function deleteline($rowid) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_det WHERE rowid = $rowid;"; - $result = $this->db->query( $sql); + // Supprime ligne + $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_fourn_det "; + $sql .= " WHERE rowid = $rowid"; + + if (! $this->db->query($sql) ) + { + print "Erreur : ".$this->db->error() . '
'.$sql; + } - $this->updateprice($this->id); + // Mise a jour prix facture + $this->updateprice($this->id); + + return 1; } /* * diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php index 2aecfc32e77..a0254386a5f 100644 --- a/htdocs/fourn/facture/fiche.php +++ b/htdocs/fourn/facture/fiche.php @@ -114,7 +114,7 @@ if ($action == 'add') $facid = $facfou->create($user); // Ajout des lignes de factures - if ($facid) { + if ($facid > 0) { for ($i = 1 ; $i < 9 ; $i++) { $label = "label$i"; @@ -122,16 +122,18 @@ if ($action == 'add') $tauxtva = "tauxtva$i"; $qty = "qty$i"; - if (strlen($$label) && $$amount > 0) + if (strlen($$label) > 0 && $$amount > 0) { $atleastoneline=1; - $facfou->add_ligne($$label, $$amount, $$tauxtva, $$qty); + $facfou->addline($$label, $$amount, $$tauxtva, $$qty, 1); } } + + $db->commit(); + } + else { + $db->rollback(); } - - $db->commit(); - } else { $mesg="
Erreur: Un numéro de facture fournisseur est obligatoire.
"; @@ -142,21 +144,16 @@ if ($action == 'del_ligne') { $facfou = new FactureFourn($db,"",$facid); - if ($facfou->delete_ligne($ligne_id)) - { - $action="edit"; - } + $facfou->deleteline($ligne_id); + + $action="edit"; } if ($action == 'add_ligne') { $facfou = new FactureFourn($db,"", $facid); - $facfou->add_ligne($_POST["label"], - $_POST["amount"], - $_POST["tauxtva"], - $_POST["qty"], - 1); + $facfou->addline($_POST["label"], $_POST["amount"], $_POST["tauxtva"], $_POST["qty"]); $action="edit"; } diff --git a/htdocs/fourn/facture/pre.inc.php b/htdocs/fourn/facture/pre.inc.php index 8e1960bbc97..be924a09672 100644 --- a/htdocs/fourn/facture/pre.inc.php +++ b/htdocs/fourn/facture/pre.inc.php @@ -39,7 +39,7 @@ function llxHeader($head = "", $urlp = "") { if ($user->societe_id == 0) { - $menu->add_submenu(DOL_URL_ROOT."/soc.php?action=create","Nouvelle société"); + $menu->add_submenu(DOL_URL_ROOT."/soc.php?action=create&type=f","Nouveau"); } diff --git a/htdocs/fourn/pre.inc.php b/htdocs/fourn/pre.inc.php index 1fa62848735..c8d329ab1da 100644 --- a/htdocs/fourn/pre.inc.php +++ b/htdocs/fourn/pre.inc.php @@ -40,7 +40,7 @@ function llxHeader($head = "", $urlp = "") */ if ($user->societe_id == 0) { - $menu->add_submenu(DOL_URL_ROOT."/soc.php?action=create","Nouvelle société"); + $menu->add_submenu(DOL_URL_ROOT."/soc.php?action=create&type=f","Nouveau"); } $menu->add_submenu("contact.php","Contacts"); diff --git a/htdocs/lib/mysql.lib.php b/htdocs/lib/mysql.lib.php index a71a1088cba..733b525bbfb 100644 --- a/htdocs/lib/mysql.lib.php +++ b/htdocs/lib/mysql.lib.php @@ -25,6 +25,10 @@ class DoliDb { var $db, $results, $ok, $connected, $database_selected; + // Constantes pour code erreurs + var $ERROR_DUPLICATE=1062; + var $ERROR_TABLEEXISTS=1050; + Function DoliDb($type = 'mysql', $host = '', $user = '', $pass = '', $name = '') // Se connecte au serveur et éventuellement à une base (si spécifié) // Renvoie 1 en cas de succès, 0 sinon @@ -140,6 +144,8 @@ class DoliDb { return mysql_close($this->db); } + + // Start transaction Function begin($do=1) { if ($do) @@ -152,6 +158,7 @@ class DoliDb { } } + // Commit transaction Function commit($do=1) { if ($do) @@ -164,6 +171,7 @@ class DoliDb { } } + // Rollback transaction Function rollback($do=1) { if ($do) @@ -260,8 +268,8 @@ class DoliDb { Function errno() { - // 1050 Table already exists - // 1062 Duplicate key + // $ERROR_DUPLICATE=1062; + // $ERROR_TABLEEXISTS=1050; return mysql_errno($this->db); } diff --git a/htdocs/soc.php b/htdocs/soc.php index 6581be83a9b..6bb92412df0 100644 --- a/htdocs/soc.php +++ b/htdocs/soc.php @@ -79,6 +79,10 @@ if ($action == 'create') * Fiche societe en mode création */ $soc = new Societe($db); + if ($_GET["type"]=='f') { $soc->fournisseur=1; } + if ($_GET["type"]=='c') { $soc->client=1; } + if ($_GET["type"]=='p') { $soc->client=2; } + print '
Nouvelle société (prospect, client, fournisseur)

'; print '
'; print ''; @@ -126,9 +130,9 @@ if ($action == 'create') print ''; print 'Prospect / Client'; print 'Fournisseur'; - if ($soc->client == 2) - { - print ''; - print ''; - print ''; - } - elseif ($soc->client == 1) - { - print ''; - print ''; - print ''; - } - else - { - print ''; - print ''; - print ''; - } - + print ''; + print ''; + print ''; print ''; print 'Fournisseur